import { test, expect, type Page } from "@playwright/test"; import { existsSync, mkdirSync, readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { EXCLUDED_URL, FORUM, PAGES, PRIVATE_NAME, STAGES_CASE, TARGET_URL, nameHash } from "./fixtures/case-stages-fixture.mjs"; import { EN_REWRITE } from "./fixtures/research-fixture.mjs"; import { researchCaseHref, researchCasePipelineHref } from "../lib/research/hrefs"; import { WARM_TIMEOUT, warm } from "./warm"; // --------------------------------------------------------------------------- // A research case's lane C stages (release 38) on the fixture case // `fixture-pipeline` (e2e/fixtures/case-stages-fixture.mjs) and the stub // editor (editor-stub-case-stages.mjs): verify (both tracks side by side, a // correction that keeps the reader's words, the audio check while paused, a // forum finding quarantined), leads (counts, never-archive as a hash, archive // on no site) and evidence (an excluded target, a capture, a forum attachment // marked unverified everywhere, and in the export's Evidence section). The // stages are panels of the case's pipeline page (lane A's), #stage-verify, // #stage-leads, #stage-evidence. In order: each test builds on the last. // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const CASE_DIR = path.join(FIXTURE, ".umtool", "research", STAGES_CASE); const read = (name: string) => readFileSync(path.join(CASE_DIR, name), "utf8"); const findings = () => read("findings.jsonl") .trim() .split("\n") .map((l) => JSON.parse(l)); const asked = (): { path: string; body: Record }[] => { try { return JSON.parse(readFileSync(path.join(FIXTURE, "editor-stub.requests.json"), "utf8")); } catch { return []; } }; // Screenshots for the review, when asked for (E2E_CASE_STAGES_SHOTS=). const SHOTS = process.env.E2E_CASE_STAGES_SHOTS ?? ""; async function shot(page: Page, name: string, stageId: string) { if (!SHOTS) return; mkdirSync(SHOTS, { recursive: true }); await page.locator(`#stage-${stageId}`).screenshot({ path: path.join(SHOTS, `${name}.png`) }); } test.describe.configure({ mode: "serial" }); test.beforeAll(async ({ playwright }) => { test.setTimeout(WARM_TIMEOUT); await warm(playwright, [researchCasePipelineHref(STAGES_CASE), researchCaseHref(STAGES_CASE)]); }); const stage = (page: Page, id: "verify" | "leads" | "evidence") => page.locator(`#stage-${id}`); test("the pipeline lists the stages; verify shows both tracks side by side, and a correction keeps the reader's words", async ({ page }) => { await page.setViewportSize({ width: 1440, height: 1000 }); await page.goto(researchCaseHref(STAGES_CASE)); await page.locator("[data-case-pipeline]").click(); await expect(page).toHaveURL(new RegExp(`${researchCasePipelineHref(STAGES_CASE)}$`)); const nav = page.getByRole("navigation", { name: "Stages" }); for (const n of ["7 Verify", "8 Leads", "10 Evidence"]) await expect(nav.getByRole("link", { name: n })).toBeVisible(); await nav.getByRole("link", { name: "7 Verify" }).click(); const list = stage(page, "verify").getByRole("complementary", { name: "Findings" }); await expect(list.locator("[data-finding]")).toHaveCount(3); await expect(list.locator('[data-finding="f3"]')).toContainText("forum"); const detail = page.locator('[data-verify-detail="f1"]'); await expect(detail.locator("[data-quote]")).toContainText(EN_REWRITE.en); const orig = detail.locator('[data-track="en-orig"]'); const en = detail.locator('[data-track="en"]'); // The window is read from the corpus on first view: give it the time a cold shard read takes on a busy machine. await expect(orig).toContainText("original audio captions", { timeout: 20_000 }); await expect(orig).toContainText(EN_REWRITE.orig); await expect(en).toContainText(EN_REWRITE.en); await expect(en.locator("[data-lit]")).toHaveText([EN_REWRITE.en]); await expect(en.locator("[data-track-holds]")).toHaveText("holds the quote"); await expect(orig.locator("[data-track-holds]")).toHaveText("4 of 5 words"); await detail.getByLabel("Quote (verbatim)").fill(EN_REWRITE.orig); await detail.getByLabel("Note").fill("en-orig says called in; the en track rewrote it"); await detail.getByRole("button", { name: "Save as corrected" }).click(); await expect(list.locator('[data-finding="f1"]')).toHaveAttribute("data-status", "corrected"); await expect(detail.locator("[data-original]")).toHaveText(`The reader wrote: “${EN_REWRITE.en}” (host)`); await expect(detail.getByRole("group", { name: "Verify status" }).getByRole("button", { name: "Corrected", exact: true })).toHaveAttribute("aria-pressed", "true"); await expect(detail.locator("[data-quote]")).toContainText(EN_REWRITE.orig); await expect(orig.locator("[data-track-holds]")).toHaveText("holds the quote"); const f1 = findings().find((r) => r.row === "f1"); expect(f1.quote).toBe(EN_REWRITE.orig); expect(f1.verify).toMatchObject({ status: "corrected", how: "hand", original: { quote: EN_REWRITE.en, speaker: "host" } }); await shot(page, "verify-corrected", "verify"); // A private name in a hand-written note is refused, and the refusal never names it. await detail.getByLabel("Note").fill(`checked with ${PRIVATE_NAME}`); await detail.getByRole("button", { name: "Save status" }).click(); await expect(detail.getByRole("alert")).toContainText("the privacy gate blocked the findings"); await expect(detail.getByRole("alert")).toContainText("(f1)"); await expect(detail.getByRole("alert")).not.toContainText(PRIVATE_NAME); }); test("the audio check transcribes the window through the editor, while paused, and the finding is verified by ear", async ({ page }) => { await page.setViewportSize({ width: 1440, height: 1000 }); await page.goto(researchCasePipelineHref(STAGES_CASE, "verify")); const list = stage(page, "verify").getByRole("complementary", { name: "Findings" }); await list.locator('[data-finding="f2"]').click(); const detail = page.locator('[data-verify-detail="f2"]'); await detail.getByLabel("Run even while transcription is paused").check(); const before = asked().length; await detail.getByRole("button", { name: "Audio check" }).click(); await expect(detail.locator("[data-audio-heard]")).toHaveText("Heard 9 of 9 words", { timeout: 15_000 }); await expect(detail.locator("[data-audio-diff] [data-diff=same]")).toHaveCount(9); const req = asked().slice(before).find((r) => r.path === "/api/ops/transcribe"); expect(req?.body).toMatchObject({ start: 2039, end: 2074, whilePaused: true }); expect(String(req?.body.path)).toMatch(/harbor-radio\/data\/hr-2022a\/audio\.m4a$/); await detail.getByRole("group", { name: "Verify status" }).getByRole("button", { name: "Verified", exact: true }).click(); await detail.getByRole("button", { name: "Save status" }).click(); await expect(list.locator('[data-finding="f2"]')).toHaveAttribute("data-status", "verified"); expect(findings().find((r) => r.row === "f2").verify).toMatchObject({ status: "verified", how: "audio", audio: { matched: 9, words: 9 } }); // The forum finding: the hint, then quarantined. await list.locator('[data-finding="f3"]').click(); const f3 = page.locator('[data-verify-detail="f3"]'); await expect(f3.locator("[data-forum-hint]")).toContainText("First seen on a forum"); await expect(f3.locator('[data-track="post"]')).toContainText("forum post #3 by deckhand", { timeout: 20_000 }); await f3.getByRole("group", { name: "Verify status" }).getByRole("button", { name: "Quarantined", exact: true }).click(); await f3.getByRole("button", { name: "Save status" }).click(); await expect(list.locator('[data-finding="f3"]')).toHaveAttribute("data-status", "quarantined"); await expect(f3.locator("[data-forum-hint]")).toHaveCount(0); }); test("leads: counted by post, a private name withheld; never-archive feeds a hash; archive creates on no site", async ({ page }) => { await page.setViewportSize({ width: 1440, height: 1000 }); await page.goto(researchCasePipelineHref(STAGES_CASE, "leads")); const leads = stage(page, "leads"); await expect(leads.getByLabel("Posts or forum channel")).toHaveValue(FORUM); await expect(page.locator("[data-leads-summary]")).toContainText("7 posts read · 5 leads"); await expect(page.locator("[data-withheld-count]")).toHaveText("1 withheld (the case's private names)"); const table = page.getByRole("table", { name: "Leads" }); await expect(table.locator("[data-lead]")).toHaveCount(5); await expect(table.locator('[data-lead="x:@harborwatch"] td').nth(2)).toHaveText("3"); await expect(table.locator('[data-lead="x:@tidewatcher"] td').nth(2)).toHaveText("2"); await expect(leads).not.toContainText(PRIVATE_NAME); // Context on demand. await table.locator('[data-lead="x:@harborwatch"]').getByRole("button", { name: "Context" }).click(); await expect(table.locator('[data-lead-context="x:@harborwatch"] li')).toHaveCount(3); // A role, then never-archive: withheld, its name a hash in the case's private names. await table.getByLabel("Role of x:@tidewatcher").fill("the account the forum blames"); await table.getByLabel("Role of x:@tidewatcher").blur(); await expect.poll(() => (existsSync(path.join(CASE_DIR, "leads.json")) ? read("leads.json") : "")).toContain("the account the forum blames"); await table.getByLabel("Disposition of x:@tidewatcher").selectOption("never-archive"); await expect(leads.locator('p[role="status"]')).toContainText("kept as a hash"); await expect(table.locator('[data-lead="x:@tidewatcher"]')).toHaveCount(0); await expect(table.locator("[data-lead-withheld]")).toHaveCount(1); await expect(table.locator("[data-lead-withheld]")).toContainText("[name withheld]"); await expect(table.locator("[data-lead-withheld]")).toContainText("the account the forum blames"); const pipeline = JSON.parse(read("pipeline.json")); expect(pipeline.privateNames.sha256).toContain(nameHash("TideWatcher")); expect(read("leads.json").toLowerCase()).not.toContain("tidewatcher"); await expect(page.locator("[data-withheld-count]")).toHaveText("2 withheld (the case's private names)"); // Archive: the channel created through the editor, on no site, its first fetch started. await table.getByLabel("Disposition of x:@harborwatch").selectOption("archive"); await expect(table.locator('[data-lead="x:@harborwatch"] [data-lead-channel]')).toBeVisible(); const create = asked().filter((r) => r.path === "/api/ops/create-channel").at(-1); expect(create?.body).toMatchObject({ fields: { url: "https://x.com/HarborWatch" }, fetchPostsNow: true }); expect(create && "sites" in create.body).toBe(false); await table.getByLabel("Disposition of youtube:@harborradio").selectOption("watch"); await expect(table.locator('[data-lead="youtube:@harborradio"]')).toHaveAttribute("data-disposition", "watch"); await shot(page, "leads-dispositions", "leads"); }); test("evidence: an excluded target, a capture at a date, a forum attachment marked unverified, attached to a finding", async ({ page }) => { await page.setViewportSize({ width: 1440, height: 1000 }); await page.goto(researchCasePipelineHref(STAGES_CASE, "evidence")); const ev = stage(page, "evidence"); const targets = ev.getByRole("form", { name: "Wayback targets" }); await targets.getByLabel("Pages, one per line").fill(`${EXCLUDED_URL}\n${TARGET_URL}`); await targets.getByLabel(/^Dates/).fill("2014-03-04"); await targets.getByRole("button", { name: "Add targets" }).click(); const table = ev.getByRole("table", { name: "Captures" }); await expect(table.locator("[data-evidence]")).toHaveCount(2); const gone = table.locator('[data-evidence="e1"]'); await gone.getByRole("button", { name: "Look up" }).click(); await expect(gone).toHaveAttribute("data-state", "excluded"); await expect(gone.locator("[data-evidence-label]")).toHaveText("excluded from the Wayback Machine (cause unknown)"); const page2 = table.locator('[data-evidence="e2"]'); await page2.getByLabel("Capture e2 into").selectOption(PAGES); await page2.getByRole("button", { name: "Capture" }).click(); await expect(page2).toHaveAttribute("data-state", "captured", { timeout: 15_000 }); await expect(page2.locator("[data-evidence-label]")).toHaveText("Wayback capture, 2014-03-04"); const imp = asked().filter((r) => r.path === "/api/ops/import-page").at(-1); expect(imp?.body).toEqual({ slug: PAGES, url: TARGET_URL, at: "20140304" }); const forum = ev.getByRole("form", { name: "Forum attachment" }); await forum.getByLabel("Forum channel").selectOption(FORUM); await forum.getByLabel("Post id").fill("70003"); await forum.getByRole("button", { name: "Capture the post" }).click(); const att = table.locator('[data-kind="forum-attachment"]'); await expect(att).toHaveAttribute("data-state", "captured", { timeout: 15_000 }); await expect(att.locator("[data-evidence-label]")).toHaveText("forum attachment, post #3, unverified"); await expect(att.locator("img")).toHaveCount(2); await expect(att.locator("img").first()).toHaveAttribute("alt", /forum attachment, post #3, unverified/); await att.getByLabel(/^Attach e3 to a finding/).selectOption("f3"); await expect(att.locator("td").last()).toContainText("f3"); expect(findings().find((r) => r.row === "f3").evidence).toEqual([ { kind: "forum-attachment", url: expect.stringContaining("post-70003"), evidence: "e3", post: "70003", position: 3 }, ]); await shot(page, "evidence-captures", "evidence"); // The finding says it everywhere it shows: its verify row, and the export (lane A's) with its Evidence section. // A new frame of the page (going to another #stage on the same page would only scroll). await page.reload(); await expect(stage(page, "verify").locator('[data-finding="f3"] [data-evidence-label]')).toHaveText("forum attachment, post #3, unverified"); await page.getByRole("button", { name: "Export the report" }).click(); await expect(page.locator("#stage-export [data-stage-state]")).toHaveAttribute("data-stage-state", "done", { timeout: 30_000 }); const md = read("REPORT.md"); expect(md).toContain("## Evidence"); expect(md).toContain(`- excluded from the Wayback Machine (cause unknown) — ${EXCLUDED_URL}, asked for 2014-03-04`); expect(md).toContain(`- Wayback capture, 2014-03-04 — [${TARGET_URL}](https://web.archive.org/web/20140304120000/${TARGET_URL})`); expect(md).toMatch(/- forum attachment, post #3, unverified — \[the post\]\(.+post-70003\), 2 files captured \(for f3\)/); expect(md).toMatch(/## Quarantined[\s\S]*row f3\. quarantined\. forum attachment, post #3, unverified\./); expect(md.toLowerCase()).not.toContain(PRIVATE_NAME.toLowerCase()); });