Files
faunosteo/supabase/tests/021_sheet_verification_jobs.sql
T
bchanot 3c141c942b fix(supabase): targeted review LOWs on retry and sandbox isolation
fail_sheet_verification refuses lease_expired (the claim's record, never a
worker report); the sandbox state dir guard resolves the repository path; the
avatar suite also refuses keys not issued by the sandbox. pgTAP 021: 90.
2026-11-01 23:13:42 +01:00

312 lines
26 KiB
PL/PgSQL

-- V1-2.3 M6: the worker RPCs of the server verdict (contract §6.4, §6.5).
-- Service role only; claim with a lease, input bound to the live token,
-- completion checked for its binding and structure (stale / malformed write
-- nothing), technical failures up to the attempt limit with their progressive
-- delay (`not_before`), dead leases.
begin;
select plan(90);
insert into auth.users (id, email, email_confirmed_at) values
('f0000000-0000-0000-0000-000000000001', 'own@vj.local', now()),
('f0000000-0000-0000-0000-00000000000e', 'ed@vj.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 '%@vj.local';
insert into auth.sessions (id, user_id, aal) values
('f1000000-0000-0000-0000-000000000001', 'f0000000-0000-0000-0000-000000000001', 'aal2'),
('f1000000-0000-0000-0000-00000000000e', 'f0000000-0000-0000-0000-00000000000e', 'aal2');
select set_config('request.jwt.claims', '', true);
select private.assign_platform_role(null, 'f0000000-0000-0000-0000-000000000001', 'owner');
select private.assign_platform_role(null, 'f0000000-0000-0000-0000-00000000000e', 'admin');
insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, verification_note, granted_by) values
('f0000000-0000-0000-0000-00000000000e','sheet_editor','a','c','f0000000-0000-0000-0000-000000000001');
create table pg_temp.k(key text primary key, id uuid);
grant all on pg_temp.k to authenticated, service_role;
create function pg_temp.id(p text) returns uuid language sql security definer as $$ select id from pg_temp.k where key = p $$;
grant execute on function pg_temp.id(text) to authenticated, service_role;
create function pg_temp.act(p uuid, s uuid) returns void language sql as $$
select set_config('request.jwt.claims', jsonb_build_object('sub',p,'role','authenticated','aal','aal2','session_id',s)::text, true);
$$;
grant execute on function pg_temp.act(uuid,uuid) to authenticated;
create function pg_temp.worker() returns void language sql as $$
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
$$;
grant execute on function pg_temp.worker() to service_role;
create function pg_temp.doc(p_asset uuid, p_name text) returns jsonb language sql as $$
select jsonb_build_object('schema_version',2,'species_id','equine',
'name',jsonb_build_object('fr',p_name,'en',p_name),
'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,
'finding_zones',jsonb_build_array(jsonb_build_object('id','z1','name',jsonb_build_object('fr','Z','en','Z'),'categories','[]'::jsonb)));
$$;
grant execute on function pg_temp.doc(uuid,text) to authenticated;
-- Test helpers read the round as the test owner (security definer): the
-- service role has no grant on the table.
-- The core's verdict for a round: passed with the document's groups and
-- drawing, or failed with a code (no group, no drawing).
create function pg_temp.verdict(p_review uuid, p_outcome text, p_code text default null) returns jsonb language sql security definer as $$
select jsonb_build_object('format',1,'outcome',p_outcome,
'failure_code',p_code,
'failures',case when p_code is null then '[]'::jsonb else jsonb_build_array(p_code) end,
'sheet_id',v.sheet_id,'version_id',v.id,'revision',r.revision,'review_id',r.id,
'verified_with','faunosteo-core test',
'fingerprint',case when p_outcome = 'passed' then repeat('f',64) end,
'drawings',case when p_outcome = 'passed' then jsonb_build_array(jsonb_build_object('asset_id',pg_temp.id('asset'),'sha256',repeat('8',64),'byte_size',900,'result','ok','detail',null)) else '[]'::jsonb end,
'document_bytes',100,'issues','[]'::jsonb,'caps',null,'completeness',null,
'groups',case when p_outcome = 'passed' then jsonb_build_array(
jsonb_build_object('key','general','digest',repeat('1',64),'coverable',true,'removed',false),
jsonb_build_object('key','view:main','digest',repeat('2',64),'coverable',true,'removed',false),
jsonb_build_object('key','zone:z1','digest',repeat('3',64),'coverable',true,'removed',false)) else '[]'::jsonb end,
'changes',null)
from public.anatomical_sheet_reviews r join public.anatomical_sheet_versions v on v.id = r.version_id
where r.id = p_review;
$$;
grant execute on function pg_temp.verdict(uuid,text,text) to service_role;
create function pg_temp.status(p text) returns text language sql security definer as $$
select status::text || '/' || coalesce(failure_outcome::text, '-') || '/' || coalesce(failure_code, '-')
from public.anatomical_sheet_reviews where id = pg_temp.id(p);
$$;
grant execute on function pg_temp.status(text) to service_role;
create function pg_temp.token(p text) returns uuid language sql security definer as $$
select lease_token from public.anatomical_sheet_reviews where id = pg_temp.id(p);
$$;
grant execute on function pg_temp.token(text) to service_role;
-- `not_before` is hidden from the API roles: the helpers read and move it as
-- the test owner. now() is the transaction's, so the delay compares exactly.
create function pg_temp.delayed(p text, p_delay interval) returns boolean language sql security definer as $$
select not_before = now() + p_delay from public.anatomical_sheet_reviews where id = pg_temp.id(p);
$$;
grant execute on function pg_temp.delayed(text, interval) to service_role;
create function pg_temp.not_before_is_null(p text) returns boolean language sql security definer as $$
select not_before is null from public.anatomical_sheet_reviews where id = pg_temp.id(p);
$$;
grant execute on function pg_temp.not_before_is_null(text) to service_role;
create function pg_temp.set_not_before(p text, p_offset interval) returns void language sql security definer as $$
update public.anatomical_sheet_reviews set not_before = now() + p_offset where id = pg_temp.id(p);
$$;
grant execute on function pg_temp.set_not_before(text, interval) to service_role;
-- A sheet with one confirmed drawing; three drafts submitted (r1, r2, r3).
set local role authenticated;
select pg_temp.act('f0000000-0000-0000-0000-00000000000e','f1000000-0000-0000-0000-00000000000e');
insert into pg_temp.k select 'sheet', public.create_official_sheet('vj-sheet', pg_temp.doc(null,'VJ'));
insert into pg_temp.k select 'v1', id from public.anatomical_sheet_versions where sheet_id = pg_temp.id('sheet');
insert into pg_temp.k select 'asset', public.register_sheet_asset(pg_temp.id('sheet'), repeat('8',64), 900, 'd.svg');
reset role;
insert into storage.objects (bucket_id,name,metadata) values ('anatomical-sheet-assets', repeat('8',64)||'.svg', '{"size":900}');
set local role authenticated;
select pg_temp.act('f0000000-0000-0000-0000-00000000000e','f1000000-0000-0000-0000-00000000000e');
select public.confirm_sheet_asset_upload(pg_temp.id('asset'));
select public.save_sheet_draft(pg_temp.id('v1'), 0, pg_temp.doc(pg_temp.id('asset'),'VJ'), 'wire');
insert into pg_temp.k select 'r1', public.submit_sheet_review(pg_temp.id('v1'), 1, 'one');
-- 1. Privileges: the service role only.
reset role;
select ok(not has_function_privilege('anon', 'public.claim_sheet_verifications(integer)', 'execute'), 'anon cannot claim');
select ok(not has_function_privilege('authenticated', 'public.claim_sheet_verifications(integer)', 'execute'), 'authenticated cannot claim');
select ok(not has_function_privilege('authenticated', 'public.get_sheet_verification_input(uuid, uuid)', 'execute'), 'authenticated cannot read the input');
select ok(not has_function_privilege('authenticated', 'public.complete_sheet_verification(uuid, uuid, jsonb)', 'execute'), 'authenticated cannot complete');
select ok(not has_function_privilege('authenticated', 'public.fail_sheet_verification(uuid, uuid, text)', 'execute'), 'authenticated cannot fail');
select ok(not has_function_privilege('anon', 'public.complete_sheet_verification(uuid, uuid, jsonb)', 'execute'), 'anon cannot complete');
select ok(has_function_privilege('service_role', 'public.claim_sheet_verifications(integer)', 'execute')
and has_function_privilege('service_role', 'public.get_sheet_verification_input(uuid, uuid)', 'execute')
and has_function_privilege('service_role', 'public.complete_sheet_verification(uuid, uuid, jsonb)', 'execute')
and has_function_privilege('service_role', 'public.fail_sheet_verification(uuid, uuid, text)', 'execute'),
'the service role executes the four RPCs');
select ok(not has_column_privilege('authenticated', 'public.anatomical_sheet_reviews', 'not_before', 'select')
and not has_column_privilege('anon', 'public.anatomical_sheet_reviews', 'not_before', 'select'),
'the API roles cannot read not_before');
select ok(not has_function_privilege('authenticated', 'private.sheet_verification_retry_delay(integer)', 'execute'), 'authenticated cannot execute the delay constant');
select is((select array_agg(extract(epoch from private.sheet_verification_retry_delay(n))::int order by n) from generate_series(1,5) n),
array[2,5,10,20,20], 'the delay after attempts 1 to 5: 2, 5, 10, 20, 20 seconds');
set local role authenticated;
select pg_temp.act('f0000000-0000-0000-0000-000000000001','f1000000-0000-0000-0000-000000000001');
select throws_ok($$ select public.claim_sheet_verifications(1) $$, '42501', null, 'the owner (authenticated) cannot claim');
-- The role check holds even when the grant is the service role's but the
-- claims are not (defence in depth).
reset role;
set local role service_role;
select set_config('request.jwt.claims', '{"role":"authenticated"}', true);
select throws_ok($$ select public.claim_sheet_verifications(1) $$, '42501', 'rights.capability', 'service role without its claims is refused');
select pg_temp.worker();
-- 2. Claim: lease set, attempt counted, a live lease not claimed twice.
select is((select count(*)::int from public.claim_sheet_verifications(5)), 1, 'one round claimed');
select ok(pg_temp.token('r1') is not null, 'the round carries a lease token');
reset role;
select is((select attempts from public.anatomical_sheet_reviews where id = pg_temp.id('r1')), 1, 'attempts counted at the claim');
set local role service_role; select pg_temp.worker();
select is((select count(*)::int from public.claim_sheet_verifications(5)), 0, 'a live lease is not claimed again');
-- 3. Input bound to the live token.
select is(public.get_sheet_verification_input(pg_temp.id('r1'), gen_random_uuid()), null, 'another token reads nothing');
select is(public.get_sheet_verification_input(pg_temp.id('r1'), pg_temp.token('r1')) ->> 'revision', '1', 'input names the round revision');
select is((public.get_sheet_verification_input(pg_temp.id('r1'), pg_temp.token('r1')) ->> 'document')::jsonb -> 'name' ->> 'fr', 'VJ', 'input carries the frozen document as text');
select is(public.get_sheet_verification_input(pg_temp.id('r1'), pg_temp.token('r1')) -> 'base_document', 'null'::jsonb, 'no base for a first version');
select is(public.get_sheet_verification_input(pg_temp.id('r1'), pg_temp.token('r1')) -> 'drawings' -> 0 ->> 'object_key', repeat('8',64) || '.svg', 'the drawing row names its object');
select is(jsonb_array_length(public.get_sheet_verification_input(pg_temp.id('r1'), pg_temp.token('r1')) -> 'drawings'), 1, 'one referenced drawing');
-- 4. Completion: stale and malformed write nothing.
select is(public.complete_sheet_verification(pg_temp.id('r1'), gen_random_uuid(), pg_temp.verdict(pg_temp.id('r1'),'passed')), 'stale', 'another token is stale');
select is(public.complete_sheet_verification(pg_temp.id('r1'), pg_temp.token('r1'),
jsonb_set(pg_temp.verdict(pg_temp.id('r1'),'passed'), '{revision}', '7')), 'stale', 'a verdict of another revision is stale');
select is(public.complete_sheet_verification(pg_temp.id('r1'), pg_temp.token('r1'),
jsonb_set(pg_temp.verdict(pg_temp.id('r1'),'passed'), '{review_id}', to_jsonb(gen_random_uuid()))), 'stale', 'a verdict of another round is stale');
select is(public.complete_sheet_verification(pg_temp.id('r1'), pg_temp.token('r1'),
jsonb_set(pg_temp.verdict(pg_temp.id('r1'),'passed'), '{format}', '2')), 'malformed', 'another verdict format is malformed');
select is(public.complete_sheet_verification(pg_temp.id('r1'), pg_temp.token('r1'),
jsonb_set(pg_temp.verdict(pg_temp.id('r1'),'passed'), '{groups}', (pg_temp.verdict(pg_temp.id('r1'),'passed') -> 'groups') - 2)), 'malformed', 'a passed verdict missing a group is malformed');
select is(public.complete_sheet_verification(pg_temp.id('r1'), pg_temp.token('r1'),
jsonb_set(pg_temp.verdict(pg_temp.id('r1'),'passed'), '{groups}', (pg_temp.verdict(pg_temp.id('r1'),'passed') -> 'groups') || jsonb_build_array(jsonb_build_object('key','general','digest',repeat('1',64),'coverable',true)))), 'malformed', 'a duplicated group key is malformed');
select is(public.complete_sheet_verification(pg_temp.id('r1'), pg_temp.token('r1'),
jsonb_set(pg_temp.verdict(pg_temp.id('r1'),'passed'), '{drawings,0,sha256}', to_jsonb(repeat('9',64)))), 'malformed', 'a drawing with another sha256 is malformed');
select is(public.complete_sheet_verification(pg_temp.id('r1'), pg_temp.token('r1'),
jsonb_set(pg_temp.verdict(pg_temp.id('r1'),'passed'), '{drawings}', '[]')), 'malformed', 'a passed verdict without its drawing is malformed');
select is(public.complete_sheet_verification(pg_temp.id('r1'), pg_temp.token('r1'),
jsonb_set(pg_temp.verdict(pg_temp.id('r1'),'passed'), '{fingerprint}', 'null')), 'malformed', 'a passed verdict without a fingerprint is malformed');
select is(public.complete_sheet_verification(pg_temp.id('r1'), pg_temp.token('r1'),
pg_temp.verdict(pg_temp.id('r1'),'failed','verification.invented')), 'malformed', 'a failed verdict with an unknown code is malformed');
select is(public.complete_sheet_verification(pg_temp.id('r1'), pg_temp.token('r1'), '[]'::jsonb), 'malformed', 'a non-object verdict is malformed');
select is(pg_temp.status('r1'), 'verifying/-/-', 'nothing written by stale or malformed completions');
-- 5. A passed verdict opens the round, once.
select is(public.complete_sheet_verification(pg_temp.id('r1'), pg_temp.token('r1'), pg_temp.verdict(pg_temp.id('r1'),'passed')), 'passed', 'a fitting passed verdict is written');
reset role;
select is((select status::text || '/' || (verdict ->> 'fingerprint') || '/' || (lease_token is null)::text
from public.anatomical_sheet_reviews where id = pg_temp.id('r1')), 'open/' || repeat('f',64) || '/true', 'round open with its verdict, lease cleared');
select ok(exists (select 1 from audit.audit_logs where action = 'anatomical_sheet_review.verification_passed' and resource_id = pg_temp.id('r1')), 'passed verification audited');
set local role service_role; select pg_temp.worker();
select is(public.complete_sheet_verification(pg_temp.id('r1'), gen_random_uuid(), pg_temp.verdict(pg_temp.id('r1'),'passed')), 'stale', 'a second completion is stale');
-- 6. A failed verdict without groups or drawings closes the round failed.
reset role;
set local role authenticated;
select pg_temp.act('f0000000-0000-0000-0000-00000000000e','f1000000-0000-0000-0000-00000000000e');
select public.withdraw_sheet_review(pg_temp.id('r1'), 'again');
select public.save_sheet_draft(pg_temp.id('v1'), 1, pg_temp.doc(pg_temp.id('asset'),'VJ2'), 'two');
insert into pg_temp.k select 'r2', public.submit_sheet_review(pg_temp.id('v1'), 2, 'two');
reset role;
set local role service_role; select pg_temp.worker();
select is((select count(*)::int from public.claim_sheet_verifications(1)), 1, 'second round claimed');
select is(public.complete_sheet_verification(pg_temp.id('r2'), pg_temp.token('r2'),
pg_temp.verdict(pg_temp.id('r2'),'failed','verification.integrity.document')), 'failed', 'a failed verdict without groups is written');
select is(pg_temp.status('r2'), 'verification_failed/failed/verification.integrity.document', 'round closed failed with its code');
select is((select count(*)::int from public.claim_sheet_verifications(10)), 0, 'a round completed with a failed verdict is never claimed again');
-- 7. Technical failures: retry below the limit, unavailable at it.
reset role;
set local role authenticated;
select pg_temp.act('f0000000-0000-0000-0000-00000000000e','f1000000-0000-0000-0000-00000000000e');
select public.save_sheet_draft(pg_temp.id('v1'), 2, pg_temp.doc(pg_temp.id('asset'),'VJ3'), 'three');
insert into pg_temp.k select 'r3', public.submit_sheet_review(pg_temp.id('v1'), 3, 'three');
reset role;
set local role service_role; select pg_temp.worker();
select is((select count(*)::int from public.claim_sheet_verifications(1)), 1, 'third round claimed');
select throws_ok(format('select public.fail_sheet_verification(%L, %L, %L)', pg_temp.id('r3'), pg_temp.token('r3'), 'verification.worker.oops'),
'P0001', 'invalid_input', 'an unknown worker code is refused');
select throws_ok(format('select public.fail_sheet_verification(%L, %L, %L)', pg_temp.id('r3'), pg_temp.token('r3'), 'verification.worker.lease_expired'),
'P0001', 'invalid_input', 'lease_expired is the claim''s record, never a worker report');
select is(public.fail_sheet_verification(pg_temp.id('r3'), gen_random_uuid(), 'verification.worker.timeout'), 'stale', 'a failure with another token is stale');
select is(public.fail_sheet_verification(pg_temp.id('r3'), pg_temp.token('r3'), 'verification.worker.timeout'), 'retry', 'a timeout below the limit is retried');
select is(pg_temp.status('r3') || '/' || (pg_temp.token('r3') is null)::text, 'verifying/-/verification.worker.timeout/true', 'round stays verifying, code recorded, lease released');
select ok(pg_temp.delayed('r3', interval '2 seconds'), 'after attempt 1 the retry waits 2 s');
reset role;
select ok(exists (select 1 from audit.audit_logs where action = 'anatomical_sheet_review.verification_attempt_failed' and resource_id = pg_temp.id('r3')
and metadata ->> 'retry_delay_seconds' = '2'), 'attempt failure audited with its delay');
set local role service_role; select pg_temp.worker();
select is(public.fail_sheet_verification(pg_temp.id('r3'), gen_random_uuid(), 'verification.worker.timeout'), 'stale', 'a stale failure again');
select ok(pg_temp.delayed('r3', interval '2 seconds'), 'a stale failure leaves the delay unchanged');
-- A round holding no lease is not an open door: a null token is stale everywhere.
select is(public.get_sheet_verification_input(pg_temp.id('r3'), null), null, 'a null token reads no input');
select is(public.fail_sheet_verification(pg_temp.id('r3'), null, 'verification.worker.timeout'), 'stale', 'a null token fails nothing');
select is(public.complete_sheet_verification(pg_temp.id('r3'), null, pg_temp.verdict(pg_temp.id('r3'),'failed')), 'stale', 'a null token completes nothing');
select ok(pg_temp.delayed('r3', interval '2 seconds'), 'a null token leaves the delay unchanged');
select is((select count(*)::int from public.claim_sheet_verifications(1)), 0, 'no claim before the delay is over');
-- The delay over exactly now: claimable.
select pg_temp.set_not_before('r3', interval '0');
select is((select attempts from public.claim_sheet_verifications(1)), 2, 'claimed once not_before is reached: second attempt');
select ok(pg_temp.not_before_is_null('r3'), 'the claim clears the delay');
select is(public.fail_sheet_verification(pg_temp.id('r3'), pg_temp.token('r3'), 'verification.worker.unavailable'), 'retry', 'a second failure is retried');
select ok(pg_temp.delayed('r3', interval '5 seconds'), 'after attempt 2 the retry waits 5 s');
select pg_temp.set_not_before('r3', interval '-1 second');
select is((select attempts from public.claim_sheet_verifications(1)), 3, 'claimed after 5 s: third attempt');
-- A dead lease is claimed again at once and recorded as an interruption.
reset role;
update public.anatomical_sheet_reviews set locked_at = now() - interval '11 minutes' where id = pg_temp.id('r3');
set local role service_role; select pg_temp.worker();
select is((select attempts from public.claim_sheet_verifications(1)), 4, 'a dead lease is claimed again');
select is(pg_temp.status('r3'), 'verifying/-/verification.worker.lease_expired', 'the interruption is recorded');
select is(public.fail_sheet_verification(pg_temp.id('r3'), pg_temp.token('r3'), 'verification.worker.timeout'), 'retry', 'a fourth failure is retried');
select ok(pg_temp.delayed('r3', interval '20 seconds'), 'after attempt 4 the retry waits 20 s');
select pg_temp.set_not_before('r3', interval '-1 second');
select is((select attempts from public.claim_sheet_verifications(1)), 5, 'claimed after 20 s: fifth attempt');
-- At the limit, a technical failure closes the round unavailable, delay cleared.
select pg_temp.set_not_before('r3', interval '1 hour');
select is(public.fail_sheet_verification(pg_temp.id('r3'), pg_temp.token('r3'), 'verification.worker.unavailable'), 'unavailable', 'the last attempt closes the round');
select is(pg_temp.status('r3'), 'verification_failed/unavailable/verification.worker.unavailable', 'round closed unavailable with the last code');
select ok(pg_temp.not_before_is_null('r3'), 'the fifth failure clears the delay');
reset role;
select ok(exists (select 1 from audit.audit_logs where action = 'anatomical_sheet_review.verification_attempt_failed' and resource_id = pg_temp.id('r3')), 'attempt failure audited');
select ok(exists (select 1 from audit.audit_logs where action = 'anatomical_sheet_review.verification_failed' and resource_id = pg_temp.id('r3')
and metadata ->> 'closed_by' = 'attempt_limit'), 'a transient code closes at the limit: closed_by attempt_limit');
-- 8. A dead lease at the limit is closed by the next claim.
set local role authenticated;
select pg_temp.act('f0000000-0000-0000-0000-00000000000e','f1000000-0000-0000-0000-00000000000e');
select public.save_sheet_draft(pg_temp.id('v1'), 3, pg_temp.doc(pg_temp.id('asset'),'VJ4'), 'four');
insert into pg_temp.k select 'r4', public.submit_sheet_review(pg_temp.id('v1'), 4, 'four');
reset role;
update public.anatomical_sheet_reviews set attempts = 5, lease_token = gen_random_uuid(), locked_at = now() - interval '11 minutes' where id = pg_temp.id('r4');
set local role service_role; select pg_temp.worker();
select is((select count(*)::int from public.claim_sheet_verifications(5)), 0, 'a round at the limit is not claimed');
select is(pg_temp.status('r4'), 'verification_failed/unavailable/verification.worker.lease_expired', 'its dead lease closes it unavailable');
-- 9. Definitive codes close the round at the first occurrence; transient
-- codes keep their retry. Rounds are sequential: each follows a closed one.
set local role authenticated;
select pg_temp.act('f0000000-0000-0000-0000-00000000000e','f1000000-0000-0000-0000-00000000000e');
select public.save_sheet_draft(pg_temp.id('v1'), 4, pg_temp.doc(pg_temp.id('asset'),'VJ5'), 'five');
insert into pg_temp.k select 'r5', public.submit_sheet_review(pg_temp.id('v1'), 5, 'five');
reset role;
set local role service_role; select pg_temp.worker();
select is((select count(*)::int from public.claim_sheet_verifications(1)), 1, 'fifth round claimed');
select is(public.fail_sheet_verification(pg_temp.id('r5'), pg_temp.token('r5'), 'verification.worker.resource_limit'), 'unavailable', 'resource_limit at attempt 1 closes the round');
select is(pg_temp.status('r5'), 'verification_failed/unavailable/verification.worker.resource_limit', 'closed unavailable, code kept');
select ok(pg_temp.not_before_is_null('r5'), 'a definitive closure sets no delay');
select is((select count(*)::int from public.claim_sheet_verifications(10)), 0, 'a definitively closed round is never claimed again');
reset role;
select ok(exists (select 1 from audit.audit_logs where action = 'anatomical_sheet_review.verification_failed' and resource_id = pg_temp.id('r5')
and metadata ->> 'closed_by' = 'definitive' and metadata ->> 'attempts' = '1'), 'closure audited as definitive at attempt 1');
set local role authenticated;
select pg_temp.act('f0000000-0000-0000-0000-00000000000e','f1000000-0000-0000-0000-00000000000e');
select public.save_sheet_draft(pg_temp.id('v1'), 5, pg_temp.doc(pg_temp.id('asset'),'VJ6'), 'six');
insert into pg_temp.k select 'r6', public.submit_sheet_review(pg_temp.id('v1'), 6, 'six');
reset role;
set local role service_role; select pg_temp.worker();
select is((select count(*)::int from public.claim_sheet_verifications(1)), 1, 'sixth round claimed');
select is(public.fail_sheet_verification(pg_temp.id('r6'), pg_temp.token('r6'), 'verification.worker.verdict_refused'), 'unavailable', 'verdict_refused at attempt 1 closes the round');
select is(pg_temp.status('r6'), 'verification_failed/unavailable/verification.worker.verdict_refused', 'closed unavailable, code kept');
select is((select count(*)::int from public.claim_sheet_verifications(10)), 0, 'a refused verdict is never claimed again');
set local role authenticated;
select pg_temp.act('f0000000-0000-0000-0000-00000000000e','f1000000-0000-0000-0000-00000000000e');
select public.save_sheet_draft(pg_temp.id('v1'), 6, pg_temp.doc(pg_temp.id('asset'),'VJ7'), 'seven');
insert into pg_temp.k select 'r7', public.submit_sheet_review(pg_temp.id('v1'), 7, 'seven');
reset role;
set local role service_role; select pg_temp.worker();
select is((select count(*)::int from public.claim_sheet_verifications(1)), 1, 'seventh round claimed');
select is(public.fail_sheet_verification(pg_temp.id('r7'), gen_random_uuid(), 'verification.worker.resource_limit'), 'stale', 'a definitive code with another token is stale');
select is(public.fail_sheet_verification(pg_temp.id('r7'), null, 'verification.worker.verdict_refused'), 'stale', 'a definitive code with a null token is stale');
select is(pg_temp.status('r7'), 'verifying/-/-', 'a stale definitive failure writes nothing');
select is(public.fail_sheet_verification(pg_temp.id('r7'), pg_temp.token('r7'), 'verification.worker.unavailable'), 'retry', 'unavailable at attempt 1 is retried');
select ok(pg_temp.delayed('r7', interval '2 seconds'), 'after attempt 1 the retry waits 2 s');
select * from finish();
rollback;