فینورا پرو - سامانه جامع حسابداری و صدور فاکتور

test(A01): prove rollback when refund correction insert fails late

davoodmehraban89 committed Oct 20, 2026 at 17:27 UTC e36ac8199e955fc20f8275fcc0450cb6671aa5e7
1 file changed +37
tests/retail-phase2.test.mjs
+37
@@ -718,3 +718,40 @@ test('A01: supplier recovery trace is linked; rejected over-recovery adds no cor
718 const trial=await accounts.trialBalance();
719 assert.equal(trial.totals.debit,trial.totals.credit);
720 });
721 +
722 +test('A01: late correction idempotency collision rolls back cash, journal, settlement and audit',async()=>{
723 + await setup();await purchase10();
724 + const sold=await retail.issueInvoice(invoice('sale',3,200,{paidAmount:500}));
725 + const returned=await retail.issueInvoice(invoice('sale_return',1,200,{
726 + originalInvoiceId:sold.id
727 + }));
728 + const id='a01-late-correction-conflict';
729 + // Fixture simulates an existing append-only correction key independently of
730 + // the treasury ID; the UNIQUE constraint must abort the entire later write.
731 + await db.transaction('document_corrections',async tx=>{
732 + await tx.add('document_corrections',{
733 + id:'a01-existing-conflict',companyId:company,
734 + sourceType:'invoice',sourceId:returned.id,
735 + correctionType:'settlement_refund',resultType:'treasury',resultId:'historical',
736 + idempotencyKey:JSON.stringify([company,'settlement_refund',id])
737 + });
738 + });
739 + const before={
740 + journals:(await db.getAll('journal_entries')).length,
741 + audits:(await db.getAll('audit_events')).length,
742 + settlements:(await db.getAll('settlements')).length,
743 + corrections:(await db.getAll('document_corrections')).length
744 + };
745 + await assert.rejects(retail.postTransaction({
746 + id,type:'payment',date,amount:100,refInvoiceId:returned.id,
747 + contactId:'contact-1',fromAccountId:'cash-1'
748 + }));
749 + assert.equal(await db.get('treasury',id),undefined);
750 + assert.equal((await db.get('invoices',returned.id)).paidAmount,0);
751 + assert.equal((await db.getAll('journal_entries')).length,before.journals);
752 + assert.equal((await db.getAll('audit_events')).length,before.audits);
753 + assert.equal((await db.getAll('settlements')).length,before.settlements);
754 + assert.equal((await db.getAll('document_corrections')).length,before.corrections);
755 + const trial=await accounts.trialBalance();
756 + assert.equal(trial.totals.debit,trial.totals.credit);
757 +});