import { test, expect, type APIRequestContext, type Page } from "@playwright/test"; import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { portFor } from "yt-dlp-transcript-common/lib/ports.mjs"; import { LIVE_ARTICLE, LIVE_JOB } from "./fixtures/run-trace-fixture.mjs"; import { runHref } from "../lib/research/runs-hrefs"; // --------------------------------------------------------------------------- // P2b: what is running (on home, beside the Inbox, since the redesign's L4; // the rail's foot before), and drafts as decisions in the Inbox. // // The Running list (components/shell/RunningList.tsx, lib/activity.ts) is // umtool's own jobs and the editor's live ones, from the editor stub's // `GET /api/ops/jobs?active=1`, which a spec controls through the stub's own // door (`POST /__stub/ops-jobs {mode, jobs}`, e2e/fixtures/editor-stub.mjs). // The editor's three ways of not answering -- unreachable, its WORKER_TOKEN // unset (503), and nothing running -- are three different sentences. // // The drafts card (lib/inbox.ts, lib/inbox/drafts.mjs) is written here: a // project whose fields a model drafted, one card in Review, its action the // review page; once every field is approved there, the card is gone on the // next read -- the approve writes only drafts.review.json, which // reportSignature now signs. // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const STUB = `http://127.0.0.1:${portFor("EDITOR_STUB_PORT")}`; // `live: false`: the stub's own drafts stay out of the list (a "slow" one an // earlier spec left running would be counted). async function editorJobs(request: APIRequestContext, mode: "ok" | "editor-token" | "unreachable", jobs: Record[] = [], live = false) { const r = await request.post(`${STUB}/__stub/ops-jobs`, { data: { mode, jobs, live } }); expect(r.ok(), await r.text()).toBeTruthy(); // Ask again now, so the page's first read is not the 2 s memo of the last test. const fresh = await request.get("/api/running?fresh=1"); expect(fresh.ok()).toBeTruthy(); } // On home since the rail kept navigation only (redesign L4). const running = (page: Page) => page.locator("[data-home-aside] [data-home-section=running]"); test.afterEach(async ({ request }) => { await editorJobs(request, "ok", [], true); }); const NOW = Date.now(); const JOBS = [ { id: "e2e-lane-dl", kind: "auto-download", status: "running", queuedAt: NOW - 9e5, startedAt: NOW - 9e5, queueKey: "" }, { id: "e2e-lane-tx", kind: "auto-transcribe", status: "running", queuedAt: NOW - 9e5, startedAt: NOW - 9e5, queueKey: "" }, { id: "e2e-draft-run", kind: "report-draft", status: "running", queuedAt: NOW - 60_000, startedAt: NOW - 60_000, progress: { metric: "turns", initial: 0, current: 3, target: 12 }, log: [ "sweep of zeta i7 on claude-sonnet-5-5 (the Claude API, cap $5)", "turn 1: search_transcripts — ~$0.04", "turn 3: get_transcripts — ~$0.31", ], }, { id: "e2e-sync", kind: "download-missing", channelSlug: "sitechan", status: "running", queuedAt: NOW - 30_000, startedAt: NOW - 30_000, progress: { metric: "downloads", initial: 2, target: 8 }, }, { id: "e2e-draft-queued", kind: "report-draft", status: "queued", queuedAt: NOW - 10_000, queue: { key: "draft:api", position: 2, queued: 2 } }, { id: "e2e-q1", kind: "download-missing", channelSlug: "otherchan", status: "queued", queuedAt: NOW - 5000, queue: { key: "youtube", position: 1, queued: 3 } }, { id: "e2e-q2", kind: "download-missing", channelSlug: "otherchan", status: "queued", queuedAt: NOW - 4000, queue: { key: "youtube", position: 2, queued: 3 } }, { id: "e2e-q3", kind: "download-missing", channelSlug: "otherchan", status: "queued", queuedAt: NOW - 3000, queue: { key: "youtube", position: 3, queued: 3 } }, ]; test("home lists the editor's running and queued jobs, a draft's spend, and the day's", async ({ page, request }) => { await editorJobs(request, "ok", JOBS); await page.goto("/"); const r = running(page); await expect(r).toHaveAttribute("data-editor-state", "ok"); await expect(r.locator("[data-running-count]")).toHaveAttribute("data-running-count", "2"); // The fixture's ledger holds today's $0.42 (iterations-fixture.mjs). await expect(r.locator("[data-today-spend]")).toHaveText("$0.42 today"); // A running draft: its kind in words, its article (from the log's first // line: no iteration names the job), its live spend in Meter, its turns, // a bar -- and, a sweep, a link to its run page (R3), not the editor. const draft = r.locator("[data-running=e2e-draft-run]"); await expect(draft).toHaveAttribute("data-running-state", "running"); await expect(draft.locator("[data-running-kind]")).toHaveText("Report draft"); await expect(draft.locator("[data-running-spend]")).toHaveText("$0.31"); await expect(draft.locator("[data-running-target]")).toHaveText("zeta i7 · 3/12 turns"); await expect(draft.locator("[data-job-progress]")).toHaveAttribute("data-job-progress", "25"); await expect(draft.locator("a")).toHaveAttribute("href", runHref("zeta", 7)); // A sync: its channel, its count; it has no piece, so it links the editor's /jobs. const sync = r.locator("[data-running=e2e-sync]"); await expect(sync.locator("[data-running-kind]")).toHaveText("Download missing"); await expect(sync.locator("[data-running-target]")).toHaveText("sitechan · 2/8 downloads"); await expect(sync.locator("[data-job-progress-text]")).toHaveText("2/8 downloads"); await expect(sync.locator("[data-running-pct]")).toHaveText("25%"); await expect(sync.locator("a")).toHaveAttribute("href", `${STUB}/jobs`); // A queued draft is a row, said as queued; queued syncs and the lanes fold. const queued = r.locator("[data-running=e2e-draft-queued]"); await expect(queued).toHaveAttribute("data-running-state", "queued"); await expect(queued.locator("[data-job-progress-text]")).toHaveText("queued · 1 ahead"); await expect(queued.locator("[data-job-progress]")).toHaveCount(0); await expect(queued.locator("[data-running-target]")).toHaveCount(0); await expect(r.locator("[data-running=e2e-q1]")).toHaveCount(0); await expect(r.locator("[data-running=e2e-lane-dl]")).toHaveCount(0); await expect(r.locator("[data-running-more]")).toHaveText("+ 3 queued · 2 lanes"); await expect(r.locator("[data-running-more]")).toHaveAttribute("href", `${STUB}/jobs`); // Rows in order: running (generation first), then the queued draft. await expect(r.locator("[data-running]")).toHaveCount(3); expect(await r.locator("[data-running]").evaluateAll((els) => els.map((e) => e.getAttribute("data-running")))).toEqual([ "e2e-draft-run", "e2e-sync", "e2e-draft-queued", ]); await expect(r.locator("[data-running-empty], [data-running-editor]")).toHaveCount(0); // The API says the same, with no tokens in it. const api = await (await request.get("/api/running")).json(); expect(api.running).toBe(2); expect(api.queued).toBe(4); expect(api.more).toMatchObject({ running: 0, queued: 3, lanes: 2 }); expect(JSON.stringify(api)).not.toContain("umtool-e2e-token"); }); test("nothing running, the editor's token unset, and an editor that does not answer are three sentences", async ({ page, request }) => { await editorJobs(request, "ok", [{ id: "e2e-lane-only", kind: "auto-publish", status: "running", queuedAt: NOW, startedAt: NOW }]); await page.goto("/"); const r = running(page); await expect(r).toHaveAttribute("data-editor-state", "ok"); await expect(r.locator("[data-running-empty]")).toHaveText("Nothing running"); await expect(r.locator("[data-running-more]")).toHaveText("1 lane"); await expect(r.locator("[data-running-count]")).toHaveAttribute("data-running-count", "0"); await editorJobs(request, "editor-token"); await page.reload(); await expect(r).toHaveAttribute("data-editor-state", "editor-token"); await expect(r.locator("[data-running-editor]")).toHaveText("Editor's WORKER_TOKEN not set"); await expect(r.locator("[data-running-editor]")).toHaveAttribute("title", /worker endpoint disabled/); await expect(r.locator("[data-running-empty]")).toHaveCount(0); await editorJobs(request, "unreachable"); await page.reload(); await expect(r).toHaveAttribute("data-editor-state", "unreachable"); await expect(r.locator("[data-running-editor]")).toHaveText("Editor unreachable"); await expect(r.locator("[data-running-empty]")).toHaveCount(0); // The day's spend is the ledger's: it is said whatever the editor does. await expect(r.locator("[data-today-spend]")).toHaveText("$0.42 today"); }); test("a report-draft row links its run page when it is a sweep (R3), its article's iterations when not", async ({ page }) => { await editorJobs(page.request, "ok", [ // Placed by its iteration's jobId (the run fixture's live sweep). { id: LIVE_JOB, kind: "report-draft", status: "running", queuedAt: NOW - 20_000, startedAt: NOW - 20_000 }, // Placed by its log: a narration step, not a sweep. { id: "e2e-draft-narrate", kind: "report-draft", status: "running", queuedAt: NOW - 10_000, startedAt: NOW - 10_000, log: ["narrate of zeta i1 on qwen3:8b (the local model)"] }, ]); await page.goto("/"); const r = running(page); const sweep = r.locator(`[data-running=${LIVE_JOB}]`); await expect(sweep.locator("[data-running-target]")).toContainText(`${LIVE_ARTICLE} i1`); await expect(sweep.locator("a")).toHaveAttribute("href", runHref(LIVE_ARTICLE, 1)); await expect(r.locator("[data-running=e2e-draft-narrate] a")).toHaveAttribute("href", "/articles/zeta?tab=iterations"); // The link lands on the live run. await sweep.locator("a").click(); await expect(page).toHaveURL(new RegExp(`/research/runs/${LIVE_ARTICLE}/1$`)); await expect(page.locator("[data-run-mode]")).toHaveAttribute("data-run-mode", "live"); }); // ─── Drafts as decisions ──────────────────────────────────────────────────── const DRAFTS = "reports/p2b-drafts"; const DRAFTS_DIR = path.join(FIXTURE, "reports", "p2b-drafts"); const P = (task: string, model = "qwen3:8b") => ({ task, model, at: "2026-10-14T10:00:00.000Z", iteration: 1, attempt: 1 }); test.describe("drafts in the Inbox", () => { test.beforeAll(() => { // The build fixture's manifest as the template (nothing rewrites it), with a // timeline of three drafted fields: one on-screen text, two narrations. const template = JSON.parse(readFileSync(path.join(FIXTURE, "reports", "draft-review-build-fixture", "video.manifest.json"), "utf8")); const manifest = { ...template, slug: "p2b-drafts", title: "The P2b Drafts Fixture", timeline: [ { type: "clip", id: "c01", video: "vid1", start: 3.0, end: 6.0, cite: 3, section: 0, lock: true, quote: "and because", onscreen: { title: "A drafted title" }, drafted: { onscreen: P("onscreen") } }, { type: "card", id: "k01", style: "chapter", seconds: "auto", heading: "One", narration: { text: "The first line." }, drafted: { narration: P("narrate") } }, { type: "card", id: "k02", style: "chapter", seconds: "auto", heading: "Two", narration: { text: "The second line." }, drafted: { narration: P("narrate") } }, ], }; rmSync(DRAFTS_DIR, { recursive: true, force: true }); mkdirSync(DRAFTS_DIR, { recursive: true }); writeFileSync(path.join(DRAFTS_DIR, "video.manifest.json"), JSON.stringify(manifest, null, 2) + "\n"); }); test.afterAll(() => { rmSync(DRAFTS_DIR, { recursive: true, force: true }); }); test("a cut with unreviewed drafts is one Review card; approving them on the review page clears it", async ({ page, request }) => { // The API: one row per drafted field, in Review, the action the review page. const rows = ((await (await request.get("/api/browse/decisions")).json()) as { items: { kind: string; project: string; group: string; target: string; action: { label: string; href: string } }[]; }).items.filter((d) => d.kind === "drafts-unreviewed" && d.project === DRAFTS); expect(rows.map((d) => d.target).sort()).toEqual(["narrate/k01/narration", "narrate/k02/narration", "onscreen/c01/onscreen"]); expect(rows.every((d) => d.group === "review")).toBe(true); expect(rows[0].action).toEqual({ label: "Review drafts", href: `/projects/${DRAFTS}/review` }); await page.goto("/"); const card = page.locator("[data-group=review] [data-card=drafts-unreviewed]").filter({ hasText: "The P2b Drafts Fixture" }); await expect(card).toHaveCount(1); await expect(card.locator("[data-count]")).toHaveText("3"); // Titled like the canvas: the piece, then what was drafted and how many; who drafted it under that. await expect(card.locator("[data-card-title]")).toHaveText("The P2b Drafts Fixture · On-screen text and narration drafts · 3 fields"); await expect(card.locator("small")).toHaveText("by qwen3:8b"); await card.locator("[data-action-link]").click(); await expect(page).toHaveURL(new RegExp(`/projects/${DRAFTS}/review$`)); // The review opens on the audition deck (release 32 R1); the list is one toggle away. await page.locator("[data-review-view=list]").click(); await expect(page).toHaveURL(new RegExp(`/projects/${DRAFTS}/review\\?view=list$`)); for (const address of ["onscreen/c01/onscreen", "narrate/k01/narration", "narrate/k02/narration"]) { const row = page.locator(`[data-draft-row="${address}"]`); await row.locator('[data-draft-action="approve"]').click(); await expect(row).toHaveAttribute("data-state", "approved"); } // Only drafts.review.json was written; the card is gone on the next read. await page.goto("/"); await expect(page.locator("[data-group=review] [data-card=drafts-unreviewed]").filter({ hasText: "The P2b Drafts Fixture" })).toHaveCount(0); const after = ((await (await request.get("/api/browse/decisions")).json()) as { items: { kind: string; project: string }[] }).items; expect(after.filter((d) => d.kind === "drafts-unreviewed" && d.project === DRAFTS)).toEqual([]); }); });