-- Official equine sheet seed (supabase/seed/anatomical-sheets/equine/draft): -- present as a platform-owned DRAFT, never published by the seed, publishable -- by platform admins only, content accepted by the SQL referential gate. begin; select plan(20); insert into auth.users (id, email) values ('40000000-0000-0000-0000-000000000001', 'root-seed@test.local'), ('40000000-0000-0000-0000-00000000000a', 'alice-seed@test.local'); select private.assign_platform_role(null, '40000000-0000-0000-0000-000000000001', 'owner'); 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 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) $$, 'official equine sheet: platform-owned, private, active, no published version' ); select results_eq( $$ select version_number, status::text, document_schema_version, published_at from public.anatomical_sheet_versions where sheet_id = pg_temp.id('sheet') $$, $$ values (1, 'draft', 2, null::timestamptz) $$, 'exactly one version, draft v1, never published' ); select is((select count(*) from public.anatomical_sheet_assets where sheet_id = pg_temp.id('sheet')), 1::bigint, 'one SVG asset registered (the cleaned drawing)'); 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 -> 'views') from public.anatomical_sheet_versions where id = pg_temp.id('draft')), 2, 'document has two views' ); select ok( (select content -> 'views' -> 1 -> 'svg_asset_id' = 'null'::jsonb and jsonb_typeof(content -> 'views' -> 1 -> 'view_box') = 'object' from public.anatomical_sheet_versions where id = pg_temp.id('draft')), 'head view is a crop of the main drawing: no asset, a view_box' ); select is( (select jsonb_array_length(content -> 'annotations') from public.anatomical_sheet_versions where id = pg_temp.id('draft')), 28, 'document has 28 annotations (27 legacy + carpus)' ); select is( (select jsonb_array_length(content -> 'finding_zones') from public.anatomical_sheet_versions where id = pg_temp.id('draft')), 27, 'document has 27 finding zones (26 legacy + carpus)' ); select is( (select private.sheet_document_issues(content) from public.anatomical_sheet_versions where id = pg_temp.id('draft')), '{}'::text[], 'SQL referential gate finds no issue' ); 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. Nobody publishes it by accident: plain users cannot even see the draft set local role authenticated; select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true); select is((select count(*) from public.anatomical_sheets where id = pg_temp.id('sheet')), 0::bigint, 'a private official draft is invisible to plain users'); select throws_ok( $$ select public.publish_sheet_version(pg_temp.id('draft')) $$, '42501', null, 'a plain user cannot publish the official draft' ); -- A platform admin can (the explicit decision step), and the gate would accept the document select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-000000000001","role":"authenticated"}', true); select is((select count(*) from public.anatomical_sheet_versions where id = pg_temp.id('draft')), 1::bigint, 'a platform admin sees the official draft'); select lives_ok($$ select public.publish_sheet_version(pg_temp.id('draft')) $$, 'a platform admin could publish it (rolled back here)'); -- 3. Storage: the seed registers metadata only; after the operator upload + confirmation, -- a platform admin reads the object and a plain user none. A base where the operator -- already uploaded and confirmed the drawing (DEV) is brought back to the seed's state -- inside this transaction: the confirmation is cleared, and the object already in the -- bucket stands for the upload (content-addressed key, never written twice). Reads are -- counted on the seed's own key: other objects of the bucket do not count. reset role; create table pg_temp.seed_objects as select a.sha256 || '.svg' as name from public.anatomical_sheet_assets a where a.sheet_id = pg_temp.id('sheet'); grant select on pg_temp.seed_objects to authenticated, service_role; update public.anatomical_sheet_assets set uploaded_at = null where sheet_id = pg_temp.id('sheet'); insert into storage.objects (bucket_id, name) select 'anatomical-sheet-assets', k.name from pg_temp.seed_objects k where not exists (select 1 from storage.objects o where o.bucket_id = 'anatomical-sheet-assets' and o.name = k.name); select is( (select count(*) from storage.objects o join pg_temp.seed_objects k on k.name = o.name where o.bucket_id = 'anatomical-sheet-assets'), 1::bigint, 'the drawing is in the bucket once, under its content address' ); set local role authenticated; select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-000000000001","role":"authenticated"}', true); select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets' and name in (select name from pg_temp.seed_objects)), 0::bigint, 'seed objects are not readable before confirmation'); set local role service_role; select is( (select count(*) from public.anatomical_sheet_assets a where a.sheet_id = pg_temp.id('sheet') and public.confirm_sheet_asset_upload(a.id) = a.id), 1::bigint, 'operator confirms the upload' ); set local role authenticated; select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-000000000001","role":"authenticated"}', true); select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets' and name in (select name from pg_temp.seed_objects)), 1::bigint, 'a platform admin reads the official SVG'); select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true); select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets' and name in (select name from pg_temp.seed_objects)), 0::bigint, 'a plain user reads no official draft SVG'); select * from finish(); rollback;