Files
Convivo/supabase/tests/leaderboard_tests.sql
T
17473916a6 fix(admin): per-row check-in toggle; feat(db): front-desk gate + badge copies
Registrations table felt buggy because both row actions did
setBusy(true) -> write -> reloadRegistrations() -> setBusy(false):
the list is sorted by points, so a reload made the row you just
touched jump (or vanish under a Checked-in filter), and the single
`busy` flag disabled every control in the table for the whole
request. The fix is per-row in-flight state (busyIds + withRowBusy)
and no reload at all: the toggle patches its own row's checked_in
plus the points delta the RPC returns, the type change patches
`type`; a rejected write rolls both back. Only the touched row is
disabled, shows a "Saving…" pulse, and two rows can save at once.
Toggle hit area is now 40x41 px with a focus-visible ring.

Front-desk gate: checkin_attendee reads registrations.checked_in
and raises "This attendee needs to be checked in at the front desk
first." when it is not true, and no longer sets checked_in as a
side effect. checkins has no INSERT policy, so the RPC is the only
write path and the gate cannot be bypassed. The scanner shows a
dedicated "Not checked in yet" sheet instead of a toast.

Badge copies: events.badge_copies (1..4, default 1). welcome_checkin
sends that many pgmq jobs in one transaction (the Pi prints one
label per job, so N jobs = N labels; no worker change) and clamps
the value again itself. preview_welcome_checkin gained a `copies`
column (drop + recreate) so the popup states the real count. Manual
reprint still prints exactly one. Set from Settings -> Badge
printing (active event) and the Events form, always as a
single-column update; the Settings badge section now renders even
when the LLM settings call fails.

Tests: badge_print 113 (+6), leaderboard 47 (+1), new
registration-checkin-toggle (6), front-desk-gate (3),
welcome-badge-copies (3); the stale registrations-PATCH mock in
registration-ticket-merge now targets set_registration_checked_in,
the ambiguous Save button is "Save copies", and the print-copy
assertion tolerates the typographic apostrophe. Docs updated
(rpcs, schema, printing, tests, admin-app, screens) with a release
record; NOT deployed - see docs/releases/2026-10-19.md.

Co-authored-by: Stephan Charles <[email protected]>
Co-authored-by: Amelia Hernandez <[email protected]>
Co-authored-by: Markus Franz <[email protected]>
Co-authored-by: Sabrina Hota <[email protected]>
Co-authored-by: Nilüfer Doğan <[email protected]>
2026-10-19 15:10:20 +02:00

201 lines
16 KiB
PL/PgSQL

