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

Add deterministic preflight/commit race, duplicate reference and removed source failure tests for both stores

davoodmehraban89 committed Oct 23, 2026 at 11:00 UTC 2ee1f68d55bb1f7ab27bdb78243701830d4aebc1
1 file changed +79
src/platform/application/treasury-service-concurrent-guard.test.ts new
+79
@@ -0,0 +1,79 @@
1 +import {afterEach,describe,it,expect} 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 pay=(id:string,amount:number,clientReference:string)=>({
14 + id,sourceAccountId:'a1',beneficiaryId:'b1',amountIrr:amount,
15 + paymentDate:'2026-10-23',clientReference,description:'synthetic race'
16 +});
17 +function fixture(kind:'local'|'sqlite'){
18 + const dir=mkdtempSync(join(tmpdir(),'tf-finance-race-'));dirs.push(dir);
19 + const store: TreasuryStore=kind==='sqlite'
20 + ?new SqliteStore(join(dir,'store.db')):new LocalStore(join(dir,'store.json'));
21 + const other=new TreasuryService(store,actor);
22 + other.createAccount({id:'a1',name:'A',bankName:'Test bank',iban});
23 + other.createBeneficiary({id:'b1',name:'B',iban,whitelisted:true});
24 + return {store,other,close:()=>{if(store instanceof SqliteStore)store.close()}};
25 +}
26 +afterEach(()=>{for(const d of dirs.splice(0))rmSync(d,{force:true,recursive:true})});
27 +
28 +describe.each(['local','sqlite'] as const)('%s write-time financial guards',kind=>{
29 + it('rejects a duplicate financial fingerprint introduced after preflight',()=>{
30 + const {store,other,close}=fixture(kind);
31 + let injected=false;
32 + const interleaved: TreasuryStore={
33 + snapshot:()=>store.snapshot(),
34 + update:mutate=>{
35 + if(!injected){injected=true;other.createPayment(pay('p-prior',12500,'PRIOR')); }
36 + return store.update(mutate);
37 + }
38 + };
39 + const attempted=new TreasuryService(interleaved,actor);
40 + expect(()=>attempted.createPayment(pay('p-late',12500,'DIFFERENT')))
41 + .toThrow(/duplicate payment/i);
42 + expect(injected).toBe(true);
43 + expect(other.listPayments().map(x=>x.id)).toEqual(['p-prior']);
44 + expect(other.listAudit().filter(x=>x.action==='PAYMENT_CREATED')).toHaveLength(1);
45 + close();
46 + });
47 +
48 + it('rejects case-insensitive reference collisions with different amounts',()=>{
49 + const {store,other,close}=fixture(kind);
50 + other.createPayment(pay('p-first',10000,'CASE-ID'));
51 + expect(()=>other.createPayment(pay('p-second',10001,'case-id')))
52 + .toThrow(/duplicate payment reference/i);
53 + expect(other.listPayments()).toHaveLength(1);
54 + expect(other.listAudit().filter(x=>x.action==='PAYMENT_CREATED')).toHaveLength(1);
55 + close();
56 + });
57 +
58 + it('rejects a source account removed between preflight and commit',()=>{
59 + const {store,other,close}=fixture(kind);
60 + let removed=false;
61 + const interleaved: TreasuryStore={
62 + snapshot:()=>store.snapshot(),
63 + update:mutate=>{
64 + if(!removed){
65 + removed=true;
66 + store.update(d=>{d.accounts=d.accounts.filter(x=>x.id!=='a1');return d});
67 + }
68 + return store.update(mutate);
69 + }
70 + };
71 + const attempted=new TreasuryService(interleaved,actor);
72 + expect(()=>attempted.createPayment(pay('p-removed',20000,'NO-SOURCE')))
73 + .toThrow('source account not found');
74 + expect(removed).toBe(true);
75 + expect(other.listPayments()).toHaveLength(0);
76 + expect(other.listAudit().some(x=>x.action==='PAYMENT_CREATED')).toBe(false);
77 + close();
78 + });
79 +});