import { test } from "node:test"; import assert from "node:assert/strict"; import { CHANNEL_TIERS, PRIORITY_OPERATIONS, autoPauseForMedia, autoPauseReasonOf, autoPausedSlugs, clearAutoPause, restoreAfterMedia, PRIO_CATCH_ALL_ID, STORED_CHANNEL_TIERS, channelPriorityFromLegacy, channelsForOperation, compileLaneRoot, compileLanes, hasCompiledLaneRoots, renameChannelInPriority, defaultChannelPriority, effectiveTier, focusSummary, isChannelPaused, isPriorityOperation, overridesOf, parsePrioLeafId, prioGroupId, prioLeafId, rankOf, resolveFocusSlugs, sanitizeChannelPriority, tierOf, tierOrder, type ChannelPriority, } from "./channelPriority"; import { LANES, isGroup } from "./autoQueueTypes"; import type { AutoQueueGroup, AutoQueueNode } from "./autoQueueTypes"; // Run with: // pnpm --filter yt-dlp-transcript-common test // // The compiler's contract in one line: a model compiles to the trees the runner // already walks, and `sanitizeAutoQueue` is the identity on what it produces. // // THE HALF THAT NEEDS THE ENGINE IS NOT HERE. ../architecture.test.ts forbids // `lib/ -> jobs/` and its ALLOWED list may only SHRINK, and it scans test files // exactly like any other, so anything asserted THROUGH `sanitizeAutoQueue` or // `buildPendingByLeaf` sits in jobs/ — the same reason `laneMigration.test.ts` // does. `jobs/channelPrioritySanitize.test.ts` holds the sanitizer round-trip; // the focus/hold/release proof through the real engine is S1's // `jobs/channelPriorityCompile.test.ts`. // --------------------------------------------------------------------------- // 1. sanitizeChannelPriority // --------------------------------------------------------------------------- test("an absent document is the empty one", () => { const empty = { focus: { kind: "none" }, channels: {} }; assert.deepEqual(sanitizeChannelPriority(undefined), empty); assert.deepEqual(sanitizeChannelPriority(null), empty); assert.deepEqual(sanitizeChannelPriority("nonsense"), empty); assert.deepEqual(sanitizeChannelPriority([1, 2]), empty); assert.deepEqual(defaultChannelPriority(), empty); }); test("an unknown tier reads as normal, and a bare normal entry is dropped", () => { const out = sanitizeChannelPriority({ channels: { a: { tier: "urgent" }, b: { tier: "normal" }, // "focus" is a compiled POSITION, never a stored tier. c: { tier: "focus" }, d: { tier: "low" }, e: { tier: "paused" }, }, }); // a/b/c all mean "normal, unranked, no overrides", which is the default. assert.deepEqual(out.channels, { d: { tier: "low" }, e: { tier: "paused" }, }); }); test("an unknown tier with a rank keeps the rank at the normal tier", () => { const out = sanitizeChannelPriority({ channels: { a: { tier: "urgent", rank: 3 } }, }); assert.deepEqual(out.channels, { a: { tier: "normal", rank: 3 } }); }); test("a junk rank is dropped and a fractional one is floored", () => { const out = sanitizeChannelPriority({ channels: { a: { tier: "low", rank: "2" }, b: { tier: "low", rank: Number.NaN }, c: { tier: "low", rank: Number.POSITIVE_INFINITY }, d: { tier: "low", rank: 2.9 }, e: { tier: "low", rank: 0 }, }, }); assert.deepEqual(out.channels, { a: { tier: "low" }, b: { tier: "low" }, c: { tier: "low" }, d: { tier: "low", rank: 2 }, e: { tier: "low", rank: 0 }, }); }); test("a rank on a channel paused everywhere is dropped; a sync-only one keeps it", () => { const out = sanitizeChannelPriority({ channels: { dead: { tier: "paused", rank: 4 }, synconly: { tier: "paused", rank: 4, overrides: { sync: "normal" } }, }, }); assert.deepEqual(out.channels.dead, { tier: "paused" }); assert.deepEqual(out.channels.synconly, { tier: "paused", rank: 4, overrides: { sync: "normal" }, }); }); test("blank slugs are dropped and keys are trimmed and sorted", () => { const out = sanitizeChannelPriority({ channels: { " ": { tier: "paused" }, "": { tier: "paused" }, " zed ": { tier: "paused" }, alpha: { tier: "low" }, }, }); assert.deepEqual(Object.keys(out.channels), ["alpha", "zed"]); }); test("a focus is sanitized: blank/dup slugs dropped, empty collapses to none", () => { assert.deepEqual(sanitizeChannelPriority({ focus: { kind: "site" } }).focus, { kind: "none", }); assert.deepEqual( sanitizeChannelPriority({ focus: { kind: "site", siteId: " " } }).focus, { kind: "none" }, ); assert.deepEqual( sanitizeChannelPriority({ focus: { kind: "site", siteId: " jeralyzer " } }) .focus, { kind: "site", siteId: "jeralyzer" }, ); assert.deepEqual( sanitizeChannelPriority({ focus: { kind: "channels", slugs: [" a ", "a", "", 7, "b"] }, }).focus, { kind: "channels", slugs: ["a", "b"] }, ); assert.deepEqual( sanitizeChannelPriority({ focus: { kind: "channels", slugs: [] } }).focus, { kind: "none" }, ); assert.deepEqual( sanitizeChannelPriority({ focus: { kind: "elsewhere" } }).focus, { kind: "none" }, ); }); test("sanitizeChannelPriority is idempotent over its own output", () => { const once = sanitizeChannelPriority({ focus: { kind: "channels", slugs: [" a ", "a", "b"] }, channels: { " zed ": { tier: "paused", overrides: { sync: "normal" } }, alpha: { tier: "bogus", rank: 1.7 }, beta: { tier: "normal" }, gamma: { tier: "low", rank: -2, overrides: { download: "paused" } }, }, }); assert.deepEqual(sanitizeChannelPriority(once), once); assert.deepEqual( sanitizeChannelPriority(JSON.parse(JSON.stringify(once))), once, ); }); // --- the override map's normalisation -------------------------------------- test("an unknown override key is dropped and an unknown value falls back to the base", () => { const out = sanitizeChannelPriority({ channels: { a: { tier: "paused", overrides: { sync: "normal", // not an operation publish: "normal", // not a tier — DROPPED, not coerced, so the base `paused` stands download: "urgent", // "focus" is not storable anywhere digest: "focus", }, }, }, }); assert.deepEqual(out.channels.a, { tier: "paused", overrides: { sync: "normal" }, }); const model = out; assert.equal(effectiveTier(model, "a", "download"), "paused"); assert.equal(effectiveTier(model, "a", "digest"), "paused"); assert.equal(effectiveTier(model, "a", "sync"), "normal"); }); test("an override equal to the base is normalised away, and an empty map with it", () => { const out = sanitizeChannelPriority({ channels: { a: { tier: "low", overrides: { sync: "low", download: "normal" } }, b: { tier: "normal", overrides: { sync: "normal" } }, c: { tier: "normal", overrides: {} }, d: { tier: "normal", overrides: [] }, e: { tier: "normal", overrides: "nope" }, }, }); assert.deepEqual(out.channels, { a: { tier: "low", overrides: { download: "normal" } }, }); }); test("override keys are emitted in PRIORITY_OPERATIONS order", () => { const out = sanitizeChannelPriority({ channels: { a: { tier: "normal", overrides: { backfill: "paused", sync: "paused", download: "low" }, }, }, }); assert.deepEqual(Object.keys(out.channels.a.overrides ?? {}), [ "sync", "download", "backfill", ]); }); test("both presets are expressible", () => { // "everything but sync" — the lossless reading of excludeFromSync. const notSync = sanitizeChannelPriority({ channels: { a: { tier: "normal", overrides: { sync: "paused" } } }, }); assert.equal(isChannelPaused(notSync, "a", "sync"), true); for (const lane of LANES) { assert.equal(isChannelPaused(notSync, "a", lane), false, lane); } // "sync only" — its inverse. const syncOnly = sanitizeChannelPriority({ channels: { a: { tier: "paused", overrides: { sync: "normal" } } }, }); assert.equal(isChannelPaused(syncOnly, "a", "sync"), false); for (const lane of LANES) { assert.equal(isChannelPaused(syncOnly, "a", lane), true, lane); } }); // --- readers --------------------------------------------------------------- test("tierOf / rankOf / isChannelPaused read the document, defaulting to normal", () => { const model = sanitizeChannelPriority({ channels: { a: { tier: "paused" }, b: { tier: "low", rank: 4 } }, }); assert.equal(tierOf(model, "a"), "paused"); assert.equal(tierOf(model, "b"), "low"); assert.equal(tierOf(model, "nobody"), "normal"); assert.equal(rankOf(model, "b"), 4); assert.equal(rankOf(model, "a"), null); assert.equal(isChannelPaused(model, "a"), true); assert.equal(isChannelPaused(model, "b"), false); assert.equal(isChannelPaused(model, "nobody"), false); }); test("effectiveTier is the override or the base; tierOf stays the base", () => { const model = sanitizeChannelPriority({ channels: { omnibased: { tier: "normal", rank: 2, overrides: { download: "paused" } }, }, }); assert.equal(tierOf(model, "omnibased"), "normal"); assert.equal(effectiveTier(model, "omnibased", "download"), "paused"); assert.equal(effectiveTier(model, "omnibased", "sync"), "normal"); assert.equal(effectiveTier(model, "omnibased", "transcription"), "normal"); assert.equal(effectiveTier(model, "nobody", "download"), "normal"); assert.deepEqual(overridesOf(model, "omnibased"), { download: "paused" }); assert.deepEqual(overridesOf(model, "nobody"), {}); }); test("channelsForOperation filters per operation, input order preserved", () => { const model = sanitizeChannelPriority({ channels: { nosync: { tier: "normal", overrides: { sync: "paused" } }, nodl: { tier: "normal", overrides: { download: "paused" } }, dead: { tier: "paused" }, }, }); const all = ["nosync", "nodl", "dead", "plain"]; assert.deepEqual(channelsForOperation(model, all, "sync"), ["nodl", "plain"]); assert.deepEqual(channelsForOperation(model, all, "download"), [ "nosync", "plain", ]); assert.deepEqual(channelsForOperation(model, all, "digest"), [ "nosync", "nodl", "plain", ]); }); test("the vocabularies agree and tierOrder sorts focus < normal < low < paused", () => { assert.deepEqual([...CHANNEL_TIERS], ["focus", "normal", "low", "paused"]); assert.deepEqual( [...STORED_CHANNEL_TIERS], CHANNEL_TIERS.filter((t) => t !== "focus"), ); assert.deepEqual([...PRIORITY_OPERATIONS], ["sync", ...LANES]); assert.ok(isPriorityOperation("sync")); assert.ok(isPriorityOperation("backfill")); assert.ok(!isPriorityOperation("publish")); assert.deepEqual( [...CHANNEL_TIERS].sort((a, b) => tierOrder(a) - tierOrder(b)), ["focus", "normal", "low", "paused"], ); }); // --------------------------------------------------------------------------- // 2. resolveFocusSlugs // --------------------------------------------------------------------------- const SITES = { jeralyzer: ["the-quartering", "quartering-live", "community-notes"], testsite: ["slow-a"], empty: [], }; test("a site focus resolves through that site's channels[]", () => { const model = sanitizeChannelPriority({ focus: { kind: "site", siteId: "jeralyzer" }, }); assert.deepEqual(resolveFocusSlugs(model, SITES), [ "the-quartering", "quartering-live", "community-notes", ]); }); test("an unknown siteId resolves to [] — a typo must not hold the corpus", () => { const model = sanitizeChannelPriority({ focus: { kind: "site", siteId: "jerlayzer" }, }); assert.deepEqual(resolveFocusSlugs(model, SITES), []); // And therefore compiles NO focus group at all. const root = compileLaneRoot("download", model, ["a", "b"], []); assert.deepEqual( root.children.map((c) => c.id), [prioGroupId("normal"), PRIO_CATCH_ALL_ID], ); }); test("a site with no channels, and a focus of none, both resolve to []", () => { assert.deepEqual( resolveFocusSlugs( sanitizeChannelPriority({ focus: { kind: "site", siteId: "empty" } }), SITES, ), [], ); assert.deepEqual(resolveFocusSlugs(defaultChannelPriority(), SITES), []); }); test("a channel focus keeps its order and drops slugs that do not exist", () => { const model = sanitizeChannelPriority({ focus: { kind: "channels", slugs: ["zed", "alpha", "ghost"] }, }); assert.deepEqual(resolveFocusSlugs(model, SITES, ["alpha", "zed", "beta"]), [ "zed", "alpha", ]); // No `known` list = no existence filter. assert.deepEqual(resolveFocusSlugs(model, SITES), ["zed", "alpha", "ghost"]); }); test("a site focus is filtered by the known channel list too", () => { const model = sanitizeChannelPriority({ focus: { kind: "site", siteId: "jeralyzer" }, }); assert.deepEqual( resolveFocusSlugs(model, SITES, ["community-notes", "the-quartering"]), ["the-quartering", "community-notes"], ); }); // --------------------------------------------------------------------------- // 3. compileLaneRoot / compileLanes // --------------------------------------------------------------------------- function leafSlugs(node: AutoQueueNode): string[] { if (!isGroup(node)) return node.match.value ? [node.match.value] : []; return node.children.flatMap(leafSlugs); } function groupNamed(root: AutoQueueGroup, id: string): AutoQueueGroup { const found = root.children.find((c) => c.id === id); assert.ok(found && isGroup(found), `no group ${id} in ${root.id}`); return found; } test("the compiled tree is focus, normal, low, then a catch-all last", () => { const model = sanitizeChannelPriority({ channels: { low1: { tier: "low" }, paused1: { tier: "paused" } }, }); const root = compileLaneRoot( "transcription", model, ["norm1", "low1", "paused1", "foc1"], ["foc1"], ); assert.equal(root.id, "transcription-root"); assert.equal(root.mode, "strict"); assert.deepEqual( root.children.map((c) => c.id), [ prioGroupId("focus"), prioGroupId("normal"), prioGroupId("low"), PRIO_CATCH_ALL_ID, ], ); const last = root.children[root.children.length - 1]; assert.ok(!isGroup(last)); assert.deepEqual(last.match, { type: "all" }); // Paused never appears, at any depth. assert.ok(!leafSlugs(root).includes("paused1")); assert.deepEqual(leafSlugs(root), ["foc1", "norm1", "low1"]); }); test("an empty tier emits no group, and an empty focus emits no focus group", () => { const model = defaultChannelPriority(); const root = compileLaneRoot("download", model, ["a", "b"], []); assert.deepEqual( root.children.map((c) => c.id), [prioGroupId("normal"), PRIO_CATCH_ALL_ID], ); // Everything paused: no group at all, just the net. const allPaused = sanitizeChannelPriority({ channels: { a: { tier: "paused" }, b: { tier: "paused" } }, }); assert.deepEqual( compileLaneRoot("download", allPaused, ["a", "b"], ["a"]).children.map( (c) => c.id, ), [PRIO_CATCH_ALL_ID], ); }); test("focus wins over the stored tier; paused wins over focus", () => { const model = sanitizeChannelPriority({ channels: { lowfoc: { tier: "low" }, pausedfoc: { tier: "paused" } }, }); const root = compileLaneRoot( "digest", model, ["lowfoc", "pausedfoc", "plain"], ["lowfoc", "pausedfoc"], ); assert.deepEqual(leafSlugs(groupNamed(root, prioGroupId("focus"))), ["lowfoc"]); assert.deepEqual(leafSlugs(root), ["lowfoc", "plain"]); }); test("within a group: rank ascending, unranked last, then slug", () => { const model = sanitizeChannelPriority({ channels: { b: { tier: "normal", rank: 0 }, a: { tier: "normal", rank: 5 }, z: { tier: "normal", rank: 5 }, // no rank: m, c low2: { tier: "low", rank: 1 }, low1: { tier: "low" }, }, }); const root = compileLaneRoot( "transcription", model, ["m", "a", "c", "z", "b", "low1", "low2"], [], ); assert.deepEqual(leafSlugs(groupNamed(root, prioGroupId("normal"))), [ "b", "a", "z", "c", "m", ]); assert.deepEqual(leafSlugs(groupNamed(root, prioGroupId("low"))), [ "low2", "low1", ]); }); test("ids are prio-*, and parsePrioLeafId is their inverse", () => { const root = compileLaneRoot( "backfill", sanitizeChannelPriority({ channels: { l: { tier: "low" } } }), ["n", "l", "f"], ["f"], ); const ids: string[] = []; const walk = (node: AutoQueueNode): void => { ids.push(node.id); if (isGroup(node)) node.children.forEach(walk); }; root.children.forEach(walk); assert.deepEqual(ids, [ "prio-focus", "prio-focus-f", "prio-normal", "prio-normal-n", "prio-low", "prio-low-l", "prio-all", ]); assert.equal(prioLeafId("focus", "f"), "prio-focus-f"); assert.deepEqual(parsePrioLeafId("prio-low-l"), { tier: "low", slug: "l" }); assert.deepEqual(parsePrioLeafId("prio-normal-a-b"), { tier: "normal", slug: "a-b", }); assert.equal(parsePrioLeafId(PRIO_CATCH_ALL_ID), null); assert.equal(parsePrioLeafId("prio-normal"), null); assert.equal(parsePrioLeafId("n-2b77f5b7-1"), null); }); test("the four lanes differ ONLY where an override moves a channel", () => { const noOverrides = sanitizeChannelPriority({ channels: { l: { tier: "low" } }, }); const same = compileLanes(noOverrides, ["a", "l"], ["a"]); assert.deepEqual(Object.keys(same), [...LANES]); assert.equal(same.transcription.id, "transcription-root"); assert.equal(same.backfill.id, "backfill-root"); for (const lane of LANES) { assert.deepEqual( { ...same[lane], id: "x" }, { ...same.transcription, id: "x" }, lane, ); } // One override, one lane moves. const withOverride = sanitizeChannelPriority({ channels: { l: { tier: "low" }, omnibased: { tier: "normal", overrides: { download: "paused" } }, }, }); const split = compileLanes(withOverride, ["a", "l", "omnibased"], ["a"]); assert.deepEqual(leafSlugs(split.download), ["a", "l"]); for (const lane of ["transcription", "digest", "backfill"] as const) { assert.deepEqual(leafSlugs(split[lane]), ["a", "omnibased", "l"], lane); } }); test("an override can also demote rather than pause, per lane", () => { const model = sanitizeChannelPriority({ channels: { b: { tier: "normal", overrides: { digest: "low" } } }, }); const roots = compileLanes(model, ["a", "b"], []); assert.deepEqual(leafSlugs(groupNamed(roots.digest, prioGroupId("low"))), ["b"]); assert.deepEqual(leafSlugs(groupNamed(roots.download, prioGroupId("normal"))), [ "a", "b", ]); }); // --------------------------------------------------------------------------- // focusSummary // --------------------------------------------------------------------------- test("focusSummary counts the focus group, the rest, and the channels held", () => { const model = sanitizeChannelPriority({ focus: { kind: "site", siteId: "jeralyzer" }, channels: { l: { tier: "low" } }, }); const summary = focusSummary(model, ["f1", "f2"], { "prio-focus-f1": ["v1", "v2"], "prio-focus-f2": [], "prio-normal-n1": ["v3"], "prio-normal-n2": [], "prio-low-l": ["v4", "v5"], "prio-all": ["v6"], }); assert.deepEqual(summary, { kind: "site", siteId: "jeralyzer", slugs: ["f1", "f2"], channelCount: 2, active: true, focusPending: 2, otherPending: 4, holding: true, // n1 and l have work and are below the focus; n2 has none; the catch-all is // not a channel. heldChannels: 2, }); }); test("a focus with no pending work holds nothing", () => { const model = sanitizeChannelPriority({ focus: { kind: "channels", slugs: ["f1"] }, }); const summary = focusSummary(model, ["f1"], { "prio-focus-f1": [], "prio-normal-n1": ["v1"], }); assert.equal(summary.holding, false); assert.equal(summary.heldChannels, 0); assert.equal(summary.focusPending, 0); assert.equal(summary.otherPending, 1); assert.equal(summary.active, true); }); test("no focus, or a focus that resolved to nothing, is not active", () => { assert.equal(focusSummary(defaultChannelPriority(), []).active, false); assert.equal(focusSummary(defaultChannelPriority(), []).siteId, null); const model = sanitizeChannelPriority({ focus: { kind: "site", siteId: "typo" }, }); assert.equal(focusSummary(model, []).active, false); assert.equal(focusSummary(model, []).siteId, "typo"); }); // --------------------------------------------------------------------------- // 5. channelPriorityFromLegacy // --------------------------------------------------------------------------- // The LIVE shape, from the repo-root settings.json: two strict roots of nine // bare channel leaves then `{type:"all"}`, disagreeing on six channels and on // the Quartering ordering. const LIVE_TRANSCRIPTION = [ "quartering-live", "the-quartering-rumble", "the-quartering", "HasanAbiVODs3", "hasanabi", "rekietalaw-rumble", "nux-taku", "nuxanor", "leaflit-rumble", ]; const LIVE_DOWNLOAD = [ "quartering-live", "the-quartering", "the-quartering-rumble", "nuxanor", "darlingstrawb", "chibi-reviews", "destiny", "omnivods-odysee", "piratesoftware", ]; function liveRoot(lane: string, slugs: readonly string[]): AutoQueueGroup { return { id: "root", mode: "strict", weight: 1, maxWorkers: null, children: [ ...slugs.map((value, i) => ({ id: `n-${lane}-${i}`, match: { type: "channel" as const, value }, weight: 1, maxWorkers: null, })), { id: `n-${lane}-all`, match: { type: "all" as const }, weight: 1, maxWorkers: null, }, ], }; } const CATCH_ALL_ROOT = (id: string): AutoQueueGroup => ({ id: `${id}-root`, mode: "strict", weight: 1, maxWorkers: null, children: [ { id: `${id}-all`, match: { type: "all" as const }, weight: 1, maxWorkers: null, }, ], }); const LIVE_AUTO_QUEUE = { transcription: { root: liveRoot("t", LIVE_TRANSCRIPTION) }, download: { root: liveRoot("d", LIVE_DOWNLOAD) }, digest: { root: CATCH_ALL_ROOT("digest") }, backfill: { root: CATCH_ALL_ROOT("backfill") }, }; // The 15 live `excludeFromSync` channels. const LIVE_EXCLUDED = [ "community-notes", "angryjoeshow", "cornbreadman", "friendofrc", "hex-headquarters", "mevsme", "omnivods-odysee", "exclusively-games", "rcflightschool", "rcspotlight", "teamrcn", "the-incredible-salt-mine", "steven-crowder", "midwestly", "redbar", ]; function liveConfigs() { const slugs = new Set([ ...LIVE_TRANSCRIPTION, ...LIVE_DOWNLOAD, ...LIVE_EXCLUDED, "unranked-one", "unranked-two", ]); return [...slugs].sort().map((slug) => ({ slug, config: LIVE_EXCLUDED.includes(slug) ? { excludeFromSync: true } : {}, })); } // THE MIGRATED ORDER, IN FULL — the thing the plan calls the migration's one // behaviour change, asserted as an ORDER and not as a set of memberships. // // Merged index first (the lane that ranked a channel higher wins), then the // transcription lane's own order, then the slug. Four merged indices carry two // channels each on the live trees, and that is exactly where a collision would // otherwise have been resolved alphabetically — by NEITHER lane's order. const EXPECTED_LIVE_ORDER = [ "quartering-live", // t0 d0 -> merged 0 "the-quartering-rumble", // t1 d2 -> merged 1, transcription first "the-quartering", // t2 d1 -> merged 1, transcription second "HasanAbiVODs3", // t3 -> merged 3, transcription 3 "nuxanor", // t7 d3 -> merged 3, transcription 7 "hasanabi", // t4 -> merged 4, transcription 4 "darlingstrawb", // d4 -> merged 4, unranked by transcription "rekietalaw-rumble", // t5 -> merged 5 "chibi-reviews", // d5 -> merged 5, unranked by transcription "nux-taku", // t6 -> merged 6 "destiny", // d6 -> merged 6, unranked by transcription "omnivods-odysee", // d7 -> merged 7 "leaflit-rumble", // t8 -> merged 8 "piratesoftware", // d8 -> merged 8, unranked by transcription ]; test("the legacy read collapses two lane orders into one rank per channel", () => { const model = channelPriorityFromLegacy(liveConfigs(), LIVE_AUTO_QUEUE); // DENSE, 0..n-1, in this order. The merge produces collisions (four pairs // here); leaving them would hand the tie to `orderWithin`'s slug fallback, // which is neither lane's order and is not what either list said. const ranked = Object.entries(model.channels) .filter(([, e]) => e.rank !== undefined) .sort(([, a], [, b]) => (a.rank ?? 0) - (b.rank ?? 0)) .map(([slug]) => slug); assert.deepEqual(ranked, EXPECTED_LIVE_ORDER); assert.deepEqual( EXPECTED_LIVE_ORDER.map((slug) => rankOf(model, slug)), EXPECTED_LIVE_ORDER.map((_, i) => i), ); // The order the COMPILER then produces is the same one — which is the only // reason the rank matters at all. const slugs = liveConfigs().map((c) => c.slug); const normal = compileLaneRoot("download", model, slugs, []).children.find( (c) => c.id === "prio-normal", ) as AutoQueueGroup; assert.deepEqual( normal.children.slice(0, EXPECTED_LIVE_ORDER.length).map((c) => c.id), EXPECTED_LIVE_ORDER.map((slug) => `prio-normal-${slug}`), ); // Unranked and not excluded = absent entirely. assert.equal(model.channels["unranked-one"], undefined); assert.equal(tierOf(model, "unranked-one"), "normal"); assert.deepEqual(model.focus, { kind: "none" }); }); test("the legacy read is a no-op on a tree it already compiled", () => { const configs = liveConfigs(); const slugs = configs.map((c) => c.slug); const model = channelPriorityFromLegacy(configs, LIVE_AUTO_QUEUE); const lanes = compileLanes(model, slugs, []); const compiled = Object.fromEntries( LANES.map((lane) => [lane, { root: lanes[lane] }]), ); assert.equal(hasCompiledLaneRoots(LIVE_AUTO_QUEUE), false); assert.equal(hasCompiledLaneRoots(compiled), true); // A SECOND RUN OVER ITS OWN OUTPUT. Compiled channel leaves are bare channel // leaves, so without the detector rule 2 would read them and renumber the // hand-made order into "focus group, then normal group, then low" — dense, // alphabetical within each tier, and irrecoverable. const second = channelPriorityFromLegacy(configs, compiled, model); assert.deepEqual(second, model); // Including when the stored document has since been EDITED: the stored // document wins whole, it is not merged with a re-derivation. const edited = sanitizeChannelPriority({ ...model, focus: { kind: "channels", slugs: ["hasanabi"] }, channels: { ...model.channels, destiny: { tier: "paused" } }, }); assert.deepEqual( channelPriorityFromLegacy(configs, compiled, edited), edited, ); }); test("the legacy read is LOSSLESS: excludeFromSync becomes a sync override only", () => { const model = channelPriorityFromLegacy(liveConfigs(), LIVE_AUTO_QUEUE); for (const slug of LIVE_EXCLUDED) { // Paused for sync, exactly as today... assert.equal(effectiveTier(model, slug, "sync"), "paused", slug); // ...and untouched everywhere else. assert.equal(tierOf(model, slug), "normal", slug); for (const lane of LANES) { assert.equal(effectiveTier(model, slug, lane), "normal", `${slug}/${lane}`); } } // omnivods-odysee is the one that is BOTH: excluded from sync today AND the // 8th leaf of the live download tree. It KEEPS its rank and keeps downloading. assert.ok(LIVE_DOWNLOAD.includes("omnivods-odysee")); assert.deepEqual(model.channels["omnivods-odysee"], { tier: "normal", // Dense rank 11, not the merged index 7 — see EXPECTED_LIVE_ORDER. Its // POSITION among the ranked channels is what the download lane reads, and // that is unchanged: still behind destiny, still ahead of piratesoftware. rank: 11, overrides: { sync: "paused" }, }); }); test("the 15-channel migration moves no lane's membership", () => { const configs = liveConfigs(); const slugs = configs.map((c) => c.slug); const model = channelPriorityFromLegacy(configs, LIVE_AUTO_QUEUE); const roots = compileLanes(model, slugs, []); // Every channel in the corpus is still reachable on every lane — nothing was // dropped by the migration, which is what "lossless" has to mean at the tree. for (const lane of LANES) { assert.deepEqual([...leafSlugs(roots[lane])].sort(), [...slugs].sort(), lane); } // And the sync side is the only thing that lost anyone. assert.deepEqual( channelsForOperation(model, slugs, "sync").sort(), slugs.filter((s) => !LIVE_EXCLUDED.includes(s)).sort(), ); for (const lane of LANES) { assert.deepEqual(channelsForOperation(model, slugs, lane), slugs, lane); } }); test("the legacy read is asserted through the sanitizer and is stable", () => { const model = channelPriorityFromLegacy(liveConfigs(), LIVE_AUTO_QUEUE); assert.deepEqual(sanitizeChannelPriority(model), model); assert.deepEqual( channelPriorityFromLegacy(liveConfigs(), LIVE_AUTO_QUEUE), model, ); }); test("an empty legacy tree and no exclusions produce the empty document", () => { assert.deepEqual( channelPriorityFromLegacy( [ { slug: "a", config: {} }, { slug: "b", config: { excludeFromSync: false } }, ], {}, ), defaultChannelPriority(), ); // A tree of catch-alls contributes no rank either. assert.deepEqual( channelPriorityFromLegacy([{ slug: "a", config: {} }], { digest: LIVE_AUTO_QUEUE.digest, backfill: LIVE_AUTO_QUEUE.backfill, }), defaultChannelPriority(), ); }); test("a leaf narrowed by bucket or operation is not a rank", () => { const root: AutoQueueGroup = { id: "root", mode: "strict", weight: 1, maxWorkers: null, children: [ { id: "retry", match: { type: "channel", value: "retryish", bucket: "failedListed" }, weight: 1, maxWorkers: null, }, { id: "op", match: { type: "channel", value: "opish", operation: "diarization" }, weight: 1, maxWorkers: null, }, { id: "bare", match: { type: "channel", value: "bare" }, weight: 1, maxWorkers: null, }, { id: "plat", match: { type: "platform", value: "youtube" }, weight: 1, maxWorkers: null, }, ], }; const model = channelPriorityFromLegacy([], { transcription: { root } }); // Rank 0, not 2: the index is the position among BARE CHANNEL LEAVES, so a // leaf the model cannot express leaves no hole in the order. assert.deepEqual(model.channels, { bare: { tier: "normal", rank: 0 } }); }); test("nested groups still yield ranks, in depth-first leaf order", () => { const root: AutoQueueGroup = { id: "root", mode: "strict", weight: 1, maxWorkers: null, children: [ { id: "g", mode: "strict", weight: 1, maxWorkers: null, children: [ { id: "x", match: { type: "channel", value: "x" }, weight: 1, maxWorkers: null, }, { id: "y", match: { type: "channel", value: "y" }, weight: 1, maxWorkers: null, }, ], }, { id: "z", match: { type: "channel", value: "z" }, weight: 1, maxWorkers: null, }, ], }; const model = channelPriorityFromLegacy([], { download: { root } }); assert.equal(rankOf(model, "x"), 0); assert.equal(rankOf(model, "y"), 1); assert.equal(rankOf(model, "z"), 2); }); // --------------------------------------------------------------------------- // The behaviour-preservation proof: a model derived from a live-shaped tree // (9 bare channel leaves + a catch-all) compiles back to an equivalent tree. // --------------------------------------------------------------------------- test("a 9-leaf + catch-all tree round-trips through the model unchanged", () => { // ONE lane's tree, so the collapse of the two live orders (which IS the one // behaviour change the migration makes, and is measured in S5) is not in // play: this is the property the compiler must hold on its own — derive, // compile, same order. const configs = LIVE_TRANSCRIPTION.map((slug) => ({ slug, config: {} })); const model = channelPriorityFromLegacy(configs, { transcription: { root: liveRoot("t", LIVE_TRANSCRIPTION) }, }); const compiled = compileLaneRoot( "transcription", model, // The corpus in slug order, which is what listChannelConfigs hands over — // so the compiled order comes from the MODEL, not from the input order. [...LIVE_TRANSCRIPTION].sort(), [], ); // Same channels, same order, catch-all still last. assert.deepEqual(leafSlugs(compiled), LIVE_TRANSCRIPTION); const last = compiled.children[compiled.children.length - 1]; assert.ok(!isGroup(last)); assert.deepEqual(last.match, { type: "all" }); // Every leaf is still BARE — no bucket, no operation, no weight or cap moved. const walkLeaves = (node: AutoQueueNode): void => { if (isGroup(node)) { node.children.forEach(walkLeaves); return; } assert.equal(node.match.bucket, undefined); assert.equal(node.match.operation, undefined); assert.equal(node.weight, 1); assert.equal(node.maxWorkers, null); }; compiled.children.forEach(walkLeaves); // Every group is strict, so strict descent means what it meant before. assert.equal(compiled.mode, "strict"); for (const child of compiled.children) { if (isGroup(child)) assert.equal(child.mode, "strict"); } // A channel added since the last compile is still reachable — through the net, // at the bottom, which is the only drift the compiler can produce. const drifted = compileLaneRoot( "transcription", model, [...LIVE_TRANSCRIPTION, "brand-new"].sort(), [], ); assert.deepEqual(leafSlugs(drifted), [...LIVE_TRANSCRIPTION, "brand-new"]); }); test("a focus on a live-shaped tree lifts exactly its channels above the rest", () => { const configs = LIVE_TRANSCRIPTION.map((slug) => ({ slug, config: {} })); const model: ChannelPriority = { ...channelPriorityFromLegacy(configs, { transcription: { root: liveRoot("t", LIVE_TRANSCRIPTION) }, }), focus: { kind: "channels", slugs: ["hasanabi", "HasanAbiVODs3"] }, }; const focusSlugs = resolveFocusSlugs(model, {}, LIVE_TRANSCRIPTION); const root = compileLaneRoot( "transcription", model, [...LIVE_TRANSCRIPTION].sort(), focusSlugs, ); // Ranked 3 and 4 in the legacy tree, so inside the focus group HasanAbiVODs3 // still precedes hasanabi: the focus lifts the pair, it does not reorder them. assert.deepEqual(leafSlugs(groupNamed(root, prioGroupId("focus"))), [ "HasanAbiVODs3", "hasanabi", ]); assert.deepEqual(leafSlugs(groupNamed(root, prioGroupId("normal"))), [ "quartering-live", "the-quartering-rumble", "the-quartering", "rekietalaw-rumble", "nux-taku", "nuxanor", "leaflit-rumble", ]); }); // --------------------------------------------------------------------------- // renameChannelInPriority — the document keys by slug, so it follows a rename // --------------------------------------------------------------------------- test("a rename carries the entry and rewrites a channel focus", () => { const model = sanitizeChannelPriority({ focus: { kind: "channels", slugs: ["old", "other"] }, channels: { old: { tier: "low", rank: 3, overrides: { sync: "paused" } }, other: { tier: "paused" }, }, }); const next = renameChannelInPriority(model, "old", "new"); // The whole entry moves — tier, rank AND the per-operation overrides. Losing // any of them is silent: nothing downstream can tell a stale entry from a // deliberate one. assert.deepEqual(next.channels["new"], { tier: "low", rank: 3, overrides: { sync: "paused" }, }); assert.equal(next.channels["old"], undefined); assert.deepEqual(next.channels["other"], { tier: "paused" }); // A focus NAMING the channel keeps naming it. Order is preserved: the focus // group compiles in the order the selector listed. assert.deepEqual(next.focus, { kind: "channels", slugs: ["new", "other"] }); // And the compiled tree has no leaf under the old slug any more. const root = compileLaneRoot("download", next, ["new", "other"], ["new"]); assert.equal(JSON.stringify(root).includes("prio-focus-old"), false); assert.equal(JSON.stringify(root).includes("prio-focus-new"), true); }); test("a rename is a no-op when there is nothing to carry", () => { const model = sanitizeChannelPriority({ focus: { kind: "site", siteId: "s" }, channels: { kept: { tier: "low" } }, }); // An unknown slug, a same-slug rename, and blanks all return the document // unchanged — and a SITE focus needs no rewrite at all: it resolves through // the site's own channels[], which the rename updates on disk. assert.equal(renameChannelInPriority(model, "absent", "new"), model); assert.equal(renameChannelInPriority(model, "kept", "kept"), model); assert.equal(renameChannelInPriority(model, "", "new"), model); assert.equal(renameChannelInPriority(model, "kept", " "), model); assert.deepEqual(renameChannelInPriority(model, "kept", "moved").focus, { kind: "site", siteId: "s", }); }); test("a rename onto an existing entry keeps the destination's", () => { // The channel controller refuses this rename, so the only way here is a // document that is already inconsistent — and the live channel's settings // are the ones worth keeping. const model = sanitizeChannelPriority({ focus: { kind: "none" }, channels: { old: { tier: "low" }, taken: { tier: "paused" } }, }); const next = renameChannelInPriority(model, "old", "taken"); assert.deepEqual(next.channels, { taken: { tier: "paused" } }); }); // --- Auto-pause: the machine's own pause, and its undo ---------------------- test("auto-pause records the tier it overwrote and forces paused", () => { const model = sanitizeChannelPriority({ focus: { kind: "none" }, channels: { a: { tier: "low", rank: 3 } }, }); const now = new Date("2026-09-20T12:00:00.000Z"); const next = autoPauseForMedia(model, "a", now); assert.equal(next.channels.a.tier, "paused"); assert.deepEqual(next.channels.a.autoPaused, { reason: "storage", since: "2026-09-20T12:00:00.000Z", previousTier: "low", }); // Everything else on the entry survives — the pause is a tier change, not a // reset. assert.equal(next.channels.a.rank, 3); // And a channel with no entry at all gets one recording the default. assert.equal( autoPauseForMedia(model, "fresh", now).channels.fresh.autoPaused ?.previousTier, "normal", ); }); // A FLAPPING DRIVE MUST NOT OVERWRITE `previousTier` WITH `paused` — that would // make the restore restore nothing, which is the failure this no-op prevents. test("auto-pause is a no-op on a channel it already auto-paused", () => { const once = autoPauseForMedia( sanitizeChannelPriority({ channels: { a: { tier: "normal" } } }), "a", new Date("2026-09-20T12:00:00.000Z"), ); const twice = autoPauseForMedia(once, "a", new Date("2026-09-21T12:00:00Z")); assert.equal(twice, once); assert.equal(twice.channels.a.autoPaused?.previousTier, "normal"); }); // A CHANNEL THE OPERATOR PAUSED IS NOT THE MACHINE'S TO CLAIM: recording it // would hand the next restore permission to turn it back on. test("auto-pause is a no-op on a channel the operator already paused", () => { const model = sanitizeChannelPriority({ channels: { a: { tier: "paused" } } }); assert.equal(autoPauseForMedia(model, "a"), model); }); test("restore puts the tier back and clears the record", () => { const paused = autoPauseForMedia( sanitizeChannelPriority({ channels: { a: { tier: "low", rank: 2 } } }), "a", ); const back = restoreAfterMedia(paused, "a"); assert.equal(back.channels.a.tier, "low"); assert.equal(back.channels.a.autoPaused, undefined); assert.equal(back.channels.a.rank, 2); }); test("restore only undoes what auto-pause made", () => { const manual = sanitizeChannelPriority({ channels: { a: { tier: "paused" } } }); assert.equal(restoreAfterMedia(manual, "a"), manual); assert.equal(restoreAfterMedia(manual, "nobody"), manual); }); test("clearAutoPause is how a manual tier change wins", () => { const paused = autoPauseForMedia( sanitizeChannelPriority({ channels: { a: { tier: "normal" } } }), "a", ); const manual = clearAutoPause({ ...paused.channels.a, tier: "low" }); assert.equal(manual.autoPaused, undefined); assert.equal(manual.tier, "low"); // And restore then has nothing to say about it. const model = { ...paused, channels: { a: manual } }; assert.equal(restoreAfterMedia(model, "a"), model); }); // AN AUTO-PAUSE ONLY MEANS ANYTHING ON A PAUSED CHANNEL. A record beside a // non-paused tier is a leftover, and restoring from it would put back a tier // from an earlier era. test("the sanitizer drops a record whose tier is not paused, and round-trips one that is", () => { const stray = sanitizeChannelPriority({ channels: { a: { tier: "normal", autoPaused: { reason: "storage", since: "x", previousTier: "low" }, }, }, }); assert.deepEqual(stray.channels, {}); const kept = sanitizeChannelPriority({ channels: { a: { tier: "paused", autoPaused: { reason: "storage", since: "2026-09-20T00:00:00.000Z", previousTier: "low", }, }, }, }); assert.deepEqual(kept.channels.a.autoPaused, { reason: "storage", since: "2026-09-20T00:00:00.000Z", previousTier: "low", }); // Idempotent. assert.deepEqual(sanitizeChannelPriority(kept), kept); // An unknown reason is not a reason. assert.equal( sanitizeChannelPriority({ channels: { a: { tier: "paused", autoPaused: { reason: "weather", previousTier: "low" } }, }, }).channels.a?.autoPaused, undefined, ); // `previousTier: "paused"` would restore to paused — a no-op dressed as a // restore. It reads as the default. assert.equal( sanitizeChannelPriority({ channels: { a: { tier: "paused", autoPaused: { reason: "storage", since: "", previousTier: "paused" }, }, }, }).channels.a.autoPaused?.previousTier, "normal", ); }); // AN OLDER SHAPE PARSES, which is the whole lane-migration discipline: a binary // that drops the field leaves the channel Paused with nothing lost but the // automatic restore. test("an entry with no record parses exactly as it did before", () => { const model = sanitizeChannelPriority({ channels: { a: { tier: "paused", rank: 1 } }, }); assert.equal(model.channels.a.autoPaused, undefined); assert.equal(autoPauseReasonOf(model, "a"), null); assert.deepEqual(autoPausedSlugs(model), []); }); test("the reason is one sentence, and names the tier it will restore", () => { const model = autoPauseForMedia( sanitizeChannelPriority({ channels: { a: { tier: "low" } } }), "a", new Date("2026-09-20T12:00:00.000Z"), ); const reason = autoPauseReasonOf(model, "a") ?? ""; assert.match(reason, /drive that is not there since 2026-09-20/); assert.match(reason, /returns to low/); assert.deepEqual(autoPausedSlugs(model), ["a"]); }); // AN UNPLUGGED USB CABLE MUST NOT DESTROY THE QUEUE ORDER. The sanitizer drops // a rank from a channel that is paused everywhere — right for a pause the // operator meant, catastrophic for a temporary one, because the restore would // put the channel back unranked at the bottom of its tier. test("an auto-paused channel keeps its rank across a sanitize", () => { const paused = sanitizeChannelPriority( autoPauseForMedia( sanitizeChannelPriority({ channels: { a: { tier: "normal", rank: 4 } } }), "a", ), ); assert.equal(paused.channels.a.tier, "paused"); assert.equal(paused.channels.a.rank, 4); const back = sanitizeChannelPriority(restoreAfterMedia(paused, "a")); assert.equal(back.channels.a.tier, "normal"); assert.equal(back.channels.a.rank, 4); // A pause the operator MEANT still drops its rank, unchanged. assert.equal( sanitizeChannelPriority({ channels: { a: { tier: "paused", rank: 4 } } }) .channels.a.rank, undefined, ); }); // THE FULL ROUND TRIP: pause → sanitize → restore → sanitize gives back the // entry the operator wrote, in every field. // // The overrides half is the one that bit. `sanitizeOverrides` normalises away // any override equal to the BASE TIER — which is what keeps the document a // list of exceptions — and while the machine's pause stands the base on the // entry is the forced `paused`. Measured against that, every `{op:"paused"}` // fence the operator set stops being an exception and is deleted, so the // restore hands back a channel with no fence at all. On this corpus that is // legal-mindset losing both of its, and cornbreadman losing its sync fence to // one hiccup of a USB cable. test("an auto-pause round trip preserves tier, rank AND per-operation overrides", () => { const written = sanitizeChannelPriority({ channels: { "legal-mindset": { tier: "normal", rank: 7, overrides: { sync: "paused", download: "paused" }, }, }, }); assert.deepEqual(written.channels["legal-mindset"], { tier: "normal", rank: 7, overrides: { sync: "paused", download: "paused" }, }); // The drive goes away. The tier is forced, the fences are NOT touched. const paused = sanitizeChannelPriority( autoPauseForMedia(written, "legal-mindset", new Date("2026-09-20T00:00:00Z")), ); assert.equal(paused.channels["legal-mindset"].tier, "paused"); assert.equal(paused.channels["legal-mindset"].rank, 7); assert.deepEqual(paused.channels["legal-mindset"].overrides, { sync: "paused", download: "paused", }); // Sanitizing twice must not erode it either — the document is written back // to disk on every settings write. assert.deepEqual(sanitizeChannelPriority(paused), paused); // And it comes back exactly as it went in. const restored = sanitizeChannelPriority( restoreAfterMedia(paused, "legal-mindset"), ); assert.deepEqual(restored.channels["legal-mindset"], { tier: "normal", rank: 7, overrides: { sync: "paused", download: "paused" }, }); }); // The mirror case: an override equal to the RESTORED base is still normalised // away while auto-paused, because that is what it will be on restore. test("an override equal to the previous tier is still dropped while auto-paused", () => { const paused = sanitizeChannelPriority( autoPauseForMedia( sanitizeChannelPriority({ channels: { a: { tier: "low", overrides: { sync: "low" } } }, }), "a", ), ); assert.equal(paused.channels.a.overrides, undefined); assert.equal( sanitizeChannelPriority(restoreAfterMedia(paused, "a")).channels.a.tier, "low", ); });