\set ON_ERROR_STOP on begin; create temporary table leaderboard_results (name text); create function pg_temp.assert_board(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 leaderboard_results values (p_name); end; $$; update public.events set is_active = false where is_active; insert into public.events (id, name, start_date, end_date, is_active) values ('a0150000-0000-0000-0000-000000000001', 'Leaderboard regression', current_date, current_date, true), ('a0150000-0000-0000-0000-000000000002', 'Other event', current_date, current_date, false); insert into public.contacts (email, full_name, is_admin) values ('lb-player@example.test', 'Player', false), ('lb-peer@example.test', 'Peer', false), ('lb-other@example.test', 'Other', false), ('lb-speaker@example.test', 'Speaker', false), ('lb-staff@example.test', 'Staff', false), ('lb-admin@example.test', 'Admin', true), ('lb-domain@appe.at', 'Domain Admin', false), ('lb-orphan@example.test', 'Orphan', false); insert into public.registrations (event_id, contact_email, type, qr_token, points) select 'a0150000-0000-0000-0000-000000000001'::uuid, email, case when email = 'lb-staff@example.test' then 'staff' when email = 'lb-speaker@example.test' then 'speaker' else 'attendee' end::public.registration_type, 'LB-' || email, case when is_admin or email = 'lb-staff@example.test' then 99 else 0 end from public.contacts where email in ('lb-player@example.test', 'lb-peer@example.test', 'lb-other@example.test', 'lb-speaker@example.test', 'lb-staff@example.test', 'lb-admin@example.test', 'lb-domain@appe.at'); insert into public.registrations (event_id, contact_email, type, qr_token, points) values ('a0150000-0000-0000-0000-000000000002', 'lb-peer@example.test', 'attendee', 'LB-OTHER-EVENT', 50); insert into public.slots (id, event_id, name, slot_date, start_time, end_time, type) values (-151001, 'a0150000-0000-0000-0000-000000000001', 'Talk', current_date, '09:00', '10:00', 'session'), (-151002, 'a0150000-0000-0000-0000-000000000001', 'Service', current_date, '10:00', '11:00', 'service_session'); insert into public.sessions (id, event_id, slot_id, title) values ('a0150000-0000-0000-0000-000000000011', 'a0150000-0000-0000-0000-000000000001', -151001, 'Talk one'), ('a0150000-0000-0000-0000-000000000012', 'a0150000-0000-0000-0000-000000000001', -151001, 'Talk two'), ('a0150000-0000-0000-0000-000000000013', 'a0150000-0000-0000-0000-000000000001', -151002, 'Service'); do $$ declare event_one uuid := 'a0150000-0000-0000-0000-000000000001'; talk_one uuid := 'a0150000-0000-0000-0000-000000000011'; talk_two uuid := 'a0150000-0000-0000-0000-000000000012'; excluded_email text; feedback_result record; begin perform pg_temp.assert_board((select bool_and(points = 0) from public.registrations where contact_email in ('lb-staff@example.test','lb-admin@example.test','lb-domain@appe.at')), 'excluded inserts force zero'); perform set_config('request.jwt.claims', '{"email":"lb-admin@example.test"}', true); perform public.checkin_attendee(talk_one, 'LB-lb-player@example.test'); perform pg_temp.assert_board((select points = 10 and checked_in from public.registrations where contact_email = 'lb-player@example.test'), 'check-in awards ten'); perform pg_temp.assert_board((select already and points = 10 from public.checkin_attendee(talk_one, 'LB-lb-player@example.test')), 'check-in idempotent'); perform public.checkin_attendee(talk_one, 'LB-lb-peer@example.test'); perform public.checkin_attendee(talk_one, 'LB-lb-speaker@example.test'); perform pg_temp.assert_board((select points = 10 from public.registrations where contact_email = 'lb-speaker@example.test'), 'nonadmin speaker earns points'); foreach excluded_email in array array['lb-staff@example.test','lb-admin@example.test','lb-domain@appe.at'] loop perform public.checkin_attendee(talk_one, 'LB-' || excluded_email); end loop; begin perform public.checkin_attendee('a0150000-0000-0000-0000-000000000013', 'LB-lb-player@example.test'); raise exception 'service check-in accepted'; exception when raise_exception then if sqlerrm <> 'service sessions cannot be checked in' then raise; end if; end; perform pg_temp.assert_board(true, 'service check-in rejected'); foreach excluded_email in array array['lb-staff@example.test','lb-admin@example.test','lb-domain@appe.at'] loop perform set_config('request.jwt.claims', json_build_object('email', excluded_email)::text, true); select * into feedback_result from public.submit_feedback(talk_one, 5, 'Review'); perform pg_temp.assert_board(feedback_result.earned = 0 and feedback_result.points = 0, excluded_email || ' feedback earns zero'); perform public._award_points(event_one, excluded_email, 7, 'connection'); perform public._award_points(event_one, excluded_email, 2, 'talk_networking'); perform public._award_points(event_one, excluded_email, 100, 'admin'); end loop; perform pg_temp.assert_board(not exists (select 1 from public.points_log where contact_email in ('lb-staff@example.test','lb-admin@example.test','lb-domain@appe.at')), 'no excluded award logs'); perform set_config('request.jwt.claims', '{"email":"lb-player@example.test"}', true); perform pg_temp.assert_board((select earned = 5 and points = 15 from public.submit_feedback(talk_one, 4, null)), 'rating awards five'); perform pg_temp.assert_board((select earned = 0 and points = 15 from public.submit_feedback(talk_one, 5, 'Updated')), 'feedback idempotent'); perform set_config('request.jwt.claims', '{"email":"lb-admin@example.test"}', true); perform public.checkin_attendee(talk_two, 'LB-lb-player@example.test'); perform set_config('request.jwt.claims', '{"email":"lb-player@example.test"}', true); perform pg_temp.assert_board((select earned = 10 and points = 35 from public.submit_feedback(talk_two, 5, 'Useful')), 'comment awards ten'); perform pg_temp.assert_board((select points = 35 from public.submit_event_feedback(5, 'Good event')), 'event feedback does not award points'); end; $$; insert into public.connections (id, event_id, from_email, to_email) values ('a0150000-0000-0000-0000-000000000021', 'a0150000-0000-0000-0000-000000000001', 'lb-player@example.test', 'lb-staff@example.test'), ('a0150000-0000-0000-0000-000000000022', 'a0150000-0000-0000-0000-000000000001', 'lb-player@example.test', 'lb-peer@example.test'), ('a0150000-0000-0000-0000-000000000023', 'a0150000-0000-0000-0000-000000000001', 'lb-player@example.test', 'lb-other@example.test'); do $$ begin perform set_config('request.jwt.claims', '{"email":"lb-staff@example.test"}', true); perform pg_temp.assert_board((select shared_talk from public.accept_connection('a0150000-0000-0000-0000-000000000021')), 'shared talk detected'); perform pg_temp.assert_board((select points = 42 from public.registrations where contact_email = 'lb-player@example.test'), 'eligible peer earns seven with staff connection'); perform pg_temp.assert_board((select points = 0 from public.registrations where contact_email = 'lb-staff@example.test'), 'staff connection earns zero'); perform set_config('request.jwt.claims', '{"email":"lb-peer@example.test"}', true); perform public.accept_connection('a0150000-0000-0000-0000-000000000022'); perform pg_temp.assert_board((select points = 49 from public.registrations where contact_email = 'lb-player@example.test'), 'sender earns connection plus bonus'); perform pg_temp.assert_board((select points = 17 from public.registrations where contact_email = 'lb-peer@example.test' and event_id = 'a0150000-0000-0000-0000-000000000001'), 'recipient earns connection plus bonus'); begin perform public.accept_connection('a0150000-0000-0000-0000-000000000022'); raise exception 'repeated acceptance allowed'; exception when raise_exception then if sqlerrm <> 'request already resolved' then raise; end if; end; perform pg_temp.assert_board(true, 'connection one-shot'); perform set_config('request.jwt.claims', '{"email":"lb-other@example.test"}', true); perform pg_temp.assert_board((select not shared_talk from public.accept_connection('a0150000-0000-0000-0000-000000000023')), 'no shared-talk bonus without shared check-in'); perform pg_temp.assert_board((select points = 5 from public.registrations where contact_email = 'lb-other@example.test'), 'ordinary connection earns five'); perform pg_temp.assert_board((select points = 50 from public.registrations where event_id = 'a0150000-0000-0000-0000-000000000002'), 'other event untouched'); perform set_config('request.jwt.claims', '{"email":"lb-admin@example.test"}', true); perform public.adjust_points('lb-other@example.test', -999, 'Floor test'); perform pg_temp.assert_board((select points = 0 from public.registrations where contact_email = 'lb-other@example.test'), 'points floored at zero'); perform public.adjust_points('lb-player@example.test', 3, 'Correction'); perform pg_temp.assert_board((select points = 57 from public.registrations where contact_email = 'lb-player@example.test'), 'admin adjustment updates player'); perform public.adjust_points('lb-staff@example.test', 100, 'Must not award'); perform public.adjust_points('lb-admin@example.test', 100, 'Must not award'); perform public._award_points('a0150000-0000-0000-0000-000000000001', 'lb-orphan@example.test', 100, 'admin'); perform pg_temp.assert_board(not exists (select 1 from public.points_log where contact_email in ('lb-staff@example.test','lb-admin@example.test','lb-domain@appe.at','lb-orphan@example.test')), 'adjustments cannot award excluded or missing registrations'); perform pg_temp.assert_board(not exists (select 1 from public.leaderboard l join public.registrations r using (event_id, contact_email) where l.points <> r.points), 'leaderboard matches registration totals'); perform pg_temp.assert_board((select contact_email = 'lb-player@example.test' from public.get_leaderboard() limit 1), 'highest points rank first'); perform pg_temp.assert_board(not exists (select 1 from public.get_leaderboard() where event_id <> 'a0150000-0000-0000-0000-000000000001'), 'active event only'); end; $$; update public.registrations set points = 99 where contact_email = 'lb-staff@example.test'; select pg_temp.assert_board((select points = 0 from public.registrations where contact_email = 'lb-staff@example.test'), 'direct staff edits forced to zero'); update public.registrations set type = 'staff' where contact_email = 'lb-player@example.test'; select pg_temp.assert_board((select points = 0 from public.registrations where contact_email = 'lb-player@example.test'), 'staff promotion clears points'); select pg_temp.assert_board(not exists (select 1 from public.leaderboard where contact_email = 'lb-player@example.test'), 'staff promotion removes ranking'); update public.contacts set is_admin = true where email = 'lb-peer@example.test'; select pg_temp.assert_board((select bool_and(points = 0) from public.registrations where contact_email = 'lb-peer@example.test'), 'admin promotion clears all event balances'); select pg_temp.assert_board(not exists (select 1 from public.leaderboard where contact_email = 'lb-peer@example.test'), 'admin promotion removes all rankings'); select pg_temp.assert_board(exists (select 1 from public.points_log where contact_email = 'lb-peer@example.test'), 'historical audit retained'); update public.registrations set type = 'attendee', points = 7 where contact_email = 'lb-player@example.test'; update public.registrations set points = 7 where contact_email = 'lb-other@example.test'; update public.contacts set full_name = 'Tied' where email in ('lb-player@example.test', 'lb-other@example.test'); select pg_temp.assert_board((select bool_and(full_name = 'Tied') from public.leaderboard where contact_email in ('lb-player@example.test','lb-other@example.test')), 'name changes refresh mirror'); select pg_temp.assert_board((select array_agg(contact_email) = array['lb-other@example.test','lb-player@example.test'] from public.get_leaderboard() where points = 7), 'deterministic tie order'); insert into public.leaderboard (event_id, contact_email, full_name, points) values ('a0150000-0000-0000-0000-000000000001', 'lb-staff@example.test', 'Stray staff', 999), ('a0150000-0000-0000-0000-000000000001', 'lb-admin@example.test', 'Stray admin', 999), ('a0150000-0000-0000-0000-000000000001', 'lb-orphan@example.test', 'Stray orphan', 999); select pg_temp.assert_board(not exists (select 1 from public.get_leaderboard() where contact_email in ('lb-staff@example.test','lb-admin@example.test','lb-orphan@example.test')), 'read defense excludes stale staff admin and orphan rows'); select public._sync_leaderboard('a0150000-0000-0000-0000-000000000001', 'lb-staff@example.test'); select pg_temp.assert_board(not exists (select 1 from public.leaderboard where contact_email = 'lb-staff@example.test'), 'sync removes stale ranking'); delete from public.registrations where contact_email = 'lb-other@example.test'; select pg_temp.assert_board(not exists (select 1 from public.leaderboard where contact_email = 'lb-other@example.test'), 'registration deletion removes ranking'); select pg_temp.assert_board(not has_function_privilege('authenticated','public._award_points(uuid,text,integer,text)','execute') and not has_function_privilege('anon','public._award_points(uuid,text,integer,text)','execute'), 'API roles cannot forge awards'); select pg_temp.assert_board(not has_function_privilege('authenticated','public._sync_leaderboard(uuid,text)','execute'), 'API roles cannot call sync helper'); select pg_temp.assert_board(has_function_privilege('authenticated','public.get_leaderboard()','execute') and not has_function_privilege('anon','public.get_leaderboard()','execute'), 'rankings require authentication'); select pg_temp.assert_board(exists (select 1 from pg_publication_tables where pubname = 'supabase_realtime' and tablename = 'leaderboard' and schemaname = 'public'), 'Realtime publication enabled'); grant insert on leaderboard_results to authenticated; select set_config('request.jwt.claims', '{"email":"lb-player@example.test","role":"authenticated"}', true); set local role authenticated; select pg_temp.assert_board(exists (select 1 from public.get_leaderboard()), 'authenticated API role can read rankings'); do $$ begin begin perform public._award_points('a0150000-0000-0000-0000-000000000001', 'lb-player@example.test', 9999, 'admin'); raise exception 'forged award accepted'; exception when insufficient_privilege then null; end; perform pg_temp.assert_board(true, 'direct helper invocation denied'); begin perform public.adjust_points('lb-player@example.test', 9999, 'Not admin'); raise exception 'unauthorized adjustment accepted'; exception when raise_exception then if sqlerrm <> 'admin only' then raise; end if; end; perform pg_temp.assert_board(true, 'nonadmin adjustment rejected'); end; $$; reset role; select count(*) as passed_assertions from leaderboard_results; rollback;