import { expect, type Page } from "@playwright/test"; const BASE = "http://localhost:3000"; const E2E_PASSWORD = "e2e-password-123"; /** * Shared E2E helpers. Both test files run against the same Postgres, so they * share the dev-console sign-in transport and the DB reset used to establish a * known-clean state before each suite. */ /** * Dev console transport: request a magic link, then complete it in the browser * context so the Auth.js session cookie is set for the navigation under test. */ export async function signInAs(page: Page, email: string) { const res = await page.request.post(`${BASE}/api/dev/magic-link`, { data: { email }, }); expect(res.ok(), "magic-link request should succeed").toBe(true); const body = (await res.json()) as { url?: string }; expect(body.url, "magic-link should return a sign-in url").toBeTruthy(); await page.goto(body.url!); // New accounts are prompted to choose credentials after their first magic // link. Set the deterministic test password, then continue into the shell. if (new URL(page.url()).pathname === "/welcome") { await expect( page.getByRole("heading", { name: "Set your login" }), ).toBeVisible(); await page.getByLabel("Username").fill(email); await page.getByLabel("Password", { exact: true }).fill(E2E_PASSWORD); await page.getByLabel("Confirm password").fill(E2E_PASSWORD); await page.getByRole("button", { name: "Save and continue" }).click(); } // The completion route redirects into the shell, which requires a session. await expect(page).toHaveURL(`${BASE}/schedule`); } /** * Reset the DB to the clean seed. Admin-only (admin.settings): truncates the * seed-owned tables and repopulates them, so each suite starts from a known * state instead of accumulating the mutations of prior runs. Runs from inside * the page (page.request does not share the session cookie). */ export async function resetDb(page: Page) { await signInAs(page, "admin@glenloch.local"); const status = await page.evaluate(async () => { const res = await fetch("/api/dev/reset", { method: "POST" }); return res.status; }); expect(status, "admin reset should succeed").toBe(200); // The reset recreates every user with a new id, so the session obtained // before the reset now points at a deleted row. Re-authenticate so helper // routes called in the same context see a live account (the guard returns 401 // for stale ids). await signInAs(page, "admin@glenloch.local"); } /** * The first service instance's id, read from the instances helper route. Needs * a `schedule.publish` session (coordinator or admin). */ export async function firstInstanceId(page: Page): Promise { const res = await page.request.get(`${BASE}/api/dev/schedule/instances`); expect(res.ok(), "instances route should succeed").toBe(true); const body = (await res.json()) as { instances: { id: string }[] }; expect( body.instances.length, "at least one instance should exist", ).toBeGreaterThan(0); return body.instances[0].id; } export { BASE };