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

test: define C8 runtime behavioral contract

davoodmehraban89 committed Oct 1, 2026 at 21:23 UTC 49af0b7e79c5edda0a43804e259dc9726e541034
1 file changed +109 -1
supabase/tests/office_c8_runtime.sql renamed
+109 -1
@@ -1,6 +1,4 @@
1 -
1 +-- Office C8 Task 2 transactional behavior. Entire fixture rolls back.
2 begin;
3
4 do $$
@@ -12,6 +10,112 @@ begin
10 if to_regprocedure('public.office_workflow_read(uuid,text,bigint,integer)') is null then raise exception 'C8 read RPC missing'; end if;
11 end $$;
12
13 +insert into public.profiles(id) values
14 + ('11111111-1111-4111-8111-111111111111'),('22222222-2222-4222-8222-222222222222') on conflict do nothing;
15 +insert into public.organizations(id,owner_user_id) values
16 + ('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','11111111-1111-4111-8111-111111111111'),
17 + ('bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb','22222222-2222-4222-8222-222222222222') on conflict do nothing;
18 +insert into public.records(organization_id,owner_id,collection,id,data) values
19 + ('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','11111111-1111-4111-8111-111111111111','correspondence','LETTER_OK','{"status":"registered","subject":"نامه آزمون"}'),
20 + ('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','11111111-1111-4111-8111-111111111111','correspondence','LETTER_DRAFT','{"status":"draft"}'),
21 + ('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','11111111-1111-4111-8111-111111111111','correspondence','LETTER_CLOSED','{"status":"closed"}'),
22 + ('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','11111111-1111-4111-8111-111111111111','correspondence','LETTER_NULL','{}'),
23 + ('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','11111111-1111-4111-8111-111111111111','correspondence','LETTER_REPLAY','{"status":"submitted"}')
24 +on conflict do nothing;
25 +insert into private.office_workflow_policy_versions(organization_id,policy_key,version,title,definition,published_by)
26 +values('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','basic',1,'گردش <آزمون>',
27 + '{"stages":[{"id":"start","label":"شروع <امن>","start":true,"terminal":false},{"id":"review","label":"بررسی","start":false,"terminal":false},{"id":"done","label":"پایان","start":false,"terminal":true}],"edges":[{"id":"to_review","from":"start","to":"review","label":"ارجاع \"بررسی\"","capability":"refer"},{"id":"to_done","from":"review","to":"done","label":"تأیید نهایی","capability":"approve"}]}'::jsonb,
28 + '11111111-1111-4111-8111-111111111111');
29 +
30 +select set_config('request.jwt.claim.sub','11111111-1111-4111-8111-111111111111',true);
31 +select set_config('request.jwt.claim.org','aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',true);
32 +select set_config('request.jwt.claim.member_active','true',true);
33 +select set_config('request.jwt.claim.entitled','true',true);
34 +select set_config('request.jwt.claim.write','true',true);
35 +select set_config('request.jwt.claim.readable','true',true);
36 +select set_config('request.jwt.claim.capabilities','read,refer,edit,approve,configure',true);
37 +
38 +do $$
39 +declare a jsonb; t jsonb; r jsonb; iid uuid; eid uuid;
40 +begin
41 + a:=public.office_workflow_attach('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','LETTER_OK','basic',1,'00000000-0000-4000-8000-000000000001');
42 + if a->>'stage_id'<>'start' or (a->>'revision')::bigint<>0 then raise exception 'attach result mismatch: %',a; end if;
43 + iid:=(a->>'instance_id')::uuid; eid:=(a->>'event_id')::uuid;
44 + if (select count(*) from private.office_workflow_events where instance_id=iid)<>1 then raise exception 'attach must create exactly one event'; end if;
45 + if public.office_workflow_attach('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','LETTER_OK','basic',1,'00000000-0000-4000-8000-000000000001')<>a then raise exception 'attach retry must return original result'; end if;
46 + if (select count(*) from private.office_workflow_events where instance_id=iid)<>1 then raise exception 'attach retry duplicated event'; end if;
47 +
48 + t:=public.office_workflow_transition('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',iid,'to_review',0,'00000000-0000-4000-8000-000000000002','برای بررسی');
49 + if t->>'stage_id'<>'review' or (t->>'revision')::bigint<>1 then raise exception 'transition result mismatch: %',t; end if;
50 + if public.office_workflow_transition('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',iid,'to_review',0,'00000000-0000-4000-8000-000000000002','برای بررسی')<>t then raise exception 'transition retry must return original result'; end if;
51 + if (select count(*) from private.office_workflow_events where instance_id=iid and revision=1)<>1 then raise exception 'transition retry duplicated revision'; end if;
52 +
53 + begin perform public.office_workflow_transition('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',iid,'to_review',0,'00000000-0000-4000-8000-000000000003','stale'); raise exception 'stale revision accepted'; exception when serialization_failure then null; end;
54 + begin perform public.office_workflow_transition('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',iid,'to_review',1,'00000000-0000-4000-8000-000000000004','illegal'); raise exception 'illegal edge accepted'; exception when invalid_parameter_value then null; end;
55 + begin perform public.office_workflow_transition('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',iid,'to_done',null,'00000000-0000-4000-8000-000000000005','null revision'); raise exception 'null revision accepted'; exception when invalid_parameter_value then null; end;
56 + begin perform public.office_workflow_transition('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',iid,'to_done',-1,'00000000-0000-4000-8000-000000000006','negative'); raise exception 'negative revision accepted'; exception when invalid_parameter_value then null; end;
57 +
58 + t:=public.office_workflow_transition('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',iid,'to_done',1,'00000000-0000-4000-8000-000000000007','تمام');
59 + if t->>'stage_id'<>'done' or (t->>'revision')::bigint<>2 then raise exception 'terminal transition mismatch'; end if;
60 + r:=public.office_workflow_read('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','LETTER_OK',-1,100);
61 + if (r->'instance'->>'policy_key')<>'basic' or (r->'instance'->>'policy_version')::int<>1 or jsonb_array_length(r->'events')<>3 then raise exception 'read history mismatch: %',r; end if;
62 + if r->'events'->0->>'to_stage_label'<>'شروع <امن>' then raise exception 'historical stage label lost'; end if;
63 + if coalesce(r->'events'->1->>'edge_label','')<>'ارجاع "بررسی"' then raise exception 'historical edge label lost'; end if;
64 +end $$;
65 +
66 +-- Invalid source states fail closed.
67 +do $$ begin
68 + begin perform public.office_workflow_attach('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','LETTER_DRAFT','basic',1,'00000000-0000-4000-8000-000000000010'); raise exception 'draft attach accepted'; exception when invalid_parameter_value then null; end;
69 + begin perform public.office_workflow_attach('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','LETTER_CLOSED','basic',1,'00000000-0000-4000-8000-000000000011'); raise exception 'closed attach accepted'; exception when invalid_parameter_value then null; end;
70 + begin perform public.office_workflow_attach('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','LETTER_NULL','basic',1,'00000000-0000-4000-8000-000000000012'); raise exception 'null status attach accepted'; exception when invalid_parameter_value then null; end;
71 +end $$;
72 +
73 +-- Expired/read-only entitlement preserves read but blocks writes and replay leakage.
74 +do $$ declare a jsonb; iid uuid; t jsonb; begin
75 + a:=public.office_workflow_attach('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','LETTER_REPLAY','basic',1,'00000000-0000-4000-8000-000000000020'); iid:=(a->>'instance_id')::uuid;
76 + t:=public.office_workflow_transition('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',iid,'to_review',0,'00000000-0000-4000-8000-000000000021','first');
77 + perform set_config('request.jwt.claim.write','false',true);
78 + if public.office_workflow_read('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','LETTER_REPLAY',-1,10)->'instance' is null then raise exception 'expired read-only access lost history'; end if;
79 + if jsonb_array_length(public.office_workflow_read('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','LETTER_REPLAY',-1,10)->'allowed_edges')<>0 then raise exception 'expired read returned executable edges'; end if;
80 + begin perform public.office_workflow_transition('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',iid,'to_review',0,'00000000-0000-4000-8000-000000000021','first'); raise exception 'expired replay leaked prior result'; exception when insufficient_privilege then null; end;
81 + perform set_config('request.jwt.claim.write','true',true);
82 + perform set_config('request.jwt.claim.capabilities','read',true);
83 + begin perform public.office_workflow_transition('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',iid,'to_review',0,'00000000-0000-4000-8000-000000000021','first'); raise exception 'revoked capability replay leaked prior result'; exception when insufficient_privilege then null; end;
84 + perform set_config('request.jwt.claim.capabilities','read,refer,edit,approve,configure',true);
85 +end $$;
86 +
87 +-- Membership/tenant/visibility/auth denial.
88 +do $$ begin
89 + perform set_config('request.jwt.claim.member_active','false',true);
90 + begin perform public.office_workflow_read('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','LETTER_OK',-1,10); raise exception 'suspended member read accepted'; exception when insufficient_privilege then null; end;
91 + perform set_config('request.jwt.claim.member_active','true',true);
92 + perform set_config('request.jwt.claim.org','bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb',true);
93 + begin perform public.office_workflow_read('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','LETTER_OK',-1,10); raise exception 'foreign tenant read accepted'; exception when insufficient_privilege then null; end;
94 + perform set_config('request.jwt.claim.org','aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',true);
95 + perform set_config('request.jwt.claim.readable','false',true);
96 + begin perform public.office_workflow_read('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','LETTER_OK',-1,10); raise exception 'unreadable record read accepted'; exception when insufficient_privilege then null; end;
97 + perform set_config('request.jwt.claim.readable','true',true);
98 + perform set_config('request.jwt.claim.sub','',true);
99 + begin perform public.office_workflow_read('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','LETTER_OK',-1,10); raise exception 'anonymous read accepted'; exception when insufficient_privilege then null; end;
100 + perform set_config('request.jwt.claim.sub','11111111-1111-4111-8111-111111111111',true);
101 +end $$;
102 +
103 +-- Direct client DML is closed; immutable evidence/policy binding survives privileged attempts.
104 +do $$ declare iid uuid; begin
105 + if has_table_privilege('authenticated','private.office_workflow_instances','INSERT') then raise exception 'authenticated can insert instances directly'; end if;
106 + if has_table_privilege('authenticated','private.office_workflow_events','INSERT') then raise exception 'authenticated can insert events directly'; end if;
107 + select id into iid from private.office_workflow_instances where correspondence_id='LETTER_OK';
108 + begin update private.office_workflow_events set note='forged' where instance_id=iid and revision=1; raise exception 'event update accepted'; exception when object_not_in_prerequisite_state then null; end;
109 + begin delete from private.office_workflow_events where instance_id=iid and revision=1; raise exception 'event delete accepted'; exception when object_not_in_prerequisite_state then null; end;
110 + begin update private.office_workflow_instances set policy_version=99 where id=iid; raise exception 'pinned policy mutation accepted'; exception when object_not_in_prerequisite_state then null; end;
111 +end $$;
112 +
113 +-- Cursor and request-key validation.
114 +do $$ begin
115 + begin perform public.office_workflow_read('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','LETTER_OK',-2,10); raise exception 'invalid cursor accepted'; exception when invalid_parameter_value then null; end;
116 + begin perform public.office_workflow_read('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','LETTER_OK',-1,0); raise exception 'invalid limit accepted'; exception when invalid_parameter_value then null; end;
117 + begin perform public.office_workflow_attach('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','LETTER_DRAFT','basic',1,null); raise exception 'null request id accepted'; exception when invalid_parameter_value then null; end;
118 + begin perform public.office_workflow_attach('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa','LETTER_REPLAY','basic',1,'00000000-0000-4000-8000-000000000020'); raise exception 'reused request key with different payload accepted'; exception when unique_violation then null; end;
119 +end $$;
120 +
121 rollback;