// 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 = '