import { readFile, writeFile } from "node:fs/promises"; import { test, expect, type Locator } from "@playwright/test"; import { baseUrl } from "./baseUrl"; import { resolvePath, testPaths } from "./helpers"; // /changelog as a release tool: the Cut and Amend forms come first and the // notes stream in after them one PAGE at a time (?page=N, whole releases up to // a size budget — common/lib/changelog.ts paginateChangelog); a link to an // older release's anchor lands on the page that holds it; and the latest // release can be amended (a patch cut made a minor) or moved back under // [Unreleased], through the same writer as `pnpm ops amend-release`. // // The changelog is the test server's disposable EDITOR_CHANGELOG_FILE, as in // cut-release.spec.ts; "commit" is off everywhere (the commit path is the // controller's unit tests', each in a git repo of its own). const CHANGELOG = resolvePath(testPaths().editorChangelog); const AUTH = { authorization: "Bearer test-worker-token" }; // What cut-release.spec.ts and ops-cut-release.spec.ts leave behind. const RESTING = "# Changelog\n\n## [9.9.9] - 2024-01-01\n- old released bullet\n"; test.afterEach(async () => { await writeFile(CHANGELOG, RESTING); }); // A release of ~60 KB: two of them never share a page (the budget is ~96 KB), // so the pages are [Unreleased] + 9.9.12 | 9.9.11 | 9.9.10 + 9.9.9. function bigRelease(version: string, date: string, word: string): string { const bullet = `- **${word} change.** ${`${word} `.repeat(120)}`; return [`## [${version}] - ${date}`, ...Array.from({ length: 70 }, () => bullet), ""].join("\n"); } const PAGED = [ "# Changelog", "", "## [Unreleased]", "- **A pending note.**", "", bigRelease("9.9.12", "2024-04-01", "twelve"), bigRelease("9.9.11", "2024-03-01", "eleven"), bigRelease("9.9.10", "2024-02-01", "tenths"), "## [9.9.9] - 2024-01-01", "- **The oldest note.**", "", ].join("\n"); const MISCUT = [ "# Changelog", "", "## [9.9.10] - 2024-02-01", "- **A minor's worth of features.**", "", "## [9.9.9] - 2024-01-01", "- old released bullet", "", ].join("\n"); // A click before React hydrates submits nothing a client action sees. async function hydrated(form: Locator): Promise { await expect(async () => { const ok = await form.evaluate((el) => Object.keys(el).some((k) => k.startsWith("__reactProps")), ); expect(ok).toBe(true); }).toPass({ timeout: 15_000 }); } test("the forms come first and the notes are paged, older pages by link", async ({ page }) => { await writeFile(CHANGELOG, PAGED); await page.goto("/changelog"); // The shell: both forms, ahead of the notes. const cut = page.getByRole("button", { name: /^cut release$/i }); await expect(cut).toBeEnabled(); await expect(page.getByRole("button", { name: /^amend release$/i })).toBeVisible(); const notes = page.getByRole("region", { name: "Release notes" }); await expect(notes).toBeVisible(); const cutBox = await cut.boundingBox(); const notesBox = await notes.boundingBox(); expect(cutBox!.y).toBeLessThan(notesBox!.y); // Page 1: [Unreleased] and the newest release, nothing older. const nav = notes.getByRole("navigation", { name: "Changelog pages" }).first(); await expect(nav).toContainText("Page 1 of 3"); await expect(notes.getByText("A pending note.")).toBeVisible(); await expect(notes.locator("#v9\\.9\\.12")).toHaveCount(1); await expect(notes.locator("#v9\\.9\\.11")).toHaveCount(0); await nav.getByRole("link", { name: "Older →" }).click(); await expect(page).toHaveURL(/\/changelog\?page=2$/); await expect(nav).toContainText("Page 2 of 3"); await expect(notes.locator("#v9\\.9\\.11")).toHaveCount(1); await expect(notes.getByText("A pending note.")).toHaveCount(0); await nav.getByRole("link", { name: "← Newer" }).click(); await expect(page).toHaveURL(/\/changelog$/); await expect(nav).toContainText("Page 1 of 3"); // A page past the end is the last page. await page.goto("/changelog?page=99"); await expect(nav).toContainText("Page 3 of 3"); await expect(notes.getByText("The oldest note.")).toBeVisible(); }); test("a link to an older release's anchor lands on the page that holds it", async ({ page }) => { await writeFile(CHANGELOG, PAGED); // By the release's own id… await page.goto("/changelog#v9.9.10"); await expect(page).toHaveURL(/\/changelog\?page=3#v9\.9\.10$/); // (The id is an empty span inside the heading; the heading is what shows.) await expect(page.locator("h2:has(#v9\\.9\\.10)")).toBeInViewport(); // …and by the date id a link copied before this page had pages carries. await page.goto("/changelog#2024-01-01"); await expect(page).toHaveURL(/\/changelog\?page=3#2024-01-01$/); await expect(page.locator('[id="2024-01-01"]')).toBeInViewport(); // An anchor on page 1 stays on page 1. await page.goto("/changelog#unreleased"); await expect(page.locator("#unreleased")).toBeVisible(); await expect(page).toHaveURL(/\/changelog#unreleased$/); // The index of every release links to each one's page. const notes = page.getByRole("region", { name: "Release notes" }); await notes.getByText(/^All releases/).click(); await expect(notes.getByRole("link", { name: "9.9.11" })).toHaveAttribute( "href", "/changelog?page=2#v9.9.11", ); }); test("amend round trip: a patch made a minor, then moved back and cut again", async ({ page }) => { await writeFile(CHANGELOG, MISCUT); await page.goto("/changelog"); const form = page.getByRole("form", { name: "Amend release 9.9.10" }); await hydrated(form); await expect(form).toContainText("a bump counts from 9.9.9"); // patch → minor, counted from 9.9.9. await form.getByLabel("Version").selectOption("minor"); await form.getByRole("checkbox", { name: "Commit the amend" }).uncheck(); await form.getByRole("button", { name: /^amend release$/i }).click(); await expect( page.getByRole("status").filter({ hasText: "Amended 9.9.10 -> [9.10.0] - 2024-02-01 (not committed)." }), ).toBeVisible(); expect(await readFile(CHANGELOG, "utf8")).toBe(MISCUT.replace("[9.9.10]", "[9.10.0]")); // The page now offers 9.10.0 as the latest release. const amended = page.getByRole("form", { name: "Amend release 9.10.0" }); await expect(amended).toBeVisible(); await expect(page.getByText("Last released: 9.10.0")).toBeVisible(); // Moved back under [Unreleased]: the cut form has something to cut again. await hydrated(amended); await amended.getByRole("checkbox", { name: "Commit the amend" }).uncheck(); await amended.getByRole("button", { name: "Move back to [Unreleased]" }).click(); await expect( page.getByRole("status").filter({ hasText: "Withdrew 9.10.0: 1 note back under [Unreleased]" }), ).toBeVisible(); expect(await readFile(CHANGELOG, "utf8")).toBe(MISCUT.replace("[9.9.10] - 2024-02-01", "[Unreleased]")); await expect(page.locator("#version-editor")).toBeEnabled(); }); test("only the latest release can be amended: a stale form is refused, nothing written", async ({ page }) => { await writeFile(CHANGELOG, MISCUT); await page.goto("/changelog"); const form = page.getByRole("form", { name: "Amend release 9.9.10" }); await hydrated(form); // Someone cuts 9.9.11 while the form is open. const moved = MISCUT.replace("# Changelog\n\n", "# Changelog\n\n## [9.9.11] - 2024-03-01\n- newer\n\n"); await writeFile(CHANGELOG, moved); await form.getByLabel("Version").selectOption("major"); await form.getByRole("checkbox", { name: "Commit the amend" }).uncheck(); await form.getByRole("button", { name: /^amend release$/i }).click(); await expect( page.getByRole("alert").filter({ hasText: "Only the latest release can be amended: 9.9.10 is older than 9.9.11.", }), ).toBeVisible(); expect(await readFile(CHANGELOG, "utf8")).toBe(moved); }); test("POST /api/ops/amend-release: the same writer over HTTP", async ({ request }) => { await writeFile(CHANGELOG, MISCUT); const post = (data: Record) => request.post(`${baseUrl}/api/ops/amend-release`, { headers: AUTH, data }); const older = await post({ workspace: "editor", release: "9.9.9", version: "minor" }); expect(older.status()).toBe(400); expect(await older.json()).toEqual({ ok: false, error: "Only the latest release can be amended: 9.9.9 is older than 9.9.10.", written: false, }); const bad = await post({ workspace: "editor", release: "9.9.10", version: "next" }); expect(bad.status()).toBe(400); expect((await bad.json()).error).toMatch(/"patch", "minor" or "major"/); expect(await readFile(CHANGELOG, "utf8")).toBe(MISCUT); const ok = await post({ workspace: "editor", release: "9.9.10", version: "minor" }); expect(ok.status()).toBe(200); const body = await ok.json(); expect(body.message).toBe("Amended 9.9.10 -> [9.10.0] - 2024-02-01 (not committed)."); expect(body.result).toMatchObject({ changed: true, to: { version: "9.10.0" } }); // Again: idempotent. const again = await post({ workspace: "editor", release: "9.9.10", version: "minor" }); expect(again.status()).toBe(200); expect((await again.json()).result.changed).toBe(false); expect(await readFile(CHANGELOG, "utf8")).toBe(MISCUT.replace("[9.9.10]", "[9.10.0]")); });