import { test, expect, type APIRequestContext } from "@playwright/test"; import { existsSync, readFileSync, rmSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import * as urls from "./urls"; import { WARM_TIMEOUT, warm } from "./warm"; // --------------------------------------------------------------------------- // Settings (plans/umtool-redesign.md, P5), on the fixture and the editor stub: // // /settings/generation the generation policy (written through the editor's // settings action, the key never shown; it moved here // from /articles/new) and the day's spend, job by job, // from the fixture's ledger // /settings/system the maintenance a terminal did, each action that // changes anything a plan first: the index prune's // plan shown, Keep it changes nothing, Confirm drops // the record; the rebuild runs as a job; a storage // move with no media root is refused, never offered // // WRITES the stub's in-memory policy (put back to `balanced` after), a // scaffolded project the spec deletes again (so its index record goes // stale), and the fixture's index (UMTOOL_CACHE_DIR, rebuilt every run) // --------------------------------------------------------------------------- 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 stubAsked = (route: string) => (JSON.parse(readFileSync(STUB_LOG, "utf8")) as { route?: string; [k: string]: unknown }[]).filter((r) => r.route === route); const STALE = "p5-stale-record"; test.beforeAll(async ({ playwright }) => { test.setTimeout(WARM_TIMEOUT); await warm(playwright, [urls.generationPolicyHref(), urls.systemHref(), "/settings", "/api/articles/policy"]); }); test.afterAll(async ({ request }) => { rmSync(path.join(FIXTURE, STALE), { recursive: true, force: true }); await request.post("/api/articles/policy", { data: { routes: { sweep: "code", "revise-report": "api", "revise-video": "local", narrate: "local", research: "api" } } }); }); test("/settings lists generation and system; both light Settings", async ({ page }) => { await page.goto("/settings"); await expect(page.locator("[data-tool-entry=generation] a")).toHaveAttribute("href", urls.generationPolicyHref()); await expect(page.locator("[data-tool-entry=system] a")).toHaveAttribute("href", urls.systemHref()); await page.goto(urls.systemHref()); await expect(page.locator("[data-policy-link]")).toHaveAttribute("href", urls.generationPolicyHref()); }); test("generation: presets write their routes, a route alone makes it custom, caps are the editor's to refuse; the key is a file", async ({ page }) => { await page.goto(urls.generationPolicyHref()); await expect(page.locator("[data-policy-summary]")).toHaveText( "Generation: balanced · Claude Code: sweeps · API: report revisions, research, frames · local: video revisions, drafts, verdicts, clip labels, the video assistant, claim groups for a cut", ); const policy = page.getByRole("region", { name: "generation policy" }); await expect(policy).toHaveAttribute("data-policy-preset", "balanced"); await expect(policy.getByRole("button", { name: "balanced" })).toHaveAttribute("aria-pressed", "true"); // The key FILE's path (the editor's read redacts it; settings.json names it) // and whether a file is there — never a key. await expect(policy.locator("[data-key-file-exists]")).toContainText("anthropic.env"); await expect(policy.locator("[data-today-usd]")).toHaveAttribute("data-today-usd", "0.42"); await policy.getByRole("button", { name: "api", exact: true }).click(); await expect(policy).toHaveAttribute("data-policy-preset", "api"); expect(stubAsked("settings").at(-1)).toMatchObject({ patch: { generation: { routes: { sweep: "api", "revise-report": "api", "revise-video": "api", narrate: "api", research: "api" } } }, authorization: "Bearer umtool-e2e-token", }); await policy.getByRole("combobox", { name: "route narrate" }).selectOption("local"); await expect(policy).toHaveAttribute("data-policy-preset", "custom"); await policy.getByRole("textbox", { name: "cap sweep" }).fill("50"); await policy.getByRole("button", { name: "Save caps" }).click(); await expect(policy.getByRole("alert")).toContainText("generation.caps.sweep"); await policy.getByRole("textbox", { name: "cap sweep" }).fill("4"); await policy.getByRole("button", { name: "Save caps" }).click(); await expect(policy.getByRole("status")).toHaveText("saved caps"); expect(stubAsked("settings").at(-1)).toMatchObject({ patch: { generation: { caps: { sweep: 4, max: 10 } } } }); for (const r of stubAsked("settings")) expect(JSON.stringify(r)).not.toContain("apiKeyFile"); // Read back on a fresh load: the round trip is the editor's, not the page's state. await page.reload(); await expect(page.getByRole("region", { name: "generation policy" })).toHaveAttribute("data-policy-preset", "custom"); await expect(page.getByRole("region", { name: "generation policy" }).getByRole("textbox", { name: "cap sweep" })).toHaveValue("4"); const again = page.getByRole("region", { name: "generation policy" }); await again.getByRole("textbox", { name: "cap sweep" }).fill("5"); await again.getByRole("button", { name: "Save caps" }).click(); await expect(again.getByRole("textbox", { name: "cap sweep" })).toHaveValue("5"); // The local sweep's two knobs (demo epic D2): who writes a finding's line, // and what happens to a quote that may be read aloud. The defaults, then // each written through the editor and put back. const local = again.locator("[data-policy-local-sweep]"); await expect(local).toHaveAttribute("data-policy-local-sweep", "neutral flag"); await again.getByRole("combobox", { name: "local sweep read-aloud quotes" }).selectOption("drop"); await expect(again.getByRole("status")).toHaveText("saved the local sweep's read-aloud rule"); expect(stubAsked("settings").at(-1)).toMatchObject({ patch: { generation: { localSweep: { claimForm: "neutral", readAloud: "drop" } } } }); await again.getByRole("combobox", { name: "local sweep claim form" }).selectOption("sentence"); await expect(local).toHaveAttribute("data-policy-local-sweep", "sentence drop"); await again.getByRole("combobox", { name: "local sweep claim form" }).selectOption("neutral"); await again.getByRole("combobox", { name: "local sweep read-aloud quotes" }).selectOption("flag"); await expect(local).toHaveAttribute("data-policy-local-sweep", "neutral flag"); await again.getByRole("button", { name: "balanced" }).click(); await expect(again).toHaveAttribute("data-policy-preset", "balanced"); }); test("generation: the day's spend, job by job, from the ledger", async ({ page }) => { await page.goto(urls.generationPolicyHref()); const spend = page.locator("[data-spend-today]"); await expect(spend).toHaveAttribute("data-spend-today", "0.42"); await expect(spend).toContainText("of the $20.00 daily cap · 1 job"); const job = spend.locator("[data-spend-job=fixture-1]"); await expect(job).toContainText("sweep · zeta i1"); await expect(job).toContainText("claude-sonnet-5-5"); await expect(job).toContainText("$0.42"); await expect(spend.getByRole("list", { name: "last 7 days" }).locator("li")).toHaveCount(7); }); async function planOf(request: APIRequestContext, data: Record) { const r = await request.post("/api/system/index", { data: { ...data, dryRun: true } }); expect(r.ok()).toBe(true); return (await r.json()).plan as { rows: { label: string }[]; blocked: boolean; summary: string }; } test("system: the index prune is a plan — Keep it changes nothing, Confirm drops the stale record", async ({ page, request }) => { // A project the index has a record of, then gone from disk: its record is stale. rmSync(path.join(FIXTURE, STALE), { recursive: true, force: true }); const made = await request.post("/api/projects/new", { data: { kind: "report-video", slug: STALE } }); expect(made.ok(), await made.text()).toBe(true); expect((await request.get("/api/browse/projects")).ok()).toBe(true); // the walk writes the record rmSync(path.join(FIXTURE, STALE), { recursive: true, force: true }); await expect.poll(async () => (await planOf(request, { action: "prune" })).rows.map((r) => r.label)).toContain(STALE); await page.goto(urls.systemHref()); const panel = page.locator("[data-panel=index]"); await panel.getByRole("button", { name: "Prune…" }).click(); const plan = page.getByRole("region", { name: "plan index prune" }); await expect(plan).toContainText("Prune the index"); await expect(plan.getByRole("list", { name: "touches" }).locator(`[data-plan-row="${STALE}"]`)).toBeVisible(); await plan.getByRole("button", { name: "Keep it" }).click(); await expect(plan).toBeHidden(); // Nothing was written: the record is still there. expect((await planOf(request, { action: "prune" })).rows.map((r) => r.label)).toContain(STALE); await panel.getByRole("button", { name: "Prune…" }).click(); const n = (await planOf(request, { action: "prune" })).rows.length; await plan.locator("[data-plan-confirm]").click(); await expect(panel.locator("[data-plan-done='index prune']")).toHaveText(`Dropped ${n} ${n === 1 ? "record" : "records"}`); const after = await planOf(request, { action: "prune" }); expect(after.rows).toEqual([]); expect(after.blocked).toBe(true); }); test("system: the index rebuild runs as a umtool job, after its plan", async ({ page, request }) => { await page.goto(urls.systemHref()); const panel = page.locator("[data-panel=index]"); await panel.getByRole("button", { name: "Rebuild…" }).click(); const plan = page.getByRole("region", { name: "plan index rebuild" }); await expect(plan).toContainText(/Re-read \d+ projects/); await expect(plan).toContainText("Runs as a job"); await plan.getByRole("button", { name: "Rebuild the index" }).click(); await expect(panel.locator("[data-plan-done='index rebuild']")).toHaveText("The index rebuild is running — a job, in Running"); // The job is umtool's own, through the CLI, and finishes. await expect .poll(async () => ((await (await request.get("/api/jobs?n=8")).json()).jobs as { label: string; state: string }[]).find((j) => j.label === "index-rebuild")?.state, { timeout: 60_000 }) .toBe("done"); }); test("system: a storage move with no media root is refused in the plan, never offered, and the API refuses its confirm", async ({ page, request }) => { await page.goto(urls.systemHref()); const panel = page.locator("[data-panel=storage]"); await expect(panel).toContainText("no media root (UMTOOL_MEDIA_DIR unset)"); await panel.getByRole("button", { name: "Move every out/ to the media root…" }).click(); const plan = page.getByRole("region", { name: "plan storage move-out all" }); await expect(plan.getByRole("list", { name: "refused" })).toContainText("UMTOOL_MEDIA_DIR is not set"); await expect(plan).toHaveAttribute("data-plan-blocked", "yes"); await expect(plan.locator("[data-plan-confirm]")).toHaveCount(0); await plan.getByRole("button", { name: "Keep it" }).click(); await expect(plan).toBeHidden(); const refused = await request.post("/api/system/storage", { data: { action: "move-out", all: true } }); expect(refused.status()).toBe(409); expect((await refused.json()).plan.refusals[0]).toContain("UMTOOL_MEDIA_DIR is not set"); // A project id, never a path; an unknown one is a 404. expect((await request.post("/api/system/storage", { data: { action: "move-back", project: "no/such", dryRun: true } })).status()).toBe(404); expect((await request.post("/api/system/storage", { data: { action: "rm", project: "x" } })).status()).toBe(400); // Nothing on disk moved. expect(existsSync(path.join(FIXTURE, "reports", "report-fixture", "video.manifest.json"))).toBe(true); }); test("system: the doctor's roots are the fixture's", async ({ page, request }) => { await page.goto(urls.systemHref()); const roots = page.locator("[data-panel=roots]"); await expect(roots).toContainText(".e2e-song"); await expect(roots).toContainText("UMTOOL_MEDIA_DIR unset"); // Status lines, not paragraphs (P6): the index is said once, in its own panel. await expect(page.locator("[data-panel=index] p").first()).toHaveText(/^\d+ records(, \d+ for projects that are gone)?( · rebuilt .+)?$|^No index — every read walks the disk\.$/); await expect(page.locator("[data-panel=tools]")).not.toContainText("index"); const doctor = await (await request.get("/api/doctor")).json(); expect(doctor.roots.reports.path).toContain(".e2e-song"); expect(doctor.roots.media.tiered).toBe(false); });