\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 ('[email protected]', 'Player', false), ('[email protected]', 'Peer', false),
('[email protected]', 'Other', false), ('[email protected]', 'Speaker', false),
('[email protected]', 'Staff', false), ('[email protected]', 'Admin', true),
('[email protected]', 'Domain Admin', false), ('[email protected]', 'Orphan', false);
insert into public.registrations (event_id, contact_email, type, qr_token, points, checked_in)
select 'a0150000-0000-0000-0000-000000000001'::uuid, email,
case when email = '[email protected]' then 'staff'
when email = '[email protected]' then 'speaker' else 'attendee' end::public.registration_type,
'LB-' || email, case when is_admin or email = '[email protected]' then 99 else 0 end,
true
from public.contacts where email in ('[email protected]', '[email protected]', '[email protected]',
'[email protected]', '[email protected]', '[email protected]', '[email protected]');
insert into public.registrations (event_id, contact_email, type, qr_token, points)
values ('a0150000-0000-0000-0000-000000000002', '[email protected]', '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');
-- A front-desk-UNCHECKED attendee: has a registration but checked_in = false,
-- so a session check-in must be blocked by the front-desk gate.
insert into public.contacts (email, full_name, is_admin)
values ('[email protected]', 'Fresh Face', false);
insert into public.registrations (event_id, contact_email, type, qr_token, points, checked_in)
values ('a0150000-0000-0000-0000-000000000001', '[email protected]', 'attendee', '[email protected]', 0, false);
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 ('[email protected]','[email protected]','[email protected]')), 'excluded inserts force zero');
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
perform public.checkin_attendee(talk_one, '[email protected]');
perform pg_temp.assert_board((select points = 10 and checked_in from public.registrations where contact_email = '[email protected]'), 'check-in awards ten');
perform pg_temp.assert_board((select already and points = 10 from public.checkin_attendee(talk_one, '[email protected]')), 'check-in idempotent');
perform public.checkin_attendee(talk_one, '[email protected]');
perform public.checkin_attendee(talk_one, '[email protected]');
perform pg_temp.assert_board((select points = 10 from public.registrations where contact_email = '[email protected]'), 'nonadmin speaker earns points');
foreach excluded_email in array array['[email protected]','[email protected]','[email protected]'] loop
perform public.checkin_attendee(talk_one, 'LB-' || excluded_email);
end loop;
begin
perform public.checkin_attendee('a0150000-0000-0000-0000-000000000013', '[email protected]');
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');
-- Front-desk gate: an attendee not checked in at the event cannot be scanned
-- into a session; the RPC raises a clear message.
begin
perform public.checkin_attendee(talk_one, '[email protected]');
raise exception 'front-desk gate allowed a session check-in';
exception when raise_exception then
if sqlerrm not like '%front desk%' then raise; end if;
end;
perform pg_temp.assert_board(
(select not exists (select 1 from public.checkins where contact_email = '[email protected]')),
'front-desk gate blocks session check-in');
foreach excluded_email in array array['[email protected]','[email protected]','[email protected]'] 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
('[email protected]','[email protected]','[email protected]')), 'no excluded award logs');
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', 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":"[email protected]"}', true);
perform public.checkin_attendee(talk_two, '[email protected]');
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', 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', '[email protected]', '[email protected]'),
('a0150000-0000-0000-0000-000000000022', 'a0150000-0000-0000-0000-000000000001', '[email protected]', '[email protected]'),
('a0150000-0000-0000-0000-000000000023', 'a0150000-0000-0000-0000-000000000001', '[email protected]', '[email protected]');
do $$
begin
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', 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 = '[email protected]'), 'eligible peer earns seven with staff connection');
perform pg_temp.assert_board((select points = 0 from public.registrations where contact_email = '[email protected]'), 'staff connection earns zero');
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
perform public.accept_connection('a0150000-0000-0000-0000-000000000022');
perform pg_temp.assert_board((select points = 49 from public.registrations where contact_email = '[email protected]'), 'sender earns connection plus bonus');
perform pg_temp.assert_board((select points = 17 from public.registrations where contact_email = '[email protected]' 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":"[email protected]"}', 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 = '[email protected]'), '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":"[email protected]"}', true);
perform public.adjust_points('[email protected]', -999, 'Floor test');
perform pg_temp.assert_board((select points = 0 from public.registrations where contact_email = '[email protected]'), 'points floored at zero');
perform public.adjust_points('[email protected]', 3, 'Correction');
perform pg_temp.assert_board((select points = 57 from public.registrations where contact_email = '[email protected]'), 'admin adjustment updates player');
perform public.adjust_points('[email protected]', 100, 'Must not award');
perform public.adjust_points('[email protected]', 100, 'Must not award');
perform public._award_points('a0150000-0000-0000-0000-000000000001', '[email protected]', 100, 'admin');
perform pg_temp.assert_board(not exists (select 1 from public.points_log where contact_email in
('[email protected]','[email protected]','[email protected]','[email protected]')), '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 = '[email protected]' 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 = '[email protected]';
select pg_temp.assert_board((select points = 0 from public.registrations where contact_email = '[email protected]'), 'direct staff edits forced to zero');
update public.registrations set type = 'staff' where contact_email = '[email protected]';
select pg_temp.assert_board((select points = 0 from public.registrations where contact_email = '[email protected]'), 'staff promotion clears points');
select pg_temp.assert_board(not exists (select 1 from public.leaderboard where contact_email = '[email protected]'), 'staff promotion removes ranking');
update public.contacts set is_admin = true where email = '[email protected]';
select pg_temp.assert_board((select bool_and(points = 0) from public.registrations where contact_email = '[email protected]'), 'admin promotion clears all event balances');
select pg_temp.assert_board(not exists (select 1 from public.leaderboard where contact_email = '[email protected]'), 'admin promotion removes all rankings');
select pg_temp.assert_board(exists (select 1 from public.points_log where contact_email = '[email protected]'), 'historical audit retained');
update public.registrations set type = 'attendee', points = 7 where contact_email = '[email protected]';
update public.registrations set points = 7 where contact_email = '[email protected]';
update public.contacts set full_name = 'Tied' where email in ('[email protected]', '[email protected]');
select pg_temp.assert_board((select bool_and(full_name = 'Tied') from public.leaderboard where contact_email in ('[email protected]','[email protected]')), 'name changes refresh mirror');
select pg_temp.assert_board((select array_agg(contact_email) = array['[email protected]','[email protected]'] 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', '[email protected]', 'Stray staff', 999),
('a0150000-0000-0000-0000-000000000001', '[email protected]', 'Stray admin', 999),
('a0150000-0000-0000-0000-000000000001', '[email protected]', 'Stray orphan', 999);
select pg_temp.assert_board(not exists (select 1 from public.get_leaderboard() where contact_email in ('[email protected]','[email protected]','[email protected]')), 'read defense excludes stale staff admin and orphan rows');
select public._sync_leaderboard('a0150000-0000-0000-0000-000000000001', '[email protected]');
select pg_temp.assert_board(not exists (select 1 from public.leaderboard where contact_email = '[email protected]'), 'sync removes stale ranking');
delete from public.registrations where contact_email = '[email protected]';
select pg_temp.assert_board(not exists (select 1 from public.leaderboard where contact_email = '[email protected]'), '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":"[email protected]","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', '[email protected]', 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('[email protected]', 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;