import { test, expect as baseExpect, type Page } from "@playwright/test"; import { existsSync, readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import * as urls from "./urls"; // A write is a request under the app's lock, and this machine runs other suites beside this one: room for it. const expect = baseExpect.configure({ timeout: 15_000 }); // --------------------------------------------------------------------------- // THE POST ITEM PANEL (umtool redesign L6, item 4; components/projects/inspector/PostItemInspector.tsx): a post // row's Edit on the Cut tab -- the EXCERPT picked by selecting the post's words (snapped to words, settled where // the build finds each span; was `posts set-item excerpt=…`), its MEDIA on/off and a video's sound, and "Use my // recording" (a sound file kept as a recorded take and picked; was `posts narrate --recording`). Every write // lands in the draft; no take touched. inspector-post-fixture (WRITES): p-5 (a long text post), p-3 (a video with // sound, and a picture of the post it does not carry). // --------------------------------------------------------------------------- test.describe.configure({ mode: "serial", timeout: 120_000 }); const P = "reports/inspector-post-fixture"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const DIR = path.join(HERE, "..", ".e2e-song", "reports", "inspector-post-fixture"); type Post = Record & { id: string; excerpt?: string[]; media?: Record[]; narration?: { takes?: { file: string; engine?: string }[]; pick?: number } }; const item = (id: string) => (JSON.parse(readFileSync(path.join(DIR, "video.manifest.json"), "utf8")) as { timeline: Post[] }).timeline.find((e) => e.id === id) as Post; const takeText = () => readFileSync(path.join(DIR, "takes", "short", "take.json"), "utf8"); /** Select the `nth` occurrence of `needle` in the element's text, as a person dragging over it would. */ async function selectText(page: Page, selector: string, needle: string, nth = 0) { await page.evaluate( ({ selector, needle, nth }) => { const root = document.querySelector(selector) as HTMLElement; const full = root.textContent ?? ""; let at = -1; for (let i = 0; i <= nth; i++) at = full.indexOf(needle, at + 1); if (at < 0) throw new Error(`no "${needle}" in the post's text`); const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT); let pos = 0; let sN: Node | null = null; let sO = 0; let eN: Node | null = null; let eO = 0; for (let n = walker.nextNode(); n; n = walker.nextNode()) { const len = (n.textContent ?? "").length; if (!sN && at < pos + len) { sN = n; sO = at - pos; } if (sN && at + needle.length <= pos + len) { eN = n; eO = at + needle.length - pos; break; } pos += len; } const r = document.createRange(); r.setStart(sN as Node, sO); r.setEnd(eN as Node, eO); const sel = window.getSelection() as Selection; sel.removeAllRanges(); sel.addRange(r); }, { selector, needle, nth }, ); } test("the excerpt, picked by selecting the post's words: a span whose words come earlier too is widened to where it was picked", async ({ page }) => { await page.setViewportSize({ width: 1366, height: 768 }); const take = takeText(); await page.goto(urls.projectHref(P, { tab: "cut" })); const row = page.locator('[data-entry="p-5"]'); await row.locator("[data-post-edit]").click(); const panel = row.locator('[data-inspector="post-item"]'); await expect(panel.locator("[data-post-text]")).toContainText("The scale said 291 this morning."); // "scale said" is said twice: the second one, picked, is read as "the scale said" so the build finds it there. await selectText(page, '[data-entry="p-5"] [data-post-text]', "scale said", 1); await panel.locator("[data-excerpt-add]").click(); await selectText(page, '[data-entry="p-5"] [data-post-text]', "obody believ"); await panel.locator("[data-excerpt-add]").click(); await expect(panel.locator("[data-excerpt-span]")).toHaveCount(2); await expect(panel.locator('[data-excerpt-span="0"]')).toContainText("“the scale said”"); await expect(panel.locator('[data-excerpt-span="1"]')).toContainText("“Nobody believes”"); await page.screenshot({ path: test.info().outputPath("post-excerpt.png") }); await panel.locator("[data-excerpt-save]").click(); await expect(panel.locator('[data-post-said="ok"]')).toContainText("Excerpt saved to the draft: 2 spans"); expect(item("p-5").excerpt).toEqual(["the scale said", "Nobody believes"]); await expect(panel.locator("[data-excerpt-piece]")).toHaveCount(2); // Removing a span and saving keeps the other; Clear and save reads it whole. await panel.locator('[data-excerpt-span-remove="1"]').click(); await panel.locator("[data-excerpt-save]").click(); await expect(panel.locator('[data-post-said="ok"]')).toContainText("1 span"); expect(item("p-5").excerpt).toEqual(["the scale said"]); expect(takeText()).toBe(take); }); test("media on/off and a video's sound: off keeps the attachment whole (its crop), on puts it back as it was", async ({ page, request }) => { await page.setViewportSize({ width: 1366, height: 768 }); const take = takeText(); // A crop set on the bench first: the knob off->on must not lose. let r = await request.post("/api/report/post-item", { data: { project: P, op: "set", id: "p-3", patch: { media: { 0: { crop: [0.1, 0.1, 0.8, 0.8] } } } } }); expect(r.ok(), await r.text()).toBe(true); await page.goto(urls.projectHref(P, { tab: "cut" })); const row = page.locator('[data-entry="p-3"]'); await row.locator("[data-post-edit]").click(); const panel = row.locator('[data-inspector="post-item"]'); await expect(panel.locator('[data-post-media="0"]')).toContainText("3_1.mp4"); await expect(panel.locator('[data-post-file="posts-media/postchan-x/3/3_2.png"]')).toBeVisible(); // Each box is the draft's state: a click asks for the change, the box shows it once it is written. await panel.locator('[data-post-media="0"] [data-post-media-sound]').click(); await expect(panel.locator('[data-post-said="ok"]')).toContainText("Sound off."); expect(item("p-3").media?.[0]).toMatchObject({ src: "posts-media/postchan-x/3/3_1.mp4", mute: true, crop: [0.1, 0.1, 0.8, 0.8] }); await expect(panel.locator('[data-post-media="0"] [data-post-media-sound]')).not.toBeChecked(); // The picture of the post it never carried, put on. await panel.locator('[data-post-file="posts-media/postchan-x/3/3_2.png"] [data-post-file-shown]').click(); await expect(panel.locator('[data-post-said="ok"]')).toContainText("3_2.png: on the item."); expect(item("p-3").media?.map((m) => [m.src, m.kind])).toEqual([["posts-media/postchan-x/3/3_1.mp4", "video"], ["posts-media/postchan-x/3/3_2.png", "image"]]); // A file the item carries already is not added twice. r = await request.post("/api/report/post-item/inspector", { data: { project: P, op: "media-on", id: "p-3", file: "posts-media/postchan-x/3/3_2.png" } }); expect(r.status()).toBe(400); expect((await r.json()).error).toContain("carries posts-media/postchan-x/3/3_2.png already"); // The video OFF: out of what plays, kept whole in mediaOff -- its crop, its sound off. await panel.locator('[data-post-media="0"] [data-post-media-shown]').click(); await expect(panel.locator('[data-post-said="ok"]')).toContainText("off (kept as it was set"); expect(item("p-3").media?.map((m) => m.src)).toEqual(["posts-media/postchan-x/3/3_2.png"]); expect((item("p-3") as Post & { mediaOff?: Record[] }).mediaOff).toEqual([ { src: "posts-media/postchan-x/3/3_1.mp4", kind: "video", seconds: 4, width: 320, height: 180, audio: true, crop: [0.1, 0.1, 0.8, 0.8], mute: true }, ]); await expect(panel.locator('[data-post-media-off="0"]')).toContainText("3_1.mp4"); await expect(panel.locator('[data-post-file="posts-media/postchan-x/3/3_1.mp4"]')).toHaveCount(0); // ON again: back as it was, crop and all. await panel.locator('[data-post-media-off="0"] [data-post-media-off-shown]').click(); await expect(panel.locator('[data-post-said="ok"]')).toContainText("on again, as it was"); expect(item("p-3").media?.[1]).toEqual({ src: "posts-media/postchan-x/3/3_1.mp4", kind: "video", seconds: 4, width: 320, height: 180, audio: true, crop: [0.1, 0.1, 0.8, 0.8], mute: true }); expect("mediaOff" in item("p-3")).toBe(false); expect(existsSync(path.join(DIR, "posts-media", "postchan-x", "3", "3_1.mp4"))).toBe(true); expect(takeText()).toBe(take); }); test("Use my recording: a sound file kept as a recorded take of the item and picked", async ({ page }) => { await page.setViewportSize({ width: 1366, height: 768 }); const take = takeText(); expect(item("p-5").narration).toBeUndefined(); await page.goto(urls.projectHref(P, { tab: "cut" })); const row = page.locator('[data-entry="p-5"]'); await row.locator("[data-post-edit]").click(); const panel = row.locator('[data-inspector="post-item"]'); await expect(panel.locator("[data-post-takes]")).toContainText("not narrated yet"); await panel.locator("[data-post-recording-file]").setInputFiles(path.join(HERE, "..", ".e2e-song", "inspector-recording.wav")); await panel.locator("[data-post-recording-use]").click(); await expect(panel.locator('[data-post-said="ok"]')).toContainText("Kept as", { timeout: 30_000 }); const n = item("p-5").narration; expect(n?.takes).toHaveLength(1); expect(n?.takes?.[0].file).toMatch(/take-1\.recorded-operator\.wav$/); expect(existsSync(path.join(DIR, ...String(n?.takes?.[0].file).split("/")))).toBe(true); await expect(panel.locator("[data-post-takes]")).toContainText("1 take; plays take-1.recorded-operator.wav"); await page.screenshot({ path: test.info().outputPath("post-recording.png") }); expect(takeText()).toBe(take); });