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

1 import {afterEach,describe,expect,it,vi} 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 {TreasuryService} from './treasury-service';
7 import type {BankAdapter} from '../../domain/bank-adapter';
8
9 const dirs:string[]=[];
10 function newStore(){
11 const dir=mkdtempSync(join(tmpdir(),'tf-poll-'));
12 dirs.push(dir);
13 return new LocalStore(join(dir,'store.json'));
14 }
15 afterEach(()=>dirs.splice(0).forEach(d=>rmSync(d,{recursive:true,force:true})));
16
17 function prepared(){
18 const store=newStore();
19 const maker=new TreasuryService(store,{tenantId:'local',userId:'maker',role:'MAKER'});
20 maker.createAccount({id:'a1',name:'حساب',iban:'IR062960000000100324200001',bankName:'بانک'});
21 maker.createBeneficiary({id:'b1',name:'ذی‌نفع',iban:'IR062960000000100324200001',whitelisted:true});
22 maker.createPayment({id:'p1',sourceAccountId:'a1',beneficiaryId:'b1',amountIrr:1000,paymentDate:'2026-10-06',clientReference:'R-POLL',description:''});
23 maker.submitPayment('p1');
24 const checker=new TreasuryService(store,{tenantId:'local',userId:'checker',role:'CHECKER'});
25 checker.approvePayment('p1');
26 checker.queuePayment('p1');
27 return checker;
28 }
29
30 describe('bank status polling',()=>{
31 it('moves rejected submitted payment to failed',async()=>{
32 const s=prepared();
33 const adapter:BankAdapter={submit:vi.fn().mockResolvedValue({outcome:'ACCEPTED',bankReference:'BR-1'}),poll:vi.fn().mockResolvedValue('REJECTED')};
34 await s.dispatchPayment('p1',adapter);
35 const p=await s.pollPaymentStatus('p1',adapter);
36 expect(p.state).toBe('FAILED');
37 });
38 it('moves unknown poll outcome to UNKNOWN for manual review',async()=>{
39 const s=prepared();
40 const adapter:BankAdapter={submit:vi.fn().mockResolvedValue({outcome:'ACCEPTED',bankReference:'BR-1'}),poll:vi.fn().mockResolvedValue('UNKNOWN')};
41 await s.dispatchPayment('p1',adapter);
42 const p=await s.pollPaymentStatus('p1',adapter);
43 expect(p.state).toBe('UNKNOWN');
44 });
45 });