@davoodmehraban89 / Factor-easy / commits / f9956e4
Fork 0

سامانه صدور فاکتور و حسابداری

Add Phase 3 behavioral accounting tests

davoodmehraban89 committed Sep 29, 2026 at 12:03 UTC f9956e4a93a1c1d599d8b71f87bbbd7b23e22d41
1 file changed +29
scripts/phase3-behavior-check.mjs new
+29
@@ -0,0 +1,29 @@
1 +import fs from 'node:fs';
2 +import vm from 'node:vm';
3 +const source=fs.readFileSync('js/journal-engine.js','utf8');
4 +let seq=0;
5 +const datastore={postingProfiles:[],journalVouchers:[],journalLines:[],journalLineDimensions:[]};
6 +const accounts=[
7 +{id:'cash',code:'1101',title:'بانک',active:true,postingAllowed:true},{id:'ar',code:'1102',title:'دریافتنی',active:true,postingAllowed:true},
8 +{id:'ap',code:'2101',title:'پرداختنی',active:true,postingAllowed:true},{id:'vatp',code:'2102',title:'مالیات پرداختنی',active:true,postingAllowed:true},
9 +{id:'rev',code:'4101',title:'فروش',active:true,postingAllowed:true},{id:'buy',code:'5101',title:'خرید',active:true,postingAllowed:true},
10 +{id:'exp',code:'6103',title:'هزینه',active:true,postingAllowed:true},{id:'inc',code:'4201',title:'سایر درآمد',active:true,postingAllowed:true}];
11 +const fiscal=[{id:'FY1',startDate:'1405/01/01',endDate:'1405/12/29',status:'open'}];
12 +const ctx={console,datastore,currentUser:{id:'U1'},requireWrite:()=>true,afCompanyId:()=> 'C1',afId:p=>p+'_'+(++seq),toEnDigits:String,getJalaliNumeric:()=> '1405/07/07',
13 +getMyFiscalYears:()=>fiscal,getMyAccounts:()=>accounts,getMyAccountDimensionRules:()=>[],getMyDimensionTypes:()=>[],afResolvedValues:()=>[],saveDatastore:()=>true,
14 +getMyInvoices:()=>[],getMyPurchases:()=>[],getMyPayments:()=>[],getMyExpenses:()=>[]};
15 +vm.createContext(ctx);vm.runInContext(source,ctx);
16 +const assert=(ok,msg)=>{if(!ok)throw new Error(msg)};
17 +ctx.jeEnsureDefaultProfiles();
18 +const receipt={id:'PAY1',date:'1405/07/07',amount:125000,contactId:'CUST1'};
19 +const v=ctx.jePostSourceRecord('receipt',receipt);
20 +assert(v.status==='posted','receipt must post');
21 +const lines=datastore.journalLines.filter(x=>x.voucherId===v.id);
22 +assert(lines.reduce((s,x)=>s+x.debit,0)===lines.reduce((s,x)=>s+x.credit,0),'receipt must balance');
23 +assert(v.fiscalYearId==='FY1','Jalali fiscal year resolution failed');
24 +let dup=false;try{ctx.jePostSourceRecord('receipt',{...receipt,journalVoucherId:''})}catch(_){dup=true}assert(dup,'duplicate source/version must fail');
25 +const rv=ctx.jeReverse(v.id,'1405/07/08','test reversal');
26 +assert(v.status==='reversed'&&rv.status==='posted','reversal lifecycle failed');
27 +assert(ctx.jeTrialBalance().every(x=>Math.abs(x.balance)<0.0001),'reversal must net original ledger to zero');
28 +fiscal[0].status='locked';let locked=false;try{ctx.jePostSourceRecord('expense',{id:'EXP1',date:'1405/07/09',amount:1000})}catch(_){locked=true}assert(locked,'locked fiscal year must reject posting');
29 +console.log('Phase 3 behavioral accounting scenarios passed.');