import { test, expect, type Page } from "@playwright/test"; import { copyFileSync, existsSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import * as urls from "./urls"; // --------------------------------------------------------------------------- // A POST ITEM's CAPTION and CLOCK (report-to-video/post-caption.mjs, // post-clock.mjs; knobs.md, "A caption", "The clock"). Fixture: // e2e/fixtures/post-narration-fixture.mjs -- p-11 "The video speaks for // itself." narrated by a 2 s take (no word timings: the reading estimate), // p-12 four pictures, p-13 words only; all dated 30 Oct 10:00 UTC. // // POST_CAPTIONS_SHOTS= screenshots there // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const DIR = path.join(HERE, "..", ".e2e-song", "reports", "post-narration-fixture"); const MANIFEST = path.join(DIR, "video.manifest.json"); const PROJECT = "reports/post-narration-fixture"; const SHOTS = process.env.POST_CAPTIONS_SHOTS ?? null; type Entry = { id: string; date?: string; fx?: Array> }; const manifest = () => JSON.parse(readFileSync(MANIFEST, "utf8")) as { timeline: Entry[]; render: Record }; const timeline = () => manifest().timeline; 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 }); rmSync(path.join(DIR, "notes.json"), { force: true }); }); type Box = { x: number; y: number; width: number; height: number }; const overlap = (a: Box, b: Box) => Math.max(0, Math.min(a.x + a.width, b.x + b.width) - Math.max(a.x, b.x)) * Math.max(0, Math.min(a.y + a.height, b.y + b.height) - Math.max(a.y, b.y)); /** In the card frame: the box of `sel` and of every card word. */ const boxesIn = (page: Page, sel: string) => page.frameLocator('[data-post-card-frame="read"]').locator("body").evaluate((body, s) => { const r = (el: Element) => { const b = el.getBoundingClientRect(); return { x: b.left, y: b.top, width: b.width, height: b.height }; }; const el = body.ownerDocument.querySelector(s); return { el: el ? r(el) : null, words: Array.from(body.ownerDocument.querySelectorAll("[data-w]")).map((w) => ({ id: w.getAttribute("data-w"), ...r(w) })) }; }, sel); test("A CAPTION (operator, 4 Nov: 不動心 on \"Fudōshin\"): click a word -> type the text -> Caption here -> saved, timed by the reading estimate; Play shows the chip (CJK drawn) by the phrase as it is read, over no word read while it is up", async ({ page }) => { await page.setViewportSize({ width: 1500, height: 1100 }); await page.goto(urls.postBenchHref(PROJECT, "p-11")); const panel = page.locator('[data-post-zoom-panel="p-11"]'); await panel.locator('[data-post-zoom-word="2"]').click(); await expect(panel.locator("[data-post-caption-here]")).toBeDisabled(); await panel.locator("[data-post-caption-text]").fill("不動心 · speaks — a test of the chip"); await panel.locator("[data-post-caption-here]").click(); await expect(panel.locator('[data-post-caption-fx="0"]')).toBeVisible(); expect(timeline().find((e) => e.id === "p-11")!.fx).toEqual([{ kind: "caption", match: "speaks", text: "不動心 · speaks — a test of the chip" }]); // Its time: no word timings, so the read spread over its script by characters (0.4 + 10/29 of 2 s). const at = Number(await panel.locator("[data-post-caption-at]").getAttribute("data-post-caption-at")); expect(at).toBeGreaterThan(0.9); expect(at).toBeLessThan(1.3); await expect(panel.locator('[data-post-caption-fx="0"]')).toContainText("the reading estimate"); await expect(page.locator('[data-post-bench-caption-mark="0"]')).toBeVisible(); // A knob: place above, one write. await panel.locator('[data-post-caption-knob="place"]').selectOption("above"); await expect.poll(() => timeline().find((e) => e.id === "p-11")!.fx?.[0]?.place).toBe("above"); // The bench's own player mounts the chip. await expect(page.locator('[data-post-play="p-11"]')).toHaveAttribute("data-post-captions", "1", { timeout: 15_000 }); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "post-caption-bench.png") }); // Play the cut: up from its lead-in, the chip in the card page, by the phrase. await page.goto(urls.playHref(PROJECT, { bare: true, clip: "p-11" })); const player = page.locator('[data-post-play="p-11"]'); await expect(player).toHaveAttribute("data-post-captions", "1", { timeout: 15_000 }); await page.locator("[data-play-toggle]").click(); await expect(player).toHaveAttribute("data-post-caption-fx", "0", { timeout: 15_000 }); await page.locator("[data-play-toggle]").click(); const chip = page.frameLocator('[data-post-card-frame="read"]').locator('[data-post-fx-caption="0"]'); await expect(chip).toContainText("不動心"); await expect.poll(() => chip.evaluate((el) => Number((el as HTMLElement).style.opacity))).toBeGreaterThan(0); // CJK is drawn by a face this machine has, not boxes. expect(await chip.evaluate((el) => el.ownerDocument.fonts.check("27px 'Noto Sans CJK JP'", "不動心"))).toBe(true); // Never over the words read while it is up ("for itself." after "speaks"); never in the card's words. const { el, words } = await boxesIn(page, '[data-post-fx-caption="0"]'); expect(el).not.toBeNull(); for (const w of words.filter((x) => x.id === "post:3" || x.id === "post:4")) expect(overlap(el!, w)).toBe(0); expect(await chip.evaluate((el) => el.closest("[data-post-card]") === null && !el.querySelector("[data-w], [data-post-block]"))).toBe(true); // Its underline under the phrase. await expect(page.frameLocator('[data-post-card-frame="read"]').locator('[data-post-fx-caption-mark="0"]')).toHaveCount(1); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "post-caption-play.png") }); }); test("THE CLOCK: each item's own time on its card, run FORWARD from the clock item before (the gap flashing) and REWOUND when the next is earlier; Play draws it over no word", async ({ page }) => { const m = manifest(); const set = (id: string, o: Record) => Object.assign(m.timeline.find((e) => e.id === id)!, o); set("p-11", { fx: [{ kind: "clock" }] }); set("p-12", { date: "2026-10-30T10:38:00.000Z", fx: [{ kind: "clock" }] }); set("p-13", { date: "2026-10-28T09:00:00.000Z", fx: [{ kind: "clock" }] }); writeFileSync(MANIFEST, JSON.stringify(m, null, 2) + "\n"); await page.setViewportSize({ width: 1500, height: 1000 }); // Forward: p-12 after p-11, 38 minutes on (America/New_York: 6:00 AM -> 6:38 AM). await page.goto(urls.playHref(PROJECT, { bare: true, clip: "p-12" })); const p12 = page.locator('[data-post-play="p-12"]'); await expect(p12).toHaveAttribute("data-post-clock-mode", "forward", { timeout: 15_000 }); await page.locator("[data-play-toggle]").click(); await expect(p12).toHaveAttribute("data-post-clock-gap", "+38 min", { timeout: 15_000 }); await expect(p12).toHaveAttribute("data-post-clock", "6:38 AM Fri 30 Oct", { timeout: 15_000 }); await page.locator("[data-play-toggle]").click(); const clock = page.frameLocator('[data-post-card-frame="read"]').locator("[data-post-clock]"); await expect(clock).toContainText("ET"); const { el, words } = await boxesIn(page, "[data-post-clock]"); for (const w of words) expect(overlap(el!, w)).toBe(0); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "post-clock-forward.png") }); // Rewind: p-13 is two days earlier than p-12. await page.goto(urls.playHref(PROJECT, { bare: true, clip: "p-13" })); const p13 = page.locator('[data-post-play="p-13"]'); await expect(p13).toHaveAttribute("data-post-clock-mode", "rewind", { timeout: 15_000 }); await page.locator("[data-play-toggle]").click(); await expect(p13).toHaveAttribute("data-post-clock-gap", /^⟲ rewind −2 d 1 h/, { timeout: 15_000 }); await expect(p13).toHaveAttribute("data-post-clock", "5:00 AM Wed 28 Oct", { timeout: 15_000 }); await page.locator("[data-play-toggle]").click(); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "post-clock-rewind.png") }); });