import { test, expect, type Page } from "@playwright/test"; import { appendFileSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { BOARD_CASE } from "./fixtures/research-fixture.mjs"; import { DROPPED, KEPT_KEEPER, LIVE_ARTICLE, REPAIRED, RUN_ARTICLE, RUN_QUESTION, TURNS_ARTICLE, jsonLines, liveTraceNext, } from "./fixtures/run-trace-fixture.mjs"; import { researchCaseHref, researchHref } from "../lib/research/hrefs"; import { runBoardId, runHref, runsHref } from "../lib/research/runs-hrefs"; import { WARM_TIMEOUT, warm } from "./warm"; // --------------------------------------------------------------------------- // Transparent runs (plans/umtool-redesign.md, R3) on the run fixture // (e2e/fixtures/run-trace-fixture.mjs): a traced sweep replayed step by step // with a dropped quote and its reason; an imported turns-only run whose two // starts are two runs; a run still going, followed live as its trace grows; // "Open as a board"; and a sweep planned from a case's scope. // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const liveTrace = path.join(FIXTURE, "articles", LIVE_ARTICLE, "iterations", "1", "trace.jsonl"); const runBoardDir = path.join(FIXTURE, ".umtool", "research", runBoardId(RUN_ARTICLE, 1)); const steps = (page: Page) => page.getByRole("group", { name: "Steps" }); const lit = (page: Page) => steps(page).locator("[data-lit-line][data-lit]"); const step = (page: Page, id: string) => steps(page).locator(`[data-lit-line][data-lit-id="${id}"]`); const detail = (page: Page) => page.locator("[data-detail]"); let liveHead = ""; test.beforeAll(async ({ playwright }) => { test.setTimeout(WARM_TIMEOUT); liveHead = readFileSync(liveTrace, "utf8"); await warm(playwright, [runHref(RUN_ARTICLE, 1), runsHref(), `/api/research/runs/${RUN_ARTICLE}/1`, researchCaseHref(BOARD_CASE)]); }); test.afterAll(() => { // What the specs wrote, put back: the live trace, the run's board. writeFileSync(liveTrace, liveHead); rmSync(runBoardDir, { recursive: true, force: true }); }); test("a traced run: the head, its steps with their spend, the lit step's detail, a dropped quote and why", async ({ page }) => { await page.goto(runHref(RUN_ARTICLE, 1)); await expect(page.locator("[data-run-mode]")).toHaveAttribute("data-run-mode", "replay"); // The question is the title (clamped to two lines, all of it in `title`); the run is in the trail. const crumbs = page.getByRole("navigation", { name: "Breadcrumb" }); await expect(crumbs.locator("h1")).toHaveText(RUN_QUESTION); await expect(crumbs.locator("h1")).toHaveAttribute("title", RUN_QUESTION); await expect(crumbs.getByRole("link", { name: `${RUN_ARTICLE} · i1` })).toHaveAttribute("href", `/articles/${RUN_ARTICLE}?tab=iterations`); const meta = page.locator("[data-run-meta]"); await expect(meta).toContainText("Sonnet 5.5 · Haiku 4.5 extractor"); await expect(meta).toContainText("harbor-radio, night-desk"); await expect(meta).toContainText("0.55 min"); await expect(page.locator("[data-run-usd]")).toHaveText("$0.18"); await expect(page.locator("[data-run-checks]")).toContainText("2 kept · 1 dropped"); // The rail lights Research; the trail reaches every run. await expect(page.locator('a[aria-current][href="/research"]')).toBeVisible(); await expect(page.getByRole("navigation", { name: "Breadcrumb" }).getByRole("link", { name: "runs" })).toHaveAttribute("href", runsHref()); // The steps, in order, with the running spend. await expect(steps(page).locator("[data-lit-line]")).toHaveCount(8); expect(await steps(page).locator("[data-lit-line]").evaluateAll((els) => els.map((e) => e.getAttribute("data-lit-id")))).toEqual([ "scope", "t0", "t1", "t2", "r0", "findings", "checks", "end", ]); await expect(step(page, "t1").locator("[data-step-usd]")).toHaveText("$0.10"); await expect(step(page, "checks")).toContainText("2 kept · 1 dropped"); await expect(page.locator("[data-lane-coverage]")).toContainText("3 matched · 3 read · complete"); await expect(page.locator("[data-spend-end]")).toHaveText("$0.18"); // The first step is lit: the scope. await expect(lit(page)).toHaveAttribute("data-lit-id", "scope"); await expect(detail(page)).toContainText("lighthouse budget"); // j: the enumeration, its ids. await steps(page).locator("[data-lit-line][data-lit]").focus(); await page.keyboard.press("j"); await expect(lit(page)).toHaveAttribute("data-lit-id", "t0"); await expect(detail(page).locator("[data-enumeration]")).toContainText("3 matched"); await expect(detail(page).locator("[data-ids] code")).toHaveCount(3); // The batch: the extractor's own lines, on the reader surface. await page.keyboard.press("j"); await expect(detail(page).locator("[data-batch-lines]")).toContainText(REPAIRED.now); // The checks: the dropped quote, its reason; the refused one repaired and kept. await step(page, "checks").click(); const dropped = detail(page).locator("[data-check-group=dropped] [data-check=dropped]"); await expect(dropped).toHaveCount(1); await expect(dropped).toContainText(DROPPED.quote); await expect(dropped.locator("[data-check-reason]")).toHaveText(`why: ${DROPPED.reason}`); await expect(detail(page).locator("[data-check-group=kept] [data-check=kept]")).toHaveCount(2); await expect(detail(page).locator("[data-check-group=refused]")).toContainText(REPAIRED.was); // The spend chart lights the same step. await step(page, "t2").click(); await expect(page.locator("[data-spend-point=t2]")).toHaveAttribute("data-lit-point", ""); }); test("replay: r lights every step in turn, from the first to the end", async ({ page }) => { await page.goto(runHref(RUN_ARTICLE, 1)); await expect(steps(page)).toHaveAttribute("data-hydrated", ""); await page.locator("body").press("r"); await expect(page.locator("[data-replay]")).toHaveAttribute("data-replay", "on"); // Each step is lit for 650 ms: watch every frame, not the assertion's backed-off retries. await page.waitForFunction(() => document.querySelector("[data-lit-line][data-lit]")?.getAttribute("data-lit-id") === "t2", null, { polling: "raf", timeout: 10_000 }); await expect(lit(page)).toHaveAttribute("data-lit-id", "end", { timeout: 10_000 }); await expect(page.locator("[data-replay]")).toHaveAttribute("data-replay", "off"); }); test("a run before the trace: turns.jsonl with two run starts is two runs, never summed; its checks are a gap", async ({ page }) => { await page.goto(runHref(TURNS_ARTICLE, 1)); await expect(page.locator("[data-run-mode]")).toContainText("from turns.jsonl"); const picker = page.getByRole("navigation", { name: "Runs" }); await expect(picker.getByRole("link")).toHaveCount(2); await expect(picker.getByRole("link", { name: "2" })).toHaveAttribute("aria-current", "page"); await expect(page.locator("[data-run-notes]")).toContainText("2 run starts"); expect(await steps(page).locator("[data-lit-line]").evaluateAll((els) => els.map((e) => e.getAttribute("data-lit-id")))).toEqual(["scope", "t0", "t1", "t2", "t3", "checks"]); await expect(step(page, "checks")).toHaveAttribute("data-gap", ""); await expect(step(page, "checks")).toContainText("kept / dropped not recorded"); await expect(page.locator("[data-run-usd]")).toHaveText("$0.16"); await expect(page.locator("[data-lane-coverage]")).toContainText("not recorded"); await picker.getByRole("link", { name: "1" }).click(); await expect(page).toHaveURL(new RegExp(`/research/runs/${TURNS_ARTICLE}/1\\?run=1$`)); await expect(picker.getByRole("link", { name: "1" })).toHaveAttribute("aria-current", "page"); await expect(page.locator("[data-run-usd]")).toHaveText("$0.09"); expect(await steps(page).locator("[data-lit-line]").evaluateAll((els) => els.map((e) => e.getAttribute("data-lit-id")))).toEqual(["scope", "t0", "t1", "t2", "checks"]); await step(page, "t1").click(); await expect(detail(page).locator("[data-lines-missing]")).toBeVisible(); }); test("live: a running sweep's page follows its trace as it grows", async ({ page }) => { await page.goto(runHref(LIVE_ARTICLE, 1)); await expect(page.locator("[data-run-mode]")).toHaveAttribute("data-run-mode", "live"); await expect(steps(page).locator("[data-lit-line]")).toHaveCount(2); await expect(lit(page)).toHaveAttribute("data-lit-id", "t0"); appendFileSync(liveTrace, jsonLines(liveTraceNext())); await expect(step(page, "t1")).toBeVisible({ timeout: 10_000 }); await expect(lit(page)).toHaveAttribute("data-lit-id", "t1"); await expect(page.locator("[data-run-usd]")).toHaveText("$0.08"); await expect(page.getByRole("button", { name: /^Replay/ })).toBeDisabled(); }); test("open as a board: the run's kept findings become quotes by the agent; the board links back", async ({ page }) => { await page.goto(runHref(RUN_ARTICLE, 1)); await page.getByRole("button", { name: /^Open as a board/ }).click(); const status = page.locator("[data-run-status-line]"); await expect(status).toContainText("Board made: 2 quotes added"); await status.getByRole("link", { name: "Open the board" }).click(); await expect(page).toHaveURL(new RegExp(`/research/${runBoardId(RUN_ARTICLE, 1)}$`)); const board = page.getByRole("group", { name: "Board" }); await expect(board.locator("[data-quote]")).toHaveCount(2); await expect(board.locator("[data-by=agent]")).toHaveCount(2); await expect(board.locator("[data-slip]").first()).toContainText(REPAIRED.now); await expect(board).toContainText(KEPT_KEEPER); await expect(page.locator("[data-case-run]").getByRole("link")).toHaveAttribute("href", runHref(RUN_ARTICLE, 1)); // Again: nothing added twice; the button says it updates. await page.goto(runHref(RUN_ARTICLE, 1)); await page.getByRole("button", { name: /^Update the board/ }).click(); await expect(page.locator("[data-run-status-line]")).toContainText("Board updated: 0 quotes added, 2 already on it"); }); test("a case's scope: Run a sweep on this scope plans it (no seed, the model and cap shown); the runs index lists every sweep", async ({ page }) => { await page.goto(researchCaseHref(BOARD_CASE)); await page.getByRole("button", { name: "Run a sweep on this scope" }).click(); const dlg = page.locator('dialog[data-handoff="sweep"]'); await expect(dlg).toContainText("Run a sweep on this scope"); await expect(dlg).toContainText("Corpus"); await expect(dlg).not.toContainText("the board as cited markdown"); await expect(dlg).toContainText(/Runs on\s*(claude-|qwen)/); await expect(dlg.getByRole("button", { name: /^Start the sweep/ })).toBeVisible(); // The run's own dates (D2 item 1): two date fields, the scope's to start with. await expect(dlg.getByLabel("From", { exact: true })).toBeVisible(); await expect(dlg.getByLabel("To", { exact: true })).toBeVisible(); await page.keyboard.press("Escape"); await expect(dlg).toBeHidden(); await page.goto(researchHref()); await page.getByRole("link", { name: "Agent runs" }).click(); await expect(page).toHaveURL(/\/research\/runs$/); for (const a of [RUN_ARTICLE, TURNS_ARTICLE, LIVE_ARTICLE]) await expect(page.locator(`[data-run-row="${a}/1"]`)).toBeVisible(); await page.locator(`[data-run-row="${RUN_ARTICLE}/1"]`).getByRole("link").click(); await expect(page).toHaveURL(new RegExp(`/research/runs/${RUN_ARTICLE}/1$`)); }); test("a scope sweep's plan takes the run's own dates (D2 item 1); bad or backwards dates are refused", async ({ request }) => { const url = `/api/research/cases/${BOARD_CASE}/handoff`; const plan = (await (await request.post(url, { data: { to: "sweep", dateFrom: "2024-01-01", dateTo: "2024-12-31" } })).json()).plan; expect([plan.dateFrom, plan.dateTo, plan.seeded]).toEqual(["2024-01-01", "2024-12-31", false]); const back = (await (await request.post(url, { data: { to: "sweep", dateFrom: "2024-12-31", dateTo: "2024-01-01" } })).json()).plan; expect(back.refusals).toContain("the dates run backwards: 2024-12-31 is after 2024-01-01"); const bad = (await (await request.post(url, { data: { to: "sweep", dateFrom: "June 2024" } })).json()).plan; expect(bad.refusals).toContain('the "from" date "June 2024" is not YYYY-MM-DD'); });