import { test, expect } from "@playwright/test"; import { copyFileSync, existsSync, readFileSync, readdirSync, rmSync } from "node:fs"; import { createHash } from "node:crypto"; import path from "node:path"; import { fileURLToPath } from "node:url"; import * as urls from "./urls"; import { READS_P1 } from "./fixtures/reads-fixture.mjs"; // --------------------------------------------------------------------------- // A VIDEO's READS (lane reads-find, S4 of plans/orpheus-retune.md; components/projects/ReadsPanel.tsx, // /api/report/reads, lib/report/readsCli.mjs). Fixture: e2e/fixtures/reads-fixture.mjs -- two post items read by this // video's "ferry" character, a recorded read, a narrated card; "north pier" in three of them. Every step a press: // // every control there (aria-labels); Find "north pier": a count of matches and of lines, spoken text only // Replace -> These reads: a preview (line, before -> after; the recorded read not changed), Apply -> the changed // rows stale, nothing synthesized, every drawn card byte-identical // Re-read 2 changed lines -> the app's queue runs them one at a time through the editor STUB to done: the post's // re-read unpicked, the card's playing; Listen through plays; Pick new -> fresh; the cards still drawn the same // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const P = "reports/reads-fixture"; const DIR = path.join(FIXTURE, "reports", "reads-fixture"); const MANIFEST = path.join(DIR, "video.manifest.json"); const STUB_LOG = path.join(FIXTURE, "editor-stub.requests.json"); type Tts = { route: string; text?: string; voice?: string }; const ttsAsks = () => (existsSync(STUB_LOG) ? (JSON.parse(readFileSync(STUB_LOG, "utf8")) as Tts[]) : []).filter((r) => r.route === "tts"); const sha = (s: string) => createHash("sha1").update(s).digest("hex"); const files = (d: string) => (existsSync(d) ? readdirSync(d, { recursive: true }).map(String).sort() : []); /** * What the cut DRAWS: each post item's card as the reading pin hashes it (plain, following, its blocks and hidden * paragraphs, its tile modes, its screens -- never its read's timing, which a new take changes), and every other entry * but its narration. */ const drawn = async () => { const { drawn: drawnOf } = (await import("../report-to-video/post-reading-pin.fixtures.mjs")) as unknown as { drawn: (e: unknown) => Record }; const m = JSON.parse(readFileSync(MANIFEST, "utf8")) as { timeline: Array> }; return m.timeline.map((e) => (e.type === "post" ? drawnOf(e) : sha(JSON.stringify(Object.fromEntries(Object.entries(e).filter(([k]) => k !== "narration")))))); }; const pristine = { takes: [] as string[], readings: [] as string[], parts: [] as string[] }; test.beforeAll(() => { if (!existsSync(`${MANIFEST}.pristine`)) copyFileSync(MANIFEST, `${MANIFEST}.pristine`); pristine.takes = files(path.join(DIR, "narration", "posts")); pristine.readings = files(path.join(DIR, "out", "narration")); pristine.parts = files(path.join(DIR, "narration", "parts-cache")); }); const reset = () => { copyFileSync(`${MANIFEST}.pristine`, MANIFEST); for (const d of ["reads", "lexicon.json"]) rmSync(path.join(DIR, "narration", d), { recursive: true, force: true }); rmSync(path.join(DIR, "revisions"), { recursive: true, force: true }); for (const f of files(path.join(DIR, "narration", "posts"))) if (!pristine.takes.includes(f) && f.endsWith(".wav")) rmSync(path.join(DIR, "narration", "posts", f), { force: true }); for (const f of files(path.join(DIR, "out", "narration"))) if (!pristine.readings.includes(f)) rmSync(path.join(DIR, "out", "narration", f), { force: true }); for (const f of files(path.join(DIR, "narration", "parts-cache"))) if (!pristine.parts.includes(f)) rmSync(path.join(DIR, "narration", "parts-cache", f), { force: true }); }; test.beforeEach(() => { test.setTimeout(240_000); reset(); }); test.afterAll(reset); test("Find, Replace these reads, Re-read the changed lines on the queue, Listen through, Pick -- what is shown never changes", async ({ page }) => { const cardsBefore = await drawn(); const takesBefore = files(path.join(DIR, "narration", "posts")); const asksBefore = ttsAsks().length; await page.goto(urls.readsHref(P)); const panel = page.locator(`[data-reads="${P}"]`); await expect(panel.locator("[data-reads-rows]")).toHaveAttribute("data-reads-rows", "8", { timeout: 30_000 }); // Every control, by its name. for (const name of ["Find in spoken text", "Match case", "Whole word", "Show every read", "Show the stale reads", "Character"]) await expect(panel.getByLabel(name, { exact: true }).or(panel.getByRole("button", { name, exact: true })).first()).toBeVisible(); await expect(panel.getByRole("button", { name: "Re-read 0 changed lines" })).toBeDisabled(); await expect(panel.locator('[data-read="p-3"]')).toHaveAttribute("data-read-state", "recorded"); // FIND: a count of matches and of lines -- the spoken text only. await panel.getByLabel("Find in spoken text").fill("north pier"); await expect(panel.locator("[data-reads-found]")).toHaveAttribute("data-reads-found", "6"); await expect(panel.locator("[data-reads-found]")).toContainText("6 matches in 5 lines ยท spoken text only"); await expect(panel.locator('[data-read="p-1"] [data-reads-match]')).toHaveCount(2); // REPLACE, these reads: previewed first, then applied -- nothing synthesized, the shown text untouched. await panel.getByLabel("Replace with").fill("northern pier"); await expect(panel.locator("[data-reads-say-limit]")).toContainText("Everywhere needs Whole word"); await panel.getByRole("button", { name: "Replace in these reads" }).click(); const preview = panel.locator('[data-reads-preview="reads"]'); await expect(preview).toBeVisible(); await expect(preview.locator("[data-reads-preview-line]")).toHaveCount(3); await expect(preview.locator('[data-reads-preview-line="p-1"]')).toContainText("northern pier"); await expect(preview.locator('[data-reads-preview-line="p-4/screen:s1:0"]')).toContainText("creates its said-as (the line as shown is untouched)"); await expect(preview.locator('[data-reads-preview-skipped="p-3"]')).toContainText("a recorded read is yours"); await expect(preview.locator('[data-reads-preview-skipped="p-4/parent:0"]')).toContainText("said as shown: only Everywhere changes this"); // This test re-reads the post and the card; the screen line is left for the queue test below (untick it). await preview.getByLabel("Rewrite p-4/screen:s1:0").uncheck(); expect(readFileSync(MANIFEST, "utf8")).toBe(readFileSync(`${MANIFEST}.pristine`, "utf8")); await panel.getByRole("button", { name: "Apply the replace" }).click(); await expect(panel.locator('[data-reads-undo="reads"]')).toBeVisible(); expect((JSON.parse(readFileSync(MANIFEST, "utf8")) as { timeline: Array<{ id: string; screens?: Array<{ saidAs?: unknown }> }> }).timeline.find((e) => e.id === "p-4")?.screens?.[0].saidAs).toBeUndefined(); await panel.getByLabel("Find in spoken text").fill(""); await expect(panel.locator('[data-read="p-1"]')).toHaveAttribute("data-read-state", "stale"); await expect(panel.locator('[data-read="c-1"]')).toHaveAttribute("data-read-state", "stale"); await expect(panel.locator('[data-read="c-1"]')).toContainText("its words changed with the replace"); await expect(panel.locator('[data-read="p-2"]')).toHaveAttribute("data-read-state", "fresh"); const m = JSON.parse(readFileSync(MANIFEST, "utf8")) as { timeline: Array<{ id: string; text?: string; narration?: { script?: string; forReading?: boolean; text?: string } }> }; expect(m.timeline[0].narration?.script).toBe(READS_P1.replaceAll("north pier", "northern pier")); expect(m.timeline[0].narration?.forReading).toBe(true); expect(m.timeline[0].text).toBe(READS_P1); expect(await drawn()).toEqual(cardsBefore); expect(files(path.join(DIR, "narration", "posts"))).toEqual(takesBefore); expect(ttsAsks().length).toBe(asksBefore); // RE-READ the changed lines: the app's queue runs them, one at a time, through the editor stub. await panel.getByRole("button", { name: "Re-read 2 changed lines" }).click(); await expect(panel.locator("[data-reads-queue]")).toBeVisible(); await expect(panel.locator('[data-reads-queue-row="p-1"]')).toHaveAttribute("data-state", "done", { timeout: 120_000 }); await expect(panel.locator('[data-reads-queue-row="c-1"]')).toHaveAttribute("data-state", "done", { timeout: 120_000 }); const asks = ttsAsks().slice(asksBefore); expect(asks.some((a) => /northern pier/.test(a.text ?? "") && a.voice === "tara")).toBe(true); expect(asks.some((a) => /closed today/.test(a.text ?? ""))).toBe(false); // The post's re-read joined its list UNPICKED; the card's words play. await expect(panel.locator('[data-read="p-1"]')).toHaveAttribute("data-read-state", "unpicked", { timeout: 20_000 }); await expect(panel.locator('[data-read="c-1"]')).toHaveAttribute("data-read-state", "fresh"); expect((JSON.parse(readFileSync(MANIFEST, "utf8")) as { timeline: Array<{ narration?: { pick?: number } }> }).timeline[0].narration?.pick).toBe(0); // LISTEN THROUGH: the re-reads played one after another (the new one of p-1 first), what played before a press away. await panel.getByRole("button", { name: "Listen through the re-read lines" }).click(); await expect(panel.locator('[data-reads-play-new="p-1"][data-playing="1"]')).toBeVisible({ timeout: 10_000 }); await expect(panel.getByRole("button", { name: "Play what p-1 said before" })).toBeEnabled(); await expect(panel.locator('[data-reads-play-cut="p-1"]')).toHaveAttribute("href", urls.playHref(P, { clip: "p-1" })); // PICK the new read: fresh; the cards still drawn as before. await panel.getByRole("button", { name: "Pick the new read of p-1" }).click(); await expect(panel.locator('[data-read="p-1"]')).toHaveAttribute("data-read-state", "fresh", { timeout: 20_000 }); expect((JSON.parse(readFileSync(MANIFEST, "utf8")) as { timeline: Array<{ narration?: { pick?: number } }> }).timeline[0].narration?.pick).toBe(1); expect(await drawn()).toEqual(cardsBefore); }); test("a thread read: a screen line's said-as made by a replace, its re-read a queue step through the stub, Stop and Retry, Listen through per part", async ({ page }) => { const cardsBefore = await drawn(); await page.goto(urls.readsHref(P)); const panel = page.locator(`[data-reads="${P}"]`); await expect(panel.locator("[data-reads-rows]")).toHaveAttribute("data-reads-rows", "8", { timeout: 30_000 }); await panel.getByLabel("Find in spoken text").fill("north pier"); await panel.getByLabel("Replace with").fill("northern pier"); await panel.getByRole("button", { name: "Replace in these reads" }).click(); await panel.getByRole("button", { name: "Apply the replace" }).click(); await expect(panel.locator('[data-reads-undo="reads"]')).toBeVisible(); const m = JSON.parse(readFileSync(MANIFEST, "utf8")) as { timeline: Array<{ id: string; screens?: Array<{ saidAs?: Record; lines: Array<{ text: string }> }>; parents?: Array<{ text: string }> }> }; const e4 = m.timeline.find((e) => e.id === "p-4")!; expect(e4.screens![0].saidAs).toEqual({ 0: "Is the northern pier open?" }); expect(e4.screens![0].lines[0].text).toBe("Is the north pier open?"); expect(e4.parents![0].text).toBe("They say the north pier will stay shut."); await panel.getByLabel("Find in spoken text").fill(""); await expect(panel.locator('[data-read="p-4/screen:s1:0"]')).toHaveAttribute("data-read-state", "stale"); await expect(panel.locator('[data-read="p-4/parent:0"]')).toHaveAttribute("data-read-state", "fresh"); // Three items to re-read, queued; Stop at once: what has not started is stopped. await panel.getByRole("button", { name: "Re-read 3 changed lines" }).click(); await panel.getByRole("button", { name: "Stop the re-reads not started" }).click(); await expect(panel.locator('[data-reads-queue-row][data-state="stopped"]').first()).toBeVisible({ timeout: 20_000 }); // Retry every stopped one; then all of them done. for (const read of ["p-1", "c-1", "p-4/screen:s1:0"]) { const row = panel.locator(`[data-reads-queue-row="${read}"]`); if ((await row.getAttribute("data-state")) === "stopped") await panel.getByRole("button", { name: `Retry ${read}` }).click(); } for (const read of ["p-1", "c-1", "p-4/screen:s1:0"]) await expect(panel.locator(`[data-reads-queue-row="${read}"]`)).toHaveAttribute("data-state", "done", { timeout: 120_000 }); // The screen line's re-read: a new take of the item, unpicked; its own part plays, not the joined read. await expect(panel.locator('[data-read="p-4/screen:s1:0"]')).toHaveAttribute("data-read-state", "unpicked", { timeout: 20_000 }); const asks = ttsAsks().filter((a) => /northern pier open/.test(a.text ?? "")); expect(asks.length).toBeGreaterThan(0); await panel.getByRole("button", { name: "Play the new read of p-4/screen:s1:0" }).click(); await expect(panel.locator('[data-reads-play-new="p-4/screen:s1:0"][data-playing="1"]')).toBeVisible({ timeout: 10_000 }); const reads = (await (await page.request.get(`/api/report/reads?project=${encodeURIComponent(P)}`)).json()) as { reads: Array<{ id: string; newTake: { files: string[]; takeFile: string } | null; take: { files: string[] } }> }; const line = reads.reads.find((r) => r.id === "p-4/screen:s1:0")!; expect(line.newTake!.files).toHaveLength(1); expect(line.newTake!.files[0]).toMatch(/^narration\/parts-cache\//); expect(line.newTake!.files[0]).not.toBe(line.take.files[0]); await panel.getByRole("button", { name: "Pick the new read of p-4/screen:s1:0" }).click(); await expect(panel.locator('[data-read="p-4/screen:s1:0"]')).toHaveAttribute("data-read-state", "fresh", { timeout: 20_000 }); expect(await drawn()).toEqual(cardsBefore); }); test("Everywhere, from now on: a pronunciation entry previewed, applied (the reads it makes stale), taken back", async ({ page }) => { await page.goto(urls.readsHref(P)); const panel = page.locator(`[data-reads="${P}"]`); await expect(panel.locator("[data-reads-rows]")).toHaveAttribute("data-reads-rows", "8", { timeout: 30_000 }); await panel.getByLabel("Find in spoken text").fill("pier"); await panel.getByLabel("Whole word").check(); await panel.getByLabel("Replace with").fill("peer"); await expect(panel.locator("[data-reads-say-limit]")).toContainText("never a card"); await panel.getByRole("button", { name: "Add a pronunciation entry" }).click(); const preview = panel.locator('[data-reads-preview="say"]'); await expect(preview).toContainText("in every read of a post, present and future"); await expect(preview.locator("[data-reads-preview-cards]")).toContainText("cards are not said through the lexicon"); await panel.getByRole("button", { name: "Apply the replace" }).click(); await panel.getByLabel("Find in spoken text").fill(""); await expect(panel.locator('[data-read="p-1"]')).toHaveAttribute("data-read-state", "stale"); await expect(panel.locator('[data-read="p-1"]')).toHaveAttribute("data-read-state", "stale"); await expect(panel.locator('[data-read="p-1"] [data-read-because]')).toHaveAttribute("data-read-because", "lexicon"); expect(JSON.parse(readFileSync(path.join(DIR, "narration", "lexicon.json"), "utf8"))).toEqual([{ word: "pier", say: "peer" }]); await panel.getByRole("button", { name: "Remove the pronunciation entry" }).click(); await expect(panel.locator('[data-read="p-1"]')).toHaveAttribute("data-read-state", "fresh", { timeout: 20_000 }); expect(JSON.parse(readFileSync(path.join(DIR, "narration", "lexicon.json"), "utf8"))).toEqual([]); }); test("MUST 1: a filtered view (one character's stale reads) re-reads ONLY its rows", async ({ page }) => { const asksBefore = ttsAsks().length; await page.goto(urls.readsHref(P)); const panel = page.locator(`[data-reads="${P}"]`); await expect(panel.locator("[data-reads-rows]")).toHaveAttribute("data-reads-rows", "8", { timeout: 30_000 }); await panel.getByLabel("Find in spoken text").fill("north pier"); await panel.getByLabel("Replace with").fill("northern pier"); await panel.getByRole("button", { name: "Replace in these reads" }).click(); await panel.getByRole("button", { name: "Apply the replace" }).click(); await expect(panel.locator('[data-reads-undo="reads"]')).toBeVisible(); // Three stale reads now: p-1 (the ferry's), p-4's screen line (said by the pier's account: no character) and the // card (no one's). The ferry's view counts one. await page.goto(urls.readsHref(P, { state: "stale", character: "ferry" })); await expect(panel.locator("[data-reads-rows]")).toHaveAttribute("data-reads-rows", "1", { timeout: 30_000 }); await expect(panel.locator('[data-read="c-1"]')).toHaveCount(0); await panel.getByRole("button", { name: "Re-read 1 changed line" }).click(); await expect(panel.locator("[data-reads-queue-row]")).toHaveCount(1, { timeout: 20_000 }); await expect(panel.locator('[data-reads-queue-row="p-1"]')).toHaveAttribute("data-state", "done", { timeout: 120_000 }); // The card and the screen line were never asked: still stale, never synthesized. const card = (await (await page.request.get(`/api/report/reads?project=${encodeURIComponent(P)}`)).json()) as { reads: Array<{ id: string; state: string }> }; expect(card.reads.find((r) => r.id === "c-1")?.state).toBe("stale"); expect(card.reads.find((r) => r.id === "p-4/screen:s1:0")?.state).toBe("stale"); expect(ttsAsks().slice(asksBefore).some((a) => /pier reopened/.test(a.text ?? ""))).toBe(false); }); test("N1: a replace from a filtered view rewrites ONLY the rows its Find counted there", async ({ page }) => { const before = JSON.parse(readFileSync(MANIFEST, "utf8")) as { timeline: Array<{ id: string; narration?: { text?: string } }> }; await page.goto(urls.readsHref(P, { character: "ferry" })); const panel = page.locator(`[data-reads="${P}"]`); await expect(panel.locator('[data-read="c-1"]')).toHaveCount(0, { timeout: 30_000 }); await panel.getByLabel("Find in spoken text").fill("north pier"); await expect(panel.locator("[data-reads-found]")).toContainText("2 matches in 1 line"); await panel.getByLabel("Replace with").fill("northern pier"); await panel.getByRole("button", { name: "Replace in these reads" }).click(); const preview = panel.locator('[data-reads-preview="reads"]'); await expect(preview.locator("[data-reads-preview-line]")).toHaveCount(1); await expect(preview.locator('[data-reads-preview-line="p-1"]')).toBeVisible(); await panel.getByRole("button", { name: "Apply the replace" }).click(); await expect(panel.locator('[data-reads-undo="reads"]')).toBeVisible(); const m = JSON.parse(readFileSync(MANIFEST, "utf8")) as { timeline: Array<{ id: string; narration?: { text?: string; script?: string }; screens?: Array<{ saidAs?: unknown }> }> }; expect(m.timeline.find((e) => e.id === "p-1")?.narration?.script).toContain("northern pier"); expect(m.timeline.find((e) => e.id === "c-1")?.narration?.text).toBe(before.timeline.find((e) => e.id === "c-1")?.narration?.text); expect(m.timeline.find((e) => e.id === "p-4")?.screens?.[0].saidAs).toBeUndefined(); }); test("the bench links here: the Voices page and a post's bench", async ({ page }) => { await page.goto(urls.voicesHref(P)); await expect(page.locator("[data-voices-reads]").first()).toHaveAttribute("href", urls.readsHref(P)); await page.goto(urls.postBenchHref(P, "p-1")); await expect(page.locator("[data-post-bench-reads]")).toHaveAttribute("href", urls.readsHref(P)); });