// Cut integrity (UI notes 2026-10-30, #3 #5 #6 #7 #8): the cite follows the // edge, a take patch's markers follow a removal, repeatOf / ★ / walk notes // through the writer, and the QR links resolved against the site's contract. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { citeFollow, citeOffsets, citeUrlSecond, offsetFrom, parseT, playedSpan, withT } from "./citeFollow.mjs"; import { patchClipEntry, removeEntry, updateClip } from "./manifest.mjs"; import { fixStalePatchMarkers, removedAnchors, reanchorPatch, stalePatchMarkers } from "./patchMarkers.mjs"; import { archiveLinkOf, checkLinks, linkVerdicts, linksOf, runLinkCheck } from "./linkCheck.mjs"; import { linksCommand, patchMarkersCommand } from "./linksCli.mjs"; import { candidateBeats, trailerCandidates } from "./trailerCandidates.mjs"; import { decisionsFor } from "../projects/core.mjs"; import { walkProjects } from "../projects/walk.mjs"; const SITE = "https://site.test"; const url = (chan, id, t) => `${SITE}/?v=${encodeURIComponent(`${chan}/${id}`)}&t=${t}`; // ---- citeFollow.mjs --------------------------------------------------------- test("the span that plays, offsets against it, t= spellings, the same recording", () => { assert.deepEqual(playedSpan({ start: 10, end: 20 }), { from: 10, to: 20 }); assert.deepEqual(playedSpan({ start: 10, end: 20, cutStart: 12, cutEnd: 18 }), { from: 12, to: 18 }); // A lead-in of up to 5 s is inside; more is N s before; past the end is N s after. assert.equal(offsetFrom(5, { from: 10, to: 20 }), 0); assert.equal(offsetFrom(4, { from: 10, to: 20 }), -6); assert.equal(offsetFrom(21, { from: 10, to: 20 }), 1); assert.equal(parseT("123"), 123); assert.equal(parseT("123s"), 123); assert.equal(parseT("1m2s"), 62); assert.equal(parseT("1h0m5s"), 3605); assert.equal(parseT("soon"), null); assert.equal(withT("https://a.test/?v=c%2Fx&t=12", 40), "https://a.test/?v=c%2Fx&t=40"); assert.equal(withT("https://www.youtube.com/watch?v=abc&t=12s", 40.9), "https://www.youtube.com/watch?v=abc&t=40s"); assert.equal(withT("https://www.youtube.com/watch?t=1m2s&v=abc", 70), "https://www.youtube.com/watch?t=70&v=abc"); // Twitch: the corpus id is bare, the site's record is v -- the same recording either way. assert.equal(citeUrlSecond({ channel: "tw", video: "290", citeUrl: url("tw", "v290", 14) }), 14); assert.equal(citeUrlSecond({ channel: "tw", video: "290", citeUrl: url("tw", "290", 14) }), 14); assert.equal(citeUrlSecond({ video: "abc", citeUrl: "https://www.youtube.com/watch?v=abc&t=9s" }), 9); assert.equal(citeUrlSecond({ video: "abc", citeUrl: "https://youtu.be/abc?t=9" }), 9); // A mirror: another recording, another clock -- never judged. assert.equal(citeUrlSecond({ channel: "c", video: "abc", citeUrl: url("mirror", "zzz", 9) }), null); }); test("citeFollow: an edge past the cite moves it (and a same-recording QR); a lead-in or a mirror stays", () => { const e = { channel: "c", video: "v1", start: 1600, end: 1620, cite: 1523, citeUrl: url("c", "v1", 1523) }; assert.deepEqual(citeFollow(e), { cite: { from: 1523, to: 1600 }, citeUrl: { from: url("c", "v1", 1523), to: url("c", "v1", 1600) } }); assert.equal(citeFollow({ ...e, cite: 1596, citeUrl: url("c", "v1", 1596) }), null, "4 s ahead is a lead-in"); assert.deepEqual(citeFollow({ ...e, citeUrl: url("mirror", "m1", 10) }), { cite: { from: 1523, to: 1600 } }); // The cut is what plays. assert.deepEqual(citeFollow({ start: 100, end: 200, cutStart: 150, cutEnd: 160, cite: 100 })?.cite, { from: 100, to: 150 }); // After the end. assert.deepEqual(citeFollow({ start: 100, end: 110, cite: 130 })?.cite, { from: 130, to: 100 }); }); test("citeOffsets: the row's numbers and the patch that settles it", () => { const e = { channel: "c", video: "v1", start: 1600, end: 1620, cite: 1522, citeUrl: url("c", "v1", 1522) }; const off = citeOffsets(e); assert.equal(off.clockOff, -78); assert.equal(off.qrOff, -78); // The writer moves the QR with an explicit cite when they agreed: the patch is the cite alone. assert.deepEqual(off.patch, { cite: 1600 }); // QR off alone: the citeUrl is the patch. const q = citeOffsets({ ...e, cite: 1601 }); assert.deepEqual(q.patch, { citeUrl: url("c", "v1", 1601) }); assert.equal(citeOffsets({ ...e, cite: 1598, citeUrl: url("c", "v1", 1598) }), null); }); // ---- the window writer -------------------------------------------------------- const manifestOf = (timeline, extra = {}) => ({ schemaVersion: 1, slug: "cut", title: "Cut", provenance: { channelSlug: "c", siteOrigin: SITE }, render: { width: 1920, height: 1080, fps: 30 }, timeline, ...extra, }); test("the writer: an edge move carries the cite and the QR along; a lead-in is kept; an explicit cite takes the QR with it", () => { const clip = () => ({ type: "clip", id: "a", channel: "c", video: "v1", start: 1500, end: 1620, cite: 1523, citeUrl: url("c", "v1", 1523) }); let m = manifestOf([clip()]); const r = patchClipEntry(m, "a", { start: 1601.2 }); assert.equal(m.timeline[0].cite, 1601); assert.equal(m.timeline[0].citeUrl, url("c", "v1", 1601)); assert.deepEqual(r.citeFollowed, { cite: { from: 1523, to: 1601 }, citeUrl: { from: url("c", "v1", 1523), to: url("c", "v1", 1601) } }); m = manifestOf([clip()]); assert.equal(patchClipEntry(m, "a", { start: 1527 }).citeFollowed, null); assert.equal(m.timeline[0].cite, 1523, "4 s of lead-in stays"); // A cut that moves past the cite carries it too. m = manifestOf([clip()]); patchClipEntry(m, "a", { cutStart: 1560, cutEnd: 1600 }); assert.equal(m.timeline[0].cite, 1560); // An explicit cite: the QR follows because it agreed with the clock. m = manifestOf([clip()]); assert.deepEqual(patchClipEntry(m, "a", { cite: 1540 }).citeFollowed, { citeUrl: { from: url("c", "v1", 1523), to: url("c", "v1", 1540) } }); assert.equal(m.timeline[0].citeUrl, url("c", "v1", 1540)); // ...and not when it said something else. m = manifestOf([{ ...clip(), citeUrl: url("c", "v1", 1510) }]); patchClipEntry(m, "a", { cite: 1540 }); assert.equal(m.timeline[0].citeUrl, url("c", "v1", 1510)); // A patch that writes citeUrl keeps exactly what it wrote; the cite still follows. m = manifestOf([clip()]); patchClipEntry(m, "a", { start: 1601, citeUrl: url("c", "v1", 1400) }); assert.equal(m.timeline[0].citeUrl, url("c", "v1", 1400)); assert.equal(m.timeline[0].cite, 1601); // No cite: the clock is the start already; a QR left behind still follows. m = manifestOf([{ type: "clip", id: "b", channel: "c", video: "v2", start: 10, end: 90, citeUrl: url("c", "v2", 10) }]); patchClipEntry(m, "b", { start: 40 }); assert.equal("cite" in m.timeline[0], false); assert.equal(m.timeline[0].citeUrl, url("c", "v2", 40)); }); test("the writer: walkNote and star -- set, cleared, refused", () => { const m = manifestOf([{ type: "clip", id: "a", video: "v1", start: 1, end: 9 }]); patchClipEntry(m, "a", { walkNote: " THIS IS GOLD ", star: true }); assert.equal(m.timeline[0].walkNote, "THIS IS GOLD"); assert.equal(m.timeline[0].star, true); assert.equal("correction" in m.timeline[0], false, "a walk note is not a correction"); assert.equal("verdict" in m.timeline[0], false, "nor a verdict"); patchClipEntry(m, "a", { walkNote: "", star: false }); assert.equal("walkNote" in m.timeline[0], false); assert.equal("star" in m.timeline[0], false); patchClipEntry(m, "a", { star: "true" }); assert.equal(m.timeline[0].star, true); assert.throws(() => patchClipEntry(m, "a", { star: "maybe" }), /star must be true or false/); }); // ---- take patches follow a removal ------------------------------------------ test("removedAnchors / reanchorPatch: the same boundary, or dropped; a patch with its own timeline is left", () => { const before = [{ type: "clip", id: "a" }, { type: "card", id: "k" }, { type: "clip", id: "b" }, { type: "clip", id: "c" }]; const after = [{ type: "clip", id: "a" }, { type: "card", id: "k" }, { type: "clip", id: "c" }]; assert.deepEqual([...removedAnchors(before, after)], [["b", "a"]]); assert.deepEqual([...removedAnchors(before, before.slice(1))], [["a", null]]); const patch = { render: { chrome: { deck: { markers: [{ after: "b", label: "Sex appeal" }, { after: "c" }] } } } }; assert.deepEqual(reanchorPatch(patch, new Map([["b", "a"]])), ['deck marker 0 ("Sex appeal") moved from after b to after a (the same boundary)']); assert.equal(patch.render.chrome.deck.markers[0].after, "a"); const own = { timeline: [], render: { chrome: { deck: { markers: [{ after: "b" }] } } } }; assert.deepEqual(reanchorPatch(own, new Map([["b", "a"]])), []); }); async function project(timeline, { files = {}, render = {} } = {}) { const root = await mkdtemp(path.join(tmpdir(), "cut-integrity-")); const channelsDir = path.join(root, "channels"); const reportsRoot = path.join(root, "reports"); for (const v of ["v1", "v2"]) { const vdir = path.join(channelsDir, "c", "data", v); await mkdir(vdir, { recursive: true }); const cues = Array.from({ length: 2000 }, (_, i) => ({ start: i, end: i + 1, text: `Line ${i}.` })); await writeFile(path.join(vdir, "transcript.cues.json"), JSON.stringify({ cues })); } const dir = path.join(reportsRoot, "cut"); await mkdir(dir, { recursive: true }); const m = manifestOf( timeline.map((e) => ({ type: "clip", channel: "c", video: "v1", lock: true, ...e })), { render: { width: 1920, height: 1080, fps: 30, ...render } }, ); m.provenance.channelsDir = path.relative(dir, channelsDir); await writeFile(path.join(dir, "video.manifest.json"), `${JSON.stringify(m, null, 2)}\n`); for (const [name, doc] of Object.entries(files)) await writeFile(path.join(dir, name), typeof doc === "string" ? doc : `${JSON.stringify(doc, null, 2)}\n`); const decisions = async () => { const [ref] = (await walkProjects(reportsRoot)).filter((p) => p.id === "cut"); return decisionsFor(ref, reportsRoot); }; return { root, reportsRoot, dir, decisions, done: () => rm(root, { recursive: true, force: true }) }; } const markersRender = (markers) => ({ chrome: { engine: "hyperframes", layout: "deck", deck: { markers } } }); test("removing a clip re-anchors the take patches beside the manifest; the check names what is still stale", async () => { const p = await project( [{ id: "a", start: 10, end: 20 }, { id: "b", start: 30, end: 40 }, { id: "c", start: 50, end: 60 }], { render: markersRender([{ after: "b", label: "Two" }]), files: { "draft-1.patch.json": { render: { chrome: { deck: { markers: [{ after: "b", label: "Two" }, { after: "c", label: "Three" }] } } } }, "own.patch.json": { timeline: [{ type: "clip", id: "x", video: "v1", start: 1, end: 2 }], render: { chrome: { deck: { markers: [{ after: "b" }] } } } }, "hand.patch.json": { render: { chrome: { deck: { markers: [{ after: "gone", label: "Hand" }] } } } }, }, }, ); try { const res = await removeEntry(p.dir, "b"); assert.deepEqual(res.patches, [{ file: "draft-1.patch.json", notes: ['deck marker 0 ("Two") moved from after b to after a (the same boundary)'] }]); const patch = JSON.parse(await readFile(path.join(p.dir, "draft-1.patch.json"), "utf8")); assert.deepEqual(patch.render.chrome.deck.markers, [{ after: "a", label: "Two" }, { after: "c", label: "Three" }]); // A patch with a timeline of its own names its own clips: untouched. const own = JSON.parse(await readFile(path.join(p.dir, "own.patch.json"), "utf8")); assert.equal(own.render.chrome.deck.markers[0].after, "b"); // The manifest's own markers moved the same way. const m = JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")); assert.equal(m.render.chrome.deck.markers[0].after, "a"); // What is still stale: the hand-written one. assert.deepEqual(await stalePatchMarkers(p.dir, m), [{ file: "hand.patch.json", markers: [{ index: 0, label: "Hand", after: "gone" }] }]); const rows = (await p.decisions()).filter((d) => d.kind === "patch-markers-stale"); assert.equal(rows.length, 1); assert.equal(rows[0].target, "hand.patch.json"); assert.equal(rows[0].severity, "open"); assert.match(rows[0].why, /"Hand" after gone — a take made with --patch hand.patch.json is refused/); } finally { await p.done(); } }); test("Take the cut's markers: a stale patch marker takes the cut's marker of the same label; an unmatched one is left and named", async () => { const p = await project( [{ id: "a", start: 10, end: 20 }, { id: "b", start: 30, end: 40 }], { render: markersRender([{ after: "a", label: "Sex appeal" }]), files: { "draft-1.patch.json": { render: { chrome: { deck: { markers: [{ after: "ow2b", label: "Sex appeal", color: "#e070d0" }, { after: "zz", label: "Nobody" }] } } } } }, }, ); try { const rows = (await p.decisions()).filter((d) => d.kind === "patch-markers-stale"); assert.deepEqual(rows[0].settle, { kind: "patch-markers", file: "draft-1.patch.json", label: "Take the cut's markers" }); assert.match(rows[0].fix, /^umtool patch-markers cut draft-1.patch.json --fix/); const lines = []; const code = await patchMarkersCommand(["cut", "--fix"], { root: p.reportsRoot, out: (s) => lines.push(s), err: (s) => lines.push(s) }); assert.equal(code, 1, "one is left"); assert.deepEqual(lines, [ `draft-1.patch.json: deck marker 0 ("Sex appeal") moved from after ow2b to after a, where the cut's "Sex appeal" is`, `draft-1.patch.json: "Nobody" is after zz, which is not in the cut`, ]); const patch = JSON.parse(await readFile(path.join(p.dir, "draft-1.patch.json"), "utf8")); assert.deepEqual(patch.render.chrome.deck.markers[0], { after: "a", label: "Sex appeal", color: "#e070d0" }); const m = JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")); assert.deepEqual((await fixStalePatchMarkers(p.dir, m, "draft-1.patch.json")).left, [{ index: 1, label: "Nobody", after: "zz" }]); await assert.rejects(fixStalePatchMarkers(p.dir, m, "../video.manifest.json"), /not a take patch/); } finally { await p.done(); } }); // ---- the check rows ------------------------------------------------------------ test("clip-cite-off: the clock and the QR N s before the clip, with the settle and the fix", async () => { const p = await project([ { id: "ag0", start: 1600, end: 1610, cite: 1522, citeUrl: url("c", "v1", 1522) }, { id: "ok1", video: "v2", start: 100, end: 110, cite: 97, citeUrl: url("c", "v2", 97) }, ]); try { const rows = (await p.decisions()).filter((d) => d.kind === "clip-cite-off"); assert.equal(rows.length, 1); const [r] = rows; assert.equal(r.target, "ag0"); assert.equal(r.severity, "open"); assert.equal(r.why, "the clock and the QR point 78 s before the clip (cite 25:22, it plays 26:40–26:50)"); assert.deepEqual(r.settle, { kind: "patch", clip: "ag0", patch: { cite: 1600 }, label: "Move to 26:40" }); assert.equal(r.fix, "umtool window cut ag0 --cite 1600"); // Settling it through the writer clears the row, and the QR followed. const res = await updateClip(p.dir, "ag0", r.settle.patch); assert.equal(res.entry.citeUrl, url("c", "v1", 1600)); assert.equal((await p.decisions()).filter((d) => d.kind === "clip-cite-off").length, 0); } finally { await p.done(); } }); test("window-overlap offers It's deliberate: the later clip repeats the earlier; the writer clears the row", async () => { const p = await project([ { id: "tr3", start: 100, end: 140, cite: 100 }, { id: "x", video: "v2", start: 10, end: 20, cite: 10 }, { id: "tr3z", start: 120, end: 130, cite: 120 }, ]); try { const rows = (await p.decisions()).filter((d) => d.kind === "window-overlap"); assert.equal(rows.length, 1); assert.deepEqual(rows[0].settle, { kind: "patch", clip: "tr3z", patch: { repeatOf: "tr3" }, label: "It's deliberate: tr3z repeats tr3" }); assert.match(rows[0].fix, /^umtool window cut tr3z --repeat-of tr3/); await updateClip(p.dir, "tr3z", rows[0].settle.patch); assert.equal((await p.decisions()).filter((d) => d.kind === "window-overlap").length, 0); } finally { await p.done(); } }); // ---- the link check -------------------------------------------------------------- /** A published site: corpus.json, one channel's manifest and pages, counting what is asked. */ function fakeSite({ channels = { c: { v1: 0, v2: 1 }, tw: { v290: 0 } }, down = false, emptyPage = null } = {}) { const asked = []; const pages = {}; for (const [chan, ids] of Object.entries(channels)) { for (const [id, n] of Object.entries(ids)) (pages[`${chan}/${n}`] ??= []).push({ id, title: id }); } const fetchImpl = async (u) => { asked.push(String(u)); if (down) throw new TypeError("fetch failed", { cause: new Error("getaddrinfo ENOTFOUND site.test") }); const p = new URL(String(u)).pathname; const json = (doc) => ({ ok: true, status: 200, json: async () => doc }); if (p === "/corpus.json") { return json({ spec: 5, generatedAt: "2026-10-09T10:00:00.000Z", site: { title: "Site" }, channels: Object.keys(channels).map((slug) => ({ slug, manifests: { transcripts: `${SITE}/transcripts/${slug}/manifest.json` } })), }); } const m = /^\/transcripts\/([^/]+)\/(manifest\.json|page-(\d{4})\.json)$/.exec(p); if (m && m[2] === "manifest.json") return json({ pageCount: 2, slugToPage: Object.fromEntries(Object.entries(channels[m[1]] ?? {})) }); if (m && m[3]) { const key = `${m[1]}/${Number(m[3])}`; // A deploy whose shard lacks a record its slugToPage promises. if (emptyPage === key) return json([]); return json(pages[key] ?? []); } return { ok: false, status: 404, json: async () => ({}) }; }; return { fetchImpl, asked }; } test("archiveLinkOf / linksOf: the build's own QR links; a platform link is not an archive link", () => { assert.deepEqual(archiveLinkOf(url("tw", "v290", 14)), { origin: SITE, channel: "tw", id: "v290", t: "14" }); assert.equal(archiveLinkOf("https://www.youtube.com/watch?v=abc&t=9s"), null); const m = manifestOf([ { type: "clip", id: "a", video: "v1", start: 10, end: 20 }, { type: "clip", id: "b", video: "v2", start: 10, end: 20, citeUrl: "https://www.youtube.com/watch?v=v2&t=10s" }, ]); assert.deepEqual(linksOf(m), [ { clip: "a", url: `${SITE}/?v=${encodeURIComponent("c/v1")}&t=10` }, { clip: "b", url: "https://www.youtube.com/watch?v=v2&t=10s" }, ]); }); test("checkLinks: open, a channel not on the site, the bare Twitch id (with its fix), a stale cached shard, one request per URL", async () => { const cacheDir = await mkdtemp(path.join(tmpdir(), "links-cache-")); try { const site = fakeSite(); const links = [ { clip: "a", url: url("c", "v1", 10) }, { clip: "a2", url: url("c", "v1", 30) }, { clip: "b", url: url("c", "v2", 10) }, { clip: "q01", url: url("tw", "290", 14538) }, { clip: "n1", url: url("nochan", "x", 1) }, { clip: "gone", url: url("c", "v9", 1) }, { clip: "yt", url: "https://www.youtube.com/watch?v=abc&t=9s" }, ]; const doc = await checkLinks(links, { fetchImpl: site.fetchImpl, cacheDir, now: () => new Date("2026-11-01T01:00:00Z"), gapMs: 0 }); const by = Object.fromEntries(doc.links.map((l) => [l.clip, l])); assert.equal(by.a.state, "ok"); assert.equal(by.a2.state, "ok"); assert.equal(by.b.state, "ok"); assert.equal(by.q01.state, "dead"); assert.equal(by.q01.why, `"290" is not a record on site.test; the site's record is "v290"`); assert.equal(by.q01.fix, url("tw", "v290", 14538)); assert.equal(by.n1.state, "dead"); assert.match(by.n1.why, /the channel "nochan" is not on site\.test \(its deploy of 2026-10-09\)/); assert.equal(by.gone.state, "dead"); assert.match(by.gone.why, /"v9" is not published in c on site\.test \(deployed 2026-10-09\)/); assert.equal(by.yt.state, "direct"); assert.equal(doc.origins[SITE].state, "ok"); // Polite: corpus.json once, each manifest once, each page once (+1 for the stale one). const count = (s) => site.asked.filter((u) => u.endsWith(s)).length; assert.equal(count("/corpus.json"), 1); assert.equal(count("/c/manifest.json"), 1); assert.equal(count("/c/page-0000.json"), 1); assert.equal(count("/c/page-0001.json"), 1); // A second check reads shards from the cache: only the contract is asked again. const again = fakeSite(); await checkLinks(links.slice(0, 3), { fetchImpl: again.fetchImpl, cacheDir, gapMs: 0 }); assert.deepEqual(again.asked.map((u) => new URL(u).pathname), ["/corpus.json", "/transcripts/c/manifest.json"]); } finally { await rm(cacheDir, { recursive: true, force: true }); } }); test("checkLinks: a shard that lacks its record is inconsistent; a cached copy older than the deploy is read again, once", async () => { const cacheDir = await mkdtemp(path.join(tmpdir(), "links-cache-")); try { const broken = fakeSite({ emptyPage: "c/1" }); const one = await checkLinks([{ clip: "b", url: url("c", "v2", 10) }], { fetchImpl: broken.fetchImpl, cacheDir, gapMs: 0 }); assert.equal(one.links[0].state, "dead"); assert.match(one.links[0].why, /slugToPage puts "v2" on page 1, but no record there has that id/); // Fetched from the network this check: never asked twice in one check. assert.equal(broken.asked.filter((u) => u.endsWith("/page-0001.json")).length, 1); // Redeployed: the cached shard misses, so it is read again -- once. const fixed = fakeSite(); const two = await checkLinks([{ clip: "b", url: url("c", "v2", 10) }], { fetchImpl: fixed.fetchImpl, cacheDir, gapMs: 0 }); assert.equal(two.links[0].state, "ok"); assert.deepEqual(fixed.asked.map((u) => new URL(u).pathname), ["/corpus.json", "/transcripts/c/manifest.json", "/transcripts/c/page-0001.json"]); } finally { await rm(cacheDir, { recursive: true, force: true }); } }); test("checkLinks offline: every link unverified, with couldn't reach; the rows say so, and a dead link blocks", async () => { const cacheDir = await mkdtemp(path.join(tmpdir(), "links-cache-")); const p = await project([ { id: "a", start: 10, end: 20, cite: 10 }, { id: "q01", start: 30, end: 40, cite: 30, citeUrl: url("tw", "290", 30) }, ]); try { // Never checked. let rows = (await p.decisions()).filter((d) => d.kind.startsWith("link")); assert.deepEqual(rows.map((d) => [d.kind, d.severity, d.why]), [["links-unchecked", "info", "2 archive links never checked against the sites they open"]]); assert.deepEqual(rows[0].settle, { kind: "check-links", label: "Check links" }); assert.equal(rows[0].fix, "umtool links cut"); const m = JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")); const off = await runLinkCheck(p.dir, m, { fetchImpl: fakeSite({ down: true }).fetchImpl, cacheDir, gapMs: 0 }); assert.ok(off.links.every((l) => l.state === "unverified")); assert.equal(off.origins[SITE].state, "unreachable"); rows = (await p.decisions()).filter((d) => d.kind.startsWith("link")); assert.equal(rows.length, 1); assert.equal(rows[0].kind, "links-unverified"); assert.equal(rows[0].severity, "info"); assert.match(rows[0].why, /^2 links not verified on .* — couldn't reach site\.test \(fetch failed \(getaddrinfo ENOTFOUND site\.test\)\)$/); // Online: q01's bare Twitch id is dead, and that blocks. await runLinkCheck(p.dir, m, { fetchImpl: fakeSite().fetchImpl, cacheDir, gapMs: 0 }); rows = (await p.decisions()).filter((d) => d.kind.startsWith("link")); assert.deepEqual(rows.map((d) => [d.kind, d.severity, d.target]), [["link-dead", "blocking", "site.test"]]); assert.match(rows[0].why, /^1 of 2 QR links don't open on site\.test \(deployed 2026-10-09\): q01 — q01: "290" is not a record on site\.test; the site's record is "v290"\. Every clip with a fix: `umtool links cut --fix`$/); assert.deepEqual(rows[0].clips, ["q01"]); assert.deepEqual(rows[0].settle, { kind: "check-links", fix: true, label: "Fix 1 link (the site's v)" }); assert.equal(rows[0].fix, "umtool links cut --fix"); // A link changed since the check is unchecked, not judged by the old answer. await updateClip(p.dir, "a", { citeUrl: url("c", "v2", 10) }); const v = linkVerdicts(JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")), JSON.parse(await readFile(path.join(p.dir, "out", "links.json"), "utf8"))); assert.deepEqual(v.unchecked.map((l) => l.clip), ["a"]); } finally { await p.done(); await rm(cacheDir, { recursive: true, force: true }); } }); test("umtool links --fix: the dead link with a known fix rewritten through the writer, then checked again", async () => { const cacheDir = await mkdtemp(path.join(tmpdir(), "links-cache-")); const p = await project([{ id: "q01", start: 30, end: 40, cite: 30, citeUrl: url("tw", "290", 30) }]); try { const lines = []; const code = await linksCommand(["cut", "--fix"], { root: p.reportsRoot, out: (s) => lines.push(s), err: (s) => lines.push(s), fetchImpl: fakeSite().fetchImpl, cacheDir, gapMs: 0 }); assert.equal(code, 0, lines.join("\n")); assert.match(lines.find((l) => l.startsWith("fixed ")) ?? "", /^fixed q01: .*%2F290&t=30 -> .*%2Fv290&t=30$/); const m = JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")); assert.equal(m.timeline[0].citeUrl, url("tw", "v290", 30)); assert.equal((await p.decisions()).filter((d) => d.kind.startsWith("link")).length, 0); } finally { await p.done(); await rm(cacheDir, { recursive: true, force: true }); } }); test("checkLinks paces one host: at least the gap between two requests, nothing waited on a cache hit", async () => { const cacheDir = await mkdtemp(path.join(tmpdir(), "links-cache-")); try { const waits = []; let clock = 0; const realNow = Date.now; Date.now = () => clock; try { const sleep = async (ms) => { waits.push(ms); clock += ms; }; await checkLinks([{ clip: "a", url: url("c", "v1", 10) }, { clip: "b", url: url("c", "v2", 10) }], { fetchImpl: fakeSite().fetchImpl, cacheDir, gapMs: 3000, sleep }); // corpus, manifest, page 0, page 1: three gaps, each ≥ 3 s and < 3.75 s (upward jitter only). assert.equal(waits.length, 3); for (const w of waits) assert.ok(w >= 3000 && w < 3750, String(w)); waits.length = 0; await checkLinks([{ clip: "a", url: url("c", "v1", 10) }], { fetchImpl: fakeSite().fetchImpl, cacheDir, gapMs: 3000, sleep }); assert.equal(waits.length, 1, "corpus.json and the manifest: one gap; the shard is a cache hit"); } finally { Date.now = realNow; } } finally { await rm(cacheDir, { recursive: true, force: true }); } }); // ---- ★ trailer candidates ------------------------------------------------------- test("trailerCandidates: the starred clips in the cut's order, over what each plays; a rejected one is left out", () => { const m = manifestOf([ { type: "clip", id: "j01", video: "v", start: 3335.1, end: 3337.14, star: true, quote: "anime girl", walkNote: "GOLD" }, { type: "clip", id: "b01", video: "v", start: 10, end: 30 }, { type: "clip", id: "tr3", video: "v", start: 2980, end: 3020, cutStart: 2988.36, cutEnd: 3011.2, star: true }, { type: "clip", id: "z", video: "v", start: 1, end: 2, star: true, verdict: "rejected", reason: "weak" }, ]); assert.deepEqual(trailerCandidates(m).map((b) => b.id), ["j01", "tr3"]); assert.deepEqual(candidateBeats(m), [{ id: "j01", start: 3335.1, end: 3337.14 }, { id: "tr3", start: 2988.36, end: 3011.2 }]); assert.equal(trailerCandidates(m)[0].walkNote, "GOLD"); });