import { test, expect, type Page } from '@playwright/test'; const adminUrl = process.env.CONVIVO_ADMIN_URL || 'http://localhost:3001'; const eventId = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa'; const user = { id: '99999999-9999-4999-8999-999999999999', email: 'admin@appe.at', aud: 'authenticated', role: 'authenticated', app_metadata: {}, user_metadata: {} }; const jane = { id: '11111111-1111-4111-8111-111111111111', event_id: eventId, contact_email: 'jane@example.com', type: 'attendee', qr_token: 'CV-JANE-01', external_id: null, import_source: null, checked_in: true, points: 3 }; const sam = { id: '22222222-2222-4222-8222-222222222222', event_id: eventId, contact_email: 'sam@example.com', type: 'speaker', qr_token: 'CV-SAM-02', external_id: null, import_source: null, checked_in: false, points: 12 }; const layout = { id: 'l-default', event_id: eventId, name: 'Default', is_default: true, page_width_mm: 103, page_height_mm: 194, created_at: '2026-10-18T10:00:00Z', updated_at: '2026-10-18T10:00:00Z', elements: [{ id: 't1', type: 'text', x: 8, y: 8, width: 60, height: 12, rotation: 0, content: 'attendee_name', text: 'fallback', font: 'Open Sans', font_size: 14, line_height: 1.2, color: '#000000', bold: false, italic: false, align_h: 'left', align_v: 'middle', transform: 'none' }], }; const minutesAgo = (m: number) => new Date(Date.now() - m * 60_000).toISOString(); function station(overrides: Record = {}) { return { id: 'st-0001', name: 'Entrance printer', created_at: '2026-10-18T09:00:00Z', revoked_at: null, last_seen_at: minutesAgo(0.05), last_status: { version: '1.2.0', station_name: 'Entrance printer', worker: 'idle', job_msg_id: null, printer: { connected: true, model: 'Brother QL-1110NWBc', ready: true, media_type: 'continuous', label_id: '103', media_width_mm: 103, media_length_mm: null, errors: [] }, network: { state: 'CONNECTED', ssid: 'OfficeWi-Fi', ip: '192.168.178.55' }, label: null, last_error: null, uptime_s: 7325, }, state: 'ready', ...overrides, }; } const errorStation = station({ id: 'st-err', name: 'Error printer', last_status: { version: '1.2.0', station_name: 'Error printer', worker: 'idle', job_msg_id: null, printer: { connected: true, model: 'Brother QL-1110NWBc', ready: false, media_type: 'die-cut', label_id: 'DK11209G', media_width_mm: 62, media_length_mm: 104, errors: ['No media when printing'] }, network: { state: 'CONNECTED', ssid: 'OfficeWi-Fi', ip: '192.168.178.56' }, label: null, last_error: 'print failed: no media', uptime_s: 600, }, state: 'error', }); const offlineStation = station({ id: 'st-off', name: 'Offline printer', last_seen_at: minutesAgo(2), last_status: { version: '1.2.0', station_name: 'Offline printer', worker: 'idle', job_msg_id: null, printer: { connected: true, model: 'Brother QL-1110NWBc', ready: true, media_type: 'continuous', label_id: '103', media_width_mm: 103, media_length_mm: null, errors: [] }, network: { state: 'CONNECTED', ssid: 'OldCafeGuest', ip: '10.0.0.9' }, label: null, last_error: null, uptime_s: 120, }, state: 'offline', }); const deadLetter = { msg_id: 77, enqueued_at: '2026-10-18T11:00:00Z', failed_at: '2026-10-18T11:05:00Z', error: 'Printer reported: End of media (die-cut size only)', attempts: 3, kind: 'checkin', full_name: 'Bob Broken', email: 'bob@example.com', event_name: 'Badge Conference', registration_id: '33333333-3333-4333-8333-333333333333', }; const recentJob = { msg_id: 42, archived_at: '2026-10-18T11:30:00Z', kind: 'checkin', full_name: 'Jane Attendee', email: 'jane@example.com', event_name: 'Badge Conference', registration_id: jane.id, }; function overview(stations: Record[], overrides: Record = {}) { return { stations, queue: { pending: 1, in_flight: 0, dead_letter: stations.length ? 1 : 0, completed: 5 }, recent: [recentJob], dead_letters: stations.length ? [deadLetter] : [], ...overrides, }; } interface Options { stations?: Record[]; layouts?: Record[]; } interface RpcState { calls: { fn: string; body: Record }[]; overviewRequests: () => number; callsTo: (fn: string) => Record[]; } async function openAdmin(page: Page, options: Options = {}): Promise { const ov = overview(options.stations ?? [station()]); const layouts = options.layouts ?? [layout]; const calls: { fn: string; body: Record }[] = []; let overviewRequests = 0; const payload = Buffer.from(JSON.stringify({ sub: user.id, exp: Math.floor(Date.now() / 1000) + 3600 })).toString('base64url'); const session = { user, access_token: `eyJhbGciOiJIUzI1NiJ9.${payload}.test`, refresh_token: 'mock-refresh-token', expires_in: 3600, token_type: 'bearer' }; const event = { id: eventId, name: 'Badge Conference', company: 'Austrian Power Platform Enthusiasts', is_active: true, start_date: '2026-10-18', end_date: '2026-10-18', num_registrations: 2, num_checked_in_registrations: 1, num_sessions: 0, num_speakers: 1 }; const contacts = [ { email: user.email, full_name: 'Admin', is_admin: true }, { email: 'jane@example.com', full_name: 'Jane Attendee', is_admin: false }, { email: 'sam@example.com', full_name: 'Sam Speaker', is_admin: false }, ]; await page.route('**/auth/v1/**', (route) => route.fulfill({ json: new URL(route.request().url()).pathname.endsWith('/user') ? user : session })); await page.route('**/rest/v1/**', (route) => { const url = new URL(route.request().url()); const rpcMatch = url.pathname.match(/\/rpc\/([a-z_]+)$/); if (rpcMatch) { const fn = rpcMatch[1]; const body = (route.request().postDataJSON() ?? {}) as Record; calls.push({ fn, body }); if (fn === 'get_print_overview') { overviewRequests += 1; return route.fulfill({ json: ov }); } if (fn === 'enqueue_badge_print') return route.fulfill({ json: 101 }); if (fn === 'create_print_station') return route.fulfill({ json: [{ id: 'st-new', token: 'cvp_supersecrettoken1234567890abcdef' }] }); if (fn === 'rename_print_station') { // Apply the rename to the mocked overview so the UI reflects it. const target = ov.stations.find((s) => s.id === body.p_id); if (target) target.name = String(body.p_name); return route.fulfill({ json: {} }); } if (fn === 'requeue_dead_badge_print') return route.fulfill({ json: 102 }); if (fn === 'discard_dead_badge_print') return route.fulfill({ json: {} }); return route.fulfill({ json: [] }); } const table = url.pathname.split('/').pop(); if (table === 'registrations') return route.fulfill({ json: [jane, sam] }); if (table === 'badge_layouts') return route.fulfill({ json: layouts }); if (table === 'contacts') return route.fulfill({ json: url.searchParams.has('email') ? contacts[0] : contacts }); if (table === 'events') return route.fulfill({ json: url.searchParams.has('is_active') ? event : [event] }); return route.fulfill({ json: [] }); }); await page.goto(`${adminUrl}/auth/callback#access_token=${session.access_token}&refresh_token=${session.refresh_token}&type=magiclink`); // Nav labels include emoji, so scope to .nav and match by regex. await expect(page.locator('.nav').getByRole('button', { name: /Printing/ })).toBeVisible(); return { calls, overviewRequests: () => overviewRequests, callsTo: (fn: string) => calls.filter((c) => c.fn === fn).map((c) => c.body), }; } const pill = (page: Page) => page.getByTestId('printer-pill'); const navTo = async (page: Page, name: RegExp) => { await page.locator('.nav').getByRole('button', { name }).click(); }; test('the printer pill is visible on Dashboard and Registrations and reflects ready/error/offline', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); await openAdmin(page); // Dashboard (initial view): ready station. await expect(pill(page)).toBeVisible(); await expect(pill(page)).toHaveAttribute('data-state', 'ready'); await expect(pill(page)).toContainText('Printer ready'); await navTo(page, /Registrations/); await expect(page.getByRole('row').filter({ hasText: 'jane@example.com' })).toBeVisible(); await expect(pill(page)).toBeVisible(); await expect(pill(page)).toHaveAttribute('data-state', 'ready'); }); test('the pill shows the first printer error, shortened', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); await openAdmin(page, { stations: [errorStation] }); await expect(pill(page)).toHaveAttribute('data-state', 'error'); await expect(pill(page)).toContainText('Printer: No media'); }); test('the pill shows offline with the time since the last heartbeat', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); await openAdmin(page, { stations: [offlineStation] }); await expect(pill(page)).toHaveAttribute('data-state', 'offline'); await expect(pill(page)).toContainText('Printer offline · 2 min'); }); test('the pill is visible at a 390px mobile viewport', async ({ page }, testInfo) => { await page.setViewportSize({ width: 390, height: 844 }); await openAdmin(page, { stations: [errorStation] }); await expect(pill(page)).toBeVisible(); await expect(pill(page)).toHaveAttribute('data-state', 'error'); await page.screenshot({ path: testInfo.outputPath('pill-mobile-390.png') }); }); test('clicking the pill opens the Printing view', async ({ page }, testInfo) => { await page.setViewportSize({ width: 1280, height: 900 }); await openAdmin(page); await pill(page).click(); await expect(page.getByRole('heading', { name: 'Printing', exact: true })).toBeVisible(); await expect(page.getByTestId('station-st-0001')).toContainText('Entrance printer'); await page.screenshot({ path: testInfo.outputPath('printing-view.png'), fullPage: true }); }); test('an offline station shows the Wi-Fi setup hint with 10.41.0.1', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); await openAdmin(page, { stations: [offlineStation] }); await pill(page).click(); const hint = page.getByRole('note'); await expect(hint).toContainText('10.41.0.1'); await expect(hint).toContainText('OldCafeGuest'); }); test('adding a station shows the token once and it is gone after Done', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); await openAdmin(page); await pill(page).click(); await page.getByRole('button', { name: '+ Add station' }).click(); await page.getByPlaceholder('Station name…').fill('Back desk'); await page.getByRole('button', { name: 'Create' }).click(); const dialog = page.getByRole('dialog', { name: /Station token/ }); await expect(dialog).toBeVisible(); await expect(dialog.getByText('cannot be shown again')).toBeVisible(); const tokenField = dialog.getByLabel('Station token'); await expect(tokenField).toHaveValue('cvp_supersecrettoken1234567890abcdef'); expect((await tokenField.evaluate((el) => (el as HTMLInputElement).readOnly)).toString()).toBe('true'); await dialog.getByRole('button', { name: 'Done' }).click(); await expect(dialog).toHaveCount(0); // The token is no longer rendered anywhere on the page. await expect(page.getByText('cvp_supersecrettoken1234567890abcdef')).toHaveCount(0); }); test('renaming a station calls the RPC with the trimmed name and updates the card', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); const state = await openAdmin(page); await pill(page).click(); const card = page.getByTestId('station-st-0001'); await expect(card).toContainText('Entrance printer'); await card.getByRole('button', { name: 'Rename', exact: true }).click(); const field = card.getByLabel('Station name'); await expect(field).toHaveValue('Entrance printer'); await field.fill(' Main hall '); await field.press('Enter'); await expect.poll(() => state.callsTo('rename_print_station').length).toBeGreaterThanOrEqual(1); expect(state.callsTo('rename_print_station')[0]).toMatchObject({ p_id: 'st-0001', p_name: 'Main hall' }); await expect(card).toContainText('Main hall'); await expect(page.locator('.toast-success')).toContainText('Main hall'); }); test('rename keeps Save disabled for a blank name and Escape cancels', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); const state = await openAdmin(page); await pill(page).click(); const card = page.getByTestId('station-st-0001'); await card.getByRole('button', { name: 'Rename', exact: true }).click(); const field = card.getByLabel('Station name'); await field.fill(' '); await expect(card.getByRole('button', { name: 'Save' })).toBeDisabled(); await field.press('Escape'); await expect(card.getByRole('button', { name: 'Save' })).toHaveCount(0); await expect(card).toContainText('Entrance printer'); expect(state.callsTo('rename_print_station')).toHaveLength(0); }); test('Requeue and Discard call their RPCs with the dead letter id', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); const state = await openAdmin(page); await pill(page).click(); const row = page.getByRole('row').filter({ hasText: 'Bob Broken' }); await expect(row).toBeVisible(); await row.getByRole('button', { name: 'Requeue', exact: true }).click(); await expect.poll(() => state.callsTo('requeue_dead_badge_print').length).toBeGreaterThanOrEqual(1); expect(state.callsTo('requeue_dead_badge_print')[0]).toMatchObject({ p_msg_id: 77 }); page.once('dialog', (dialog) => dialog.accept()); await row.getByRole('button', { name: 'Discard', exact: true }).click(); await expect.poll(() => state.callsTo('discard_dead_badge_print').length).toBeGreaterThanOrEqual(1); expect(state.callsTo('discard_dead_badge_print')[0]).toMatchObject({ p_msg_id: 77 }); }); test('Reprint on a completed job enqueues that registration', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); const state = await openAdmin(page); await pill(page).click(); const row = page.getByRole('row').filter({ hasText: 'Jane Attendee' }).filter({ hasText: 'check-in' }); await row.getByRole('button', { name: 'Reprint' }).click(); await expect.poll(() => state.callsTo('enqueue_badge_print').length).toBeGreaterThanOrEqual(1); expect(state.callsTo('enqueue_badge_print')[0]).toMatchObject({ p_registration_id: jane.id }); }); test('the Registrations Print button enqueues the row and toasts', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); const state = await openAdmin(page); await navTo(page, /Registrations/); const row = page.getByRole('row').filter({ hasText: 'jane@example.com' }); await row.getByRole('button', { name: 'Print', exact: true }).click(); await expect.poll(() => state.callsTo('enqueue_badge_print').length).toBeGreaterThanOrEqual(1); expect(state.callsTo('enqueue_badge_print')[0]).toMatchObject({ p_registration_id: jane.id }); await expect(page.locator('.toast-success')).toContainText('Sent to printer'); }); test('with the printer offline, Print still queues and the toast says so', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); const state = await openAdmin(page, { stations: [offlineStation] }); await navTo(page, /Registrations/); await page.getByRole('row').filter({ hasText: 'jane@example.com' }).getByRole('button', { name: 'Print', exact: true }).click(); await expect.poll(() => state.callsTo('enqueue_badge_print').length).toBeGreaterThanOrEqual(1); await expect(page.locator('.toast-success')).toContainText('Queued — printer is offline'); }); test('Print is disabled with the configure-first tooltip when no layout exists', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); await openAdmin(page, { layouts: [] }); await navTo(page, /Registrations/); const print = page.getByRole('row').filter({ hasText: 'jane@example.com' }).getByRole('button', { name: 'Print', exact: true }); await expect(print).toBeVisible(); await expect(print).toBeDisabled(); await print.locator('xpath=ancestor::span[contains(@class,"tip-anchor")]').hover(); const tip = page.getByRole('tooltip'); await expect(tip).toContainText('No badge yet'); }); test('the overview polls repeatedly while the app is open', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); const state = await openAdmin(page); // StrictMode double effects + the 5 s interval: at least one initial poll. await expect.poll(state.overviewRequests).toBeGreaterThanOrEqual(1); });