import path from 'node:path'; import { test, expect } from './base'; import { APP_URL, ID, SPEAKER_SESSION } from '../src/helpers/config'; import { login } from '../src/helpers/auth'; import { createUser } from '../src/helpers/multi'; import { signInJwt, userRpcResult } from '../src/helpers/api'; import { db } from '../src/helpers/db'; import { AppPage } from '../src/pages/AppPage'; import { AgendaPage } from '../src/pages/AgendaPage'; import { HomePage } from '../src/pages/HomePage'; /** * ยง27 Speaker role + ยง28 Speaker session material upload. * * SPEAKER_01 (Duncan Boyne) is a `session_speakers` row for SESSION_A (slot 1, * "From Vision to Reality โ€“ A Live Launch Moment"). The seed leaves the * speaker's `qr_token` NULL (a real gap โ€” see findings), so the reset helper * assigns a token + re-hashes the auth password. * * Material upload is a two-step backend operation: a Supabase Storage write to * the `materials` bucket (RLS: `is_speaker_or_admin()`) + the `set_session_material` * RPC (security definer; raises unless the caller is admin or a speaker of the * session). We verify both the UI and the backend authorization. */ /** The speaker's session title (slot 1). */ const SPEAKER_SESSION_TITLE = 'From Vision to Reality'; /** A small, valid test file for the material upload. */ const MATERIAL_FILE = path.join(__dirname, '..', 'fixtures', 'material.txt'); /** Poll until a session has material (robust against commit timing). */ async function waitForMaterial(sessionId: string, timeoutMs = 5000): Promise { const deadline = Date.now() + timeoutMs; for (;;) { if (db.sessionHasMaterial(sessionId)) return true; if (Date.now() > deadline) return db.sessionHasMaterial(sessionId); await new Promise((r) => setTimeout(r, 200)); } } test.describe('Speaker + material upload', () => { test('CONV-SPEAKER-001 session assignment grants the Speaker role automatically', async ({ page }) => { const app = new AppPage(page, APP_URL); // SPEAKER_01 is a session_speakers row for SESSION_A (slot 1). await login(page, APP_URL, ID.speaker); await app.waitForAdmitted(); // The registration type is 'speaker' (auto-promoted by the trigger). expect(db.registration(ID.speaker.email)?.type).toBe('speaker'); // Speaker-specific functionality is available: the Agenda marks the session // with the "๐ŸŽค you" speaker tag. await app.gotoTab('agenda'); const agenda = new AgendaPage(page); await agenda.openSession(SPEAKER_SESSION_TITLE); await expect(agenda.speakerTag()).toBeVisible(); }); test('CONV-MATERIAL-001 speaker sees the pending material request (red attachment)', async ({ page }) => { const app = new AppPage(page, APP_URL); const home = new HomePage(page); await login(page, APP_URL, ID.speaker); await app.waitForAdmitted(); // Home displays the pending-material area (the red ๐Ÿ“Ž attachment pill). await expect(home.materialPill()).toBeVisible(); // The material request references the relevant session (the Agenda shows the // upload notice for the speaker's session). await app.gotoTab('agenda'); const agenda = new AgendaPage(page); await agenda.openSession(SPEAKER_SESSION_TITLE); await expect(page.getByTestId('material-upload-notice')).toBeVisible(); }); test('CONV-MATERIAL-002 speaker uploads material and it persists across reload', async ({ page }) => { const app = new AppPage(page, APP_URL); const agenda = new AgendaPage(page); await login(page, APP_URL, ID.speaker); await app.waitForAdmitted(); const home = new HomePage(page); // The pending-material pill count before the upload (this session is one of // the pending ones; the speaker may have other pending sessions too). const beforeCount = Number(((await home.materialPill().textContent()) ?? '').replace(/\D/g, '')); await app.gotoTab('agenda'); // Open the speaker's session and upload a valid test file. await agenda.openSession(SPEAKER_SESSION_TITLE); await agenda.uploadMaterial(MATERIAL_FILE); // The upload is persisted (material_path set on the session). expect(await waitForMaterial(SPEAKER_SESSION)).toBe(true); // The upload state survives a reload: the session shows the material (๐Ÿ“Ž) + // a download link, and the pending-material pill no longer counts this // session (the speaker's other session may still be pending, so the pill // count decreases rather than reaching 0). await page.reload(); await app.waitForAdmitted(); const afterCount = Number(((await home.materialPill().textContent()) ?? '').replace(/\D/g, '')); expect(afterCount).toBe(beforeCount - 1); await app.gotoTab('agenda'); await agenda.openSession(SPEAKER_SESSION_TITLE); await expect(page.getByText(/Download material/)).toBeVisible(); }); test('CONV-MATERIAL-003 material upload is available whenever it is still relevant', async ({ page }) => { const app = new AppPage(page, APP_URL); const agenda = new AgendaPage(page); // The speaker's session (slot 1) is a "done" slot at the simulated clock, // but the upload control is not time-gated (selNeedsMaterial has no time // check) โ€” it is available as long as the session is the user's and has no // material yet. await login(page, APP_URL, ID.speaker); await app.waitForAdmitted(); await app.gotoTab('agenda'); await agenda.openSession(SPEAKER_SESSION_TITLE); // The upload control is present (the session is the speaker's + no material). await expect(agenda.materialUploadInput()).toBeVisible(); // The upload notice is shown (non-dismissible until uploaded). await expect(page.getByTestId('material-upload-notice')).toBeVisible(); }); test('CONV-MATERIAL-004 material upload authorization (backend + frontend)', async ({ browser }) => { // Authorized: the speaker can upload (storage write allowed for speakers). const spk = await createUser(browser, 'speaker'); // Unauthorized: a plain attendee (no session/material assignment). const att = await createUser(browser, 'attendeeA'); try { // Backend: the speaker's JWT can call set_session_material. const spkJwt = await signInJwt(ID.speaker.email, ID.speaker.qrToken); const spkRes = await userRpcResult(spkJwt, 'set_session_material', { p_session_id: SPEAKER_SESSION, p_material_path: 'sessions/test/speaker.txt', }); expect(spkRes.ok, `speaker set_session_material should succeed: ${spkRes.message}`).toBe(true); // Backend: a plain attendee's JWT is rejected (not a speaker/admin). const attJwt = await signInJwt(ID.attendeeA.email, ID.attendeeA.qrToken); const attRes = await userRpcResult(attJwt, 'set_session_material', { p_session_id: SPEAKER_SESSION, p_material_path: 'sessions/test/attendee.txt', }); expect(attRes.ok, `attendee set_session_material should be rejected`).toBe(false); // Frontend: the plain attendee does not see the material upload control. await login(att.page, APP_URL, ID.attendeeA); const app = new AppPage(att.page, APP_URL); await app.waitForAdmitted(); await app.gotoTab('agenda'); const agenda = new AgendaPage(att.page); await agenda.openSession(SPEAKER_SESSION_TITLE); await expect(att.page.getByTestId('material-upload')).toHaveCount(0); } finally { await spk.close(); await att.close(); } }); });