import { test, expect } from "@playwright/test"; import { existsSync, readFileSync, readdirSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { WARM_TIMEOUT, warm } from "./warm"; import * as urls from "./urls"; // --------------------------------------------------------------------------- // Release 28 M9, the surfaces of the media drafts -- since P4b ONE surface, // the Media tab: // - "Draft with " per section: the job asked of the editor (its // model, its entries, its force), followed to its end; then the section // re-read — the field's line with its provenance and review, the last // run, the alternative kept for the operator's field and Use this; a // project made from no iteration offers none, and says why; // - Draft everything on the Media tab: the chain with its drafts, a // refused draft recorded failed and passed, the panels skipped, the // voices, the fast build and the podcast, then the YouTube draft and // out/youtube.txt. // The editor stub drafts this article canned (fixtures/editor-stub.mjs // cannedDraft); the build, the voices (the stub's tone) and the podcast are // real. Fixture: fixtures/media-surfaces-fixture.mjs. // --------------------------------------------------------------------------- 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 = "msurf"; const PROJECT = `gen-projects/${ARTICLE}/i1`; const projectDir = (n: number) => path.join(FIXTURE, "gen-projects", ARTICLE, `i${n}`); const manifestOf = (n: number) => JSON.parse(readFileSync(path.join(projectDir(n), "video.manifest.json"), "utf8")); const asked = () => (JSON.parse(readFileSync(STUB_LOG, "utf8")) as { route?: string; article?: string; [k: string]: unknown }[]).filter((r) => r.route === "report-draft" && r.article === ARTICLE); test.beforeAll(async ({ playwright }) => { test.setTimeout(WARM_TIMEOUT); await warm(playwright, [urls.section(PROJECT, "drafts"), urls.section(`gen-projects/${ARTICLE}/i2`, "drafts"), `/api/articles/draft?project=${encodeURIComponent(PROJECT)}`, "/api/articles/policy", `/articles/${ARTICLE}?tab=iterations`]); }); test("Draft with per section: the job followed, the badge, the last run, an alternative and Use this", async ({ page }) => { test.setTimeout(180_000); await page.goto(urls.section(PROJECT, "drafts")); const panel = page.getByRole("region", { name: "Media drafts" }); await expect(panel).toHaveAttribute("data-draft-panel", `${ARTICLE}/i1`); // Eight sections (release 29 G2: the social posts); flips and threads say why they cannot draft on this cut. await expect(panel.locator("[data-draft-task]")).toHaveCount(8); await expect(panel.locator("[data-draft-task='flips'] [data-draft-blocked]")).toContainText("this cut has none (render.chrome)"); const cut = panel.locator("[data-draft-task='cut']"); // The policy's models, the route's preselected. await expect(cut.getByRole("combobox", { name: "model for the cut" })).toHaveValue("qwen3:8b"); // The cut: drafted over the converter's order; its badge and its review. await cut.getByRole("combobox", { name: "model for the cut" }).selectOption("claude-sonnet-5-5"); await cut.getByRole("button", { name: "Draft the cut" }).click(); await expect(cut).toHaveAttribute("data-draft-job", "done", { timeout: 30_000 }); expect(asked().at(-1)).toMatchObject({ task: "cut", article: ARTICLE, iteration: 1, model: "claude-sonnet-5-5", authorization: "Bearer umtool-e2e-token" }); // The field's line: its provenance (the draft file, by what model, the attempt) and its review. const order = panel.locator("[data-draft-row='cut/*/timeline']"); await expect(order.locator("[data-draft-prov]")).toContainText("by claude-sonnet-5-5"); await expect(order.locator("[data-draft-prov]")).toContainText("cut-draft.json"); await expect(cut.locator("[data-draft-run='cut']")).toContainText("1 written"); expect(manifestOf(1).timeline.filter((e: { type: string }) => e.type === "clip").map((e: { id: string }) => e.id)).toEqual(["c02", "c01"]); await order.locator("[data-draft-action='approve']").click(); await expect(order).toHaveAttribute("data-state", "approved"); // On-screen, limited to the entry ticked. const onscreen = panel.locator("[data-draft-task='onscreen']"); await onscreen.locator("summary").click(); await onscreen.getByRole("checkbox", { name: "only c01 (on-screen titles)" }).check(); await onscreen.getByRole("button", { name: "Draft on-screen titles" }).click(); await expect(onscreen).toHaveAttribute("data-draft-job", "done", { timeout: 30_000 }); expect(asked().at(-1)).toMatchObject({ task: "onscreen", entries: ["c01"] }); await expect(onscreen.locator("[data-draft-run='onscreen']")).toContainText("1 written"); const m = manifestOf(1); const c01 = m.timeline.find((e: { id: string }) => e.id === "c01"); expect(c01.onscreen).toEqual({ title: "What c01 shows", subtitle: "From the county vote" }); expect(c01.drafted.onscreen).toMatchObject({ task: "onscreen", model: "qwen3:8b", iteration: 1 }); expect(m.timeline.find((e: { id: string }) => e.id === "c02").onscreen).toBeUndefined(); // YouTube: the operator's text. Unforced, nothing to draft; forced, the // draft is kept as an alternative, and Use this writes it. const yt = panel.locator("[data-draft-task='youtube']"); await yt.getByRole("button", { name: "Draft YouTube text" }).click(); await expect(yt).toHaveAttribute("data-draft-job", "failed", { timeout: 30_000 }); await expect(yt.locator("[data-draft-run='youtube']")).toContainText("1 skipped"); await yt.getByRole("checkbox", { name: "force: re-draft YouTube text" }).check(); await yt.getByRole("button", { name: "Draft YouTube text" }).click(); await expect(yt.locator("[data-draft-run='youtube']")).toContainText("1 kept as an alternative", { timeout: 30_000 }); expect(asked().at(-1)).toMatchObject({ task: "youtube", force: true }); expect(manifestOf(1).publish.youtube.title).toBe("The operator's own title"); const alt = panel.locator("[data-media-section='youtube'] [data-draft-alternatives='youtube/*/publish.youtube']"); await expect(alt).toContainText("The county vote, as the channel told it"); await alt.getByRole("button", { name: "Use this" }).click(); await expect.poll(() => manifestOf(1).publish.youtube.title, { timeout: 15_000 }).toBe("The county vote, as the channel told it"); // Taken, it is the operator's: no mark. expect(manifestOf(1).drafted?.["publish.youtube"]).toBeUndefined(); // A project made from no iteration offers no draft, and says why. await page.goto(urls.section("reports/youtube-fixture", "drafts")); await expect(page.getByRole("region", { name: "Media drafts" }).locator("[data-draft-unlinked]")).toHaveText("Drafting is off: no article iteration made this project."); }); test("Draft everything: every draft in order on the model chosen, a refused one passed, the panels skipped, then the fast build, the podcast and out/youtube.txt", async ({ page }) => { test.setTimeout(420_000); // On i2's cut's Media tab (P4b): the Iterations tab links it. await page.goto(urls.articleHref({ site: "articles", id: ARTICLE }, { tab: "iterations" })); await page.locator("[data-iteration='2'] [data-media-link]").click(); await expect(page.getByRole("region", { name: "Media drafts" })).toHaveAttribute("data-draft-panel", `${ARTICLE}/i2`); await page.getByRole("combobox", { name: "Draft everything model" }).selectOption("qwen3:8b"); const before = asked().length; await page.getByRole("button", { name: "Draft everything" }).click(); const chain = page.getByRole("region", { name: "i2 Draft everything" }); await expect(chain.locator("[data-chain-step]")).toHaveCount(16); const step = (name: string) => chain.locator(`[data-chain-step='${name}']`); await expect(step("draft-social")).toHaveAttribute("data-step-status", "done", { timeout: 360_000 }); await expect(chain.locator("[data-chain-job]")).toHaveAttribute("data-chain-job", "done", { timeout: 30_000 }); for (const [name, status] of [ ["current", "done"], ["manifest", "done"], ["draft-cut", "done"], ["draft-onscreen", "done"], ["draft-teaser", "failed"], ["draft-flips", "skipped"], ["draft-threads", "skipped"], ["draft-narrate", "done"], ["voices", "done"], ["video", "done"], ["podcast", "done"], ["draft-youtube", "done"], ["draft-social", "done"], ] as const) { await expect(step(name), name).toHaveAttribute("data-step-status", status); } await expect(step("draft-flips")).toContainText("no on-screen deck"); // The drafts the editor was asked for, in order, on the model chosen (flips // and threads never asked: no deck to draw them on). const drafts = asked().slice(before); expect(drafts.map((r) => r.task)).toEqual(["cut", "onscreen", "teaser", "narrate", "youtube", "social"]); expect(drafts.every((r) => r.model === "qwen3:8b" && r.iteration === 2)).toBe(true); const m = manifestOf(2); expect(m.drafted.timeline).toMatchObject({ task: "cut", model: "qwen3:8b" }); expect(m.timeline.find((e: { id: string }) => e.id === "k01").narration.text).toBe("The county, as the channel told it."); expect(m.publish.youtube.title).toBe("The county vote, as the channel told it"); // The fast build, the podcast and the YouTube text are on disk. const out = path.join(projectDir(2), "out"); expect(existsSync(path.join(out, "msurf-i2.mp4"))).toBe(true); const text = readFileSync(path.join(out, "youtube.txt"), "utf8"); expect(text.split("\n")[0]).toBe("The county vote, as the channel told it"); expect(text).toContain("Sources"); // The social posts (release 29 G2), as files to copy. expect(m.publish.x.post).toBe("The county vote, as the channel told it."); expect(readFileSync(path.join(out, "x.txt"), "utf8")).toBe("The county vote, as the channel told it.\n\n— 2/2 —\nThe channel said it twice.\n"); expect(readFileSync(path.join(out, "bluesky.txt"), "utf8")).toBe("The county vote, as the channel told it.\n"); // Off by default: no deck step, and the cut still has no deck. await expect(chain.locator("[data-chain-step='deck']")).toHaveCount(0); expect(m.render.chrome).toBeUndefined(); }); test("Draft everything with the deck (release 29 G1): the deck step adds the minimal deck, the flips are drafted on it, the threads then skipped", async ({ page }) => { test.setTimeout(240_000); await page.goto(urls.articleHref({ site: "articles", id: ARTICLE }, { tab: "iterations" })); await page.locator("[data-iteration='3'] [data-media-link]").click(); await expect(page.getByRole("region", { name: "Media drafts" })).toHaveAttribute("data-draft-panel", `${ARTICLE}/i3`); expect(manifestOf(3).render.chrome).toBeUndefined(); await page.getByRole("combobox", { name: "Draft everything model" }).selectOption("qwen3:8b"); await page.getByRole("checkbox", { name: "with the deck" }).check(); const before = asked().length; await page.getByRole("button", { name: "Draft everything" }).click(); const chain = page.getByRole("region", { name: "i3 Draft everything" }); await expect(chain.locator("[data-chain-step]")).toHaveCount(17); const step = (name: string) => chain.locator(`[data-chain-step='${name}']`); await expect(step("draft-threads")).toHaveAttribute("data-step-status", "skipped", { timeout: 180_000 }); // Done with what this case is about: the build is the other test's. await chain.getByRole("button", { name: "Cancel the chain" }).click(); await expect(step("deck")).toHaveAttribute("data-step-status", "done"); await expect(step("deck")).toContainText("deck added"); await expect(step("draft-flips")).toHaveAttribute("data-step-status", "done"); await expect(step("draft-threads")).toContainText("the cut has flips"); const m = manifestOf(3); expect(m.render.chrome).toMatchObject({ engine: "hyperframes", layout: "deck", deck: {} }); expect(m.render.chrome.flips.pairs[0].id).toBe("county"); expect(m.drafted["render.chrome.flips"]).toMatchObject({ task: "flips", model: "qwen3:8b", iteration: 3 }); // The cut before the deck is in revisions/. expect(readdirSync(path.join(projectDir(3), "revisions")).some((f) => f.includes("auto-before-deck"))).toBe(true); expect(asked().slice(before).map((r) => r.task).slice(0, 4)).toEqual(["cut", "onscreen", "teaser", "flips"]); });