@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / d7f5972
Fork 0

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

Mirrored from https://github.com/davoodmehraban89/Factor-easy.git

Synced Dec 3, 17:00 UTC

test: define D3c SLA delegation and outbox behavior

davoodmehraban89 committed Oct 1, 2026 at 23:14 UTC d7f5972476cabde22b340bbcfa94ac2aa7e57093
4 files changed +106 -1
.github/workflows/quality.yml
+7 -1
@@ -76,6 +76,8 @@ jobs:
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: Request Workflow Slice D3c operations invariants
80 + run: node scripts/request-workflow-d3c-check.mjs
81 - name: Install browser smoke-test runtime
82 run: |
83 npm install --no-save @playwright/test@1.55.0 pdf-lib@1.17.1
@@ -83,7 +85,7 @@ jobs:
85 - name: Real DOM smoke
86 run: |
87 python3 -m http.server 4173 >/tmp/finora-http.log 2>&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
88 + 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/request-workflow-d3c.spec.js scripts/taxpayer-print.spec.js --reporter=line --workers=1
89
90 c8-policy-database:
91 runs-on: ubuntu-latest
@@ -182,3 +184,7 @@ jobs:
184 run: psql -v ON_ERROR_STOP=1 -f supabase/migrations/20261001233000_request_workflow_d3b_conditions_timeline.sql
185 - name: Execute D3b condition and timeline assertions
186 run: psql -v ON_ERROR_STOP=1 -f supabase/tests/request_d3b_condition_timeline.sql
187 + - name: Apply D3c SLA delegation and outbox
188 + run: psql -v ON_ERROR_STOP=1 -f supabase/migrations/20261001235500_request_workflow_d3c_operations.sql
189 + - name: Execute D3c operational assertions
190 + run: psql -v ON_ERROR_STOP=1 -f supabase/tests/request_d3c_operations.sql
scripts/request-workflow-d3c-check.mjs new
+17
@@ -0,0 +1,17 @@
1 +import fs from 'node:fs';
2 +import assert from 'node:assert/strict';
3 +const js=fs.readFileSync('js/request-workflow-d3c.js','utf8');
4 +const migration=fs.readFileSync('supabase/migrations/20261001235500_request_workflow_d3c_operations.sql','utf8');
5 +const bootstrap=fs.readFileSync('js/bootstrap.js','utf8');
6 +assert.match(js,/requestD3cCreateDelegation/,'D3c must expose explicit delegation management');
7 +assert.match(js,/requestD3cEscalateOverdue/,'D3c must expose controlled SLA escalation');
8 +assert.match(js,/request_outbox_status/,'D3c UI must surface transactional outbox state');
9 +assert.match(js,/slaHours/,'D3c editor must publish step SLA configuration');
10 +assert.match(migration,/request_delegations/,'D3c must persist delegation evidence');
11 +assert.match(migration,/delegated_from_user_id/,'D3c votes must retain substitution provenance');
12 +assert.match(migration,/request_outbox/,'D3c must use a transactional outbox');
13 +assert.match(migration,/request_escalate_overdue/,'D3c must expose server-authorized escalation');
14 +assert.match(migration,/sla_hours/,'D3c must persist SLA configuration on runtime steps');
15 +assert.match(migration,/request_step_decide/,'D3c must integrate delegation and outbox with the authoritative decision transaction');
16 +assert.match(bootstrap,/request-workflow-d3b\.js[\s\S]*request-workflow-d3c\.js/,'D3c must load after D3b');
17 +console.log('Request Workflow D3c invariants: OK');
scripts/request-workflow-d3c.spec.js new
+29
@@ -0,0 +1,29 @@
1 +const {test,expect}=require('@playwright/test');
2 +
3 +test('D3c adds SLA configuration and surfaces outbox/runtime operations',async({page})=>{
4 + await page.goto('http://127.0.0.1:4173/index.html',{waitUntil:'domcontentloaded'});
5 + await page.waitForFunction(()=>typeof window.requestD3cCreateDelegation==='function');
6 + await page.evaluate(()=>{
7 + window.__d3c={rpc:[]};window.alert=()=>{};
8 + currentUser={id:'11111111-1111-1111-1111-111111111111',email:'owner@example.test',organizationId:'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',organizationName:'D3c Org',isOrganizationOwner:true,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:'OPS',title:'عملیات',category:'Workflow',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:'Workflow',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:'approve',title:'تأیید',mode:'sequential',requiredApprovals:1,requiredCapability:'approve',slaHours:4}],onApproved:[{targetModule:'commerce',actionKey:'create_purchase_draft',payload:{}}]},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:'approved',revision:2,updated_at:'2026-10-01T00:00:00Z'}];
13 + const members=[{id:'m1',organization_id:currentUser.organizationId,user_id:currentUser.id,status:'active',is_owner:true},{id:'m2',organization_id:currentUser.organizationId,user_id:'55555555-5555-5555-5555-555555555555',status:'active',is_owner:false}];
14 + sb.from=(table)=>({select:()=>({eq:()=>({order:()=>Promise.resolve({data:table==='request_type_definitions'?defs:table==='request_type_versions'?versions:table==='request_workflow_versions'?workflows:table==='organization_members'?members:instances,error:null})})})});
15 + sb.rpc=async(name,args)=>{window.__d3c.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:'approved',revision:2},steps:[{key:'approve',title:'تأیید',index:1,mode:'sequential',state:'completed',requiredApprovals:1,approvalsReceived:1,conditionMatched:true,slaHours:4,dueAt:'2026-10-01T04:00:00Z',escalatedAt:null,escalationLevel:0}],events:[]},error:null};if(name==='request_outbox_status')return {data:[{targetModule:'commerce',actionKey:'create_purchase_draft',status:'pending',attempts:0,createdAt:'2026-10-01T01:00:00Z'}],error:null};if(name==='request_list_delegations')return {data:[],error:null};if(name==='request_escalate_overdue')return {data:0,error:null};return {data:null,error:null}};
16 + switchView('view-requests');
17 + });
18 + await page.evaluate(async()=>{await requestsRender('types');await requestD2EditType('d1111111-1111-1111-1111-111111111111')});
19 + await expect(page.locator('[data-d3c-sla]')).toHaveValue('4');
20 + await page.fill('[data-d3c-sla]','8');
21 + await page.evaluate(()=>requestD3bPublishCombined());
22 + const call=await page.evaluate(()=>window.__d3c.rpc.find(x=>x.name==='request_publish_type_version'));
23 + expect(call.args.p_workflow_schema.steps[0].slaHours).toBe(8);
24 + await page.evaluate(()=>requestsRender('mine'));
25 + await expect(page.locator('#request-d3c-ops')).toBeVisible();
26 + await page.evaluate(()=>requestD3bOpenTimeline('r1111111-1111-1111-1111-111111111111'));
27 + await expect(page.locator('#request-d3c-runtime-evidence')).toContainText('SLA');
28 + await expect(page.locator('#request-d3c-runtime-evidence')).toContainText('در انتظار ارسال');
29 +});
supabase/tests/request_d3c_operations.sql new
+53
@@ -0,0 +1,53 @@
1 +\set ON_ERROR_STOP on
2 +insert into public.profiles(id) values ('55555555-5555-5555-5555-555555555555') on conflict do nothing;
3 +insert into public.organization_members(organization_id,user_id,status,is_owner,position_title) values('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','55555555-5555-5555-5555-555555555555','active',false,'جانشین') on conflict(organization_id,user_id) do update set status='active';
4 +
5 +set role authenticated;
6 +select set_config('request.jwt.claim.sub','11111111-1111-1111-1111-111111111111',false);
7 +select set_config('request.jwt.claim.org','aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',false);
8 +select set_config('request.jwt.claim.member_active','true',false);
9 +select set_config('request.jwt.claim.entitled','true',false);
10 +select set_config('request.jwt.claim.write','true',false);
11 +select set_config('request.jwt.claim.capabilities','read,configure,create,edit,approve',false);
12 +create temporary table d3c_ids(k text primary key,v uuid);
13 +insert into d3c_ids select 'type',request_type_version_id from public.request_publish_type_version('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','D3C_OPS','عملیات درخواست','Workflow','{"schemaVersion":1,"fields":[{"key":"amount","label":"مبلغ","type":"number","required":true}]}'::jsonb,'{"schemaVersion":2,"steps":[{"key":"approve","title":"تأیید","mode":"sequential","requiredApprovals":1,"requiredCapability":"approve","slaHours":1}],"onApproved":[{"targetModule":"commerce","actionKey":"create_purchase_draft","payload":{"source":"request"}}]}'::jsonb,0);
14 +insert into d3c_ids values('instance',public.request_create_instance('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',(select v from d3c_ids where k='type'),'{"amount":"500"}'::jsonb,'d3c-create-0001'));
15 +insert into d3c_ids values('delegation',public.request_create_delegation('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','55555555-5555-5555-5555-555555555555','approve',now(),now()+interval '2 days','مرخصی'));
16 +reset role;
17 +
18 +do $$ declare i uuid; begin i=(select v from d3c_ids where k='instance');if not exists(select 1 from private.request_step_instances where request_instance_id=i and state='active' and sla_hours=1 and due_at is not null) then raise exception 'SLA deadline was not materialized';end if;end $$;
19 +
20 +set role authenticated;
21 +select set_config('request.jwt.claim.sub','55555555-5555-5555-5555-555555555555',false);
22 +select set_config('request.jwt.claim.org','aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',false);
23 +select set_config('request.jwt.claim.capabilities','read',false);
24 +do $$ declare r record; begin select * into r from public.request_step_decide((select v from d3c_ids where k='instance'),'approve',1,'d3c-vote-0001','به جانشینی') limit 1;if r.status<>'approved' then raise exception 'delegated approval did not complete request';end if;end $$;
25 +reset role;
26 +
27 +do $$ declare i uuid; begin i=(select v from d3c_ids where k='instance');if not exists(select 1 from private.request_step_votes where request_instance_id=i and actor_user_id='55555555-5555-5555-5555-555555555555' and delegated_from_user_id='11111111-1111-1111-1111-111111111111') then raise exception 'delegation provenance missing from vote';end if;if not exists(select 1 from private.request_outbox where request_instance_id=i and target_module='commerce' and action_key='create_purchase_draft' and status='pending') then raise exception 'approved request did not atomically enqueue outbox action';end if;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.capabilities','read,configure,create,edit,approve',false);
33 +insert into d3c_ids values('sla_instance',public.request_create_instance('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',(select v from d3c_ids where k='type'),'{"amount":"700"}'::jsonb,'d3c-create-0002'));
34 +reset role;
35 +update private.request_step_instances set due_at=now()-interval '5 minutes' where request_instance_id=(select v from d3c_ids where k='sla_instance') and state='active';
36 +set role authenticated;
37 +select set_config('request.jwt.claim.sub','11111111-1111-1111-1111-111111111111',false);
38 +select set_config('request.jwt.claim.org','aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',false);
39 +select set_config('request.jwt.claim.capabilities','read,configure,create,edit,approve',false);
40 +do $$ declare n integer; begin n:=public.request_escalate_overdue('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa');if n<>1 then raise exception 'expected one overdue escalation, got %',n;end if;end $$;
41 +reset role;
42 +do $$ declare i uuid; begin i=(select v from d3c_ids where k='sla_instance');if not exists(select 1 from private.request_step_instances where request_instance_id=i and escalated_at is not null and escalation_level=1) then raise exception 'escalation evidence missing';end if;if not exists(select 1 from public.request_instance_events where request_instance_id=i and action='step_escalated:approve') then raise exception 'immutable escalation event missing';end if;end $$;
43 +
44 +set role authenticated;
45 +select set_config('request.jwt.claim.sub','11111111-1111-1111-1111-111111111111',false);
46 +select set_config('request.jwt.claim.org','aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',false);
47 +select set_config('request.jwt.claim.capabilities','read,configure,create,edit,approve',false);
48 +do $$ declare x jsonb; begin x:=public.request_outbox_status((select v from d3c_ids where k='instance'));if jsonb_array_length(x)<>1 or x->0->>'status'<>'pending' then raise exception 'outbox status RPC mismatch';end if;end $$;
49 +select public.request_revoke_delegation('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',(select v from d3c_ids where k='delegation'));
50 +
51 +do $$ begin begin perform * from public.request_publish_type_version('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','D3C_BAD_SLA','bad',null,'{}'::jsonb,'{"schemaVersion":2,"steps":[{"key":"x","title":"X","mode":"sequential","requiredApprovals":1,"requiredCapability":"approve","slaHours":0}]}'::jsonb,0);raise exception 'invalid SLA unexpectedly published';exception when others then if position('slaHours must be between' in sqlerrm)=0 then raise;end if;end;end $$;
52 +do $$ begin begin perform * from public.request_publish_type_version('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','D3C_BAD_OUTBOX','bad',null,'{}'::jsonb,'{"schemaVersion":2,"steps":[{"key":"x","title":"X","mode":"sequential","requiredApprovals":1,"requiredCapability":"approve"}],"onApproved":[{"targetModule":"accounting","actionKey":"DROP TABLE","payload":{}}]}'::jsonb,0);raise exception 'unsafe outbox action unexpectedly published';exception when others then if position('outbox actionKey is invalid' in sqlerrm)=0 then raise;end if;end;end $$;
53 +reset role;