/** * The admin's flows (WEBSITE-1.0-PLAN ยง13): edit โ†’ preview (click through the site) โ†’ publish โ†’ * the public site changes; discard one change; a shared preview link works signed out and stops * after revoking; restore an older version; switch template in preview without losing anything. * Runs on the e2e server (e2e/server.ts): the demo church on Heritage, an admin signed in directly. */ import AxeBuilder from "@axe-core/playwright"; import { expect, test, type BrowserContext, type Page } from "@playwright/test"; const BACKEND = "http://127.0.0.1:5310"; async function reset(page: Page) { expect((await page.request.post(`${BACKEND}/test/demo?template=heritage`)).ok()).toBeTruthy(); } async function signIn(context: BrowserContext, role = "editor") { const res = await context.request.post(`${BACKEND}/test/session?role=${role}`); const { cookie } = await res.json(); await context.addCookies([{ name: cookie.name, value: cookie.value, url: "http://localhost:3310" }]); } async function aboutTitle(page: Page, title: string) { await page.goto("/admin/pages"); await page.getByRole("link", { name: "About us", exact: true }).first().click(); // The list page has a Title field too (Add a page): wait for the page's own editor. await expect(page).toHaveURL(/\/admin\/pages\/[0-9a-f-]+$/); await expect(page.getByRole("heading", { level: 1, name: "About us" })).toBeVisible(); await page.getByLabel("Title", { exact: true }).fill(title); await page.getByRole("button", { name: "Save details" }).click(); await expect(page.getByText("Saved as a draft.")).toBeVisible(); } test.beforeEach(async ({ page }) => reset(page)); test("edit, preview the whole site, publish: then visitors see it", async ({ page, context, browser }) => { await signIn(context); await aboutTitle(page, "Our story"); await page.goto("/admin"); await expect(page.getByRole("link", { name: "1 change not published" })).toBeVisible(); // Preview: the real addresses, with the bar; click through to the changed page. await page.getByRole("link", { name: "Preview", exact: true }).click(); await expect(page.getByRole("region", { name: "Preview" })).toBeVisible(); await page.getByRole("navigation", { name: "Main" }).getByRole("link", { name: "Our story" }).click(); await expect(page.getByRole("heading", { level: 1, name: "Our story" })).toBeVisible(); // Someone else still sees the published site. const visitor = await browser.newPage(); await visitor.goto("/about"); await expect(visitor.getByRole("heading", { level: 1, name: "About us" })).toBeVisible(); await expect(visitor.getByRole("region", { name: "Preview" })).toHaveCount(0); await page.getByRole("button", { name: "Publish" }).click(); await expect(page).toHaveURL(/\/admin\/publish/); await expect(page.getByText("Nothing waiting to be published")).toBeVisible(); await visitor.goto("/about"); await expect(visitor.getByRole("heading", { level: 1, name: "Our story" })).toBeVisible(); await visitor.close(); }); test("discard one change", async ({ page, context }) => { await signIn(context); await aboutTitle(page, "Changed my mind"); await page.goto("/admin/publish"); await expect(page.getByRole("heading", { name: "1 change not published" })).toBeVisible(); await page.getByRole("button", { name: "Discard" }).click(); await expect(page.getByText("Discarded: back to the published version.")).toBeVisible(); await expect(page.getByText("Nothing waiting to be published")).toBeVisible(); }); test("a shared preview link works signed out, and stops when revoked", async ({ page, context, browser }) => { await signIn(context); await aboutTitle(page, "For the pastor"); await page.goto("/admin/publish"); await page.getByRole("button", { name: "Make a preview link" }).click(); const url = (await page.locator("code").textContent())!.trim(); const pastor = await browser.newContext(); const p = await pastor.newPage(); await p.goto(new URL(url).pathname); await expect(p.getByRole("region", { name: "Preview" })).toBeVisible(); await expect(p.getByRole("button", { name: "Publish" })).toHaveCount(0); await p.goto("/about"); await expect(p.getByRole("heading", { level: 1, name: "For the pastor" })).toBeVisible(); await page.getByRole("button", { name: "Stop it" }).click(); await expect(page.getByText("The link stopped working.")).toBeVisible(); await p.goto("/about"); await expect(p.getByRole("heading", { level: 1, name: "About us" })).toBeVisible(); await expect(p.getByRole("region", { name: "Preview" })).toHaveCount(0); await pastor.close(); }); test("restore an older version, then publish it", async ({ page, context }) => { await signIn(context); await aboutTitle(page, "Version two"); await page.goto("/admin/publish"); await page.getByRole("button", { name: "Publish all changes" }).click(); await expect(page.getByText("Published: visitors see it now.")).toBeVisible(); await page.getByRole("button", { name: "Restore" }).click(); await expect(page.getByText("Version 1 is back in your drafts")).toBeVisible(); await page.getByRole("button", { name: "Publish all changes" }).click(); // Wait for it, as above: on real Postgres the publish takes long enough to race the page load. await expect(page.getByText("Published: visitors see it now.")).toBeVisible(); await page.goto("/about"); await expect(page.getByRole("heading", { level: 1, name: "About us" })).toBeVisible(); }); test("switch template in the preview, and back, without losing anything", async ({ page, context }) => { await signIn(context); await page.goto("/admin/template"); await page.getByRole("button", { name: "Use Bold" }).click(); await expect(page.getByText("Switched to Bold.")).toBeVisible(); await page.goto("/preview/start"); await expect(page.locator("html")).toHaveAttribute("data-template", "bold"); await expect(page.getByRole("heading", { level: 1, name: "A home for everyone on the hill" })).toBeVisible(); await page.goto("/admin/template"); await page.getByRole("button", { name: "Use Heritage" }).click(); await expect(page.getByText("Switched to Heritage.")).toBeVisible(); await page.goto("/admin/publish"); await expect(page.getByText("Nothing waiting to be published")).toBeVisible(); }); test("signed out, the admin sends people to sign in and back to the same page", async ({ page }) => { const res = await page.request.get("/admin/pages", { maxRedirects: 0 }); expect([303, 307, 308]).toContain(res.status()); expect(res.headers().location).toContain("/api/auth/signin?returnTo=%2Fadmin%2Fpages"); }); test("a Viewer sees the admin but can't publish", async ({ page, context }) => { await signIn(context, "viewer"); await page.goto("/admin/publish"); await expect(page.getByText("Nothing waiting to be published")).toBeVisible(); await expect(page.getByRole("button", { name: /Publish/ })).toHaveCount(0); }); test("an event added and published shows on the site at once (its own Publish)", async ({ page, context }) => { await signIn(context); await page.goto("/admin/events"); await page.getByRole("button", { name: "Add an event" }).click(); await page.getByLabel("Title").fill("Prayer breakfast"); await page.getByLabel("Date", { exact: true }).fill(new Date(Date.now() + 3 * 864e5).toISOString().slice(0, 10)); await page.getByLabel("Where", { exact: true }).fill("The hall"); await page.getByRole("button", { name: "Save" }).click(); await expect(page.getByText("Not published").first()).toBeVisible(); await page.getByRole("listitem").filter({ hasText: "Prayer breakfast" }).getByRole("button", { name: "Publish" }).click(); await expect(page.getByText("On the site now.")).toBeVisible(); await page.goto("/events"); await expect(page.getByRole("link", { name: "Prayer breakfast" })).toBeVisible(); }); test("the admin's screens are accessible", async ({ page, context }) => { // Thirteen screens, each checked by axe. test.setTimeout(240_000); await signIn(context, "owner"); for (const path of ["/admin", "/admin/template", "/admin/pages", "/admin/ministries", "/admin/leaders", "/admin/messages", "/admin/events", "/admin/linked", "/admin/media", "/admin/publish", "/admin/settings", "/admin/people", "/admin/audit"]) { await page.goto(path); await expect(page.getByRole("heading", { level: 1 })).toBeVisible(); const axe = await new AxeBuilder({ page }).withTags(["wcag2a", "wcag2aa", "wcag21aa", "wcag22aa"]).analyze(); expect(axe.violations.map((v) => `${path}: ${v.id} (${v.nodes.length}) ${v.nodes[0]?.target}`)).toEqual([]); } });