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

test: add D2 request workflow real DOM coverage

davoodmehraban89 committed Oct 1, 2026 at 22:36 UTC 9481adef30af9a9c18378062cddf56fa2a0aca73
1 file changed +74
scripts/request-workflow-d2.spec.js new
+74
@@ -0,0 +1,74 @@
1 +const {test,expect}=require('@playwright/test');
2 +
3 +async function boot(page,{configure=true,create=true}={}){
4 + await page.goto('http://127.0.0.1:4173/index.html',{waitUntil:'domcontentloaded'});
5 + await page.waitForFunction(()=>typeof window.requestsRender==='function'&&typeof window.requestD2PublishType==='function');
6 + await page.evaluate(({configure,create})=>{
7 + window.__d2={rpc:[],alerts:[],publishMode:'ok',createAttempts:0};
8 + window.alert=m=>window.__d2.alerts.push(String(m));
9 + currentUser={id:'11111111-1111-1111-1111-111111111111',email:'d2@example.test',organizationId:'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',organizationName:'D2 Org',isOrganizationOwner:false,commercialModules:['requests_workflow'],modulePermissions:[{module_key:'requests_workflow',capabilities:['read',...(configure?['configure']:[]),...(create?['create']:[])]}],modules:['requests_workflow'],subscription:{type:'lifetime',status:'active',endDate:'2099-01-01'}};
10 + const defs=[{id:'d1111111-1111-1111-1111-111111111111',organization_id:currentUser.organizationId,code:'PURCHASE',title:'درخواست خرید',category:'تدارکات',revision:2,active:true}];
11 + const versions=[{id:'v1111111-1111-1111-1111-111111111111',organization_id:currentUser.organizationId,request_type_id:defs[0].id,version_no:2,title:'درخواست خرید',category:'تدارکات',form_schema:{schemaVersion:1,fields:[{key:'f1',label:'شرح خرید',type:'text',required:true}]},published_at:'2026-10-01T00:00:00Z'}];
12 + const instances=[];
13 + sb.from=(table)=>({select:()=>({eq:()=>({order:()=>Promise.resolve({data:table==='request_type_definitions'?defs:table==='request_type_versions'?versions:instances,error:null})})})});
14 + sb.rpc=async(name,args)=>{
15 + window.__d2.rpc.push({name,args:JSON.parse(JSON.stringify(args))});
16 + if(name==='request_publish_type_version'){
17 + if(window.__d2.publishMode==='stale')return {data:null,error:{message:'stale definition revision'}};
18 + return {data:[{request_type_id:defs[0].id,request_type_version_id:versions[0].id,workflow_version_id:'w1111111-1111-1111-1111-111111111111',version_no:3,definition_revision:3}],error:null};
19 + }
20 + if(name==='request_create_instance'){
21 + window.__d2.createAttempts++;
22 + if(window.__d2.createAttempts===1)return {data:null,error:{message:'network test failure'}};
23 + return {data:'r1111111-1111-1111-1111-111111111111',error:null};
24 + }
25 + return {data:null,error:null};
26 + };
27 + switchView('view-requests');
28 + },{configure,create});
29 +}
30 +
31 +test('D2 builder shows published state and publishes through server RPC',async({page})=>{
32 + await boot(page);
33 + await page.evaluate(()=>requestsRender('types'));
34 + await expect(page.locator('#view-requests')).toContainText('نسخه ۲');
35 + await page.evaluate(()=>requestD2EditType('d1111111-1111-1111-1111-111111111111'));
36 + await expect(page.locator('#request-d2-draft-state')).toContainText('منتشرشده');
37 + await page.fill('#request-d2-title','درخواست خرید اصلاح‌شده');
38 + await page.dispatchEvent('#request-d2-title','input');
39 + await expect(page.locator('#request-d2-draft-state')).toContainText('تغییرات منتشرنشده');
40 + await page.evaluate(()=>requestD2PublishType());
41 + await expect.poll(()=>page.evaluate(()=>window.__d2.rpc.filter(x=>x.name==='request_publish_type_version').length)).toBe(1);
42 + const call=await page.evaluate(()=>window.__d2.rpc.find(x=>x.name==='request_publish_type_version'));
43 + expect(call.args.p_expected_definition_revision).toBe(2);
44 + expect(call.args.p_form_schema.fields[0].label).toBe('شرح خرید');
45 +});
46 +
47 +test('D2 hides publishing for users without configure capability',async({page})=>{
48 + await boot(page,{configure:false});
49 + await page.evaluate(()=>requestsRender('types'));
50 + await expect(page.locator('#view-requests')).toContainText('دسترسی پیکربندی ندارید');
51 + await expect(page.locator('#request-d2-publish')).toHaveCount(0);
52 +});
53 +
54 +test('D2 retries create with the same idempotency key',async({page})=>{
55 + await boot(page);
56 + await page.evaluate(()=>requestsRender('new'));
57 + await page.selectOption('#request-d2-type-version','v1111111-1111-1111-1111-111111111111');
58 + await page.fill('.request-d2-value','یک لپ‌تاپ');
59 + await page.evaluate(()=>requestD2CreateInstance());
60 + await expect(page.locator('#request-d2-error')).toContainText('تلاش دوباره');
61 + await page.evaluate(()=>requestD2CreateInstance());
62 + await expect.poll(()=>page.evaluate(()=>window.__d2.rpc.filter(x=>x.name==='request_create_instance').length)).toBe(2);
63 + const keys=await page.evaluate(()=>window.__d2.rpc.filter(x=>x.name==='request_create_instance').map(x=>x.args.p_idempotency_key));
64 + expect(keys[0]).toBe(keys[1]);
65 +});
66 +
67 +test('D2 stale publish revision refreshes instead of overwriting',async({page})=>{
68 + await boot(page);
69 + await page.evaluate(async()=>{await requestsRender('types');requestD2EditType('d1111111-1111-1111-1111-111111111111');window.__d2.publishMode='stale';await requestD2PublishType()});
70 + const alerts=await page.evaluate(()=>window.__d2.alerts);
71 + expect(alerts.some(x=>x.includes('نسخه فرم در سرور تغییر کرده است'))).toBe(true);
72 + const call=await page.evaluate(()=>window.__d2.rpc.find(x=>x.name==='request_publish_type_version'));
73 + expect(call.args.p_expected_definition_revision).toBe(2);
74 +});