import { test, expect } from "@playwright/test"; import { signInAs, resetDb, firstInstanceId, BASE } from "./dev"; /** * Auto-fill — unit D9. * * The route computes proposals (GET) and applies them (POST). The two * properties pinned here: * - proposals are deterministic for a fixed seed (GET twice, same result), * - apply is idempotent (POST twice: the second writes 0 rows). * * The DB is reset to the clean seed (which now includes the skill taxonomy and * volunteer verified levels) before the suite, so the proposals are stable. */ /** * The first instance id, resolved once while the admin session (which holds * `schedule.publish`) is live. The instances helper route is coordinator-gated, * so a volunteer session cannot read it — fetching it up front keeps the * volunteer test focused on the 403 it actually pins. */ let instanceId: string; test.beforeAll(async ({ browser }) => { const context = await browser.newContext(); const page = await context.newPage(); await resetDb(page); instanceId = await firstInstanceId(page); await context.close(); }); test.describe("auto-fill", () => { test("proposals are deterministic and apply is a no-op on the second run", 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, "coordinator@glenloch.local"); expect(instanceId, "a first instance should exist").toBeTruthy(); const url = `${BASE}/api/dev/schedule/${instanceId}/auto-fill`; // GET twice — deterministic for a fixed seed. const first = await (await page.request.get(url)).json(); const second = await (await page.request.get(url)).json(); expect(first).toEqual(second); // The seed pre-assigns only each instance's Audio seat, so Livestream and // Slides are the open seats — the Audio seat must never be re-proposed. expect(first.proposals.length, "proposals should be non-empty").toBeGreaterThan(0); expect( new Set(first.proposals.map((p: { roleName: string }) => p.roleName)), "an already-filled seat is not an open seat", ).not.toContain("Audio"); // Apply once — writes one assignment per still-free proposal. const applied = await ( await page.request.post(url, { data: JSON.stringify({}) }) ).json() as { written: number; proposals: { slotId: string }[] }; expect(applied.written).toBe(applied.proposals.length); expect(applied.written).toBeGreaterThan(0); // Apply again — every seat is now filled, so it is a no-op. const again = await ( await page.request.post(url, { data: JSON.stringify({}) }) ).json() as { written: number }; expect(again.written, "second apply should write 0 rows").toBe(0); expect(errors, `unexpected console/page errors: ${errors.join("; ")}`).toEqual( [], ); }); // Review-focus point: a volunteer cannot auto-fill. The route enforces the // chokepoint directly — a volunteer POST is rejected 403, not 200 and not a // 500. test("volunteer is rejected by the auto-fill route with 403", async ({ page }) => { await page.setViewportSize({ width: 1024, height: 768 }); await signInAs(page, "avery@glenloch.local"); await page.goto(`${BASE}/schedule`); await expect(page).toHaveURL(`${BASE}/schedule`); expect(instanceId).toBeTruthy(); const res = await page.request.post( `${BASE}/api/dev/schedule/${instanceId}/auto-fill`, { data: JSON.stringify({}) }, ); expect(res.status(), "volunteer auto-fill must be rejected").toBe(403); }); });