import { test } from 'node:test'; import assert from 'node:assert/strict'; import { spawn } from 'node:child_process'; import { fileURLToPath } from 'node:url'; import path from 'node:path'; const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..'); test('sales-pricing health', async () => { const port = 18012; const child = spawn(process.execPath, ['src/server.js'], { cwd: root, env: { ...process.env, PORT: String(port) }, stdio: ['ignore', 'pipe', 'pipe'], }); await new Promise((r) => setTimeout(r, 400)); try { const r = await fetch(`http://127.0.0.1:${port}/health`); assert.equal((await r.json()).role, 'zappier-sales-pricing'); const home = await fetch(`http://127.0.0.1:${port}/`); assert.equal(home.status, 200); } finally { child.kill('SIGTERM'); } }); test('sales review via account-balance HTTP', async () => { const booksPort = 18016; const salesPort = 18017; const books = spawn(process.execPath, ['src/server.js'], { cwd: path.join(root, '..', 'zappier-account-balance'), env: { ...process.env, PORT: String(booksPort) }, stdio: ['ignore', 'pipe', 'pipe'], }); const sales = spawn(process.execPath, ['src/server.js'], { cwd: root, env: { ...process.env, PORT: String(salesPort), ACCOUNT_BALANCE_URL: `http://127.0.0.1:${booksPort}`, }, stdio: ['ignore', 'pipe', 'pipe'], }); await new Promise((r) => setTimeout(r, 500)); try { await fetch(`http://127.0.0.1:${booksPort}/adjust`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ customerId: 'c-sales', cents: 300, reason: 'promo', agent: 'sales' }), }); const st = await (await fetch(`http://127.0.0.1:${salesPort}/review/c-sales`)).json(); assert.equal(st.prepaidCents, 300); assert.equal(st.source, 'account-balance'); } finally { sales.kill('SIGTERM'); books.kill('SIGTERM'); } });