@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / a71cefd
Fork 0

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

Mirrored from https://github.com/davoodmehraban89/Factor-easy.git

Synced Dec 3, 20:45 UTC

test(stage3.2): require ENFORCE invoice server RPC, immutable source and read-model linkage (RED first)

davoodmehraban89 committed Oct 23, 2026 at 03:20 UTC a71cefd97467a8c85bbc4c88c5a6ddd0b67ec980
1 file changed +62
scripts/accounting-document-workflow.spec.js
+62
@@ -729,3 +729,65 @@ test('Stage3 new sale and purchase UI must persist complete SQL-v1 monetary cont
729 require('node:fs').writeFileSync('stage3-ui-fixtures.json',
730 JSON.stringify({sale:monetary(sale),purchase:monetary(purchase)},null,2));
731 });
732 +
733 +
734 +test('Stage3 ENFORCE sale finalization posts only through the authoritative server, with source unchanged',async({page})=>{
735 + const server=fakeServer();
736 + await installHarness(page,server);await authenticatedMockBrowser(page);
737 + await fillInvoice(page,'STAGE3-ENFORCE-SALE',2);
738 + await page.evaluate(()=>{
739 + document.getElementById('invoice-vat-mode').value='with_vat';
740 + document.getElementById('invoice-vat-rate-input').value='10';
741 + recomputeTotals();commitSaveInvoice();
742 + });
743 + await expect.poll(()=>remoteInvoices(server).find(x=>x.number==='STAGE3-ENFORCE-SALE')?.grandTotal,{timeout:15000}).toBe(3300);
744 + const savedBefore=remoteInvoices(server).find(x=>x.number==='STAGE3-ENFORCE-SALE');
745 + const id=savedBefore.id;
746 + await page.evaluate(()=>{
747 + currentUser.organizationId='11111111-1111-4111-8111-111111111111';
748 + window.__stage3AutopostRequests=[];
749 + const oldRpc=sb.rpc.bind(sb);
750 + sb.rpc=async(name,args)=>{
751 + if(name==='finora_journal_get_guard_mode')return {data:'enforce',error:null};
752 + if(name==='finora_journal_autopost'){
753 + window.__stage3AutopostRequests.push(args);
754 + const committed=(await window.qaF0Read()).find(r=>r.collection==='invoices'&&r.id===args.p_source_id);
755 + if(!committed)return {data:null,error:{message:'FINORA_SOURCE_NOT_COMMITTED_IN_ORG'}};
756 + const row={id:args.p_voucher_id,ownerUserId:currentUser.id,
757 + companyId:args.p_header.companyId,fiscalYearId:args.p_header.fiscalYearId,
758 + sourceId:args.p_source_id,sourceType:'sale',sourceVersion:'1',
759 + number:'1',status:'posted',postedAt:'2026-10-23T00:00:00.000Z'};
760 + window.__stage3PostedMock=row;
761 + return {data:{voucher:row,computedLines:[
762 + {accountId:'A_CASH',debit:3300,credit:0},{accountId:'A_REV',debit:0,credit:3000},
763 + {accountId:'A_VAT',debit:0,credit:300}],contractVersion:1},error:null};
764 + }
765 + return oldRpc(name,args);
766 + };
767 + finoraManualReload=async id=>{
768 + if(!window.__stage3PostedMock||window.__stage3PostedMock.id!==id)
769 + throw Error('TARGETED_SOURCE_VOUCHER_NOT_FOUND');
770 + const local={...window.__stage3PostedMock};
771 + datastore.journalVouchers.push(local);
772 + syncSnap.set('journalVouchers|'+id,JSON.stringify(stripOwner(local)));
773 + versionSnap.set('journalVouchers|'+id,2);
774 + return local;
775 + };
776 + });
777 + const actual=await page.evaluate(async invoiceId=>{
778 + const voucherId=await finalizeInvoiceDocument(invoiceId);
779 + const inv=getMyInvoices().find(x=>x.id===invoiceId);
780 + return {voucherId,status:invoiceDocumentStatus(inv),
781 + linked:jeSourceVoucher(inv)?.id||'',sourceStatus:inv.documentStatus,
782 + autopostArgs:window.__stage3AutopostRequests};
783 + },id);
784 + expect(actual.status).toBe('posted');
785 + expect(actual.sourceStatus).toBe('draft');
786 + expect(actual.linked).toBe(actual.voucherId);
787 + expect(actual.autopostArgs).toHaveLength(1);
788 + expect(actual.autopostArgs[0].p_lines).toBeNull();
789 + expect(actual.autopostArgs[0].p_source_id).toBe(id);
790 + expect(actual.autopostArgs[0].p_source_collection).toBe('invoices');
791 + expect(remoteInvoices(server).find(x=>x.id===id)).toEqual(savedBefore);
792 + expect([...server.records.values()].filter(x=>x.collection==='journalVouchers')).toHaveLength(0);
793 +});