import { test, expect, type APIRequestContext, type Page } from "@playwright/test"; import { execFileSync, spawnSync } from "node:child_process"; import { readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import * as urls from "./urls"; // --------------------------------------------------------------------------- // THE VIDEO MODULES (release 39): the generic module panel in the Media tab // (components/projects/ModulesPanel.tsx), drawn from each module's `ui` // descriptor and writing through the writers (PUT /api/report/modules); the // single-layer preview (POST /api/report/chrome/preview { layers }); the look // presets (the panel's list, `umtool preset apply`) and `umtool modules // apply` (a module-config.json onto the cut). // // modules-fixture the deck on, three clips, a pair and a status row // modules-preset-fixture no chrome of its own, a module-config.json beside it // // The deck refuses to draw without its faces: the preview test skips on a // machine make-fixture.mjs found no font on, as onscreen.spec.ts does. // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const UMTOOL = path.join(HERE, ".."); const FIXTURE = path.join(UMTOOL, ".e2e-song"); const PROJECT = "reports/modules-fixture"; const DIR = path.join(FIXTURE, "reports", "modules-fixture"); const PRESET_PROJECT = "reports/modules-preset-fixture"; const PRESET_DIR = path.join(FIXTURE, "reports", "modules-preset-fixture"); const enc = encodeURIComponent; const cliEnv = { ...process.env, SONG_REPORTS_DIR: path.join(FIXTURE, "reports"), SONG_DIR: path.join(FIXTURE, "data"), CHANNELS_DIR: path.join(FIXTURE, "channels"), UMTOOL_CACHE_DIR: path.join(FIXTURE, "cache"), UMTOOL_MEDIA_DIR: "", }; const umtool = (args: string[]) => spawnSync("node", ["bin/umtool.mjs", ...args], { cwd: UMTOOL, encoding: "utf8", env: cliEnv }); type Manifest = { preset?: string; render: Record; timeline: { id: string; claim?: unknown }[] }; const readManifest = (dir = DIR): Manifest => JSON.parse(readFileSync(path.join(dir, "video.manifest.json"), "utf8")); async function modulesState(request: APIRequestContext, project = PROJECT) { const r = await request.get(`/api/report/modules?project=${enc(project)}`); expect(r.ok(), await r.text()).toBeTruthy(); return (await r.json()) as { token: string; preset: string | null; presets: { name: string }[]; modules: { id: string; enabled: boolean; fields: Record; effective: Record; problems: string[] }[] }; } async function putModule(request: APIRequestContext, body: Record, project = PROJECT) { const { token } = await modulesState(request, project); return request.put("/api/report/modules", { data: { project, token, ...body } }); } async function openPanel(page: Page, project = PROJECT) { await page.goto(urls.section(project, "onscreen")); const panel = page.getByTestId("modules-panel"); await expect(panel.getByTestId("module-pair")).toBeAttached({ timeout: 60_000 }); return panel; } async function openModule(page: Page, id: string) { const card = page.getByTestId(`module-${id}`); await card.locator("summary").click(); await expect(card).toHaveAttribute("open", ""); return card; } // workers: 1, not fully parallel (playwright.config.ts): the tests run in // file order, and the later ones start from the state the earlier leave. test.beforeEach(() => { test.setTimeout(120_000); }); test("the panel draws every module from its descriptor, and a module is switched on, filled and off through the writers", async ({ page, request }) => { // From a known state: no chapters. expect((await putModule(request, { module: "chapter", enabled: false })).ok() || true).toBeTruthy(); const panel = await openPanel(page); for (const id of ["pair", "chapter", "status", "finale", "intro", "counter", "shell", "rail", "stamp"]) { await expect(panel.getByTestId(`module-${id}`)).toBeAttached(); } await expect(panel.getByTestId("module-state-chapter")).toHaveText("off"); await expect(panel.getByTestId("module-state-pair")).toHaveText("on"); const card = await openModule(page, "chapter"); // A controlled box: it turns when the write lands and the panel re-reads, so click, then wait. await card.getByTestId("module-toggle-chapter").click(); await expect(panel.getByTestId("module-state-chapter")).toHaveText("on"); await expect.poll(() => readManifest().render.chrome.chapter).toEqual({ list: [] }); // A row of the list editor, its clip from the cut's clips, saved. await card.getByTestId("module-field-chapter-list-add").click(); await card.getByTestId("module-field-chapter-list-0-at").fill("c02"); await card.getByTestId("module-field-chapter-list-0-title").fill("Part two"); await card.getByTestId("module-field-chapter-list-0-date").fill("2020-10"); await card.getByTestId("module-save-chapter").click(); await expect(card.getByTestId("module-status-chapter")).toHaveText("saved"); await expect.poll(() => readManifest().render.chrome.chapter).toEqual({ list: [{ at: "c02", title: "Part two", date: "2020-10" }] }); // A scalar field: the pair strip to the top. const pair = await openModule(page, "pair"); await pair.getByTestId("module-field-pair-position").selectOption("top"); await pair.getByTestId("module-save-pair").click(); await expect(pair.getByTestId("module-status-pair")).toHaveText("saved"); await expect.poll(() => readManifest().render.chrome.pair.position).toBe("top"); // The rest of the pair as it was. expect(readManifest().render.chrome.pair.pairs[0].then.clip).toBe("c01"); // Off: the object removed, nothing else of the deck moved. await card.getByTestId("module-toggle-chapter").click(); await expect(panel.getByTestId("module-state-chapter")).toHaveText("off"); await expect.poll(() => "chapter" in readManifest().render.chrome).toBe(false); expect(readManifest().render.chrome.layout).toBe("deck"); }); test("the chat module's settings are saved through the panel and refused with the build's sentence; it has no switch (a clip's chat=on opts in)", async ({ page }) => { const panel = await openPanel(page); await expect(panel.getByTestId("module-chat")).toBeAttached(); const card = await openModule(page, "chat"); await expect(card.getByTestId("module-toggle-chat")).toHaveCount(0); await card.getByTestId("module-field-chat-position").selectOption("left"); await card.getByTestId("module-save-chat").click(); await expect(card.getByTestId("module-status-chat")).toHaveText("saved"); await expect.poll(() => readManifest().render.chrome.chat.position).toBe("left"); // A value the build would refuse is refused with its sentence, the manifest untouched. // (The save reloads the panel, and the reload resets the card's draft: a fill // that lands before it is wiped. Fill until the draft holds.) await expect(async () => { await card.getByTestId("module-field-chat-lines").fill("99"); await expect(card.getByTestId("module-save-chat")).toBeEnabled({ timeout: 1_000 }); }).toPass({ timeout: 15_000 }); await card.getByTestId("module-save-chat").click(); await expect(card.getByTestId("module-errors-chat")).toContainText("render.chrome.chat.lines"); expect(readManifest().render.chrome.chat.position).toBe("left"); expect(readManifest().render.chrome.layout).toBe("deck"); }); test("a save the build would refuse is refused with the build's sentence, and the manifest is untouched", async ({ page }) => { const before = JSON.stringify(readManifest().render.chrome.status); await openPanel(page); const card = await openModule(page, "status"); await card.getByTestId("module-field-status-rows").fill("12"); await card.getByTestId("module-save-status").click(); await expect(card.getByTestId("module-errors-status")).toContainText("render.chrome.status.rows"); expect(JSON.stringify(readManifest().render.chrome.status)).toBe(before); }); test("a chapter at a clip the cut does not hold is refused by the writer", async ({ request }) => { const r = await putModule(request, { module: "chapter", fields: { list: [{ at: "nope", title: "Gone" }] } }); expect(r.status()).toBe(400); expect(JSON.stringify(await r.json())).toContain("no timeline entry is nope"); const off = await putModule(request, { module: "chapter", enabled: false }); expect(off.ok() || off.status() === 400).toBeTruthy(); }); test("Preview alone composes one module's page by itself; the files route serves it", async ({ page, request }) => { test.skip(!readManifest().render.fontRegular, "the deck refuses to draw without its faces, and make-fixture.mjs found no font on this machine"); // The route: a whole-cut layer (pair) and a window layer (chapter), alone. expect((await putModule(request, { module: "chapter", enabled: true, fields: { list: [{ at: "c02", title: "Part two" }] } })).ok()).toBeTruthy(); const res = await request.post("/api/report/chrome/preview", { data: { project: PROJECT, layers: ["pair", "chapter", "subtitle", "threads"] } }); expect(res.ok(), await res.text()).toBeTruthy(); const j = (await res.json()) as { layers: { id: string; region?: string; src?: string; off?: string; windows?: { segment: string; src?: string }[] }[] }; const by = Object.fromEntries(j.layers.map((l) => [l.id, l])); expect(by.pair.src).toContain("pair-preview/index.html"); expect(by.chapter.windows?.[0]?.segment).toBe("c02"); expect(by.chapter.windows?.[0]?.src).toContain("chapter-preview-c02/index.html"); expect(by.subtitle.region).toBe("deck"); expect(by.threads.off).toBeTruthy(); const html = await (await request.get(by.pair.src!)).text(); expect(html).toContain('id="pair-data"'); expect(html).toContain("The same opening, renamed."); expect(html).not.toContain('id="deck-data"'); // A window page another cut's entry names is not served. expect((await request.get(by.pair.src!.replace("pair-preview/", "chapter-preview-zz/"))).status()).toBe(404); // The panel's button: the module's own iframe over a scaled frame. await openPanel(page); const card = await openModule(page, "pair"); await card.getByTestId("module-preview-pair").click(); await expect(card.getByTestId("module-preview-iframe")).toHaveAttribute("src", /pair-preview\/index\.html/); await expect(card.getByTestId("module-preview-scrub")).toBeVisible(); await putModule(request, { module: "chapter", enabled: false }); }); test("a look preset is the layer under the manifest: named from the panel, read through, removed from a terminal", async ({ page, request }) => { const r0 = umtool(["preset", "apply", "none", PRESET_PROJECT]); expect(r0.status, r0.stderr).toBe(0); const s0 = await modulesState(request, PRESET_PROJECT); expect(s0.presets.map((p) => p.name)).toEqual(expect.arrayContaining(["huck-deck", "plain-deck", "archive-catalog"])); expect(s0.modules.find((m) => m.id === "intro")!.enabled).toBe(false); await openPanel(page, PRESET_PROJECT); await page.getByTestId("modules-preset").selectOption("plain-deck"); await expect(page.getByTestId("modules-preset-status")).toContainText("plain-deck"); await expect.poll(() => readManifest(PRESET_DIR).preset).toBe("plain-deck"); // Nothing of the preset copied in: the manifest's render is as it was. expect(readManifest(PRESET_DIR).render.chrome).toBeUndefined(); // Read through: the intro and the deck are the preset's, as the build reads them. const s1 = await modulesState(request, PRESET_PROJECT); expect(s1.modules.find((m) => m.id === "intro")!.enabled).toBe(true); expect(s1.modules.find((m) => m.id === "intro")!.effective.style).toBe("boot-dark"); expect(s1.modules.find((m) => m.id === "subtitle")!.effective.parts).toEqual(["channel", "title", "date", "clock"]); // A chrome save over the preset is whole: the writer reads the deck from it. expect((await putModule(request, { module: "pair", enabled: true }, PRESET_PROJECT)).ok()).toBeTruthy(); expect(readManifest(PRESET_DIR).render.chrome).toEqual({ pair: { pairs: [] } }); const r1 = umtool(["preset", "list", "--json"]); expect(JSON.parse(r1.stdout).map((p: { name: string }) => p.name)).toContain("archive-catalog"); const bad = umtool(["preset", "apply", "no-such-preset", PRESET_PROJECT]); expect(bad.status).toBe(1); expect(bad.stderr).toContain('no preset "no-such-preset"'); // Removed: the chrome named only what it changed from the preset, so the cut // over no preset would not build -- refused, the preset kept. const r2 = umtool(["preset", "apply", "none", PRESET_PROJECT]); expect(r2.status).toBe(1); expect(readManifest(PRESET_DIR).preset).toBe("plain-deck"); expect((await putModule(request, { module: "pair", enabled: false }, PRESET_PROJECT)).ok()).toBeTruthy(); const r3 = umtool(["preset", "apply", "none", PRESET_PROJECT]); expect(r3.status, r3.stderr).toBe(0); expect(readManifest(PRESET_DIR).preset).toBeUndefined(); }); test("umtool modules apply maps a module-config.json onto the cut, listing what has no home", async () => { const dry = umtool(["modules", "apply", PRESET_PROJECT, "--dry-run", "--json"]); expect(dry.status, dry.stderr).toBe(0); const plan = JSON.parse(dry.stdout); expect(plan.patch.preset).toBe("plain-deck"); expect(plan.patch.render.chrome.chapter.list[0]).toEqual({ at: "c02", title: "Part two", subtitle: "the second clip" }); expect(plan.unmapped.some((u: string) => u.startsWith("modules.shell.title"))).toBe(true); expect(readManifest(PRESET_DIR).preset).toBeUndefined(); const run = umtool(["modules", "apply", PRESET_PROJECT]); expect(run.status, run.stderr).toBe(0); const m = readManifest(PRESET_DIR); expect(m.preset).toBe("plain-deck"); expect(m.render.chrome.pair.pairs[0].now).toEqual({ clip: "c02", date: "2026-02" }); // Back as it was, through the same writers. execFileSync("node", ["bin/umtool.mjs", "preset", "apply", "plain-deck", PRESET_PROJECT], { cwd: UMTOOL, env: cliEnv }); });