-- Official equine seed after V1-2.1: the historical sheet is present as a -- platform-owned DRAFT, protected on the server (never saved, submitted, -- published, archived or deleted), visible to capability holders at aal2 and -- to no one else (no published version → not in the catalogue). Contract §14.2. begin; select plan(17); create table pg_temp.ids (key text primary key, id uuid not null); create function pg_temp.id(p_key text) returns uuid language sql stable as $$ select id from pg_temp.ids where key = p_key $$; grant all on pg_temp.ids to anon, authenticated, service_role; grant execute on function pg_temp.id(text) to anon, authenticated, service_role; insert into pg_temp.ids select 'sheet', id from public.anatomical_sheets where slug = 'faunosteo-cheval' and is_official; insert into pg_temp.ids select 'draft', id from public.anatomical_sheet_versions where sheet_id = pg_temp.id('sheet'); -- 1. Seeded state select is(pg_temp.id('sheet'), '503dc301-a8b1-50d4-9e44-0919b2fc2916'::uuid, 'the historical sheet keeps its fixed id'); select results_eq( $$ select species_id, creator_user_id, is_official, visibility::text, status::text, current_version_id from public.anatomical_sheets where id = pg_temp.id('sheet') $$, $$ values ('equine', null::uuid, true, 'private', 'active', null::uuid) $$, 'historical sheet: platform-owned, private, active, no published version' ); select results_eq( $$ select version_number, status::text, document_schema_version, revision, coalesce(array_length(authors,1),0), published_at from public.anatomical_sheet_versions where id = pg_temp.id('draft') $$, $$ values (1, 'draft', 2, 0, 0, null::timestamptz) $$, 'one version: draft v1, schema 2, revision 0, empty author set, never published' ); select is((select count(*) from public.anatomical_sheet_assets where sheet_id = pg_temp.id('sheet')), 1::bigint, 'one SVG asset registered'); select is((select content ->> 'species_id' from public.anatomical_sheet_versions where id = pg_temp.id('draft')), 'equine', 'document species is equine'); select is((select jsonb_array_length(content -> 'annotations') from public.anatomical_sheet_versions where id = pg_temp.id('draft')), 28, '28 annotations'); select is((select jsonb_array_length(content -> 'finding_zones') from public.anatomical_sheet_versions where id = pg_temp.id('draft')), 27, '27 finding zones'); select ok( (select private.sheet_document_has_shape(content) from public.anatomical_sheet_versions where id = pg_temp.id('draft')), 'the seeded document has the structural shape SQL still checks' ); select is( (select private.sheet_document_unknown_assets(sheet_id, content) from public.anatomical_sheet_versions where id = pg_temp.id('draft')), '{}'::text[], 'every view asset is registered for the sheet' ); select is( (select count(*) from audit.audit_logs where resource_id = pg_temp.id('draft') and action = 'anatomical_sheet_version.created' and actor_user_id is null), 1::bigint, 'the seed import is audited as a system action' ); -- 2. An editor with the capability (aal2) is set up, then the protections. insert into auth.users (id, email, email_confirmed_at) values ('40000000-0000-0000-0000-000000000001', 'own-eq@test.local', now()), ('40000000-0000-0000-0000-00000000000e', 'ed-eq@test.local', now()), ('40000000-0000-0000-0000-0000000000a0', 'pl-eq@test.local', now()); insert into auth.mfa_factors (id, user_id, factor_type, status, created_at, updated_at) values (gen_random_uuid(), '40000000-0000-0000-0000-00000000000e', 'totp', 'verified', now(), now()); insert into auth.sessions (id, user_id, aal) values ('41000000-0000-0000-0000-00000000000e', '40000000-0000-0000-0000-00000000000e', 'aal2'); select set_config('request.jwt.claims', '', true); select private.assign_platform_role(null, '40000000-0000-0000-0000-000000000001', 'owner'); select private.assign_platform_role(null, '40000000-0000-0000-0000-00000000000e', 'admin'); insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, verification_note, granted_by) values ('40000000-0000-0000-0000-00000000000e', 'sheet_editor', 'equine work', 'checked', '40000000-0000-0000-0000-000000000001'), ('40000000-0000-0000-0000-00000000000e', 'sheet_publisher', 'equine work', 'checked', '40000000-0000-0000-0000-000000000001'); create function pg_temp.act(p_uid uuid, p_sid uuid, p_aal text) returns void language sql as $$ select set_config('request.jwt.claims', jsonb_build_object('sub',p_uid,'role','authenticated','aal',p_aal,'session_id',p_sid)::text, true); $$; grant execute on function pg_temp.act(uuid,uuid,text) to authenticated; set local role authenticated; select pg_temp.act('40000000-0000-0000-0000-00000000000e', '41000000-0000-0000-0000-00000000000e', 'aal2'); -- The capability holder sees the draft (caller_can_see_official_sheet). select is((select count(*) from public.anatomical_sheet_versions where id = pg_temp.id('draft')), 1::bigint, 'a capability holder at aal2 sees the historical draft'); -- Every write path refuses the historical sheet. select throws_ok( $$ select public.save_sheet_draft(pg_temp.id('draft'), 0, '{"schema_version":2,"species_id":"equine"}'::jsonb, 'x') $$, '42501', 'sheet.historical_read_only', 'save refused on the historical sheet' ); select throws_ok( $$ select public.submit_sheet_review(pg_temp.id('draft'), 0, 'x') $$, '42501', 'sheet.historical_read_only', 'submit refused on the historical sheet' ); select throws_ok( $$ select public.register_sheet_asset(pg_temp.id('sheet'), repeat('e',64), 100, 'x.svg') $$, '42501', 'sheet.historical_read_only', 'register refused on the historical sheet' ); select throws_ok( $$ select public.archive_anatomical_sheet(pg_temp.id('sheet')) $$, '42501', 'sheet.historical_read_only', 'archive refused on the historical sheet' ); select throws_ok( $$ delete from public.anatomical_sheet_versions where id = pg_temp.id('draft') $$, '42501', null, 'the historical draft cannot be deleted (trigger)' ); -- 3. A plain confirmed account sees nothing (no published version, no capability). select pg_temp.act('40000000-0000-0000-0000-0000000000a0', gen_random_uuid(), 'aal1'); select is((select count(*) from public.anatomical_sheets where id = pg_temp.id('sheet')), 0::bigint, 'a plain account does not see the historical sheet (no published version)'); select * from finish(); rollback;