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)), '..'); const authzRoot = path.join(root, '..', 'verae-access-authz'); const booksRoot = path.join(root, '..', 'zappier-account-balance'); test('web plane can read statement after authz, cannot skip authz', async () => { const authzPort = 18021; const booksPort = 18022; const webPort = 18023; const booksFile = path.join(os.tmpdir(), `web-books-${process.pid}.json`); const authz = spawn(process.execPath, ['src/server.js'], { cwd: authzRoot, env: { ...process.env, PORT: String(authzPort), NATS_URL: '' }, stdio: ['ignore', 'pipe', 'pipe'], }); const books = spawn(process.execPath, ['src/server.js'], { cwd: booksRoot, env: { ...process.env, PORT: String(booksPort), BOOKS_PATH: booksFile, NATS_URL: '' }, stdio: ['ignore', 'pipe', 'pipe'], }); const web = spawn(process.execPath, ['src/server.js'], { cwd: root, env: { ...process.env, PORT: String(webPort), AUTHZ_URL: `http://127.0.0.1:${authzPort}`, ACCOUNT_BALANCE_URL: `http://127.0.0.1:${booksPort}`, NATS_URL: '', }, stdio: ['ignore', 'pipe', 'pipe'], }); await new Promise((r) => setTimeout(r, 600)); try { await fetch(`http://127.0.0.1:${booksPort}/adjust`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ customerId: 'c-web', cents: 400, kind: 'reload' }), }); const h = await (await fetch(`http://127.0.0.1:${webPort}/health`)).json(); assert.equal(h.plane, 'web'); const portal = await fetch(`http://127.0.0.1:${webPort}/portal/`); assert.equal(portal.status, 200); assert.match(await portal.text(), /Zappier Portal|portal/i); const st = await (await fetch(`http://127.0.0.1:${webPort}/statement/c-web`)).json(); assert.equal(st.prepaidCents, 400); assert.equal(st.plane, 'web'); } finally { web.kill('SIGTERM'); books.kill('SIGTERM'); authz.kill('SIGTERM'); fs.rmSync(booksFile, { force: true }); } });