import { test, expect, type Page } from '@playwright/test'; import { APP_URL, EVENT_ID, ID } from '../src/helpers/config'; import { login } from '../src/helpers/auth'; import { db, psql, psqlScalar, sqlQuote } from '../src/helpers/db'; import { AppPage } from '../src/pages/AppPage'; import { ScanPage } from '../src/pages/ScanPage'; /** * Points awarded by the two check-in paths, measured against the real database. * * The bug this guards: the front-desk (welcome) check-in awarded NOTHING while a * session scan awarded +10, so "check me in" paid out differently depending on * which check-in a person happened to get. On top of that, the scanner sheet * claimed "+10 pts · now 0" for staff accounts, which never earn anything. * * Every assertion is the DB row, not just the UI text — the sheet and the score * have to agree. */ const EMAIL = 'points-flow-test@example.test'; const TOKEN = 'CVV-POINTS-FLOW-01'; const STAFF_EMAIL = 'points-staff-test@example.test'; const STAFF_TOKEN = 'CVV-POINTS-STAFF-01'; function someSessionId(): string { return psqlScalar(`select s.id from public.sessions s join public.slots sl on sl.id = s.slot_id where s.event_id = ${sqlQuote(EVENT_ID)} and sl.type <> 'service_session' order by sl.id, s.id limit 1`); } function provision(email: string, token: string, type: 'attendee' | 'staff'): void { psql(`insert into public.contacts (email, full_name, is_admin) values (${sqlQuote(email)}, ${sqlQuote(type === 'staff' ? 'Staff Points Test' : 'Points Flow Attendee')}, false) on conflict (email) do update set full_name = excluded.full_name, is_admin = false`); psql(`insert into public.registrations (event_id, contact_email, type, checked_in, points, qr_token) values (${sqlQuote(EVENT_ID)}, ${sqlQuote(email)}, ${sqlQuote(type)}, false, 0, ${sqlQuote(token)}) on conflict (event_id, contact_email) do update set checked_in = false, points = 0, qr_token = excluded.qr_token, type = excluded.type`); psql(`delete from public.checkins where contact_email = ${sqlQuote(email)}`); psql(`delete from public.points_log where contact_email = ${sqlQuote(email)}`); psql(`delete from pgmq.q_badge_print where message->'sample'->>'email' = ${sqlQuote(email)}`); } function cleanup(email: string): void { psql(`delete from public.checkins where contact_email = ${sqlQuote(email)}`); psql(`delete from public.points_log where contact_email = ${sqlQuote(email)}`); psql(`delete from pgmq.q_badge_print where message->'sample'->>'email' = ${sqlQuote(email)}`); psql(`delete from public.registrations where contact_email = ${sqlQuote(email)}`); psql(`delete from public.contacts where email = ${sqlQuote(email)}`); } const pointsOf = (email: string): number => db.registration(email)?.points ?? -1; /** * Toggle the admin check-in switch through the real RPC. psql has no JWT and * `is_admin()` reads `auth.jwt()`, so the claims are supplied exactly as * PostgREST would supply them. Each psql() call is its own session, so `set * local role` cannot leak, and the write is committed (this test asserts on * persisted points, not on a rollback). */ const ADMIN_CLAIMS = '{"sub":"00000000-0000-0000-0000-000000000000","email":"stephan@appe.at","role":"authenticated","exp":1893456000}'; function toggleCheckedIn(email: string, checkedIn: boolean, field: 'points_removed' | 'points_added'): number { const out = psql(`begin; set local role authenticated; set local request.jwt.claims = ${sqlQuote(ADMIN_CLAIMS)}; select ${field} from public.set_registration_checked_in( (select id from public.registrations where contact_email = ${sqlQuote(email)} and event_id = ${sqlQuote(EVENT_ID)}), ${checkedIn}); commit;`); // psql echoes BEGIN/COMMIT around the row, so take the numeric line. const value = out.split('\n').map((l) => l.trim()).filter((l) => /^-?\d+$/.test(l)).pop(); if (value === undefined) throw new Error(`set_registration_checked_in returned no number: ${out}`); return Number(value); } test.describe('Check-in points (+10 per path)', () => { test.beforeEach(() => { provision(EMAIL, TOKEN, 'attendee'); provision(STAFF_EMAIL, STAFF_TOKEN, 'staff'); }); test.afterAll(() => { cleanup(EMAIL); cleanup(STAFF_EMAIL); }); async function openScanner(page: Page) { const app = new AppPage(page, APP_URL); const scan = new ScanPage(page); await login(page, APP_URL, ID.staff); await app.waitForAdmitted(); await app.gotoTab('scan'); await scan.setStaffMode(); return scan; } test('CONV-PTS-001 the welcome (front desk) check-in awards +10 and says so', async ({ page }) => { const scan = await openScanner(page); expect(pointsOf(EMAIL), 'starts at zero').toBe(0); await scan.setWelcomeMode(); // The confirmation sheet promises the points before the write happens. await scan.scanCode(TOKEN); await expect(scan.welcomeConfirm()).toContainText(/awards \+10 points/i); await scan.confirmWelcome(); await expect(scan.welcomeResult()).toBeVisible(); await expect(scan.welcomePoints()).toHaveText('+10 pts · now 10'); expect(pointsOf(EMAIL)).toBe(10); expect(db.isWelcomeCheckedIn(EMAIL)).toBe(true); // Audited, so the score can be explained later. expect(psqlScalar(`select category || '=' || delta from public.points_log where contact_email = ${sqlQuote(EMAIL)} order by created_at desc limit 1`)).toBe('checkin=10'); }); test('CONV-PTS-002 a second welcome adds nothing, and a session scan then totals 20', async ({ page }) => { const scan = await openScanner(page); const session = someSessionId(); expect(session, 'the active event needs at least one checkable session').toBeTruthy(); await scan.setWelcomeMode(); await scan.scanWelcome(TOKEN); expect(pointsOf(EMAIL)).toBe(10); await scan.dismissResult(); // Same person welcomed twice: the sheet must say "no extra points", not "+10". await scan.scanWelcome(TOKEN); await expect(scan.welcomePoints()).toContainText(/no extra points/i); expect(pointsOf(EMAIL)).toBe(10); await scan.dismissResult(); // Now a session scan — the second, independent +10. await scan.setSessionMode(); await scan.selectSession(session); await scan.scanCode(TOKEN); await expect(scan.sessionResult()).toBeVisible(); await expect(scan.sessionPoints()).toHaveText('+10 pts · now 20'); expect(pointsOf(EMAIL)).toBe(20); }); test('CONV-PTS-003 a staff pass checks in fine but earns nothing, visibly', async ({ page }) => { const scan = await openScanner(page); await scan.setWelcomeMode(); await scan.scanCode(STAFF_TOKEN); await expect(scan.welcomeConfirm()).toContainText(/does not earn points/i); await scan.confirmWelcome(); await expect(scan.welcomePoints()).toContainText(/staff earn no points/i); await expect(scan.welcomePoints()).not.toContainText('+10'); // Checked in, zero points — the exclusion is deliberate, and now it says so. expect(db.isWelcomeCheckedIn(STAFF_EMAIL)).toBe(true); expect(pointsOf(STAFF_EMAIL)).toBe(0); expect(psqlScalar(`select count(*) from public.points_log where contact_email = ${sqlQuote(STAFF_EMAIL)}`)).toBe('0'); }); test('CONV-PTS-004 re-checking via the admin toggle restores the points the check-in rows justify', async ({ page }) => { const scan = await openScanner(page); const session = someSessionId(); expect(session).toBeTruthy(); await scan.setWelcomeMode(); await scan.scanWelcome(TOKEN); // Wait for the sheet before dismissing: `dismissResult` only clicks when the // button is already there, and dismissing too early leaves the backdrop up. await expect(scan.welcomeResult()).toBeVisible(); await scan.dismissResult(); await scan.setSessionMode(); await scan.selectSession(session); await scan.scanCode(TOKEN); await expect(scan.sessionResult()).toBeVisible(); expect(pointsOf(EMAIL)).toBe(20); // Admin switches the pass off, then back on — through the real RPC, which is // the only admin path and the one that lost points permanently before. expect(toggleCheckedIn(EMAIL, false, 'points_removed')).toBe(20); expect(pointsOf(EMAIL)).toBe(0); expect(toggleCheckedIn(EMAIL, true, 'points_added')).toBe(20); expect(pointsOf(EMAIL)).toBe(20); // A third click must not farm points: the target is derived from the // check-in rows that exist, never from the number of toggles. expect(toggleCheckedIn(EMAIL, true, 'points_added')).toBe(0); expect(pointsOf(EMAIL)).toBe(20); }); });