import test from 'node:test'; import assert from 'node:assert/strict'; import {DatabaseSync} from 'node:sqlite'; import {NETWORTH_SCHEMA,snapshotPersonalNetworth,readPersonalNetworth} from '../node/personal-networth.mjs'; import {entityId} from '../node/postbank-authority.mjs'; test('personal history excludes organisational and clandestine balances, keeps reservations and never rewrites an existing snapshot',async()=>{ const db=new DatabaseSync(':memory:'); db.exec(`CREATE TABLE pb_entities(id TEXT,kind TEXT,active INT);CREATE TABLE pb_clients(client_number TEXT,entity_id TEXT);CREATE TABLE pb_accounts(client_number TEXT,type_code TEXT,system_key TEXT,balance_minor INT,reserved_minor INT);CREATE TABLE pb_credit_loans(entity_id TEXT,state TEXT,outstanding_minor INT);${NETWORTH_SCHEMA.replace(' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4','')}`); const bank={query:async(sql,args=[])=>{sql=sql.replace(/ON DUPLICATE KEY UPDATE entity_id=VALUES\(entity_id\)/,'ON CONFLICT DO NOTHING');const stmt=db.prepare(sql);return /^SELECT/i.test(sql)?stmt.all(...args):stmt.run(...args);},transaction:async fn=>fn(bank)}; const id=entityId('person','citizen'); db.prepare('INSERT INTO pb_entities VALUES(?,?,1)').run(id,'person');db.prepare('INSERT INTO pb_entities VALUES(?,?,1)').run('organisation','clan'); db.prepare('INSERT INTO pb_clients VALUES(?,?)').run('personal',id);db.prepare('INSERT INTO pb_clients VALUES(?,?)').run('org','organisation'); const insert=db.prepare('INSERT INTO pb_accounts VALUES(?,?,NULL,?,?)'); insert.run('personal','102',10000,4000);insert.run('personal','105',90000,0);insert.run('personal','979',80000,0);insert.run('org','102',70000,0); db.prepare('INSERT INTO pb_credit_loans VALUES(?,?,?)').run(id,'active',2500); try{await snapshotPersonalNetworth(bank,1,{clock:()=>1000});assert.deepEqual((await readPersonalNetworth(bank,'citizen',{clock:()=>1001})).points,[{at:1000,minor:'7500'}]);db.exec("UPDATE pb_accounts SET balance_minor=20000 WHERE type_code='102' AND client_number='personal'");await snapshotPersonalNetworth(bank,1,{clock:()=>2000});await snapshotPersonalNetworth(bank,2,{clock:()=>3000});assert.deepEqual((await readPersonalNetworth(bank,'citizen',{clock:()=>3001})).points,[{at:1000,minor:'7500'},{at:3000,minor:'17500'}]);assert.equal((await readPersonalNetworth(bank,'someone-else',{clock:()=>3001})).state,'empty');}finally{db.close();} }); test('unavailable bank history remains unavailable instead of fabricating balances',async()=>{assert.equal((await readPersonalNetworth(null,'citizen')).state,'unavailable');assert.equal((await readPersonalNetworth({query:async()=>{throw Error('offline');}},'citizen')).state,'unavailable');});