@davoodmehraban89 / Factor-easy / commits / 8ba2f3d
Fork 0

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

test(qa): converge linked-draft real PASS with strict sale/purchase P0 acceptance

davoodmehraban89 committed Nov 22, 2026 at 19:47 UTC 8ba2f3dc5a318a1a3e1425a51d701b2b8f40572a
1 file changed +150 -3
scripts/accounting-document-workflow.spec.js
+150 -3
@@ -3,6 +3,23 @@ const {test,expect}=require('@playwright/test');
3 const BASE=process.env.FINORA_TEST_BASE_URL||'http://127.0.0.1:4173/index.html';
4 const USER='FINORA_F0_FIXTURE_ONLY';
5 const copy=value=>JSON.parse(JSON.stringify(value));
6 +const {isDeepStrictEqual}=require('node:util');
7 +// Do not allow an unexpected third product state to masquerade as expected P0 RED.
8 +function assertKnownRedOrFixed(observed,knownBroken,required,label){
9 + if(isDeepStrictEqual(observed,knownBroken)||isDeepStrictEqual(observed,required))return;
10 + throw new Error('FINORA_ISSUE229_UNEXPECTED_PRODUCT_STATE '+label+' (not the known P0 RED): '+
11 + JSON.stringify({observed,knownBroken,required}));
12 +}
13 +test('P0 #229 sentinel rejects unexpected financial states before expected-RED markers',()=>{
14 + const broken={status:'posted',count:1,voucherStates:['posted']};
15 + const fixed={status:'draft',count:0,voucherStates:['draft']};
16 + expect(()=>assertKnownRedOrFixed(broken,broken,fixed,'fixture')).not.toThrow();
17 + expect(()=>assertKnownRedOrFixed(fixed,broken,fixed,'fixture')).not.toThrow();
18 + expect(()=>assertKnownRedOrFixed({status:'partial',count:2,voucherStates:['posted']},
19 + broken,fixed,'fixture')).toThrow(/FINORA_ISSUE229_UNEXPECTED_PRODUCT_STATE/);
20 + expect(()=>assertKnownRedOrFixed({status:'posted',count:1,voucherStates:['posted','orphan']},
21 + broken,fixed,'fixture')).toThrow(/FINORA_ISSUE229_UNEXPECTED_PRODUCT_STATE/);
22 +});
23 function fakeServer(){
24 const records=new Map();
25 for(const [collection,id,data] of [
@@ -941,9 +958,9 @@ test('contact operational balance reflects a saved unallocated supplier payment
958 });
959
960
944 -// Issue #229 / ADR0216: purchase Save remains expected-RED.
945 -// Linked-draft status is fixed in this independent UI-only candidate; its
946 -// regression below is now a normal PASS, not a false product-fix claim.
961 +// Issue #229 / ADR0216: executable expected-RED acceptance contracts.
962 +// test.fail is a transparent temporary xfail, NOT a product PASS. Remove
963 +// annotations only after the linked-draft lifecycle works end-to-end.
964 test('P0 #229 expected RED: purchase Save must not auto-post',async({page})=>{
965 const server=fakeServer();
966 await installHarness(page,server);
@@ -983,6 +1000,13 @@ test('P0 #229 expected RED: purchase Save must not auto-post',async({page})=>{
1000 };
1001 // CURRENT: posted, posted, no linked draft, one posted voucher, one autopost.
1002 // REQUIRED: business source + linked draft; no final number/posted voucher.
1003 + assertKnownRedOrFixed(observed,{
1004 + documentStatus:'posted',accountingStatus:'posted',hasLinkedDraft:false,
1005 + postedVouchers:1,automaticPostCalls:1
1006 + },{
1007 + documentStatus:'draft',accountingStatus:'draft',hasLinkedDraft:true,
1008 + postedVouchers:0,automaticPostCalls:0
1009 + },'purchase-initial-save');
1010 test.fail(true,'#229: Save invokes automatic posting instead of persisted linked draft');
1011 expect(observed).toEqual({
1012 documentStatus:'draft',accountingStatus:'draft',hasLinkedDraft:true,
@@ -990,6 +1014,7 @@ test('P0 #229 expected RED: purchase Save must not auto-post',async({page})=>{
1014 });
1015 });
1016
1017 +
1018 test('P0 #229 linked journal draft stays draft for both sales and purchases',async({page})=>{
1019 const server=fakeServer();
1020 await installHarness(page,server);
@@ -1163,3 +1188,125 @@ test('P0 #229 linked draft direct actions deny before confirm, sync or autopost'
1188 expect(observed.purchaseLabel).not.toContain('موقت قابل اصلاح');
1189 expect(server.autoPostCount||0).toBe(0);
1190 });
1191 +
1192 +
1193 +test('P0 #229 expected RED: formal sale Save must persist linked journal draft without posting',async({page})=>{
1194 + const server=fakeServer();
1195 + await installHarness(page,server);
1196 + await authenticatedMockBrowser(page);
1197 + await fillInvoice(page,'QA-ADR216-SALE-LINK-DRAFT',2);
1198 + await page.evaluate(()=>commitSaveInvoice());
1199 + await expect.poll(()=>remoteInvoices(server).length,{timeout:14000}).toBe(1);
1200 + // A fixture failure is not allowed to masquerade as the expected accounting RED.
1201 + await page.waitForFunction(()=>!syncPending&&!syncRunning);
1202 + const source=remoteInvoices(server)[0];
1203 + if(source.number!=='QA-ADR216-SALE-LINK-DRAFT'||source.kind!=='formal'||
1204 + source.companyId!=='QA_CO'||source.grandTotal!==3000)
1205 + throw Error('QA formal sale Save fixture invalid; NOT expected-red: '+JSON.stringify(source));
1206 + const vouchers=[...server.records.values()].filter(row=>row.collection==='journalVouchers');
1207 + const linked=vouchers.find(row=>row.id===source.journalVoucherId);
1208 + const lines=[...server.records.values()].filter(row=>
1209 + row.collection==='journalLines'&&row.data.voucherId===source.journalVoucherId);
1210 + const debit=lines.reduce((sum,row)=>sum+Number(row.data.debit||0),0);
1211 + const credit=lines.reduce((sum,row)=>sum+Number(row.data.credit||0),0);
1212 + const observed={
1213 + documentStatus:source.documentStatus,
1214 + accountingStatus:source.accountingStatus,
1215 + hasLinkedDraft:!!linked&&linked.data.status==='draft',
1216 + linkedSourceMatches:!!linked&&linked.data.sourceType==='sale'&&linked.data.sourceId===source.id,
1217 + draftHasNoFinalNumber:!!linked&&!linked.data.number,
1218 + draftHasBalancedLines:lines.length>=2&&debit===credit&&debit===source.grandTotal,
1219 + postedVouchers:vouchers.filter(row=>row.data.status==='posted').length,
1220 + automaticPostCalls:server.autoPostCount||0
1221 + };
1222 + // CURRENT: source-only sale draft, no linked journal, no autopost.
1223 + // REQUIRED ADR216: atomic source + linked journal draft, balanced lines, no final number.
1224 + assertKnownRedOrFixed(observed,{
1225 + documentStatus:'draft',accountingStatus:'draft',
1226 + hasLinkedDraft:false,linkedSourceMatches:false,
1227 + draftHasNoFinalNumber:false,draftHasBalancedLines:false,
1228 + postedVouchers:0,automaticPostCalls:0
1229 + },{
1230 + documentStatus:'draft',accountingStatus:'draft',
1231 + hasLinkedDraft:true,linkedSourceMatches:true,
1232 + draftHasNoFinalNumber:true,draftHasBalancedLines:true,
1233 + postedVouchers:0,automaticPostCalls:0
1234 + },'sale-initial-save');
1235 + test.fail(true,'#229 / ADR216: formal sale Save still lacks server-persisted linked journal draft');
1236 + expect(observed).toEqual({
1237 + documentStatus:'draft',accountingStatus:'draft',
1238 + hasLinkedDraft:true,linkedSourceMatches:true,
1239 + draftHasNoFinalNumber:true,draftHasBalancedLines:true,
1240 + postedVouchers:0,automaticPostCalls:0
1241 + });
1242 +});
1243 +
1244 +
1245 +test('P0 #229 expected RED: posted purchase correction Save must remain linked accounting draft',async({page})=>{
1246 + const server=fakeServer();
1247 + await installHarness(page,server);
1248 + await authenticatedMockBrowser(page);
1249 + const id=await createPostedPurchase(page,server,'QA-ADR216-CORRECTION-SAVE',2);
1250 + const original=remotePurchases(server).find(p=>p.id===id);
1251 + const originalVoucher=[...server.records.values()].find(r=>r.collection==='journalVouchers'&&r.id===original.journalVoucherId);
1252 + if(original?.accountingStatus!=='posted'||originalVoucher?.data.status!=='posted')
1253 + throw Error('Original posted purchase fixture invalid; NOT the expected correction Save RED');
1254 + const originalSourceFingerprint=copy(original);
1255 + const originalVoucherFingerprint=copy(originalVoucher);
1256 + const originalLines=[...server.records.values()].filter(r=>r.collection==='journalLines'&&
1257 + r.data.voucherId===original.journalVoucherId).map(copy);
1258 + await page.evaluate(x=>beginPurchaseAmendment(x),id);
1259 + await expect(page.locator('#pur-edit-id')).toHaveValue(id);
1260 + await page.locator('#pur-items-body .pur-qty').first().fill('3');
1261 + await page.locator('#pur-items-body .pur-qty').first().dispatchEvent('input');
1262 + await page.locator('#pur-save-btn').click();
1263 + // Await the actual completed Save path, not its intermediate locally inserted draft.
1264 + await expect(page.locator('#pur-edit-id')).toHaveValue('',{timeout:22000});
1265 + await expect.poll(()=>remotePurchases(server).filter(p=>p.correctsPurchaseId===id).length,
1266 + {timeout:15000}).toBe(1);
1267 + await page.waitForFunction(()=>!syncPending&&!syncRunning);
1268 + const corrected=remotePurchases(server).find(p=>p.correctsPurchaseId===id);
1269 + if(corrected?.grandTotal!==300||corrected?.companyId!=='QA_CO'||
1270 + corrected?.correctionOriginalNumber!==original.number)
1271 + throw Error('Correction Save fixture invalid; NOT the expected product RED: '+JSON.stringify(corrected));
1272 + // A correction must never rewrite or destroy the original posted source/journal.
1273 + expect(remotePurchases(server).find(p=>p.id===id)).toEqual(originalSourceFingerprint);
1274 + expect([...server.records.values()].find(r=>r.collection==='journalVouchers'&&
1275 + r.id===original.journalVoucherId)).toEqual(originalVoucherFingerprint);
1276 + expect([...server.records.values()].filter(r=>r.collection==='journalLines'&&
1277 + r.data.voucherId===original.journalVoucherId)).toEqual(originalLines);
1278 + const vouchers=[...server.records.values()].filter(r=>r.collection==='journalVouchers');
1279 + const linked=vouchers.find(r=>r.id===corrected.journalVoucherId);
1280 + const lines=[...server.records.values()].filter(r=>r.collection==='journalLines'&&
1281 + r.data.voucherId===corrected.journalVoucherId);
1282 + const debit=lines.reduce((sum,r)=>sum+Number(r.data.debit||0),0);
1283 + const credit=lines.reduce((sum,r)=>sum+Number(r.data.credit||0),0);
1284 + const observed={
1285 + documentStatus:corrected.documentStatus,
1286 + accountingStatus:corrected.accountingStatus,
1287 + hasSourceLinkedJournalDraft:!!linked&&linked.data.status==='draft',
1288 + journalPointsToCorrection:!!linked&&linked.data.sourceType==='purchase'&&
1289 + linked.data.sourceId===corrected.id,
1290 + balancedDraftLines:lines.length>=2&&debit===credit&&debit===300,
1291 + correctionAlreadyPosted:vouchers.some(r=>r.data.status==='posted'&&
1292 + r.data.sourceType==='purchase'&&r.data.sourceId===corrected.id),
1293 + originalReversalCount:server.reverseCount
1294 + };
1295 + // CURRENT: purchaseSaveSeamless directly calls finalizePurchasePilotDraft(newId)
1296 + // on Save, bypassing independent accounting approval for the correction.
1297 + assertKnownRedOrFixed(observed,{
1298 + documentStatus:'posted',accountingStatus:'posted',
1299 + hasSourceLinkedJournalDraft:false,journalPointsToCorrection:true,
1300 + balancedDraftLines:true,correctionAlreadyPosted:true,originalReversalCount:1
1301 + },{
1302 + documentStatus:'draft',accountingStatus:'draft',
1303 + hasSourceLinkedJournalDraft:true,journalPointsToCorrection:true,
1304 + balancedDraftLines:true,correctionAlreadyPosted:false,originalReversalCount:1
1305 + },'purchase-correction-save');
1306 + test.fail(true,'#229 / ADR216: correcting posted purchase Save auto-finalizes the new source');
1307 + expect(observed).toEqual({
1308 + documentStatus:'draft',accountingStatus:'draft',
1309 + hasSourceLinkedJournalDraft:true,journalPointsToCorrection:true,
1310 + balancedDraftLines:true,correctionAlreadyPosted:false,originalReversalCount:1
1311 + });
1312 +});