openvibers/openvibe.finance testharnessserve-sample.js

serve-sample.js

main 3.9 KB · 61 lines · javascript

1'use strict';
2/**
3 * A local look at Finance's pages (not a test): the real app on PGlite, a stand-in sign-in as one person, and a month
4 * of sample data. node test/harness/serve-sample.js [port] → prints the port, serves until killed.
5 */
6const path = require('node:path');
7const crypto = require('node:crypto');
8const { createTestDb } = require('openvibe-sdk/testing');
9const { createVault } = require('../../server/crypto');
10const { createApp } = require('../../server/app');
11const { createStore } = require('../../server/store');
12const { readCsv } = require('../../server/csv');
13
14const ADA = 'usr_01JCZ3NDEKTSV4RRFFQ69G5FAW';
15(async () => {
16 const { db } = await createTestDb({ migrations: path.join(__dirname, '..', '..', 'migrations') });
17 const vault = createVault(crypto.randomBytes(32).toString('base64'));
18 const store = createStore({ db, vault });
19 const checking = await store.addAccount(ADA, { name: 'Everyday checking', kind: 'checking', opening_cents: 184250 });
20 await store.addAccount(ADA, { name: 'Rainy day', kind: 'savings', opening_cents: 520000 });
21 const card = await store.addAccount(ADA, { name: 'Travel card', kind: 'credit', opening_cents: -32075 });
22 const csv = `Date,Description,Amount,Category
232026-11-01,Paycheck ACME Corp,3120.00,Income
242026-11-01,Lakeview Apartments,-1450.00,Housing
252026-11-02,Corner Grocer,-86.42,Groceries
262026-11-03,City Power & Light,-71.30,Utilities
272026-11-04,Bean There Coffee,-4.75,Eating out
282026-11-05,StreamFlix,-15.99,Subscriptions
292026-11-06,Metro Transit,-40.00,Transport
302026-11-08,Corner Grocer,-54.10,Groceries
312026-11-09,Pharmacy Plus,-23.80,Health
322026-11-11,Thai Garden,-38.60,Eating out
332026-11-12,Transfer to savings,-300.00,Transfers
342026-11-14,Bookshop on Main,-18.99,
352026-11-15,Paycheck ACME Corp,3120.00,Income
362026-11-16,Gym Monthly,-29.00,Subscriptions
37`;
38 await store.importRows(ADA, checking.id, readCsv(csv).rows);
39 // Three earlier months, so the average month (Net worth and what if) has something to stand on.
40 for (const [m, extra] of [['2026-08', 0], ['2026-09', 12000], ['2026-10', -8000]]) {
41 await store.importRows(ADA, checking.id, [
42 { posted_on: `${m}-01`, amount_cents: 312000, payee: 'Paycheck ACME Corp', category: 'Income' },
43 { posted_on: `${m}-01`, amount_cents: -145000, payee: 'Lakeview Apartments', category: 'Housing' },
44 { posted_on: `${m}-04`, amount_cents: -42000 - extra, payee: 'Corner Grocer', category: 'Groceries' },
45 { posted_on: `${m}-09`, amount_cents: -7130, payee: 'City Power & Light', category: 'Utilities' },
46 { posted_on: `${m}-15`, amount_cents: -38500, payee: 'Eating out, various', category: 'Eating out' },
47 ]);
48 }
49 await store.importRows(ADA, card.id, readCsv('Date,Payee,Amount\n2026-11-07,Airline Tickets,-412.20\n2026-11-10,Hotel Downtown,-268.00\n').rows);
50 await store.setBudget(ADA, 'Eating out', 4000);
51 await store.setBudget(ADA, 'Groceries', 40000);
52 await store.setBudget(ADA, 'Health', 5000);
53 const rainy = (await store.accounts(ADA)).find((x) => x.name === 'Rainy day');
54 await store.addGoal(ADA, { name: 'Emergency fund', target_cents: 1000000, target_on: '2027-11-01', account_id: rainy.id });
55 await store.addGoal(ADA, { name: 'Trip to Lisbon', target_cents: 150000, saved_cents: 40000 });
56 await store.setDebt(ADA, card.id, { apr_bps: 2499, payment_cents: 15000 });
57 const auth = { router: (express) => express.Router(), optionalAuth: () => (req, _res, next) => { if (!req.get('x-guest')) req.user = { subject_id: ADA }; next(); } };
58 const app = createApp({ db, vault, auth, baseUrl: 'http://127.0.0.1', now: () => new Date('2026-11-18T12:00:00Z') });
59 const server = app.listen(Number(process.argv[2] || 0), '127.0.0.1', () => console.log(`port ${server.address().port} account ${checking.id}`));
60})().catch((e) => { console.error(e); process.exit(1); });
61