import { test, expect } from "@playwright/test"; import { existsSync, readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import * as urls from "./urls"; // --------------------------------------------------------------------------- // The narration review (release 25, T5): a cut's narration script, card by // card, before synthesis. Approve / reject (a note required) / edit the words // (which un-approves them) / play one card through the editor -- the suite's // stub, whose "voice" is an ffmpeg tone -- and a build that refuses what is not // approved once verdicts exist. Verdicts are drafts.review.json beside the // manifest (release 28; release 25's narration.review.json is still read). // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const PROJECT = "reports/narration-review-fixture"; const DIR = path.join(FIXTURE, "reports", "narration-review-fixture"); // Since release 28 the verdicts are the drafts review's: a card's are // `narrate//narration` in drafts.review.json. const REVIEW = path.join(DIR, "drafts.review.json"); const STUB_LOG = path.join(FIXTURE, "editor-stub.requests.json"); const review = (): Record => { const all = existsSync(REVIEW) ? (JSON.parse(readFileSync(REVIEW, "utf8")) as Record) : {}; return Object.fromEntries(Object.entries(all).map(([address, row]) => [address.replace(/^narrate\/(.+)\/narration$/, "$1"), row])); }; const manifest = () => JSON.parse(readFileSync(path.join(DIR, "video.manifest.json"), "utf8")) as { timeline: { id: string; narration?: { text: string } }[] }; const ttsAsks = (): { route?: string; text?: string }[] => { try { return (JSON.parse(readFileSync(STUB_LOG, "utf8")) as { route?: string; text?: string }[]).filter((r) => r.route === "tts"); } catch { return []; } }; test("review the narration card by card: approve, reject with a note, edit un-approves, play asks the editor", async ({ page }) => { await page.goto(urls.section(PROJECT, "narration")); await page.locator("[data-narration-link]").click(); await expect(page).toHaveURL(new RegExp(`/projects/${PROJECT}/narration$`)); const k1 = page.locator('[data-narration-card="k01"]'); const k2 = page.locator('[data-narration-card="k02"]'); await expect(k1).toHaveAttribute("data-state", "unreviewed"); await expect(k2).toHaveAttribute("data-state", "unreviewed"); await expect(page.locator('[data-review-file="absent"]')).toBeVisible(); // The words beside what the card shows, and the voice. await expect(k1.locator("[data-narration-screen]")).toContainText("The county"); await expect(k1.locator("[data-narration-text]")).toHaveValue("Half a year on, the county said yes."); await expect(k1.locator("[data-narration-voice]")).toContainText("kokoro ยท af_heart"); // Approve the first. await k1.locator('[data-verdict-button="approved"]').click(); await expect(k1).toHaveAttribute("data-state", "approved"); expect(review().k01.verdict).toBe("approved"); // A rejection needs its note. await k2.locator('[data-verdict-button="rejected"]').click(); await expect(k2.locator("[data-narration-error]")).toContainText("a rejection needs its note"); await k2.locator("[data-narration-note]").fill("furious is not in the clip"); await k2.locator('[data-verdict-button="rejected"]').click(); await expect(k2).toHaveAttribute("data-state", "rejected"); expect(review().k02).toMatchObject({ verdict: "rejected", note: "furious is not in the clip", text: "And then everybody was furious." }); // Edit the rejected words: saved through the manifest, and no longer rejected. await k2.locator("[data-narration-text]").fill("And then the meeting ran long."); await expect(k2.locator('[data-verdict-button="approved"]')).toBeDisabled(); await k2.locator('[data-action="save-text"]').click(); await expect(k2).toHaveAttribute("data-state", "unreviewed"); expect(manifest().timeline.find((e) => e.id === "k02")!.narration!.text).toBe("And then the meeting ran long."); expect(review().k02.verdict).toBeNull(); // Play: one card asked of the editor, then its voice on the page. const before = ttsAsks().length; await k1.locator('[data-action="play"]').click(); await expect(k1.locator("[data-narration-audio]")).toBeVisible({ timeout: 60_000 }); const asks = ttsAsks().slice(before); expect(asks.map((a) => a.text)).toEqual(["Half a year on, the county said yes."]); const audio = await page.request.get(`/api/report/narration/audio?project=${encodeURIComponent(PROJECT)}&card=k01`); expect(audio.status()).toBe(200); expect(audio.headers()["content-type"]).toBe("audio/wav"); // A played voice does not approve anything. await expect(k2).toHaveAttribute("data-state", "unreviewed"); }); test("the build refuses a narrated card the review has not approved", async ({ request }) => { const start = await request.post("/api/report/build", { data: { project: PROJECT, preset: "fast" } }); expect(start.ok()).toBeTruthy(); const { job } = (await start.json()) as { job: { id: string } }; let done: { state: string; error: string | null } = { state: "running", error: null }; const until = Date.now() + 120_000; while (Date.now() < until) { done = (await (await request.get(`/api/report/build?job=${job.id}`)).json()) as typeof done; if (done.state !== "running") break; await new Promise((r) => setTimeout(r, 400)); } expect(done.state).toBe("failed"); // The build's own sentence, in its events (an `error`) and log. expect(JSON.stringify(done)).toContain("timeline k02: its narration is not reviewed"); expect(existsSync(path.join(DIR, "out", "narration-review-fixture.mp4"))).toBe(false); }); test("A CARD READ AGAIN (lane line-rerun): Read again keeps a new reading without changing the card, Pick plays it, the verdict on the same words kept; the first reading one press away", async ({ page }) => { await page.goto(urls.section(PROJECT, "narration")); await page.locator("[data-narration-link]").click(); const k1 = page.locator('[data-narration-card="k01"]'); const card = () => manifest().timeline.find((e) => e.id === "k01")!.narration as { text: string; reading?: number }; const text = card().text; const state = await k1.getAttribute("data-state"); for (const label of ["Read the card again", "Read the card as edited", "Tune the card only", "Play new reads when they land"]) await expect(k1.locator(`[aria-label="${label}"]`), label).toHaveCount(1); const readings = k1.locator("[data-narration-reading]"); const had = await readings.count(); const before = ttsAsks().length; await k1.locator('[aria-label="Read the card again"]').click(); // The first reading it kept (Play's, if any) and this one. await expect.poll(() => readings.count(), { timeout: 60_000 }).toBeGreaterThan(had); expect(ttsAsks().length).toBe(before + 1); expect(card()).toMatchObject({ text }); expect(card().reading ?? 0).toBe(0); // Pick the new reading: the card plays it -- its verdict on these words kept. const fresh = k1.locator('[data-narration-reading]:not([data-playing])').last(); const key = await fresh.getAttribute("data-narration-reading"); await k1.locator(`[data-narration-reading-pick="${key}"]`).click(); await expect(k1.locator(`[data-narration-reading="${key}"]`)).toHaveAttribute("data-playing", "1"); expect(card().reading).toBeGreaterThan(0); if (state) await expect(k1).toHaveAttribute("data-state", state); // Back to the first: one press, nothing asked. const asked = ttsAsks().length; await k1.locator('[data-narration-reading]:not([data-playing]) [data-narration-reading-pick]').first().click(); await expect.poll(() => card().reading ?? 0).toBe(0); expect(ttsAsks().length).toBe(asked); });