-- Run against the configured project; all fixture changes roll back. begin; do $$ declare original public.datasets; fixture uuid; fixture_child uuid; job uuid; child_job uuid; owner uuid; moderator uuid; before_path text; before_revision uuid; begin select * into original from public.datasets where state='published' and parent_dataset_id is null limit 1; if not found then raise exception 'A published fixture source is required'; end if; owner:=original.owner_id; select id into moderator from public.profiles where role='moderator' limit 1; if moderator is null then raise exception 'A moderator fixture is required'; end if; insert into public.datasets(owner_id,server_id,name,source,bounds,state,map_path,panorama_count,update_state) values(owner,original.server_id,'Refresh regression',original.source,original.bounds,'published',original.map_path,original.panorama_count,'draft') returning id into fixture; insert into public.dataset_inputs(dataset_id,manifest,validated) select fixture,manifest,true from public.dataset_inputs where dataset_id=original.id; begin perform public.refresh_map(fixture,gen_random_uuid(),original.id); raise exception 'Unauthorized refresh succeeded'; exception when others then if sqlerrm<>'Map owner permission required' then raise; end if; end; perform public.refresh_map(fixture,owner,original.id); select id into job from public.upload_jobs where dataset_id=fixture; if not exists(select 1 from public.upload_jobs where id=job and kind='map' and state='queued' and source_dataset_id=original.id) then raise exception 'Queue missing'; end if; begin perform public.refresh_map(fixture,owner,original.id); raise exception 'Duplicate refresh succeeded'; exception when others then if sqlerrm<>'A render is already in progress' then raise; end if; end; begin perform public.submit_draft(fixture,owner); raise exception 'Submission raced with rendering'; exception when others then if sqlerrm<>'Wait for map rendering to finish before submitting' then raise; end if; end; update public.upload_jobs set state='running',lease_owner='regression',lease_until=now()+interval '2 minutes' where id=job; perform public.complete_job(job,'regression',0,''); if (select map_path from public.datasets where id=fixture) is distinct from original.map_path then raise exception 'Live map changed before approval'; end if; if (select manifest->>'mapPath' from public.dataset_inputs where dataset_id=fixture) <> fixture::text||'/maps/'||job::text||'.webp' then raise exception 'Draft map missing'; end if; perform public.submit_draft(fixture,owner); perform public.publish_dataset(fixture,moderator,'approve'); select revision_id,map_path into before_revision,before_path from public.datasets where id=fixture; if before_path<>fixture::text||'/maps/'||job::text||'.webp' then raise exception 'Approved map was not published'; end if; perform public.refresh_map(fixture,owner,original.id); select id into job from public.upload_jobs where dataset_id=fixture; update public.upload_jobs set state='running',lease_owner='regression',lease_until=now()+interval '2 minutes' where id=job; perform public.complete_job(job,'regression',0,''); if (select manifest->>'mapPath' from public.revisions where id=before_revision)<>before_path then raise exception 'Existing game revision changed'; end if; update public.upload_jobs set state='failed' where id=job; update public.datasets set bounds=jsonb_set(bounds,'{maxX}',to_jsonb((bounds->>'maxX')::int+1)) where id=fixture; begin perform public.refresh_map(fixture,owner,original.id); raise exception 'Mismatched bounds accepted'; exception when others then if sqlerrm<>'Capture boundaries must match the map exactly' then raise; end if; end; insert into public.datasets(owner_id,server_id,name,source,bounds,state,parent_dataset_id,contribution_status,contribution_count) values(owner,original.server_id,'Refresh guard contribution',original.source,original.bounds,'draft',fixture,'pending',1) returning id into fixture_child; begin perform public.refresh_map(fixture_child,owner,original.id); raise exception 'Contribution refresh succeeded'; exception when others then if sqlerrm<>'Only a map can refresh its guessing map' then raise; end if; end; insert into public.upload_jobs(dataset_id,kind,source_dataset_id,state,lease_owner,lease_until) values(fixture_child,'map',original.id,'running','regression',now()+interval '2 minutes') returning id into child_job; begin perform public.complete_job(child_job,'regression',0,''); raise exception 'Contribution map render completed'; exception when others then if sqlerrm<>'Contributions cannot refresh a guessing map' then raise; end if; end; end; $$; rollback;