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

test: define D3b workflow editor and condition semantics

davoodmehraban89 committed Oct 1, 2026 at 23:01 UTC 8371918f8a0792a2c156161820ed622117d0006c
4 files changed +131 -1
.github/workflows/quality.yml
+7 -1
@@ -74,6 +74,8 @@ jobs:
74 run: node scripts/request-workflow-d1-security-check.mjs
75 - name: Request Workflow Slice D2 UI/read-model invariants
76 run: node scripts/request-workflow-d2-check.mjs
77 + - name: Request Workflow Slice D3b visual/condition/timeline invariants
78 + run: node scripts/request-workflow-d3b-check.mjs
79 - name: Install browser smoke-test runtime
80 run: |
81 npm install --no-save @playwright/test@1.55.0 pdf-lib@1.17.1
@@ -81,7 +83,7 @@ jobs:
83 - name: Real DOM smoke
84 run: |
85 python3 -m http.server 4173 >/tmp/finora-http.log 2>&1 &
84 - npx playwright test scripts/dom-smoke.spec.js scripts/platform-office.spec.js scripts/enterprise-license.spec.js scripts/enterprise-role.spec.js scripts/enterprise-invitation.spec.js scripts/enterprise-org-ux.spec.js scripts/office-slice-c1.spec.js scripts/office-slice-c2.spec.js scripts/office-slice-c3.spec.js scripts/office-slice-c4.spec.js scripts/office-slice-c5.spec.js scripts/office-slice-c6.spec.js scripts/office-slice-c7.spec.js scripts/office-slice-c8.spec.js scripts/office-slice-c8-runtime.spec.js scripts/request-workflow-d2.spec.js scripts/taxpayer-print.spec.js --reporter=line --workers=1
86 + npx playwright test scripts/dom-smoke.spec.js scripts/platform-office.spec.js scripts/enterprise-license.spec.js scripts/enterprise-role.spec.js scripts/enterprise-invitation.spec.js scripts/enterprise-org-ux.spec.js scripts/office-slice-c1.spec.js scripts/office-slice-c2.spec.js scripts/office-slice-c3.spec.js scripts/office-slice-c4.spec.js scripts/office-slice-c5.spec.js scripts/office-slice-c6.spec.js scripts/office-slice-c7.spec.js scripts/office-slice-c8.spec.js scripts/office-slice-c8-runtime.spec.js scripts/request-workflow-d2.spec.js scripts/request-workflow-d3b.spec.js scripts/taxpayer-print.spec.js --reporter=line --workers=1
87
88 c8-policy-database:
89 runs-on: ubuntu-latest
@@ -176,3 +178,7 @@ jobs:
178 run: psql -v ON_ERROR_STOP=1 -f supabase/migrations/20261001230000_request_workflow_d3a_step_runtime.sql
179 - name: Execute D3a sequential and parallel workflow assertions
180 run: psql -v ON_ERROR_STOP=1 -f supabase/tests/request_d3a_workflow_behavior.sql
181 + - name: Apply D3b conditions and timeline
182 + run: psql -v ON_ERROR_STOP=1 -f supabase/migrations/20261001233000_request_workflow_d3b_conditions_timeline.sql
183 + - name: Execute D3b condition and timeline assertions
184 + run: psql -v ON_ERROR_STOP=1 -f supabase/tests/request_d3b_condition_timeline.sql
scripts/request-workflow-d3b-check.mjs new
+19
@@ -0,0 +1,19 @@
1 +import fs from 'node:fs';
2 +import assert from 'node:assert/strict';
3 +
4 +const js=fs.readFileSync('js/request-workflow-d3b.js','utf8');
5 +const migration=fs.readFileSync('supabase/migrations/20261001233000_request_workflow_d3b_conditions_timeline.sql','utf8');
6 +const bootstrap=fs.readFileSync('js/bootstrap.js','utf8');
7 +
8 +assert.match(js,/requestD3bAddStep/,'D3b visual editor must expose step creation');
9 +assert.match(js,/requestD3bPublishCombined/,'D3b editor must publish immutable workflow versions through the server RPC');
10 +assert.match(js,/requestD3bOpenTimeline/,'D3b must expose the read-only runtime timeline');
11 +assert.match(js,/schemaVersion\s*:\s*2/,'D3b editor must publish workflow schema v2');
12 +assert.match(js,/condition/,'D3b editor must model bounded step conditions');
13 +assert.match(migration,/validate_request_condition/,'D3b must validate the condition DSL on the server');
14 +assert.match(migration,/evaluate_request_condition/,'D3b must evaluate conditions on the server');
15 +assert.match(migration,/request_workflow_timeline/,'D3b must expose an authorized read-only timeline RPC');
16 +assert.match(migration,/condition_matched/,'D3b runtime must retain condition evaluation evidence');
17 +assert.match(migration,/skipped/,'D3b runtime must preserve skipped-step evidence');
18 +assert.match(bootstrap,/request-workflow-d2\.js[\s\S]*request-workflow-d3b\.js/,'D3b must load after D2');
19 +console.log('Request Workflow D3b invariants: OK');
scripts/request-workflow-d3b.spec.js new
+44
@@ -0,0 +1,44 @@
1 +const {test,expect}=require('@playwright/test');
2 +
3 +async function boot(page){
4 + await page.goto('http://127.0.0.1:4173/index.html',{waitUntil:'domcontentloaded'});
5 + await page.waitForFunction(()=>typeof window.requestD3bAddStep==='function'&&typeof window.requestD3bOpenTimeline==='function');
6 + await page.evaluate(()=>{
7 + window.__d3b={rpc:[],alerts:[]};window.alert=m=>window.__d3b.alerts.push(String(m));
8 + currentUser={id:'11111111-1111-1111-1111-111111111111',email:'d3b@example.test',organizationId:'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',organizationName:'D3b Org',isOrganizationOwner:false,commercialModules:['requests_workflow'],modulePermissions:[{module_key:'requests_workflow',capabilities:['read','configure','create','edit','approve']}],modules:['requests_workflow'],subscription:{type:'lifetime',status:'active',endDate:'2099-01-01'}};
9 + const defs=[{id:'d1111111-1111-1111-1111-111111111111',organization_id:currentUser.organizationId,code:'PURCHASE',title:'درخواست خرید',category:'تدارکات',revision:2,active:true}];
10 + 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:'number',required:true}]},published_at:'2026-10-01T00:00:00Z'}];
11 + const workflows=[{id:'w1111111-1111-1111-1111-111111111111',organization_id:currentUser.organizationId,request_type_version_id:versions[0].id,workflow_schema:{schemaVersion:2,steps:[{key:'manager',title:'مدیر',mode:'sequential',requiredApprovals:1,requiredCapability:'approve'}]},published_at:'2026-10-01T00:00:00Z'}];
12 + const instances=[{id:'r1111111-1111-1111-1111-111111111111',organization_id:currentUser.organizationId,request_type_version_id:versions[0].id,requester_user_id:currentUser.id,status:'submitted',revision:2,updated_at:'2026-10-01T00:00:00Z'}];
13 + sb.from=(table)=>({select:()=>({eq:()=>({order:()=>Promise.resolve({data:table==='request_type_definitions'?defs:table==='request_type_versions'?versions:table==='request_workflow_versions'?workflows:instances,error:null})})})});
14 + sb.rpc=async(name,args)=>{window.__d3b.rpc.push({name,args:JSON.parse(JSON.stringify(args||{}))});if(name==='request_publish_type_version')return {data:[{request_type_id:defs[0].id,request_type_version_id:versions[0].id,workflow_version_id:workflows[0].id,version_no:3,definition_revision:3}],error:null};if(name==='request_workflow_timeline')return {data:{request:{id:instances[0].id,status:'submitted',revision:2},steps:[{key:'manager',title:'مدیر',index:1,mode:'sequential',state:'skipped',requiredApprovals:1,approvalsReceived:0,conditionMatched:false},{key:'finance',title:'مالی',index:2,mode:'parallel',state:'active',requiredApprovals:2,approvalsReceived:1,conditionMatched:true}],events:[]},error:null};return {data:null,error:null}};
15 + switchView('view-requests');
16 + });
17 +}
18 +
19 +test('D3b visual editor publishes ordered steps with a bounded condition',async({page})=>{
20 + await boot(page);await page.evaluate(async()=>{await requestsRender('types');await requestD2EditType('d1111111-1111-1111-1111-111111111111')});
21 + await expect(page.locator('#request-d3b-editor')).toBeVisible();
22 + await expect(page.locator('.request-d3b-step')).toHaveCount(1);
23 + await page.evaluate(()=>requestD3bAddStep());
24 + await expect(page.locator('.request-d3b-step')).toHaveCount(2);
25 + await page.fill('.request-d3b-step:nth-child(2) [data-d3b="key"]','finance');
26 + await page.fill('.request-d3b-step:nth-child(2) [data-d3b="title"]','مالی');
27 + await page.selectOption('.request-d3b-step:nth-child(2) [data-d3b="mode"]','parallel');
28 + await page.fill('.request-d3b-step:nth-child(2) [data-d3b="approvals"]','2');
29 + await page.fill('.request-d3b-step:nth-child(2) [data-d3b="condition-field"]','f1');
30 + await page.selectOption('.request-d3b-step:nth-child(2) [data-d3b="condition-op"]','gte');
31 + await page.fill('.request-d3b-step:nth-child(2) [data-d3b="condition-value"]','1000');
32 + await page.evaluate(()=>requestD3bPublishCombined());
33 + const call=await page.evaluate(()=>window.__d3b.rpc.find(x=>x.name==='request_publish_type_version'));
34 + expect(call.args.p_workflow_schema.schemaVersion).toBe(2);expect(call.args.p_workflow_schema.steps).toHaveLength(2);expect(call.args.p_workflow_schema.steps[1].condition).toEqual({field:'f1',op:'gte',value:1000});
35 +});
36 +
37 +test('D3b renders a read-only timeline with skipped and active condition evidence',async({page})=>{
38 + await boot(page);await page.evaluate(()=>requestsRender('mine'));
39 + await expect(page.locator('#request-d3b-runtime')).toBeVisible();
40 + await page.evaluate(()=>requestD3bOpenTimeline('r1111111-1111-1111-1111-111111111111'));
41 + await expect(page.locator('#request-d3b-timeline')).toContainText('رد شده توسط شرط');
42 + await expect(page.locator('#request-d3b-timeline')).toContainText('فعال');
43 + await expect(page.locator('#request-d3b-timeline')).toContainText('۱ از ۲');
44 +});
supabase/tests/request_d3b_condition_timeline.sql new
+61
@@ -0,0 +1,61 @@
1 +\set ON_ERROR_STOP on
2 +insert into public.profiles(id) values ('44444444-4444-4444-4444-444444444444') on conflict do nothing;
3 +
4 +set role authenticated;
5 +select set_config('request.jwt.claim.sub','11111111-1111-1111-1111-111111111111',false);
6 +select set_config('request.jwt.claim.org','aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',false);
7 +select set_config('request.jwt.claim.member_active','true',false);
8 +select set_config('request.jwt.claim.entitled','true',false);
9 +select set_config('request.jwt.claim.write','true',false);
10 +select set_config('request.jwt.claim.capabilities','read,configure,create,edit,approve',false);
11 +
12 +create temporary table d3b_ids(k text primary key,v uuid);
13 +insert into d3b_ids
14 +select 'type',request_type_version_id from public.request_publish_type_version(
15 + 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','D3B_COND','گردش شرطی','Workflow',
16 + '{"schemaVersion":1,"fields":[{"key":"amount","label":"مبلغ","type":"number","required":true}]}'::jsonb,
17 + '{"schemaVersion":2,"steps":[{"key":"manager","title":"مدیر","mode":"sequential","requiredApprovals":1,"requiredCapability":"approve","condition":{"field":"amount","op":"gte","value":1000}},{"key":"finance","title":"مالی","mode":"sequential","requiredApprovals":1,"requiredCapability":"approve"}]}'::jsonb,
18 + 0
19 +);
20 +insert into d3b_ids values('instance',public.request_create_instance('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',(select v from d3b_ids where k='type'),'{"amount":500}'::jsonb,'d3b-create-0001'));
21 +
22 +reset role;
23 +do $$ declare i uuid; begin
24 + i=(select v from d3b_ids where k='instance');
25 + if not exists(select 1 from private.request_step_instances where request_instance_id=i and step_key='manager' and state='skipped' and condition_matched=false) then raise exception 'false condition was not retained as skipped evidence'; end if;
26 + if not exists(select 1 from private.request_step_instances where request_instance_id=i and step_key='finance' and state='active' and condition_matched=true) then raise exception 'first matched step was not activated'; end if;
27 +end $$;
28 +
29 +set role authenticated;
30 +select set_config('request.jwt.claim.sub','11111111-1111-1111-1111-111111111111',false);
31 +select set_config('request.jwt.claim.org','aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',false);
32 +select set_config('request.jwt.claim.entitled','true',false);
33 +select set_config('request.jwt.claim.capabilities','read,configure,create,edit,approve',false);
34 +do $$ declare t jsonb; begin
35 + t:=public.request_workflow_timeline((select v from d3b_ids where k='instance'));
36 + if jsonb_array_length(t->'steps')<>2 then raise exception 'timeline step count mismatch'; end if;
37 + if (t->'steps'->0->>'state')<>'skipped' then raise exception 'timeline did not expose skipped step'; end if;
38 + if (t->'steps'->1->>'state')<>'active' then raise exception 'timeline did not expose active step'; end if;
39 + if (t->'steps'->0->>'conditionMatched')::boolean<>false then raise exception 'timeline lost condition evidence'; end if;
40 +end $$;
41 +
42 +do $$ begin
43 + begin
44 + perform * from public.request_publish_type_version('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','D3B_BAD_OP','bad',null,'{}'::jsonb,'{"schemaVersion":2,"steps":[{"key":"x","title":"X","mode":"sequential","requiredApprovals":1,"requiredCapability":"approve","condition":{"field":"amount","op":"sql","value":"drop table"}}]}'::jsonb,0);
45 + raise exception 'unsafe condition operator unexpectedly published';
46 + exception when others then if position('condition operator is not allowed' in sqlerrm)=0 then raise; end if; end;
47 +end $$;
48 +
49 +do $$ begin
50 + begin
51 + perform * from public.request_publish_type_version('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','D3B_BAD_FIELD','bad',null,'{}'::jsonb,'{"schemaVersion":2,"steps":[{"key":"x","title":"X","mode":"sequential","requiredApprovals":1,"requiredCapability":"approve","condition":{"field":"amount;drop","op":"eq","value":1}}]}'::jsonb,0);
52 + raise exception 'unsafe condition field unexpectedly published';
53 + exception when others then if position('condition field is invalid' in sqlerrm)=0 then raise; end if; end;
54 +end $$;
55 +
56 +select set_config('request.jwt.claim.org','bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb',false);
57 +do $$ begin
58 + begin perform public.request_workflow_timeline((select v from d3b_ids where k='instance')); raise exception 'cross-tenant timeline unexpectedly succeeded';
59 + exception when insufficient_privilege then null; end;
60 +end $$;
61 +reset role;