import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto'; import {BankFixture} from './postbank-fixture.mjs'; import {bankNumber} from '../node/postbank-catalogue.mjs'; import {createProjectBanking,PROJECT_BANK_SCHEMA} from '../node/project-banking.mjs'; import {delegatedAuthority,canDebitAccount} from '../node/postbank-authorisations.mjs'; async function fixture(){ const bank=new BankFixture();await bank.query(PROJECT_BANK_SCHEMA); const tax=await bank.query("INSERT INTO pb_accounts(system_key,type_code,display_name,balance_minor,state,created_at) VALUES('sys_tax_treasury','979','Tax',100000,'active',0)"); await bank.query('UPDATE pb_accounts SET account_number=? WHERE id=?',[bankNumber('979',tax.insertId),tax.insertId]); const actor={id:randomUUID()},input={projectId:randomUUID(),applicantId:randomUUID(),requestId:randomUUID(),title:'A public arena',amountMinor:25000};let allowed=true; const service=createProjectBanking(bank,{clock:()=>1000,authorize:async who=>allowed&&who.id===actor.id}); return{bank,actor,input,service,taxId:tax.insertId,revoke:()=>{allowed=false;}}; } test('approved project funding reuses Tax posting once and delegates only its 979 account',async()=>{ const f=await fixture();try{ const result=await f.service.fund(f.actor,f.input);assert.equal(result.state,'funded');assert.equal((await f.service.fund(f.actor,f.input)).replayed,true); const account=await f.bank.first('SELECT * FROM pb_accounts WHERE id=?',[result.accountId]);assert.equal(account.type_code,'979');assert.equal(account.balance_minor,25000); assert.equal((await f.bank.first('SELECT balance_minor FROM pb_accounts WHERE id=?',[f.taxId])).balance_minor,75000); const legs=await f.bank.first('SELECT COUNT(*) total,SUM(amount_minor) net FROM pb_entries');assert.equal(legs.total,2);assert.equal(legs.net,0); const context=await delegatedAuthority({entities:[],ranks:[]},{id:f.input.applicantId},f.bank,1000); assert.equal(context.entities.length,1);assert(canDebitAccount(context,context.entities[0].id,result.accountId));assert(!canDebitAccount(context,context.entities[0].id,String(f.taxId))); assert.equal(context.entities[0].canActivate,false);assert.equal(context.readAdmin,undefined); await assert.rejects(f.service.fund(f.actor,{...f.input,amountMinor:30000}),/reference conflict/); }finally{f.bank.close();} }); test('failed funding rolls back provisioning and cannot leave an unfunded account or mandate',async()=>{ const f=await fixture();try{ await assert.rejects(f.service.fund(f.actor,{...f.input,amountMinor:200000}),/Insufficient/); assert.equal((await f.bank.query('SELECT * FROM pb_clients')).length,0);assert.equal((await f.bank.query('SELECT * FROM pb_account_authorisations')).length,0); f.revoke();await assert.rejects(f.service.fund(f.actor,f.input),/decision required/); assert.deepEqual(await f.service.close({...f.actor},{projectId:f.input.projectId,requestId:randomUUID()}).catch(e=>e.message),'Project closure authority required'); }finally{f.bank.close();} }); test('completion returns remaining funding to Tax and closes mandates without deleting statements',async()=>{ const f=await fixture();try{ const grant=await f.service.fund(f.actor,f.input);const close={projectId:f.input.projectId,requestId:randomUUID(),completed:true}; assert.deepEqual(await f.service.close(f.actor,{...close,completed:false}),{closed:false,reason:'funds-remaining'}); assert.equal((await f.service.close(f.actor,close)).closed,true);assert.equal((await f.service.close(f.actor,close)).replayed,true); assert.equal((await f.bank.first('SELECT balance_minor,state FROM pb_accounts WHERE id=?',[grant.accountId])).state,'closed'); assert.equal((await f.bank.first('SELECT balance_minor FROM pb_accounts WHERE id=?',[f.taxId])).balance_minor,100000); assert.equal((await f.bank.first('SELECT state FROM pb_account_authorisations WHERE account_id=?',[grant.accountId])).state,'revoked'); assert.equal((await f.bank.first('SELECT COUNT(*) total FROM pb_entries')).total,4); }finally{f.bank.close();} }); test('zero-balance closure waits for pending incoming or outgoing settlements',async()=>{ const f=await fixture();try{ const grant=await f.service.fund(f.actor,f.input);await f.bank.query('UPDATE pb_accounts SET balance_minor=0 WHERE id=?',[grant.accountId]); const pending=randomUUID();await f.bank.query("INSERT INTO pb_orders(id,actor_id,source_id,target_id,fingerprint,amount_minor,memo,mode,state,created_at) VALUES(?,?,?,?,?,100,'Pending incoming','standard','queued',0)",[pending,f.actor.id,f.taxId,grant.accountId,'f'.repeat(64)]); const close={projectId:f.input.projectId,requestId:randomUUID()};assert.deepEqual(await f.service.close(f.actor,close),{closed:false,reason:'pending-settlement'}); await f.bank.query("UPDATE pb_orders SET state='rejected' WHERE id=?",[pending]);assert.equal((await f.service.close(f.actor,close)).closed,true); }finally{f.bank.close();} });