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

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

test: cover ambiguous source freeze and definitive rejection recovery

davoodmehraban89 committed Nov 17, 2026 at 14:10 UTC fd37cf5fd17bf35a5304e9e2286f472bb8761f93
1 file changed +23 -1
scripts/journal-rpc-adapter.test.mjs
+23 -1
@@ -135,7 +135,7 @@ test('fresh membership and pending generic sync are hard preconditions before mu
135 });
136
137
138 -function sourceHarness(kind='sale',{journalDirty=false,failSourceSync=false,loseFirstResponse=false}={}){
138 +function sourceHarness(kind='sale',{journalDirty=false,failSourceSync=false,loseFirstResponse=false,rejectWithCode=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',
@@ -159,6 +159,7 @@ function sourceHarness(kind='sale',{journalDirty=false,failSourceSync=false,lose
159 h.context.sb.rpc=async(name,args)=>{
160 h.calls.push({name,args});
161 if(name==='finora_journal_autopost'){
162 + if(rejectWithCode)return {data:null,error:{message:'synthetic posting validation failure',code:'23514'}};
163 if(loseFirstResponse&&first){first=false;throw new Error('network response lost after server commit');}
164 return {data:{voucher:{id:'JV_SERVER_'+kind,status:'posted',version:2,postedAt:'1405-07-19T10:00:00Z'}},error:null};
165 }
@@ -214,7 +215,9 @@ test('auto-post never calls server when source draft persistence is not acknowle
215 test('ambiguous auto-post retry reuses source identity and accepts server idempotent replay',async()=>{
216 const h=sourceHarness('sale',{loseFirstResponse:true});
217 await assert.rejects(()=>h.context.jeRpcAutoPost('sale',h.source.id),/network response lost/);
218 + assert.equal(h.source.accountingPostingPending,true);
219 const v=await h.context.jeRpcAutoPost('sale',h.source.id);
220 + assert.equal(h.source.accountingPostingPending,undefined);
221 const calls=h.calls.filter(x=>x.name==='finora_journal_autopost');
222 assert.equal(calls.length,2);
223 assert.equal(calls[0].args.p_source_id,calls[1].args.p_source_id);
@@ -266,3 +269,22 @@ test('initial purchase creation uses the server autopost path and remains retrya
269 purchaseSource.indexOf('window.deletePurchasePilotDraft',purchaseSource.indexOf('window.finalizePurchasePilotDraft=async function')));
270 assert.doesNotMatch(finalizer,/!p\.correctsPurchaseId/);
271 });
272 +
273 +
274 +test('explicit server rejection clears the source freeze so a draft can be corrected',async()=>{
275 + const h=sourceHarness('sale',{rejectWithCode:true});
276 + await assert.rejects(()=>h.context.jeRpcAutoPost('sale',h.source.id),/synthetic posting validation failure/);
277 + assert.equal(h.source.accountingPostingPending,undefined);
278 + assert.equal(h.source.documentStatus,'draft');
279 + assert.equal(h.calls.filter(x=>x.name==='finora_journal_autopost').length,1);
280 +});
281 +
282 +test('source edit and delete surfaces freeze while autopost outcome is ambiguous',async()=>{
283 + const engine=await readFile(new URL('../js/journal-engine.js',import.meta.url),'utf8');
284 + const invoices=await readFile(new URL('../js/invoices.js',import.meta.url),'utf8');
285 + const purchases=await readFile(new URL('../js/purchases.js',import.meta.url),'utf8');
286 + assert.match(engine,/source\?\.accountingPostingPending/);
287 + assert.match(invoices,/!inv\.accountingPostingPending/);
288 + assert.match(invoices,/inv\.accountingPostingPending\)/);
289 + assert.match(purchases,/p\.accountingPostingPending/);
290 +});