import { test, expect } from "@playwright/test"; import { readFileSync, rmSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { WARM_TIMEOUT, warm } from "./warm"; // --------------------------------------------------------------------------- // Research runs (release 27 S11) over the editor stub: /articles/new starts an // article with RESEARCH under a chosen autonomy; its Iterations tab shows the // run's queue — each acquisition with its reason, evidence and note, a yes or // no for each that waits, the live state of an approved one's job — and, once // nothing is pending, Continue into a sweep whose parent is the run. // // WRITES articles/research-e2e/ (through the stub; removed after) // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const STUB_LOG = path.join(FIXTURE, "editor-stub.requests.json"); const ARTICLE = "research-e2e"; const stubAsked = (route: string) => (JSON.parse(readFileSync(STUB_LOG, "utf8")) as { route?: string; [k: string]: unknown }[]).filter((r) => r.route === route); test.beforeAll(async ({ playwright }) => { test.setTimeout(WARM_TIMEOUT); await warm(playwright, ["/articles/new", "/api/articles/policy", "/api/articles/channels", "/articles/zeta?tab=iterations"]); }); const AUTO = "research-auto-e2e"; test.afterAll(() => { for (const a of [ARTICLE, AUTO]) rmSync(path.join(FIXTURE, "articles", a), { recursive: true, force: true }); }); test("research: started with an autonomy, its queue answered yes and no, its job followed, then continued into a sweep", async ({ page }) => { test.setTimeout(120_000); await page.goto("/articles/new"); const form = page.getByRole("form", { name: "new article" }); await form.getByRole("radio", { name: /^research/ }).check(); // The settings' default autonomy, and the run's limits, before anything is sent. const autonomy = form.getByRole("combobox", { name: "autonomy" }); await expect(autonomy).toHaveValue("propose"); await expect(form.locator("[data-research-limits]")).toContainText("20 items, 5 GB, 6 h of transcription"); await expect(form.getByRole("combobox", { name: "model" })).toHaveValue("claude-sonnet-5-5"); await autonomy.selectOption("limits"); await form.getByRole("textbox", { name: "question" }).fill("Where did the vote story start?"); // No channel needed: research finds who and where. await form.getByRole("textbox", { name: "article id" }).fill(ARTICLE); await form.getByRole("button", { name: "Start" }).click(); await expect(page).toHaveURL(new RegExp(`/articles/${ARTICLE}\\?tab=iterations$`)); const started = stubAsked("report-draft").at(-1)!; expect(started).toMatchObject({ task: "research", article: ARTICLE, question: "Where did the vote story start?", model: "claude-sonnet-5-5", autonomy: "limits", maxUsd: 5 }); expect(started).not.toHaveProperty("channels"); const run = page.locator("[data-iteration='1']"); await expect(run).toHaveAttribute("data-kind", "research"); await expect(run).toHaveAttribute("data-status", "done", { timeout: 20_000 }); const slot = run.getByRole("region", { name: "i1 research" }); await expect(slot).toHaveAttribute("data-research-phase", "wait"); await expect(slot.locator("[data-research-autonomy]")).toHaveAttribute("data-research-autonomy", "limits"); await expect(slot).toContainText("The archive holds one stream on it."); // Each acquisition: what, why, the evidence, and why it waits or was refused. const p01 = slot.locator("[data-proposal='p01']"); const p02 = slot.locator("[data-proposal='p02']"); const p03 = slot.locator("[data-proposal='p03']"); await expect(p01).toHaveAttribute("data-proposal-status", "proposed"); await expect(p01).toContainText("import-archive-org"); await expect(p01).toContainText("sitechan · site-channel-episodes · Vote day.mp4"); await expect(p01).toContainText("The vote-day episode, where the claim is first made"); await expect(p01.getByRole("link", { name: "https://archive.org/details/site-channel-episodes" })).toHaveAttribute("target", "_blank"); await expect(p01.locator("[data-proposal-note]")).toContainText("the operator approves each acquisition"); await expect(p03).toHaveAttribute("data-proposal-status", "rejected"); await expect(p03.locator("[data-proposal-note]")).toContainText("a Twitch VOD is never fetched again"); await expect(p03.getByRole("button", { name: "Yes to p03" })).toHaveCount(0); // Nothing continues while it waits. await expect(slot.getByRole("button", { name: "Continue into a sweep" })).toHaveCount(0); // No to the new channel, with why. await p02.getByRole("textbox", { name: "why not p02" }).fill("not this one"); await p02.getByRole("button", { name: "No to p02" }).click(); await expect(p02).toHaveAttribute("data-proposal-status", "rejected"); await expect(p02.locator("[data-proposal-note]")).toHaveText("the operator said no: not this one"); expect(stubAsked("research-approve").at(-1)).toMatchObject({ article: ARTICLE, iteration: 1, ids: ["p02"], approve: false, note: "not this one", authorization: "Bearer umtool-e2e-token", }); // Yes to the import: the editor enqueues it; its job is followed to done. await p01.getByRole("button", { name: "Yes to p01" }).click(); await expect(p01).toHaveAttribute("data-proposal-status", "enqueued"); expect(stubAsked("research-approve").at(-1)).toMatchObject({ ids: ["p01"], approve: true }); await expect(p01.locator("[data-acq-job]")).toBeVisible(); await expect(slot.locator("[data-research-spent]")).toHaveAttribute("data-research-spent", "1/0.2/0"); await expect(p01).toHaveAttribute("data-proposal-status", "done", { timeout: 20_000 }); await expect(slot).toHaveAttribute("data-research-phase", "ready"); // Continue: a sweep whose parent is the run, over its widened scope. await slot.getByRole("combobox", { name: "continuation model for i1" }).selectOption("qwen3:8b"); await slot.getByRole("button", { name: "Continue into a sweep" }).click(); await expect.poll(() => stubAsked("report-draft").at(-1)).toMatchObject({ task: "sweep", article: ARTICLE, iteration: 1, model: "qwen3:8b" }); const sweep = page.locator("[data-iteration='2']"); await expect(sweep).toHaveAttribute("data-kind", "sweep"); await expect(sweep).toContainText("of i1"); await expect(sweep).toContainText("Where did the vote story start?"); await expect(sweep).toContainText("sitechan"); await expect(sweep).toHaveAttribute("data-status", "done", { timeout: 20_000 }); await page.reload(); await expect(page.locator("[data-iteration='1']").getByRole("region", { name: "i1 research" })).toHaveAttribute("data-research-phase", "continued"); await expect(page.locator("[data-iteration='1'] [data-continued-by]")).toHaveAttribute("data-continued-by", "2"); }); // Release 28 F1: an `auto` run whose acquisitions settled is continued by the // editor itself — umtool asks for nothing, and its queue says so. test("research: an auto run continues into its sweep by itself, and the queue says so", async ({ page }) => { test.setTimeout(120_000); await page.goto("/articles/new"); const form = page.getByRole("form", { name: "new article" }); await form.getByRole("radio", { name: /^research/ }).check(); await form.getByRole("combobox", { name: "autonomy" }).selectOption("auto"); await form.getByRole("textbox", { name: "question" }).fill("Where did the recount story start?"); await form.getByRole("textbox", { name: "article id" }).fill(AUTO); await form.getByRole("button", { name: "Start" }).click(); await expect(page).toHaveURL(new RegExp(`/articles/${AUTO}\\?tab=iterations$`)); expect(stubAsked("report-draft").at(-1)).toMatchObject({ task: "research", article: AUTO, autonomy: "auto" }); const slot = page.locator("[data-iteration='1']").getByRole("region", { name: "i1 research" }); await expect(slot.locator("[data-auto-continued]")).toHaveAttribute("data-auto-continued", "2", { timeout: 20_000 }); await expect(slot.locator("[data-auto-continued]")).toContainText("continued automatically by i2"); await expect(slot.locator("[data-auto-continued]")).toContainText("not in any build yet: radio-tape"); await expect(slot).toHaveAttribute("data-research-phase", "continued"); const sweep = page.locator("[data-iteration='2']"); await expect(sweep).toHaveAttribute("data-kind", "sweep"); await expect(sweep).toContainText("of i1"); await expect(sweep).toContainText("unbuilt channels: not in any build yet: radio-tape"); // Umtool posted no sweep: the editor made it. expect(stubAsked("report-draft").filter((r) => r.article === AUTO).map((r) => r.task)).toEqual(["research"]); }); test("the research route refuses what is not a decision", async ({ request }) => { const bad = await request.post("/api/articles/research", { data: { article: ARTICLE, n: 1, ids: ["p01"] } }); expect(bad.status()).toBe(400); expect((await bad.json()).error).toContain("approve is true or false"); const notId = await request.post("/api/articles/research", { data: { article: ARTICLE, n: 1, ids: ["x"], approve: true } }); expect(notId.status()).toBe(400); expect((await notId.json()).error).toContain("proposal ids"); });