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