\set ON_ERROR_STOP on begin; create function pg_temp.assert_sessionize_guard(p_condition boolean, p_name text) returns void language plpgsql as $$ begin if p_condition is distinct from true then raise exception 'FAIL: %', p_name; end if; raise notice 'PASS: %', p_name; end; $$; create function pg_temp.expect_sessionize_reject(p_statement text, p_name text, p_imported_date date default date '2026-02-20', p_start_date date default date '2026-10-23', p_end_date date default date '2026-10-24') returns void language plpgsql as $$ declare v_message text; begin begin execute p_statement; exception when others then v_message := sqlerrm; perform pg_temp.assert_sessionize_guard( position(p_imported_date::text in v_message) > 0 and position(p_start_date::text in v_message) > 0 and position(p_end_date::text in v_message) > 0 and v_message like '%Check Sessionize endpoint/Excel/event dates%', p_name || ' includes actual date, event range and actionable check'); return; end; raise exception 'FAIL: % was allowed', p_name; end; $$; create temporary table sessionize_guard_fixture as select gen_random_uuid() as event_id, gen_random_uuid() as existing_session_id, gen_random_uuid() as stale_session_id, (select least(coalesce(min(id), 0), 0) from public.slots) - 100 as slot_day_one_id, (select least(coalesce(min(id), 0), 0) from public.slots) - 101 as slot_day_two_id, (select least(coalesce(min(id), 0), 0) from public.slots) - 102 as slot_stale_id, 'guard-existing-' || gen_random_uuid() || '@example.invalid' as existing_email, 'guard-replacement-' || gen_random_uuid() || '@example.invalid' as replacement_email, 'guard-new-' || gen_random_uuid() || '@example.invalid' as new_email, 'guard-staff-' || gen_random_uuid() || '@example.invalid' as staff_email, 'guard-sponsor-' || gen_random_uuid() || '@example.invalid' as sponsor_email, 'guard-existing-' || gen_random_uuid() as existing_external_id, 'guard-new-' || gen_random_uuid() as new_external_id, 'guard-bad-' || gen_random_uuid() as bad_external_id, 'guard-stale-' || gen_random_uuid() as stale_external_id; insert into public.contacts(email, full_name, external_id) select existing_email, 'Guard Existing Speaker', 'contact-existing' from sessionize_guard_fixture union all select replacement_email, 'Guard Replacement Speaker', 'contact-replacement' from sessionize_guard_fixture union all select staff_email, 'Guard Staff', 'contact-staff' from sessionize_guard_fixture union all select sponsor_email, 'Guard Sponsor', 'contact-sponsor' from sessionize_guard_fixture; insert into public.events(id, name, start_date, end_date, is_active) select event_id, 'Sessionize guard regression', '2026-10-23', '2026-10-24', false from sessionize_guard_fixture; insert into public.slots(id, event_id, type, name, slot_date, start_time, end_time) select slot_day_one_id, event_id, 'session'::public.slot_type, 'Guard day one', date '2026-10-23', time '09:00', time '10:00' from sessionize_guard_fixture union all select slot_day_two_id, event_id, 'session', 'Guard day two', '2026-10-24', '16:00', '17:00' from sessionize_guard_fixture union all select slot_stale_id, event_id, 'session', 'Guard stale outside range', '2026-02-20', '12:00', '13:00' from sessionize_guard_fixture; insert into public.sessions(id, event_id, slot_id, title, description, room, capacity, average_feedback, ai_generated_feedback, material_path, external_id, level, area) select existing_session_id, event_id, slot_day_one_id, 'Guard old title', 'local description', 'Local room', 42, 4.5, 'local feedback', 'sessions/local/slides.pdf', existing_external_id, 'Intermediate', 'Backend' from sessionize_guard_fixture union all select stale_session_id, event_id, slot_stale_id, 'Guard retained stale title', 'stale description', 'Stale room', 7, null, null, null, stale_external_id, null, null from sessionize_guard_fixture; insert into public.registrations(event_id, contact_email, type, points, checked_in) select event_id, existing_email, 'speaker'::public.registration_type, 11, false from sessionize_guard_fixture union all select event_id, staff_email, 'staff', 22, true from sessionize_guard_fixture union all select event_id, sponsor_email, 'sponsor', 33, true from sessionize_guard_fixture; insert into public.session_speakers(session_id, contact_email) select existing_session_id, existing_email from sessionize_guard_fixture; select public._ensure_auth_user_exists(existing_email, 'Guard Existing Speaker') from sessionize_guard_fixture; create temporary table sessionize_guard_before as select (select last_value from public.slots_id_seq) as slots_last_value, (select is_called from public.slots_id_seq) as slots_is_called, (select count(*) from public.contacts) as contacts_count, (select count(*) from auth.users) as auth_users_count, (select count(*) from auth.identities) as identities_count, (select count(*) from public.registrations) as registrations_count, (select count(*) from public.sessions) as sessions_count, (select count(*) from public.slots) as slots_count, (select count(*) from public.session_speakers) as links_count, (select count(*) from public.sync_runs) as sync_runs_count, (select jsonb_agg(to_jsonb(c) order by c.email) from public.contacts c cross join sessionize_guard_fixture f where c.email in (f.existing_email, f.replacement_email, f.new_email, f.staff_email, f.sponsor_email)) as contact_rows, (select jsonb_agg(to_jsonb(u) order by u.id) from auth.users u cross join sessionize_guard_fixture f where u.email in (f.existing_email, f.replacement_email, f.new_email, f.staff_email, f.sponsor_email)) as auth_rows, (select jsonb_agg(to_jsonb(i) order by i.id) from auth.identities i join auth.users u on u.id = i.user_id cross join sessionize_guard_fixture f where u.email in (f.existing_email, f.replacement_email, f.new_email, f.staff_email, f.sponsor_email)) as identity_rows, (select to_jsonb(e) from public.events e join sessionize_guard_fixture f on f.event_id = e.id) as event_row, (select to_jsonb(s) from public.sessions s join sessionize_guard_fixture f on f.existing_session_id = s.id) as existing_session_row, (select to_jsonb(s) from public.sessions s join sessionize_guard_fixture f on f.stale_session_id = s.id) as stale_session_row, (select jsonb_agg(to_jsonb(r) order by r.contact_email) from public.registrations r join sessionize_guard_fixture f on f.event_id = r.event_id) as fixture_registrations, (select jsonb_agg(to_jsonb(ss) order by ss.session_id, ss.contact_email) from public.session_speakers ss join public.sessions s on s.id = ss.session_id join sessionize_guard_fixture f on f.event_id = s.event_id) as fixture_links; do $$ begin perform public.sync_sessionize_event(event_id, jsonb_build_object('sessions', '[]'::jsonb)) from sessionize_guard_fixture; raise exception 'FAIL: anonymous API sync was allowed'; exception when others then perform pg_temp.assert_sessionize_guard(sqlerrm = 'service_role only', 'anonymous API sync rejected before event/date lookup'); end; $$; select set_config('request.jwt.claims', '{"role":"authenticated","email":"guard-user@example.invalid"}', true); do $$ begin perform public.sync_sessionize_excel(event_id, jsonb_build_object('sessions', '[]'::jsonb)) from sessionize_guard_fixture; raise exception 'FAIL: authenticated Excel sync was allowed'; exception when others then perform pg_temp.assert_sessionize_guard(sqlerrm = 'service_role only', 'authenticated Excel sync rejected before event/date lookup'); end; $$; select set_config('request.jwt.claims', '{"role":"service_role"}', true); do $$ begin perform public.sync_sessionize_event(gen_random_uuid(), jsonb_build_object('sessions', '[]'::jsonb)); raise exception 'FAIL: missing event was allowed'; exception when others then perform pg_temp.assert_sessionize_guard(sqlerrm = 'event not found', 'missing event behavior unchanged'); end; $$; select pg_temp.expect_sessionize_reject( format($sql$ select public.sync_sessionize_event(%L::uuid, %L::jsonb) $sql$, event_id, jsonb_build_object( 'speakers', jsonb_build_array( jsonb_build_object('email', replacement_email, 'full_name', 'Guard Replacement Updated'), jsonb_build_object('email', new_email, 'full_name', 'Guard New Created') ), 'sessions', jsonb_build_array( jsonb_build_object('external_id', existing_external_id, 'title', 'Guard updated title', 'date', '2026-10-23', 'start_time', '09:00', 'end_time', '10:00', 'room', 'Updated room', 'speakers', jsonb_build_array(replacement_email)), jsonb_build_object('external_id', new_external_id, 'title', 'Guard new title', 'date', '2026-10-24', 'start_time', '16:00', 'end_time', '17:00', 'room', 'New room', 'speakers', jsonb_build_array(new_email)), jsonb_build_object('external_id', bad_external_id, 'title', 'Guard outside title', 'date', '2026-02-20', 'start_time', '11:00', 'end_time', '12:00', 'room', 'Bad room', 'speakers', jsonb_build_array(new_email)) ) )::text), 'API sync rejects wrong date after valid updates in payload') from sessionize_guard_fixture; select pg_temp.expect_sessionize_reject( format($sql$ select public.sync_sessionize_excel(%L::uuid, %L::jsonb) $sql$, event_id, jsonb_build_object( 'speakers', jsonb_build_array( jsonb_build_object('email', replacement_email, 'full_name', 'Guard Replacement Updated'), jsonb_build_object('email', new_email, 'full_name', 'Guard New Created') ), 'sessions', jsonb_build_array( jsonb_build_object('external_id', existing_external_id, 'title', 'Guard Excel updated title', 'date', '2026-10-23', 'start_time', '09:00', 'end_time', '10:00', 'speakers', jsonb_build_array(replacement_email)), jsonb_build_object('external_id', new_external_id, 'title', 'Guard Excel new title', 'date', '2026-10-24', 'start_time', '16:00', 'end_time', '17:00', 'speakers', jsonb_build_array(new_email)), jsonb_build_object('external_id', bad_external_id, 'title', 'Guard Excel outside title', 'date', '2026-02-20', 'start_time', '11:00', 'end_time', '12:00', 'speakers', jsonb_build_array(new_email)) ) )::text), 'Excel sync rejects wrong date after valid speaker/session payload') from sessionize_guard_fixture; do $$ declare v_rpc text; v_fixture record; begin select * into v_fixture from sessionize_guard_fixture; foreach v_rpc in array array['sync_sessionize_event', 'sync_sessionize_excel'] loop perform pg_temp.expect_sessionize_reject( format('select public.%I(%L::uuid, %L::jsonb)', v_rpc, v_fixture.event_id, jsonb_build_object('sessions', jsonb_build_array(jsonb_build_object( 'external_id', v_fixture.bad_external_id, 'title', 'Single outside session', 'date', '2026-02-20', 'start_time', '09:00', 'end_time', '10:00')))::text), v_rpc || ' rejects a single outside session'); end loop; end; $$; select pg_temp.assert_sessionize_guard( (select jsonb_agg(to_jsonb(c) order by c.email) from public.contacts c cross join sessionize_guard_fixture f where c.email in (f.existing_email, f.replacement_email, f.new_email, f.staff_email, f.sponsor_email)) is not distinct from (select contact_rows from sessionize_guard_before) and (select jsonb_agg(to_jsonb(u) order by u.id) from auth.users u cross join sessionize_guard_fixture f where u.email in (f.existing_email, f.replacement_email, f.new_email, f.staff_email, f.sponsor_email)) is not distinct from (select auth_rows from sessionize_guard_before) and (select jsonb_agg(to_jsonb(i) order by i.id) from auth.identities i join auth.users u on u.id = i.user_id cross join sessionize_guard_fixture f where u.email in (f.existing_email, f.replacement_email, f.new_email, f.staff_email, f.sponsor_email)) is not distinct from (select identity_rows from sessionize_guard_before), 'rejected imports do not insert or update contacts, auth users or identities'); select pg_temp.assert_sessionize_guard( (select (last_value, is_called) from public.slots_id_seq) = (select (slots_last_value, slots_is_called) from sessionize_guard_before), 'rejected imports do not advance slots_id_seq'); select pg_temp.assert_sessionize_guard( (select count(*) from public.contacts) = (select contacts_count from sessionize_guard_before) and (select count(*) from auth.users) = (select auth_users_count from sessionize_guard_before) and (select count(*) from auth.identities) = (select identities_count from sessionize_guard_before) and (select count(*) from public.registrations) = (select registrations_count from sessionize_guard_before) and (select count(*) from public.sessions) = (select sessions_count from sessionize_guard_before) and (select count(*) from public.slots) = (select slots_count from sessionize_guard_before) and (select count(*) from public.session_speakers) = (select links_count from sessionize_guard_before) and (select count(*) from public.sync_runs) = (select sync_runs_count from sessionize_guard_before), 'rejected imports leave table counts unchanged'); select pg_temp.assert_sessionize_guard( (select to_jsonb(e) from public.events e join sessionize_guard_fixture f on f.event_id = e.id) = (select event_row from sessionize_guard_before) and (select to_jsonb(s) from public.sessions s join sessionize_guard_fixture f on f.existing_session_id = s.id) = (select existing_session_row from sessionize_guard_before) and (select to_jsonb(s) from public.sessions s join sessionize_guard_fixture f on f.stale_session_id = s.id) = (select stale_session_row from sessionize_guard_before) and (select jsonb_agg(to_jsonb(r) order by r.contact_email) from public.registrations r join sessionize_guard_fixture f on f.event_id = r.event_id) = (select fixture_registrations from sessionize_guard_before) and (select jsonb_agg(to_jsonb(ss) order by ss.session_id, ss.contact_email) from public.session_speakers ss join public.sessions s on s.id = ss.session_id join sessionize_guard_fixture f on f.event_id = s.event_id) = (select fixture_links from sessionize_guard_before), 'rejected imports leave event, sessions, registrations and links unchanged'); do $$ declare v_result jsonb; begin select public.sync_sessionize_event(event_id, jsonb_build_object( 'sessions', jsonb_build_array( jsonb_build_object('external_id', 'malformed-date-first', 'title', 'Malformed first', 'date', 'not-a-date', 'start_time', '09:00', 'end_time', '10:00'), jsonb_build_object('external_id', bad_external_id, 'title', 'Outside after malformed', 'date', '2026-02-20', 'start_time', '11:00', 'end_time', '12:00') ) )) into v_result from sessionize_guard_fixture; raise exception 'FAIL: malformed first plus outside date was allowed'; exception when others then perform pg_temp.assert_sessionize_guard( sqlerrm like '%2026-02-20%' and sqlerrm like '%2026-10-23%' and sqlerrm like '%2026-10-24%', 'preflight continues after malformed date and rejects later outside date'); end; $$; do $$ declare v_result jsonb; begin select public.sync_sessionize_event(event_id, jsonb_build_object( 'sessions', jsonb_build_array( jsonb_build_object('external_id', 'malformed-date-only', 'title', 'Malformed date only', 'date', 'not-a-date', 'start_time', '09:00', 'end_time', '10:00'), jsonb_build_object('external_id', 'malformed-time-only', 'title', 'Malformed time only', 'date', '2026-10-23', 'start_time', 'nope', 'end_time', '10:00'), jsonb_build_object('external_id', 'missing-field-only', 'title', 'Missing field only', 'date', '2026-10-23', 'start_time', '11:00') ) )) into v_result from sessionize_guard_fixture; perform pg_temp.assert_sessionize_guard( jsonb_array_length(v_result -> 'errors') = 3 and (v_result -> 'errors')::text like '%Malformed date only%' and (v_result -> 'errors')::text like '%Malformed time only%' and (v_result -> 'errors')::text like '%Missing field only%missing required field%', 'inside/malformed dates keep per-row malformed date/time/missing-field errors'); end; $$; do $$ declare v_result jsonb; begin select public.sync_sessionize_excel(event_id, jsonb_build_object( 'speakers', jsonb_build_array( jsonb_build_object('email', existing_email, 'full_name', 'Guard Existing Updated'), jsonb_build_object('email', replacement_email, 'full_name', 'Guard Replacement Speaker'), jsonb_build_object('email', new_email, 'full_name', 'Guard New Speaker') ), 'sessions', jsonb_build_array( jsonb_build_object('external_id', existing_external_id, 'title', 'Guard final title', 'description', null, 'date', '2026-10-23', 'start_time', '09:00', 'end_time', '10:00', 'room', null, 'level', 'Advanced', 'area', 'Data', 'speakers', jsonb_build_array(replacement_email)), jsonb_build_object('external_id', new_external_id, 'title', 'Guard inclusive end title', 'description', 'new description', 'date', '2026-10-24', 'start_time', '16:00', 'end_time', '17:00', 'room', 'New room', 'level', 'Beginner', 'area', 'Frontend', 'speakers', jsonb_build_array(new_email)) ) )) into v_result from sessionize_guard_fixture; perform pg_temp.assert_sessionize_guard( v_result @> '{"sessions_created":1,"sessions_updated":1,"slots_created":0,"errors":[]}'::jsonb, 'Excel success accepts same-day start and multiday inclusive end using existing slots'); end; $$; select pg_temp.assert_sessionize_guard( exists (select 1 from public.sessions s join sessionize_guard_fixture f on f.existing_session_id = s.id where s.title = 'Guard final title' and s.description = 'local description' and s.room = 'Local room' and s.capacity = 42 and s.average_feedback = 4.5 and s.ai_generated_feedback = 'local feedback' and s.material_path = 'sessions/local/slides.pdf' and s.level = 'Advanced' and s.area = 'Data'), 'local material, capacity, feedback, description and room are preserved on update'); select pg_temp.assert_sessionize_guard( exists (select 1 from public.session_speakers ss join sessionize_guard_fixture f on f.existing_session_id = ss.session_id where ss.contact_email = replacement_email) and not exists (select 1 from public.session_speakers ss join sessionize_guard_fixture f on f.existing_session_id = ss.session_id where ss.contact_email = existing_email), 'speaker links keep DIFF behavior on successful import'); select pg_temp.assert_sessionize_guard( exists (select 1 from public.sessions s join sessionize_guard_fixture f on f.stale_session_id = s.id where s.title = 'Guard retained stale title') and exists (select 1 from public.slots sl join sessionize_guard_fixture f on f.slot_stale_id = sl.id where sl.slot_date = '2026-02-20'), 'stale slot/session outside range are retained after successful imports'); select pg_temp.assert_sessionize_guard( exists (select 1 from public.registrations r join sessionize_guard_fixture f on f.event_id = r.event_id where r.contact_email = staff_email and r.type = 'staff' and r.points = 22 and r.checked_in) and exists (select 1 from public.registrations r join sessionize_guard_fixture f on f.event_id = r.event_id where r.contact_email = sponsor_email and r.type = 'sponsor' and r.points = 33 and r.checked_in) and exists (select 1 from public.registrations r join sessionize_guard_fixture f on f.event_id = r.event_id where r.contact_email = new_email and r.type = 'speaker'), 'Excel speaker registrations preserve staff/sponsor and create speaker registration'); select pg_temp.assert_sessionize_guard( (select (last_value, is_called) from public.slots_id_seq) = (select (slots_last_value, slots_is_called) from sessionize_guard_before), 'successful fixture imports do not advance slots_id_seq when slots already exist'); do $$ declare v_first jsonb; v_second jsonb; begin select public.sync_sessionize_event(event_id, jsonb_build_object( 'sessions', jsonb_build_array( jsonb_build_object('external_id', existing_external_id, 'title', 'Guard final title', 'date', '2026-10-23', 'start_time', '09:00', 'end_time', '10:00', 'speakers', jsonb_build_array(replacement_email)), jsonb_build_object('external_id', new_external_id, 'title', 'Guard inclusive end title', 'date', '2026-10-24', 'start_time', '16:00', 'end_time', '17:00', 'speakers', jsonb_build_array(new_email)) ) )) into v_first from sessionize_guard_fixture; select public.sync_sessionize_event(event_id, jsonb_build_object( 'sessions', jsonb_build_array( jsonb_build_object('external_id', existing_external_id, 'title', 'Guard final title', 'date', '2026-10-23', 'start_time', '09:00', 'end_time', '10:00', 'speakers', jsonb_build_array(replacement_email)), jsonb_build_object('external_id', new_external_id, 'title', 'Guard inclusive end title', 'date', '2026-10-24', 'start_time', '16:00', 'end_time', '17:00', 'speakers', jsonb_build_array(new_email)) ) )) into v_second from sessionize_guard_fixture; perform pg_temp.assert_sessionize_guard( v_first @> '{"sessions_created":0,"sessions_updated":2,"slots_created":0,"errors":[]}'::jsonb and v_second @> '{"sessions_created":0,"sessions_updated":2,"slots_created":0,"errors":[]}'::jsonb, 'API replay is idempotent across same-day and inclusive-end sessions'); end; $$; do $$ declare v_before timestamptz; v_after timestamptz; begin select last_synced_at into v_before from public.events e join sessionize_guard_fixture f on f.event_id = e.id; perform public.sync_sessionize_event(event_id, '{"sessions":[]}'::jsonb) from sessionize_guard_fixture; select last_synced_at into v_after from public.events e join sessionize_guard_fixture f on f.event_id = e.id; perform pg_temp.assert_sessionize_guard(v_after >= coalesce(v_before, '-infinity'::timestamptz), 'empty sessions import is preserved and updates audit timestamp'); end; $$; update public.events set end_date = start_date where id = (select event_id from sessionize_guard_fixture); do $$ declare v_rpc text; v_fixture record; v_payload jsonb; v_result jsonb; begin select * into v_fixture from sessionize_guard_fixture; foreach v_rpc in array array['sync_sessionize_event', 'sync_sessionize_excel'] loop v_payload := jsonb_build_object('sessions', jsonb_build_array(jsonb_build_object( 'external_id', v_fixture.existing_external_id, 'title', 'Guard single-day title', 'date', '2026-10-23', 'start_time', '09:00', 'end_time', '10:00'))); execute format('select public.%I($1, $2)', v_rpc) into v_result using v_fixture.event_id, v_payload; perform pg_temp.assert_sessionize_guard( v_result @> '{"sessions_created":0,"sessions_updated":1,"slots_created":0,"errors":[]}'::jsonb, v_rpc || ' accepts an event whose start and end dates are identical'); perform pg_temp.expect_sessionize_reject( format('select public.%I(%L::uuid, %L::jsonb)', v_rpc, v_fixture.event_id, jsonb_set(v_payload, '{sessions,0,date}', '"2026-10-24"'::jsonb)::text), v_rpc || ' rejects the day after a single-day event', date '2026-10-24', date '2026-10-23', date '2026-10-23'); execute format('select public.%I($1, $2)', v_rpc) into v_result using v_fixture.event_id, '{"sessions":[]}'::jsonb; perform pg_temp.assert_sessionize_guard( v_result @> '{"sessions_created":0,"sessions_updated":0,"slots_created":0,"errors":[]}'::jsonb, v_rpc || ' preserves empty-session payload behavior'); end loop; end; $$; select pg_temp.assert_sessionize_guard( (select (last_value, is_called) from public.slots_id_seq) = (select (slots_last_value, slots_is_called) from sessionize_guard_before), 'complete suite leaves the slot sequence unchanged'); rollback;