openvibers/openvibe.finance testbudgets.test.js
budgets.test.js
| 1 | ; |
| 2 | /** |
| 3 | * Budgets (migrations/0003_budgets.sql): a monthly limit for a category of spending, set from the month page; the month |
| 4 | * shows what is left or how much over, budgeted categories with nothing spent yet included; income, transfers and |
| 5 | * Uncategorized take no budget; an empty amount removes one; another person sees none; export carries them and delete |
| 6 | * removes them. |
| 7 | */ |
| 8 | const assert = require('assert'); |
| 9 | const path = require('node:path'); |
| 10 | const crypto = require('node:crypto'); |
| 11 | const { createTestDb } = require('openvibe-sdk/testing'); |
| 12 | const { createVault } = require('../server/crypto'); |
| 13 | const { createStore } = require('../server/store'); |
| 14 | const { createApp } = require('../server/app'); |
| 15 | |
| 16 | const ADA = 'usr_01JCZ3NDEKTSV4RRFFQ69G5FAW'; |
| 17 | const BO = 'usr_01JCZ3NDEKTSV4RRFFQ69G5FAX'; |
| 18 | |
| 19 | (async () => { |
| 20 | const { db, close } = await createTestDb({ migrations: path.join(__dirname, '..', 'migrations'), store: process.env.FINANCE_TEST_STORE || 'pglite', service: 'finance' }); |
| 21 | const vault = createVault(crypto.randomBytes(32).toString('base64')); |
| 22 | const store = createStore({ db, vault }); |
| 23 | 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(); } }; |
| 24 | const server = createApp({ db, vault, auth, now: () => new Date('2026-11-18T12:00:00Z') }).listen(0, '127.0.0.1'); |
| 25 | await new Promise((r) => server.once('listening', r)); |
| 26 | const base = `http://127.0.0.1:${server.address().port}`; |
| 27 | const post = (p, form, as = ADA) => fetch(base + p, { method: 'POST', redirect: 'manual', headers: { 'sec-fetch-site': 'same-origin', 'x-test-user': as, 'content-type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams(form).toString() }) |
| 28 | .then(async (r) => ({ status: r.status, location: r.headers.get('location'), text: await r.text() })); |
| 29 | const page = (p, as = ADA) => fetch(base + p, { headers: { 'x-test-user': as } }).then((r) => r.text()); |
| 30 | try { |
| 31 | const acc = await store.addAccount(ADA, { name: 'Checking' }); |
| 32 | await store.importRows(ADA, acc.id, [ |
| 33 | { posted_on: '2026-11-02', amount_cents: -24000, payee: 'Thai Garden', category: 'Eating out' }, |
| 34 | { posted_on: '2026-11-09', amount_cents: -9000, payee: 'Pizza Place', category: 'Eating out' }, |
| 35 | { posted_on: '2026-11-03', amount_cents: -12000, payee: 'Corner Grocer', category: 'Groceries' }, |
| 36 | ]); |
| 37 | |
| 38 | let r = await post('/budgets', { category: 'Eating out', amount: '300', back: '2026-11' }); |
| 39 | assert.strictEqual(r.status, 303, r.text); |
| 40 | assert.strictEqual(r.location, '/month?m=2026-11&budgeted=1'); |
| 41 | r = await post('/budgets', { category: 'Groceries', amount: '$400.00', back: '2026-11' }); |
| 42 | r = await post('/budgets', { category: 'Health', amount: '50', back: '2026-11' }); |
| 43 | assert.deepStrictEqual(await store.budgets(ADA), [ |
| 44 | { category: 'Eating out', monthly_cents: 30000 }, { category: 'Groceries', monthly_cents: 40000 }, { category: 'Health', monthly_cents: 5000 }]); |
| 45 | |
| 46 | let html = await page('/month?m=2026-11'); |
| 47 | assert.match(html, /\$30\.00 over/, 'Eating out: $330 of $300'); |
| 48 | assert.match(html, /\$280\.00 left/, 'Groceries: $120 of $400'); |
| 49 | assert.match(html, /\$50\.00 left/, 'Health: nothing spent yet, still shown'); |
| 50 | assert.match(html, /\$450\.00 of \$750\.00 spent/, '$330 + $120 + $0, the overspend counted'); |
| 51 | assert.match(html, /class="is-over"/); |
| 52 | |
| 53 | r = await post('/budgets', { category: 'Eating out', amount: '400', back: '2026-11' }); |
| 54 | assert.strictEqual((await store.budgets(ADA))[0].monthly_cents, 40000, 'the same category again replaces it'); |
| 55 | r = await post('/budgets', { category: 'Health', amount: '', back: '2026-11' }); |
| 56 | assert.strictEqual(r.location, '/month?m=2026-11&unbudgeted=1'); |
| 57 | assert.ok(!(await store.budgets(ADA)).some((b) => b.category === 'Health'), 'an empty amount removes it'); |
| 58 | |
| 59 | for (const category of ['Income', 'Transfers', 'Uncategorized']) { |
| 60 | r = await post('/budgets', { category, amount: '100' }); |
| 61 | assert.strictEqual(r.status, 422, category); |
| 62 | } |
| 63 | r = await post('/budgets', { category: 'Fees', amount: 'lots' }); |
| 64 | assert.strictEqual(r.status, 422); |
| 65 | r = await fetch(`${base}/budgets`, { method: 'POST', redirect: 'manual', headers: { 'sec-fetch-site': 'cross-site', 'x-test-user': ADA, 'content-type': 'application/x-www-form-urlencoded' }, body: 'category=Fees&amount=5' }); |
| 66 | assert.strictEqual(r.status, 403); |
| 67 | |
| 68 | assert.deepStrictEqual((await fetch(`${base}/api/v1/budgets`, { headers: { 'x-test-user': BO } }).then((x) => x.json())).budgets, [], 'nobody sees another person\'s'); |
| 69 | const put = await fetch(`${base}/api/v1/budgets/${encodeURIComponent('Shopping')}`, { method: 'PUT', headers: { 'sec-fetch-site': 'same-origin', 'x-test-user': ADA, 'content-type': 'application/json' }, body: JSON.stringify({ monthly_cents: 10000 }) }); |
| 70 | assert.strictEqual(put.status, 200); |
| 71 | assert.ok((await store.budgets(ADA)).some((b) => b.category === 'Shopping')); |
| 72 | |
| 73 | assert.strictEqual((await store.exportAll(ADA)).budgets.length, 3); |
| 74 | await store.removeAll(ADA); |
| 75 | assert.strictEqual(Number(await db.value('SELECT count(*) FROM finance_budgets WHERE owner = $1', [ADA])), 0); |
| 76 | } finally { |
| 77 | server.close(); |
| 78 | await close(); |
| 79 | } |
| 80 | console.log('budgets: set, replaced and removed; left and over on the month; refused where they mean nothing; exported and deleted'); |
| 81 | })().catch((err) => { console.error(err); process.exit(1); }); |
| 82 |