|
| 1 | +// Integration tests for credit accounting on approval ingest (POST /api/approvals). |
| 2 | +// |
| 3 | +// These boot the real router against a throwaway libsql file database. They |
| 4 | +// skip cleanly when the PWA dependencies are not installed (a fresh repo |
| 5 | +// clone only has the root CLI deps), so the root `npm test` stays green |
| 6 | +// either way. Run `npm install` in apps/pwa to enable them. |
| 7 | +import assert from "node:assert/strict"; |
| 8 | +import http from "node:http"; |
| 9 | +import fs from "node:fs"; |
| 10 | +import { mkdtempSync } from "node:fs"; |
| 11 | +import { tmpdir } from "node:os"; |
| 12 | +import path from "node:path"; |
| 13 | +import { createRequire } from "node:module"; |
| 14 | +import test from "node:test"; |
| 15 | + |
| 16 | +const require = createRequire(import.meta.url); |
| 17 | +let deps = null; |
| 18 | +try { |
| 19 | + deps = { express: require("express"), cookieParser: require("cookie-parser") }; |
| 20 | +} catch { |
| 21 | + deps = null; // pwa dependencies not installed — tests below skip |
| 22 | +} |
| 23 | + |
| 24 | +// Point the app at a throwaway database BEFORE importing its modules (config |
| 25 | +// reads the environment once, at import time). |
| 26 | +const workdir = mkdtempSync(path.join(tmpdir(), "moshcode-pwa-approvals-test-")); |
| 27 | +process.env.DATABASE_URL = `file:${path.join(workdir, "test.db")}`; |
| 28 | +process.env.SESSION_SECRET = "test-secret"; |
| 29 | + |
| 30 | +async function boot() { |
| 31 | + const { migrate } = await import("../src/migrate.mjs"); |
| 32 | + await migrate(); |
| 33 | + const { run, all, db } = await import("../src/db.mjs"); |
| 34 | + // The local libsql driver resolves statements in microtasks, which fully |
| 35 | + // serializes concurrent request handlers and hides read-check-write races. |
| 36 | + // Production runs against a network database (Turso), where every statement |
| 37 | + // is a round trip. Defer each statement to a macrotask so two in-flight |
| 38 | + // handlers genuinely interleave, like they would against the remote DB. |
| 39 | + const execute = db.execute.bind(db); |
| 40 | + db.execute = (stmt) => new Promise((resolve, reject) => { |
| 41 | + setTimeout(() => execute(stmt).then(resolve, reject), 2); |
| 42 | + }); |
| 43 | + const { sessionMiddleware, csrfGuard } = await import("../src/lib/session.mjs"); |
| 44 | + const { approvalsRouter } = await import("../src/routes/approvals.mjs"); |
| 45 | + const { createApiKey } = await import("../src/lib/apikey.mjs"); |
| 46 | + const { balance } = await import("../src/lib/credits.mjs"); |
| 47 | + const { id } = await import("../src/lib/crypto.mjs"); |
| 48 | + |
| 49 | + const app = deps.express(); |
| 50 | + app.use(deps.express.json({ verify: (req, _res, buf) => { req.rawBody = buf.toString("utf8"); } })); |
| 51 | + app.use(deps.express.urlencoded({ extended: false })); |
| 52 | + app.use(deps.cookieParser()); |
| 53 | + app.use(sessionMiddleware); |
| 54 | + app.use(csrfGuard); |
| 55 | + app.use(approvalsRouter); |
| 56 | + const server = await new Promise((resolve) => { |
| 57 | + const s = app.listen(0, "127.0.0.1", () => resolve(s)); |
| 58 | + }); |
| 59 | + const { port } = server.address(); |
| 60 | + |
| 61 | + // A user with the given enabled channels and a starting balance. Returns the |
| 62 | + // plaintext API key the CLI would send. |
| 63 | + const seedUser = async (userId, { credits = 0, channels = [] } = {}) => { |
| 64 | + await run(`INSERT INTO users (id, email, display_name, created_at) VALUES (?,?,?,?)`, |
| 65 | + [userId, `${userId}@b.c`, "demo", Date.now()]); |
| 66 | + for (const [kind, target] of channels) { |
| 67 | + await run(`INSERT INTO channels (id,user_id,kind,target,enabled,created_at) VALUES (?,?,?,?,?,?)`, |
| 68 | + [id(), userId, kind, target ?? null, 1, Date.now()]); |
| 69 | + } |
| 70 | + if (credits) { |
| 71 | + await run(`INSERT INTO credit_ledger (id,user_id,delta,reason,created_at) VALUES (?,?,?,?,?)`, |
| 72 | + [id(), userId, credits, "test.seed", Date.now()]); |
| 73 | + } |
| 74 | + const { plaintext } = await createApiKey(userId, "test"); |
| 75 | + return plaintext; |
| 76 | + }; |
| 77 | + |
| 78 | + const charges = (userId) => |
| 79 | + all(`SELECT delta FROM credit_ledger WHERE user_id = ? AND reason = 'approval.delivered'`, [userId]); |
| 80 | + |
| 81 | + // Raw http with a fresh connection per request: fetch()/undici would reuse a |
| 82 | + // keep-alive socket for same-origin calls and serialize the "concurrent" |
| 83 | + // ingests, hiding the race this exercises. |
| 84 | + const ingest = (key, message) => new Promise((resolve, reject) => { |
| 85 | + const req = http.request({ |
| 86 | + host: "127.0.0.1", port, path: "/api/approvals", method: "POST", agent: false, |
| 87 | + headers: { "content-type": "application/json", authorization: `Bearer ${key}` }, |
| 88 | + }, (res) => { |
| 89 | + let data = ""; |
| 90 | + res.on("data", (chunk) => { data += chunk; }); |
| 91 | + res.on("end", () => resolve({ status: res.statusCode, body: JSON.parse(data) })); |
| 92 | + }); |
| 93 | + req.on("error", reject); |
| 94 | + req.end(JSON.stringify({ message })); |
| 95 | + }); |
| 96 | + |
| 97 | + return { run, all, db, server, seedUser, charges, ingest, balance }; |
| 98 | +} |
| 99 | + |
| 100 | +// One shared app/db for the whole file (db.mjs is a module-level singleton — |
| 101 | +// closing it between tests would break the next boot). |
| 102 | +let booted = null; |
| 103 | +const app = () => (booted ||= boot()); |
| 104 | + |
| 105 | +test.after(() => { |
| 106 | + if (!booted) return; |
| 107 | + booted.then(({ server, db }) => { server.close(); db.close?.(); }) |
| 108 | + .finally(() => { try { fs.rmSync(workdir, { recursive: true, force: true }); } catch { /* noop */ } }); |
| 109 | +}); |
| 110 | + |
| 111 | +test("api/approvals: a paid delivery is charged exactly once", { skip: !deps && "apps/pwa deps not installed" }, async () => { |
| 112 | + const { seedUser, charges, ingest, balance } = await app(); |
| 113 | + |
| 114 | + const key = await seedUser("u-single", { credits: 12, channels: [["sms", "+15550000"]] }); |
| 115 | + const res = await ingest(key, "ship it?"); |
| 116 | + |
| 117 | + assert.equal(res.status, 201); |
| 118 | + assert.deepEqual(res.body.delivered, ["sms"]); |
| 119 | + assert.equal(res.body.charged, 12); |
| 120 | + assert.deepEqual((await charges("u-single")).map((r) => Number(r.delta)), [-12]); |
| 121 | + assert.equal(await balance("u-single"), 0); |
| 122 | +}); |
| 123 | + |
| 124 | +test("api/approvals: concurrent ingests cannot spend the same credits twice", { skip: !deps && "apps/pwa deps not installed" }, async () => { |
| 125 | + const { seedUser, charges, ingest, balance } = await app(); |
| 126 | + |
| 127 | + // A moshscript can fire ask()/notify() in parallel, so two ingests for one |
| 128 | + // account are in flight at once. Both read a balance that covers one paid |
| 129 | + // delivery — only one of them may actually get it. |
| 130 | + const key = await seedUser("u-race", { credits: 12, channels: [["sms", "+15550000"]] }); |
| 131 | + const [a, b] = await Promise.all([ingest(key, "deploy A?"), ingest(key, "deploy B?")]); |
| 132 | + |
| 133 | + assert.equal(a.status, 201); |
| 134 | + assert.equal(b.status, 201); |
| 135 | + assert.deepEqual((await charges("u-race")).map((r) => Number(r.delta)), [-12]); |
| 136 | + assert.equal(await balance("u-race"), 0); |
| 137 | + // exactly one of the two got the paid channel; the other was told why not |
| 138 | + const paid = [a, b].filter((r) => r.body.delivered.includes("sms")); |
| 139 | + assert.equal(paid.length, 1); |
| 140 | + const refused = [a, b].find((r) => !r.body.delivered.includes("sms")); |
| 141 | + assert.match(refused.body.warning, /insufficient credits/); |
| 142 | +}); |
| 143 | + |
| 144 | +test("api/approvals: a second sequential ingest falls back to free channels", { skip: !deps && "apps/pwa deps not installed" }, async () => { |
| 145 | + const { seedUser, ingest, balance } = await app(); |
| 146 | + |
| 147 | + const key = await seedUser("u-seq", { credits: 12, channels: [["sms", "+15550000"], ["webhook", "https://example.test/hook"]] }); |
| 148 | + const first = await ingest(key, "first"); |
| 149 | + const second = await ingest(key, "second"); |
| 150 | + |
| 151 | + assert.deepEqual(first.body.delivered, ["sms", "webhook"]); |
| 152 | + assert.equal(first.body.charged, 12); |
| 153 | + assert.deepEqual(second.body.delivered, ["webhook"]); // free channel still goes out |
| 154 | + assert.equal(second.body.charged, 0); |
| 155 | + assert.match(second.body.warning, /insufficient credits/); |
| 156 | + assert.equal(await balance("u-seq"), 0); |
| 157 | +}); |
| 158 | + |
| 159 | +test("api/approvals: a channel that fails to deliver is not charged for", { skip: !deps && "apps/pwa deps not installed" }, async () => { |
| 160 | + const { seedUser, charges, ingest, balance } = await app(); |
| 161 | + |
| 162 | + // slack has no webhook target and no configured default, so it refuses the |
| 163 | + // send. The ledger must show only what actually went out. |
| 164 | + const key = await seedUser("u-partial", { credits: 16, channels: [["sms", "+15550000"], ["slack", null]] }); |
| 165 | + const res = await ingest(key, "half of this lands"); |
| 166 | + |
| 167 | + assert.deepEqual(res.body.delivered, ["sms"]); |
| 168 | + assert.equal(res.body.charged, 12); |
| 169 | + assert.deepEqual((await charges("u-partial")).map((r) => Number(r.delta)), [-12]); |
| 170 | + assert.equal(await balance("u-partial"), 4); // the 4 held for slack is released |
| 171 | +}); |
| 172 | + |
| 173 | +test("api/approvals: a free-only account is never charged", { skip: !deps && "apps/pwa deps not installed" }, async () => { |
| 174 | + const { seedUser, charges, ingest, balance } = await app(); |
| 175 | + |
| 176 | + const key = await seedUser("u-free", { credits: 0, channels: [["webhook", "https://example.test/hook"]] }); |
| 177 | + const res = await ingest(key, "free ping"); |
| 178 | + |
| 179 | + assert.deepEqual(res.body.delivered, ["webhook"]); |
| 180 | + assert.equal(res.body.charged, 0); |
| 181 | + assert.equal(res.body.warning, undefined); |
| 182 | + assert.deepEqual(await charges("u-free"), []); // no zero-value ledger noise |
| 183 | + assert.equal(await balance("u-free"), 0); |
| 184 | +}); |
0 commit comments