import { test, expect, type Page } from "@playwright/test"; import { execFileSync } from "node:child_process"; import { createHash } from "node:crypto"; import { copyFileSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import * as urls from "./urls"; // --------------------------------------------------------------------------- // POST ITEMS (report-to-video/post-item.mjs; knobs.md, "A post item"): a cut // that mixes a clip with posts played as items of their own. Fixture: // e2e/fixtures/post-items-fixture.mjs -- c01 (a clip), p-txt (words only, a // self-reply under its parent), p-img (a picture), p-vid (a 4 s video with a // tone, a reply to a private account) from the synthesized archive postchan-x. // // The rule's seconds (the pacing rule, 2 Nov: the item is its reading time -- // 1 s + words / 3, at least 3; a parent 1-4 s -- the attachments playing // beside the words, not after them): p-txt 12 words + a 7-word parent = 5 + // 2.33 = 7.33; p-img 5 words = 3 (its picture up the whole time); p-vid 7 // words + a 4-word parent = 3.33 + 1.33 = 4.67, its 4 s video from the first // frame, the last frame held. // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const DIR = path.join(FIXTURE, "reports", "post-items-fixture"); const MANIFEST = path.join(DIR, "video.manifest.json"); const PROJECT = "reports/post-items-fixture"; const SHOTS = process.env.POST_ITEMS_SHOTS ?? null; type Entry = { id: string; type: string; media?: { start?: number; end?: number; mute?: boolean }[]; hold?: number }; const timeline = () => (JSON.parse(readFileSync(MANIFEST, "utf8")) as { timeline: Entry[] }).timeline; const umtool = (args: string[]) => execFileSync("node", ["bin/umtool.mjs", ...args], { cwd: path.join(HERE, ".."), encoding: "utf8", env: { ...process.env, REPORTS_DIR: FIXTURE, CHANNELS_DIR: path.join(FIXTURE, "channels"), UMTOOL_CACHE_DIR: path.join(FIXTURE, "cache"), UMTOOL_MEDIA_DIR: "" }, }); test.beforeAll(() => { if (!existsSync(`${MANIFEST}.pristine`)) copyFileSync(MANIFEST, `${MANIFEST}.pristine`); }); test.beforeEach(() => { test.setTimeout(120_000); copyFileSync(`${MANIFEST}.pristine`, MANIFEST); rmSync(path.join(DIR, "revisions"), { recursive: true, force: true }); }); const root = (page: Page) => page.locator("[data-play-cut]"); test("the Cut tab lists the mixed cut: the clip, then post rows with their glyph, what they carry and how long they play", async ({ page }) => { await page.setViewportSize({ width: 1366, height: 1000 }); await page.goto(urls.projectHref(PROJECT, { tab: "cut" })); const rows = page.locator("[data-entry]"); await expect(rows).toHaveCount(4); expect(await rows.evaluateAll((els) => els.map((e) => `${e.getAttribute("data-entry")}:${e.getAttribute("data-kind")}`))).toEqual([ "c01:clip", "p-txt:post", "p-img:post", "p-vid:post", ]); await expect(page.locator('[data-entry="c01"] [data-post-glyph]')).toHaveCount(0); for (const id of ["p-txt", "p-img", "p-vid"]) await expect(page.locator(`[data-entry="${id}"] [data-post-glyph]`)).toBeVisible(); await expect(page.locator('[data-entry="p-txt"] [data-post-carries]')).toHaveText("text"); await expect(page.locator('[data-entry="p-img"] [data-post-carries]')).toHaveText("image"); await expect(page.locator('[data-entry="p-vid"] [data-post-carries]')).toHaveText("video"); await expect(page.locator('[data-entry="p-txt"] [data-post-seconds]')).toHaveText("7.3s"); await expect(page.locator('[data-entry="p-img"] [data-post-seconds]')).toHaveText("3.0s"); await expect(page.locator('[data-entry="p-vid"] [data-post-seconds]')).toHaveText("4.7s"); await expect(page.locator('[data-entry="p-vid"]')).toContainText("Yes, and here is the video."); // A post item takes a section's place like a clip: the bar after c01 opens on p-txt. await expect(page.locator('[data-entry="p-vid"] [data-post-bench-link]')).toHaveAttribute("href", urls.postBenchHref(PROJECT, "p-vid")); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "post-items-cut-tab.png"), fullPage: true }); }); test("Play plays a post item: its card, the private parent blurred, its video playing with the clock advancing", async ({ page }) => { await page.setViewportSize({ width: 1366, height: 1000 }); await page.goto(urls.playHref(PROJECT, { bare: true, clip: "p-vid" })); await expect(root(page)).toHaveAttribute("data-play-state", "paused"); await expect(root(page)).toHaveAttribute("data-play-item", "p-vid"); const player = page.locator('[data-post-play="p-vid"]'); await expect(player).toBeVisible(); await expect(player).toHaveAttribute("data-post-layout", "side"); // One layout: the video beside the card from the item's first frame (not after a reading beat). await expect(player).toHaveAttribute("data-post-step", "video"); // The card is the build's page: the post's words, its counts, the parent above it blurred. const card = page.frameLocator('[data-post-card-frame="read"]'); await expect(card.locator('[data-post-card="p-vid"]')).toBeVisible(); await expect(card.locator("[data-post-words]")).toContainText("Yes, and here is the video."); await expect(card.locator('[data-post-parent="0"][data-redacted]')).toHaveCount(1); await expect(card.locator("body")).not.toContainText("someone_private"); await expect(card.locator("body")).not.toContainText("Private Person"); // Paused: its first frame is up beside the card. await expect(page.locator('[data-post-video="0"]')).toHaveAttribute("data-post-video-playing", "0"); const t0 = Number(await root(page).getAttribute("data-play-t")); await page.locator("[data-play-toggle]").click(); await expect(page.locator('[data-post-video="0"]')).toHaveAttribute("data-post-video-playing", "1", { timeout: 15_000 }); await expect.poll(async () => page.locator('[data-post-video="0"]').evaluate((v: HTMLVideoElement) => v.currentTime), { timeout: 10_000 }).toBeGreaterThan(0.5); expect(await page.locator('[data-post-video="0"]').evaluate((v: HTMLVideoElement) => v.muted)).toBe(false); const t1 = Number(await root(page).getAttribute("data-play-t")); expect(t1).toBeGreaterThan(t0); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "post-items-play.png") }); await page.locator("[data-play-toggle]").click(); // Edit this post opens its bench, and Back to the cut returns to it. await page.locator("[data-play-edit]").click(); await expect(page.locator('[data-post-bench="p-vid"]')).toBeVisible(); await page.locator("[data-post-bench-back]").click(); await expect(root(page)).toHaveAttribute("data-play-item", "p-vid"); }); test("a redacted term: Play shows the words; As published draws blank bars and the term is nowhere in the card's DOM", async ({ page }) => { // A made-up term on the fixture's words (post-redact.mjs; knobs.md, "Redacted words"). umtool(["posts", "redact", PROJECT, "add", "here is"]); expect(umtool(["posts", "redact", PROJECT, "ls"])).toContain("1. here is"); await page.setViewportSize({ width: 1366, height: 1000 }); await page.goto(urls.playHref(PROJECT, { bare: true, clip: "p-vid" })); await page.evaluate(() => window.localStorage.removeItem("umtool.play.asPublished")); await page.reload(); await expect(root(page)).toHaveAttribute("data-play-item", "p-vid"); const card = page.frameLocator('[data-post-card-frame="read"]'); // umtool's own view: the words as posted. await expect(card.locator("[data-post-words]")).toContainText("Yes, and here is the video."); await expect(card.locator("[data-post-blanked]")).toHaveCount(0); await page.locator("[data-play-as-published]").check(); // A bar per word of the phrase, each in its own word's slot (a word zoom's indices hold). await expect(card.locator("[data-post-blanked]")).toHaveCount(2); await expect(card.locator("[data-post-words]")).toContainText("Yes, and"); await expect(card.locator("[data-post-words]")).toContainText("the video."); const dom = await card.locator("html").evaluate((el) => el.outerHTML); expect(dom.toLowerCase()).not.toContain("here is"); expect(await card.locator("[data-post-blanked]").evaluateAll((els) => els.map((el) => el.textContent).join(""))).toBe(""); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "post-items-as-published.png") }); // The snapshot is never rewritten. expect(JSON.stringify(timeline())).toContain("here is the video"); }); test("the slur filter: As published (censored) draws the placeholder pill and never the word; Uncensored draws the word; the redaction stays on in both", async ({ page }) => { // A made-up word put on the filter (umtool posts slurs add; knobs.md, "The slur filter"), drawn as the default placeholder pill. umtool(["posts", "slurs", PROJECT, "add", "video"]); umtool(["posts", "redact", PROJECT, "add", "here is"]); const ls = umtool(["posts", "slurs", PROJECT, "ls"]); expect(ls).toContain("the slur filter is ON"); expect(ls).toMatch(/\*{5} +\(its own\) +×1/); expect(ls).not.toContain("video"); await page.setViewportSize({ width: 1366, height: 1000 }); await page.goto(urls.playHref(PROJECT, { bare: true, clip: "p-vid" })); await page.evaluate(() => { window.localStorage.removeItem("umtool.play.asPublished"); window.localStorage.removeItem("umtool.play.uncensored"); }); await page.reload(); await expect(root(page)).toHaveAttribute("data-play-item", "p-vid"); const card = page.frameLocator('[data-post-card-frame="read"]'); // umtool's own view: the words as posted; no Uncensored toggle until As published is on. await expect(card.locator("[data-post-words]")).toContainText("the video."); await expect(page.locator("[data-play-uncensored]")).toHaveCount(0); await page.locator("[data-play-as-published]").check(); await expect(page.locator("label:has([data-play-as-published])")).toContainText("As published (censored)"); // CENSORED: the word a dark pill holding its mask; the redacted phrase still bars. await expect(card.locator("[data-post-slur]")).toHaveCount(1); await expect(card.locator("[data-post-slur]")).toHaveText("*****"); await expect(card.locator("[data-post-blanked]")).toHaveCount(2); const dom = await card.locator("html").evaluate((el) => el.outerHTML); expect(dom.toLowerCase()).not.toContain("video."); expect(dom.toLowerCase()).not.toContain("here is"); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "post-items-slurs-censored.png") }); // An EDGE mask (posts slurs edges; knobs.md): the same pill, its first letter, stars and its last letter. umtool(["posts", "slurs", PROJECT, "edges", "video"]); expect(umtool(["posts", "slurs", PROJECT, "ls"])).toContain("edges: v***o"); await page.reload(); await expect(root(page)).toHaveAttribute("data-play-item", "p-vid"); await expect(page.locator("[data-play-as-published]")).toBeChecked(); await expect(card.locator("[data-post-slur]")).toHaveText("v***o"); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "post-items-slurs-edge.png") }); umtool(["posts", "slurs", PROJECT, "edges", "video", "--off"]); await page.reload(); await expect(page.locator("[data-play-as-published]")).toBeChecked(); await expect(card.locator("[data-post-slur]")).toHaveText("*****"); // UNCENSORED: the word as posted; the redaction is not part of the switch. await page.locator("[data-play-uncensored]").check(); await expect(page.locator("label:has([data-play-as-published])")).toContainText("As published (uncensored)"); await expect(card.locator("[data-post-slur]")).toHaveCount(0); await expect(card.locator("[data-post-words]")).toContainText("the video."); await expect(card.locator("[data-post-blanked]")).toHaveCount(2); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "post-items-slurs-uncensored.png") }); // The snapshot is never rewritten. expect(JSON.stringify(timeline())).toContain("here is the video"); }); test("a censored attachment: set-item blurs it, Play previews the blur and its label, the bench toggles it off and saves", async ({ page }) => { umtool(["posts", "set-item", PROJECT, "p-vid", "media.0.blur=on"]); await page.setViewportSize({ width: 1366, height: 1000 }); await page.goto(urls.playHref(PROJECT, { bare: true, clip: "p-vid" })); await expect(page.locator('[data-post-media="0"]')).toHaveAttribute("data-post-blurred", "frame"); await expect(page.locator('[data-post-media="0"] [data-post-blur-label]')).toHaveText("Censored"); expect(await page.locator('[data-post-video="0"]').evaluate((v) => getComputedStyle(v).filter)).toMatch(/blur\(/); await page.goto(urls.postBenchHref(PROJECT, "p-vid")); await expect(page.locator('[data-post-bench-blur="0"]')).toBeChecked(); await expect(page.locator('[data-post-bench-blur-pad="0"]')).toBeVisible(); await page.locator('[data-post-bench-blur="0"]').uncheck(); await page.locator("[data-post-bench-save]").click(); await expect(page.locator("[data-post-bench-status]")).toBeVisible(); const vid = timeline().find((e) => e.id === "p-vid") as Entry & { media: Array<{ blur?: { on: boolean }; src: string }> }; expect(vid.media[0].blur?.on).toBe(false); expect(vid.media[0].src).toMatch(/\.mp4$/); }); test("the post bench trims the video attachment and saves it; the cut's seconds follow", async ({ page }) => { await page.setViewportSize({ width: 1366, height: 1000 }); await page.goto(urls.postBenchHref(PROJECT, "p-vid")); await expect(page.locator('[data-post-bench="p-vid"]')).toBeVisible(); await expect(page.locator("[data-post-bench-rule]")).toHaveText("4.7 s"); await page.locator('[data-post-bench-start="0"]').fill("1"); await page.locator('[data-post-bench-end="0"]').fill("3"); // The item is its read: a shorter clip holds its last frame, the length stays. await expect(page.locator("[data-post-bench-rule]")).toHaveText("4.7 s"); await expect(page.locator('[data-post-bench-step="video"]')).toContainText("video 2.0 s"); await page.locator('[data-post-bench-sound="0"]').uncheck(); await page.locator("[data-post-bench-save]").click(); await expect(page.locator("[data-post-bench-status]")).toHaveText("saved"); const v = timeline().find((e) => e.id === "p-vid")!; expect(v.media?.[0]).toMatchObject({ start: 1, end: 3, mute: true }); // A trim that leaves less than half a second is refused in the build's words, and nothing is written. await page.locator('[data-post-bench-end="0"]').fill("1.2"); await page.locator("[data-post-bench-save]").click(); await expect(page.locator("[data-post-bench-error]")).toContainText("at least 0.5 s after start"); expect(timeline().find((e) => e.id === "p-vid")!.media?.[0]?.end).toBe(3); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "post-items-bench.png") }); await page.goto(urls.projectHref(PROJECT, { tab: "cut" })); await expect(page.locator('[data-entry="p-vid"] [data-post-seconds]')).toHaveText("4.7s"); }); test("the post bench draws the attachment's lane: its out edge dragged into the DRAFT (an old take-only address is the draft's bench now), the field and the plan following; a GIF's lane has no sound and still drags", async ({ page }) => { // The take-only scope is retired (L3: every edit lands in the draft): an old `?inTake=` address is the draft's bench. const takes = path.join(DIR, "takes"); rmSync(takes, { recursive: true, force: true }); const projectText = readFileSync(MANIFEST, "utf8"); mkdirSync(path.join(takes, "t1"), { recursive: true }); writeFileSync(path.join(takes, "t1", "take.json"), JSON.stringify({ id: "t1", group: "takes", order: 1, label: "Take one", kind: "different", summary: "", changes: [], preview: "preview.mp4", seconds: null, builtAt: null, style: null, patch: null, window: null, draft: true, draftedAt: "2026-11-05T00:00:00.000Z", baseHash: createHash("sha256").update(projectText).digest("hex"), }, null, 2) + "\n"); const takeText = readFileSync(path.join(takes, "t1", "take.json"), "utf8"); try { await page.setViewportSize({ width: 1366, height: 1000 }); await page.goto(`${urls.postBenchHref(PROJECT, "p-vid")}?inTake=t1`); await expect(page.locator('[data-post-bench-scope="cut"]')).toContainText("the draft"); const lane = page.locator('[data-post-lane="attachment"][data-post-lane-media="0"]'); await expect(lane).toBeVisible(); await expect(lane).toHaveAttribute("data-post-lane-kind", "video"); const start0 = (JSON.parse(projectText).timeline as Entry[]).find((e) => e.id === "p-vid")?.media?.[0]; await expect(lane).toHaveAttribute("data-post-lane-sel", `${Number(start0?.start ?? 0).toFixed(2)}-${Number(start0?.end ?? 4).toFixed(2)}`); // Its sound's envelope arrives (the peaks route over the project file). await expect(lane).toHaveAttribute("data-post-lane-peaks", "on"); // Drag the out handle from the file's end to half way: 2 s. await lane.scrollIntoViewIfNeeded(); const box = (await lane.locator('[data-testid="waveform"]').boundingBox())!; const y = box.y + box.height / 2; await page.mouse.move(box.x + box.width - 2, y); await page.mouse.down(); await page.mouse.move(box.x + box.width * 0.75, y, { steps: 4 }); await page.mouse.move(box.x + box.width * 0.5, y, { steps: 4 }); await page.mouse.up(); const end = async () => Number(await page.locator('[data-post-bench-end="0"]').inputValue()); await expect.poll(end).toBeGreaterThan(1.9); expect(await end()).toBeLessThan(2.1); const dragged = await end(); // The plan's video step plays what the trim leaves. await expect(page.locator('[data-post-bench-step="video"]')).toContainText(`video ${dragged.toFixed(1)} s`); // Focused, → moves the edge last moved by 0.1 s. await lane.focus(); await page.keyboard.press("ArrowRight"); await expect.poll(end).toBeCloseTo(dragged + 0.1, 2); await expect(lane).toHaveAttribute("data-post-lane-sel", `0.00-${(dragged + 0.1).toFixed(2)}`); if (SHOTS) { await lane.evaluate((el) => el.scrollIntoView({ block: "center" })); await page.screenshot({ path: path.join(SHOTS, "bench.png") }); } await page.locator("[data-post-bench-save]").click(); await expect(page.locator("[data-post-bench-status]")).toHaveText("saved"); const vid = (JSON.parse(readFileSync(MANIFEST, "utf8")).timeline as Entry[]).find((e) => e.id === "p-vid"); expect(vid?.media?.[0]?.end).toBeCloseTo(dragged + 0.1, 2); // The take byte for byte the same; the draft put back for the specs after. expect(readFileSync(path.join(takes, "t1", "take.json"), "utf8")).toBe(takeText); } finally { writeFileSync(MANIFEST, projectText); rmSync(takes, { recursive: true, force: true }); } // A GIF (the combo fixture's p-c1, 2 s, no sound): the lane draws a flat line and its in handle still drags. await page.goto(urls.postBenchHref("reports/post-combo-fixture", "p-c1")); const gif = page.locator('[data-post-lane="attachment"][data-post-lane-media="0"]'); await expect(gif).toHaveAttribute("data-post-lane-kind", "gif"); await expect(gif).toHaveAttribute("data-post-lane-peaks", "none"); await gif.scrollIntoViewIfNeeded(); const gb = (await gif.locator('[data-testid="waveform"]').boundingBox())!; await page.mouse.move(gb.x + 2, gb.y + gb.height / 2); await page.mouse.down(); await page.mouse.move(gb.x + gb.width * 0.25, gb.y + gb.height / 2, { steps: 4 }); await page.mouse.up(); await expect.poll(async () => Number(await page.locator('[data-post-bench-start="0"]').inputValue())).toBeCloseTo(0.5, 1); }); test("the post bench paraphrases a context post: the card draws it labelled, the read counts it, the original kept", async ({ page }) => { await page.setViewportSize({ width: 1366, height: 1000 }); await page.goto(urls.postBenchHref(PROJECT, "p-txt")); await expect(page.locator('[data-post-bench="p-txt"]')).toBeVisible(); // 12 words (5 s) + its parent's 7 (2.33 s). await expect(page.locator("[data-post-bench-rule]")).toHaveText("7.3 s"); const block = page.locator('[data-post-bench-context="parent.0"]'); await expect(block.locator('[data-post-bench-original="parent.0"]')).toHaveText("The first thing I said about it."); await block.locator('[data-post-bench-paraphrase="parent.0"]').fill("He said it first."); // The read counts the paraphrase's 4 words (1.33 s), at once. await expect(page.locator("[data-post-bench-rule]")).toHaveText("6.3 s"); await page.locator("[data-post-bench-save]").click(); await expect(page.locator("[data-post-bench-status]")).toHaveText("saved"); const saved = timeline().find((e) => e.id === "p-txt") as Entry & { parents: { text: string; paraphrase?: string }[] }; expect(saved.parents[0]).toMatchObject({ text: "The first thing I said about it.", paraphrase: "He said it first." }); // The card (the build's page, drawn again after the save): the paraphrase, labelled; never the original words. const card = page.frameLocator('[data-post-card-frame="read"]'); await expect(card.locator('[data-post-parent="0"] [data-post-paraphrase]')).toHaveText("He said it first."); await expect(card.locator('[data-post-parent="0"] [data-post-paraphrased]')).toHaveText(/paraphrased/i); await expect(card.locator("body")).not.toContainText("The first thing I said about it."); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "post-items-paraphrase.png") }); // Cleared (the terminal's writer): its own words again, no label. umtool(["posts", "set-item", PROJECT, "p-txt", "parent.0.paraphrase=off"]); expect((timeline().find((e) => e.id === "p-txt") as Entry & { parents: Record[] }).parents[0].paraphrase).toBeUndefined(); await page.reload(); await expect(page.frameLocator('[data-post-card-frame="read"]').locator('[data-post-parent="0"]')).toContainText("The first thing I said about it."); await expect(page.frameLocator('[data-post-card-frame="read"]').locator("[data-post-paraphrased]")).toHaveCount(0); await expect(page.locator("[data-post-bench-rule]")).toHaveText("7.3 s"); }); test("umtool posts reel adds only what the cut does not play yet, in one write", async () => { const dry = JSON.parse(umtool(["posts", "reel", PROJECT, "--channel", "postchan-x", "--own", "--dry-run", "--json"])) as { entries: { id: string }[]; already: number }; // 0 is p-txt's parent (not an item yet), 1-3 are in the cut, 4 is new; 9 is not the subject's. expect(dry.entries.map((e) => e.id)).toEqual(["p-0", "p-4"]); expect(dry.already).toBe(3); umtool(["posts", "reel", PROJECT, "--channel", "postchan-x", "--own", "--since", "2026-10-31"]); expect(timeline().map((e) => e.id)).toEqual(["c01", "p-txt", "p-img", "p-vid", "p-4"]); const again = JSON.parse(umtool(["posts", "reel", PROJECT, "--channel", "postchan-x", "--own", "--since", "2026-10-31", "--json"])) as { added: number }; expect(again.added).toBe(0); });