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: {} }; interface Options { layouts?: Record[]; failSave?: boolean; } async function openBadgeEditor(page: Page, options: Options = {}) { 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-23', end_date: '2026-10-23', num_registrations: 3, num_sessions: 0, num_speakers: 0 }; const contact = { email: user.email, full_name: 'Admin', is_admin: true }; const attendee = { email: 'jane@example.com', full_name: 'Jane Attendee', is_admin: false }; const layouts = options.layouts ?? [ { id: 'l-default', event_id: eventId, name: 'Default', is_default: true, page_width_mm: 100, page_height_mm: 70, created_at: '2026-10-23T10:00:00Z', updated_at: '2026-10-23T10:00:00Z', elements: [ { id: 'e-text', type: 'text', x: 10, y: 10, width: 60, height: 10, rotation: 0, content: 'attendee_name', text: '', font: 'Open Sans', font_size: 13, line_height: 1, color: '#000000', bold: false, italic: false, align_h: 'left', align_v: 'middle', transform: 'none' }, { id: 'e-qr', type: 'qr', x: 70, y: 40, width: 22, height: 22, rotation: 0, value: 'qr_token', static_value: '' }, ], }, ]; const saves: Record[] = []; let deletes = 0; let setDefaults = 0; 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()); if (url.pathname.endsWith('/rpc/save_badge_layout')) { if (options.failSave) return route.fulfill({ status: 400, json: { code: 'P0001', message: 'admin only' } }); const body = JSON.parse(route.request().postData() || '{}'); saves.push(body); return route.fulfill({ json: { id: 'l-default', event_id: eventId, name: body.p_name, is_default: body.p_is_default, page_width_mm: body.p_page_width_mm, page_height_mm: body.p_page_height_mm, elements: body.p_elements, created_at: '2026-10-23T10:00:00Z', updated_at: '2026-10-23T11:00:00Z' } }); } if (url.pathname.endsWith('/rpc/set_default_badge_layout')) { setDefaults += 1; return route.fulfill({ json: null }); } if (url.pathname.endsWith('/rpc/delete_badge_layout')) { deletes += 1; return route.fulfill({ json: null }); } if (url.pathname.endsWith('/badge_layouts')) return route.fulfill({ json: layouts }); if (url.pathname.endsWith('/registrations')) return route.fulfill({ json: [{ id: 'r1', event_id: eventId, contact_email: 'jane@example.com', type: 'attendee', qr_token: 'CV-JANE-01', points: 0 }] }); if (url.pathname.endsWith('/events')) return route.fulfill({ json: url.searchParams.has('is_active') ? event : [event] }); if (url.pathname.endsWith('/contacts')) return route.fulfill({ json: url.searchParams.has('email') ? contact : [contact, attendee] }); return route.fulfill({ json: [] }); }); await page.goto(`${adminUrl}/auth/callback#access_token=${session.access_token}&refresh_token=${session.refresh_token}&type=magiclink`); await expect(page.locator('.nav').getByRole('button', { name: /Dashboard/ })).toBeVisible(); await page.locator('.nav').getByRole('button', { name: /Badges/ }).click(); return { saves, deleteCount: () => deletes, setDefaultCount: () => setDefaults }; } test('badge editor loads the default layout onto a millimetre canvas', async ({ page }, testInfo) => { await page.setViewportSize({ width: 1280, height: 900 }); await openBadgeEditor(page); await expect(page.getByRole('heading', { name: 'Badge Editor' })).toBeVisible(); await expect(page.getByLabel('Badge layout')).toHaveValue('Default'); await expect(page.locator('.badge-page')).toBeVisible(); await expect(page.locator('.badge-el')).toHaveCount(2); // The bound attendee name resolves against the real sample registration. await expect(page.locator('.badge-el-text')).toContainText('Jane Attendee'); await page.screenshot({ path: testInfo.outputPath('badge-editor.png'), fullPage: true }); }); test('adding a text element selects it and opens its properties', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); await openBadgeEditor(page); await page.getByRole('button', { name: 'Text', exact: true }).click(); await expect(page.locator('.badge-el')).toHaveCount(3); await expect(page.getByText('Text box', { exact: true })).toBeVisible(); await expect(page.getByLabel('Content')).toBeVisible(); // A selected element shows its resize + rotate handles. await expect(page.locator('.badge-handle')).toHaveCount(9); }); test('editing the x position moves the element on the canvas', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); await openBadgeEditor(page); await page.locator('.badge-el').first().click(); await expect(page.getByText('Text box', { exact: true })).toBeVisible(); const before = await page.locator('.badge-el').first().evaluate((n) => (n as HTMLElement).style.left); await page.getByLabel('x (mm)').fill('50'); const after = await page.locator('.badge-el').first().evaluate((n) => (n as HTMLElement).style.left); expect(parseFloat(after)).toBeGreaterThan(parseFloat(before)); }); test('saving posts the layout to save_badge_layout', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); const { saves } = await openBadgeEditor(page); await page.getByRole('button', { name: 'Text', exact: true }).click(); await page.getByRole('button', { name: 'Save' }).click(); await expect.poll(() => saves.length).toBe(1); expect(saves[0]).toMatchObject({ p_event_id: eventId, p_name: 'Default', p_is_default: true }); expect((saves[0].p_elements as unknown[]).length).toBe(3); }); test('preview mode hides the editing handles', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); await openBadgeEditor(page); await page.locator('.badge-el').first().click(); await expect(page.locator('.badge-handle')).toHaveCount(9); await page.getByRole('button', { name: 'Preview' }).click(); await expect(page.locator('.badge-handle')).toHaveCount(0); }); test('the JSON view shows the current layout', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); await openBadgeEditor(page); await page.getByRole('button', { name: 'Layout JSON' }).click(); await expect(page.locator('.badge-json')).toBeVisible(); await expect(page.locator('.badge-json')).toContainText('"attendee_name"'); }); test('a failed save keeps the editor usable and reports the error', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); await openBadgeEditor(page, { failSave: true }); await page.getByRole('button', { name: 'Text', exact: true }).click(); await page.getByRole('button', { name: 'Save' }).click(); await expect(page.getByText('admin only')).toBeVisible(); await expect(page.locator('.badge-el')).toHaveCount(3); }); test('badge controls fit a mobile viewport', async ({ page }) => { await page.setViewportSize({ width: 390, height: 844 }); await openBadgeEditor(page); await expect(page.getByRole('heading', { name: 'Badge Editor' })).toBeVisible(); await expect(page.getByRole('button', { name: 'Save' })).toBeVisible(); }); test('the Delete key removes the selected element', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); await openBadgeEditor(page); await page.locator('.badge-el').first().click(); await expect(page.locator('.badge-handle')).toHaveCount(9); await page.keyboard.press('Delete'); await expect(page.locator('.badge-el')).toHaveCount(1); }); test('a text box can bind the company / organizer field', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); await openBadgeEditor(page); await page.getByRole('button', { name: 'Text', exact: true }).click(); await page.getByLabel('Content').selectOption('company'); await expect(page.locator('.badge-text-body', { hasText: 'Austrian Power Platform Enthusiasts' })).toBeVisible(); }); test('align-centers horizontally centers the element on the page', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); await openBadgeEditor(page); // The default text box is x=10, width=60 on a 100mm page → centered x = 20. await page.locator('.badge-el').first().click(); await page.getByTitle('Align centers horizontally').first().click(); await expect(page.getByLabel('x (mm)')).toHaveValue('20'); }); test('shift-click selects multiple elements for group alignment', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); await openBadgeEditor(page); await page.locator('.badge-el').first().click(); await page.locator('.badge-el').nth(1).click({ modifiers: ['Shift'] }); await expect(page.getByText('2 selected')).toBeVisible(); // Both selected elements show their own handles (9 each). await expect(page.locator('.badge-handle')).toHaveCount(18); }); test('dragging near the page centre shows a smart guide and snaps', async ({ page }) => { await page.setViewportSize({ width: 1280, height: 900 }); await openBadgeEditor(page); const pageBox = await page.locator('.badge-page').boundingBox(); const qr = page.locator('.badge-el').nth(1); // the QR element const qrBox = await qr.boundingBox(); if (!pageBox || !qrBox) throw new Error('missing boxes'); const startX = qrBox.x + qrBox.width / 2; const startY = qrBox.y + qrBox.height / 2; const targetX = pageBox.x + pageBox.width / 2; // drag the QR centre to the page centre await page.mouse.move(startX, startY); await page.mouse.down(); await page.mouse.move(targetX - 2, startY, { steps: 8 }); // just short of centre → within snap threshold await expect(page.locator('.badge-guide')).toBeVisible(); await page.mouse.up(); // After snapping, the QR centre sits on the page centre (x = 50 - w/2 = 39mm). await expect(page.getByLabel('x (mm)')).toHaveValue('39'); });