import { test, expect, type Page } from "@playwright/test"; import { signInAs, resetDb, BASE } from "./dev"; /** * Admin surface: people lifecycle, reminder rules, notification log, and the * publish controls on the schedule. These are the actions that previously had * data/API support but no UI. */ /** The seed's draft week (its second Sunday). */ async function draftWeek(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; occursOn: string; status: string }[]; }; const draft = body.instances.find((i) => i.status === "draft"); expect(draft, "the seed should include a draft instance").toBeTruthy(); return draft!.occursOn.slice(0, 10); } test.describe("admin", () => { test.beforeAll(async ({ browser }) => { const context = await browser.newContext(); const page = await context.newPage(); await resetDb(page); await context.close(); }); test("admin adds, edits, approves, and removes a person", async ({ page }) => { await page.setViewportSize({ width: 1280, height: 1000 }); await signInAs(page, "admin@glenloch.local"); await page.goto(`${BASE}/admin`); await expect(page.getByRole("heading", { name: "Admin", level: 1 })).toBeVisible(); await expect(page.getByText("People", { exact: true })).toBeVisible(); await expect(page.getByText("Reminder rules", { exact: true })).toBeVisible(); await expect(page.getByText("Notifications", { exact: true })).toBeVisible(); // --- add an account that needs approval const email = `e2e-admin-${Date.now()}@example.com`; await page.getByLabel("First name").fill("E2E"); await page.getByLabel("Last name").fill("Volunteer"); await page.getByLabel("Email").fill(email); await page.getByLabel("Role").selectOption("volunteer"); await page.getByLabel("Needs approval before signing in").check(); await page.getByRole("button", { name: "Add user" }).click(); // The admin gets a sign-in link to hand over. await expect(page.getByText(/api\/dev\/complete/).first()).toBeVisible(); const row = page.getByRole("row", { name: new RegExp(email) }); await expect(row).toBeVisible(); await expect(row.getByText("pending approval")).toBeVisible(); await row.getByRole("button", { name: "Approve" }).click(); await expect(row.getByText("active", { exact: true })).toBeVisible(); await expect(row.getByRole("button", { name: "Approve" })).toHaveCount(0); // --- edit every editable field through the row editor await row.getByRole("button", { name: "Edit" }).click(); const editor = page.getByRole("form", { name: `Edit ${email}` }); await expect(editor).toBeVisible(); await editor.getByLabel("First name").fill("Edited"); await editor.getByLabel("Display name").fill("Edited Volunteer"); await editor.getByLabel("Role").selectOption("coordinator"); await editor.getByLabel("Phone").fill("+15551234567"); await editor.getByLabel("Emergency contact").fill("Sam — +15550000000"); await editor.getByLabel("Notes").fill("E2E edit"); await editor.getByRole("button", { name: "Save changes" }).click(); await expect(page.getByText("Changes saved.")).toBeVisible(); const edited = page.getByRole("row", { name: new RegExp(email) }); await expect(edited.getByText("Edited Volunteer")).toBeVisible(); await expect(edited.getByText("coordinator")).toBeVisible(); await expect(edited.getByText("+15551234567")).toBeVisible(); const list = (await (await page.request.get(`${BASE}/api/dev/users`)).json()) as { users: { id: string; email: string; firstName: string; systemRole: string; phoneE164: string | null; }[]; }; const saved = list.users.find((user) => user.email === email); expect(saved?.firstName, "the edit should persist").toBe("Edited"); expect(saved?.systemRole).toBe("coordinator"); expect(saved?.phoneE164).toBe("+15551234567"); // --- skill levels: add (default none), raise, then delete a row await edited.getByRole("button", { name: "Edit" }).click(); const skillEditor = page.getByRole("form", { name: `Edit ${email}` }); await expect(skillEditor.getByText("No skills recorded yet.")).toBeVisible(); const addSkill = skillEditor.getByLabel("Add skill"); const firstSkill = await addSkill .locator('option:not([disabled])') .nth(1) .getAttribute("value"); await addSkill.selectOption(firstSkill!); await skillEditor.getByRole("button", { name: "Add skill" }).click(); await expect(page.getByText("Skill added at level none.")).toBeVisible(); const levelSelect = skillEditor.getByLabel(/Skill level for/).first(); await expect(levelSelect, "a new entry starts at none").toHaveValue("none"); await levelSelect.selectOption("qualified"); await expect(page.getByText("Skill level updated.")).toBeVisible(); await expect(skillEditor.getByLabel(/Skill level for/).first()).toHaveValue( "qualified", ); // A second entry, then delete the first row outright. const secondSkill = await addSkill .locator('option:not([disabled])') .nth(1) .getAttribute("value"); await addSkill.selectOption(secondSkill!); await skillEditor.getByRole("button", { name: "Add skill" }).click(); await expect(skillEditor.getByLabel(/Skill level for/)).toHaveCount(2); await skillEditor.getByRole("button", { name: "Remove" }).first().click(); await expect(page.getByText("Skill entry removed.")).toBeVisible(); await expect(skillEditor.getByLabel(/Skill level for/)).toHaveCount(1); await page .getByRole("row", { name: new RegExp(email) }) .getByRole("button", { name: "Cancel" }) .click(); // --- deactivate, then remove await edited.getByRole("button", { name: "Deactivate" }).click(); await expect( page.getByRole("row", { name: new RegExp(email) }).getByText("deactivated"), ).toBeVisible(); page.once("dialog", (dialog) => dialog.accept()); await page .getByRole("row", { name: new RegExp(email) }) .getByRole("button", { name: "Delete" }) .click(); await expect(page.getByRole("row", { name: new RegExp(email) })).toHaveCount(0); // Removing the person removes their skill-level entries too. const remaining = (await ( await page.request.get(`${BASE}/api/dev/user-skills?userId=${saved!.id}`) ).json()) as { rows: unknown[] }; expect( remaining.rows, "deleting a user should delete their competency rows", ).toHaveLength(0); }); test("admin can send a test email and toggle a reminder rule", async ({ page }) => { await page.setViewportSize({ width: 1024, height: 900 }); await signInAs(page, "admin@glenloch.local"); await page.goto(`${BASE}/admin`); // The email panel also offers a test send; this one belongs to the log. await page .locator("#notifications") .getByRole("button", { name: "Send test email" }) .click(); await expect(page.getByText(/Test message sent via/)).toBeVisible(); const disable = page.getByRole("button", { name: "Disable" }).first(); await expect(disable).toBeVisible(); await disable.click(); await expect(page.getByRole("button", { name: "Enable" }).first()).toBeVisible(); }); test("admin can publish a draft week from the schedule", async ({ page }) => { await page.setViewportSize({ width: 1024, height: 900 }); await signInAs(page, "admin@glenloch.local"); const week = await draftWeek(page); await page.goto(`${BASE}/schedule?week=${week}`); await page.getByRole("button", { name: "Publish", exact: true }).first().click(); // Open seats are a deliberate override, not a silent publish. const publishAnyway = page.getByRole("button", { name: "Publish anyway" }).first(); await expect(publishAnyway).toBeVisible(); await publishAnyway.click(); await expect(page.getByText(/Published\. Assignment emails/)).toBeVisible(); await expect(page.getByRole("button", { name: "Unpublish" }).first()).toBeVisible(); }); test("admin verifies a skill level and edits position requirements", async ({ page }) => { await page.setViewportSize({ width: 1280, height: 1000 }); await signInAs(page, "admin@glenloch.local"); await page.goto(`${BASE}/admin`); const matrixRes = await page.request.get(`${BASE}/api/dev/skills/matrix`); expect(matrixRes.ok(), "matrix route should require and accept the admin").toBe(true); const matrix = (await matrixRes.json()) as { rows: { userId: string; userName: string; skillId: string; skillName: string; verifiedLevel: string; }[]; }; const target = matrix.rows.find((row) => row.verifiedLevel !== "expert"); expect(target, "the seed should include a promotable competency").toBeTruthy(); const levels = ["none", "observer", "assisted", "qualified", "expert"]; const newLevel = levels[levels.indexOf(target!.verifiedLevel) + 1]; const targetRow = page .getByRole("row", { name: new RegExp(target!.skillName) }) .filter({ hasText: target!.userName }) .first(); await targetRow.getByRole("button", { name: "Verify" }).click(); const form = page.getByRole("form", { name: `Verify ${target!.skillName} for ${target!.userName}`, }); await expect(form).toBeVisible(); await form.getByLabel("Level").selectOption(newLevel); await form.getByLabel("Notes (optional)").fill("E2E verification"); await form.getByRole("button", { name: "Save assessment" }).click(); await expect( page.getByText(`${target!.skillName}: ${target!.userName} is now ${newLevel}`), ).toBeVisible(); const afterRes = await page.request.get( `${BASE}/api/dev/skills/matrix?userId=${target!.userId}&skillId=${target!.skillId}`, ); const after = (await afterRes.json()) as { rows: { verifiedLevel: string }[] }; expect(after.rows[0]?.verifiedLevel, "the level should persist").toBe(newLevel); // Requirements editor: the B4 gate refuses a publishable position with no // core skill, and the same set saves when it is not publishable. const positionRow = page.getByRole("row", { name: /^Audio/ }).first(); await positionRow.getByRole("button", { name: "Edit requirements" }).click(); const editor = page.getByRole("form", { name: "Requirements for Audio" }); await expect(editor).toBeVisible(); await editor.getByLabel("Core").first().uncheck(); await editor.getByRole("button", { name: "Save requirements" }).click(); await expect(editor.getByRole("alert")).toContainText("at least one core skill"); await editor.getByLabel(/Publishable/).uncheck(); await editor.getByLabel("Core").first().check(); await editor.getByRole("button", { name: "Save requirements" }).click(); await expect(page.getByText("Requirements saved for Audio.")).toBeVisible(); }); test("admin connects Gmail with an app password and disconnects it", async ({ page, }) => { await page.setViewportSize({ width: 1280, height: 1000 }); await signInAs(page, "admin@glenloch.local"); await page.goto(`${BASE}/admin`); await expect(page.getByText("Email delivery", { exact: true })).toBeVisible(); await expect(page.getByText(/Not connected/)).toBeVisible(); await page.getByLabel("Gmail address").fill("media.team.test@gmail.com"); await page.getByLabel("App password").fill("abcd efgh ijkl mnop"); await page.getByRole("button", { name: "Save Gmail" }).click(); await expect( page.getByText(/Connected as media\.team\.test@gmail\.com/), ).toBeVisible(); // The app password is write-only: the API reports that it exists and never // returns the secret itself. const saved = (await ( await page.request.get(`${BASE}/api/dev/settings`) ).json()) as { settings: Record }; expect(saved.settings.smtpConfigured).toBe(true); expect(saved.settings.smtpUser).toBe("media.team.test@gmail.com"); expect(JSON.stringify(saved)).not.toContain("abcdefghijklmnop"); // Persisted in the database, not just in component state. await page.reload(); await expect( page.getByText(/Connected as media\.team\.test@gmail\.com/), ).toBeVisible(); page.once("dialog", (dialog) => dialog.accept()); await page.getByRole("button", { name: "Remove Gmail" }).click(); await expect(page.getByText(/Not connected/)).toBeVisible(); const cleared = (await ( await page.request.get(`${BASE}/api/dev/settings`) ).json()) as { settings: { smtpConfigured: boolean; smtpUser: string } }; expect(cleared.settings.smtpConfigured).toBe(false); expect(cleared.settings.smtpUser).toBe(""); }); // Never leave fake Gmail credentials behind: the notification tests send // through the fallback transport and must not try to authenticate as Gmail. test.afterAll(async ({ browser }) => { const context = await browser.newContext(); const page = await context.newPage(); await signInAs(page, "admin@glenloch.local"); const current = (await ( await page.request.get(`${BASE}/api/dev/settings`) ).json()) as { settings: { emailFrom: string; notificationEmail: string } }; await page.request.put(`${BASE}/api/dev/settings`, { data: { emailFrom: current.settings.emailFrom, notificationEmail: current.settings.notificationEmail, smtpUser: "", }, }); await context.close(); }); });