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

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

test(accounting): enforce draft-only purchase Save and seven first-save regressions

davoodmehraban89 committed Nov 24, 2026 at 10:41 UTC 0bc8e86a23263602663476ce30a6814afdfcc3cd
1 file changed +81 -3
scripts/journal-rpc-adapter.test.mjs
+81 -3
@@ -282,17 +282,18 @@ test('sync hydration reads database row version required by journal CAS operatio
282 });
283
284
285 -test('initial purchase creation uses the server autopost path and remains retryable as a draft',async()=>{
285 +test('initial purchase Save creates a linked draft without posting; explicit finalizer posts separately',async()=>{
286 const purchaseSource=await readFile(new URL('../js/purchases.js',import.meta.url),'utf8');
287 const start=purchaseSource.indexOf('window.purSave=async function(){');
288 const end=purchaseSource.indexOf('window.purCancelEdit=function(){',start);
289 assert.ok(start>=0&&end>start);
290 const saveFlow=purchaseSource.slice(start,end);
291 assert.match(saveFlow,/accountingStatus:'draft',documentStatus:'draft'/);
292 - assert.match(saveFlow,/jeRpcAutoPost\('purchase',record\.id\)/);
293 - assert.doesNotMatch(saveFlow,/jePostSourceRecord/);
292 + assert.match(saveFlow,/jeRpcCreateSourceDraft\('purchase',record\.id\)/);
293 + assert.doesNotMatch(saveFlow,/jeRpcAutoPost|jeRpcPost\(|jePostSourceRecord|jePost\(/);
294 const finalizer=purchaseSource.slice(purchaseSource.indexOf('window.finalizePurchasePilotDraft=async function'),
295 purchaseSource.indexOf('window.deletePurchasePilotDraft',purchaseSource.indexOf('window.finalizePurchasePilotDraft=async function')));
296 + assert.match(finalizer,/jeRpcAutoPost\('purchase',id\)/);
297 assert.doesNotMatch(finalizer,/!p\.correctsPurchaseId/);
298 });
299
@@ -351,3 +352,80 @@ test('client fails closed if the server posts a voucher without atomically linki
352 assert.equal(h.source.accountingPostingPending,true);
353 assert.equal(h.source.journalVoucherId,undefined);
354 });
355 +
356 +
357 +// Focused regression for PR219 first-save path. Existing legacy autopost
358 +// behavioral cases above remain visible until the finalize contract is repaired.
359 +function sourceDraftHarness(kind='sale',options={}){
360 + const h=harness(),collection=kind==='sale'?'invoices':'purchases';
361 + let row={id:'SOURCE_1',ownerUserId:'USER_A',companyId:'CO_A',date:'1405/07/19',
362 + accountingVersion:1,documentStatus:'draft',accountingStatus:'draft'};
363 + let persisted=0,serverVoucher=null;
364 + h.context.getMyInvoices=()=>kind==='sale'?[row]:[];
365 + h.context.getMyPurchases=()=>kind==='purchase'?[row]:[];
366 + h.context.jeRpcPersistSourceRecord=async(coll,id)=>{
367 + assert.equal(coll,collection);assert.equal(id,row.id);persisted++;
368 + if(options.onPersist)await options.onPersist(h,row);
369 + row={...row,...options.refreshed};
370 + return row;
371 + };
372 + h.context.sb.rpc=async(name,args)=>{
373 + h.calls.push({name,args});
374 + assert.equal(name,'finora_journal_create_source_draft','Save must never post');
375 + assert.equal(args.p_source_collection,collection);
376 + serverVoucher={id:args.p_voucher_id,status:'draft',sourceType:kind,sourceId:row.id,version:1};
377 + return {data:{voucher:serverVoucher},error:null};
378 + };
379 + h.context.pullAll=async()=>{
380 + if(!options.noLink)row={...row,journalVoucherId:serverVoucher.id,documentStatus:'draft',accountingStatus:'draft'};
381 + h.records.journalVouchers=[serverVoucher];
382 + };
383 + return {...h,collection,row:()=>row,persisted:()=>persisted};
384 +}
385 +for(const kind of ['sale','purchase']){
386 + test('source draft first Save: '+kind+' persists and creates only a linked draft with refreshed values',async()=>{
387 + const h=sourceDraftHarness(kind,{refreshed:{accountingVersion:2,date:'1405/07/20',description:'refreshed source'}});
388 + const v=await h.context.jeRpcCreateSourceDraft(kind,'SOURCE_1');
389 + assert.equal(h.persisted(),1);assert.equal(h.calls.length,1);
390 + const args=h.calls[0].args;
391 + assert.equal(args.p_organization_id,'ORG_A');assert.equal(args.p_header.companyId,'CO_A');
392 + assert.equal(args.p_header.sourceVersion,'2');assert.equal(args.p_header.date,'1405/07/20');
393 + assert.equal(args.p_header.description,'refreshed source');assert.equal(args.p_header.sourceType,kind);
394 + assert.deepEqual(Array.from(args.p_lines),[]);
395 + assert.equal(v.status,'draft');assert.equal(h.row().journalVoucherId,v.id);
396 + assert.equal(h.row().accountingStatus,'draft');assert.equal(h.row().documentStatus,'draft');
397 + });
398 +}
399 +test('source draft first Save rejects changed active company after persistence',async()=>{
400 + const h=sourceDraftHarness('sale',{onPersist:async h=>{h.context.afCompanyId=()=> 'CO_B';}});
401 + await assert.rejects(()=>h.context.jeRpcCreateSourceDraft('sale','SOURCE_1'),/شرکت فعال.*تغییر کرد/);
402 + assert.equal(h.persisted(),1);assert.equal(h.calls.length,0);
403 +});
404 +test('source draft first Save rejects invalid refreshed version before RPC',async()=>{
405 + const h=sourceDraftHarness('purchase',{refreshed:{accountingVersion:0}});
406 + await assert.rejects(()=>h.context.jeRpcCreateSourceDraft('purchase','SOURCE_1'),/نسخه منبع معتبر نیست/);
407 + assert.equal(h.calls.length,0);
408 +});
409 +test('source draft first Save rejects a failed source flush and releases serialization',async()=>{
410 + let fail=true;
411 + const h=sourceDraftHarness('sale',{onPersist:async()=>{if(fail)throw new Error('fixture persistence failed');}});
412 + await assert.rejects(()=>h.context.jeRpcCreateSourceDraft('sale','SOURCE_1'),/fixture persistence failed/);
413 + assert.equal(h.calls.length,0);
414 + fail=false;
415 + assert.equal((await h.context.jeRpcCreateSourceDraft('sale','SOURCE_1')).status,'draft');
416 +});
417 +test('source draft first Save serializes concurrent requests before source flush',async()=>{
418 + let release;const gate=new Promise(resolve=>{release=resolve;});
419 + const h=sourceDraftHarness('purchase',{onPersist:async()=>{await gate;}});
420 + const first=h.context.jeRpcCreateSourceDraft('purchase','SOURCE_1');
421 + try{
422 + await assert.rejects(()=>h.context.jeRpcCreateSourceDraft('purchase','SOURCE_1'),/در حال اجراست/);
423 + }finally{release();}
424 + assert.equal((await first).status,'draft');
425 + assert.equal(h.persisted(),1);assert.equal(h.calls.length,1);
426 +});
427 +test('source draft first Save fails closed when the server returns a voucher without a source link',async()=>{
428 + const h=sourceDraftHarness('sale',{noLink:true});
429 + await assert.rejects(()=>h.context.jeRpcCreateSourceDraft('sale','SOURCE_1'),/ارتباط پیش‌نویس منبع و سند حسابداری از سرور تأیید نشد/);
430 + assert.equal(h.calls.length,1);assert.equal(h.row().journalVoucherId,undefined);
431 +});