سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 3, 20:45 UTC
test: define D3a sequential and parallel workflow behavior
davoodmehraban89 committed
Oct 1, 2026 at 22:47 UTC
254a318cafc5811860c36649ebb1a91d9efa09b1
1 file changed
+86
supabase/tests/request_d3a_workflow_behavior.sql
new
+86
@@ -0,0 +1,86 @@
1
+\set ON_ERROR_STOP on
2
+insert into public.profiles(id) values ('33333333-3333-3333-3333-333333333333') 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 d3_ids(k text primary key,v uuid);
13
+insert into d3_ids
14
+select 'type',request_type_version_id from public.request_publish_type_version(
15
+ 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','D3_APPROVAL','تأیید چندمرحلهای','Workflow',
16
+ '{"schemaVersion":1,"fields":[{"key":"subject","label":"موضوع","type":"text","required":true}]}'::jsonb,
17
+ '{"schemaVersion":2,"steps":[{"key":"manager","title":"مدیر","mode":"sequential","requiredApprovals":1,"requiredCapability":"approve"},{"key":"finance","title":"مالی","mode":"parallel","requiredApprovals":2,"requiredCapability":"approve"}]}'::jsonb,
18
+ 0
19
+);
20
+insert into d3_ids values('instance',public.request_create_instance('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',(select v from d3_ids where k='type'),'{"subject":"خرید"}'::jsonb,'d3-create-000001'));
21
+
22
+do $$ declare i uuid; begin
23
+ i=(select v from d3_ids where k='instance');
24
+ if (select count(*) from private.request_step_instances where request_instance_id=i)<>2 then raise exception 'expected two D3 step instances'; end if;
25
+ if not exists(select 1 from private.request_step_instances where request_instance_id=i and step_key='manager' and state='active' and required_approvals=1) then raise exception 'first sequential step was not activated'; end if;
26
+ if not exists(select 1 from private.request_step_instances where request_instance_id=i and step_key='finance' and state='pending' and required_approvals=2) then raise exception 'parallel step was not initialized pending'; end if;
27
+end $$;
28
+
29
+-- First approver completes the sequential manager step and activates finance.
30
+select set_config('request.jwt.claim.sub','22222222-2222-2222-2222-222222222222',false);
31
+select set_config('request.jwt.claim.capabilities','read,approve',false);
32
+select * from public.request_step_decide((select v from d3_ids where k='instance'),'approve',1,'d3-vote-000001','manager ok');
33
+do $$ declare i uuid; begin
34
+ i=(select v from d3_ids where k='instance');
35
+ if (select revision from public.request_instances where id=i)<>2 then raise exception 'manager approval did not advance revision'; end if;
36
+ if (select status from public.request_instances where id=i)<>'submitted' then raise exception 'request completed before final step'; end if;
37
+ if not exists(select 1 from private.request_step_instances where request_instance_id=i and step_key='manager' and state='completed') then raise exception 'manager step not completed'; end if;
38
+ if not exists(select 1 from private.request_step_instances where request_instance_id=i and step_key='finance' and state='active') then raise exception 'finance step not activated'; end if;
39
+end $$;
40
+
41
+-- One finance approval is not enough for a parallel quorum of two.
42
+select * from public.request_step_decide((select v from d3_ids where k='instance'),'approve',2,'d3-vote-000002','finance 1');
43
+do $$ declare i uuid; begin
44
+ i=(select v from d3_ids where k='instance');
45
+ if (select status from public.request_instances where id=i)<>'submitted' then raise exception 'parallel quorum completed too early'; end if;
46
+ if (select revision from public.request_instances where id=i)<>3 then raise exception 'first parallel vote did not advance revision'; end if;
47
+ if (select count(*) from private.request_step_votes v join private.request_step_instances s on s.id=v.step_instance_id where s.request_instance_id=i and s.step_key='finance' and v.decision='approve')<>1 then raise exception 'first parallel vote missing'; end if;
48
+end $$;
49
+
50
+-- Idempotent retry must not add another vote or revision.
51
+select * from public.request_step_decide((select v from d3_ids where k='instance'),'approve',2,'d3-vote-000002','retry');
52
+do $$ declare i uuid; begin i=(select v from d3_ids where k='instance'); if (select revision from public.request_instances where id=i)<>3 then raise exception 'idempotent vote retry changed revision'; end if; end $$;
53
+
54
+-- Second distinct approver reaches quorum and closes the workflow.
55
+select set_config('request.jwt.claim.sub','33333333-3333-3333-3333-333333333333',false);
56
+select set_config('request.jwt.claim.capabilities','read,approve',false);
57
+select * from public.request_step_decide((select v from d3_ids where k='instance'),'approve',3,'d3-vote-000003','finance 2');
58
+do $$ declare i uuid; begin
59
+ i=(select v from d3_ids where k='instance');
60
+ if (select status from public.request_instances where id=i)<>'approved' then raise exception 'parallel quorum did not approve request'; end if;
61
+ if (select revision from public.request_instances where id=i)<>4 then raise exception 'final approval did not advance revision'; end if;
62
+ if not exists(select 1 from private.request_step_instances where request_instance_id=i and step_key='finance' and state='completed') then raise exception 'finance step not completed'; end if;
63
+ if (select count(*) from public.request_instance_events where request_instance_id=i and action like 'step_approve:%')<>3 then raise exception 'step decision evidence incomplete'; end if;
64
+end $$;
65
+
66
+-- Invalid D3 schema must fail before a version is published.
67
+select set_config('request.jwt.claim.sub','11111111-1111-1111-1111-111111111111',false);
68
+select set_config('request.jwt.claim.capabilities','read,configure,create,edit,approve',false);
69
+do $$ begin
70
+ begin
71
+ perform * from public.request_publish_type_version('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','D3_BAD','bad',null,'{}'::jsonb,'{"schemaVersion":2,"steps":[{"key":"x","mode":"parallel","requiredApprovals":1,"requiredCapability":"approve"}]}'::jsonb,0);
72
+ raise exception 'invalid parallel schema unexpectedly published';
73
+ exception when others then if position('parallel step requires at least two approvals' in sqlerrm)=0 then raise; end if; end;
74
+end $$;
75
+
76
+-- Capability is enforced from the active step on the server.
77
+insert into d3_ids
78
+select 'type2',request_type_version_id from public.request_publish_type_version('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','D3_CAP','capability',null,'{}'::jsonb,'{"schemaVersion":2,"steps":[{"key":"approval","title":"Approval","mode":"sequential","requiredApprovals":1,"requiredCapability":"approve"}]}'::jsonb,0);
79
+insert into d3_ids values('instance2',public.request_create_instance('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',(select v from d3_ids where k='type2'),'{}'::jsonb,'d3-create-000002'));
80
+select set_config('request.jwt.claim.sub','22222222-2222-2222-2222-222222222222',false);
81
+select set_config('request.jwt.claim.capabilities','read',false);
82
+do $$ begin
83
+ begin perform * from public.request_step_decide((select v from d3_ids where k='instance2'),'approve',1,'d3-vote-000004',null); raise exception 'step decision without capability unexpectedly succeeded';
84
+ exception when insufficient_privilege then null; end;
85
+end $$;
86
+reset role;