// The source backlog (UI notes item 16): the channels a cut cites (the // archive's slug first), one channel's counts from the editor's snapshot // answer, Prioritize's focus body (the channels in focus kept; a site focus // refused), and the watch -- armed, recorded, FIRED once when every watched // channel is done, an unreadable file never overwritten. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdtemp, readFile, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { SOURCE_WATCH_FILE, armSourceWatch, backlogOf, backlogWords, citedChannels, clearSourceWatch, focusBodyFor, readSourceWatch, recordSourceCheck } from "./sourceBacklog.mjs"; const answer = ({ videos = 2751, transcribed = 1454, downloaded = 2700, undownloaded = 47, untranscribed = 1297 } = {}) => ({ ok: true, config: { name: "Some Streamer" }, priority: { tier: "normal" }, report: { generatedAt: "2026-11-01T00:00:00Z", totals: { videos, transcribed, downloaded }, buckets: { downloadedNoTranscript: untranscribed }, undownloaded }, }); test("the cited channels: siteChannel first, else the clip's channel, else the provenance's; own-media clips cite none", () => { const m = { provenance: { channelSlug: "main" }, timeline: [ { type: "clip", id: "a", video: "v1" }, { type: "clip", id: "b", video: "v2", channel: "other" }, { type: "clip", id: "c", video: "v3", channel: "rumble-local", siteChannel: "rumble-site" }, { type: "clip", id: "d", video: "v4" }, { type: "clip", id: "e", src: "media/x.mp4" }, { type: "card", id: "k" }, ], }; assert.deepEqual(citedChannels(m), [{ slug: "main", clips: 2 }, { slug: "other", clips: 1 }, { slug: "rumble-site", clips: 1 }]); }); test("one channel's backlog: listed is held + not downloaded; done when nothing is left to download or transcribe", () => { const r = backlogOf("chan", answer()); assert.equal(r.listed, 2798); assert.equal(r.held, 2751); assert.equal(r.transcribed, 1454); assert.equal(r.done, false); assert.equal(backlogWords(r), "chan 1,454/2,798 transcribed ยท 2,751 held"); const d = backlogOf("chan", answer({ undownloaded: 0, untranscribed: 0, transcribed: 2751 })); assert.equal(d.done, true); assert.match(backlogWords(d), /complete$/); assert.equal(backlogOf("chan", null).done, false, "no answer is never done"); }); test("Prioritize: the focus gains the channels, the ones in it kept; a site focus is the editor's", () => { assert.deepEqual(focusBodyFor({ kind: "none" }, ["a"]), { body: { focus: { kind: "channels", slugs: ["a"] } }, added: ["a"], already: [] }); assert.deepEqual(focusBodyFor({ kind: "channels", slugs: ["x", "a"] }, ["a", "b"]).body.focus.slugs, ["x", "a", "b"]); assert.deepEqual(focusBodyFor({ kind: "channels", slugs: ["a"] }, ["a"]).added, []); assert.throws(() => focusBodyFor({ kind: "site", siteId: "s1" }, ["a"]), /site "s1"/); assert.throws(() => focusBodyFor(null, []), /name the channels/); }); test("the watch: armed, recorded, fired ONCE when every watched channel is done; cleared; an unreadable file refused", async () => { const dir = await mkdtemp(path.join(tmpdir(), "umtool-watch-")); await armSourceWatch(dir, ["a", "b"], { now: "2026-11-01T00:00:00.000Z" }); const notDone = [backlogOf("a", answer({ undownloaded: 0, untranscribed: 0 })), backlogOf("b", answer())]; assert.deepEqual((await recordSourceCheck(dir, notDone)).fired, false); // One channel read and done is not enough: both must be. assert.deepEqual((await recordSourceCheck(dir, [notDone[0]])).fired, false); const done = [notDone[0], backlogOf("b", answer({ undownloaded: 0, untranscribed: 0 }))]; assert.equal((await recordSourceCheck(dir, done, { now: "2026-11-02T00:00:00.000Z" })).fired, true); assert.equal((await recordSourceCheck(dir, done)).fired, false, "once"); const { doc } = await readSourceWatch(dir); assert.equal(doc.watch.firedAt, "2026-11-02T00:00:00.000Z"); assert.deepEqual(doc.watch.fired.map((r) => r.slug), ["a", "b"]); await clearSourceWatch(dir); assert.equal((await readSourceWatch(dir)).doc.watch, null); assert.ok((await readSourceWatch(dir)).doc.last, "what was last seen stays"); await assert.rejects(armSourceWatch(dir, []), /no channel/); await writeFile(path.join(dir, SOURCE_WATCH_FILE), "nope"); await assert.rejects(armSourceWatch(dir, ["a"]), (e) => e.status === 409); assert.equal(await readFile(path.join(dir, SOURCE_WATCH_FILE), "utf8"), "nope"); });