// The manifest lint (lint.mjs): its QR arithmetic, each rule's severity, the // validators it folds in, `--only`, its two command lines, and the build's // refusal before anything renders. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { execFile } from "node:child_process"; import { mkdtempSync, rmSync, writeFileSync, mkdirSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { fileURLToPath } from "node:url"; import { MIN_QR_PX_PER_MODULE_720P, TEASER_HOUSE_CHARS, WARN_QR_PX_PER_MODULE_720P, imageSrcsOf, lintManifest, lintManifestFile, originQrsOf, pxPerModuleAt720, qrModules, qrsOf, qrVersion, } from "./lint.mjs"; import { buildVideo } from "./build-video.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const PALETTE = { bg: "#12101a", fg: "#f4f1ea", muted: "#9a93ad", accent: "#a97bff", amber: "#ffc860" }; const PROV = { siteOrigin: "https://example.test", channelSlug: "chan" }; const BASE = { width: 1920, height: 1080, fps: 30, transition: 0.5, palette: PALETTE }; const DECK = { ...BASE, chrome: { engine: "hyperframes", layout: "deck", deck: {} } }; const all = () => true; test("the QR arithmetic: version by byte capacity, modules with the quiet zone, px per module at 720p", () => { assert.equal(qrVersion("x".repeat(14), "M"), 1); assert.equal(qrVersion("x".repeat(15), "M"), 2); assert.equal(qrVersion("x".repeat(62), "M"), 4); assert.equal(qrVersion("x".repeat(63), "M"), 5); assert.equal(qrVersion("x".repeat(17), "L"), 1); assert.equal(qrVersion("x".repeat(667), "M"), null, "past version 20 is not a scannable code"); assert.equal(qrModules(1, 3), 27); assert.equal(qrModules(5, 3), 43); assert.equal(qrModules(5, 0), 37); // 150 px on a 1080p frame is 100 px at 720p; over 50 modules, 2. assert.equal(pxPerModuleAt720(150, 50, 1080), 2); assert.equal(pxPerModuleAt720(100, 50, 720), 2); }); test("a teaser line over house style is a WARNING, never an error; at the limit it says nothing", () => { const ok = "x".repeat(TEASER_HOUSE_CHARS); const long = "y".repeat(TEASER_HOUSE_CHARS + 1); // Three rows: an overline, a title, a kicker (deck.mjs teaserLines). const m = { render: BASE, provenance: PROV, timeline: [ { id: "t1", type: "teaser", lines: [long, ok, { text: `Kicker ${long}`, break: long }] }, { id: "t2", type: "teaser", lines: [ok, "Short title", ok] }, ], }; const r = lintManifest(m, { exists: all }); assert.deepEqual(r.errors, [], "the validators accept every line: each fits the frame"); assert.equal(r.warnings.length, 2, r.warnings.join("\n")); assert.match(r.warnings[0], /^timeline t1\.lines\[0\] "y{35}" is 35 characters; house style is at most 34$/); assert.match(r.warnings[1], /^timeline t1\.lines\[2\] break "y{35}" is 35 characters/); // A TITLE past 34 does not fit the frame: the validator's error, said once. const title = lintManifest({ ...m, timeline: [{ id: "t3", type: "teaser", lines: ["Soon", long] }] }, { exists: all }); assert.deepEqual(title.warnings, []); assert.equal(title.errors.length, 1); assert.match(title.errors[0], /at most 34 fit the frame as the title/); }); test("an image src that is not relative to the manifest, or names no file, is an ERROR", () => { const m = { render: { ...BASE, qr: false }, provenance: PROV, timeline: [ { id: "abs", type: "image", src: "/somewhere/else/shot.png", seconds: 4 }, { id: "home", type: "image", src: "~/shot.png", seconds: 4 }, { id: "gone", type: "image", src: "stills/missing.png", seconds: 4 }, { id: "here", type: "image", src: "stills/here.png", seconds: 4 }, { id: "row", type: "image", seconds: 4, panels: [{ src: "stills/here.png" }, { src: "/abs/b.png" }] }, ], }; const exists = (p) => p === path.resolve("/m", "stills/here.png"); const r = lintManifest(m, { manifestDir: "/m", exists, validators: false }); assert.equal(r.errors.length, 4, r.errors.join("\n")); assert.match(r.errors[0], /^timeline abs: the image "\/somewhere\/else\/shot\.png" must be a path relative to the manifest/); assert.match(r.errors[1], /^timeline home: the image "~\/shot\.png" must be a path relative/); assert.match(r.errors[2], /^timeline gone: the image "stills\/missing\.png" names no file beside the manifest/); assert.match(r.errors[3], /^timeline row\.panels\[1\]: .* must be a path relative/); assert.deepEqual(r.warnings, []); }); test("under the deck a post's shot and logo are pictures beside the manifest too", () => { const m = { render: DECK, provenance: PROV, timeline: [{ id: "c1", type: "clip", video: "v", start: 0, end: 5 }], posts: [{ id: "p1", attachTo: "c1", shot: "/abs/p.png", logo: "logos/l.png", url: "https://bsky.app/profile/a/post/abc" }], }; assert.deepEqual(imageSrcsOf(m).map((s) => s.where), ["posts p1.shot", "posts p1.logo"]); const r = lintManifest(m, { manifestDir: "/m", exists: all, validators: false }); assert.equal(r.errors.filter((e) => /posts p1\.shot/.test(e)).length, 1); assert.equal(r.errors.filter((e) => /posts p1\.logo/.test(e)).length, 0); // Without the deck no post is drawn, so none is read. assert.deepEqual(imageSrcsOf({ ...m, render: BASE }), []); }); test("a QR below 1.5 px/module at 720p is an ERROR, below 2.0 a WARNING, at 2.0 nothing", () => { assert.equal(MIN_QR_PX_PER_MODULE_720P, 1.5); assert.equal(WARN_QR_PX_PER_MODULE_720P, 2.0); const clip = (id, citeUrl) => ({ id, type: "clip", video: "v", start: 0, end: 5, citeUrl }); // The corner code (no deck): qrencode's scale, unresized. const corner = (scale, height = 1080) => lintManifest( { render: { ...BASE, height, qr: { scale } }, provenance: PROV, timeline: [clip("c1", "https://e.test/a")] }, { exists: all, validators: false }, ); assert.match(corner(2).errors[0] ?? "", /timeline c1 \(its corner QR\): 1\.33 px per module at 720p .* too small to scan/); assert.match(corner(2, 900).warnings[0] ?? "", /1\.60 px per module at 720p .* scans only on a good screen/); assert.deepEqual(corner(2, 900).errors, []); assert.deepEqual(corner(3), { errors: [], warnings: [] }); // The deck's code: resized to qr.size, quiet zone included. A 62-byte link // is a version-4 code, 39 modules: 80 px on 1080p is 1.37 per module, an // error; 100 px is 1.71, a warning; 120 px is 2.05, fine. const url62 = `https://e.test/${"a".repeat(62 - 15)}`; assert.equal(url62.length, 62); const deck = (size) => lintManifest( { render: { ...DECK, chrome: { ...DECK.chrome, deck: { qr: { size } } } }, provenance: PROV, timeline: [clip("c1", url62)] }, { exists: all, validators: false }, ); assert.match(deck(80).errors[0] ?? "", /timeline c1 \(the deck's QR\): 1\.37 px per module at 720p \(80 px on a 1080p frame, a version-4 code, 39 modules\)/); assert.match(deck(100).warnings[0] ?? "", /1\.71 px per module/); assert.deepEqual(deck(100).errors, []); assert.deepEqual(deck(120), { errors: [], warnings: [] }); // A link no QR can hold. const huge = lintManifest( { render: BASE, provenance: PROV, timeline: [clip("c1", `https://e.test/${"z".repeat(700)}`)] }, { exists: all, validators: false }, ); assert.match(huge.errors[0], /too long for a scannable QR/); }); test("which codes a cut draws: the deck's (none for a card), a post's at its longest, the rail's, the corner's", () => { const timeline = [ { id: "c1", type: "clip", video: "v1", start: 12.7, end: 20 }, { id: "k1", type: "card", seconds: 3 }, { id: "s1", type: "clip", src: "media/local.mp4" }, { id: "i1", type: "image", src: "a.png", citeUrl: "https://e.test/i1" }, ]; const deckQrs = qrsOf({ render: DECK, provenance: PROV, timeline }); assert.deepEqual(deckQrs.map((q) => [q.where, q.url, q.px]), [ ["timeline c1 (the deck's QR)", "https://example.test/?v=chan%2Fv1&t=12", 150], ["timeline i1 (the deck's QR)", "https://e.test/i1", 150], ]); // A post under "archive" with no channel resolved yet: the longer of its // url and its archive page under the manifest's channel. const post = { id: "p1", attachTo: "c1", url: "https://bsky.app/profile/a/post/abc" }; const [pq] = qrsOf({ render: DECK, provenance: PROV, timeline, posts: [post] }).filter((q) => q.where.startsWith("posts")); assert.equal(pq.url, "https://example.test/?v=chan%2Fabc&vm=post"); assert.equal(pq.px, 120); // The corner code: clips with a moment, stills with a citeUrl. assert.deepEqual(qrsOf({ render: BASE, provenance: PROV, timeline }).map((q) => q.where), [ "timeline c1 (its corner QR)", "timeline i1 (its corner QR)", ]); // The rail draws one per entry, resized to rail.qrSize; `qr: false` none. const rail = qrsOf({ render: { ...BASE, rail: { qrSize: 132 } }, provenance: { ...PROV, qrLink: "https://e.test/q" }, timeline }); assert.ok(rail.every((q) => q.px === 132)); assert.deepEqual(qrsOf({ render: { ...BASE, qr: false }, provenance: PROV, timeline }), []); }); test("the threads and flips validators are part of the lint (and stay out when a build has run them)", () => { const m = { render: { ...BASE, qr: false, chrome: { flips: { pairs: [{ id: "p1", then: { entry: "nope" }, now: { entry: "c1" } }] } } }, provenance: PROV, timeline: [{ id: "c1", type: "clip", video: "v", start: 0, end: 5, thread: "t-unknown" }], }; const r = lintManifest(m, { exists: all }); assert.ok(r.errors.some((e) => /timeline\[0\] \(c1\)\.thread t-unknown is not in render\.chrome\.threads\.list/.test(e)), r.errors.join("\n")); assert.ok(r.errors.some((e) => /render\.chrome\.flips\.pairs\[0\] \(p1\)\.then\.entry nope is not in the timeline/.test(e)), r.errors.join("\n")); assert.deepEqual(lintManifest(m, { exists: all, validators: false }).errors, []); }); test("--only lints its one entry", () => { const m = { render: { ...BASE, qr: false }, provenance: PROV, timeline: [ { id: "bad", type: "image", src: "/abs/a.png", seconds: 4 }, { id: "good", type: "image", src: "b.png", seconds: 4 }, { id: "t1", type: "teaser", lines: ["z".repeat(40), "Soon"] }, ], }; assert.equal(lintManifest(m, { exists: all, only: "good" }).errors.length, 0); assert.equal(lintManifest(m, { exists: all, only: "good" }).warnings.length, 0); assert.equal(lintManifest(m, { exists: all, only: "bad" }).errors.length, 1); assert.equal(lintManifest(m, { exists: all, only: "t1" }).warnings.length, 1); }); // ---- the command lines and the build --------------------------------------------- function project(manifest) { const dir = mkdtempSync(path.join(tmpdir(), "rtv-lint-")); mkdirSync(path.join(dir, "stills"), { recursive: true }); writeFileSync(path.join(dir, "stills", "ok.png"), ""); const manifestPath = path.join(dir, "video.manifest.json"); writeFileSync(manifestPath, JSON.stringify(manifest)); return { dir, manifestPath, done: () => rmSync(dir, { recursive: true, force: true }) }; } const run = (args) => new Promise((resolve) => { execFile(process.execPath, args, { env: { ...process.env, HEAVY: "0" } }, (err, stdout, stderr) => { resolve({ code: err ? err.code : 0, stdout, stderr }); }); }); const BAD = { slug: "lint-demo", title: "Lint demo", provenance: PROV, render: { ...BASE, qr: false }, timeline: [ { id: "i1", type: "image", src: "/elsewhere/shot.png", seconds: 4 }, { id: "i2", type: "image", src: "stills/ok.png", seconds: 4 }, { id: "t1", type: "teaser", lines: ["An overline well past the house style", "Soon"] }, ], }; test("lint.mjs and build-video.mjs --lint print every warning and error and exit 1 on an error", async () => { const bad = project(BAD); const good = project({ ...BAD, timeline: BAD.timeline.slice(1) }); try { const a = await run([path.join(HERE, "lint.mjs"), bad.manifestPath]); assert.equal(a.code, 1); assert.match(a.stdout, /^warning: timeline t1\.lines\[0\] .* house style is at most 34$/m); assert.match(a.stdout, /^ERROR: timeline i1: the image "\/elsewhere\/shot\.png" must be a path relative to the manifest/m); assert.match(a.stdout, /^1 error\(s\), 1 warning\(s\)$/m); const json = await run([path.join(HERE, "lint.mjs"), bad.manifestPath, "--json"]); assert.equal(JSON.parse(json.stdout).errors.length, 1); const b = await run([path.join(HERE, "build-video.mjs"), bad.manifestPath, "--lint"]); assert.equal(b.code, 1); assert.match(b.stdout, /^lint ERROR: timeline i1: /m); assert.match(b.stdout, /^lint: timeline t1\.lines\[0\] /m); const nd = await run([path.join(HERE, "build-video.mjs"), bad.manifestPath, "--lint", "--progress", "ndjson"]); const done = nd.stdout.trim().split("\n").map((l) => JSON.parse(l)).find((e) => e.ev === "done"); assert.equal(done.lint.errors.length, 1); assert.equal(done.lint.warnings.length, 1); // A warning alone is not a failure. const c = await run([path.join(HERE, "lint.mjs"), good.manifestPath]); assert.equal(c.code, 0, c.stdout); assert.match(c.stdout, /^0 error\(s\), 1 warning\(s\)$/m); const r = await lintManifestFile(good.manifestPath); assert.deepEqual(r.errors, []); assert.equal(r.variant, "sourced"); } finally { bad.done(); good.done(); } }); test("a build refuses a lint error before it renders anything; --no-lint builds past it", async () => { const p = project(BAD); try { await assert.rejects( buildVideo({ manifestPath: p.manifestPath, opts: {} }), /^Error: lint: timeline i1: the image "\/elsewhere\/shot\.png" must be a path relative .* — --lint lists them; --no-lint builds anyway$/, ); // Past the lint the image itself is looked for -- and is not there. await assert.rejects( buildVideo({ manifestPath: p.manifestPath, opts: { noLint: true }, only: "i1" }), (err) => !/^lint:/.test(err.message) && /no image at \/elsewhere\/shot\.png/.test(err.message), ); // `--only` lints its one entry: the good still is not held up by the bad one. await assert.rejects( buildVideo({ manifestPath: p.manifestPath, opts: {}, only: "i1" }), /^Error: lint: timeline i1/, ); } finally { p.done(); } }); test("a basic-mode cut draws no code: none measured, none built from the origin -- a missing origin blocks nothing there", () => { const timeline = [{ id: "c1", type: "clip", video: "v1", start: 12.7, end: 20 }, { id: "i1", type: "image", src: "a.png", citeUrl: "https://e.test/i1" }]; const basic = { ...BASE, mode: "basic" }; assert.deepEqual(qrsOf({ render: basic, provenance: PROV, timeline }), []); assert.deepEqual(originQrsOf({ render: basic, provenance: { channelSlug: "chan" }, timeline }), []); // The same cut drawn with its corner code: the clip's code is built from the origin. assert.deepEqual(originQrsOf({ render: BASE, provenance: { channelSlug: "chan" }, timeline }).map((q) => q.where), ["timeline c1 (its corner QR)"]); });