Treasury & Bank Transfer Management System — سامانه مدیریت خزانه‌داری و حواله‌های بانکی

1 import {afterEach,describe,expect,it} from 'vitest';
2 import {mkdtempSync,rmSync} from 'node:fs';
3 import {tmpdir} from 'node:os';
4 import {join} from 'node:path';
5 import {LocalStore} from '../local/local-store';
6 import {SqliteStore} from '../local/sqlite-store';
7 import type {TreasuryStore} from './store';
8 import {TreasuryService} from './treasury-service';
9
10 const dirs:string[]=[];
11 const actor={tenantId:'t1',userId:'maker',role:'MAKER' as const};
12 const iban='IR062960000000100324200001';
13 const open=(kind:'local'|'sqlite')=>{
14 const dir=mkdtempSync(join(tmpdir(),'tf-atomic-audit-'));
15 dirs.push(dir);
16 const file=join(dir,kind==='sqlite'?'ledger.sqlite':'ledger.json');
17 const store: TreasuryStore=kind==='sqlite'?new SqliteStore(file):new LocalStore(file);
18 return {file,store,service:new TreasuryService(store,actor)};
19 };
20 afterEach(()=>{for(const dir of dirs.splice(0))rmSync(dir,{recursive:true,force:true})});
21
22 describe.each(['local','sqlite'] as const)('%s transactional finance audit',kind=>{
23 it('commits each create and exactly one audit record in a single update',()=>{
24 const {store,service}=open(kind);
25 let updates=0;
26 const tracked: TreasuryStore={
27 snapshot:()=>store.snapshot(),
28 update:mutation=>{updates++;return store.update(mutation)}
29 };
30 const s=new TreasuryService(tracked,actor);
31 s.createAccount({id:'a1',name:'حساب آزمایشی',bankName:'بانک آزمایشی',iban});
32 expect(updates).toBe(1);
33 s.createBeneficiary({id:'b1',name:'ذی‌نفع آزمایشی',iban,whitelisted:true});
34 expect(updates).toBe(2);
35 const row=s.createPayment({id:'p1',sourceAccountId:'a1',beneficiaryId:'b1',
36 amountIrr:125000,paymentDate:'2026-10-23',clientReference:'ATOMIC-001',description:'synthetic only'});
37 expect(row.state).toBe('DRAFT');
38 expect(updates).toBe(3);
39 expect(s.listAudit().map(x=>x.action)).toEqual([
40 'ACCOUNT_CREATED','BENEFICIARY_CREATED','PAYMENT_CREATED'
41 ]);
42 expect(s.listPayments()).toHaveLength(1);
43 if(store instanceof SqliteStore)store.close();
44 expect(service.listAudit).toBeDefined();
45 });
46
47 it('rolls back both entity and audit when storage fails after mutation',()=>{
48 const {store}=open(kind);
49 const s=new TreasuryService(store,actor);
50 s.createAccount({id:'a1',name:'حساب آزمایشی',bankName:'بانک آزمایشی',iban});
51 s.createBeneficiary({id:'b1',name:'ذی‌نفع آزمایشی',iban,whitelisted:true});
52 const before=store.snapshot();
53 let updates=0;
54 const fault: TreasuryStore={
55 snapshot:()=>store.snapshot(),
56 update:mutate=>{
57 updates++;
58 return store.update(snapshot=>{
59 const mutated=mutate(snapshot);
60 expect(mutated.payments).toHaveLength(1);
61 expect(mutated.audit.at(-1)?.action).toBe('PAYMENT_CREATED');
62 throw Error('SIMULATED_COMMIT_FAILURE');
63 });
64 }
65 };
66 const failing=new TreasuryService(fault,actor);
67 expect(()=>failing.createPayment({id:'p-fail',sourceAccountId:'a1',beneficiaryId:'b1',
68 amountIrr:200000,paymentDate:'2026-10-23',clientReference:'SIM-ROLLBACK',
69 description:'failure injection'})).toThrow('SIMULATED_COMMIT_FAILURE');
70 expect(updates).toBe(1);
71 expect(store.snapshot()).toEqual(before);
72 if(store instanceof SqliteStore)store.close();
73 });
74 });
75
76 describe('SQLite locking and rollback',()=>{
77 it('serializes the read snapshot under the write transaction',()=>{
78 const dir=mkdtempSync(join(tmpdir(),'tf-sqlite-race-'));
79 dirs.push(dir);
80 const path=join(dir,'race.db');
81 const left=new SqliteStore(path),right=new SqliteStore(path);
82 try{
83 left.update(d=>{
84 expect(()=>right.update(other=>{
85 other.meta.organizationName='OVERWRITING_CONCURRENT_WRITER';return other;
86 })).toThrow();
87 d.meta.organizationName='FIRST_SERIALIZED_WRITER';
88 return d;
89 });
90 expect(left.snapshot().meta.organizationName).toBe('FIRST_SERIALIZED_WRITER');
91 expect(right.snapshot().meta.organizationName).toBe('FIRST_SERIALIZED_WRITER');
92 }finally{left.close();right.close()}
93 });
94 });