// Video ops over the fixture manifests: trims within the fetched window, // clips only from the report's citations, narration through grounding. import { test } from "node:test"; import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { LOOK_KEYS } from "../look.mjs"; import { applyVideoOps, chaptersNamedBy, chaptersOf, cutView, trimmedCutView, TRIM_SLACK_SECONDS, type VideoManifest } from "./videoOps"; const DRAFT = path.join(path.dirname(fileURLToPath(import.meta.url)), "../../../testing/draft"); const json = async (p: string) => JSON.parse(await readFile(path.join(DRAFT, p), "utf8")); const manifest: VideoManifest = await json("manifests/basic.video.manifest.json"); const report = await json("reports/basic.report.json"); const ids = (m: VideoManifest) => m.timeline.map((e) => e.id); test("drop, move (after an entry, or to the start), heading, title, subtitle", () => { const r = applyVideoOps( manifest, [ { op: "drop", id: "c02" }, { op: "move", id: "c06", after: "the-bakery" }, { op: "move", id: "the-contest", after: "start" }, { op: "heading", id: "the-bakery", text: "The shop" }, { op: "title", text: "The baker, in her words" }, { op: "subtitle", text: "Two recordings" }, ], { report }, ); assert.deepEqual(r.refused, []); assert.deepEqual(ids(r.manifest), ["the-contest", "title", "the-bakery", "c06", "c01", "c03", "c04", "c05"]); assert.equal(r.manifest.timeline.find((e) => e.id === "the-bakery")!.heading, "The shop"); assert.equal(r.manifest.title, "The baker, in her words"); assert.equal(r.manifest.subtitle, "Two recordings"); // The input is not touched. assert.equal(manifest.timeline.length, 9); }); test("trim: within the parent's window ±3 s; outside it is refused with the window", () => { assert.equal(TRIM_SLACK_SECONDS, 3); const r = applyVideoOps( manifest, [ { op: "trim", id: "c01", start: 12.5, end: 22 }, { op: "trim", id: "c03", start: 41, end: 49 }, { op: "trim", id: "c04", start: 11, end: 10 }, { op: "trim", id: "the-bakery", start: 1, end: 2 }, { op: "trim", id: "c05", start: "x", end: 3 }, ], { report }, ); assert.deepEqual(r.applied.map((a) => a.index), [0]); const c01 = r.manifest.timeline.find((e) => e.id === "c01")!; assert.deepEqual([c01.start, c01.end], [12.5, 22]); assert.match(r.refused[0]!.why, /outside the fetched window \(42\.00–52\.80 s\)/); assert.match(r.refused[1]!.why, /start is before its end/); assert.match(r.refused[2]!.why, /is a card, not a clip/); assert.match(r.refused[3]!.why, /start and end in seconds/); // A second revision is bounded by the PARENT cut, not by the trimmed one. const again = applyVideoOps(r.manifest, [{ op: "trim", id: "c01", start: 11.5, end: 22.5 }], { report, parent: manifest }); assert.match(again.refused[0]!.why, /12\.00–22\.80/); }); test("add_clip: only the report's own video citations, once each", () => { const r = applyVideoOps( manifest, [ { op: "drop", id: "c02" }, { op: "add_clip", citation: "c02", after: "c01" }, { op: "add_clip", citation: "c03" }, { op: "add_clip", citation: "c99" }, { op: "trim", id: "c02", start: 22, end: 30 }, ], { report: { ...report, citations: { ...report.citations, p01: { kind: "post", channel: "s", id: "1", quote: "x" } } } }, ); assert.deepEqual(r.applied.map((a) => a.index), [0, 1, 4]); assert.match(r.refused[0]!.why, /"c03" is already in the cut/); assert.match(r.refused[1]!.why, /no citation "c99"/); assert.deepEqual(ids(r.manifest).slice(0, 4), ["title", "the-bakery", "c01", "c02"]); const c02 = r.manifest.timeline.find((e) => e.id === "c02")!; assert.deepEqual( { channel: c02.channel, video: c02.video, start: c02.start, end: c02.end, quote: c02.quote }, { channel: "synth-kitchen", video: "bread-day-01", start: 22, end: 30, quote: "we never said that about the old oven" }, ); const post = applyVideoOps(manifest, [{ op: "add_clip", citation: "p01" }], { report: { ...report, citations: { p01: { kind: "post", channel: "s", id: "1", quote: "x" } } }, }); assert.match(post.refused[0]!.why, /only a video citation becomes a clip/); }); test("narration: through grounding — refused with its reasons, flagged with the lints, removed with an empty text", () => { const r = applyVideoOps( manifest, [ { op: "narration", id: "the-bakery", text: "First, the bakery and the oven." }, { op: "narration", id: "the-contest", text: "She sold 7 pies to Megacorp. " }, { op: "narration", id: "the-contest", text: "She says she would never sell the bakery to a chain." }, { op: "narration", id: "c01", text: "A clip." }, { op: "narration", id: "the-contest", text: "She did enter the contest this year." }, { op: "narration", id: "title", text: "" }, ], { report }, ); assert.deepEqual(r.applied.map((a) => a.index), [0, 4, 5]); assert.equal(r.manifest.timeline.find((e) => e.id === "the-bakery")!.narration!.text, "First, the bakery and the oven."); assert.match(r.refused[0]!.why, /^narration refused: .*has a tag.*name “Megacorp”.*number 7/); assert.match(r.refused[1]!.why, /repeats 6\+ words of a quote/); assert.match(r.refused[2]!.why, /"c01" is a clip, not a card/); // A reversed meaning is applied, and flagged. assert.match(r.applied[1]!.warnings![0]!, /^negation mismatch/); assert.equal(r.manifest.timeline.find((e) => e.id === "title")!.narration, undefined); }); test("unknown ops and entries are refused with a reason", () => { const r = applyVideoOps(manifest, [{ op: "spin" }, { op: "drop", id: "nope" }, { op: "move", id: "c01", after: "c01" }], { report, }); assert.deepEqual(r.applied, []); assert.deepEqual( r.refused.map((x) => x.why), [ 'no op "spin" — the ops are drop, move, heading, narration, trim, add_clip, title, subtitle, look, slide, room', 'no entry "nope" in the cut', "an entry cannot move after itself", ], ); }); test("the cut as a model reads it", () => { const v = cutView(manifest); assert.match(v, /^TITLE: Synth Kitchen — what the baker says about the bakery\nSUBTITLE: \n- card title \(title\) heading: /); assert.match(v, /\n- clip c01 synth-kitchen\/bread-day-01 15–19\.8 s quote: "I would never sell the bakery to a chain"/); }); test("trimmed cut view (Q3): the named chapter in full, every entry still listed by id", () => { const full = cutView(manifest); const chapters = chaptersOf(manifest); assert.ok(chapters.length >= 3); const last = chapters.at(-1)!; const card = last.find((e) => e.type === "card")!; const v = trimmedCutView(manifest, { maxChars: 600, feedback: `Rename ${card.id}.` }); assert.deepEqual([...chaptersNamedBy(manifest, `Rename ${card.id}.`)], [chapters.length - 1]); for (const e of last) assert.ok(v.full.includes(e.id), `${e.id} in full`); for (const e of manifest.timeline) assert.ok(v.text.includes(` ${e.id}`), `${e.id} listed`); assert.ok(v.short.length > 0); assert.match(v.text, /\(not shown in full\)/); assert.ok(v.text.length < full.length); assert.equal(trimmedCutView(manifest, { maxChars: 1e6 }).short.length, 0); }); test("the look ops (release 38): look, slide and room through the look's own rules; a bad value refused with the build's sentence", () => { const r = applyVideoOps( manifest, [ { op: "look", key: "transition", value: 0.15 }, { op: "slide", key: "blur", value: 0.3 }, { op: "room", key: "tail", value: 2 }, { op: "look", key: "transition", value: 9 }, { op: "look", key: "chrome", value: {} }, ], { report, feedback: "quicker joins, less smear, keep 2 s after the last word" }, ); const render = r.manifest.render as { transition: number; slide: unknown; room: unknown }; assert.equal(render.transition, 0.15); assert.deepEqual(render.slide, { blur: 0.3 }); assert.deepEqual(render.room, { tail: true, maxS: 2 }); assert.deepEqual(r.applied.map((a) => a.index), [0, 1, 2]); // The slide's knobs warn on a cut whose join is not a blur slide. assert.match(r.applied[1]!.warnings![0]!, /the slide's knobs show once the join style is blur slide/); assert.deepEqual( r.refused.map((x) => x.why), // The list is the product's own (look.mjs LOOK_KEYS), never a copy that rots when a look key is added. ["render.transition must be a number of seconds from 0 (a hard cut) to 3", `"chrome" is not a look setting (${LOOK_KEYS.join(", ")})`], ); assert.equal((manifest.render as { transition: number }).transition, 0.4, "the input is not touched"); });