@davoodmehraban89 / Factor-easy / commits / 8b6b2ca
Fork 0

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

test: cover sale and purchase server autopost retries and sync guards

davoodmehraban89 committed Nov 17, 2026 at 13:53 UTC 8b6b2ca752491a153f10a7aea9c89eae7c02b892
1 file changed +82
scripts/journal-rpc-adapter.test.mjs
+82
@@ -133,3 +133,85 @@ test('fresh membership and pending generic sync are hard preconditions before mu
133 await assert.rejects(()=>pending.context.jeRpcCreateDraft({date:'1405/07/19',lines:[{accountId:'A1',debit:1,credit:0},{accountId:'A2',debit:0,credit:1}]}),/همگام‌سازی/);
134 assert.equal(pending.calls.length,0);
135 });
136 +
137 +
138 +function sourceHarness(kind='sale',{journalDirty=false,failSourceSync=false,loseFirstResponse=false}={}){
139 + const h=harness(),collection=kind==='sale'?'invoices':'purchases';
140 + const source={id:kind==='sale'?'INV_SOURCE_1':'PUR_SOURCE_1',ownerUserId:'USER_A',companyId:'CO_A',
141 + date:'1405/07/19',number:'SRC-1',description:'synthetic source',reference:'REF-1',
142 + grandTotal:100,accountingVersion:1,kind:kind==='sale'?'formal':undefined,
143 + items:[{qty:1,price:100,lineTotal:100}],documentStatus:'draft'};
144 + const rows={[collection]:[source]};
145 + h.context.getMyInvoices=()=>rows.invoices||[];
146 + h.context.getMyPurchases=()=>rows.purchases||[];
147 + h.context.stripOwner=item=>{const x={...item};delete x.ownerUserId;delete x._serverVersion;return x;};
148 + h.context.syncSnap=new Map();
149 + h.context.syncRunning=false;h.context.syncPending=false;h.context.syncFailCount=0;
150 + h.context.computeDiff=()=>journalDirty?{up:[{coll:'journalLines',key:'journalLines|LOCAL'}],del:[]}:{up:[],del:[]};
151 + h.context.saveDatastore=()=>{h.context.syncPending=true;};
152 + h.context.runSync=async()=>{
153 + if(failSourceSync){h.context.syncFailCount=1;h.context.syncPending=true;return;}
154 + h.context.syncSnap.set(collection+'|'+source.id,JSON.stringify(h.context.stripOwner(source)));
155 + h.context.syncPending=false;
156 + };
157 + h.context.pullAll=async()=>{};
158 + let first=true;
159 + h.context.sb.rpc=async(name,args)=>{
160 + h.calls.push({name,args});
161 + if(name==='finora_journal_autopost'){
162 + if(loseFirstResponse&&first){first=false;throw new Error('network response lost after server commit');}
163 + return {data:{voucher:{id:'JV_SERVER_'+kind,status:'posted',version:2,postedAt:'1405-07-19T10:00:00Z'}},error:null};
164 + }
165 + return {data:null,error:null};
166 + };
167 + return {...h,source,rows,collection};
168 +}
169 +
170 +test('sale auto-post persists source first and delegates authoritative lines to the dedicated RPC',async()=>{
171 + const h=sourceHarness('sale');
172 + const v=await h.context.jeRpcAutoPost('sale',h.source.id);
173 + const call=h.calls.find(x=>x.name==='finora_journal_autopost');
174 + assert.ok(call);
175 + assert.equal(call.args.p_organization_id,'ORG_A');
176 + assert.equal(call.args.p_source_collection,'invoices');
177 + assert.equal(call.args.p_source_id,h.source.id);
178 + assert.equal(call.args.p_header.sourceType,'sale');
179 + assert.equal(call.args.p_header.sourceVersion,'1');
180 + assert.equal(call.args.p_header.companyId,'CO_A');
181 + assert.deepEqual(Array.from(call.args.p_lines),[]);
182 + assert.equal(v.id,'JV_SERVER_sale');
183 + assert.equal(h.calls.some(x=>x.name==='finora_sync_records'),false);
184 +});
185 +
186 +test('purchase auto-post uses the same server-owned source contract with purchases scope',async()=>{
187 + const h=sourceHarness('purchase');
188 + const v=await h.context.jeRpcAutoPost('purchase',h.source.id);
189 + const call=h.calls.find(x=>x.name==='finora_journal_autopost');
190 + assert.equal(call.args.p_source_collection,'purchases');
191 + assert.equal(call.args.p_header.sourceType,'purchase');
192 + assert.equal(call.args.p_header.sourceId,h.source.id);
193 + assert.equal(v.id,'JV_SERVER_purchase');
194 +});
195 +
196 +test('auto-post fails closed when local generic sync contains dirty journal rows',async()=>{
197 + const h=sourceHarness('sale',{journalDirty:true});
198 + await assert.rejects(()=>h.context.jeRpcAutoPost('sale',h.source.id),/مسیر قدیمی/);
199 + assert.equal(h.calls.some(x=>x.name==='finora_journal_autopost'),false);
200 +});
201 +
202 +test('auto-post never calls server when source draft persistence is not acknowledged',async()=>{
203 + const h=sourceHarness('sale',{failSourceSync:true});
204 + await assert.rejects(()=>h.context.jeRpcAutoPost('sale',h.source.id),/تأیید ذخیره رکورد منبع/);
205 + assert.equal(h.calls.some(x=>x.name==='finora_journal_autopost'),false);
206 +});
207 +
208 +test('ambiguous auto-post retry reuses source identity and accepts server idempotent replay',async()=>{
209 + const h=sourceHarness('sale',{loseFirstResponse:true});
210 + await assert.rejects(()=>h.context.jeRpcAutoPost('sale',h.source.id),/network response lost/);
211 + const v=await h.context.jeRpcAutoPost('sale',h.source.id);
212 + const calls=h.calls.filter(x=>x.name==='finora_journal_autopost');
213 + assert.equal(calls.length,2);
214 + assert.equal(calls[0].args.p_source_id,calls[1].args.p_source_id);
215 + assert.equal(calls[0].args.p_source_collection,calls[1].args.p_source_collection);
216 + assert.equal(v.id,'JV_SERVER_sale');
217 +});