import { test, expect, type Page } from "@playwright/test"; import { rmSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { RUN_ARTICLE } from "./fixtures/run-trace-fixture.mjs"; import { MODEL_LOG_MEDIA_ARTICLE } from "./fixtures/media-draft-fixture.mjs"; import { runHref } from "../lib/research/runs-hrefs"; import { WARM_TIMEOUT, warm } from "./warm"; import * as urls from "./urls"; // --------------------------------------------------------------------------- // THE MODEL LOG (demo epic D3): the model's side of the work, on screen. A // local sweep runs FOR REAL in the editor stub (the pipeline over the // synthetic corpus, its model the fake ollama with the recorded run, each // answer held a moment — e2e/fixtures/run-sweep.ts): its calls appear on the // run page while it runs, grouped by phase; the quote the code dropped shows // its reason; a call opens to its whole prompt; the filter narrows to the // dropped. The same log on the Iterations tab and on a media draft; with no // call recorded, the panel says what will appear. // --------------------------------------------------------------------------- const SWEEP = "mlog-sweep"; const QUESTION = "Did the baker enter the pie contest?"; // Screenshots for the lane's report (1366×768), when a directory is named. const SHOTS = process.env.E2E_MODEL_LOG_SHOTS; const shot = async (page: Page, name: string) => { if (SHOTS) await page.screenshot({ path: path.join(SHOTS, `${name}.png`) }); }; const log = (page: Page) => page.locator("[data-model-log]").first(); const calls = (page: Page) => log(page).locator("[data-call]"); test.beforeAll(async ({ playwright }) => { test.setTimeout(WARM_TIMEOUT); await warm(playwright, [ `${runHref(RUN_ARTICLE, 1)}?view=model`, `/api/model-log?article=${RUN_ARTICLE}&n=1`, `/api/model-log/call?article=${RUN_ARTICLE}&n=1&id=1.1`, `/articles/${MODEL_LOG_MEDIA_ARTICLE}?tab=iterations`, ]); }); // The sweep's article is this spec's own: gone after it (projects.spec counts // the store's sweep reports). const FIXTURE = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", ".e2e-song"); test.afterAll(() => rmSync(path.join(FIXTURE, "articles", SWEEP), { recursive: true, force: true })); test.use({ viewport: { width: 1366, height: 768 } }); test("a local sweep's calls appear live by phase; a dropped quote says why; a call opens to its whole prompt; the filter", async ({ page }) => { test.setTimeout(120_000); const made = await page.request.post("/api/articles/generate", { data: { task: "sweep", article: SWEEP, question: QUESTION, channels: ["synth-kitchen"], model: "qwen3:8b" } }); expect(made.status(), await made.text()).toBe(200); expect(await made.json()).toMatchObject({ ok: true, iteration: 1 }); await page.goto(`${runHref(SWEEP, 1)}?view=model`); await expect(page.getByRole("navigation", { name: "Run view" })).toHaveAttribute("data-run-pane", "model"); // LIVE: the run is going and its first calls are on the page. await expect(async () => { await expect(page.locator("[data-run-mode]")).toHaveAttribute("data-run-mode", "live", { timeout: 500 }); await expect(log(page)).toHaveAttribute("data-model-log-live", "", { timeout: 500 }); expect(await calls(page).count()).toBeGreaterThan(0); }).toPass({ timeout: 30_000, intervals: [250] }); await shot(page, "run-page-live"); // It ends: every call, in the sweep's phases, the count header says so. await expect(page.locator("[data-run-mode]")).toHaveAttribute("data-run-mode", "replay", { timeout: 60_000 }); await expect(log(page).locator("[data-model-log-count]")).toHaveAttribute("data-model-log-count", "8", { timeout: 10_000 }); await expect(log(page).locator("[data-phase]")).toHaveCount(4); expect(await log(page).locator("[data-phase]").evaluateAll((els) => els.map((e) => e.getAttribute("data-phase")))).toEqual(["terms", "reading", "meaning-check", "grouping"]); await expect(log(page).locator("[data-model-log-models]")).toHaveAttribute("data-model-log-models", "qwen3:8b"); await expect(log(page)).toContainText("qwen3:8b (local)"); // The terms call: kept, and what was searched. const terms = log(page).locator("[data-phase=terms] [data-call]"); await expect(terms.locator("[data-call-decision]")).toContainText("searched for contest, judges, oven"); // The reading calls (two recordings): three quotes kept, one dropped, and why. const reading = log(page).locator("[data-phase=reading] [data-call]"); await expect(reading).toHaveCount(2); await expect(reading.locator("[data-item-outcome=kept]")).toHaveCount(3); const dropped = reading.locator("[data-item-outcome=dropped]"); await expect(dropped).toContainText("I entered the contest and I won"); await expect(dropped.locator("[data-item-why]")).toHaveText("why: not verbatim in its window"); // The meaning check (D2 item 3), a call per kept quote: kept, rewritten to its fix, flagged to check by ear. const meaning = log(page).locator("[data-phase=meaning-check] [data-call]"); await expect(meaning).toHaveCount(3); await expect(meaning.nth(2).locator("[data-call-decision]")).toContainText("flagged: check by ear"); await expect(meaning.nth(1).locator("[data-item-outcome=rewritten]")).toContainText("She says the judges were friends of hers"); // The second grouping ask met the fake's empty script: shown failed. await expect(log(page).locator("[data-phase=grouping] [data-call]").nth(1).locator("[data-call-answer]")).toContainText("failed:"); await shot(page, "run-page-done"); // The whole prompt, fetched for that row alone. await reading.getByRole("button", { name: /^call 1\.\d+ extract pie-stream-02@0/ }).click(); const open = reading.locator("[data-call-open]"); await expect(open.locator("[data-call-prompt]")).toContainText(`Research question: ${QUESTION}`); await expect(open.locator("[data-call-prompt]")).toContainText("Transcript excerpt:"); await open.getByRole("button", { name: /^System prompt/ }).click(); await expect(open).toContainText("You read excerpts of an automatic transcript"); await shot(page, "run-page-call-open"); // The filter: only the calls code dropped something of. await log(page).getByRole("button", { name: /^Dropped/ }).click(); await expect(calls(page)).toHaveCount(1); await expect(calls(page).first()).toHaveAttribute("data-call-phase", "reading"); await log(page).getByRole("button", { name: /^All/ }).click(); await expect(calls(page)).toHaveCount(8); // The steps are one press away, and the address keeps the pane. await page.getByRole("button", { name: "Steps" }).click(); await expect(page.getByRole("group", { name: "Steps" })).toBeVisible(); expect(new URL(page.url()).searchParams.get("view")).toBeNull(); // The Iterations tab: the count on the toggle, the same log under it. await page.goto(urls.articleHref({ site: "articles", id: SWEEP }, { tab: "iterations" })); const toggle = page.locator("[data-iteration='1'] [data-model-log-toggle]"); await expect(toggle).toHaveText("Model log · 8"); await toggle.click(); await expect(page.locator("[data-iteration='1'] [data-model-log-count]")).toHaveAttribute("data-model-log-count", "8"); await shot(page, "iterations-tab"); }); test("a media draft's model log: the task's calls, its drafted fields and what the checks did", async ({ page }) => { test.setTimeout(120_000); const r = await page.request.post("/api/articles/generate", { data: { task: "teaser", article: MODEL_LOG_MEDIA_ARTICLE, iteration: 1, model: "qwen3:8b" } }); expect(r.status(), await r.text()).toBe(200); await expect .poll( async () => { const list = (await (await page.request.get(`/api/articles/iterations?article=${MODEL_LOG_MEDIA_ARTICLE}`)).json()) as { iterations: { n: number; steps: { task: string; status: string }[] }[] }; return list.iterations.find((i) => i.n === 1)?.steps.find((s) => s.task === "teaser")?.status ?? "none"; }, { timeout: 60_000, intervals: [1000] }, ) .toBe("done"); await page.goto(urls.articleHref({ site: "articles", id: MODEL_LOG_MEDIA_ARTICLE }, { tab: "iterations" })); await page.locator("[data-media-link='1']").click(); const section = page.locator("[data-draft-model-log='teaser']"); await section.getByRole("button", { name: /^Model log of / }).click(); const panel = section.locator("[data-model-log]"); await expect(panel).toHaveAttribute("data-model-log", "calls"); await expect(panel.locator("[data-phase='draft'] [data-call]").first()).toBeVisible(); await expect(panel.locator("[data-model-log-models]")).toHaveAttribute("data-model-log-models", "qwen3:8b"); await section.scrollIntoViewIfNeeded(); await shot(page, "media-draft"); }); test("no call recorded: the panel says what will appear (no AI needed)", async ({ page }) => { await page.goto(`${runHref(RUN_ARTICLE, 1)}?view=model`); await expect(log(page)).toHaveAttribute("data-model-log", "empty"); await expect(log(page).locator("[data-model-log-empty]")).toContainText("No model call recorded. When a model works on this, each call appears here"); await expect(log(page).locator("[data-model-log-count]")).toHaveAttribute("data-model-log-count", "0"); }); test("settings: per task its route, the model it will run on, no API key when there is none, its last run's model", async ({ page }) => { await page.goto(urls.generationPolicyHref()); const policy = page.getByRole("region", { name: "generation policy" }); const rows = policy.locator("[data-policy-tasks] tbody tr"); await expect(rows).toHaveCount(17); for (const t of ["sweep", "narrate", "frames", "revise-report"]) { const row = policy.locator(`[data-task='${t}']`); const route = await row.getByRole("combobox", { name: `route ${t}` }).inputValue(); await expect(row).toHaveAttribute("data-task-route", route); const model = await row.locator("[data-task-model]").getAttribute("data-task-model"); if (route === "code") { expect(model).toBe(""); await expect(row.locator("[data-task-model]")).toHaveText("a Claude Code session"); } else expect(model, `${t} on ${route}`).toMatch(route === "api" ? /^claude-/ : /^qwen/); // No key on the e2e machine (its key file is never written): an API route says so. const keyed = (await policy.locator("[data-key-file-exists]").getAttribute("data-key-file-exists")) === "yes"; await expect(row.locator("[data-task-no-key]")).toHaveCount(route === "api" && !keyed ? 1 : 0); } // The ledger's fixture row: the sweep's last run was on Sonnet 5.5. await expect(policy.locator("[data-task='sweep'] [data-task-last]")).toHaveAttribute("data-task-last", "claude-sonnet-5-5"); await expect(policy.locator("[data-task='threads'] [data-task-last]")).toHaveText("none in the ledger"); await shot(page, "settings-generation"); });