import { test, expect, type Locator, 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"; /** A schedule write's click and its answer (POST /api/schedules): each runs the editor stub's real report-schedule * action in a tsx child (editor-stub.mjs, spawnSync -- about 3.4 s on an idle machine, measured), longer than an * expect's 5 s under load. The page is read once the answer is in. */ const clickAnswered = async (page: Page, button: Locator) => { const answered = page.waitForResponse((r) => new URL(r.url()).pathname.startsWith("/api/schedules") && r.request().method() !== "GET", { timeout: 60_000 }); await button.click(); await answered; }; // --------------------------------------------------------------------------- // TRIAGE FIRST (plans/umtool-redesign.md, R6) over the editor stub, which runs // the REAL report-schedule action, store and tick over the fixture's article // store, with a tick's steps stubbed (common/testing/schedule/opsStub.ts): a // triage writes a real reports/.triage///triage.json -- "quiet" in the // terms is candidates and no flag; else a strong flag (sitechan/sv1) and a // weak one (sv2), an invented line dismissed as not verbatim, a window not // relevant -- and an escalation's sweep a done iteration with its trace. // (triage.spec.ts is the song deck's clip triage, a different thing.) // // Create → A recurring report → Watch (and Triage first on a series) · // a quiet tick (below the threshold: no article, $0) · an escalating tick: // the tiers on the triage page and on the run page · the Inbox's Worth // researching card → its board (flags `by triage`, made by umtool) · `y` / // `x` on the lit flag, the precision and the suggested threshold, the card // gone once every flag is marked // // WRITES articles/.series/r6-*, articles/.triage/r6-*, articles/r6-*, // .umtool/triage/r6-*, .umtool/research/r6-* (removed after) // // R6_SHOTS= also saves the screenshots (1366×768 and 1440×900, both themes). // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const ARTICLES = path.join(FIXTURE, "articles"); const UMTOOL = path.join(FIXTURE, ".umtool"); const SHOTS = process.env.R6_SHOTS ?? ""; const series = (id: string) => JSON.parse(readFileSync(path.join(ARTICLES, ".series", id, "series.json"), "utf8")); function clean() { for (const dir of [path.join(ARTICLES, ".series"), path.join(ARTICLES, ".triage"), ARTICLES, path.join(UMTOOL, "triage"), path.join(UMTOOL, "research")]) { if (!existsSync(dir)) continue; for (const n of readdirSync(dir).filter((x) => x.startsWith("r6-"))) rmSync(path.join(dir, n), { recursive: true, 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")); for (const theme of ["dark", "light"] as const) { await page.evaluate((t) => document.documentElement.setAttribute("data-theme", t), theme); for (const [w, h] of [ [1366, 768], [1440, 900], ] as const) { await page.setViewportSize({ width: w, height: h }); await page.waitForTimeout(150); await page.screenshot({ path: path.join(SHOTS, `${name}-${w}x${h}-${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 }); } 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.createHref({ make: "recurring" }), "/api/articles/channels", urls.seriesHref("r6-none"), urls.triageHref("r6-none", 1), "/"]); }); test.afterAll(() => clean()); test("Create → A recurring report → Watch: its terms, topics and threshold, planned (the triage named), then started; a series offers Triage first", async ({ page }) => { test.setTimeout(90_000); await page.goto(urls.createHref({ make: "recurring" })); const form = page.getByRole("form", { name: "a recurring report" }); await form.getByRole("radio", { name: "Series" }).check(); await expect(form.getByRole("checkbox", { name: "triage first" })).not.toBeChecked(); await form.getByRole("checkbox", { name: "triage first" }).check(); await expect(form.getByRole("textbox", { name: "digest tags" })).toBeVisible(); await form.getByRole("radio", { name: "Watch" }).check(); await expect(form.getByRole("checkbox", { name: "triage first" })).toHaveCount(0); await form.getByRole("textbox", { name: "topic" }).fill("R6 watch"); await form.getByRole("textbox", { name: "question" }).fill("Who says the vote was rigged?"); await form.getByRole("textbox", { name: "find a channel" }).fill("site"); await form.getByRole("button", { name: /Site Channel/ }).click(); await form.getByRole("textbox", { name: "terms" }).fill("vote"); await form.getByRole("textbox", { name: "digest tags" }).fill("elections"); await expect(form.getByRole("textbox", { name: "strong flags" })).toHaveValue("1"); await expect(form.getByRole("textbox", { name: "flags of any strength" })).toHaveValue("3"); await shoot(page, "create-watch"); await clickAnswered(page, form.getByRole("button", { name: "Plan the schedule" })); const plan = form.getByRole("region", { name: "plan recurring report" }); await expect(plan).toContainText("Watch r6-watch · weekly · Mon 06:00"); await expect(plan.locator("[data-plan-triage]")).toContainText(/triage first on \S+ \(\$0\): escalates on 1 strong or 3 weak · at most \d+ escalations? a day; no article until one does/); expect(existsSync(path.join(ARTICLES, ".series", "r6-watch"))).toBe(false); await shoot(page, "create-watch-plan"); await expect(form.getByRole("button", { name: "Start the schedule" })).toBeFocused(); await page.keyboard.press("Enter"); await page.waitForURL(/\/series\/r6-watch$/, { timeout: 20_000 }); await expect(page.locator("[data-series-shape]")).toHaveText("watch"); await expect(page.locator("[data-watch-empty]")).toContainText("Nothing escalated yet"); expect(series("r6-watch")).toMatchObject({ shape: "watch", topic: "R6 watch", scope: { channels: ["sitechan"], terms: "vote", topics: ["elections"] } }); }); test("a quiet watch: the triage is below the threshold — no article, $0, the tiers on its triage page", async ({ page }) => { test.setTimeout(90_000); await page.goto("/"); await api(page, { action: "create", shape: "watch", id: "r6-quiet", topic: "R6 quiet", channels: ["sitechan"], cadence: "weekly mon 06:00", terms: "quiet" }); await api(page, { action: "tick-now", id: "r6-quiet" }); const t = series("r6-quiet").ticks.at(-1); expect(t).toMatchObject({ status: "no-change", usd: 0, triage: { k: 1, candidates: 4, flagged: 0, escalated: false } }); expect(t.article).toBeUndefined(); await page.goto(urls.seriesHref("r6-quiet")); const run = page.locator("[data-series-triage] [data-triage-run='1']"); await expect(run).toHaveAttribute("data-escalated", "no"); await expect(run).toContainText("4 candidates · 0 flagged · 3 dismissed"); await run.getByRole("link").click(); await page.waitForURL(/\/series\/r6-quiet\/triage\/1$/); await expect(page.locator("[data-escalation]")).toHaveAttribute("data-escalation", "below"); await expect(page.locator("[data-tier='2']")).toContainText("not escalated: $0 API"); // R6.2: listed without a transcript yet — said, not seen. await expect(page.locator("[data-tier0-untranscribed]")).toContainText("· 2 without a transcript yet"); await expect(page.locator("[data-dismissal]")).toHaveCount(3); await expect(page.locator("[data-flag]")).toHaveCount(0); }); test("R6.1: a partial triage is never quiet — the tick is partial (Your call: See what was missed), the series page and the triage page name what it did not reach", async ({ page }) => { test.setTimeout(90_000); await page.goto("/"); await api(page, { action: "create", shape: "watch", id: "r6-partial", topic: "R6 partial", channels: ["sitechan"], cadence: "weekly mon 06:00", terms: "partial" }); await api(page, { action: "tick-now", id: "r6-partial" }); const t = series("r6-partial").ticks.at(-1); expect(t).toMatchObject({ status: "partial", usd: 0, triage: { k: 1, flagged: 0, escalated: false, partial: "not reached: Site Other (stopped at page 2 of 5)" } }); expect(t.reason).toMatch(/^partial — not reached: Site Other \(stopped at page 2 of 5\); nothing flagged in what was read/); await page.goto(urls.seriesHref("r6-partial")); await expect(page.locator("tr[data-tick-status='partial']")).toContainText("partial — not reached: Site Other"); const run = page.locator("[data-series-triage] [data-triage-run='1']"); await expect(run).toHaveAttribute("data-triage-partial", ""); await expect(run).toContainText("not reached: Site Other (stopped at page 2 of 5)"); await run.getByRole("link").click(); await page.waitForURL(/\/series\/r6-partial\/triage\/1$/); await expect(page.locator("[data-tier0-partial]")).toContainText("partial — not reached: Site Other (stopped at page 2 of 5)"); await shoot(page, "triage-partial"); await page.goto("/"); const card = page.locator("[data-group=your-call] [data-lit-line][data-piece='s:r6-partial'][data-card='tick-partial']"); await expect(card).toBeVisible(); await expect(card).toContainText("not reached: Site Other"); await expect(card.locator("[data-action-link]")).toHaveText("See what was missed"); await expect(card.locator("[data-action-link]")).toHaveAttribute("href", urls.seriesHref("r6-partial")); }); test("an escalating tick: the flagged ids swept as r6-watch-; the tiers on the triage page (each flag and dismissal with its reason) and on the run page", async ({ page }) => { test.setTimeout(90_000); await page.goto(urls.seriesHref("r6-watch")); await clickAnswered(page, page.getByRole("button", { name: "Tick now" })); await expect(page.locator("[data-found]")).toHaveCount(1, { timeout: 30_000 }); const t = series("r6-watch").ticks.at(-1); expect(t).toMatchObject({ status: "done", iteration: 1, usd: 0.37, triage: { k: 1, flagged: 2, escalated: true, ids: ["sv1", "sv2"], usd: { tier1: 0, tier2: 0.37 } } }); expect(t.article).toMatch(/^r6-watch-\d{4}-\d{2}-\d{2}$/); await expect(page.locator("[data-found]")).toHaveAttribute("data-found", t.article); await expect(page.locator("[data-series-triage] [data-triage-run='1']")).toHaveAttribute("data-escalated", "yes"); await page.goto(urls.triageHref("r6-watch", 1)); await expect(page.locator("[data-escalation]")).toHaveAttribute("data-escalation", "escalate"); await expect(page.locator("[data-escalation]")).toContainText("Escalated — 1 strong flag, 1 weak flag (threshold: 1 strong or 3 weak)"); await expect(page.locator("[data-tier='0']")).toContainText("4 candidates of 4 new by id since the last run"); await expect(page.locator("[data-tier='1']")).toContainText("2 flagged · 2 dismissed"); const tier2 = page.locator("[data-tier='2']"); await expect(tier2).toHaveAttribute("data-escalated", "yes"); await expect(tier2.locator("[data-tier2-run]")).toHaveText(`${t.article} i1`); await expect(tier2).toContainText("done · 1 finding"); // The lit flag's line on the reader, its cite and the judge's reason; a dismissal keeps its own. await expect(page.locator("[data-flag='f1']")).toHaveAttribute("data-strength", "strong"); await expect(page.locator("[data-detail='f1']")).toContainText("“The vote was rigged and everybody knew.”"); await expect(page.locator("[data-detail='f1']")).toContainText("why: The speaker says the vote was rigged (stub)."); await page.locator("[data-dismissal='verbatim']").click(); await expect(page.locator("[data-dismissal-reason]")).toHaveText("why: not verbatim in its window"); await expect(page.locator("[data-detail='d1']")).toContainText("Everybody rigged the count on purpose."); await page.locator("[data-flag='f1']").click(); await shoot(page, "triage-run"); // The run page of the escalated sweep: its first step is the triage's tiers. await tier2.locator("[data-tier2-run]").click(); await page.waitForURL(new RegExp(`/research/runs/${t.article}/1`)); const step = page.locator("[data-lit-line][data-step='triage']"); await expect(step).toContainText("triage first"); await expect(step).toContainText("4 candidates · 2 flagged · 2 dismissed · escalated"); await step.click(); await expect(page.locator("[data-run-triage='r6-watch/1']")).toBeVisible(); await shoot(page, "triage-run-page"); await page.locator("[data-run-triage='r6-watch/1']").getByRole("link").click(); await page.waitForURL(/\/series\/r6-watch\/triage\/1$/); }); test("the Inbox: Worth researching (Your call) with the flags; its action opens the board umtool made of them, by triage", async ({ page }) => { test.setTimeout(90_000); await page.goto("/"); const card = page.locator("[data-group=your-call] [data-lit-line][data-piece='s:r6-watch'][data-card='worth-researching']"); await expect(card).toBeVisible(); await expect(card).toContainText("2 flagged of 4 new"); await expect(card).toContainText("The vote was rigged and everybody knew."); await expect(card.locator("[data-action-link]")).toHaveText("Open the board"); const article = series("r6-watch").ticks.at(-1).article as string; await expect(card.locator("[data-action-link]")).toHaveAttribute("href", `/research/${article}`); // The escalation's article is the watch's: no iteration card of its own. await expect(page.locator(`[data-lit-line][data-piece='a:articles/${article}']`)).toHaveCount(0); await shoot(page, "inbox-worth-researching"); await card.locator("[data-action-link]").click(); await page.waitForURL(new RegExp(`/research/${article}$`)); await expect(page.locator("[data-by='triage']")).toHaveCount(2); await expect(page.locator("[data-case-triage]")).toContainText("r6-watch · run 1"); const doc = JSON.parse(readFileSync(path.join(UMTOOL, "research", article, "case.json"), "utf8")); expect(doc.triage).toMatchObject({ schedule: "r6-watch", k: 1 }); expect(doc.sections.map((s: { title: string }) => s.title)).toEqual(["Strong flags", "Weak flags"]); expect(doc.quotes.map((q: { by?: string; cite: { video: string } }) => [q.by, q.cite.video])).toEqual([ ["triage", "sv1"], ["triage", "sv2"], ]); await shoot(page, "triage-board"); }); test("marks: y and x on the lit flag, kept per watch; the precision and a suggested threshold (never applied); every flag marked settles the card", async ({ page }) => { test.setTimeout(90_000); await page.goto(urls.triageHref("r6-watch", 1)); const f1 = page.locator("[data-flag='f1']"); await expect(f1).toHaveAttribute("data-mark", "none"); await expect(page.getByRole("button", { name: /Update the board|Open as a board/ })).toBeEnabled(); await f1.click(); await page.keyboard.press("y"); await expect(f1).toHaveAttribute("data-mark", "right"); await page.keyboard.press("j"); await expect(page.locator("[data-flag='f2']")).toBeFocused(); await page.keyboard.press("x"); await expect(page.locator("[data-flag='f2']")).toHaveAttribute("data-mark", "wrong"); await expect(page.locator("[data-precision]")).toHaveAttribute("data-precision", "0.5"); await expect(page.locator("[data-precision]")).toContainText("50% right over 2 marked flags"); await expect(page.locator("[data-suggestion]")).toContainText("mark 2 more to get a suggestion"); const marks = JSON.parse(readFileSync(path.join(UMTOOL, "triage", "r6-watch.json"), "utf8")); expect(Object.fromEntries(Object.entries(marks.marks).map(([k, m]) => [k, (m as { verdict: string }).verdict]))).toEqual({ "1/f1": "right", "1/f2": "wrong" }); await page.reload(); await expect(page.locator("[data-flag='f2']")).toHaveAttribute("data-mark", "wrong"); await shoot(page, "triage-marked"); // The watch's page: the precision; the schedule's threshold is untouched. await page.goto(urls.seriesHref("r6-watch")); await expect(page.locator("[data-series-triage] [data-precision]")).toContainText("precision 50% over 2 marked"); await shoot(page, "watch-page"); expect(series("r6-watch").triage).toBeUndefined(); // Work lists a watch as its own kind (P6b), not under Series. await page.goto(urls.workHref({ kind: "watch" })); const watchRow = page.locator("[data-lit-line][data-piece='s:r6-watch']"); await expect(watchRow).toBeVisible(); await expect(watchRow).toContainText("watch"); await page.goto(urls.workHref({ kind: "series" })); await expect(page.locator("[data-lit-line][data-piece='s:r6-watch']")).toHaveCount(0); // Every flag of the run marked: the card is settled. await page.goto("/"); await expect(page.locator("[data-card='worth-researching'][data-piece='s:r6-watch']")).toHaveCount(0); }); test("keys: the flags and dismissals are one lit list; y on a dismissal does nothing; u unmarks; Home and End", async ({ page }) => { await page.goto(urls.triageHref("r6-watch", 1)); const f1 = page.locator("[data-flag='f1']"); await expect(page.getByRole("button", { name: /Update the board|Open as a board/ })).toBeEnabled(); await f1.focus(); await page.keyboard.press("u"); await expect(f1).toHaveAttribute("data-mark", "none"); await page.keyboard.press("End"); await expect(page.locator("[data-dismissal]").last()).toBeFocused(); await page.keyboard.press("y"); await expect(page.locator("[data-flag][data-mark='right']")).toHaveCount(0); await page.keyboard.press("Home"); await expect(f1).toBeFocused(); await page.keyboard.press("y"); await expect(f1).toHaveAttribute("data-mark", "right"); });