import { test, expect, type Page } from "@playwright/test"; import { readFileSync, writeFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { WARM_TIMEOUT, warm } from "./warm"; import * as urls from "./urls"; // --------------------------------------------------------------------------- // The media drafts (release 28 M1–M3; lane E adds flips, threads, youtube): // each task asked through umtool's generate, run by the editor stub FOR REAL // — the common task over the fixture iteration's project, its model the fake // ollama scripted with the test kit's recorded run — and followed on the // iteration to its step's end, shown on the Iterations tab. Then the project's manifest // says what was drafted, marked with its provenance. // // mdraft an article in the making; i1, i2, i3 each a done sweep with the // kit's basic cut (fixtures/media-draft-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 = "mdraft"; const manifestOf = (n: number) => JSON.parse(readFileSync(path.join(FIXTURE, "gen-projects", ARTICLE, `i${n}`, "video.manifest.json"), "utf8")); const draftFileOf = (n: number, task: string) => JSON.parse(readFileSync(path.join(FIXTURE, "gen-projects", ARTICLE, `i${n}`, `${task}-draft.json`), "utf8")); const lastAsked = () => (JSON.parse(readFileSync(STUB_LOG, "utf8")) as { route?: string; [k: string]: unknown }[]).filter((r) => r.route === "report-draft").at(-1); const ids = (m: { timeline: { id: string }[] }) => m.timeline.map((e) => e.id); const entry = (m: { timeline: { id: string }[] }, id: string) => m.timeline.find((e) => e.id === id) as Record; test.beforeAll(async ({ playwright }) => { test.setTimeout(WARM_TIMEOUT); await warm(playwright, [`/articles/${ARTICLE}`, `/api/articles/iterations?article=${ARTICLE}`, "/api/articles/policy"]); }); // Ask for the task on iteration n, and follow its step to its end on the tab. async function draft(page: Page, task: string, n: number, status = "done") { const r = await page.request.post("/api/articles/generate", { data: { task, article: ARTICLE, iteration: n, model: "qwen3:8b" } }); expect(r.status(), await r.text()).toBe(200); expect(await r.json()).toMatchObject({ ok: true, iteration: n }); expect(lastAsked()).toMatchObject({ task, article: ARTICLE, iteration: n, model: "qwen3:8b", authorization: "Bearer umtool-e2e-token" }); // The job runs in the stub (a child under tsx); its step is the iteration's. await expect .poll( async () => { const list = (await (await page.request.get(`/api/articles/iterations?article=${ARTICLE}`)).json()) as { iterations: { n: number; steps: { task: string; status: string }[] }[]; }; return list.iterations.find((i) => i.n === n)?.steps.find((s) => s.task === task)?.status ?? "none"; }, { timeout: 60_000, intervals: [1000] }, ) .toBe(status); await page.goto(urls.articleHref({ site: "articles", id: ARTICLE }, { tab: "iterations" })); await expect(page.locator(`[data-iteration='${n}'] [data-step='${task}']`)).toHaveAttribute("data-step-status", status); } test("cut: the clips and their order drafted from the report's citations, the timeline built from the entries it had", async ({ page }) => { test.setTimeout(120_000); const before = manifestOf(1); expect(before.drafted.timeline).toMatchObject({ task: "convert", model: "code" }); await draft(page, "cut", 1); const m = manifestOf(1); expect(ids(m)).toEqual(["title", "the-contest", "c04", "c06", "the-bakery", "c03", "c01"]); expect(entry(m, "c04")).toEqual(entry(before, "c04")); expect(m.drafted.timeline).toMatchObject({ task: "cut", model: "qwen3:8b", iteration: 1, attempt: 2 }); expect(draftFileOf(1, "cut").units.cut.applied).toBe("applied"); }); test("onscreen: each clip's title and subtitle, several to an ask; a clip with no clean draft left as it was", async ({ page }) => { test.setTimeout(120_000); await draft(page, "onscreen", 2); const m = manifestOf(2); expect(entry(m, "c01").onscreen).toEqual({ title: "She will not sell to a chain", subtitle: '"I would never sell the bakery to a chain"' }); expect(entry(m, "c01").drafted.onscreen).toMatchObject({ task: "onscreen", model: "qwen3:8b", iteration: 2, attempt: 1 }); expect(entry(m, "c04").drafted.onscreen.attempt).toBe(2); expect(entry(m, "c06").onscreen).toBeUndefined(); expect(draftFileOf(2, "onscreen").units.c06.attempts).toHaveLength(2); }); test("teaser: a cut with none gets one after its last clip, its lines drafted and checked by the build's validator", async ({ page }) => { test.setTimeout(120_000); await draft(page, "teaser", 3); const m = manifestOf(3); expect(ids(m).slice(-2)).toEqual(["c06", "teaser"]); expect(entry(m, "teaser")).toMatchObject({ type: "teaser", lines: ["Synth Kitchen", "The bakery", "The contest", "What the baker says"] }); expect(entry(m, "teaser").drafted.lines).toMatchObject({ task: "teaser", attempt: 2 }); }); test("social (release 29 G2): a post for X and one for Bluesky with their threads, counted by code; no link for an article not public; shown on the Media tab with Copy", async ({ page }) => { test.setTimeout(120_000); await draft(page, "social", 4); const m = manifestOf(4); expect(m.publish.x).toEqual({ post: 'Synth Kitchen\'s baker says "I would never sell the bakery to a chain" and that she paid for the new mixer herself.', thread: ["In a later stream she says she did not enter the contest this year.", "She will not talk about the lease."], }); expect(m.publish.bluesky.post).toBe("What the baker of Synth Kitchen says about the bakery: she would not sell it to a chain, and she paid for the new mixer herself."); expect(m.drafted["publish.x"]).toMatchObject({ task: "social", model: "qwen3:8b", iteration: 4, attempt: 2 }); expect(m.drafted["publish.bluesky"]).toMatchObject({ task: "social", attempt: 1 }); const d = draftFileOf(4, "social"); expect(d.units.x.attempts[0].hard.join(" | ")).toMatch(/the narrator is third person/); // The Media tab shows each platform's posts with their counted lengths, and Copy. await page.goto(urls.section(`gen-projects/${ARTICLE}/i4`, "drafts")); const panel = page.getByRole("region", { name: "Social posts" }); await expect(panel.getByRole("region", { name: "X post" }).locator("[data-social-post]")).toHaveCount(3); await expect(panel.getByRole("region", { name: "X post" }).locator("[data-social-count]").first()).toHaveText(`1/3 · ${m.publish.x.post.length}/280 as X counts`); await expect(panel.getByRole("region", { name: "Bluesky post" }).locator("[data-social-count]").first()).toHaveText(`1/2 · ${m.publish.bluesky.post.length}/300 graphemes`); await expect(panel.getByRole("region", { name: "X post" }).getByRole("button", { name: "Copy" })).toBeVisible(); // Its draft row on the Media tab, in Draft everything's order (the last). await expect(page.locator("[data-media-chip='social']")).toBeVisible(); // The video is up: its URL set after the posts were drafted. The section // says they link the old address and re-drafts them (review fix). const file = path.join(FIXTURE, "gen-projects", ARTICLE, "i4", "video.manifest.json"); const videoUrl = "https://video.example/watch/mdraft-4"; writeFileSync(file, JSON.stringify({ ...manifestOf(4), publish: { ...manifestOf(4).publish, videoUrl } }, null, 2) + "\n"); await page.reload(); const stale = panel.locator("[data-social-stale]"); await expect(stale).toHaveAttribute("data-social-stale", "x bluesky"); await stale.getByRole("button", { name: "Re-draft the posts" }).click(); await expect(stale).toContainText("queued"); expect(lastAsked()).toMatchObject({ task: "social", article: ARTICLE, iteration: 4, force: true }); await expect.poll(() => manifestOf(4).publish.x.post, { timeout: 60_000 }).toMatch(/\n\nhttps:\/\/video\.example\/watch\/mdraft-4$/); expect(manifestOf(4).publish.bluesky.post.endsWith(`\n\n${videoUrl}`)).toBe(true); });