\set ON_ERROR_STOP on begin; -- Regression suite for 20261020120000_staff_auto_connections.sql. -- Run against a database that already has the migration applied: -- docker exec -i supabase_db_Convivo psql -X -U postgres -d postgres -v ON_ERROR_STOP=1 \ -- < supabase/tests/staff_auto_connections_tests.sql -- Everything is created inside this transaction and rolled back at the end. create temporary table staff_conn_results (name text); create function pg_temp.assert_conn(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; insert into staff_conn_results values (p_name); end; $$; -- --------------------------------------------------------------------------- -- Fixture: one disposable (inactive) event. Staff side = type 'staff', an -- is_admin contact, or an @appe.at address; people side = everyone else. -- --------------------------------------------------------------------------- insert into public.events (id, name, start_date, end_date, is_active) values ('a0170000-0000-0000-0000-000000000001', 'Staff autoconnect regression', current_date, current_date, false); insert into public.contacts (email, full_name, is_admin) values ('sc-staff@example.test', 'Staff One', false), ('sc-admin@example.test', 'Admin Person', true), ('sc-domain@appe.at', 'Domain Person', false), ('sc-att1@example.test', 'Att One', false), ('sc-att2@example.test', 'Att Two', false), ('sc-spk@example.test', 'Speaker One', false); insert into public.registrations (event_id, contact_email, type, qr_token, points) values ('a0170000-0000-0000-0000-000000000001', 'sc-staff@example.test', 'staff', 'SC-1', 0), ('a0170000-0000-0000-0000-000000000001', 'sc-admin@example.test', 'attendee', 'SC-2', 0), ('a0170000-0000-0000-0000-000000000001', 'sc-domain@appe.at', 'attendee', 'SC-3', 0), ('a0170000-0000-0000-0000-000000000001', 'sc-att1@example.test', 'attendee', 'SC-4', 0), ('a0170000-0000-0000-0000-000000000001', 'sc-att2@example.test', 'attendee', 'SC-5', 0), ('a0170000-0000-0000-0000-000000000001', 'sc-spk@example.test', 'speaker', 'SC-6', 0); -- 3 staff-side registrations × 3 people = 9 auto pairs, all accepted+auto. select pg_temp.assert_conn( (select count(*) from public.connections where event_id = 'a0170000-0000-0000-0000-000000000001' and status = 'accepted' and auto and resolved_at is not null) = 9, 'staff x people pairs created by the trigger'); -- Every auto pair crosses the staff/people boundary exactly — no staff × -- staff and no people × people rows were ever created. select pg_temp.assert_conn( (with staff_side(e) as (values ('sc-staff@example.test'), ('sc-admin@example.test'), ('sc-domain@appe.at')), auto_rows(a, b) as ( select least(c.from_email, c.to_email), greatest(c.from_email, c.to_email) from public.connections c where c.event_id = 'a0170000-0000-0000-0000-000000000001' and c.auto) select count(*) from auto_rows r where (r.a in (select e from staff_side)) = (r.b in (select e from staff_side))) = 0, 'every auto pair is exactly one staff side and one person'); select pg_temp.assert_conn( exists (select 1 from public.connections where event_id = 'a0170000-0000-0000-0000-000000000001' and from_email = 'sc-staff@example.test' and to_email = 'sc-att1@example.test' and status = 'accepted' and auto), 'staff-to-attendee row stored accepted and auto'); -- --------------------------------------------------------------------------- -- NO POINTS: the whole feature must never touch points or points_log. -- --------------------------------------------------------------------------- select pg_temp.assert_conn( (select count(*) from public.points_log where event_id = 'a0170000-0000-0000-0000-000000000001') = 0, 'auto-connect writes no points_log rows'); select pg_temp.assert_conn( (select bool_and(points = 0) from public.registrations where event_id = 'a0170000-0000-0000-0000-000000000001'), 'auto-connect leaves every points value at 0'); -- --------------------------------------------------------------------------- -- Late arrivals pair in both directions. -- --------------------------------------------------------------------------- insert into public.contacts (email, full_name, is_admin) values ('sc-late@example.test', 'Late Person', false); insert into public.registrations (event_id, contact_email, type, qr_token, points) values ('a0170000-0000-0000-0000-000000000001', 'sc-late@example.test', 'attendee', 'SC-7', 0); select pg_temp.assert_conn( (select count(*) from public.connections where event_id = 'a0170000-0000-0000-0000-000000000001' and auto) = 12, 'a person registered after the staff pairs with all three staff'); insert into public.contacts (email, full_name, is_admin) values ('sc-staff2@example.test', 'Staff Two', false); insert into public.registrations (event_id, contact_email, type, qr_token, points) values ('a0170000-0000-0000-0000-000000000001', 'sc-staff2@example.test', 'staff', 'SC-8', 0); select pg_temp.assert_conn( (select count(*) from public.connections where event_id = 'a0170000-0000-0000-0000-000000000001' and auto) = 16, 'a staff member registered later pairs with all four people'); -- --------------------------------------------------------------------------- -- Existing manual pairs are never touched. -- --------------------------------------------------------------------------- -- People x people stay connectable by hand and stay pending. insert into public.connections (event_id, from_email, to_email, status) values ('a0170000-0000-0000-0000-000000000001', 'sc-spk@example.test', 'sc-att1@example.test', 'pending'); -- A staff that was manually rejected before registering: the trigger must -- not flip the rejection back to accepted (and must still pair the rest). insert into public.contacts (email, full_name, is_admin) values ('sc-staff3@example.test', 'Staff Three', false); insert into public.connections (event_id, from_email, to_email, status, resolved_at) values ('a0170000-0000-0000-0000-000000000001', 'sc-staff3@example.test', 'sc-spk@example.test', 'rejected', now()); insert into public.registrations (event_id, contact_email, type, qr_token, points) values ('a0170000-0000-0000-0000-000000000001', 'sc-staff3@example.test', 'staff', 'SC-9', 0); select public._autoconnect_staff_event('a0170000-0000-0000-0000-000000000001'); -- idempotent re-run select pg_temp.assert_conn( (select count(*) from public.connections where event_id = 'a0170000-0000-0000-0000-000000000001') = 21, 'backfill re-run inserts nothing new (19 auto + pending + rejected)'); select pg_temp.assert_conn( (select count(*) from public.connections where event_id = 'a0170000-0000-0000-0000-000000000001' and auto) = 19, 'late staff pairs with three people and skips the rejected pair'); select pg_temp.assert_conn( exists (select 1 from public.connections where event_id = 'a0170000-0000-0000-0000-000000000001' and least(from_email, to_email) = 'sc-att1@example.test' and greatest(from_email, to_email) = 'sc-spk@example.test' and status = 'pending'), 'manual pending people-pair untouched by the backfill'); select pg_temp.assert_conn( exists (select 1 from public.connections where event_id = 'a0170000-0000-0000-0000-000000000001' and least(from_email, to_email) = 'sc-spk@example.test' and greatest(from_email, to_email) = 'sc-staff3@example.test' and status = 'rejected'), 'manually rejected staff pair is not re-accepted'); -- --------------------------------------------------------------------------- -- Type change: attendee becomes staff and pairs with the remaining people. -- --------------------------------------------------------------------------- update public.registrations set "type" = 'staff' where event_id = 'a0170000-0000-0000-0000-000000000001' and contact_email = 'sc-att2@example.test'; select pg_temp.assert_conn( exists (select 1 from public.connections where event_id = 'a0170000-0000-0000-0000-000000000001' and from_email = 'sc-att2@example.test' and to_email = 'sc-late@example.test' and status = 'accepted' and auto), 'type flip to staff creates the new staff-side pairs'); select pg_temp.assert_conn( (select count(*) from public.points_log where event_id = 'a0170000-0000-0000-0000-000000000001') = 0, 'type flip still awards no points'); -- --------------------------------------------------------------------------- -- Helpers stay internal. -- --------------------------------------------------------------------------- select pg_temp.assert_conn( not has_function_privilege('authenticated', 'public._autoconnect_staff_event(uuid)', 'execute') and not has_function_privilege('anon', 'public._registrations_autoconnect_staff()', 'execute'), 'API roles cannot call the autoconnect helpers'); select count(*) as passed_assertions from staff_conn_results; rollback;