import { psql, sqlQuote } from './db'; import { EVENT_ID, ID } from './config'; /** * Test-state reset. The suite shares the demo database, so every test resets * the rows it touches in `beforeEach`. These resets are idempotent and * targeted (they only touch the identities the suite uses), so they never * disturb the rest of the demo data. * * `resetAll()` is the full reset used by most tests. Individual helpers are * available for tests that only need a slice of it. */ const RESET_CONTACTS = [ ID.attendeeNew.email, ID.attendeeReturning.email, ID.attendeeA.email, ID.attendeeB.email, ID.attendeeC.email, ID.attendeeD.email, ID.speaker.email, ID.sponsor.email, ]; /** Reset the welcome-check-in flag for a contact. */ export function setWelcomeCheckedIn(contactEmail: string, checkedIn: boolean): void { psql( `update public.registrations set checked_in = ${checkedIn} where contact_email = ${sqlQuote(contactEmail)} and event_id = ${sqlQuote(EVENT_ID)}`, ); } /** Reset session check-ins for a contact. */ export function clearSessionCheckins(contactEmail: string): void { psql( `delete from public.checkins where contact_email = ${sqlQuote(contactEmail)}`, ); } /** * Set a session check-in for a contact (direct DB). Used by tests that need a * specific session check-in without driving the full staff-scan flow (e.g. the * feedback tests, where check-in is a precondition, not the subject). */ export function setSessionCheckin(contactEmail: string, sessionId: string, staffEmail: string): void { psql( `insert into public.checkins (event_id, contact_email, session_id, staff_email) values (${sqlQuote(EVENT_ID)}, ${sqlQuote(contactEmail)}, ${sqlQuote(sessionId)}, ${sqlQuote(staffEmail)}) on conflict (event_id, contact_email, session_id) do nothing`, ); } /** Reset all connections involving a contact. */ export function clearConnections(contactEmail: string): void { psql( `delete from public.connections where from_email = ${sqlQuote(contactEmail)} or to_email = ${sqlQuote(contactEmail)}`, ); } /** Reset feedback for a contact. */ export function clearFeedback(contactEmail: string): void { psql( `delete from public.feedback where contact_email = ${sqlQuote(contactEmail)}`, ); } /** Reset a contact's points + audit trail + leaderboard mirror. */ export function resetPoints(contactEmail: string): void { psql( `update public.registrations set points = 0 where contact_email = ${sqlQuote(contactEmail)}`, ); psql( `delete from public.points_log where contact_email = ${sqlQuote(contactEmail)}`, ); psql( `delete from public.leaderboard where contact_email = ${sqlQuote(contactEmail)}`, ); } /** Reset a contact's profile visibility settings. */ export function resetProfile(contactEmail: string): void { psql( `update public.contacts set show_email = false, show_linkedin = false, linkedin_url = null where email = ${sqlQuote(contactEmail)}`, ); } /** Clear a session's material (storage object + material_path). */ export function clearSessionMaterial(sessionId: string): void { psql( `update public.sessions set material_path = null where id = ${sqlQuote(sessionId)}`, ); } /** * Ensure the speaker can log in via the normal QR flow. The seed leaves the * speaker's `qr_token` NULL (a real gap — see findings), so we assign a token * and re-hash it as the auth password. */ export function ensureSpeakerQrToken(): void { psql( `update public.registrations set qr_token = ${sqlQuote(ID.speaker.qrToken)} where contact_email = ${sqlQuote(ID.speaker.email)} and event_id = ${sqlQuote(EVENT_ID)}`, ); psql( `update auth.users set encrypted_password = extensions.crypt(${sqlQuote(ID.speaker.qrToken)}, extensions.gen_salt('bf')) where email = ${sqlQuote(ID.speaker.email)}`, ); } /** * Ensure the sponsor identity exists (contact + registration + auth user). * No sponsor exists in the seed, so we create one. Idempotent. */ export function ensureSponsor(): void { const { email, qrToken } = ID.sponsor; // Auth user (QR token = password). Upsert by email so the password hash is // always re-synced to the current QR token on every reset (idempotent). // NOTE: GoTrue's user query fails to scan a NULL in any of the token columns // (confirmation_token, email_change_token_new, recovery_token, email_change, // reauthentication_token, phone_change_token) → 500 "Database error querying // schema". So we set them all to '' (matching how seed users are stored). psql( `insert into auth.users (instance_id, id, aud, role, email, encrypted_password, email_confirmed_at, confirmation_token, email_change_token_new, email_change, recovery_token, reauthentication_token, phone_change_token, raw_app_meta_data, raw_user_meta_data, created_at, updated_at) values ('00000000-0000-0000-0000-000000000000', gen_random_uuid(), 'authenticated', 'authenticated', ${sqlQuote(email)}, extensions.crypt(${sqlQuote(qrToken)}, extensions.gen_salt('bf')), now(), '', '', '', '', '', '', '{"provider":"email","providers":["email"]}', '{"full_name":"Demo Sponsor"}', now(), now()) on conflict (email) where is_sso_user = false do update set encrypted_password = extensions.crypt(${sqlQuote(qrToken)}, extensions.gen_salt('bf')), confirmation_token = '', email_change_token_new = '', email_change = '', recovery_token = '', reauthentication_token = '', phone_change_token = '', updated_at = now()`, ); // Contact. psql( `insert into public.contacts (email, full_name, linkedin_url, show_email, show_linkedin, is_admin, password) values (${sqlQuote(email)}, 'Demo Sponsor', null, false, false, false, null) on conflict (email) do nothing`, ); // Registration (sponsor type). psql( `insert into public.registrations (event_id, contact_email, type, checked_in, points, qr_token) values (${sqlQuote(EVENT_ID)}, ${sqlQuote(email)}, 'sponsor', false, 0, ${sqlQuote(qrToken)}) on conflict (event_id, contact_email) do update set type = 'sponsor', qr_token = ${sqlQuote(qrToken)}`, ); } /** * The full reset. Sets the known baseline the tests expect: * - ATTENDEE_NEW: not welcome-checked-in * - ATTENDEE_RETURNING: welcome-checked-in * - all: no session check-ins, no connections, no feedback, 0 points, profile reset * - speaker: QR token assigned * - sponsor: created */ export function resetAll(): void { // Welcome check-in baseline. setWelcomeCheckedIn(ID.attendeeNew.email, false); setWelcomeCheckedIn(ID.attendeeReturning.email, true); setWelcomeCheckedIn(ID.attendeeA.email, true); setWelcomeCheckedIn(ID.attendeeB.email, true); setWelcomeCheckedIn(ID.attendeeC.email, true); setWelcomeCheckedIn(ID.attendeeD.email, true); setWelcomeCheckedIn(ID.speaker.email, true); setWelcomeCheckedIn(ID.sponsor.email, true); // Clear per-contact state. for (const c of RESET_CONTACTS) { clearSessionCheckins(c); clearConnections(c); clearFeedback(c); resetPoints(c); resetProfile(c); } // Clear material on the speaker's session. clearSessionMaterial('37ab7060-3d81-4221-9322-6971e97d1175'); // Temporary identities. ensureSpeakerQrToken(); ensureSponsor(); }