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

test(accounting): verify authoritative manual journal RPC contracts

davoodmehraban89 committed Nov 16, 2026 at 07:09 UTC 9b2da4066207cd03dc186f14ccdd2793ee944441
3 files changed +88
.github/workflows/quality.yml
+2
@@ -55,6 +55,8 @@ jobs:
55 run: node scripts/phase3-behavior-check.mjs
56 - name: Accounting P0 dimensions and journal invariants
57 run: node scripts/accounting-p0-check.mjs
58 + - name: Authoritative manual journal RPC adapter contracts
59 + run: node --test scripts/journal-rpc-adapter.test.mjs
60 - name: Phase 4 accounting invariants
61 run: node scripts/phase4-behavior-check.mjs
62 - name: Phase 5 complementary ERP invariants
js/journal-rpc.js
+1
@@ -31,6 +31,7 @@ function jeRpcLines(lines){
31 }
32 async function jeRpcCall(name,args){
33 await jeRpcAssertContext();
34 + if(syncPending||syncRunning)throw new Error('ابتدا همگام‌سازی تغییرات دیگر را کامل کنید؛ عملیات دفتر کل برای جلوگیری از بازنویسی داده معلق متوقف شد.');
35 const {data,error}=await sb.rpc(name,args);
36 if(error){const e=new Error(error.message||'عملیات دفتر کل در سرور انجام نشد.');e.code=error.code||'';throw e;}
37 return data;
scripts/journal-rpc-adapter.test.mjs new
+85
@@ -0,0 +1,85 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import {readFile} from 'node:fs/promises';
4 +import vm from 'node:vm';
5 +
6 +const source=await readFile(new URL('../js/journal-rpc.js',import.meta.url),'utf8');
7 +
8 +function harness({vouchers=[],statuses=[],failMembership=false,pending=false}={}){
9 + const calls=[],records={journalVouchers:vouchers.map(x=>({...x})),journalLines:[],journalLineDimensions:[]};
10 + let idCounter=0;
11 + const context={
12 + currentUser:{id:'USER_A',organizationId:'ORG_A'},
13 + window:{requireFreshFinoraMembership:async()=>!failMembership},
14 + syncPending:pending,syncRunning:false,
15 + requireWrite:()=>true,afCompanyId:()=> 'CO_A',
16 + getJalaliNumeric:()=> '1405/07/19',toEnDigits:x=>String(x),
17 + getMyFiscalYears:()=>[{id:'FY_A',startDate:'1405/01/01',endDate:'1405/12/29',status:'open'}],
18 + jeFiscalForDate:()=>({id:'FY_A',startDate:'1405/01/01',endDate:'1405/12/29',status:'open'}),
19 + jeValidateLines:()=>true,jeNum:x=>Number(x)||0,afId:()=> 'JV_'+(++idCounter),
20 + getMyJournalVouchers:()=>records.journalVouchers,
21 + jeVoucherDraftInput:id=>({voucher:records.journalVouchers.find(v=>v.id===id),lines:[]}),
22 + pullAll:async()=>{},
23 + sb:{
24 + rpc:async(name,args)=>{calls.push({name,args});if(name==='finora_journal_create_draft')return {data:{voucher:{id:args.p_voucher_id,version:1,status:'draft'}},error:null};return {data:{voucher:{id:args.p_voucher_id||args.p_original_id,version:8,status:'posted'}},error:null};},
25 + from:()=>({select(){return this;},eq(){return this;},then(resolve){return Promise.resolve({data:statuses,error:null}).then(resolve);}})
26 + },
27 + console
28 + };
29 + vm.createContext(context);vm.runInContext(source,context);
30 + return {context,calls,records};
31 +}
32 +
33 +test('manual draft create calls only the dedicated server RPC with active organization scope',async()=>{
34 + const h=harness();
35 + const result=await h.context.jeRpcCreateDraft({date:'1405/07/19',description:'manual',voucherType:'manual',lines:[{accountId:'A1',debit:100,credit:0},{accountId:'A2',debit:0,credit:100}]});
36 + assert.equal(h.calls.length,1);
37 + assert.equal(h.calls[0].name,'finora_journal_create_draft');
38 + assert.equal(h.calls[0].args.p_organization_id,'ORG_A');
39 + assert.equal(h.calls[0].args.p_header.companyId,'CO_A');
40 + assert.equal(h.calls[0].args.p_header.fiscalYearId,'FY_A');
41 + assert.equal(h.calls[0].args.p_header.date,'1405/07/19');
42 + assert.equal(h.calls[0].args.p_lines.length,2);
43 + assert.equal(h.calls.some(x=>x.name==='finora_sync_records'),false);
44 + assert.equal(result.id,'JV_1');
45 +});
46 +
47 +test('draft update uses the server row version as compare-and-swap token',async()=>{
48 + const h=harness({vouchers:[{id:'JV_D',status:'draft',sourceType:'manual',companyId:'CO_A',fiscalYearId:'FY_A',_serverVersion:7}]});
49 + await h.context.jeRpcUpdateDraft('JV_D',{date:'1405/07/19',description:'edited',voucherType:'manual',lines:[{accountId:'A1',debit:100,credit:0},{accountId:'A2',debit:0,credit:100}]});
50 + assert.equal(h.calls[0].name,'finora_journal_update_draft');
51 + assert.equal(h.calls[0].args.p_expected_version,7);
52 +});
53 +
54 +test('post and delete use dedicated RPCs and exact server version',async()=>{
55 + const h=harness({vouchers:[{id:'JV_D',status:'draft',sourceType:'manual',companyId:'CO_A',fiscalYearId:'FY_A',_serverVersion:12}]});
56 + await h.context.jeRpcPost('JV_D');
57 + assert.equal(h.calls[0].name,'finora_journal_post');
58 + assert.equal(h.calls[0].args.p_expected_version,12);
59 + h.calls.length=0;
60 + h.records.journalVouchers[0].status='draft';
61 + h.records.journalVouchers[0]._serverVersion=13;
62 + await h.context.jeRpcDeleteDraft('JV_D');
63 + assert.equal(h.calls[0].name,'finora_journal_delete_draft');
64 + assert.equal(h.calls[0].args.p_expected_version,13);
65 +});
66 +
67 +test('reverse is rejected after full/partial reversal and only sends null allocations for untouched originals',async()=>{
68 + const h=harness({vouchers:[{id:'JV_O',status:'posted',sourceType:'manual',_serverVersion:4}]});
69 + await h.context.jeRpcReverse('JV_O','1405/07/19','reason');
70 + assert.equal(h.calls[0].name,'finora_journal_reverse');
71 + assert.equal(h.calls[0].args.p_expected_version,4);
72 + assert.equal(h.calls[0].args.p_allocations,null);
73 + const partial=harness({vouchers:[{id:'JV_O',status:'posted',sourceType:'manual',_serverVersion:4}],statuses:[{voucher_id:'JV_O',effective_status:'partially_reversed'}]});
74 + await assert.rejects(()=>partial.context.jeRpcReverse('JV_O','1405/07/19','reason'),/برگشت جزئی/);
75 + assert.equal(partial.calls.length,0);
76 +});
77 +
78 +test('fresh membership and pending generic sync are hard preconditions before mutation',async()=>{
79 + const revoked=harness({failMembership:true});
80 + await assert.rejects(()=>revoked.context.jeRpcCreateDraft({date:'1405/07/19',lines:[{accountId:'A1',debit:1,credit:0},{accountId:'A2',debit:0,credit:1}]}),/عضویت فعال/);
81 + assert.equal(revoked.calls.length,0);
82 + const pending=harness({pending:true});
83 + await assert.rejects(()=>pending.context.jeRpcCreateDraft({date:'1405/07/19',lines:[{accountId:'A1',debit:1,credit:0},{accountId:'A2',debit:0,credit:1}]}),/همگام‌سازی/);
84 + assert.equal(pending.calls.length,0);
85 +});