// The analysis item in the build: its sound (the clip's span under it, or the teaser's silence), its encode, its // length in the schedule, the deck away over it, its chapter -- and its errors among the build's validators. import { test } from "node:test"; import assert from "node:assert/strict"; import { existsSync } from "node:fs"; import { spawnSync } from "node:child_process"; import { mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { analysisAudioGraph, analysisEncodeArgs, chapterTitle, teaserAudioGraph } from "./build-video.mjs"; import { estimatedDuration, estimateSchedule, hidesDeck, resolveDeck } from "./deck.mjs"; import { validatorErrors, lintManifest, qrsOf } from "./lint.mjs"; const FONTS = { fontRegular: new URL("./fonts/IBMPlexMono-Regular.ttf", import.meta.url).pathname, fontBold: new URL("./fonts/IBMPlexMono-Bold.ttf", import.meta.url).pathname }; const render = { width: 1920, height: 1080, fps: 30, audioRate: 48000, audioChannels: 2, crf: 20, preset: "fast", audioBitrate: "192k", ...FONTS }; test("the sound: the clip's span laid on its clock, levelled, faded, padded to the item; silent: the teaser's silence", () => { const g = analysisAudioGraph({ seconds: 14.5, render, audio: { clip: "c46" }, gain: -2.5 }); assert.equal(g, "[1:a]aresample=async=1:min_hard_comp=0.01:first_pts=0,volume=-2.5dB,alimiter=limit=0.891:level=0:latency=1," + "afade=t=in:d=0.3,afade=t=out:st=14.2:d=0.3,apad=whole_dur=14.5,aformat=sample_rates=48000:channel_layouts=stereo[ta]"); assert.doesNotMatch(analysisAudioGraph({ seconds: 14.5, render, audio: { clip: "c46" } }), /volume=/); assert.equal(analysisAudioGraph({ seconds: 7, render }), teaserAudioGraph([], { seconds: 7, render })); }); test("the encode: the frames, then the clip's span as input 1, then the graph", () => { const args = analysisEncodeArgs({ framesDir: "/f", seconds: 14.5, render, audio: "[1:a]anull[ta]", sound: { path: "/raw/c.mp4", from: 1.2, seconds: 14.5 }, outPath: "/o.mp4" }); const i = args.indexOf("-i"); const j = args.indexOf("-i", i + 1); assert.equal(args[i + 1], "/f/frame_%06d.png"); assert.deepEqual(args.slice(j - 4, j + 2), ["-ss", "1.200", "-t", "14.500", "-i", "/raw/c.mp4"]); assert.ok(args.indexOf("-filter_complex") > j); const silent = analysisEncodeArgs({ framesDir: "/f", seconds: 7, render, audio: "x[ta]", outPath: "/o.mp4" }); assert.equal(silent.filter((a) => a === "-i").length, 1); }); const SCHEMA = { kind: "measure", tracks: [{ key: "weight", role: "value" }] }; const manifest = (extra = {}) => ({ render: { ...render, chrome: { deck: {} } }, ledgerSchema: SCHEMA, ledger: [ { id: "w1", scope: "weight", population: "current", date: "2026-09-01", value: 310 }, { id: "w2", scope: "weight", population: "current", date: "2026-10-01", value: 300 }, ], timeline: [ { type: "clip", id: "c1", video: "v1", start: 10, end: 40 }, { type: "analysis", id: "an", title: "The run", series: { rows: ["w1", "w2"] }, reading: "It went down.", ...extra }, ], provenance: { siteOrigin: "https://example.pages.dev", channelSlug: "chan" }, }); test("in the schedule: its length, full frame (the deck away), named by its title", async () => { const m = manifest(); const e = m.timeline[1]; assert.equal(estimatedDuration(e, m.render), 7); assert.equal(estimatedDuration({ ...e, audio: { clip: "c1", start: 12, end: 26.5 } }, m.render), 14.5); assert.equal(hidesDeck(e, resolveDeck(m.render)), true); const seg = estimateSchedule(m).segments.find((s) => s.id === "an"); assert.equal(seg.title, "The run"); assert.equal(seg.qrUrl, null); assert.equal(await chapterTitle(e, 1, {}), "The run"); }); test("the build's validators and the lint: its errors, its warnings, its code measured", () => { assert.deepEqual(validatorErrors(manifest()).filter((x) => /analysis/.test(x)), []); const bad = manifest({ series: { rows: ["w1", "w9"] }, seconds: 9 }); const errs = validatorErrors(bad); assert.ok(errs.some((x) => /series row w9 is not in the ledger/.test(x)), errs.join("\n")); assert.ok(errs.some((x) => /held 9 s with no audio/.test(x))); const heard = manifest({ audio: { clip: "c1", start: 12, end: 26.5 } }); const { warnings } = lintManifest(heard, { whole: heard, exists: () => true }); assert.ok(warnings.some((w) => /its words are heard twice/.test(w)), warnings.join("\n")); const q = qrsOf(heard).find((x) => x.id === "an"); assert.ok(q && /example\.pages\.dev/.test(q.url) && q.px === 210, JSON.stringify(q)); }); test("the build measures the page: what fits passes, a reading or a table past its room is named", { skip: !existsSync(process.env.CHROME ?? "/usr/bin/chromium") && "no chromium" }, async () => { const { measureAnalysisPage } = await import("./build-video.mjs"); const { analysisHtml } = await import("./chrome-analysis.mjs"); const m = manifest({ audio: { clip: "c1", start: 12, end: 26.5 }, pace: [{ from: "w1", to: "w2" }], status: [{ label: "SCALE VIDEO", value: "NOT POSTED", color: "red" }] }); const e = m.timeline[1]; const ctx = { ledger: m.ledger, schema: m.ledgerSchema, timeline: m.timeline, render: m.render }; const dir = await mkdtemp(path.join(tmpdir(), "analysis-measure-")); try { const page = (html) => writeFile(path.join(dir, "index.html"), html); const html = analysisHtml(e, ctx, { cite: "HEARD: chan · a stream · 1:08:32", qrSrc: "qr.png" }); await page(html); assert.deepEqual(await measureAnalysisPage(dir, m.render), []); // A reading past its room (the plan refuses it first; the page is the build's last word). await page(html.replace("It went down.", "word ".repeat(120))); const long = await measureAnalysisPage(dir, m.render); assert.ok(long.some((x) => /the reading leaves the frame|the QR overlaps the reading/.test(x)), long.join("\n")); // A table past the panel's room runs under the code. const row = '
1 → 2 OCT
1 days
−1 lb
'; await page(html.replace('
', `
${row.repeat(8)}`)); const tall = await measureAnalysisPage(dir, m.render); assert.ok(tall.some((x) => /the QR overlaps table row \d|the table runs into/.test(x)), tall.join("\n")); } finally { await rm(dir, { recursive: true, force: true }); } }); test("the first use's shape WITH audio passes the build's own validator block (buildVideo) and the lint", async () => { const { buildVideo } = await import("./build-video.mjs"); const W = (id, population, date, value, extra = {}) => ({ id, scope: "weight", population, date, value, ...extra }); const schema = { kind: "measure", unit: "lb", bandGapDays: 60, tracks: [{ key: "weight", role: "value", label: "Weight" }], fields: [{ key: "qualifier", label: "Bound", kind: "enum", options: [">", "<", "≈"], render: "cell" }] }; const m = { schemaVersion: 1, slug: "fu", title: "First use", provenance: { siteOrigin: "https://example.pages.dev", channelSlug: "chan" }, render: { width: 1920, height: 1080, fps: 30, ...FONTS }, ledgerSchema: schema, ledger: [W("w83", "delta", "2026-07-23", -5), W("w91", "past", "2026-08-03", 349, { asOf: "2026-07-19" }), W("w104", "delta", "2026-09-04", -31.5), W("w110", "current", "2026-09-24", 310.5), W("w113", "delta", "2026-10-06", -47), W("w115", "current", "2026-10-15", 300, { qualifier: "<" }), W("w118", "current", "2026-10-26", 291.5, { asOf: "2026-10-23" }), W("w122", "delta", "2026-11-03", -58.2), W("w137", "current", "2026-11-07", 288.5), { id: "p01", scope: "weight", population: "promise", date: "2026-10-23" }], timeline: [ { type: "teaser", id: "t1", lines: ["One"] }, { type: "clip", id: "c46", video: "v1", start: 4111.4, end: 4158.8, cutStart: 4112.6, cutEnd: 4158.5, gaps: [{ from: 4127.15, to: 4132.45 }] }, { type: "analysis", id: "analysis", kicker: "THE KETO RUN · EVERY WEIGHT HE STATED", title: "What his own numbers say", series: { rows: ["w91", "w110", "w115", "w118", "w122", "w137"] }, band: { from: "2026-10-23", to: "2026-11-03", label: "WORLD SERIES" }, marks: [{ row: "w91", label: "he starts" }, { row: "w115", label: "" }, { row: "w118", label: "offers the scale video" }], pace: [{ from: "2026-09-24", to: "2026-10-15" }, { from: "2026-10-15", to: "2026-10-23" }, { from: "2026-10-23", to: "2026-11-03", note: "the World Series" }, { from: "2026-11-03", to: "2026-11-07" }], status: [{ label: "SCALE VIDEO", value: "NOT POSTED", note: "offered 23 Oct", color: "red", row: "p01" }], reading: "By his own numbers the loss is steady — and it stalled for two weeks around the World Series, exactly when the scale video was promised.", footnote: "All figures are his own statements; no weigh-in has been shown.", mark: { row: "w115" }, audio: { clip: "c46", start: 4112.6, end: 4127.1 } }, ], }; assert.deepEqual(validatorErrors(m), []); assert.deepEqual(lintManifest(m, { whole: m, exists: () => true }).errors, []); const dir = await mkdtemp(path.join(tmpdir(), "analysis-first-use-")); try { const manifestPath = path.join(dir, "video.manifest.json"); await writeFile(manifestPath, JSON.stringify(m)); // A fetch of the teaser is a no-op AFTER the build's validators: the block that refused `audio` on a non-clip. assert.deepEqual(await buildVideo({ manifestPath, opts: { noNetwork: true, skipFetch: true }, fetchOnly: "t1" }), { out: null, failures: [] }); // A vertical build refuses it before anything is spent. await assert.rejects(buildVideo({ manifestPath, opts: { noNetwork: true, skipFetch: true, vertical: true }, fetchOnly: "t1" }), /vertical: analysis is an analysis item, which has no vertical form/); } finally { await rm(dir, { recursive: true, force: true }); } }); test("without the deck's faces an analysis item is refused before anything is spent", () => { const m = manifest(); m.render = { width: 1920, height: 1080, fps: 30 }; assert.ok(validatorErrors(m).some((x) => /an analysis item draws in the deck's faces -- the cut needs render\.fontRegular and render\.fontBold/.test(x))); }); test("--cut-window over an analysis item: the whole cut's segments, then the window laid -- it builds", { skip: (!existsSync(process.env.CHROME ?? "/usr/bin/chromium") || spawnSync("ffmpeg", ["-version"], { stdio: "ignore" }).status !== 0) && "no chromium or ffmpeg" }, async () => { const { buildVideo } = await import("./build-video.mjs"); const W = (id, population, date, value) => ({ id, scope: "weight", population, date, value }); const m = { schemaVersion: 1, slug: "win", title: "Window", provenance: { siteOrigin: "https://example.pages.dev", channelSlug: "chan" }, render: { width: 640, height: 360, fps: 10, transition: 0, audioRate: 48000, audioChannels: 2, ...FONTS }, ledgerSchema: { kind: "measure", unit: "lb", tracks: [{ key: "weight", role: "value" }] }, ledger: [W("w1", "current", "2026-09-01", 310), W("w2", "current", "2026-10-01", 300)], timeline: [{ type: "analysis", id: "an", title: "The run", series: { rows: ["w1", "w2"] }, pace: [{ from: "w1", to: "w2" }], reading: "It went down.", seconds: 6 }], }; const dir = await mkdtemp(path.join(tmpdir(), "analysis-window-")); try { const manifestPath = path.join(dir, "video.manifest.json"); await writeFile(manifestPath, JSON.stringify(m)); const r = await buildVideo({ manifestPath, opts: { noNetwork: true, skipFetch: true, cutWindow: { at: 1, dur: 2 } } }); assert.match(r.out, /win\.window\.mp4$/); const probe = spawnSync("ffprobe", ["-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0", r.out], { encoding: "utf8" }); assert.ok(Math.abs(Number(probe.stdout) - 2) < 0.25, probe.stdout); } finally { await rm(dir, { recursive: true, force: true }); } });