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]>
459 lines
20 KiB
PL/PgSQL
459 lines
20 KiB
PL/PgSQL
-- =============================================================================
|
|
-- Convivo — RPC test suite
|
|
--
|
|
-- A self-contained, idempotent test script for the transactional points RPCs,
|
|
-- role guards, and the speaker auto-promote trigger.
|
|
--
|
|
-- HOW IT WORKS
|
|
-- * Everything runs inside a single transaction that ROLLS BACK at the end,
|
|
-- so the seed data is never mutated. Safe to run repeatedly.
|
|
-- * The "current user" is set the same way PostgREST does it:
|
|
-- set_config('request.jwt.claims', '{"email":"..."}', false)
|
|
-- which is what public.current_email() / public.is_admin() read.
|
|
-- * Each test is a DO block that catches its own exceptions and records a
|
|
-- pass/fail row into a temp table, so ALL tests always run and a summary
|
|
-- is always printed (the summary is printed BEFORE the rollback, since the
|
|
-- temp table is dropped by it). The runner (npm run test:rpc) fails the
|
|
-- process if any test failed.
|
|
--
|
|
-- RUN IT
|
|
-- npm run test:rpc (from the repo root)
|
|
-- or manually:
|
|
-- docker exec -i supabase_db_Convivo psql -U postgres \
|
|
-- < supabase/tests/rpc_tests.sql
|
|
-- =============================================================================
|
|
|
|
begin;
|
|
|
|
-- Scratch table for the pass/fail summary (transaction-scoped).
|
|
create temporary table test_results (
|
|
name text,
|
|
passed boolean,
|
|
message text
|
|
);
|
|
|
|
-- Set the "current user" for the following RPC calls (PostgREST-style JWT claim).
|
|
create or replace function set_user(p_email text)
|
|
returns void
|
|
language plpgsql as $$
|
|
begin
|
|
perform set_config('request.jwt.claims', json_build_object('email', p_email)::text, false);
|
|
end;
|
|
$$;
|
|
|
|
-- Fixtures (from the seed):
|
|
-- active event id
|
|
-- staff/admin: [email protected] (is_admin = true)
|
|
-- attendee: [email protected] (QR CVV-ATT-1000, 0 points)
|
|
-- attendee: [email protected] (QR CVV-ATT-1001, 0 points)
|
|
-- a normal session (slot 2) and a service session (slot 4).
|
|
|
|
-- =============================================================================
|
|
-- 1. checkin_attendee: awards +10, marks checked_in
|
|
-- =============================================================================
|
|
do $$
|
|
declare
|
|
v_session uuid;
|
|
v_res_points integer;
|
|
v_points integer;
|
|
v_ok boolean;
|
|
v_msg text;
|
|
begin
|
|
select id into v_session from public.sessions where slot_id = 2 limit 1;
|
|
perform set_user('[email protected]');
|
|
-- Front-desk (welcome) check-in first: a session check-in now requires the
|
|
-- attendee to be checked in to the event.
|
|
perform public.welcome_checkin('CVV-ATT-1000');
|
|
select points into v_res_points from public.checkin_attendee(v_session, 'CVV-ATT-1000');
|
|
select points into v_points from public.registrations
|
|
where contact_email = '[email protected]'
|
|
and event_id = (select id from public.events where is_active limit 1);
|
|
v_ok := (v_res_points = 10 and v_points = 10);
|
|
v_msg := 'expected 10 points, got ' || coalesce(v_res_points::text, 'null');
|
|
insert into test_results values ('checkin awards +10', v_ok, case when v_ok then null else v_msg end);
|
|
|
|
v_ok := exists (select 1 from public.registrations
|
|
where contact_email = '[email protected]' and checked_in);
|
|
v_msg := 'registration not marked checked_in';
|
|
insert into test_results values ('front-desk check-in marks checked_in', v_ok, case when v_ok then null else v_msg end);
|
|
exception when others then
|
|
insert into test_results values ('checkin (1)', false, 'error: ' || sqlerrm);
|
|
end;
|
|
$$;
|
|
|
|
-- =============================================================================
|
|
-- 2. checkin_attendee: idempotent (re-check-in does not double-award)
|
|
-- =============================================================================
|
|
do $$
|
|
declare
|
|
v_session uuid;
|
|
v_res_already boolean;
|
|
v_points integer;
|
|
v_ok boolean;
|
|
v_msg text;
|
|
begin
|
|
select id into v_session from public.sessions where slot_id = 2 limit 1;
|
|
perform set_user('[email protected]');
|
|
select already into v_res_already from public.checkin_attendee(v_session, 'CVV-ATT-1000');
|
|
select points into v_points from public.registrations
|
|
where contact_email = '[email protected]'
|
|
and event_id = (select id from public.events where is_active limit 1);
|
|
v_ok := v_res_already;
|
|
v_msg := 'expected already=true on second check-in';
|
|
insert into test_results values ('re-check-in flagged already', v_ok, case when v_ok then null else v_msg end);
|
|
|
|
v_ok := (v_points = 10);
|
|
v_msg := 'expected still 10 points, got ' || v_points;
|
|
insert into test_results values ('re-check-in no double-award', v_ok, case when v_ok then null else v_msg end);
|
|
exception when others then
|
|
insert into test_results values ('checkin (2)', false, 'error: ' || sqlerrm);
|
|
end;
|
|
$$;
|
|
|
|
-- =============================================================================
|
|
-- 3. checkin_attendee: service sessions cannot be checked in
|
|
-- =============================================================================
|
|
do $$
|
|
declare
|
|
v_session uuid;
|
|
v_err text;
|
|
begin
|
|
select id into v_session from public.sessions
|
|
where slot_id in (select id from public.slots where type = 'service_session')
|
|
limit 1;
|
|
perform set_user('[email protected]');
|
|
-- Front-desk check-in first so the service-session guard (not the front-desk
|
|
-- gate) is what rejects this scan.
|
|
perform public.welcome_checkin('CVV-ATT-1001');
|
|
begin
|
|
perform public.checkin_attendee(v_session, 'CVV-ATT-1001');
|
|
insert into test_results values ('service session check-in rejected', false,
|
|
'expected an exception for a service session');
|
|
exception
|
|
when others then
|
|
get stacked diagnostics v_err = message_text;
|
|
insert into test_results values ('service session check-in rejected',
|
|
v_err like '%service sessions cannot be checked in%',
|
|
case when v_err like '%service sessions cannot be checked in%'
|
|
then null else 'unexpected error: ' || v_err end);
|
|
end;
|
|
exception when others then
|
|
insert into test_results values ('checkin (3)', false, 'error: ' || sqlerrm);
|
|
end;
|
|
$$;
|
|
|
|
-- =============================================================================
|
|
-- 4. checkin_attendee: role guard (non-admin rejected)
|
|
-- =============================================================================
|
|
do $$
|
|
declare
|
|
v_session uuid;
|
|
v_err text;
|
|
begin
|
|
select id into v_session from public.sessions where slot_id = 2 limit 1;
|
|
perform set_user('[email protected]'); -- an attendee, not an admin
|
|
begin
|
|
perform public.checkin_attendee(v_session, 'CVV-ATT-1001');
|
|
insert into test_results values ('check-in role guard (non-admin)', false,
|
|
'expected "staff or admin only"');
|
|
exception
|
|
when others then
|
|
get stacked diagnostics v_err = message_text;
|
|
insert into test_results values ('check-in role guard (non-admin)',
|
|
v_err = 'staff or admin only',
|
|
case when v_err = 'staff or admin only'
|
|
then null else 'unexpected error: ' || v_err end);
|
|
end;
|
|
exception when others then
|
|
insert into test_results values ('checkin (4)', false, 'error: ' || sqlerrm);
|
|
end;
|
|
$$;
|
|
|
|
-- =============================================================================
|
|
-- 5. submit_feedback: +5 rating only, idempotent on re-submit
|
|
-- =============================================================================
|
|
do $$
|
|
declare
|
|
v_session uuid;
|
|
v_res_earned integer;
|
|
v_points integer;
|
|
v_ok boolean;
|
|
v_msg text;
|
|
begin
|
|
select id into v_session from public.sessions where slot_id = 2 limit 1;
|
|
perform set_user('[email protected]');
|
|
perform public.checkin_attendee(v_session, 'CVV-ATT-1001');
|
|
|
|
-- Rating only -> +5. (felix already has +10 from the check-in above, so 15.)
|
|
perform set_user('[email protected]');
|
|
select earned into v_res_earned from public.submit_feedback(v_session, 4, null);
|
|
select points into v_points from public.registrations
|
|
where contact_email = '[email protected]'
|
|
and event_id = (select id from public.events where is_active limit 1);
|
|
v_ok := (v_res_earned = 5 and v_points = 15);
|
|
v_msg := 'expected earned=5/points=15, got earned=' || v_res_earned || ' points=' || v_points;
|
|
insert into test_results values ('feedback rating-only earns +5', v_ok, case when v_ok then null else v_msg end);
|
|
|
|
-- Re-submit (idempotent) -> no extra points (stays at 15).
|
|
select earned into v_res_earned from public.submit_feedback(v_session, 5, null);
|
|
select points into v_points from public.registrations
|
|
where contact_email = '[email protected]'
|
|
and event_id = (select id from public.events where is_active limit 1);
|
|
v_ok := (v_res_earned = 0 and v_points = 15);
|
|
v_msg := 'expected earned=0/points=15, got earned=' || v_res_earned || ' points=' || v_points;
|
|
insert into test_results values ('feedback re-submit idempotent', v_ok, case when v_ok then null else v_msg end);
|
|
exception when others then
|
|
insert into test_results values ('feedback (5)', false, 'error: ' || sqlerrm);
|
|
end;
|
|
$$;
|
|
|
|
-- =============================================================================
|
|
-- 6. submit_feedback: requires check-in
|
|
-- =============================================================================
|
|
do $$
|
|
declare
|
|
v_session uuid;
|
|
v_err text;
|
|
begin
|
|
select id into v_session from public.sessions where slot_id = 3 limit 1;
|
|
perform set_user('[email protected]'); -- not checked into slot 3
|
|
begin
|
|
perform public.submit_feedback(v_session, 4, null);
|
|
insert into test_results values ('feedback requires check-in', false,
|
|
'expected "you must be checked in"');
|
|
exception
|
|
when others then
|
|
get stacked diagnostics v_err = message_text;
|
|
insert into test_results values ('feedback requires check-in',
|
|
v_err like '%must be checked in%',
|
|
case when v_err like '%must be checked in%'
|
|
then null else 'unexpected error: ' || v_err end);
|
|
end;
|
|
exception when others then
|
|
insert into test_results values ('feedback (6)', false, 'error: ' || sqlerrm);
|
|
end;
|
|
$$;
|
|
|
|
-- =============================================================================
|
|
-- 7. accept_connection: +5 to both parties (+ talk networking bonus, since
|
|
-- lena and felix were both checked into the slot-2 session above)
|
|
-- =============================================================================
|
|
do $$
|
|
declare
|
|
v_conn_id uuid;
|
|
v_from integer;
|
|
v_to integer;
|
|
v_shared boolean;
|
|
v_ok boolean;
|
|
v_msg text;
|
|
begin
|
|
-- lena -> felix (both were checked into the slot-2 session).
|
|
perform set_user('[email protected]');
|
|
select id into v_conn_id from public.create_connection_request('CVV-ATT-1001');
|
|
|
|
-- felix accepts.
|
|
perform set_user('[email protected]');
|
|
select shared_talk into v_shared from public.accept_connection(v_conn_id);
|
|
|
|
select points into v_from from public.registrations
|
|
where contact_email = '[email protected]'
|
|
and event_id = (select id from public.events where is_active limit 1);
|
|
select points into v_to from public.registrations
|
|
where contact_email = '[email protected]'
|
|
and event_id = (select id from public.events where is_active limit 1);
|
|
-- lena had 10 from the check-in test; +5 connection + 2 talk bonus = 17.
|
|
-- felix had 15 (check-in + feedback); +5 connection + 2 talk bonus = 22.
|
|
v_ok := (v_from = 17 and v_to = 22);
|
|
v_msg := 'expected lena=17/felix=22, got lena=' || v_from || ' felix=' || v_to;
|
|
insert into test_results values ('connection accepted +5 each (+ talk bonus)', v_ok, case when v_ok then null else v_msg end);
|
|
|
|
v_ok := v_shared;
|
|
v_msg := 'expected shared_talk=true (both checked into slot 2)';
|
|
insert into test_results values ('accept_connection shared_talk flag', v_ok, case when v_ok then null else v_msg end);
|
|
exception when others then
|
|
insert into test_results values ('connection (7)', false, 'error: ' || sqlerrm);
|
|
end;
|
|
$$;
|
|
|
|
-- =============================================================================
|
|
-- 7b. accept_connection: NO talk bonus when the parties share no check-in
|
|
-- =============================================================================
|
|
do $$
|
|
declare
|
|
v_conn_id uuid;
|
|
v_from integer;
|
|
v_to integer;
|
|
v_shared boolean;
|
|
v_ok boolean;
|
|
v_msg text;
|
|
begin
|
|
-- Two attendees who were never checked in: no shared talk.
|
|
-- [email protected] (1003) -> [email protected] (1004).
|
|
perform set_user('[email protected]');
|
|
select id into v_conn_id from public.create_connection_request('CVV-ATT-1004');
|
|
|
|
-- emma accepts.
|
|
perform set_user('[email protected]');
|
|
select shared_talk into v_shared from public.accept_connection(v_conn_id);
|
|
|
|
select points into v_from from public.registrations
|
|
where contact_email = '[email protected]'
|
|
and event_id = (select id from public.events where is_active limit 1);
|
|
select points into v_to from public.registrations
|
|
where contact_email = '[email protected]'
|
|
and event_id = (select id from public.events where is_active limit 1);
|
|
-- +5 connection each, NO talk bonus (no shared check-in).
|
|
v_ok := (v_from = 5 and v_to = 5);
|
|
v_msg := 'expected jonas=5/emma=5 (no bonus), got jonas=' || v_from || ' emma=' || v_to;
|
|
insert into test_results values ('no talk bonus without shared check-in', v_ok, case when v_ok then null else v_msg end);
|
|
|
|
v_ok := (v_shared = false);
|
|
v_msg := 'expected shared_talk=false';
|
|
insert into test_results values ('shared_talk=false without shared check-in', v_ok, case when v_ok then null else v_msg end);
|
|
exception when others then
|
|
insert into test_results values ('connection (7b)', false, 'error: ' || sqlerrm);
|
|
end;
|
|
$$;
|
|
|
|
-- =============================================================================
|
|
-- 7c. accept_connection: talk bonus disabled when talk_networking_bonus = 0
|
|
-- =============================================================================
|
|
do $$
|
|
declare
|
|
v_session uuid;
|
|
v_conn_id uuid;
|
|
v_from integer;
|
|
v_to integer;
|
|
v_ok boolean;
|
|
v_msg text;
|
|
begin
|
|
-- Disable the bonus for the active event.
|
|
update public.events set talk_networking_bonus = 0
|
|
where id = (select id from public.events where is_active limit 1);
|
|
|
|
-- [email protected] (1002) and [email protected] (1005): check both
|
|
-- into the slot-2 session so they share a check-in, then connect.
|
|
select id into v_session from public.sessions where slot_id = 2 limit 1;
|
|
perform set_user('[email protected]');
|
|
perform public.checkin_attendee(v_session, 'CVV-ATT-1002');
|
|
perform public.checkin_attendee(v_session, 'CVV-ATT-1005');
|
|
|
|
perform set_user('[email protected]');
|
|
select id into v_conn_id from public.create_connection_request('CVV-ATT-1005');
|
|
perform set_user('[email protected]');
|
|
perform public.accept_connection(v_conn_id);
|
|
|
|
select points into v_from from public.registrations
|
|
where contact_email = '[email protected]'
|
|
and event_id = (select id from public.events where is_active limit 1);
|
|
select points into v_to from public.registrations
|
|
where contact_email = '[email protected]'
|
|
and event_id = (select id from public.events where is_active limit 1);
|
|
-- +10 check-in + 5 connection each, NO bonus (disabled).
|
|
v_ok := (v_from = 15 and v_to = 15);
|
|
v_msg := 'expected mia=15/paul=15 (bonus disabled), got mia=' || v_from || ' paul=' || v_to;
|
|
insert into test_results values ('talk bonus disabled when bonus=0', v_ok, case when v_ok then null else v_msg end);
|
|
exception when others then
|
|
insert into test_results values ('connection (7c)', false, 'error: ' || sqlerrm);
|
|
end;
|
|
$$;
|
|
|
|
-- =============================================================================
|
|
-- 8. adjust_points: admin only
|
|
-- =============================================================================
|
|
do $$
|
|
declare
|
|
v_err text;
|
|
begin
|
|
perform set_user('[email protected]'); -- not an admin
|
|
begin
|
|
perform public.adjust_points('[email protected]', 5, 'test');
|
|
insert into test_results values ('adjust_points role guard (non-admin)', false,
|
|
'expected "admin only"');
|
|
exception
|
|
when others then
|
|
get stacked diagnostics v_err = message_text;
|
|
insert into test_results values ('adjust_points role guard (non-admin)',
|
|
v_err = 'admin only',
|
|
case when v_err = 'admin only'
|
|
then null else 'unexpected error: ' || v_err end);
|
|
end;
|
|
exception when others then
|
|
insert into test_results values ('adjust_points (8)', false, 'error: ' || sqlerrm);
|
|
end;
|
|
$$;
|
|
|
|
-- =============================================================================
|
|
-- 9. Speaker auto-promote trigger (attendee -> speaker, never overrides staff)
|
|
-- =============================================================================
|
|
do $$
|
|
declare
|
|
v_session uuid;
|
|
v_type public.registration_type;
|
|
v_ok boolean;
|
|
v_msg text;
|
|
begin
|
|
select id into v_session from public.sessions where slot_id = 2 limit 1;
|
|
-- lena is an attendee; adding her as a speaker should promote her.
|
|
insert into public.session_speakers (session_id, contact_email)
|
|
values (v_session, '[email protected]');
|
|
select "type" into v_type from public.registrations
|
|
where contact_email = '[email protected]'
|
|
and event_id = (select id from public.events where is_active limit 1);
|
|
v_ok := (v_type = 'speaker');
|
|
v_msg := 'expected type=speaker, got ' || coalesce(v_type::text, 'null');
|
|
insert into test_results values ('speaker auto-promote (attendee->speaker)', v_ok, case when v_ok then null else v_msg end);
|
|
|
|
-- A staff registration must NOT be overridden by the promote trigger.
|
|
insert into public.session_speakers (session_id, contact_email)
|
|
values (v_session, '[email protected]');
|
|
select "type" into v_type from public.registrations
|
|
where contact_email = '[email protected]'
|
|
and event_id = (select id from public.events where is_active limit 1);
|
|
v_ok := (v_type = 'staff');
|
|
v_msg := 'expected type=staff, got ' || coalesce(v_type::text, 'null');
|
|
insert into test_results values ('speaker auto-promote never overrides staff', v_ok, case when v_ok then null else v_msg end);
|
|
exception when others then
|
|
insert into test_results values ('speaker promote (9)', false, 'error: ' || sqlerrm);
|
|
end;
|
|
$$;
|
|
|
|
-- =============================================================================
|
|
-- 10. Leaderboard excludes staff
|
|
-- =============================================================================
|
|
do $$
|
|
declare
|
|
v_count integer;
|
|
v_ok boolean;
|
|
v_msg text;
|
|
begin
|
|
-- Give amelia (staff) some points, then confirm she is not on the board.
|
|
perform set_user('[email protected]');
|
|
perform public.adjust_points('[email protected]', 50, 'test');
|
|
select count(*) into v_count from public.leaderboard
|
|
where contact_email = '[email protected]'
|
|
and event_id = (select id from public.events where is_active limit 1);
|
|
v_ok := (v_count = 0);
|
|
v_msg := 'expected 0 staff rows on leaderboard, got ' || v_count;
|
|
insert into test_results values ('leaderboard excludes staff', v_ok, case when v_ok then null else v_msg end);
|
|
exception when others then
|
|
insert into test_results values ('leaderboard (10)', false, 'error: ' || sqlerrm);
|
|
end;
|
|
$$;
|
|
|
|
-- =============================================================================
|
|
-- Summary (printed BEFORE the rollback, since the temp table is dropped by it)
|
|
-- =============================================================================
|
|
select name, passed, message
|
|
from test_results
|
|
order by name;
|
|
|
|
select
|
|
count(*) as total,
|
|
count(*) filter (where passed) as passed,
|
|
count(*) filter (where not passed) as failed
|
|
from test_results;
|
|
|
|
-- Roll back so the seed is never mutated.
|
|
rollback;
|