import { test, expect, type Page } from "@playwright/test"; import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import * as urls from "./urls"; import { WARM_TIMEOUT, warm } from "./warm"; // --------------------------------------------------------------------------- // LEARNED SPELLINGS (plans/umtool-redesign.md, R6.3) over the editor stub, // which runs the REAL report-schedule action and learned.json writer over the // fixture's article store (common/testing/schedule/opsStub.ts). A create of a // watch whose terms name someone runs the stub's proposal pass at once: "Vote // Rigg" kept (proposed, its proving line sitechan/sv1), "Vote Rick" kept // (heard; its candidates dismissed four runs running — suggest removing), // "Boat Rig" dropped (no hits). // // the series page's Learned spellings: the lit list, the proving line on the // reader with its cite, suggest removing · Promote to search alias: the plan // (the list chosen, the alias as it would be written), Confirm promote — // written through the editor's alias writer · Remove: plan, then confirm — // gone, and a new pass does not learn it again · the triage page names the // candidate a learned spelling brought // // WRITES articles/.series/r63-*, articles/.triage/r63-*, /search-aliases.json // (removed after) // // R63_SHOTS= also saves the screenshots (1366×768, both themes). // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const ARTICLES = path.join(FIXTURE, "articles"); const ALIASES = path.join(FIXTURE, "search-aliases.json"); const SHOTS = process.env.R63_SHOTS ?? ""; const learned = (id: string) => JSON.parse(readFileSync(path.join(ARTICLES, ".series", id, "learned.json"), "utf8")); function clean() { for (const dir of [path.join(ARTICLES, ".series"), path.join(ARTICLES, ".triage")]) { if (!existsSync(dir)) continue; for (const n of readdirSync(dir).filter((x) => x.startsWith("r63-"))) rmSync(path.join(dir, n), { recursive: true, force: true }); } rmSync(ALIASES, { force: true }); } async function shoot(page: Page, name: string) { if (!SHOTS) return; mkdirSync(SHOTS, { recursive: true }); const was = await page.evaluate(() => document.documentElement.getAttribute("data-theme")); await page.setViewportSize({ width: 1366, height: 768 }); for (const theme of ["dark", "light"] as const) { await page.evaluate((t) => document.documentElement.setAttribute("data-theme", t), theme); await page.waitForTimeout(150); await page.screenshot({ path: path.join(SHOTS, `${name}-1366x768-${theme}.png`) }); } await page.evaluate((t) => (t ? document.documentElement.setAttribute("data-theme", t) : document.documentElement.removeAttribute("data-theme")), was); await page.setViewportSize({ width: 1280, height: 720 }); } // Every plan, promote, remove and proposal is one `tsx opsStub.ts` the editor stub // SPAWNS (fixtures/editor-stub.mjs): a cold start of the whole controller graph, // ~3 s on an idle machine and 15–25 s beside a render's encode (measured // 2026-10-30, load 48). The first expectation after each one waits for that, not // for the default 5 s. const STUB = { timeout: 45_000 }; async function api(page: Page, body: Record) { const r = await page.request.post("/api/schedules", { data: body }); const b = (await r.json()) as Record; expect(r.ok(), JSON.stringify(b)).toBe(true); return b; } test.describe.configure({ mode: "serial" }); test.beforeAll(async ({ playwright }) => { test.setTimeout(WARM_TIMEOUT); clean(); await warm(playwright, [urls.seriesHref("r63-none"), urls.triageHref("r63-none", 1), "/"]); }); test.afterAll(() => clean()); test("a watch's Learned spellings: made by its first proposal pass; the lit list, the proving line with its cite, suggest removing", async ({ page }) => { test.setTimeout(90_000); await page.goto("/"); const made = await api(page, { action: "create", shape: "watch", id: "r63-watch", topic: "R63 watch", question: "Who says the vote was rigged?", channels: ["sitechan"], cadence: "weekly mon 06:00", terms: "Ada Ballot|rigged", regex: true }); expect(made.learn).toMatchObject({ names: ["Ada Ballot"] }); expect(learned("r63-watch")).toMatchObject({ owner: "r63-watch", termsKey: "re:Ada Ballot|rigged", learned: [{ spelling: "Vote Rigg" }, { spelling: "Vote Rick" }], dropped: [{ spelling: "Boat Rig", reason: "no-hits" }] }); await page.goto(urls.seriesHref("r63-watch")); const panel = page.locator("[data-series-learned]"); await expect(panel).toContainText("terms + 2 learned"); const list = panel.getByRole("group", { name: "Learned spellings" }); await expect(list.locator("[data-learned-spelling]")).toHaveCount(2); const rigg = list.locator("[data-learned-spelling='Vote Rigg']"); await expect(rigg).toContainText("“Vote Rigg” → Ada Ballot"); await expect(rigg).toContainText("2 recordings · proposed"); const rick = list.locator("[data-learned-spelling='Vote Rick']"); await expect(rick).toHaveAttribute("data-suggest-removing", ""); await expect(rick).toContainText("suggest removing"); // The lit one's proving line on the reader, its cite. await expect(rigg).toHaveAttribute("data-lit", ""); const detail = panel.locator("[data-learned-detail='Vote Rigg']"); await expect(detail.locator("[data-learned-quote]")).toHaveText("“The vote was rigged and everybody knew.”"); await expect(detail.locator("[data-learned-cite]")).toHaveAttribute("data-learned-cite", "sitechan/sv1 at 0:03"); await expect(detail.getByRole("link", { name: "sitechan/sv1 at 0:03" })).toHaveAttribute("href", "https://priv.example/?v=sitechan%2Fsv1&t=3"); // Each window the judge read, with its verdict. const windows = detail.getByRole("list", { name: "windows the judge read" }); await expect(windows.locator("li")).toHaveCount(3); await expect(windows.locator("li").nth(0)).toContainText("the name"); await expect(windows.locator("li").nth(1)).toContainText("not the name"); await expect(windows.locator("li").nth(1)).toContainText("another person (stub)"); // j moves the light (focus in the list): the heard one, with why it is suggested for removal. await rigg.focus(); await page.keyboard.press("j"); await expect(rick).toHaveAttribute("data-lit", ""); await expect(panel.locator("[data-learned-detail='Vote Rick']")).toContainText("heard by the judge in triage 1"); await expect(panel.locator("[data-learned-suggestion]")).toContainText("suggested for removal"); await expect(panel.locator("[data-learned-pass]")).toContainText("2 of 20 kept · last pass"); await panel.locator("[data-learned-dropped] summary").click(); await expect(panel.locator("[data-learned-dropped]")).toContainText("“Boat Rig” → Ada Ballot"); await expect(panel.locator("[data-learned-dropped]")).toContainText("no hits"); await page.keyboard.press("Escape"); await rigg.click(); await shoot(page, "learned-spellings"); }); test("Promote to search alias: the plan (the list, the alias as written), then Confirm promote — through the editor's alias writer", async ({ page }) => { test.setTimeout(90_000); await page.goto(urls.seriesHref("r63-watch")); const panel = page.locator("[data-series-learned]"); await panel.locator("[data-learned-spelling='Vote Rigg']").click(); await panel.getByRole("button", { name: "Promote to search alias" }).click(); const plan = panel.getByRole("region", { name: "promote Vote Rigg" }); await expect(plan).toHaveAttribute("data-promote-action", "new", STUB); await expect(plan).toHaveAttribute("data-promote-to", "global", STUB); await expect(plan.locator("[data-promote-why]")).toHaveText('A new alias "Ada Ballot" firing on "Ada Ballot" and "Vote Rigg".'); await expect(plan).toContainText("ada ballot, vote rigg"); await expect(plan).toContainText("\\bAda Ballot\\b|\\bVote Rigg\\b"); await expect(plan).toContainText("learned by watch r63-watch from sitechan/sv1 at 0:03"); // The sites are offered; choosing one asks for its plan (nothing written). const select = plan.getByRole("combobox", { name: "Alias list" }); await expect(select.locator("option")).toHaveText(["Global (every site)", "priv", "pub"]); await select.selectOption("priv"); await expect(plan).toHaveAttribute("data-promote-to", "priv", STUB); await expect(plan).toContainText("sites/priv/search-aliases.json"); await select.selectOption("global"); await expect(plan).toHaveAttribute("data-promote-to", "global", STUB); await expect(plan.locator("[data-promote-file]")).toHaveText("search-aliases.json"); expect(existsSync(ALIASES)).toBe(false); await shoot(page, "learned-promote-plan"); await expect(plan.getByRole("button", { name: "Confirm promote" })).toBeFocused(); await plan.getByRole("button", { name: "Confirm promote" }).click(); await expect(panel.locator("[data-learned-status]")).toContainText("“Vote Rigg” is a search alias in the global list", STUB); await expect(panel.locator("[data-learned-spelling='Vote Rigg']")).toHaveAttribute("data-promoted", "global"); const written = JSON.parse(readFileSync(ALIASES, "utf8")); expect(written.aliases.at(-1)).toEqual({ id: "ada-ballot", label: "Ada Ballot", triggers: ["ada ballot", "vote rigg"], suggestion: "\\bAda Ballot\\b|\\bVote Rigg\\b", useRegex: true, enabled: true, note: "learned by watch r63-watch from sitechan/sv1 at 0:03" }); // Keep it closes a plan, nothing written. await panel.locator("[data-learned-spelling='Vote Rick']").click(); await panel.getByRole("button", { name: "Promote to search alias" }).click(); await panel.getByRole("region", { name: "promote Vote Rick" }).getByRole("button", { name: "Keep it" }).click(); await expect(panel.getByRole("region", { name: "promote Vote Rick" })).toHaveCount(0, STUB); }); test("Remove: plan, then confirm — gone, blocked, and a new proposal pass does not learn it again", async ({ page }) => { test.setTimeout(90_000); await page.goto(urls.seriesHref("r63-watch")); const panel = page.locator("[data-series-learned]"); await panel.locator("[data-learned-spelling='Vote Rick']").click(); await panel.getByRole("button", { name: "Remove" }).click(); const region = panel.getByRole("region", { name: "remove Vote Rick" }); await expect(region).toContainText("It is never learned again", STUB); await expect(region.getByRole("button", { name: "Confirm remove" })).toBeFocused(); await region.getByRole("button", { name: "Confirm remove" }).click(); await expect(panel.locator("[data-learned-spelling]")).toHaveCount(1, STUB); expect(learned("r63-watch").blocked).toMatchObject([{ spelling: "Vote Rick", means: "Ada Ballot" }]); await panel.getByRole("button", { name: "Propose spellings" }).click(); await expect(panel.locator("[data-learned-status]")).toContainText("Proposal pass queued", STUB); await expect(panel.locator("[data-learned-status]")).toContainText("asks about Ada Ballot"); await page.reload(); await expect(page.locator("[data-series-learned] [data-learned-spelling]")).toHaveCount(1); const f = learned("r63-watch"); expect(f.learned.map((l: { spelling: string }) => l.spelling)).toEqual(["Vote Rigg"]); expect(f.dropped.at(-1)).toMatchObject({ spelling: "Vote Rick", reason: "blocked" }); expect(f.passes.map((p: { trigger: string }) => p.trigger)).toEqual(["create", "ops"]); }); test("the triage page names the candidate a learned spelling brought (terms + N learned)", async ({ page }) => { test.setTimeout(90_000); await page.goto("/"); await api(page, { action: "tick-now", id: "r63-watch" }); await page.goto(urls.triageHref("r63-watch", 1)); await expect(page.locator("[data-tier0-learned]")).toContainText("terms + 1 learned (1 by a learned spelling alone)"); await expect(page.locator("[data-candidate-learned='Vote Rigg']")).toHaveText("learned “Vote Rigg”"); await shoot(page, "learned-triage"); });