-- V1-2.1 M5: the submission, approval and publication rules of contract §6.2, -- §6.3, §6.7 and §6.9 taken one at a time, after the independent audit (M1, -- M2, M3): approval on an archived sheet, the base of a derived draft, each -- rule failing alone, removal groups, no carry for an author, F2, D8 reads -- after archive, species, working copy, closed codes, audit. The verdict (and, -- for the publication rules, the approval) is written as the table owner, -- standing in for the worker of M6 (§12.2). Every assertion is scoped to the -- ids created here, so the file also runs on a stack that already holds -- sheets, rounds or a working copy. begin; select plan(111); -- Accounts: O owner, E admin + editor + publisher, V reviewer, V2 admin + -- editor + reviewer (an author of a later round), V3 reviewer, R reader. insert into auth.users (id, email, email_confirmed_at) values ('17000000-0000-0000-0000-000000000001', 'own@r17.local', now()), ('17000000-0000-0000-0000-00000000000e', 'edpub@r17.local', now()), ('17000000-0000-0000-0000-0000000000f1', 'rev@r17.local', now()), ('17000000-0000-0000-0000-0000000000f2', 'edrev@r17.local', now()), ('17000000-0000-0000-0000-0000000000f3', 'rev3@r17.local', now()), ('17000000-0000-0000-0000-0000000000cc', 'reader@r17.local', now()); insert into auth.mfa_factors (id, user_id, factor_type, status, created_at, updated_at) select gen_random_uuid(), id, 'totp', 'verified', now(), now() from auth.users where email like '%@r17.local'; insert into auth.sessions (id, user_id, aal) select ('17100000' || substr(id::text, 9))::uuid, id, 'aal2' from auth.users where email like '%@r17.local'; select set_config('request.jwt.claims', '', true); select private.assign_platform_role(null, '17000000-0000-0000-0000-000000000001', 'owner'); select private.assign_platform_role(null, '17000000-0000-0000-0000-00000000000e', 'admin'); select private.assign_platform_role(null, '17000000-0000-0000-0000-0000000000f2', 'admin'); insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, verification_note, granted_by) values ('17000000-0000-0000-0000-00000000000e', 'sheet_editor', 'a', 'c', '17000000-0000-0000-0000-000000000001'), ('17000000-0000-0000-0000-00000000000e', 'sheet_publisher', 'a', 'c', '17000000-0000-0000-0000-000000000001'), ('17000000-0000-0000-0000-0000000000f1', 'sheet_reviewer', 'a', 'c', '17000000-0000-0000-0000-000000000001'), ('17000000-0000-0000-0000-0000000000f2', 'sheet_editor', 'a', 'c', '17000000-0000-0000-0000-000000000001'), ('17000000-0000-0000-0000-0000000000f2', 'sheet_reviewer', 'a', 'c', '17000000-0000-0000-0000-000000000001'), ('17000000-0000-0000-0000-0000000000f3', 'sheet_reviewer', 'a', 'c', '17000000-0000-0000-0000-000000000001'), ('17000000-0000-0000-0000-0000000000cc', 'catalog_reader', 'a', null, '17000000-0000-0000-0000-000000000001'); create table pg_temp.k (key text primary key, id uuid); create function pg_temp.id(p_key text) returns uuid language sql stable as $$ select id from pg_temp.k where key = p_key $$; create function pg_temp.u(p text) returns uuid language sql immutable as $$ select ('17000000-0000-0000-0000-0000000000' || case p when 'O' then '01' when 'E' then '0e' when 'V' then 'f1' when 'V2' then 'f2' when 'V3' then 'f3' when 'R' then 'cc' end)::uuid; $$; -- The calling session of an account at aal2 (its session row exists). create function pg_temp.as_user(p text) returns void language plpgsql as $$ begin perform set_config('role', 'authenticated', true); perform set_config('request.jwt.claims', jsonb_build_object('sub', pg_temp.u(p), 'role', 'authenticated', 'aal', 'aal2', 'session_id', ('17100000' || substr(pg_temp.u(p)::text, 9))::uuid)::text, true); end $$; -- A small document: one main view, one annotation, one zone (z1) or two -- (z1, z2); `st` is the review status of every status-bearing element. create function pg_temp.doc(p_asset uuid, st text, p_zones int default 1) returns jsonb language sql as $$ select jsonb_build_object('schema_version', 2, 'species_id', 'equine', 'name', jsonb_build_object('fr', 'Cheval R', 'en', 'R horse'), 'description', jsonb_build_object('fr', 'd', 'en', 'd'), 'scope_disclaimer', jsonb_build_object('fr', 'x', 'en', 'x'), 'attribution', jsonb_build_object('authors', jsonb_build_array('F'), 'license', null, 'notes', null), 'vertebral_config', jsonb_build_object('regions', '[]'::jsonb), 'references', '[]'::jsonb, 'categories', jsonb_build_array(jsonb_build_object('id', 'c', 'name', jsonb_build_object('fr', 'C', 'en', 'C'), 'color', null)), 'views', jsonb_build_array(jsonb_build_object('id', 'main', 'kind', 'main', 'name', jsonb_build_object('fr', 'V', 'en', 'V'), 'svg_asset_id', p_asset, 'view_box', null)), 'annotations', jsonb_build_array(jsonb_build_object('id', 'a1', 'kind', 'point', 'view_id', 'main', 'category_id', 'c', 'laterality', 'unpaired', 'geometry', null, 'review_status', st, 'name', jsonb_build_object('fr', 'P', 'en', 'P'), 'finding_zone_id', 'z1', 'vertebral_region_code', null)), 'finding_zones', (select jsonb_agg(jsonb_build_object('id', 'z' || i, 'name', jsonb_build_object('fr', 'Z', 'en', 'Z'), 'categories', jsonb_build_array(jsonb_build_object('id', 'cat', 'kind', 'dysfunction', 'name', jsonb_build_object('fr', 'D', 'en', 'D'), 'review_status', st, 'children', jsonb_build_array(jsonb_build_object('id', 'n1', 'name', jsonb_build_object('fr', 'N', 'en', 'N'), 'review_status', st)))))) from generate_series(1, p_zones) i)); $$; -- A verdict group, and the three groups of the one-zone document (1, 2, 3). create function pg_temp.g(p_key text, p_digest text, p_coverable boolean default true) returns jsonb language sql as $$ select jsonb_build_object('key', p_key, 'digest', repeat(p_digest, 64), 'coverable', p_coverable); $$; create function pg_temp.groups3() returns jsonb language sql as $$ select jsonb_build_array(pg_temp.g('general', '1'), pg_temp.g('view:main', '2'), pg_temp.g('zone:z1', '3')); $$; -- The same three groups, every digest made of one hex character (one per -- content variant, so no acknowledgment carries between variants). create function pg_temp.groups_of(ch text) returns jsonb language sql as $$ select jsonb_build_array(pg_temp.g('general', ch), pg_temp.g('view:main', ch), pg_temp.g('zone:z1', ch)); $$; -- Writes a passing verdict and opens the round (the worker's stand-in). create function pg_temp.open_round(p_review uuid, p_asset uuid, p_sha text, p_groups jsonb) returns void language sql as $$ update public.anatomical_sheet_reviews set status = 'open', verified_at = now(), verified_with = 'test', verdict = jsonb_build_object('fingerprint', repeat('a', 64), 'drawings', jsonb_build_array(jsonb_build_object('asset_id', p_asset, 'sha256', p_sha)), 'groups', p_groups) where id = p_review; $$; -- An official sheet whose draft references a confirmed drawing, saved fully -- reviewed (revision 1); keys

,

.v,

.asset. As E. create function pg_temp.sheet(p text, p_sha text) returns void language plpgsql as $$ begin perform pg_temp.as_user('E'); insert into pg_temp.k select p, public.create_official_sheet('r17-' || p, pg_temp.doc(null, 'reviewed')); insert into pg_temp.k select p || '.v', id from public.anatomical_sheet_versions where sheet_id = pg_temp.id(p); insert into pg_temp.k select p || '.asset', public.register_sheet_asset(pg_temp.id(p), p_sha, 100, 'd.svg'); perform set_config('role', 'postgres', true); insert into storage.objects (bucket_id, name, metadata) values ('anatomical-sheet-assets', p_sha || '.svg', '{"size":100}') on conflict do nothing; perform pg_temp.as_user('E'); perform public.confirm_sheet_asset_upload(pg_temp.id(p || '.asset')); perform public.save_sheet_draft(pg_temp.id(p || '.v'), 0, pg_temp.doc(pg_temp.id(p || '.asset'), 'reviewed'), 'wire'); end $$; -- E submits the draft of

at ; the round opens with . create function pg_temp.round(p text, rk text, rev int, p_sha text, p_groups jsonb) returns void language plpgsql as $$ begin perform pg_temp.as_user('E'); insert into pg_temp.k select rk, public.submit_sheet_review(pg_temp.id(p || '.v'), rev, 'round'); perform set_config('role', 'postgres', true); perform pg_temp.open_round(pg_temp.id(rk), pg_temp.id(p || '.asset'), p_sha, p_groups); perform pg_temp.as_user('E'); end $$; -- V acknowledges every group of a round. create function pg_temp.ack_all(rk text) returns void language plpgsql as $$ declare g jsonb; begin perform pg_temp.as_user('V'); for g in select e from public.anatomical_sheet_reviews r, jsonb_array_elements(r.verdict -> 'groups') e where r.id = pg_temp.id(rk) loop perform public.acknowledge_sheet_review_group(pg_temp.id(rk), g ->> 'key', g ->> 'digest'); end loop; end $$; -- saves on

, E submits, the round opens with digests -- and the fixture approves it as with every group acknowledged by -- (none when null). Ends as E. create function pg_temp.fixture_round(p text, rk text, ch text, p_doc jsonb, p_saver text, p_decider text, p_acker text) returns void language plpgsql as $$ declare v_rev int; g jsonb; begin perform pg_temp.as_user(p_saver); select revision into v_rev from public.anatomical_sheet_versions where id = pg_temp.id(p || '.v'); perform public.save_sheet_draft(pg_temp.id(p || '.v'), v_rev, p_doc, 'variant'); perform pg_temp.round(p, rk, v_rev + 1, repeat('b6', 32), pg_temp.groups_of(ch)); perform set_config('role', 'postgres', true); update public.anatomical_sheet_reviews set status = 'approved', decided_by = pg_temp.u(p_decider), decided_at = now(), decision_note = 'fixture' where id = pg_temp.id(rk); if p_acker is not null then for g in select e from public.anatomical_sheet_reviews r, jsonb_array_elements(r.verdict -> 'groups') e where r.id = pg_temp.id(rk) loop insert into public.anatomical_sheet_review_acknowledgments (review_id, group_key, group_digest, reviewer) values (pg_temp.id(rk), g ->> 'key', g ->> 'digest', pg_temp.u(p_acker)); end loop; end if; perform pg_temp.as_user('E'); end $$; -- A document whose JSON text (`::text`, the contract's measure, §12.2) is -- exactly p_bytes UTF-8 bytes: the base plus a `pad` string of p_unit -- characters, completed with ASCII. create function pg_temp.sized(p_base jsonb, p_bytes int, p_unit text) returns jsonb language sql as $$ select p_base || jsonb_build_object('pad', repeat(p_unit, f / octet_length(p_unit)) || repeat('x', f % octet_length(p_unit))) from (select p_bytes - octet_length((p_base || '{"pad": ""}'::jsonb)::text) as f) s; $$; -- Saves on the draft

at its current revision. create function pg_temp.save_now(p text, p_doc jsonb) returns void language sql as $$ select public.save_sheet_draft(pg_temp.id(p || '.v'), (select revision from public.anatomical_sheet_versions where id = pg_temp.id(p || '.v')), p_doc, 'size'); $$; grant all on pg_temp.k to authenticated; grant execute on all functions in schema pg_temp to authenticated; -- ============ M1: approval needs an active sheet (§6.7) ============ select pg_temp.sheet('m1', repeat('a1', 32)); select pg_temp.round('m1', 'm1.r', 1, repeat('a1', 32), pg_temp.groups3()); select pg_temp.ack_all('m1.r'); select pg_temp.as_user('E'); select public.archive_anatomical_sheet(pg_temp.id('m1')); select pg_temp.as_user('V'); select throws_ok($$ select public.decide_sheet_review(pg_temp.id('m1.r'), true, 'ok') $$, '55000', 'sheet.inactive', 'M1: an approval on an archived sheet is refused (§6.7)'); select is((select status::text from public.anatomical_sheet_reviews where id = pg_temp.id('m1.r')), 'open', 'M1: the refused approval leaves the round open'); select pg_temp.as_user('E'); select public.restore_anatomical_sheet(pg_temp.id('m1')); select pg_temp.as_user('V'); select lives_ok($$ select public.decide_sheet_review(pg_temp.id('m1.r'), true, 'ok') $$, 'M1: once the sheet is restored the same approval succeeds'); -- ============ M2: a base only from a round-published version (§6.2) ============ -- m2: v1 published through a round, then deprecated; a draft from it has no base. select pg_temp.sheet('m2', repeat('a2', 32)); select pg_temp.round('m2', 'm2.r1', 1, repeat('a2', 32), pg_temp.groups3()); select pg_temp.ack_all('m2.r1'); select public.decide_sheet_review(pg_temp.id('m2.r1'), true, 'ok'); select pg_temp.as_user('E'); select public.publish_sheet_version(pg_temp.id('m2.r1')); select public.deprecate_sheet_version(pg_temp.id('m2.v'), 'superseded'); insert into pg_temp.k select 'm2b.v', public.create_sheet_draft(pg_temp.id('m2'), pg_temp.id('m2.v'), 'from deprecated'); reset role; select is((select source_version_id from public.anatomical_sheet_versions where id = pg_temp.id('m2b.v')), null, 'M2: a draft from a deprecated version has no base'); -- Same digests as the deprecated version's round: still every group to cover. insert into pg_temp.k values ('m2b', pg_temp.id('m2')), ('m2b.asset', pg_temp.id('m2.asset')); select pg_temp.round('m2b', 'm2b.r', 0, repeat('a2', 32), pg_temp.groups3()); select pg_temp.as_user('V'); select throws_ok($$ select public.decide_sheet_review(pg_temp.id('m2b.r'), true, 'ok') $$, 'P0001', 'review.groups.uncovered', 'M2: groups unchanged against a deprecated version still need an acknowledgment'); select pg_temp.ack_all('m2b.r'); select lives_ok($$ select public.decide_sheet_review(pg_temp.id('m2b.r'), true, 'ok') $$, 'M2: once every group is acknowledged the approval succeeds'); -- m3: the positive base. v1 published through a round and still published: a -- draft from it takes it as base, and its unchanged groups need no new ack. select pg_temp.sheet('m3', repeat('a3', 32)); select pg_temp.round('m3', 'm3.r1', 1, repeat('a3', 32), pg_temp.groups3()); select pg_temp.ack_all('m3.r1'); select public.decide_sheet_review(pg_temp.id('m3.r1'), true, 'ok'); select pg_temp.as_user('E'); select public.publish_sheet_version(pg_temp.id('m3.r1')); insert into pg_temp.k select 'm3b.v', public.create_sheet_draft(pg_temp.id('m3'), null, 'next edition'); reset role; select is((select source_version_id from public.anatomical_sheet_versions where id = pg_temp.id('m3b.v')), pg_temp.id('m3.v'), 'M2: a draft from a version published through a round takes it as base'); insert into pg_temp.k values ('m3b', pg_temp.id('m3')), ('m3b.asset', pg_temp.id('m3.asset')); select pg_temp.round('m3b', 'm3b.r', 0, repeat('a3', 32), pg_temp.groups3()); select pg_temp.as_user('V'); select lives_ok($$ select public.decide_sheet_review(pg_temp.id('m3b.r'), true, 'ok') $$, 'M2: groups equal to the base are covered by the base''s approved round'); -- m4: a version published before M5 (no published_review_id) gives no base. reset role; select set_config('request.jwt.claims', '{"sub":"17000000-0000-0000-0000-00000000000e"}', true); insert into public.anatomical_sheets (id, species_id, name, slug, is_official, visibility, status) values ('17200000-0000-0000-0000-000000000004', 'equine', 'Pre', 'r17-pre-m5', true, 'private', 'active'); insert into public.anatomical_sheet_versions (id, sheet_id, version_number, status, document_schema_version, content, published_at, document_fingerprint) values ('17300000-0000-0000-0000-000000000004', '17200000-0000-0000-0000-000000000004', 1, 'published', 2, pg_temp.doc(null, 'reviewed'), now(), repeat('0', 64)); update public.anatomical_sheets set current_version_id = '17300000-0000-0000-0000-000000000004', last_version_number = 1 where id = '17200000-0000-0000-0000-000000000004'; select pg_temp.as_user('E'); insert into pg_temp.k select 'm4b.v', public.create_sheet_draft('17200000-0000-0000-0000-000000000004', null, 'from pre-M5'); reset role; select is((select source_version_id from public.anatomical_sheet_versions where id = pg_temp.id('m4b.v')), null, 'M2: a version published before M5 (no review) gives no base'); -- ============ Submission and approval rules one at a time (§6.7, §15) ============ select pg_temp.sheet('s5', repeat('b5', 32)); select throws_ok($$ select public.submit_sheet_review(pg_temp.id('s5.v'), 0, 'stale') $$, 'P0001', 'draft_conflict', 'submit with a stale revision is a conflict'); select public.save_sheet_draft(pg_temp.id('s5.v'), 1, pg_temp.doc('ffffffff-ffff-ffff-ffff-ffffffffffff', 'reviewed'), 'unknown drawing'); select throws_ok($$ select public.submit_sheet_review(pg_temp.id('s5.v'), 2, 'x') $$, 'P0001', 'view.asset.unknown', 'submit refuses a drawing id not registered for the sheet (§12.2)'); select public.save_sheet_draft(pg_temp.id('s5.v'), 2, pg_temp.doc(pg_temp.id('s5.asset'), 'reviewed'), 'back'); select pg_temp.round('s5', 's5.r1', 3, repeat('b5', 32), jsonb_build_array(pg_temp.g('general', '1'), pg_temp.g('view:main', '2'), pg_temp.g('zone:z1', '3', false))); select pg_temp.as_user('V'); select throws_ok($$ select public.acknowledge_sheet_review_group(pg_temp.id('s5.r1'), 'zone:z1', repeat('3', 64)) $$, 'P0001', 'review.uncoverable', 'a group the verdict marks as holding unread elements cannot be acknowledged'); -- Acknowledgments are append-only (§6.3): the second one keeps the first row. insert into pg_temp.k select 's5.ack', public.acknowledge_sheet_review_group(pg_temp.id('s5.r1'), 'general', repeat('1', 64)); select is(public.acknowledge_sheet_review_group(pg_temp.id('s5.r1'), 'general', repeat('1', 64)), pg_temp.id('s5.ack'), 'acknowledging a group twice returns the first acknowledgment'); reset role; select is((select count(*) from public.anatomical_sheet_review_acknowledgments where review_id = pg_temp.id('s5.r1') and group_key = 'general'), 1::bigint, 'one acknowledgment row per reviewer and group'); select is((select count(*) from audit.audit_logs where action = 'anatomical_sheet_review.acknowledged' and resource_id = pg_temp.id('s5.r1') and metadata ->> 'group_key' = 'general'), 1::bigint, 'the repeated acknowledgment writes no second audit row (append-only)'); -- An open round without a verdict, then a verdict of another revision. select pg_temp.as_user('E'); select public.withdraw_sheet_review(pg_temp.id('s5.r1'), 'redo'); insert into pg_temp.k select 's5.r2', public.submit_sheet_review(pg_temp.id('s5.v'), 3, 'r2'); reset role; update public.anatomical_sheet_reviews set status = 'open' where id = pg_temp.id('s5.r2'); select pg_temp.as_user('V'); select throws_ok($$ select public.decide_sheet_review(pg_temp.id('s5.r2'), true, 'ok') $$, 'P0001', 'verdict.missing', 'approval refused without a verdict'); reset role; -- The verdict names a drawing sha256 other than the row's (checked at publication). select pg_temp.open_round(pg_temp.id('s5.r2'), pg_temp.id('s5.asset'), repeat('9', 64), pg_temp.groups3()); update public.anatomical_sheet_versions set revision = revision + 1 where id = pg_temp.id('s5.v'); select pg_temp.ack_all('s5.r2'); select throws_ok($$ select public.decide_sheet_review(pg_temp.id('s5.r2'), true, 'ok') $$, 'P0001', 'verdict.stale', 'approval refused on a verdict of another revision'); reset role; update public.anatomical_sheet_versions set revision = revision - 1 where id = pg_temp.id('s5.v'); select pg_temp.as_user('E'); select throws_ok($$ select public.publish_sheet_version(pg_temp.id('s5.r2')) $$, '55000', 'publish.review.missing', 'publication refused while the round is not approved'); select pg_temp.as_user('V'); select public.decide_sheet_review(pg_temp.id('s5.r2'), true, 'ok'); select pg_temp.as_user('E'); select throws_ok($$ select public.publish_sheet_version(pg_temp.id('s5.r2')) $$, 'P0001', 'publish.drawing.changed', 'publication refused when a drawing''s sha256 differs from the verdict''s'); -- ============ Publication rules one at a time (§6.7) on fixture approvals ============ select pg_temp.sheet('s6', repeat('b6', 32)); select pg_temp.fixture_round('s6', 's6.a', '4', pg_temp.doc(pg_temp.id('s6.asset'), 'to_confirm'), 'E', 'V', 'V'); select throws_ok($$ select public.publish_sheet_version(pg_temp.id('s6.a')) $$, 'P0001', 'publish.to_confirm.open', 'publication refused while an element is to_confirm'); select public.withdraw_sheet_review(pg_temp.id('s6.a'), 'next'); select pg_temp.fixture_round('s6', 's6.b', '5', pg_temp.doc(pg_temp.id('s6.asset'), 'reviewed') #- '{annotations,0,review_status}', 'E', 'V', 'V'); select throws_ok($$ select public.publish_sheet_version(pg_temp.id('s6.b')) $$, 'P0001', 'publish.inherited.present', 'publication refused with an omitted (inherited) status'); select public.withdraw_sheet_review(pg_temp.id('s6.b'), 'next'); select pg_temp.fixture_round('s6', 's6.c', '6', jsonb_set(pg_temp.doc(pg_temp.id('s6.asset'), 'reviewed'), '{scope_disclaimer,fr}', '" "'), 'E', 'V', 'V'); select throws_ok($$ select public.publish_sheet_version(pg_temp.id('s6.c')) $$, 'P0001', 'publish.disclaimer.missing', 'publication refused with a blank scope disclaimer'); select public.withdraw_sheet_review(pg_temp.id('s6.c'), 'next'); select pg_temp.fixture_round('s6', 's6.d', '7', pg_temp.doc(pg_temp.id('s6.asset'), 'reviewed'), 'E', 'V', null); select throws_ok($$ select public.publish_sheet_version(pg_temp.id('s6.d')) $$, 'P0001', 'review.groups.uncovered', 'publication refused while a group is uncovered'); select public.withdraw_sheet_review(pg_temp.id('s6.d'), 'next'); -- V2 saves this variant, so V2 is an author of the round it then "approves". select pg_temp.fixture_round('s6', 's6.e', '8', jsonb_set(pg_temp.doc(pg_temp.id('s6.asset'), 'reviewed'), '{description,fr}', '"e"'), 'V2', 'V2', 'V'); select throws_ok($$ select public.publish_sheet_version(pg_temp.id('s6.e')) $$, 'P0001', 'publish.approval.invalid', 'publication refused when the approver is an author of the round'); select public.withdraw_sheet_review(pg_temp.id('s6.e'), 'next'); -- Groups acknowledged by V3, whose grant is then revoked: no longer covered. select pg_temp.fixture_round('s6', 's6.f', '9', jsonb_set(pg_temp.doc(pg_temp.id('s6.asset'), 'reviewed'), '{description,fr}', '"f"'), 'E', 'V', 'V3'); reset role; update public.anatomical_catalog_grants set revoked_at = now(), revoked_by = pg_temp.u('O'), revocation_reason = 'left' where user_id = pg_temp.u('V3') and revoked_at is null; select pg_temp.as_user('E'); select throws_ok($$ select public.publish_sheet_version(pg_temp.id('s6.f')) $$, 'P0001', 'review.groups.uncovered', 'acknowledgments of a reviewer whose grant was revoked no longer cover'); select public.withdraw_sheet_review(pg_temp.id('s6.f'), 'next'); select pg_temp.fixture_round('s6', 's6.g', 'a', jsonb_set(pg_temp.doc(pg_temp.id('s6.asset'), 'reviewed'), '{description,fr}', '"g"'), 'E', 'V', 'V'); select is(public.publish_sheet_version(pg_temp.id('s6.g')), pg_temp.id('s6.v'), 'with every rule met the same sheet publishes (each refusal above was alone)'); -- ============ No carry for an author (§6.3) ============ select pg_temp.sheet('s7', repeat('b7', 32)); select pg_temp.round('s7', 's7.r1', 1, repeat('b7', 32), pg_temp.groups3()); select pg_temp.as_user('V2'); select public.acknowledge_sheet_review_group(pg_temp.id('s7.r1'), 'general', repeat('1', 64)); select pg_temp.as_user('V'); select public.decide_sheet_review(pg_temp.id('s7.r1'), false, 'one more pass'); -- V2 edits the draft: an author of the next round. select pg_temp.as_user('V2'); select public.save_sheet_draft(pg_temp.id('s7.v'), 1, jsonb_set(pg_temp.doc(pg_temp.id('s7.asset'), 'reviewed'), '{description,en}', '"v2 edit"'), 'v2'); select pg_temp.round('s7', 's7.r2', 2, repeat('b7', 32), pg_temp.groups3()); select pg_temp.as_user('V'); select public.acknowledge_sheet_review_group(pg_temp.id('s7.r2'), 'view:main', repeat('2', 64)); select public.acknowledge_sheet_review_group(pg_temp.id('s7.r2'), 'zone:z1', repeat('3', 64)); select throws_ok($$ select public.decide_sheet_review(pg_temp.id('s7.r2'), true, 'ok') $$, 'P0001', 'review.groups.uncovered', 'an acknowledgment does not carry to a round its reviewer has since authored'); select public.acknowledge_sheet_review_group(pg_temp.id('s7.r2'), 'general', repeat('1', 64)); select lives_ok($$ select public.decide_sheet_review(pg_temp.id('s7.r2'), true, 'ok') $$, 'the group acknowledged again by a non-author is covered'); -- ============ Removal group (§6.3) and D8 reads after archive (§6.9) ============ select pg_temp.as_user('E'); insert into pg_temp.k select 's8', public.create_official_sheet('r17-s8', pg_temp.doc(null, 'reviewed', 2)); insert into pg_temp.k select 's8.v', id from public.anatomical_sheet_versions where sheet_id = pg_temp.id('s8'); insert into pg_temp.k select 's8.asset', public.register_sheet_asset(pg_temp.id('s8'), repeat('b8', 32), 100, 'd.svg'); reset role; insert into storage.objects (bucket_id, name, metadata) values ('anatomical-sheet-assets', repeat('b8', 32) || '.svg', '{"size":100}') on conflict do nothing; select pg_temp.as_user('E'); select public.confirm_sheet_asset_upload(pg_temp.id('s8.asset')); select public.save_sheet_draft(pg_temp.id('s8.v'), 0, pg_temp.doc(pg_temp.id('s8.asset'), 'reviewed', 2), 'two zones'); select pg_temp.round('s8', 's8.r1', 1, repeat('b8', 32), pg_temp.groups3() || jsonb_build_array(pg_temp.g('zone:z2', '4'))); select pg_temp.ack_all('s8.r1'); select public.decide_sheet_review(pg_temp.id('s8.r1'), true, 'ok'); select pg_temp.as_user('E'); select public.publish_sheet_version(pg_temp.id('s8.r1')); -- The round of a published version is its review link: never withdrawn (§6.1). select throws_ok($$ select public.withdraw_sheet_review(pg_temp.id('s8.r1'), 'late') $$, '55000', 'draft.not_a_draft', 'the round of a published version cannot be withdrawn'); select is((select status::text from public.anatomical_sheet_reviews where id = pg_temp.id('s8.r1')), 'approved', 'a published round stays approved'); -- The next edition drops zone z2: its removal group is to cover although every -- other group equals the base. insert into pg_temp.k select 's8b.v', public.create_sheet_draft(pg_temp.id('s8'), null, 'drop z2'); select public.save_sheet_draft(pg_temp.id('s8b.v'), 0, pg_temp.doc(pg_temp.id('s8.asset'), 'reviewed', 1), 'z2 removed'); insert into pg_temp.k values ('s8b', pg_temp.id('s8')), ('s8b.asset', pg_temp.id('s8.asset')); select pg_temp.round('s8b', 's8b.r', 1, repeat('b8', 32), pg_temp.groups3() || jsonb_build_array(pg_temp.g('zone:z2', 'e'))); select pg_temp.as_user('V'); select throws_ok($$ select public.decide_sheet_review(pg_temp.id('s8b.r'), true, 'ok') $$, 'P0001', 'review.groups.uncovered', 'a removal group is to cover even when every other group equals the base'); select public.acknowledge_sheet_review_group(pg_temp.id('s8b.r'), 'zone:z2', repeat('e', 64)); select lives_ok($$ select public.decide_sheet_review(pg_temp.id('s8b.r'), true, 'ok') $$, 'acknowledging the removal group completes the coverage'); select pg_temp.as_user('E'); select public.publish_sheet_version(pg_temp.id('s8b.r')); select public.deprecate_sheet_version(pg_temp.id('s8.v'), 'superseded'); select public.archive_anatomical_sheet(pg_temp.id('s8')); select pg_temp.as_user('R'); select is(public.get_sheet_version(pg_temp.id('s8.v')) ->> 'status', 'deprecated', 'D8: a deprecated version of an archived sheet stays readable by id'); select is(public.get_sheet_version(pg_temp.id('s8b.v')) ->> 'status', 'published', 'D8: a published version of an archived sheet stays readable by id'); select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets' and name = repeat('b8', 32) || '.svg'), 1::bigint, 'D8: the drawing of an archived sheet stays readable'); select is((select count(*) from jsonb_array_elements(public.list_official_catalog()) e where (e ->> 'sheet_id')::uuid = pg_temp.id('s8')), 0::bigint, 'an archived sheet leaves the catalogue list'); -- ============ F2: an archived sheet is frozen (§6.7, §12.1) ============ select pg_temp.sheet('s9', repeat('b9', 32)); select pg_temp.round('s9', 's9.r1', 1, repeat('b9', 32), pg_temp.groups3()); select pg_temp.ack_all('s9.r1'); select public.decide_sheet_review(pg_temp.id('s9.r1'), true, 'ok'); select pg_temp.as_user('E'); select public.archive_anatomical_sheet(pg_temp.id('s9')); select throws_ok($$ select public.publish_sheet_version(pg_temp.id('s9.r1')) $$, '55000', 'sheet.inactive', 'F2: the approved round of an archived sheet is not published'); select throws_ok($$ select public.save_sheet_draft(pg_temp.id('s9.v'), 1, jsonb_set(pg_temp.doc(pg_temp.id('s9.asset'), 'reviewed'), '{description,fr}', '"z"'), 'x') $$, '55000', 'sheet.inactive', 'F2: the draft of an archived sheet is not saved'); select public.withdraw_sheet_review(pg_temp.id('s9.r1'), 'archived'); select throws_ok($$ select public.submit_sheet_review(pg_temp.id('s9.v'), 1, 'x') $$, '55000', 'sheet.inactive', 'F2: the draft of an archived sheet is not submitted'); select throws_ok($$ select public.create_sheet_draft(pg_temp.id('s9'), null, 'x') $$, '55000', 'sheet.inactive', 'F2: no draft is opened on an archived sheet'); select throws_ok($$ select public.delete_sheet_version(pg_temp.id('s9.v')) $$, '55000', 'version.has_rounds', 'a draft that has rounds is never deleted (D6 trail)'); -- ============ Species (§7.2) ============ select pg_temp.as_user('E'); select public.create_species('r17sp', 'Espèce', 'Species', 90); select throws_ok($$ select public.create_species('r17sp', 'Espèce', 'Species', 90) $$, 'P0001', 'species.exists', 'an existing species id gets one answer'); select throws_ok($$ select public.create_official_sheet('r17-other', jsonb_set(pg_temp.doc(null, 'reviewed'), '{species_id}', '"other"')) $$, 'P0001', 'sheet.species_invalid', 'no official sheet for the species other'); select throws_ok($$ select public.create_official_sheet('r17-nosuch', jsonb_set(pg_temp.doc(null, 'reviewed'), '{species_id}', '"r17nosuch"')) $$, 'P0001', 'sheet.species_invalid', 'no official sheet for an unknown species'); select throws_ok($$ select public.rename_species('r17sp', 'A', 'A') $$, '42501', 'rights.owner', 'an editor cannot rename a species'); select pg_temp.as_user('V'); select throws_ok($$ select public.update_inactive_species('r17sp', 'A', 'A', 1) $$, '42501', 'rights.capability', 'a reviewer cannot edit an inactive species'); select pg_temp.as_user('E'); select lives_ok($$ select public.update_inactive_species('r17sp', 'Espèce B', 'Species B', 91) $$, 'an editor edits an inactive species'); select pg_temp.as_user('O'); select throws_ok($$ select public.rename_species('r17sp', 'A', 'A') $$, 'P0001', 'species.not_active', 'renaming applies to an active species only'); select public.activate_species('r17sp'); select lives_ok($$ select public.rename_species('r17sp', 'Espèce C', 'Species C') $$, 'the owner corrects an active species'' names'); select pg_temp.as_user('E'); select throws_ok($$ select public.update_inactive_species('r17sp', 'A', 'A', 1) $$, 'P0001', 'species.not_inactive', 'an editor no longer edits an active species'); -- ============ Working copy of the historical sheet (§14.3) ============ reset role; -- Any working copy already active on this stack is archived inside this -- transaction, so the slot rules are tested from a known state (LRN-093). update public.anatomical_sheets set status = 'archived', archived_at = now() where status = 'active' and private.is_historical_working_copy(id); insert into pg_temp.k select 'h.v', id from public.anatomical_sheet_versions where sheet_id = '503dc301-a8b1-50d4-9e44-0919b2fc2916'; insert into pg_temp.k select 'h.pending', '00000000-0000-0000-0000-000000000001'::uuid where exists (select 1 from public.anatomical_sheet_assets where sheet_id = '503dc301-a8b1-50d4-9e44-0919b2fc2916' and uploaded_at is null); select pg_temp.as_user('E'); select case when pg_temp.id('h.pending') is not null then throws_ok($$ select public.create_sheet_working_copy(pg_temp.id('h.v'), 'r17-wc0') $$, 'P0001', 'working_copy.assets_missing', 'a working copy is refused while a source drawing is not confirmed') else skip('the historical drawing is already confirmed on this stack', 1) end; reset role; insert into storage.objects (bucket_id, name, metadata) select 'anatomical-sheet-assets', a.sha256 || '.svg', jsonb_build_object('size', a.byte_size) from public.anatomical_sheet_assets a where a.sheet_id = '503dc301-a8b1-50d4-9e44-0919b2fc2916' on conflict do nothing; update public.anatomical_sheet_assets set uploaded_at = now() where sheet_id = '503dc301-a8b1-50d4-9e44-0919b2fc2916' and uploaded_at is null; create table pg_temp.h_version as select id, content, revision, authors, status, change_note, updated_at from public.anatomical_sheet_versions where sheet_id = '503dc301-a8b1-50d4-9e44-0919b2fc2916'; create table pg_temp.h_assets as select * from public.anatomical_sheet_assets where sheet_id = '503dc301-a8b1-50d4-9e44-0919b2fc2916'; create table pg_temp.h_sheet as select * from public.anatomical_sheets where id = '503dc301-a8b1-50d4-9e44-0919b2fc2916'; select pg_temp.as_user('E'); insert into pg_temp.k select 'wc1', public.create_sheet_working_copy(pg_temp.id('h.v'), 'r17-wc1'); reset role; select results_eq( $$ select id, content, revision, authors, status, change_note, updated_at from public.anatomical_sheet_versions where sheet_id = '503dc301-a8b1-50d4-9e44-0919b2fc2916' $$, $$ select * from pg_temp.h_version $$, 'the working copy leaves the source version unchanged'); select results_eq( $$ select * from public.anatomical_sheet_assets where sheet_id = '503dc301-a8b1-50d4-9e44-0919b2fc2916' order by id $$, $$ select * from pg_temp.h_assets order by id $$, 'the working copy leaves the source drawing rows unchanged'); select results_eq( $$ select * from public.anatomical_sheets where id = '503dc301-a8b1-50d4-9e44-0919b2fc2916' $$, $$ select * from pg_temp.h_sheet $$, 'the working copy leaves the historical sheet row unchanged'); select is( (select c.content from public.anatomical_sheet_versions c where c.sheet_id = pg_temp.id('wc1')), (select private.remap_document_assets(s.content, (select jsonb_object_agg(ha.id::text, ca.id::text) from public.anatomical_sheet_assets ha join public.anatomical_sheet_assets ca on ca.sha256 = ha.sha256 and ca.sheet_id = pg_temp.id('wc1') where ha.sheet_id = '503dc301-a8b1-50d4-9e44-0919b2fc2916')) from public.anatomical_sheet_versions s where s.id = pg_temp.id('h.v')), 'statuses verbatim: the copy is the source document with only its drawing ids remapped'); select pg_temp.as_user('E'); select throws_ok($$ select public.create_sheet_working_copy(pg_temp.id('h.v'), 'r17-wc2') $$, 'P0001', 'working_copy.exists', 'one active working copy of the historical sheet at a time'); select public.archive_anatomical_sheet(pg_temp.id('wc1')); select throws_ok($$ select public.create_sheet_working_copy(pg_temp.id('h.v'), 'r17-wc1') $$, 'P0001', 'sheet.slug_taken', 'an archived sheet keeps its slug'); insert into pg_temp.k select 'wc2', public.create_sheet_working_copy(pg_temp.id('h.v'), 'r17-wc2'); select ok(pg_temp.id('wc2') is not null, 'archiving a working copy frees the slot'); select throws_ok($$ select public.restore_anatomical_sheet(pg_temp.id('wc1')) $$, 'P0001', 'working_copy.exists', 'restoring a copy while another one is active is refused'); -- ============ Closed codes and the lifecycle (§6.2, §6.9, §9.3, §12.2) ============ select pg_temp.sheet('s13', repeat('bd', 32)); insert into pg_temp.k select 's13.extra', public.register_sheet_asset(pg_temp.id('s13'), repeat('be', 32), 100, 'e.svg'); select lives_ok($$ select public.delete_sheet_asset(pg_temp.id('s13.extra')) $$, 'an unreferenced drawing row is deleted'); select throws_ok($$ select public.delete_sheet_asset(pg_temp.id('s13.asset')) $$, '55000', 'asset.referenced', 'a drawing referenced by a version is kept'); select throws_ok($$ select public.delete_sheet_asset((select id from public.anatomical_sheet_assets where sheet_id = '503dc301-a8b1-50d4-9e44-0919b2fc2916' limit 1)) $$, '42501', 'sheet.historical_read_only', 'the historical sheet''s drawing row is never deleted'); select throws_ok($$ select public.delete_anatomical_sheet('503dc301-a8b1-50d4-9e44-0919b2fc2916') $$, '42501', 'sheet.historical_read_only', 'the historical sheet is never deleted through the RPC'); select throws_ok($$ select public.delete_sheet_version(pg_temp.id('h.v')) $$, '42501', 'sheet.historical_read_only', 'the historical draft is never deleted through the RPC'); select throws_ok($$ select public.restore_anatomical_sheet('503dc301-a8b1-50d4-9e44-0919b2fc2916') $$, '42501', 'sheet.historical_read_only', 'the historical sheet is never restored through the RPC'); select throws_ok($$ select public.create_sheet_draft('503dc301-a8b1-50d4-9e44-0919b2fc2916', null, 'x') $$, '42501', 'sheet.historical_read_only', 'no draft is opened on the historical sheet'); select throws_ok($$ select public.confirm_sheet_asset_upload((select id from public.anatomical_sheet_assets where sheet_id = '503dc301-a8b1-50d4-9e44-0919b2fc2916' limit 1)) $$, '42501', 'sheet.historical_read_only', 'an editor never confirms a drawing of the historical sheet'); select throws_ok($$ select public.create_official_sheet('r17-s13', pg_temp.doc(null, 'reviewed')) $$, 'P0001', 'sheet.slug_taken', 'a slug already used by an official sheet'); select throws_ok($$ select public.create_official_sheet('Bad Slug', pg_temp.doc(null, 'reviewed')) $$, 'P0001', 'invalid_input', 'a slug outside the format'); select throws_ok($$ select public.save_sheet_draft(pg_temp.id('s13.v'), 1, jsonb_set(pg_temp.doc(pg_temp.id('s13.asset'), 'reviewed'), '{species_id}', '"canine"'), 'x') $$, '23514', 'document.species', 'save refuses a document of another species'); select throws_ok($$ select public.save_sheet_draft(pg_temp.id('s13.v'), 1, jsonb_set(pg_temp.doc(pg_temp.id('s13.asset'), 'reviewed'), '{annotations}', '{}'), 'x') $$, '23514', 'document.shape', 'save refuses a collection that is not an array'); select throws_ok($$ select public.save_sheet_draft(pg_temp.id('s13.v'), 1, jsonb_set(pg_temp.doc(pg_temp.id('s13.asset'), 'reviewed'), '{schema_version}', '3'), 'x') $$, '23514', 'document.schema_version', 'save refuses another schema version'); select throws_ok($$ select public.save_sheet_draft(pg_temp.id('s13.v'), 1, jsonb_set(pg_temp.doc(pg_temp.id('s13.asset'), 'reviewed'), '{description,fr}', '"n"'), repeat('x', 1001)) $$, 'P0001', 'invalid_input', 'a change note over 1 000 characters is invalid input, not a raw constraint error'); insert into pg_temp.k select 'blank', public.create_official_sheet('r17-blank', jsonb_set(pg_temp.doc(null, 'reviewed'), '{name,fr}', '" "')); select is((select name from public.anatomical_sheets where id = pg_temp.id('blank')), 'R horse', 'a blank FR name falls back to the EN name at creation'); select throws_ok($$ select public.create_sheet_draft(pg_temp.id('m3'), null, 'x') $$, 'P0001', 'draft.exists', 'one draft per sheet'); select public.publish_sheet_version(pg_temp.id('m3b.r')); select throws_ok($$ select public.publish_sheet_version(pg_temp.id('m3b.r')) $$, '55000', 'draft.not_a_draft', 'a round is published once'); select public.deprecate_sheet_version(pg_temp.id('m3b.v'), 'rollback'); select is((select current_version_id from public.anatomical_sheets where id = pg_temp.id('m3')), pg_temp.id('m3.v'), 'deprecating the current version makes the latest remaining published one current'); select public.deprecate_sheet_version(pg_temp.id('m3.v'), 'gone'); select is((select current_version_id from public.anatomical_sheets where id = pg_temp.id('m3')), null, 'with no published version left the sheet has no current version'); select throws_ok($$ select public.delete_anatomical_sheet(pg_temp.id('m3')) $$, '55000', 'sheet.published_history', 'a sheet that was ever published is never deleted'); select public.delete_sheet_version(pg_temp.id('m4b.v')); insert into pg_temp.k select 'm4c.v', public.create_sheet_draft('17200000-0000-0000-0000-000000000004', null, 'again'); select is((select version_number from public.anatomical_sheet_versions where id = pg_temp.id('m4c.v')), 3, 'a deleted version''s number is never reused'); select public.delete_sheet_version(pg_temp.id('s13.v')); select lives_ok($$ select public.delete_anatomical_sheet(pg_temp.id('s13')) $$, 'a draft-only sheet is deleted'); select is((select count(*) from public.anatomical_sheets where id = pg_temp.id('s13')), 0::bigint, 'a deleted sheet is invisible, capability holders included'); -- ============ M6: an owner may review, never a round it authored (§5.2, §5.5) ============ select pg_temp.as_user('O'); select lives_ok($$ select public.grant_catalog_right(pg_temp.u('O'), 'sheet_reviewer', 'owner reviews', 'DVM, owner') $$, 'M6: the owner at aal2 grants itself sheet_reviewer'); select public.grant_catalog_right(pg_temp.u('O'), 'sheet_editor', 'owner edits', 'owner'); -- o1: written by E only; the owner acknowledges, approves, E publishes. select pg_temp.sheet('o1', repeat('c1', 32)); select pg_temp.round('o1', 'o1.r', 1, repeat('c1', 32), pg_temp.groups3()); select pg_temp.as_user('O'); select lives_ok($$ select public.acknowledge_sheet_review_group(pg_temp.id('o1.r'), g ->> 'key', g ->> 'digest') from jsonb_array_elements(pg_temp.groups3()) g $$, 'M6: the owner-reviewer acknowledges the groups of a draft written by another author'); select lives_ok($$ select public.decide_sheet_review(pg_temp.id('o1.r'), true, 'ok') $$, 'M6: the owner-reviewer approves a version written by another author'); select pg_temp.as_user('E'); select is(public.publish_sheet_version(pg_temp.id('o1.r')), pg_temp.id('o1.v'), 'M6: the owner''s approval publishes when every other rule is met'); -- o2: the owner saves a variant, so it is an author of the round. select pg_temp.sheet('o2', repeat('b6', 32)); select pg_temp.as_user('O'); select public.save_sheet_draft(pg_temp.id('o2.v'), 1, jsonb_set(pg_temp.doc(pg_temp.id('o2.asset'), 'reviewed'), '{description,fr}', '"o"'), 'owner edit'); select pg_temp.round('o2', 'o2.r', 2, repeat('b6', 32), pg_temp.groups3()); select pg_temp.as_user('O'); select throws_ok($$ select public.acknowledge_sheet_review_group(pg_temp.id('o2.r'), 'general', repeat('1', 64)) $$, '42501', 'review.separation', 'M6: the owner-reviewer cannot acknowledge a round it authored'); select throws_ok($$ select public.decide_sheet_review(pg_temp.id('o2.r'), true, 'ok') $$, '42501', 'review.separation', 'M6: the owner-reviewer cannot approve its own version'); -- An approval by an author (written as the table owner, the RPC refuses it) is -- refused again at publication. select pg_temp.as_user('E'); select public.withdraw_sheet_review(pg_temp.id('o2.r'), 'next'); select pg_temp.fixture_round('o2', 'o2.f', 'c', jsonb_set(pg_temp.doc(pg_temp.id('o2.asset'), 'reviewed'), '{description,fr}', '"o2"'), 'O', 'O', 'V'); select throws_ok($$ select public.publish_sheet_version(pg_temp.id('o2.f')) $$, 'P0001', 'publish.approval.invalid', 'M6: publication refused after an approval by an author (the owner)'); -- ============ Publication refused for the species `other` alone (§6.7) ============ -- No RPC creates such a sheet (`sheet.species_invalid`); a row moved to `other` -- by the table owner after an otherwise valid approval stands in for it. select pg_temp.sheet('so', repeat('b6', 32)); select pg_temp.fixture_round('so', 'so.f', 'd', jsonb_set(pg_temp.doc(pg_temp.id('so.asset'), 'reviewed'), '{description,fr}', '"so"'), 'E', 'V', 'V'); reset role; alter table public.anatomical_sheets disable trigger user; update public.anatomical_sheets set species_id = 'other' where id = pg_temp.id('so'); alter table public.anatomical_sheets enable trigger user; select pg_temp.as_user('E'); select throws_ok($$ select public.publish_sheet_version(pg_temp.id('so.f')) $$, 'P0001', 'publish.species.other', 'publication refused for the species other, every other rule met'); -- ============ Document size: UTF-8 bytes of the JSON text (§6.2, §12.2) ============ select pg_temp.sheet('sz', repeat('c3', 32)); select lives_ok($$ select pg_temp.save_now('sz', pg_temp.sized(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), 2097151, 'x')) $$, 'size: a document of cap − 1 bytes is saved'); reset role; select is((select octet_length(content::text) from public.anatomical_sheet_versions where id = pg_temp.id('sz.v')), 2097151, 'size: the stored document measures cap − 1 bytes'); select pg_temp.as_user('E'); select lives_ok($$ select pg_temp.save_now('sz', pg_temp.sized(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), 2097152, 'x')) $$, 'size: a document of exactly 2 MiB is saved'); reset role; select is((select octet_length(content::text) from public.anatomical_sheet_versions where id = pg_temp.id('sz.v')), 2097152, 'size: the stored document measures exactly the cap'); select pg_temp.as_user('E'); select throws_ok($$ select pg_temp.save_now('sz', pg_temp.sized(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), 2097153, 'x')) $$, '23514', 'document.too_large', 'size: a document of cap + 1 bytes gets the closed code'); -- Multi-byte text: the cap counts bytes, not characters. select lives_ok($$ select pg_temp.save_now('sz', pg_temp.sized(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), 2097152, '€')) $$, 'size: a document of exactly 2 MiB written in 3-byte characters is saved'); reset role; select ok((select octet_length(content::text) = 2097152 and char_length(content::text) < 800000 from public.anatomical_sheet_versions where id = pg_temp.id('sz.v')), 'size: that document is 2 MiB in bytes and far fewer characters'); select ok(char_length(pg_temp.sized(pg_temp.doc(null, 'reviewed'), 2097153, '𝄞')::text) < 600000, 'size: the 4-byte document below is far under the cap in characters'); select pg_temp.as_user('E'); select throws_ok($$ select pg_temp.save_now('sz', pg_temp.sized(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), 2097153, '𝄞')) $$, '23514', 'document.too_large', 'size: one byte over the cap in 4-byte characters gets the closed code'); -- A dense document: within the cap as text, over it as binary jsonb (`|| '{}'` -- measures a fresh, uncompressed value, as the RPC receives it). reset role; create table pg_temp.sz_dense as select jsonb_set(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), '{pad}', (select jsonb_agg(jsonb_build_object('a', 1)) from generate_series(1, 100000))) as d; grant select on pg_temp.sz_dense to authenticated; select ok((select octet_length(d::text) <= 2097152 and pg_column_size(d || '{}'::jsonb) > 2097152 from pg_temp.sz_dense), 'size: the dense fixture fits the cap as text and exceeds it as binary jsonb'); select pg_temp.as_user('E'); select lives_ok($$ select pg_temp.save_now('sz', (select d from pg_temp.sz_dense)) $$, 'size: a dense document within the cap as text is saved (the binary jsonb size is not the measure)'); reset role; select ok((select octet_length(content::text) <= 2097152 and jsonb_array_length(content -> 'pad') = 100000 from public.anatomical_sheet_versions where id = pg_temp.id('sz.v')), 'size: the dense document is stored whole'); select pg_temp.as_user('E'); select throws_ok($$ select public.create_official_sheet('r17-szbig', pg_temp.sized(pg_temp.doc(null, 'reviewed'), 2097153, 'x')) $$, '23514', 'document.too_large', 'size: creation refuses a document of cap + 1 bytes with the closed code'); -- Numbers count as PostgreSQL prints them: plain digits, never an exponent. select lives_ok($$ select pg_temp.save_now('sz', jsonb_set(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), '{pad}', '[1e-7, 1e21]')) $$, 'size: numbers that JavaScript prints with an exponent are saved'); reset role; select is((select content -> 'pad' #>> '{}' from public.anatomical_sheet_versions where id = pg_temp.id('sz.v')), '[0.0000001, 1000000000000000000000]', 'size: they are measured in their plain form'); create table pg_temp.sz_exp as select jsonb_set(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), '{pad}', (select jsonb_agg(1e21) from generate_series(1, 100000))) as d; grant select on pg_temp.sz_exp to authenticated; select ok((select octet_length(replace(replace(d::text, ', ', ','), ': ', ':')) - 17 * 100000 <= 2097152 and octet_length(d::text) > 2097152 from pg_temp.sz_exp), 'size: the exponent fixture is under the cap in compact JavaScript form and over it as measured'); select pg_temp.as_user('E'); select throws_ok($$ select pg_temp.save_now('sz', (select d from pg_temp.sz_exp)) $$, '23514', 'document.too_large', 'size: a document over the cap only once its numbers print in full is refused'); -- 9 000 numbers of 131 072 digits each: a 90 kB payload whose text would be -- 1.2 GB. The closed code comes back, not an out-of-memory error. select throws_ok($$ select pg_temp.save_now('sz', jsonb_set(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), '{pad}', (select jsonb_agg(1e131071) from generate_series(1, 9000)))) $$, '23514', 'document.too_large', 'size: numbers that print as gigabytes of digits get the closed code, not an out-of-memory error'); -- 600 000 small numbers: within the cap as text (1.8 MB); the number walk is -- linear, so the save completes (a quadratic walk would run for minutes here). select lives_ok($$ select pg_temp.save_now('sz', jsonb_set(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), '{pad}', (select jsonb_agg(0) from generate_series(1, 600000)))) $$, 'size: a document of 600 000 small numbers within the cap is saved'); reset role; select is((select pg_get_constraintdef(oid) from pg_constraint where conname = 'anatomical_sheet_versions_content_size'), 'CHECK ((octet_length((content)::text) <= 2097152))', 'size: the table CHECK uses the same measure as the RPCs'); -- ============ Audit (§13): every lifecycle step of a sheet is recorded ============ reset role; select results_eq( $$ select action from audit.audit_logs where resource_id in (pg_temp.id('m1'), pg_temp.id('m1.v'), pg_temp.id('m1.asset'), pg_temp.id('m1.r'), pg_temp.id('m2'), pg_temp.id('m2.v'), pg_temp.id('m2b.v'), pg_temp.id('m2.asset'), pg_temp.id('m2.r1'), pg_temp.id('m2b.r')) group by action order by action collate "C" $$, $$ values ('anatomical_sheet.archived'), ('anatomical_sheet.created'), ('anatomical_sheet.restored'), ('anatomical_sheet_asset.registered'), ('anatomical_sheet_asset.uploaded'), ('anatomical_sheet_review.acknowledged'), ('anatomical_sheet_review.approved'), ('anatomical_sheet_review.submitted'), ('anatomical_sheet_version.created'), ('anatomical_sheet_version.deprecated'), ('anatomical_sheet_version.published'), ('anatomical_sheet_version.saved') $$, 'every lifecycle step left its audit event'); select * from finish(); rollback;