Files
Convivo/e2e/tests/registration-ticket-merge.spec.ts
T

109 lines
7.6 KiB
TypeScript

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 sourceId = '11111111-1111-4111-8111-111111111111';
const targetId = '22222222-2222-4222-8222-222222222222';
const otherId = '33333333-3333-4333-8333-333333333333';
const manualId = '44444444-4444-4444-8444-444444444444';
async function openRegistrations(page: Page, options: { patchFailure?: boolean; mergeFailure?: boolean } = {}) {
const user = { id: '99999999-9999-4999-8999-999999999999', email: '[email protected]', aud: 'authenticated', role: 'authenticated', app_metadata: {}, user_metadata: {} };
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: 'Merge Conference', is_active: true, start_date: '2026-11-01', end_date: '2026-11-01', num_registrations: 2, num_checked_in_registrations: 0, num_sessions: 1, num_speakers: 1 };
const contacts = [
{ email: user.email, full_name: 'Admin', is_admin: true },
{ email: '[email protected]', full_name: 'Alex Smith', is_admin: false },
{ email: '[email protected]', full_name: 'Alex Smith', is_admin: false },
{ email: '[email protected]', full_name: 'Robin Jones', is_admin: false },
{ email: '[email protected]', full_name: 'Manual Person', is_admin: false },
];
let registrations = [
{ id: sourceId, event_id: eventId, contact_email: contacts[1].email, type: 'speaker', qr_token: null, external_id: null, import_source: 'sessionize', checked_in: false, points: 0 },
{ id: targetId, event_id: eventId, contact_email: contacts[2].email, type: 'attendee', qr_token: 'real-qr-one', external_id: 'pretix-one', import_source: null, checked_in: false, points: 0 },
{ id: otherId, event_id: eventId, contact_email: contacts[3].email, type: 'attendee', qr_token: 'real-qr-two', external_id: 'pretix-two', import_source: null, checked_in: false, points: 0 },
{ id: manualId, event_id: eventId, contact_email: contacts[4].email, type: 'attendee', qr_token: ' ', external_id: null, import_source: null, checked_in: true, points: 0 },
];
const merges: Array<{ p_source_id: string; p_target_id: string }> = [];
await page.route('**/auth/v1/**', async route => {
const path = new URL(route.request().url()).pathname;
await route.fulfill({ json: path.endsWith('/user') ? user : path.endsWith('/token') ? session : {} });
});
await page.route('**/rest/v1/**', async route => {
const url = new URL(route.request().url());
const table = url.pathname.split('/').pop();
if (table === 'merge_tickets') {
const body = route.request().postDataJSON();
merges.push(body);
if (options.mergeFailure) return route.fulfill({ status: 400, json: { message: 'Ticket no longer exists' } });
const target = registrations.find(row => row.id === body.p_target_id)!;
registrations = registrations.filter(row => row.id !== body.p_source_id).map(row => row.id === target.id ? { ...row, type: 'speaker' } : row);
return route.fulfill({ json: { source_id: body.p_source_id, target_id: target.id, target_email: target.contact_email, type: 'speaker', sessions_transferred: 2 } });
}
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] });
if (table === 'registrations') {
if (route.request().method() === 'PATCH') {
if (options.patchFailure) return route.fulfill({ status: 500, json: { message: 'Check-in failed' } });
const id = url.searchParams.get('id')!.replace('eq.', '');
registrations = registrations.map(row => row.id === id ? { ...row, ...route.request().postDataJSON() } : row);
return route.fulfill({ json: { id } });
}
return route.fulfill({ json: registrations });
}
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.getByRole('button', { name: 'Sign out', exact: true })).toBeVisible();
await page.locator('.nav').getByRole('button', { name: /Registrations/ }).click();
await expect(page.getByRole('button', { name: 'Merge tickets', exact: true })).toBeVisible();
return { merges };
}
test('counts only QR tickets and check-in slides without refresh', async ({ page }) => {
await openRegistrations(page);
await expect(page.getByText('2 tickets · 0 checked in · 2 without tickets.', { exact: false })).toBeVisible();
const row = page.getByRole('row').filter({ hasText: '[email protected]' });
await row.locator('label.toggle').click();
await expect(row.getByRole('checkbox')).toBeChecked();
await expect(page.getByText('2 tickets · 1 checked in · 2 without tickets.', { exact: false })).toBeVisible();
});
test('failed check-in rolls the toggle back', async ({ page }) => {
await openRegistrations(page, { patchFailure: true });
const row = page.getByRole('row').filter({ hasText: '[email protected]' });
await row.locator('label.toggle').click();
await expect(page.getByText('Check-in failed', { exact: true })).toBeVisible();
await expect(row.getByRole('checkbox')).not.toBeChecked();
});
test('merge is imported-only, suggests names, cancels safely, and permits any purchased ticket', async ({ page }) => {
const state = await openRegistrations(page);
await expect(page.getByRole('button', { name: 'Merge tickets', exact: true })).toHaveCount(1);
await page.getByRole('button', { name: 'Merge tickets', exact: true }).click();
const choices = page.getByRole('radiogroup', { name: 'Destination ticket' });
await expect(choices.getByRole('radio').first()).toHaveAccessibleName(/Alex Smith/);
await expect(page.getByRole('button', { name: 'Confirm merge' })).toBeDisabled();
await page.getByRole('button', { name: 'Cancel', exact: true }).click();
expect(state.merges).toHaveLength(0);
await page.getByRole('button', { name: 'Merge tickets', exact: true }).click();
await page.getByPlaceholder('Search name or email').fill('Robin');
await page.getByRole('radio', { name: /Robin Jones/ }).check();
await page.getByRole('button', { name: 'Confirm merge' }).click();
await expect(page.getByText('Merged into [email protected]; 2 session assignment(s) transferred.', { exact: true })).toBeVisible();
expect(state.merges).toEqual([{ p_source_id: sourceId, p_target_id: otherId }]);
await expect(page.getByRole('row').filter({ hasText: '[email protected]' })).toHaveCount(0);
await expect(page.getByRole('row').filter({ hasText: '[email protected]' }).locator('select')).toHaveValue('speaker');
});
test('failed merge keeps source row and allows retry', async ({ page }) => {
await openRegistrations(page, { mergeFailure: true });
await page.getByRole('button', { name: 'Merge tickets', exact: true }).click();
await page.getByRole('radio', { name: /Alex Smith/ }).check();
await page.getByRole('button', { name: 'Confirm merge' }).click();
await expect(page.getByRole('alert')).toHaveText('Ticket no longer exists');
await expect(page.getByRole('button', { name: 'Confirm merge' })).toBeEnabled();
await page.getByRole('button', { name: 'Cancel', exact: true }).click();
await expect(page.getByRole('row').filter({ hasText: '[email protected]' })).toBeVisible();
});