\set ON_ERROR_STOP on begin; create function pg_temp.assert_grid(condition boolean, label text) returns void language plpgsql as $$ begin if condition is distinct from true then raise exception 'FAIL: %', label; end if; raise notice 'PASS: %', label; end; $$; create temp table fixture as select gen_random_uuid() event_id, gen_random_uuid() manual_id, gen_random_uuid() talk_id, gen_random_uuid() service_external, gen_random_uuid() placeholder_external, 'grid-staff-' || gen_random_uuid() || '@example.invalid' staff_email, -14000000-floor(random()*1000000)::integer service_slot, -16000000-floor(random()*1000000)::integer talk_slot; insert into public.events(id,name,start_date,end_date) select event_id,'Grid fixture',date '2026-10-23',date '2026-10-23' from fixture; insert into public.contacts(email,full_name) select staff_email,'Grid Staff' from fixture; insert into public.registrations(event_id,contact_email,type) select event_id,staff_email,'staff' from fixture; insert into public.slots(id,event_id,type,name,slot_date,start_time,end_time) select service_slot,event_id,'service_session'::public.slot_type,'Coffee',date '2026-10-23',time '08:30',time '09:00' from fixture union all select talk_slot,event_id,'session','Talk',date '2026-10-23',time '11:15',time '12:00' from fixture; insert into public.sessions(id,event_id,slot_id,title,external_id,description,level,area) select manual_id,event_id,service_slot,'Coffee','manual-service-' || manual_id::text,null,null,null from fixture union all select talk_id,event_id,talk_slot,'Unpublished talk',talk_id::text,'Keep description','Advanced','Integration' from fixture; insert into public.session_speakers(session_id,contact_email) select talk_id,staff_email from fixture; select set_config('request.jwt.claims','{"role":"service_role"}',true); create temp table payload as select jsonb_build_object('speakers','[]'::jsonb,'sessions',jsonb_build_array( jsonb_build_object('external_id',service_external::text,'title','Coffee','date','2026-10-23','start_time','08:30','end_time','09:00','is_service_session',true,'speakers','[]'::jsonb), jsonb_build_object('external_id',placeholder_external::text,'title','Placeholder','room','Classroom','date','2026-10-23','start_time','11:15','end_time','12:00','is_service_session',true,'speakers','[]'::jsonb) )) body from fixture; create temp table result as select public.sync_sessionize_event(event_id,body) summary from fixture,payload; select pg_temp.assert_grid(jsonb_array_length(summary->'errors')=0,'GridSmart sync succeeds') from result; select pg_temp.assert_grid((select external_id=fixture.service_external::text from public.sessions where id=fixture.manual_id),'manual service adopts real ID and keeps session ID') from fixture; select pg_temp.assert_grid((select sl.type='service_session' from public.sessions se join public.slots sl on sl.id=se.slot_id where se.external_id=fixture.placeholder_external::text),'placeholder uses a service slot at concurrent talk time') from fixture; select pg_temp.assert_grid((select count(*)=1 from public.session_speakers ss join public.sessions se on se.id=ss.session_id where se.external_id=fixture.placeholder_external::text),'placeholder receives all event staff') from fixture; select pg_temp.assert_grid((select description='Keep description' and level='Advanced' and area='Integration' from public.sessions where id=fixture.talk_id),'omitted talk and metadata retained') from fixture; select pg_temp.assert_grid(exists(select 1 from public.session_speakers where session_id=fixture.talk_id and contact_email=fixture.staff_email),'omitted talk speaker retained') from fixture; select public.sync_sessionize_event(event_id,body) from fixture,payload; select pg_temp.assert_grid((select count(*)=3 from public.sessions where event_id=fixture.event_id),'repeat polling does not duplicate sessions') from fixture; rollback;