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 has an // assign mounts for a volunteer. const assignControls = await page.locator("tbody tr").locator("select").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( [], ); }); });