openvibers/openvibe.finance testahead.test.js
ahead.test.js
| 1 | ; |
| 2 | /** |
| 3 | * Net worth and what if (server/ahead.js, /ahead, /api/v1/net-worth, /api/v1/what-if): net worth at each month's end |
| 4 | * (moves before the window counted, a negative balance owed); the average month over the months that had any; a |
| 5 | * one-off cost that fits, one that does not yet (and the month it would), a monthly change from a month, a baseline |
| 6 | * that runs out on its own; the page's verdicts, the goals line, bad input shown on the page, the API, another person. |
| 7 | */ |
| 8 | const assert = require('assert'); |
| 9 | const path = require('node:path'); |
| 10 | const crypto = require('node:crypto'); |
| 11 | const { history, averageMonth, whatIf } = require('../server/ahead'); |
| 12 | |
| 13 | 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']; |
| 14 | |
| 15 | assert.deepStrictEqual(history({ |
| 16 | accounts: [{ id: 'a', opening_cents: 100000 }, { id: 'b', opening_cents: 0 }], |
| 17 | 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 }], |
| 18 | months: ['2026-09', '2026-10', '2026-11'], |
| 19 | }), [ |
| 20 | { month: '2026-09', assets_cents: 150007, owed_cents: 0, net_cents: 150007 }, |
| 21 | { month: '2026-10', assets_cents: 150007, owed_cents: 30000, net_cents: 120007 }, |
| 22 | { month: '2026-11', assets_cents: 130007, owed_cents: 30000, net_cents: 100007 }, |
| 23 | ]); |
| 24 | assert.deepStrictEqual(averageMonth([ |
| 25 | { 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 }, |
| 26 | ]), { income_cents: 305000, spending_cents: 210000, months: ['2026-09', '2026-10'] }, 'an empty month is not an average month'); |
| 27 | |
| 28 | const base = { start_cents: 100000, income_cents: 300000, spending_cents: 250000, months: months12 }; |
| 29 | let p = whatIf(base); |
| 30 | assert.strictEqual(p.now[11].cents, 700000); |
| 31 | assert.strictEqual(p.below_zero_on, null); |
| 32 | p = whatIf({ ...base, cost_cents: 200000 }); |
| 33 | assert.strictEqual(p.with[0].cents, -50000); |
| 34 | assert.strictEqual(p.below_zero_on, '2026-12'); |
| 35 | assert.strictEqual(p.afford_by, '2027-01', 'two months of $500 left over pay a $2,000 cost from $1,000'); |
| 36 | p = whatIf({ ...base, cost_cents: 100000, cost_on: '2027-03' }); |
| 37 | assert.deepStrictEqual([p.with[2].cents, p.with[3].cents, p.below_zero_on], [250000, 200000, null]); |
| 38 | p = whatIf({ ...base, change_cents: -60000, change_from: '2027-02' }); |
| 39 | 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'); |
| 40 | p = whatIf({ start_cents: 120000, income_cents: 100000, spending_cents: 150000, months: months12 }); |
| 41 | assert.strictEqual(p.now_below_zero_on, '2027-02', 'more going out than coming in runs out on its own'); |
| 42 | |
| 43 | (async () => { |
| 44 | const { createTestDb } = require('openvibe-sdk/testing'); |
| 45 | const { createVault } = require('../server/crypto'); |
| 46 | const { createStore } = require('../server/store'); |
| 47 | const { createApp } = require('../server/app'); |
| 48 | const { db, close } = await createTestDb({ migrations: path.join(__dirname, '..', 'migrations'), store: process.env.FINANCE_TEST_STORE || 'pglite', service: 'finance' }); |
| 49 | const ADA = 'usr_01JCZ3NDEKTSV4RRFFQ69G5FAW'; |
| 50 | const BO = 'usr_01JCZ3NDEKTSV4RRFFQ69G5FAX'; |
| 51 | const vault = createVault(crypto.randomBytes(32).toString('base64')); |
| 52 | const store = createStore({ db, vault }); |
| 53 | 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(); } }; |
| 54 | const server = createApp({ db, vault, auth, now: () => new Date('2026-11-18T12:00:00Z') }).listen(0, '127.0.0.1'); |
| 55 | await new Promise((r) => server.once('listening', r)); |
| 56 | const base = `http://127.0.0.1:${server.address().port}`; |
| 57 | 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() })); |
| 58 | 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() })); |
| 59 | try { |
| 60 | const checking = await store.addAccount(ADA, { name: 'Checking', kind: 'checking', opening_cents: 100000 }); |
| 61 | const savings = await store.addAccount(ADA, { name: 'Savings', kind: 'savings', opening_cents: 500000 }); |
| 62 | const card = await store.addAccount(ADA, { name: 'Card', kind: 'credit' }); |
| 63 | const rows = []; |
| 64 | for (const m of ['2026-08', '2026-09', '2026-10']) { |
| 65 | rows.push({ posted_on: `${m}-01`, amount_cents: 300000, payee: 'Paycheck', category: 'Income' }); |
| 66 | rows.push({ posted_on: `${m}-03`, amount_cents: -150000, payee: 'Rent', category: 'Housing' }); |
| 67 | rows.push({ posted_on: `${m}-10`, amount_cents: -50000, payee: 'Grocer', category: 'Groceries' }); |
| 68 | } |
| 69 | rows.push({ posted_on: '2026-10-20', amount_cents: -50000, payee: 'To savings', category: 'Transfers' }); |
| 70 | await store.importRows(ADA, checking.id, rows); |
| 71 | await store.importRows(ADA, savings.id, [{ posted_on: '2026-10-20', amount_cents: 50000, payee: 'From checking', category: 'Transfers' }]); |
| 72 | await store.importRows(ADA, card.id, [{ posted_on: '2026-10-12', amount_cents: -30000, payee: 'Shoes', category: 'Shopping' }]); |
| 73 | |
| 74 | let r = await page(); |
| 75 | assert.strictEqual(r.status, 200, r.text.slice(0, 300)); |
| 76 | assert.match(r.text, /<p class="fin-big">\$8,700\.00<\/p>/, '$3,500 checking + $5,500 savings − $300 owed'); |
| 77 | assert.match(r.text, /\+\$1,700\.00<\/span> since August 2026/, 'from the first month with data, not a year of openings'); |
| 78 | assert.match(r.text, /\$3,000\.00 in, \$2,100\.00 out, <b>\$900\.00<\/b> left over\. \$8,700\.00 to spend now/, 'transfers left out of the average'); |
| 79 | assert.match(r.text, /about <b>\$19,500\.00<\/b> to spend in November 2027/); |
| 80 | assert.match(r.text, /<svg class="fin-chart"/); |
| 81 | |
| 82 | r = await page('?cost=12%2C000&cost_on=2026-12'); |
| 83 | assert.match(r.text, /Not yet: it takes you below zero in December 2026/); |
| 84 | assert.match(r.text, /could pay it in <b>March 2027<\/b>/); |
| 85 | r = await page('?cost=5000'); |
| 86 | assert.match(r.text, /Yes\. The lowest it gets is <b>\$4,600\.00<\/b>, in December 2026/); |
| 87 | assert.match(r.text, /As things are/); |
| 88 | |
| 89 | await store.addGoal(ADA, { name: 'House', target_cents: 1000000, target_on: '2027-11-01' }); |
| 90 | r = await page('?change=1000&dir=out&from=2027-02'); |
| 91 | assert.match(r.text, /Left over each month: \$900\.00 now, <b>-\$100\.00<\/b> with the change/); |
| 92 | assert.match(r.text, /Your goals with dates need \$833\.34 a month: this leaves less than that/); |
| 93 | assert.match(r.text, /name="change"[^>]*value="1000"/, 'the question stays in the form'); |
| 94 | assert.match(r.text, /name="from" value="2027-02" checked/, 'and its month chip'); |
| 95 | assert.doesNotMatch(r.text, /type="month"/, 'no browser month box'); |
| 96 | |
| 97 | r = await page('?cost=lots'); |
| 98 | assert.strictEqual(r.status, 200); |
| 99 | assert.match(r.text, /role="alert">The cost is not a number\./); |
| 100 | r = await page('?cost=10&cost_on=2030-01'); |
| 101 | assert.match(r.text, /within the next twelve months/); |
| 102 | |
| 103 | let a = await api('/what-if?cost=5000'); |
| 104 | assert.strictEqual(a.status, 200); |
| 105 | assert.deepStrictEqual([a.body.plan.lowest.cents, a.body.spendable_cents, a.body.average.income_cents], [460000, 870000, 300000]); |
| 106 | a = await api('/what-if?cost=abc'); |
| 107 | assert.strictEqual(a.status, 422); |
| 108 | assert.deepStrictEqual(a.body, { error: 'The cost is not a number.', code: 'finance.ahead.ask' }, 'an API error is JSON, not a page'); |
| 109 | a = await api('/net-worth'); |
| 110 | assert.deepStrictEqual(a.body.net_worth.map((x) => [x.month, x.net_cents]), [['2026-08', 700000], ['2026-09', 800000], ['2026-10', 870000], ['2026-11', 870000]]); |
| 111 | |
| 112 | r = await page('', BO); |
| 113 | assert.match(r.text, /Add an account on <a href="\/">Your money<\/a>/, 'another person sees none of it'); |
| 114 | assert.doesNotMatch(r.text, /8,700/); |
| 115 | r = await page('', null); |
| 116 | assert.ok([302, 303].includes(r.status), `signed out goes to sign in: ${r.status}`); |
| 117 | } finally { |
| 118 | server.close(); |
| 119 | await close(); |
| 120 | } |
| 121 | console.log('ahead: net worth over time, the average month, yes, not yet and when, a monthly change, goals, bad input, the API, another person'); |
| 122 | })().catch((err) => { console.error(err); process.exit(1); }); |
| 123 |