import { test, expect, type Page, type Locator } from "@playwright/test"; import { signInAs, resetDb, BASE } from "./dev"; /** * Reset the DB to the clean seed before the suite runs. The reset truncates and * repopulates the seed-owned tables; it must run as admin (admin.settings) and * from the browser context (page.request does not share the session cookie), so * it goes through `fetch` inside the page after an admin sign-in. */ test.beforeAll(async ({ browser }) => { // The `page` fixture is per-test, so drive a throwaway context here: sign in // as admin, hit the reset endpoint from inside the page (it needs the session // cookie), then close the context. Each test still gets its own page below. const context = await browser.newContext(); const page = await context.newPage(); await resetDb(page); await context.close(); }); /** * The grid renders one per position, in the same order as the * headers (which are prefixed by the "When" row header). Map a role name to * its cell so assertions never depend on a hard-coded column index. */ async function cellForRole(page: Page, row: Locator, role: string) { const headers = await page.locator("thead th").allTextContents(); const headerIndex = headers.indexOf(role); expect(headerIndex, `grid header "${role}" present`).toBeGreaterThanOrEqual(0); // header[0] is the "When" row header; the first data sits under header[1]. return row.locator("td").nth(headerIndex - 1); } test.describe("assign mutations", () => { test("assign appears immediately, forced duplicate rolls back, clear returns to open", async ({ page, }) => { const errors: string[] = []; // The rule-1 rollback is an intentional 409; the browser logs that failed // fetch as a console error. Only real failures (500s, crashes) count. const EXPECTED_409 = "status of 409 (Conflict)"; page.on("console", (msg) => { if (msg.type() === "error" && !msg.text().includes(EXPECTED_409)) errors.push(msg.text()); }); page.on("pageerror", (err) => errors.push(`page error: ${err.message}`)); await page.setViewportSize({ width: 1024, height: 768 }); await signInAs(page, "coordinator@glenloch.local"); await page.goto(`${BASE}/schedule`); await expect(page).toHaveURL(`${BASE}/schedule`); // The interactive editor mounts for coordinators: each open cell opens the // ranked picker. Take the first instance row (Audio pre-assigned; // Livestream + Slides open). const row = page.locator("tbody tr").first(); await expect(row.getByRole("button", { name: /Choose volunteer/ }).first()).toBeVisible(); const audio = await cellForRole(page, row, "Audio"); const livestream = await cellForRole(page, row, "Livestream"); const slides = await cellForRole(page, row, "Slides"); // Clear the pre-assigned Audio seat so every seat in the row is open and a // free volunteer can be assigned twice — the second trips rule 1. await audio.locator("button", { hasText: "Clear" }).click(); // Optimistic revert: the cell is open again and shows the ranked picker. await expect( audio.getByRole("button", { name: /Choose volunteer/ }), ).toBeVisible(); // Assign Sam Okafor to Livestream. Sam is verified qualified on both the // Livestream and Slides skills. await livestream .getByRole("button", { name: /Choose volunteer/ }) .click(); const livestreamDialog = page.getByRole("dialog"); await expect(livestreamDialog).toBeVisible(); await livestreamDialog .locator("li") .filter({ hasText: "Sam Okafor" }) .getByRole("button", { name: "Assign" }) .click(); await expect( livestream.locator("button", { hasText: "Clear" }), ).toBeVisible(); await expect( livestream.locator("span", { hasText: "Sam Okafor" }), ).toBeVisible(); // The picker refuses the rule-1 duplicate in the UI: Sam is already serving // in this instance, so the excluded list explains why there is no Assign. await slides.getByRole("button", { name: /Choose volunteer/ }).click(); const slidesDialog = page.getByRole("dialog"); await slidesDialog .getByRole("button", { name: /unavailable \/ not eligible/i }) .click(); const samRow = slidesDialog.locator("li").filter({ hasText: "Sam Okafor" }); await expect(samRow).toContainText("Already serving in this service"); await expect(samRow.getByRole("button", { name: "Assign" })).toHaveCount(0); await page.getByRole("button", { name: "Close ranked picker" }).click(); // The successful optimistic assign is still live. await expect( livestream.locator("button", { hasText: "Clear" }), ).toBeVisible(); await expect( livestream.locator("span", { hasText: "Sam Okafor" }), ).toBeVisible(); // No console or page errors while the mutations run. expect(errors, `unexpected console/page errors: ${errors.join("; ")}`).toEqual( [], ); }); // Review-focus point 3: a volunteer cannot assign. The read-only grid mounts // (no assign control), and a direct POST to the route is rejected 403 — not // 200 and not a 500. Pins the auth chokepoint the plan calls out. test("volunteer is read-only and the route rejects assign with 403", async ({ page, }) => { const errors: string[] = []; page.on("console", (msg) => { if (msg.type() === "error") errors.push(msg.text()); }); page.on("pageerror", (err) => errors.push(`page error: ${err.message}`)); await page.setViewportSize({ width: 1024, height: 768 }); await signInAs(page, "avery@glenloch.local"); await page.goto(`${BASE}/schedule`); await expect(page).toHaveURL(`${BASE}/schedule`); // Read-only grid: no per-cell picker trigger mounts for a volunteer. const assignControls = await page .locator("tbody tr") .getByRole("button", { name: /Choose volunteer/ }) .count(); expect(assignControls, "volunteer grid should expose no assign control").toBe(0); // The route still enforces the chokepoint directly. const res = await page.request.post(`${BASE}/api/assignments`, { data: JSON.stringify({ action: "assign", slotId: "x", userId: "y" }), headers: { "content-type": "application/json" }, }); expect(res.status(), "volunteer assign must be rejected").toBe(403); expect(errors, `unexpected console/page errors: ${errors.join("; ")}`).toEqual( [], ); }); });