import { test, expect } from './base'; import { APP_URL, EVENT_ID, ID } from '../src/helpers/config'; import { login } from '../src/helpers/auth'; import { db, psql, sqlQuote } from '../src/helpers/db'; import { ScanPage } from '../src/pages/ScanPage'; /** * Welcome check-in prints `events.badge_copies` labels (1..4, default 1), and * the confirmation popup states that number so staff are not surprised by a * small run of labels. A manual reprint always prints one, and a second welcome * scan of an already-checked-in attendee prints nothing. * * The suite provisions its own attendee + badge layout: printing depends on the * event having a layout with elements, which is operator data and not seeded. */ const EMAIL = 'badge-copies-test@example.test'; const TOKEN = 'CVV-BADGE-COPIES-01'; const LAYOUT = 'e2e badge copies layout'; /** One QR element is enough for a printable payload. */ const ELEMENTS = JSON.stringify([{ id: 'e2e-qr', type: 'qr', content: 'qr_token', static_value: '', x: 2, y: 2, width: 24, height: 24, rotation: 0, }]); function provision(copies: number): void { psql(`insert into public.contacts (email, full_name, is_admin) values (${sqlQuote(EMAIL)}, 'Copies Test', false) on conflict (email) do update set full_name = excluded.full_name`); psql(`insert into public.registrations (event_id, contact_email, type, checked_in, points, qr_token) values (${sqlQuote(EVENT_ID)}, ${sqlQuote(EMAIL)}, 'attendee', false, 0, ${sqlQuote(TOKEN)}) on conflict (event_id, contact_email) do update set checked_in = false, points = 0, qr_token = excluded.qr_token`); psql(`insert into public.badge_layouts (event_id, name, is_default, page_width_mm, page_height_mm, elements) values (${sqlQuote(EVENT_ID)}, ${sqlQuote(LAYOUT)}, false, 101, 30, ${sqlQuote(ELEMENTS)}::jsonb) on conflict do nothing`); psql(`update public.events set badge_copies = ${copies}`); clearJobs(); } function clearJobs(): void { psql(`delete from pgmq.q_badge_print where message->'sample'->>'email' = ${sqlQuote(EMAIL)}`); } const queuedLabels = (): number => Number(psql(`select count(*) from pgmq.q_badge_print where message->'sample'->>'email' = ${sqlQuote(EMAIL)}`) || '0'); function teardown(): void { clearJobs(); psql(`delete from public.badge_layouts where name = ${sqlQuote(LAYOUT)}`); psql(`delete from public.checkins where contact_email = ${sqlQuote(EMAIL)}`); psql(`delete from public.registrations where contact_email = ${sqlQuote(EMAIL)}`); psql(`delete from public.contacts where email = ${sqlQuote(EMAIL)}`); psql(`update public.events set badge_copies = 1`); } test.describe('Welcome check-in badge copies', () => { test.afterAll(() => teardown()); test('CONV-COPIES-001 a welcome check-in queues badge_copies labels', async ({ page }) => { const scan = new ScanPage(page); provision(3); await login(page, APP_URL, ID.staff); await page.getByTestId('tab-scan').click(); await scan.setStaffMode(); await scan.setWelcomeMode(); await scan.scanCode(TOKEN); await expect(scan.welcomeConfirm()).toBeVisible(); // The popup states the real count instead of promising a single label. await expect(scan.welcomeConfirm()).toContainText(/their badge 3 times/i); await scan.confirmWelcome(); await expect(scan.welcomeResult()).toBeVisible(); expect(queuedLabels()).toBe(3); }); test('CONV-COPIES-002 with copies = 1 the popup keeps the single-label wording', async ({ page }) => { const scan = new ScanPage(page); provision(1); await login(page, APP_URL, ID.staff); await page.getByTestId('tab-scan').click(); await scan.setStaffMode(); await scan.setWelcomeMode(); await scan.scanCode(TOKEN); await expect(scan.welcomeConfirm()).toContainText(/label/i); await expect(scan.welcomeConfirm()).not.toContainText(/badge 1 times/i); await scan.confirmWelcome(); await expect(scan.welcomeResult()).toBeVisible(); expect(queuedLabels()).toBe(1); }); test('CONV-COPIES-003 welcoming an already checked-in attendee prints nothing', async ({ page }) => { const scan = new ScanPage(page); provision(4); // even at the maximum, a repeat must not print await login(page, APP_URL, ID.staff); await page.getByTestId('tab-scan').click(); await scan.setStaffMode(); await scan.setWelcomeMode(); // First welcome prints 4. await scan.scanCode(TOKEN); await scan.confirmWelcome(); await expect(scan.welcomeResult()).toBeVisible(); expect(queuedLabels()).toBe(4); await scan.dismissResult(); // Second welcome: flagged as already checked in, nothing added to the queue. await scan.scanCode(TOKEN); await expect(scan.welcomeConfirm()).toContainText(/Already checked in/i); await scan.confirmWelcome(); await expect(scan.welcomeResult()).toBeVisible(); expect(queuedLabels()).toBe(4); expect(db.isWelcomeCheckedIn(EMAIL)).toBe(true); }); });