'use strict'; /** * Net worth and what if (server/ahead.js, /ahead, /api/v1/net-worth, /api/v1/what-if): net worth at each month's end * (moves before the window counted, a negative balance owed); the average month over the months that had any; a * one-off cost that fits, one that does not yet (and the month it would), a monthly change from a month, a baseline * that runs out on its own; the page's verdicts, the goals line, bad input shown on the page, the API, another person. */ const assert = require('assert'); const path = require('node:path'); const crypto = require('node:crypto'); const { history, averageMonth, whatIf } = require('../server/ahead'); const months12 = ['2026-12', '2027-01', '2027-02', '2027-03', '2027-04', '2027-05', '2027-06', '2027-07', '2027-08', '2027-09', '2027-10', '2027-11']; assert.deepStrictEqual(history({ accounts: [{ id: 'a', opening_cents: 100000 }, { id: 'b', opening_cents: 0 }], moves: [{ account_id: 'a', month: '2026-01', cents: 7 }, { account_id: 'a', month: '2026-09', cents: 50000 }, { account_id: 'b', month: '2026-10', cents: -30000 }, { account_id: 'a', month: '2026-11', cents: -20000 }], months: ['2026-09', '2026-10', '2026-11'], }), [ { month: '2026-09', assets_cents: 150007, owed_cents: 0, net_cents: 150007 }, { month: '2026-10', assets_cents: 150007, owed_cents: 30000, net_cents: 120007 }, { month: '2026-11', assets_cents: 130007, owed_cents: 30000, net_cents: 100007 }, ]); assert.deepStrictEqual(averageMonth([ { month: '2026-08', income_cents: 0, spent_cents: 0 }, { month: '2026-09', income_cents: 300000, spent_cents: 200000 }, { month: '2026-10', income_cents: 310000, spent_cents: 220000 }, ]), { income_cents: 305000, spending_cents: 210000, months: ['2026-09', '2026-10'] }, 'an empty month is not an average month'); const base = { start_cents: 100000, income_cents: 300000, spending_cents: 250000, months: months12 }; let p = whatIf(base); assert.strictEqual(p.now[11].cents, 700000); assert.strictEqual(p.below_zero_on, null); p = whatIf({ ...base, cost_cents: 200000 }); assert.strictEqual(p.with[0].cents, -50000); assert.strictEqual(p.below_zero_on, '2026-12'); assert.strictEqual(p.afford_by, '2027-01', 'two months of $500 left over pay a $2,000 cost from $1,000'); p = whatIf({ ...base, cost_cents: 100000, cost_on: '2027-03' }); assert.deepStrictEqual([p.with[2].cents, p.with[3].cents, p.below_zero_on], [250000, 200000, null]); p = whatIf({ ...base, change_cents: -60000, change_from: '2027-02' }); assert.deepStrictEqual([p.with[1].cents, p.with[2].cents, p.with[11].cents, p.left_with_cents], [200000, 190000, 100000, -10000], 'rent up $600 from February'); p = whatIf({ start_cents: 120000, income_cents: 100000, spending_cents: 150000, months: months12 }); assert.strictEqual(p.now_below_zero_on, '2027-02', 'more going out than coming in runs out on its own'); (async () => { const { createTestDb } = require('openvibe-sdk/testing'); const { createVault } = require('../server/crypto'); const { createStore } = require('../server/store'); const { createApp } = require('../server/app'); const { db, close } = await createTestDb({ migrations: path.join(__dirname, '..', 'migrations'), store: process.env.FINANCE_TEST_STORE || 'pglite', service: 'finance' }); const ADA = 'usr_01JCZ3NDEKTSV4RRFFQ69G5FAW'; const BO = 'usr_01JCZ3NDEKTSV4RRFFQ69G5FAX'; const vault = createVault(crypto.randomBytes(32).toString('base64')); const store = createStore({ db, vault }); const auth = { router: (express) => express.Router(), optionalAuth: () => (req, _res, next) => { const u = req.get('x-test-user'); if (u) req.user = { subject_id: u }; next(); } }; const server = createApp({ db, vault, auth, now: () => new Date('2026-11-18T12:00:00Z') }).listen(0, '127.0.0.1'); await new Promise((r) => server.once('listening', r)); const base = `http://127.0.0.1:${server.address().port}`; const page = (q = '', as = ADA) => fetch(`${base}/ahead${q}`, { redirect: 'manual', headers: as ? { 'x-test-user': as } : {} }).then(async (r) => ({ status: r.status, text: await r.text() })); const api = (p, as = ADA) => fetch(`${base}/api/v1${p}`, { headers: { 'x-test-user': as } }).then(async (r) => ({ status: r.status, body: await r.json() })); try { const checking = await store.addAccount(ADA, { name: 'Checking', kind: 'checking', opening_cents: 100000 }); const savings = await store.addAccount(ADA, { name: 'Savings', kind: 'savings', opening_cents: 500000 }); const card = await store.addAccount(ADA, { name: 'Card', kind: 'credit' }); const rows = []; for (const m of ['2026-08', '2026-09', '2026-10']) { rows.push({ posted_on: `${m}-01`, amount_cents: 300000, payee: 'Paycheck', category: 'Income' }); rows.push({ posted_on: `${m}-03`, amount_cents: -150000, payee: 'Rent', category: 'Housing' }); rows.push({ posted_on: `${m}-10`, amount_cents: -50000, payee: 'Grocer', category: 'Groceries' }); } rows.push({ posted_on: '2026-10-20', amount_cents: -50000, payee: 'To savings', category: 'Transfers' }); await store.importRows(ADA, checking.id, rows); await store.importRows(ADA, savings.id, [{ posted_on: '2026-10-20', amount_cents: 50000, payee: 'From checking', category: 'Transfers' }]); await store.importRows(ADA, card.id, [{ posted_on: '2026-10-12', amount_cents: -30000, payee: 'Shoes', category: 'Shopping' }]); let r = await page(); assert.strictEqual(r.status, 200, r.text.slice(0, 300)); assert.match(r.text, /
\$8,700\.00<\/p>/, '$3,500 checking + $5,500 savings − $300 owed'); assert.match(r.text, /\+\$1,700\.00<\/span> since August 2026/, 'from the first month with data, not a year of openings'); assert.match(r.text, /\$3,000\.00 in, \$2,100\.00 out, \$900\.00<\/b> left over\. \$8,700\.00 to spend now/, 'transfers left out of the average'); assert.match(r.text, /about \$19,500\.00<\/b> to spend in November 2027/); assert.match(r.text, /