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 SLAM (report-to-video/post-slam.mjs; knobs.md, "A slam"): a // trailer-style title slam -- each glyph huge, shrinking onto the card just // above the matched words and hitting, one trailer hit per glyph. 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-13 words only. // // POST_SLAM_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_SLAM_SHOTS ?? null; type Entry = { id: 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: each slam unit's glyph box, and the card words'. */ const slamBoxes = (page: Page) => page.frameLocator('[data-post-card-frame="read"]').locator("body").evaluate((body) => { const r = (el: Element) => { const b = el.getBoundingClientRect(); return { x: b.left, y: b.top, width: b.width, height: b.height }; }; const doc = body.ownerDocument; return { units: Array.from(doc.querySelectorAll(".pslam")).map((el) => ({ id: el.getAttribute("data-post-fx-slam"), opacity: Number((el as HTMLElement).style.opacity), text: el.querySelector(".pslam-front")?.textContent ?? "", ...r(el.querySelector(".pslam-front")!) })), words: Array.from(doc.querySelectorAll("[data-w]")).map((w) => ({ id: w.getAttribute("data-w"), ...r(w) })), inCard: Array.from(doc.querySelectorAll(".pslam")).some((el) => el.closest("[data-post-card]") !== null || !!el.querySelector("[data-w], [data-post-block]")), }; }); /** Play the item until every glyph has landed, then pause. */ async function playUntilLanded(page: Page, id: string, n: number) { const player = page.locator(`[data-post-play="${id}"]`); await expect(player).toHaveAttribute("data-post-slams", "1", { timeout: 15_000 }); await page.locator("[data-play-toggle]").click(); await expect(player).toHaveAttribute("data-post-slam-landed", String(n), { timeout: 15_000 }); await page.locator("[data-play-toggle]").click(); return player; } test("A SLAM (operator, 4 Nov: 不動心 on \"Fudōshin\"): click a word -> type the glyphs -> Slam here -> saved; the bench marks its hits; Play lands three gold glyphs in a row just ABOVE the word, over no word of it, drawn over the card", 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-slam-here]")).toBeDisabled(); await panel.locator("[data-post-slam-text]").fill("不動心"); await panel.locator("[data-post-slam-here]").click(); await expect(panel.locator('[data-post-slam-fx="0"]')).toBeVisible(); expect(timeline().find((e) => e.id === "p-11")!.fx).toEqual([{ kind: "slam", match: "speaks", text: "不動心" }]); // No word timings: three hits from the reading estimate's start, SLAM_GAP apart. await expect(panel.locator('[data-post-slam-fx="0"]')).toContainText("the estimate"); await expect(page.locator('[data-post-bench-slam-mark="0"]')).toBeVisible(); await expect(page.locator('[data-post-bench-slam-mark="0"] [data-post-bench-slam-hit]')).toHaveCount(3); await expect(page.locator('[data-post-play="p-11"]')).toHaveAttribute("data-post-slams", "1", { timeout: 15_000 }); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "post-slam-bench.png") }); // Play the cut: the three glyphs land, left to right, above the word. await page.goto(urls.playHref(PROJECT, { bare: true, clip: "p-11" })); const player = await playUntilLanded(page, "p-11", 3); await expect(player).toHaveAttribute("data-post-slam-where", "above"); const { units, words, inCard } = await slamBoxes(page); expect(units.map((u) => u.text)).toEqual(["不", "動", "心"]); expect(units.every((u) => u.opacity > 0)).toBe(true); expect(units[0].x).toBeLessThan(units[1].x); expect(units[1].x).toBeLessThan(units[2].x); // Never over the word it lands on; sitting above its line; never in the card's words. const word = words.find((w) => w.id === "post:2")!; for (const u of units) { expect(overlap(u, word)).toBe(0); expect(u.y + u.height).toBeLessThanOrEqual(word.y + 2); } const mid = (units[0].x + units[2].x + units[2].width) / 2; expect(Math.abs(mid - (word.x + word.width / 2))).toBeLessThan(word.width); expect(inCard).toBe(false); // The CJK is drawn by a face this machine has, at its Black weight. expect(await page.frameLocator('[data-post-card-frame="read"]').locator(".pslam").first().evaluate((el) => el.ownerDocument.fonts.check("900 100px 'Noto Sans CJK JP'", "不動心"))).toBe(true); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "post-slam-play.png") }); }); test("A PHRASE (operator, 4 Nov: 八极拳 on \"Baji Quan\"): a two-word match, the SC face, three glyphs over the phrase; an at and no word span: the hits SLAM_GAP apart from it", async ({ page }) => { const m = manifest(); Object.assign(m.timeline.find((e) => e.id === "p-13")!, { fx: [{ kind: "slam", text: "八极拳", match: "waiting for", face: "sc", at: 0.5 }] }); writeFileSync(MANIFEST, JSON.stringify(m, null, 2) + "\n"); await page.setViewportSize({ width: 1500, height: 1000 }); await page.goto(urls.playHref(PROJECT, { bare: true, clip: "p-13" })); await playUntilLanded(page, "p-13", 3); const { units, words } = await slamBoxes(page); expect(units.map((u) => u.text)).toEqual(["八", "极", "拳"]); const phrase = words.filter((w) => w.id === "post:2" || w.id === "post:3"); expect(phrase).toHaveLength(2); for (const u of units) for (const w of phrase) expect(overlap(u, w)).toBe(0); expect(await page.frameLocator('[data-post-card-frame="read"]').locator(".pslam").first().evaluate((el) => el.ownerDocument.fonts.check("900 100px 'Noto Sans CJK SC'", "八极拳"))).toBe(true); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "post-slam-phrase.png") }); }); test("THE BAR (operator, 4 Nov: \"have the characters appear above a bar with the translation\"): the glyphs land ON a translation bar, the two above the word and over none of it; the caption module untouched", async ({ page }) => { const m = manifest(); Object.assign(m.timeline.find((e) => e.id === "p-11")!, { fx: [{ kind: "slam", text: "不動心", match: "speaks", gap: 0.22, volume: 0.5, bar: "fudōshin — \"immovable mind\"" }] }); writeFileSync(MANIFEST, JSON.stringify(m, null, 2) + "\n"); await page.setViewportSize({ width: 1500, height: 1000 }); await page.goto(urls.playHref(PROJECT, { bare: true, clip: "p-11" })); const player = await playUntilLanded(page, "p-11", 3); await expect(player).toHaveAttribute("data-post-slam-bar", "1"); const bar = page.frameLocator('[data-post-card-frame="read"]').locator(".pslam-bar"); await expect(bar).toContainText("fudōshin"); await expect(bar).toContainText("immovable mind"); await expect.poll(() => bar.evaluate((el) => Number((el as HTMLElement).style.opacity))).toBeGreaterThan(0.5); const barBox = await bar.evaluate((el) => { const b = el.getBoundingClientRect(); return { x: b.left, y: b.top, width: b.width, height: b.height }; }); const { units, words } = await slamBoxes(page); const word = words.find((w) => w.id === "post:2")!; // The row stands on the bar, centred on it; the bar sits above the word, over none of it. for (const u of units) expect(u.y + u.height).toBeLessThanOrEqual(barBox.y + 4); const mid = (units[0].x + units[2].x + units[2].width) / 2; expect(Math.abs(mid - (barBox.x + barBox.width / 2))).toBeLessThan(4); expect(overlap(barBox, word)).toBe(0); expect(barBox.y + barBox.height).toBeLessThanOrEqual(word.y + 2); if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "post-slam-bar.png") }); });