import { test, expect, type Page } from "@playwright/test"; import { spawnSync } from "node:child_process"; import { existsSync, readFileSync, readdirSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { RESEARCH_PHRASE } from "./fixtures/research-fixture.mjs"; import { researchArchiveHref, researchHref } from "../lib/research/hrefs"; import { nearRegex } from "../lib/research/query.mjs"; import { WARM_TIMEOUT, warm } from "./warm"; // --------------------------------------------------------------------------- // Research (plans/umtool-redesign.md, R1) on the fixture corpus // (e2e/fixtures/research-fixture.mjs: a composed public dir the server reads // as its local corpus, TRANSCRIPT_LOCAL_DIR): the scope preview before // anything runs, a search whose total and ids ARE enumerate_matches' over the // same corpus, the facets, the reading log round trip, the reader's window // with the hit lit, the citation in the house format, j/k, the clip fetched // through the editor (stubbed), and the archive inventory. // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const PUBLIC = path.join(FIXTURE, "research-public"); const SESSIONS = path.join(FIXTURE, ".umtool", "research", "sessions"); const STUB_LOG = path.join(FIXTURE, "editor-stub.requests.json"); /** enumerate_matches through createCorpusTools, over the fixture's public dir. */ function enumerated(query: string, extra: Record = {}): { total: number; ids: string[]; partial: boolean } { const r = spawnSync("pnpm", ["exec", "tsx", path.join(HERE, "fixtures", "research-enumerate.ts"), PUBLIC, query, JSON.stringify(extra)], { cwd: path.join(HERE, ".."), encoding: "utf8", }); if (r.status !== 0) throw new Error(`research-enumerate failed: ${r.stderr}`); return JSON.parse(r.stdout.trim().split("\n").pop()!); } const holds = (page: Page) => page.locator("[data-holds]"); const honest = (page: Page) => page.locator("[data-honest]"); const results = (page: Page) => page.getByRole("group", { name: "Results" }); const reader = (page: Page) => page.getByRole("complementary", { name: "Reader" }); const litLine = (page: Page) => results(page).locator("[data-lit-line][data-lit]"); async function search(page: Page, q = RESEARCH_PHRASE) { await page.getByRole("textbox", { name: "Ask" }).fill(q); await page.getByRole("button", { name: /^Search/ }).click(); await expect(honest(page)).toBeVisible({ timeout: 30_000 }); } test.beforeAll(async ({ playwright }) => { test.setTimeout(WARM_TIMEOUT); await warm(playwright, [researchHref(), researchArchiveHref(), "/api/research/scope", "/api/research/coverage"]); }); test("the scope says what it holds before anything runs, and follows the chips", async ({ page }) => { await page.goto(researchHref()); await expect(page.getByRole("navigation", { name: "Breadcrumb" }).locator("h1")).toHaveText("research"); await expect(holds(page)).toContainText("4 channels · 12 videos · 2019–2026"); // Nothing has been searched in this scope by this test yet. await expect(page.locator("[data-honest]")).toHaveCount(0); // One channel: its own count and span. await page.getByText("All 4 channels").click(); await page.getByRole("checkbox", { name: /Night Desk/ }).check(); await expect(holds(page)).toContainText("1 channel · 3 videos · 2022–2025"); // A date window narrows the count (the summaries index), before any search. await page.getByText("Any date").click(); await page.getByLabel("From").fill("2023-01-01"); await expect(holds(page)).toContainText("1 channel · 2 videos · 2023–2025"); // A Candace channel is offered on the local corpus, marked local only. await page.locator("[data-scope] summary", { hasText: "Night Desk" }).click(); await expect(page.locator("[data-scope] label", { hasText: "Local Only Fixture" })).toContainText("local only"); }); test("a search finds exactly what enumerate_matches lists for the same scope: total, ids, order", async ({ page }) => { const want = enumerated(RESEARCH_PHRASE); expect(want.total).toBe(8); await page.goto(researchHref()); await search(page); await expect(honest(page).locator("[data-total]")).toHaveAttribute("data-total", String(want.total)); await expect(honest(page)).toContainText(`${want.total} recordings`); await expect(honest(page).locator("[data-coverage]")).toHaveAttribute("data-coverage", "full"); await expect(honest(page)).toContainText("1 mirror folded"); const keys = await results(page).locator("[data-hit]").evaluateAll((els) => els.map((e) => (e as HTMLElement).dataset.hit!.split("/")[1])); expect(keys).toEqual(want.ids); // The search is a link: the query is in the address. await expect(page).toHaveURL(/[?&]q=lighthouse\+budget/); // The scope preview learned transcripts vs captions from the search. await expect(holds(page).locator("[data-tracks]")).toContainText("transcripts 42% · captions 58%"); // One channel, the same parity. const nd = enumerated(RESEARCH_PHRASE, { channels: ["night-desk"] }); await page.goto(researchHref({ q: RESEARCH_PHRASE, channels: ["night-desk"] })); await expect(honest(page).locator("[data-total]")).toHaveAttribute("data-total", String(nd.total), { timeout: 30_000 }); expect(nd.ids).toEqual(["nd-2023"]); }); test("the facets: years with their gaps, channels, text, state -- each narrows what is drawn", async ({ page }) => { await page.goto(researchHref({ q: RESEARCH_PHRASE })); await expect(honest(page)).toBeVisible({ timeout: 30_000 }); const facets = page.getByRole("complementary", { name: "Facets" }); const years = facets.getByRole("group", { name: "Recordings per year" }).getByRole("button"); await expect(years).toHaveCount(8); await expect(facets.getByRole("button", { name: "2020: 0 recordings" })).toBeDisabled(); await expect(facets.getByRole("button", { name: "2022: 1 recording" })).toBeVisible(); await expect(facets.getByRole("button", { name: "Harbor Radio: 5" })).toBeVisible(); await expect(facets.getByRole("button", { name: "captions: 5" })).toBeVisible(); await expect(facets.getByRole("button", { name: "transcription: 3" })).toBeVisible(); await expect(facets.getByRole("button", { name: "Deleted: 1" })).toBeVisible(); await facets.getByRole("button", { name: "Night Desk: 1" }).click(); await expect(honest(page)).toContainText("showing 1 of 8"); await expect(results(page).locator("[data-hit]")).toHaveCount(1); await facets.getByRole("button", { name: "Night Desk: 1" }).click(); await facets.getByRole("button", { name: "2024: 1 recording" }).click(); await expect(results(page).locator("[data-hit]")).toHaveCount(1); await expect(results(page).locator("[data-hit]")).toHaveAttribute("data-hit", "harbor-radio/hr-2024a"); }); test("j/k walk the lines; the reader shows the lit hit's window with its cue lit", async ({ page }) => { await page.goto(researchHref({ q: RESEARCH_PHRASE })); await expect(honest(page)).toBeVisible({ timeout: 30_000 }); await expect(litLine(page)).toHaveCount(1); const first = await litLine(page).getAttribute("data-lit-id"); expect(first).toBe("harbor-radio/hr-2019a|0"); await page.keyboard.press("j"); await expect(litLine(page)).toHaveAttribute("data-lit-id", "harbor-radio/hr-2021a|0"); await page.keyboard.press("j"); await expect(litLine(page)).toHaveAttribute("data-lit-id", "harbor-radio/hr-2021a|1"); await page.keyboard.press("k"); await expect(litLine(page)).toHaveAttribute("data-lit-id", "harbor-radio/hr-2021a|0"); // The cite thread is on the lit line only. await expect(litLine(page).locator(".cite-thread")).toBeVisible(); // The audit: its window, the hit lit among dim cues, the length on the thread. await results(page).locator('[data-lit-id="harbor-radio/hr-2024a|0"]').click(); const r = reader(page); await expect(r).toContainText("Harbor Radio 140: the audit"); await expect(r).toContainText("deleted"); const lit = r.locator("[data-lit-line][data-lit]"); await expect(lit).toContainText("How much was the lighthouse budget? You cut it all,"); await expect(lit).toContainText("1:22:52"); await expect(r.locator(".cite-thread").first()).toHaveAttribute("data-cite-at", "4972"); expect(await r.locator("[data-lit-line]").count()).toBeGreaterThan(10); // ± narrows and widens the window. const wide = await r.locator("[data-lit-line]").count(); await r.getByRole("button", { name: "Narrower window" }).click(); await expect(r.getByText("± 15 s")).toBeVisible(); expect(await r.locator("[data-lit-line]").count()).toBeLessThan(wide); }); test("Copy citation and c put the house format on the clipboard", async ({ page, context }) => { await context.grantPermissions(["clipboard-read", "clipboard-write"]); await page.goto(researchHref({ q: RESEARCH_PHRASE })); await expect(honest(page)).toBeVisible({ timeout: 30_000 }); await results(page).locator('[data-lit-id="harbor-radio/hr-2024a|0"]').click(); const want = '"How much was the lighthouse budget? You cut it all," [Harbor Radio 140: the audit @ 1:22:52](https://archive.example/?v=harbor-radio%2Fhr-2024a&t=4972)'; await expect(reader(page).locator("[data-citation]")).toHaveText(want); await reader(page).getByRole("button", { name: "Copy citation" }).click(); await expect(reader(page).getByRole("button", { name: "Copied" })).toBeVisible(); expect(await page.evaluate(() => navigator.clipboard.readText())).toBe(want); await page.evaluate(() => navigator.clipboard.writeText("")); await results(page).locator('[data-lit-id="night-desk/nd-2023|0"]').focus(); await expect(reader(page).locator("[data-citation]")).toContainText("[Night Desk: the keeper @ 6:40](https://archive.example/?v=night-desk%2Fnd-2023&t=400)"); await page.keyboard.press("c"); await expect.poll(() => page.evaluate(() => navigator.clipboard.readText())).toBe( '"the keeper never saw the lighthouse budget" [Night Desk: the keeper @ 6:40](https://archive.example/?v=night-desk%2Fnd-2023&t=400)', ); }); test("the reading log: r and s mark and move on, survive a reload, and live umtool-side", async ({ page }) => { await page.goto(researchHref({ q: RESEARCH_PHRASE, channels: ["harbor-radio"] })); await expect(honest(page)).toBeVisible({ timeout: 30_000 }); await expect(honest(page).locator("[data-reading]")).toHaveText("read 0 of 5"); await page.keyboard.press("r"); await expect(honest(page).locator("[data-reading]")).toHaveText("read 1 of 5"); // r moved the light to the next recording. await expect(litLine(page)).toHaveAttribute("data-lit-id", "harbor-radio/hr-2021a|0"); await page.keyboard.press("s"); await expect(honest(page).locator("[data-reading]")).toHaveText("read 1 of 5 · 1 skipped"); await expect(results(page).locator('[data-hit="harbor-radio/hr-2019a"] [data-read]').first()).toHaveText("read"); await expect(results(page).locator('[data-hit="harbor-radio/hr-2021a"] [data-read]').first()).toHaveText("skipped"); // Stored under REPORTS_ROOT/.umtool/research/sessions/, never in the corpus. await expect.poll(() => (existsSync(SESSIONS) ? readdirSync(SESSIONS).length : 0)).toBeGreaterThan(0); const docs = readdirSync(SESSIONS).map((f) => JSON.parse(readFileSync(path.join(SESSIONS, f), "utf8"))); const doc = docs.find((d) => d.marks?.["harbor-radio/hr-2021a"]); expect(doc?.format).toBe("umtool-research-log"); expect(doc?.query).toBe(RESEARCH_PHRASE); expect(doc?.marks["harbor-radio/hr-2019a"].state).toBe("read"); expect(doc?.marks["harbor-radio/hr-2021a"].state).toBe("skipped"); await page.reload(); await expect(honest(page).locator("[data-reading]")).toHaveText("read 1 of 5 · 1 skipped", { timeout: 30_000 }); // The Reading facet narrows to the unread. await page.getByRole("complementary", { name: "Facets" }).getByRole("button", { name: "unread: 3" }).click(); await expect(results(page).locator("[data-hit]")).toHaveCount(3); // u clears a mark. await page.getByRole("complementary", { name: "Facets" }).getByRole("button", { name: "unread: 3" }).click(); await results(page).locator('[data-lit-id="harbor-radio/hr-2019a|0"]').click(); await page.keyboard.press("u"); await expect(honest(page).locator("[data-reading]")).toHaveText("read 0 of 5 · 1 skipped"); }); test("/ focuses the ask bar; a regex and near: say what they are", async ({ page }) => { await page.goto(researchHref()); await page.locator("body").click(); await page.keyboard.press("/"); await expect(page.getByRole("textbox", { name: "Ask" })).toBeFocused(); await page.keyboard.type("/light\\w+ keeper/"); await expect(page.getByRole("search")).toContainText("regex"); await page.keyboard.press("Enter"); const re = enumerated("light\\w+ keeper", { regex: true }); await expect(honest(page).locator("[data-total]")).toHaveAttribute("data-total", String(re.total), { timeout: 30_000 }); expect(re.total).toBeGreaterThan(0); await search(page, "keeper near:3 lighthouse"); await expect(page.getByRole("search")).toContainText("near: 3 words, one line"); const near = enumerated(nearRegex("keeper", "lighthouse", 3), { regex: true }); await expect(honest(page).locator("[data-total]")).toHaveAttribute("data-total", String(near.total)); }); test("Fetch clip asks the editor exactly as fetch_clip does, then plays the window", async ({ page }) => { await page.goto(researchHref({ q: RESEARCH_PHRASE })); await expect(honest(page)).toBeVisible({ timeout: 30_000 }); await results(page).locator('[data-lit-id="night-desk/nd-2023|0"]').click(); const r = reader(page); await expect(r.locator("[data-citation]")).toContainText("Night Desk: the keeper @ 6:40"); await expect(r.getByRole("button", { name: /^Play from/ })).toHaveCount(0); await r.getByRole("button", { name: "Fetch clip" }).click(); await expect(r.locator("[data-fetch-state=ready]")).toContainText("Clip fetched", { timeout: 20_000 }); await expect(r.getByRole("button", { name: "Play from 6:40" })).toBeVisible(); const asked = (JSON.parse(readFileSync(STUB_LOG, "utf8")) as Record[]).filter((q) => q.channelSlug === "night-desk"); expect(asked.at(-1)).toMatchObject({ channelSlug: "night-desk", from: 397, to: 406.8, pad: 3, requestedBy: "umtool" }); expect(String(asked.at(-1)?.reason)).toContain('umtool research: "lighthouse budget"'); await r.getByRole("button", { name: "Play from 6:40" }).click(); await expect(r.locator("video[data-player]")).toHaveAttribute("src", /\/api\/research\/media\?/); const res = await page.request.get((await r.locator("video[data-player]").getAttribute("src"))!, { headers: { range: "bytes=0-99" } }); expect(res.status()).toBe(206); expect(res.headers()["content-type"]).toBe("video/mp4"); }); test("the archive inventory: channels × years, held vs published, gaps, keys, a table", async ({ page }) => { await page.goto(researchArchiveHref()); await expect(page.getByRole("navigation", { name: "Breadcrumb" }).locator("h1")).toHaveText("Archive"); const heat = page.getByRole("table", { name: "Inventory" }); await expect(heat).toBeVisible(); // No editor answers coverage here (the stub has no such route): read on disk, and said so. await expect(page.locator("[data-held-note=disk]")).toContainText("read on disk instead"); // The held counts are kept five minutes (the warm-up read them): the age says so, Refresh asks again. await expect(page.locator("[data-held-age]")).toContainText(/Held counts from (just now|\d+ min ago) \(kept 5 min\)/); await page.getByRole("button", { name: "Refresh" }).click(); await expect(page.locator("[data-held-age]")).not.toContainText("kept 5 min"); await expect(page.getByRole("button", { name: "Refresh" })).toBeEnabled(); await expect(heat.locator("thead th")).toHaveCount(1 + 8 + 1); const hr = heat.locator('tr[data-row="harbor-radio"]'); await expect(hr.locator('td[data-year="2022"]')).toHaveText("1"); await expect(hr.locator('td[data-year="2020"]')).toHaveText("—"); await expect(hr.locator('td[data-year="2020"]')).toHaveAttribute("data-gap", ""); await expect(heat.locator('tr[data-row="harbor-radio-rumble"] td[data-year="2025"]')).toHaveText("—"); // Published: what the source lists, whether held or not. await page.getByRole("button", { name: "Videos published" }).click(); await expect(hr.locator('td[data-year="2022"]')).toHaveText("2"); await expect(heat.locator('tr[data-row="harbor-radio-rumble"] td[data-year="2025"]')).toHaveText("1"); // Keys and the tooltip. await hr.locator('td[data-year="2022"]').hover(); await expect(page.locator("[data-heat-tip]")).toContainText("Harbor Radio · 2022 · 2 published · 1 held"); await hr.locator('td[data-year="2024"]').click(); await page.keyboard.press("ArrowRight"); await expect(hr.locator('td[data-year="2025"]')).toBeFocused(); await expect(page.locator("[data-heat-tip]")).toContainText("Harbor Radio · 2025"); await page.getByRole("button", { name: "Table" }).click(); await expect(page.getByRole("table", { name: "Inventory table" })).toContainText("Night Desk"); });