@davoodmehraban89 / Factor-easy / commits / 259dce2
Fork 0

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

test: isolated invoice save/reload/edit/print and sync retry regression for Phase 0

davoodmehraban89 committed Oct 11, 2026 at 19:32 UTC 259dce25f5e1396afa289fe8573309b2cd01236b
1 file changed +121
scripts/phase0-invoice-persistence.spec.js new
+121
@@ -0,0 +1,121 @@
1 +const {test,expect}=require('@playwright/test');
2 +
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 +function fakeServer(){
7 + const records=new Map();
8 + for(const [collection,id,data] of [
9 + ['companies','QA_CO',{name:'شرکت آزمون ایزوله',entityType:'legal'}],
10 + ['contacts','QA_CONTACT',{name:'خریدار آزمایشی'}],
11 + ['products','QA_PROD',{name:'بلوک سیمانی',code:'BLK-001',unit:'عدد',sale_price:1500}],
12 + ['settings','main',{invoice_kind:'pre_invoice',vat_mode:'none',vat_rate:10,default_company_id:'QA_CO',currency_mode:'rial'}],
13 + ])records.set(collection+'|'+id,{collection,id,data:copy(data)});
14 + return {records,failNext:false,failCount:0,rpcCount:0};
15 +}
16 +function remoteInvoices(server){return [...server.records.values()].filter(r=>r.collection==='invoices').map(r=>copy(r.data));}
17 +
18 +async function installHarness(page,server){
19 + await page.exposeFunction('qaF0Read',async()=>[...server.records.values()].map(copy));
20 + await page.exposeFunction('qaF0Write',async args=>{
21 + server.rpcCount++;
22 + if(server.failNext){server.failNext=false;server.failCount++;return {error:{message:'Network unavailable — isolated QA failure',code:'QA_NETWORK'}};}
23 + for(const row of args.p_upserts||[])server.records.set(row.collection+'|'+row.id,{collection:row.collection,id:row.id,data:copy(row.data)});
24 + for(const row of args.p_deletes||[])server.records.delete(row.collection+'|'+row.id);
25 + return {error:null};
26 + });
27 + await page.route(/https:\/\/[^/]*supabase\.co\//,route=>route.abort());
28 +}
29 +async function authenticatedMockBrowser(page){
30 + await page.goto(BASE,{waitUntil:'domcontentloaded'});
31 + await page.waitForFunction(()=>typeof enterApp==='function'&&typeof commitSaveInvoice==='function'&&typeof pullAll==='function');
32 + await page.evaluate(async()=>{
33 + window.__FINORA_PRINT_TEST_MODE=true;
34 + window.__f0Alerts=[];
35 + window.alert=m=>window.__f0Alerts.push(String(m));
36 + window.confirm=()=>false;
37 + fetchCurrentUser=async()=>({id:'FINORA_F0_FIXTURE_ONLY',username:'qa-only',email:'qa@example.invalid',fullName:'QA F0',role:'admin',maxCompanies:2,subscription:{type:'lifetime',status:'active',endDate:'2099-12-31'}});
38 + maybeMigrateLegacyData=()=>{};
39 + sb.from=collection=>({
40 + select:()=>({
41 + order(){return this;},
42 + async range(from,to){const rows=(await window.qaF0Read()).filter(r=>r.collection).sort((a,b)=>a.collection.localeCompare(b.collection)||a.id.localeCompare(b.id));return {data:rows.slice(from,to+1),error:null};}
43 + })
44 + });
45 + sb.rpc=async(name,args)=>{
46 + if(name!=='finora_sync_records')throw Error('Unexpected RPC '+name);
47 + return await window.qaF0Write(args);
48 + };
49 + await enterApp({id:'FINORA_F0_FIXTURE_ONLY'});
50 + });
51 + await expect(page.locator('#auth-screen')).toBeHidden();
52 + await expect(page.locator('#invoice-company-id option[value="QA_CO"]')).toHaveCount(1);
53 + await expect(page.locator('#invoice-items-table-body .row-product-select').first().locator('option[value="QA_PROD"]')).toHaveCount(1);
54 +}
55 +async function fillInvoice(page,number,qty){
56 + await page.locator('#invoice-kind').selectOption('pre_invoice');
57 + await page.locator('#invoice-company-id').selectOption('QA_CO');
58 + await page.locator('#invoice-contact-id').selectOption('QA_CONTACT');
59 + await page.locator('#invoice-number').fill(number);
60 + await page.locator('#invoice-date-input').fill('۱۴۰۵/۰۷/۱۹');
61 + await page.locator('#invoice-items-table-body .row-product-select').first().selectOption('QA_PROD');
62 + await page.locator('#invoice-items-table-body .row-quantity').first().fill(String(qty));
63 + await page.locator('#invoice-items-table-body .row-quantity').first().dispatchEvent('input');
64 +}
65 +
66 +test('F0-03 isolated invoice save → mock-server sync → reload → edit → reload → print',async({page})=>{
67 + const server=fakeServer();
68 + await installHarness(page,server);
69 + await authenticatedMockBrowser(page);
70 + await fillInvoice(page,'QA-PINV-0001',2);
71 + await expect(page.locator('#summary-grandtotal')).toContainText('۳٬۰۰۰');
72 + await page.evaluate(()=>commitSaveInvoice());
73 + await expect.poll(()=>remoteInvoices(server).length,{timeout:12000}).toBe(1);
74 + let first=remoteInvoices(server)[0];
75 + expect({number:first.number,kind:first.kind,subtotal:first.subtotal,total:first.grandTotal,qty:first.items[0].qty,price:first.items[0].price})
76 + .toEqual({number:'QA-PINV-0001',kind:'pre_invoice',subtotal:3000,total:3000,qty:2,price:1500});
77 + expect([...server.records.values()].filter(r=>r.collection==='journalVouchers')).toHaveLength(0);
78 +
79 + await page.reload({waitUntil:'domcontentloaded'});
80 + await authenticatedMockBrowser(page);
81 + const savedId=await page.evaluate(()=>getMyInvoices().find(i=>i.number==='QA-PINV-0001')?.id||'');
82 + expect(savedId).toMatch(/^INV_/);
83 + await page.evaluate(id=>editInvoice(id),savedId);
84 + await expect(page.locator('#edit-invoice-id')).toHaveValue(savedId);
85 + await expect(page.locator('#invoice-items-table-body .row-product-select').first()).toHaveValue('QA_PROD');
86 + await expect(page.locator('#invoice-items-table-body .row-quantity').first()).toHaveValue('2');
87 + await page.locator('#invoice-items-table-body .row-quantity').first().fill('3');
88 + await page.locator('#invoice-items-table-body .row-quantity').first().dispatchEvent('input');
89 + await page.evaluate(()=>commitSaveInvoice());
90 + await expect.poll(()=>remoteInvoices(server)[0]?.grandTotal,{timeout:12000}).toBe(4500);
91 + expect(remoteInvoices(server)).toHaveLength(1);
92 + expect(remoteInvoices(server)[0].id).toBe(savedId);
93 +
94 + await page.reload({waitUntil:'domcontentloaded'});
95 + await authenticatedMockBrowser(page);
96 + const reloaded=await page.evaluate(id=>getMyInvoices().find(x=>x.id===id),savedId);
97 + expect({count:reloaded.items.length,qty:reloaded.items[0].qty,total:reloaded.grandTotal})
98 + .toEqual({count:1,qty:3,total:4500});
99 + await page.evaluate(id=>renderAndPrintDirect(id),savedId);
100 + await expect(page.locator('#printable-invoice')).toContainText('QA-PINV-۰۰۰۱');
101 + await expect(page.locator('#printable-invoice')).toContainText('۴٬۵۰۰');
102 + const printable=await page.locator('#printable-invoice').innerText();
103 + expect(printable).not.toMatch(/[0-9٠-٩]/);
104 +});
105 +
106 +test('F0-03 isolated transient RPC failure remains pending and retries without duplicate invoice',async({page})=>{
107 + const server=fakeServer();
108 + await installHarness(page,server);
109 + await authenticatedMockBrowser(page);
110 + await fillInvoice(page,'QA-PINV-RETRY',2);
111 + server.failNext=true;
112 + await page.evaluate(()=>commitSaveInvoice());
113 + await expect.poll(()=>server.failCount,{timeout:12000}).toBe(1);
114 + await expect.poll(()=>remoteInvoices(server).length,{timeout:16000}).toBe(1);
115 + expect(remoteInvoices(server)[0].number).toBe('QA-PINV-RETRY');
116 + expect(remoteInvoices(server)).toHaveLength(1);
117 + expect(server.rpcCount).toBeGreaterThanOrEqual(2);
118 + await page.reload({waitUntil:'domcontentloaded'});
119 + await authenticatedMockBrowser(page);
120 + expect(await page.evaluate(()=>getMyInvoices().filter(i=>i.number==='QA-PINV-RETRY').length)).toBe(1);
121 +});