Initial import of zappier-account-balance from zapier monorepo

This commit is contained in:
George Lambert 2026-09-11 19:26:36 -04:00
commit 9165b6f36a
10 changed files with 457 additions and 0 deletions

45
test/books.test.js Normal file
View file

@ -0,0 +1,45 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { AccountBooks, handle } from '../src/books.js';
import { SUBJECTS } from '../src/subjects.js';
import { loadInto, saveFrom, booksPath } from '../src/persist.js';
test('adjust credits prepaid and statement lists credits usage payments', () => {
const books = new AccountBooks();
handle(
SUBJECTS.BALANCE_ADJUST,
{ customerId: 'c1', veraeUserId: 'vu_abc', cents: 500, reason: 'goodwill', agent: 'cs' },
books,
);
handle(SUBJECTS.USAGE_RECORDED, { customerId: 'c1', endpointId: 'timestamp', cents: 4 }, books);
handle(SUBJECTS.PAYMENT_RECORDED, { customerId: 'c1', cents: 1000, reason: 'reload' }, books);
const st = handle(SUBJECTS.STATEMENT_GET, { customerId: 'c1' }, books);
assert.equal(st.prepaidCents, 1496);
assert.equal(st.veraeUserId, 'vu_abc');
assert.equal(st.credits[0].cents, 500);
assert.equal(st.usage[0].endpointId, 'timestamp');
assert.equal(st.payments[0].kind, 'payment');
});
test('customer.put stores display name and lookup by name', () => {
const books = new AccountBooks();
handle(SUBJECTS.CUSTOMER_PUT, { customerId: 'cust_1', name: 'Ada (free)' }, books);
const st = handle(SUBJECTS.STATEMENT_GET, { customerId: 'Ada (free)' }, books);
assert.equal(st.customerId, 'cust_1');
assert.equal(st.name, 'Ada (free)');
});
test('persist round-trip keeps prepaid', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'books-'));
process.env.BOOKS_PATH = path.join(dir, 'books.json');
const a = new AccountBooks();
handle(SUBJECTS.BALANCE_ADJUST, { customerId: 'c9', cents: 120, kind: 'reload' }, a);
saveFrom(a);
assert.ok(fs.existsSync(booksPath()));
const b = loadInto(new AccountBooks());
assert.equal(b.prepaidCents('c9'), 120);
delete process.env.BOOKS_PATH;
});

34
test/health.test.js Normal file
View file

@ -0,0 +1,34 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { spawn } from 'node:child_process';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
test('account-balance health and statement', async () => {
const port = 18010;
const books = path.join(os.tmpdir(), `books-health-${Date.now()}.json`);
const child = spawn(process.execPath, ['src/server.js'], {
cwd: root,
env: { ...process.env, PORT: String(port), BOOKS_PATH: books },
stdio: ['ignore', 'pipe', 'pipe'],
});
await new Promise((r) => setTimeout(r, 400));
try {
const h = await (await fetch(`http://127.0.0.1:${port}/health`)).json();
assert.equal(h.role, 'zappier-account-balance');
await fetch(`http://127.0.0.1:${port}/adjust`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ customerId: 'c1', cents: 200, reason: 'test' }),
});
const st = await (await fetch(`http://127.0.0.1:${port}/statement/c1`)).json();
assert.equal(st.prepaidCents, 200);
} finally {
child.kill('SIGTERM');
fs.rmSync(books, { force: true });
}
});