سامانه صدور فاکتور و حسابداری
test(finora): expose receipt and payment correction auto-post regressions
davoodmehraban89 committed
Nov 22, 2026 at 23:43 UTC
3c9906b872656d33fe7389b02cfce6d4fc9454ed
3 files changed
+68
-31
PROJECT_STATE.md
+5
@@ -738,3 +738,8 @@ Next action: run and inspect Quality on the latest PR212 head. If any browser te
738
- **Scope of next QA correction:** keep the UI-only no-write-on-open/reference-preservation regression as a normal PASS; replace the misleading auto-post acceptance with strict browser expected-RED tests for both inbound receipt and outbound payment corrections. Drive the actual edit→Save DOM path, preserve original source and original voucher lines, require the reversal history to remain, require exactly one source-linked balanced **DRAFT** correction voucher, and require that draft not be counted as an effective posted treasury balance. Current exact defect fingerprint: correction source and linked voucher immediately `posted`, with original reversal present. A third state (orphan, unbalanced, multiple links, missing reversal/history, partial sync) must be an ordinary CI failure before xfail. A future correct state must force removal of xfail and become normal PASS.
739
- **Important design alignment:** the existing purchase-correction expected-RED contract expects the original reversal history to be retained while the *new* correction stays DRAFT; this payment test must use the same distinction. It must not silently downgrade or delete the original posted voucher.
740
- **Files allowed:** existing `scripts/accounting-document-workflow.spec.js`, `PROJECT_STATE.md`, `docs/EXECUTION_STATUS.md` only. No app `js/`, SQL/migrations, workflows, server/Auth/RLS/grants, financial data or history changes. Verify final exact SHA Quality/WP1/WP2/Security if triggered; report product defect separately from green QA.
741
+
742
+### 2026-11-22 — FIN-QA-ISSUE238-PAYMENT-CORRECTION / browser regression added
743
+- Replaced the misleading normal-PASS treasury correction test that celebrated immediate reposting. Retained its independent UI normal-PASS coverage for opening/cancelling the edit form without ledger writes and without mutating the original source/lines.
744
+- Added two actual DOM Save expected-RED tests: inbound receipt correction and outbound payment correction. They seed a real posted source+voucher through the existing browser harness, invoke the loaded edit→Save path, and inspect synchronized source/voucher/lines. Exact current fingerprint requires original source and original journal lines unchanged, original reversal retained, correction source+linked voucher both POSTED, balanced lines, one correction link, three total vouchers and effective balance incorrectly includes the new correction. Required future state keeps the reversal/history, leaves the new source+linked balanced voucher DRAFT, creates no posted correction, and excludes the unapproved draft from effective posted balance.
745
+- Strict `assertKnownRedOrFixed` rejects any unknown/orphan/unbalanced/partial state before xfail; a corrected state is a normal unexpected PASS that requires removal of `test.fail`. Marker: `FINORA_ISSUE238_PAYMENT_CORRECTION_EXPECTED_RED`. QA-only; no runtime, SQL/RPC/schema/RLS/Auth/grants, main, production or historical data touched. Final code SHA must rerun exact-head CI.
docs/EXECUTION_STATUS.md
+6
@@ -219,3 +219,9 @@
219
- یک آزمون مرورگر قبلی بهاشتباه ثبت اصلاح دریافت/پرداخت را با برگشت سند قبلی و قطعیسازی فوری سند اصلاحی، PASS عادی میدانست؛ این رفتار با ADR216 سازگار نیست. مسیر فعال Save از `paymentAmendPosted` در `js/payments.js` عبور میکند.
220
- محدودهٔ مجاز فقط QA است: آزمون عادی بازکردن فرم و عدم ایجاد ثبت در زمان بازکردن حفظ میشود؛ برای اصلاح دریافت و پرداخت دو آزمون مرورگر expected-RED ساخته میشود. اصل سند و آرتیکلهای قطعی آن باید حفظ شوند؛ سابقهٔ برگشت باقی بماند؛ سند اصلاحی جدید باید متصل، متوازن و DRAFT باشد و تا تأیید حسابداری در ماندهٔ قطعی خزانه محاسبه نشود.
221
- هیچ مجوز تازهای برای تغییر SQL/RPC، اسکیما، RLS/Auth، کد اجرایی مالی، main یا Production وجود ندارد. فقط فایل آزمون موجود و اسناد وضعیت مجازند.
222
+
223
+
224
+### ۲۰۲۶/۱۱/۲۲ — آزمون مرورگر اصلاح دریافت و پرداخت
225
+- آزمون قبلی که قطعیسازی فوری اصلاح خزانه را PASS میدانست، حذف و به دو بخش تفکیک شد: آزمون عادی عدم ایجاد ثبت هنگام بازکردن/لغو فرم؛ دو آزمون expected-RED برای اصلاح دریافت و پرداخت با مسیر واقعی Save.
226
+- آزمونها اصل سند و آرتیکلهای تاریخی را حفظ میکنند و بررسی میکنند سند اصلاحی جدید با آرتیکل متوازن بهصورت DRAFT متصل بماند، تا قبل از تأیید وارد ماندهٔ قطعی نشود. رفتار فعلی که سند اصلاحی را همان لحظه قطعی میکند با نشانگر `FINORA_ISSUE238_PAYMENT_CORRECTION_EXPECTED_RED` ثبت میشود. هر حالت سوم یا ناقص، شکست واقعی CI است.
227
+- تغییر فقط در مجموعه آزمون مرورگر و اسناد وضعیت است؛ هیچ کد مالی اجرایی، SQL/RPC، مجوز، اسکیما یا دادهای تغییر نکرده است.
scripts/accounting-document-workflow.spec.js
+57
-31
@@ -676,11 +676,11 @@ async function createUiPayment(page,server,amount=1000,direction='inbound'){
676
return remotePayments(server)[0].id;
677
}
678
679
-test('treasury amendment in the same form preserves one record and same reference without on-open ledger writes',async({page})=>{
679
+test('treasury amendment edit form opens without on-open ledger writes or changing the original',async({page})=>{
680
const server=fakeServer();await installHarness(page,server);await authenticatedMockBrowser(page);
681
const firstId=await createUiPayment(page,server);
682
await page.waitForFunction(()=>!syncPending&&!syncRunning);
683
- const first=copy(remotePayments(server)[0]);
683
+ const first=copy(remotePayments(server).find(p=>p.id===firstId));
684
const oldLines=[...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===first.journalVoucherId).map(copy);
685
const beforeRpc=server.rpcCount;
686
await expect(page.locator('#payments-ledger tr')).toHaveCount(1);
@@ -692,43 +692,69 @@ test('treasury amendment in the same form preserves one record and same referenc
692
await page.evaluate(()=>resetPaymentForm());
693
expect(server.rpcCount).toBe(beforeRpc);
694
expect([...server.records.values()].filter(x=>x.collection==='journalVouchers')).toHaveLength(1);
695
- expect(remotePayments(server)).toHaveLength(1);
695
+ expect(remotePayments(server)).toEqual([first]);
696
+ expect(oldLines).toEqual([...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===first.journalVoucherId).map(copy));
697
+});
698
+
699
+for(const direction of ['inbound','outbound'])test('P0 #238 expected RED: '+direction+' treasury correction Save must leave the new linked journal DRAFT',async({page})=>{
700
+ const server=fakeServer();await installHarness(page,server);await authenticatedMockBrowser(page);
701
+ const firstId=await createUiPayment(page,server,1000,direction);
702
+ await page.waitForFunction(()=>!syncPending&&!syncRunning);
703
+ const first=copy(remotePayments(server).find(p=>p.id===firstId));
704
+ const originalVoucher=copy(server.records.get('journalVouchers|'+first.journalVoucherId));
705
+ const originalLines=[...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===first.journalVoucherId).map(copy);
706
+ if(first?.accountingStatus!=='posted'||originalVoucher?.data.status!=='posted'||originalLines.length<2)
707
+ throw Error('Original posted treasury fixture invalid; NOT the expected correction Save RED');
708
+ const sourceType=direction==='outbound'?'payment':'receipt';
709
await page.locator('#payments-ledger button').first().click();
710
+ await expect(page.locator('#pay-amount')).toHaveValue('1,000');
711
await page.locator('#pay-amount').fill('1250');
712
await page.locator('#pay-note').fill('اصلاح مبلغ');
699
- const beforeCorrectionRpc=server.rpcCount;
713
await page.locator('#pay-save').click();
701
- await expect.poll(()=>remotePayments(server).filter(x=>x.correctsPaymentId===firstId&&x.accountingStatus==='posted').length,{timeout:18000}).toBe(1);
714
+ await expect.poll(()=>remotePayments(server).filter(p=>p.correctsPaymentId===firstId).length,{timeout:18000}).toBe(1);
715
await page.waitForFunction(()=>!syncPending&&!syncRunning);
703
- // Reversal + new source + repost must be a single server-side sync_records batch.
704
- expect(server.rpcCount-beforeCorrectionRpc).toBe(1);
716
const original=remotePayments(server).find(p=>p.id===firstId);
706
- expect(original).toEqual(first);
717
const amended=remotePayments(server).find(p=>p.correctsPaymentId===firstId);
708
- expect(amended.amount).toBe(1250);
709
- expect(amended.reference).toBe(first.reference);
710
- expect(amended.journalVoucherId).toBeTruthy();
718
+ if(!amended||amended.amount!==1250||amended.companyId!=='QA_CO'||amended.direction!==direction||
719
+ amended.reference!==first.reference||!amended.journalVoucherId)
720
+ throw Error('Treasury correction fixture invalid; NOT the expected product RED: '+JSON.stringify(amended));
721
const vouchers=[...server.records.values()].filter(x=>x.collection==='journalVouchers');
712
- expect(vouchers).toHaveLength(3);
713
- expect(vouchers.find(x=>x.id===first.journalVoucherId).data.status).toBe('reversed');
714
- expect(oldLines).toEqual([...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===first.journalVoucherId).map(copy));
715
- for(const v of vouchers){
716
- const lines=[...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===v.id);
717
- expect(lines.reduce((n,x)=>n+Number(x.data.debit||0),0)).toBe(lines.reduce((n,x)=>n+Number(x.data.credit||0),0));
718
- }
719
- expect(await page.evaluate(()=>paymentVisibleRecords().map(x=>x.id))).toEqual([amended.id]);
720
- expect(await page.evaluate(()=>paymentEffectiveRecords().map(x=>x.amount))).toEqual([1250]);
721
- await page.evaluate(()=>navigateShell('accounting','view-journal','documents'));
722
- await expect(page.locator('#finora-treasury-document-management-body tr')).toHaveCount(1);
723
- await expect(page.locator('#finora-treasury-document-management-body')).toContainText('REF-ORIGINAL-001');
724
- await page.evaluate(()=>{switchView('view-payments');renderPayments()});
725
- await expect(page.locator('#payments-ledger tr')).toHaveCount(1);
726
- await expect(page.locator('#payments-ledger')).toContainText('۱٬۲۵۰');
727
- await expect(page.locator('#payments-ledger')).toContainText('REF-ORIGINAL-001');
728
- await page.reload({waitUntil:'domcontentloaded'});await authenticatedMockBrowser(page);
729
- await page.evaluate(()=>{switchView('view-payments');renderPayments()});
730
- await expect(page.locator('#payments-ledger tr')).toHaveCount(1);
731
- expect(await page.evaluate(()=>paymentEffectiveRecords().map(x=>x.amount))).toEqual([1250]);
722
+ const linked=vouchers.find(x=>x.id===amended.journalVoucherId);
723
+ const lines=[...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===amended.journalVoucherId);
724
+ const debit=lines.reduce((n,x)=>n+Number(x.data.debit||0),0);
725
+ const credit=lines.reduce((n,x)=>n+Number(x.data.credit||0),0);
726
+ const observed={
727
+ originalSourceUnchanged:isDeepStrictEqual(original,first),
728
+ originalVoucherStatus:vouchers.find(x=>x.id===first.journalVoucherId)?.data.status||'missing',
729
+ originalLinesUnchanged:isDeepStrictEqual(originalLines,
730
+ [...server.records.values()].filter(x=>x.collection==='journalLines'&&x.data.voucherId===first.journalVoucherId).map(copy)),
731
+ correctionSourceStatus:amended.accountingStatus||'',
732
+ correctionVoucherStatus:linked?.data.status||'missing',
733
+ correctionLinkedToSource:!!linked&&linked.data.sourceType===sourceType&&linked.data.sourceId===amended.id,
734
+ correctionHasReversalLink:!!amended.correctionReversalId,
735
+ balancedCorrectionLines:lines.length>=2&&debit===credit&&debit===1250,
736
+ correctionAlreadyPosted:vouchers.some(x=>x.data.status==='posted'&&x.data.sourceType===sourceType&&x.data.sourceId===amended.id),
737
+ originalReversalCount:server.reverseCount,
738
+ voucherCount:vouchers.length,
739
+ effectiveAmounts:await page.evaluate(()=>paymentEffectiveRecords().map(x=>x.amount))
740
+ };
741
+ const knownBroken={
742
+ originalSourceUnchanged:true,originalVoucherStatus:'reversed',originalLinesUnchanged:true,
743
+ correctionSourceStatus:'posted',correctionVoucherStatus:'posted',correctionLinkedToSource:true,
744
+ correctionHasReversalLink:true,balancedCorrectionLines:true,correctionAlreadyPosted:true,
745
+ originalReversalCount:1,voucherCount:3,effectiveAmounts:[1250]
746
+ };
747
+ const required={
748
+ originalSourceUnchanged:true,originalVoucherStatus:'reversed',originalLinesUnchanged:true,
749
+ correctionSourceStatus:'draft',correctionVoucherStatus:'draft',correctionLinkedToSource:true,
750
+ correctionHasReversalLink:true,balancedCorrectionLines:true,correctionAlreadyPosted:false,
751
+ originalReversalCount:1,voucherCount:3,effectiveAmounts:[]
752
+ };
753
+ assertKnownRedOrFixed(observed,knownBroken,required,'treasury-correction-'+direction);
754
+ if(isDeepStrictEqual(observed,knownBroken))
755
+ console.log('FINORA_ISSUE238_PAYMENT_CORRECTION_EXPECTED_RED '+direction+': correction Save immediately posts its new source-linked journal');
756
+ test.fail(true,'#238 / ADR216: treasury correction Save must leave the new source-linked journal draft until independent accounting approval');
757
+ expect(observed).toEqual(required);
758
});
759
760
test('treasury correction excludes cheque, linked settlements and locked periods; cancellation retains immutable history',async({page})=>{