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

test(stage3): RED contract for enforce new purchase draft and explicit RPC-only posting

davoodmehraban89 committed Oct 23, 2026 at 15:04 UTC d36950f3016bfada3ac8acf6a5d5a7673ef0d999
1 file changed +78
scripts/accounting-document-workflow.spec.js
+78
@@ -795,3 +795,81 @@ test('Stage3 ENFORCE sale finalization posts only through the authoritative serv
795 expect(remoteInvoices(server).find(x=>x.id===id)).toEqual(savedBefore);
796 expect([...server.records.values()].filter(x=>x.collection==='journalVouchers')).toHaveLength(0);
797 });
798 +
799 +
800 +test('Stage3 ENFORCE new purchase must stay draft until explicit server-only posting',async({page})=>{
801 + const server=fakeServer();
802 + await installHarness(page,server);
803 + await authenticatedMockBrowser(page);
804 + await page.evaluate(()=>{
805 + // Override only the synthetic test transport's server-owned guard flag.
806 + const originalRpc=sb.rpc.bind(sb);
807 + window.__stage3PurchaseCalls=[];
808 + sb.rpc=async(name,args)=>{
809 + if(name==='finora_journal_get_guard_mode')return {data:'enforce',error:null};
810 + if(name==='finora_journal_autopost'){
811 + window.__stage3PurchaseCalls.push(args);
812 + const rows=await window.qaF0Read();
813 + const src=rows.find(x=>x.collection==='purchases'&&x.id===args.p_source_id);
814 + if(!src)return {data:null,error:{message:'FINORA_SOURCE_NOT_COMMITTED_IN_ORG'}};
815 + const voucher={id:args.p_voucher_id,ownerUserId:currentUser.id,
816 + companyId:args.p_header.companyId,fiscalYearId:args.p_header.fiscalYearId,
817 + sourceId:args.p_source_id,sourceType:'purchase',sourceVersion:'1',
818 + status:'posted',number:'7',postedAt:'2026-10-23T00:00:00.000Z'};
819 + window.__stage3PurchasePosted=voucher;
820 + return {data:{voucher,contractVersion:1,computedLines:[
821 + {accountId:'A_BUY',debit:200,credit:0},
822 + {accountId:'A_AP',debit:0,credit:200}]},error:null};
823 + }
824 + return originalRpc(name,args);
825 + };
826 + window.__stage3PurchaseTargetedReads=[];
827 + finoraManualReload=async id=>{
828 + window.__stage3PurchaseTargetedReads.push(id);
829 + if(window.__stage3PurchasePosted?.id!==id)throw Error('TARGETED_PURCHASE_VOUCHER_NOT_FOUND');
830 + const row={...window.__stage3PurchasePosted};
831 + datastore.journalVouchers.push(row);
832 + syncSnap.set('journalVouchers|'+id,JSON.stringify(stripOwner(row)));
833 + versionSnap.set('journalVouchers|'+id,2);
834 + return row;
835 + };
836 + switchView('view-purchases');
837 + purCancelEdit();
838 + document.getElementById('pur-number').value='STAGE3-ENFORCE-PUR';
839 + document.getElementById('pur-supplier').value='QA_CONTACT';
840 + document.getElementById('pur-payment-method').value='credit';
841 + const tr=document.querySelector('#pur-items-body tr');
842 + const prod=tr.querySelector('.pur-prod');
843 + prod.value='QA_PROD';prod.dispatchEvent(new Event('change',{bubbles:true}));
844 + tr.querySelector('.pur-qty').value='2';
845 + tr.querySelector('.pur-price').value='100';
846 + document.getElementById('pur-discount').value='0';
847 + document.getElementById('pur-vat-mode').value='none';
848 + purRecalc();
849 + purSave();
850 + });
851 + await expect.poll(()=>remotePurchases(server).find(x=>x.number==='STAGE3-ENFORCE-PUR')?.accountingStatus,
852 + {timeout:15000}).toBe('draft');
853 + const persisted=remotePurchases(server).find(x=>x.number==='STAGE3-ENFORCE-PUR');
854 + expect(persisted).toBeTruthy();
855 + expect(persisted.journalVoucherId).toBeUndefined();
856 + expect([...server.records.values()].filter(x=>x.collection==='journalVouchers')).toHaveLength(0);
857 + const before=copy(persisted);
858 + const final=await page.evaluate(async id=>{
859 + const voucherId=await finalizePurchasePilotDraft(id);
860 + const source=getMyPurchases().find(x=>x.id===id);
861 + return {voucherId,status:purchaseDocumentStatus(source),
862 + sourceAccountingStatus:source.accountingStatus,
863 + calls:window.__stage3PurchaseCalls,
864 + targetedReads:window.__stage3PurchaseTargetedReads};
865 + },persisted.id);
866 + expect(final.status).toBe('posted');
867 + expect(final.sourceAccountingStatus).toBe('draft');
868 + expect(final.calls).toHaveLength(1);
869 + expect(final.calls[0].p_lines).toBeNull();
870 + expect(final.calls[0].p_source_collection).toBe('purchases');
871 + expect(final.calls[0].p_source_id).toBe(persisted.id);
872 + expect(final.targetedReads).toEqual([final.voucherId]);
873 + expect(remotePurchases(server).find(x=>x.id===persisted.id)).toEqual(before);
874 + expect([...server.records.values()].filter(x=>x.collection==='journalVouchers')).toHaveLength(0);
875 +});