131 lines
7.8 KiB
TypeScript
131 lines
7.8 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 user = { id: '99999999-9999-4999-8999-999999999999', email: '[email protected]', aud: 'authenticated', role: 'authenticated', app_metadata: {}, user_metadata: {} };
|
|
|
|
interface Options { poolSize?: number; fail?: boolean; }
|
|
|
|
async function openRaffle(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: 'Raffle Conference', is_active: true, start_date: '2026-10-23', end_date: '2026-10-23', num_registrations: 8, num_sessions: 0, num_speakers: 0 };
|
|
const poolSize = options.poolSize ?? 4;
|
|
const pool = Array.from({ length: poolSize }, (_v, i) => ({ contact_email: `attendee-${i}@example.com`, full_name: `Attendee ${i + 1}`, points: 10 - i }));
|
|
const winners: Record<string, unknown>[] = [];
|
|
const draws: string[] = [];
|
|
let resets = 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/get_raffle_pool')) {
|
|
return route.fulfill({ json: pool.filter(e => !winners.some(w => w.contact_email === e.contact_email)) });
|
|
}
|
|
if (url.pathname.endsWith('/rpc/draw_raffle_winner')) {
|
|
draws.push('draw');
|
|
if (options.fail) return route.fulfill({ status: 400, json: { code: 'P0001', message: 'No eligible attendees left to draw' } });
|
|
const remaining = pool.filter(e => !winners.some(w => w.contact_email === e.contact_email));
|
|
if (remaining.length === 0) return route.fulfill({ status: 400, json: { code: 'P0001', message: 'No eligible attendees left to draw' } });
|
|
const winner = remaining[0];
|
|
const row = { ...winner, draw_number: winners.length + 1, drawn_at: '2026-10-23T16:00:00Z' };
|
|
winners.push(row);
|
|
return route.fulfill({ json: [row] });
|
|
}
|
|
if (url.pathname.endsWith('/rpc/reset_raffle')) { resets += 1; winners.length = 0; return route.fulfill({ json: null }); }
|
|
if (url.pathname.endsWith('/raffle_draws')) return route.fulfill({ json: winners });
|
|
if (url.pathname.endsWith('/events')) return route.fulfill({ json: url.searchParams.has('is_active') ? event : [event] });
|
|
if (url.pathname.endsWith('/contacts')) {
|
|
const contact = { email: user.email, full_name: 'Admin', is_admin: true };
|
|
return route.fulfill({ json: url.searchParams.has('email') ? contact : [contact] });
|
|
}
|
|
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: /Raffle/ }).click();
|
|
return { draws, resetCount: () => resets, winnerCount: () => winners.length };
|
|
}
|
|
|
|
test('raffle defaults to five picks and excludes staff, admin and the top three', async ({ page }, testInfo) => {
|
|
await page.setViewportSize({ width: 1280, height: 900 });
|
|
await openRaffle(page);
|
|
await expect(page.getByRole('heading', { name: 'Raffle' })).toBeVisible();
|
|
await expect(page.getByLabel('Picks per run')).toHaveValue('5');
|
|
await expect(page.getByText('Winners this run: 0 of 5')).toBeVisible();
|
|
await expect(page.getByText('Excludes staff/Admin and the leaderboard top 3.')).toBeVisible();
|
|
await expect(page.getByRole('button', { name: 'Spin', exact: true })).toBeEnabled();
|
|
await page.screenshot({ path: testInfo.outputPath('raffle-idle.png'), fullPage: true });
|
|
});
|
|
|
|
test('spinning decelerates onto a winner and removes them from the pool', async ({ page }, testInfo) => {
|
|
await page.setViewportSize({ width: 1280, height: 900 });
|
|
const { draws } = await openRaffle(page);
|
|
const spin = page.getByRole('button', { name: 'Spin', exact: true });
|
|
await spin.click();
|
|
await expect(page.getByRole('button', { name: 'Spinning...' })).toBeVisible();
|
|
await expect(page.getByRole('status').filter({ hasText: /^Attendee/ }).first()).toBeVisible();
|
|
await expect(page.getByRole('button', { name: 'Spin', exact: true })).toBeEnabled();
|
|
expect(draws).toHaveLength(1);
|
|
await expect(page.getByText('Winners this run: 1 of 5')).toBeVisible();
|
|
await expect(page.getByRole('cell', { name: 'Attendee 1', exact: true })).toBeVisible();
|
|
await expect(page.getByText('[email protected]')).toHaveCount(0);
|
|
await expect(page.locator('canvas')).toBeVisible();
|
|
await page.screenshot({ path: testInfo.outputPath('raffle-winner.png'), fullPage: true });
|
|
});
|
|
|
|
test('draws continue until the pick count is reached, then the run is complete', async ({ page }) => {
|
|
const { draws } = await openRaffle(page, { poolSize: 3 });
|
|
for (let i = 1; i <= 3; i += 1) {
|
|
await page.getByRole('button', { name: 'Spin', exact: true }).click();
|
|
await expect(page.getByText(`Winners this run: ${i} of 5`)).toBeVisible();
|
|
}
|
|
expect(draws).toHaveLength(3);
|
|
await expect(page.getByRole('button', { name: 'No attendees left', exact: true })).toBeDisabled();
|
|
await expect(page.getByText('no eligible attendees left')).toBeVisible();
|
|
});
|
|
|
|
test('a failed draw keeps the previous winners and allows retry', async ({ page }) => {
|
|
const { draws, winnerCount } = await openRaffle(page, { fail: true });
|
|
await page.getByRole('button', { name: 'Spin', exact: true }).click();
|
|
await expect(page.getByRole('alert')).toHaveText('No eligible attendees left to draw');
|
|
await expect(page.getByRole('button', { name: 'Spin', exact: true })).toBeEnabled();
|
|
expect(draws).toHaveLength(1);
|
|
expect(winnerCount()).toBe(0);
|
|
});
|
|
|
|
test('starting a new run clears the winners and re-enables drawing', async ({ page }) => {
|
|
const { resetCount } = await openRaffle(page);
|
|
await page.getByRole('button', { name: 'Spin', exact: true }).click();
|
|
await expect(page.getByText('Winners this run: 1 of 5')).toBeVisible();
|
|
await page.getByRole('button', { name: 'New run', exact: true }).click();
|
|
await expect(page.getByText('Winners this run: 0 of 5')).toBeVisible();
|
|
await expect(page.getByRole('cell', { name: 'Attendee 1' })).toHaveCount(0);
|
|
expect(resetCount()).toBe(1);
|
|
});
|
|
|
|
test('the pick count is configurable before the run starts', async ({ page }) => {
|
|
await openRaffle(page);
|
|
await page.getByLabel('Picks per run').fill('2');
|
|
await expect(page.getByText('Winners this run: 0 of 2')).toBeVisible();
|
|
await page.getByRole('button', { name: 'Spin', exact: true }).click();
|
|
await expect(page.getByText('Winners this run: 1 of 2')).toBeVisible();
|
|
await page.getByRole('button', { name: 'Spin', exact: true }).click();
|
|
await expect(page.getByRole('region', { name: 'Raffle winners' }).getByText('Run complete', { exact: true })).toBeVisible();
|
|
await expect(page.getByRole('button', { name: 'Run complete', exact: true })).toBeDisabled();
|
|
});
|
|
|
|
test('raffle controls fit a mobile viewport', async ({ page }, testInfo) => {
|
|
await page.setViewportSize({ width: 390, height: 844 });
|
|
await openRaffle(page);
|
|
const stage = page.getByRole('region', { name: 'Raffle roulette' });
|
|
await expect(stage).toBeVisible();
|
|
const bounds = await stage.boundingBox();
|
|
expect(bounds).not.toBeNull();
|
|
expect(bounds!.x).toBeGreaterThanOrEqual(0);
|
|
expect(bounds!.x + bounds!.width).toBeLessThanOrEqual(390);
|
|
expect(await stage.evaluate(element => element.scrollWidth <= element.clientWidth)).toBe(true);
|
|
await page.screenshot({ path: testInfo.outputPath('raffle-mobile.png'), fullPage: true });
|
|
});
|