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

test(wp2): assert financial audit rollback JWT actor and source field preservation

davoodmehraban89 committed Nov 22, 2026 at 07:18 UTC 9122b8b8da0d0a2e4f8976030a6f7c4ef3bcc91a
4 files changed +33 -2
PROJECT_STATE.md
+3
@@ -487,3 +487,6 @@ Next action: run and inspect Quality on the latest PR212 head. If any browser te
487 - **Correction with preserved history:** prior QA description of `finora_journal_autopost` missed later replacement migration `supabase/migrations/20261117110000_wp1_autopost_source_link_atomic.sql`. The *effective* function **atomically** writes an already-persisted sale/purchase source link and posts its journal voucher in one RPC transaction, including recovery by persisted source identity/version; it is already verified in narrow mock/SQL evidence. The earlier 20261016102000-only inference was wrong. A dated correction was recorded in PR218 comments; do NOT recreate or replace this useful existing feature. Separate source creation/sync before posting and draft-first independent finalization remain unproven against Issue216.
488 - **Exact Stage C QA decision:** new *independent* branch `qa/source-autopost-jwt-atomicity-20261122` from PR224 green SHA. Modify ONLY synthetic WP2 Auth/PostgREST test, its workflow path filter, and existing canonical status documents. Test the effective sale source `autopost` with a real user JWT, replay with a DIFFERENT request ID for same source version, competing simultaneous requests, rollback injected at the source-link update after voucher creation/posting, and ledger/source audit integrity. Preserve PR224 green head and PR218/221 owner-held SQL/JS. No migrations, financial production code, production access, deployed data, schema, sensitive values or trigger changes in the repo.
489 - **Acceptance:** actual RED/GREEN evidence on exact QA SHA in disposable Supabase; no interpretation of a failed, skipped or pending CI as pass. This tests source+posted-voucher linkage for already-persisted sources only; independent draft-first lifecycle, permanent ATF, cheque, company/year numbering, historic conversion, source create/update/delete atomics still open. Codex-main final reviewer/integration authority.
490 +
491 +
492 +- **2026-11-22 first same-SHA Stage C server gate:** PR225 original candidate `3f8e8a9cbc22f0fb5e42d3845a6e550a0af544a8`: [WP2 44982073582](https://github.com/davoodmehraban89/Factor-easy/actions/runs/44982073582) completed SUCCESS; job log emitted `FINORA_SOURCE_JWT_ATOMIC_PASS` for positive source-to-posted voucher link, different-ID replay, injected late-link rollback of header/lines/counter and simultaneous two-client request. [Security 44982073480](https://github.com/davoodmehraban89/Factor-easy/actions/runs/44982073480) SUCCESS (CodeQL + safety; dependency review skipped). Quality `44982073488` and WP1 `44982073524` were still in progress at this checkpoint; no claim of their success. **QA review strengthened acceptance before final SHA:** add checks that source commercial fields remain unchanged, the successful posted journal audit actor equals the actual JWT user, retries do not append duplicate audit, and failed link transaction rolls back audit events as well as data/counter. This is test-only; final SHA requires fresh four-gate check. No production changes or external review acceptance.
docs/ACCOUNTING_CORE_ROADMAP_V1.md
+1 -1
@@ -437,7 +437,7 @@ This register maps execution stages A/B to the **existing** accounting Phase 6 (
437
438 | FIN-QA-POSITIVE-RPC | احراز آزمون مثبت ثبت و قطعی‌کردن سند دستی با JWT واقعی | QA مستقل؛ سرور/مالی نزد Codex اصلی | PR222 و WP2 disposable؛ RPC موجود | در پیاده‌سازی / منتظر CI | واقعی بودن JWT، create→replay→post، تراز و نسخه/شماره، rollback خطا، همهٔ گیت‌ها | QA follow-on stacked on PR222 | پیش از این تغییر فقط آزمون منفی `scripts/wp3-financial-tenant-e2e.mjs` وجود داشت؛ اجرا هنوز ادعا نشده | اجرای WP2 و Quality/WP1/Security روی SHA جدید؛ خروجی واقعی در همین ردیف ثبت شود |
439
440 -| FIN-QA-SOURCE-JWT-ATOMIC | اثبات واقعی پیوند تراکنشی منبع از قبل ثبت‌شده، rollback و retry | QA مستقل؛ پذیرش Codex اصلی | PR218 migration `20261117110000` و PR224 CI موفق | در بررسی / در پیاده‌سازی | JWT مثبت فروش، پیوند اتمیک، رقابت دو اتصال، replay با شناسه دیگر، برگشت شمارنده/سند در شکستِ پیوند، CI SHA نهایی | QA successor stacked on PR224 | PR224 `6f16c00b` چهار گیت موفق؛ اسکریپت جدید هنوز روی SHA نهایی آزموده نشده | بررسی WP2/Quality/WP1/Security؛ بررسی مستقل، بدون merge یا تغییر مالی |
440 +| FIN-QA-SOURCE-JWT-ATOMIC | اثبات واقعی پیوند تراکنشی منبع از قبل ثبت‌شده، rollback و retry | QA مستقل؛ پذیرش Codex اصلی | PR218 migration `20261117110000` و PR224 CI موفق | در بررسی / در پیاده‌سازی | JWT مثبت فروش، پیوند اتمیک، رقابت دو اتصال، replay با شناسه دیگر، برگشت شمارنده/سند در شکستِ پیوند، CI SHA نهایی | QA successor stacked on PR224 | PR224 `6f16c00b` چهار گیت موفق؛ WP2 روی 3f8e8a9c موفق (44982073582) و Security موفق (44982073480)؛ کنترل audit تکمیلی برای SHA بعدی افزوده شد | بررسی WP2/Quality/WP1/Security؛ بررسی مستقل، بدون merge یا تغییر مالی |
441
442 **Release boundary:** successful Pages run 43606173842 applies only to `main dab1a45`; no candidate above is `منتشرشده`. Competing PR214/215/217/219 are alternative implementations, not sequential milestones. This worker changes documentation only; Codex main owns integration and release decisions.
443
docs/EXECUTION_STATUS.md
+3
@@ -1,5 +1,8 @@
1 # وضعیت اجرای فینورا
2
3 +> **Stage C checkpoint ۲۰۲۶-۱۱-۲۲:** PR225 `3f8e8a9c`، WP2 `44982073582` موفق با JWT واقعی و تزریق شکست در ثبت پیوند، Security `44982073480` موفق (Dependency review skipped). آزمون صحت actor audit، عدم ثبت audit تکراری، حذف audit در rollback و حفظ فیلدهای تجاری بدون تغییر در کاندیدای بعدی اضافه شده‌اند. **نتایج Quality/WP1 و همهٔ گیت‌ها روی SHA نهایی هنوز باید بررسی شوند.** پذیرش مرحله C و انتشار همچنان مسدود است.
4 +
5 +
6 ### ۲۰۲۶-۱۱-۲۲ — FIN-QA-SOURCE-JWT-ATOMIC (Stage C، آزمون غیرهم‌پوشان)
7 - **شاهد قطعی قبلی:** PR224 `6f16c00b9b67eeace45cc2f5b46e8dfaff34c3ef`، تمام چهار workflow اصلی SUCCESS؛ Quality `44980084933`، WP1 `44980084934`، WP2 `44980084953`، Security `44980084871`. این نتیجه به هیچ SHA جدیدی تعمیم داده نمی‌شود؛ Dependency review در Security skipped بوده است. بازبینی Codex اصلی انجام نشده و انتشار نیست.
8 - **تصحیح فنی با حفظ تاریخچه:** نسخه مؤثر `finora_journal_autopost` در migration `20261117110000_wp1_autopost_source_link_atomic.sql` است، نه تابع superseded در `20261016102000`. این تابع پیوند فاکتور/خرید از قبل ذخیره‌شده و سند قطعی را در یک تراکنش ثبت می‌کند. ادعای قبلی مبنی بر نبود پیوند اتمیک، دربارهٔ نسخهٔ فعلی نادرست بود و در PR218 تصحیح شده است. تعمیم آن به ایجاد/ویرایش/حذف منبع و چرخهٔ کامل سند موقت هم نادرست است.
scripts/wp2-source-autopost-jwt-e2e.mjs
+26 -1
@@ -112,6 +112,19 @@ const voucherRows=async(source)=> (await own('journalVouchers')).filter(x=>
112 const voucherLines=async(id)=> (await own('journalLines')).filter(x=>x.data?.voucherId===id);
113 const counter=()=>command('SELECT last_no FROM private.finora_phase1a_journal_counters WHERE organization_id='+
114 q(org)+' AND fiscal_year_id='+q(fy)+';',true);
115 +const auditCount=id=>Number(command(
116 + 'SELECT count(*) FROM private.finora_phase1a_journal_audit WHERE organization_id='+
117 + q(org)+' AND voucher_id='+q(id)+';',true));
118 +const auditWrongActors=id=>Number(command(
119 + 'SELECT count(*) FROM private.finora_phase1a_journal_audit WHERE organization_id='+
120 + q(org)+' AND voucher_id='+q(id)+' AND actor_id IS DISTINCT FROM '+q(uid)+'::uuid;',true));
121 +const businessOnly=data=>{
122 + const copy={...data};
123 + for(const key of ['journalVoucherId','accountingStatus','accountingPostedAt',
124 + 'documentStatus','documentPostedAt','documentPostedBy','documentVersion',
125 + 'updatedAt','accountingPostingPending','accountingPostingPendingAt'])delete copy[key];
126 + return copy;
127 +};
128
129 // 1. Positive real source→journal commit, plus replay with an unrelated RPC
130 // request voucher ID: server must recover by stable source/version, not caller ID.
@@ -128,12 +141,17 @@ assert.equal(source.data.journalVoucherId,firstVoucher,'server failed to link so
141 assert.equal(source.data.accountingStatus,'posted');
142 assert.equal(source.data.documentStatus,'posted');
143 assert.equal(source.data.grandTotal,120,'autopost mutated source business amount');
144 +assert.deepEqual(businessOnly(source.data),businessOnly(initial.data),
145 + 'posted source must preserve all unrelated business fields');
146 let vouchers=await voucherRows(firstSource);
147 assert.equal(vouchers.length,1,'duplicate source vouchers');
148 let lines=await voucherLines(firstVoucher);
149 assert.equal(lines.length,2,'source has wrong number of ledger lines');
150 assert.equal(lines.reduce((s,x)=>s+Number(x.data?.debit||0),0),120);
151 assert.equal(lines.reduce((s,x)=>s+Number(x.data?.credit||0),0),120);
152 +const successfulAuditCount=auditCount(firstVoucher);
153 +assert.ok(successfulAuditCount>=2,'source posting must keep draft-created and posted audit events');
154 +assert.equal(auditWrongActors(firstVoucher),0,'source audit actor must be Auth JWT subject');
155 const replay=ok(await rpc('finora_journal_autopost',
156 voucherArgs('QA_DIFFERENT_RETRY_'+suffix,firstSource),jwt),'replay with changed request ID');
157 assert.equal(replay.replayed,true,'source-level replay did not succeed');
@@ -141,6 +159,8 @@ assert.equal(replay.voucher?.id,firstVoucher,'source retry created different vou
159 assert.equal(replay.voucher?.number,posted.voucher.number,'source retry changed financial number');
160 assert.equal((await voucherRows(firstSource)).length,1);
161 assert.equal((await voucherLines(firstVoucher)).length,2);
162 +assert.equal(auditCount(firstVoucher),successfulAuditCount,
163 + 'idempotent source retry must not append another posting audit event');
164
165 // 2. Deterministic AFTER-POST failure at the source link: all rows and
166 // counters MUST roll back. Only the disposable local database gets this trigger.
@@ -165,6 +185,8 @@ const afterFailure=await sourceRow(failingSource);
185 assert.equal(afterFailure.data.accountingStatus,'draft','source status changed despite rollback');
186 assert.ok(!afterFailure.data.journalVoucherId,'source link survived failed transaction');
187 assert.equal(counter(),oldCounter,'failed source link consumed a committed number');
188 +assert.equal(auditCount(failingVoucher),0,
189 + 'audit records survived a rolled-back source-link failure');
190 command('DROP TRIGGER '+trigger+' ON public.records;');
191 command('DROP FUNCTION private.'+fn+'();');
192
@@ -189,4 +211,7 @@ lines=await voucherLines(a.voucher.id);
211 assert.equal(lines.length,2,'concurrent source created duplicate lines');
212 assert.equal(lines.reduce((s,x)=>s+Number(x.data?.debit||0),0),140);
213 assert.equal(lines.reduce((s,x)=>s+Number(x.data?.credit||0),0),140);
192 -console.log('FINORA_SOURCE_JWT_ATOMIC_PASS: real user JWT positive source link/replay, injected late-link rollback with unchanged counter, and two-request source concurrency in disposable Supabase');
214 +assert.ok(auditCount(a.voucher.id)>=2,'concurrent winner is missing audit trail');
215 +assert.equal(auditWrongActors(a.voucher.id),0,
216 + 'concurrent source posting audit actor must be Auth JWT subject');
217 +console.log('FINORA_SOURCE_JWT_ATOMIC_PASS: real user JWT positive source link/replay, injected late-link rollback with unchanged counter, and two-request source concurrency; audit attribution and rollback also verified in disposable Supabase');