Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit 92741a1277212425cbb3b72de61fc753498588dc
parent 0ae20fa9fcaf52438333e3e592ce4723646ec63b
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Fri, 11 Sep 2026 11:24:23 -0400

common: the channel-priority contract under test, in both layers

41 cases in lib/channelPriority.test.ts and 3 in jobs/, +43 over the baseline
(908 -> 951, all green, architecture.test.ts unchanged).

The sanitizer round-trip is in jobs/channelPrioritySanitize.test.ts and not in
lib/ because architecture.test.ts scans a test file like any other and forbids
lib/ -> jobs/, and its ALLOWED list may only shrink — the same reason
laneMigration.test.ts sits in jobs/. What it proves is that sanitizeAutoQueue is
the IDENTITY on a compiled tree, ids included: leaf ids key a lane's fairness
memory and its pick log, so a sanitizer that reassigned one would give the
operator's tree and the runner's tree a silent difference that shows up as a
reset ledger rather than an error. (S1's channelPriorityCompile.test.ts is the
other jobs/-side proof: focus holds, releases and retakes through the real
engine.)

Two facts the tests pinned down that the plan had not:

- The legacy rank is a channel's position among a root's BARE CHANNEL LEAVES,
  not its leaf index. Identical on the live tree (all 22 leaves are bare), but a
  bucket or operation leaf the model cannot express must not leave a hole.
- The 15-channel migration is asserted to move NO lane's membership: every
  channel still on every compiled tree, only `sync` losing anyone.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

Diffstat:
Acommon/jobs/channelPrioritySanitize.test.ts | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acommon/lib/channelPriority.test.ts | 992+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 1093 insertions(+), 0 deletions(-)

diff --git a/common/jobs/channelPrioritySanitize.test.ts b/common/jobs/channelPrioritySanitize.test.ts @@ -0,0 +1,101 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { + compileLaneRoot, + compileLanes, + sanitizeChannelPriority, +} from "../lib/channelPriority"; +import { LANES, isGroup } from "../lib/autoQueueTypes"; +import type { AutoQueueNode } from "../lib/autoQueueTypes"; +import { sanitizeAutoQueue } from "./autoQueuePolicy"; + +// IN jobs/, NOT lib/, for the reason `laneMigration.test.ts` is: the property +// being asserted is that `sanitizeAutoQueue` is the IDENTITY on a compiled +// tree, and ../architecture.test.ts forbids `lib/ -> jobs/` — including from a +// test file, and its ALLOWED list may only shrink. +// +// WHY IT MATTERS. The compiler writes into `autoQueue[lane].root`, and every +// read of settings.json puts that back through `sanitizeAutoQueue`. If the +// sanitizer touched anything the compiler emits — reassigned an id (leaf ids +// key the lane's fairness memory and its pick log), defaulted a weight, coerced +// a mode — then the tree an operator compiled and the tree the runner walks +// would be two different trees, and the difference would show up as a silently +// reset ledger rather than as an error. That is why every node the compiler +// emits already spells `weight: 1` and `maxWorkers: null`. +// +// The focus/hold/release proof through the real engine (buildPendingByLeaf + +// selectNextWork) is slice S1's `channelPriorityCompile.test.ts`. + +function eachNode(node: AutoQueueNode, fn: (n: AutoQueueNode) => void): void { + fn(node); + if (isGroup(node)) for (const child of node.children) eachNode(child, fn); +} + +test("sanitizeAutoQueue is the identity on a compiled tree", () => { + const model = sanitizeChannelPriority({ + channels: { + lowly: { tier: "low" }, + gone: { tier: "paused" }, + first: { tier: "normal", rank: 0 }, + // A per-operation override, so the four lanes are NOT all the same tree. + nodl: { tier: "normal", overrides: { download: "paused" } }, + }, + }); + const slugs = ["first", "lowly", "gone", "other", "focused", "nodl"]; + const roots = compileLanes(model, slugs, ["focused"]); + const before = { + transcription: { enabled: true, root: roots.transcription }, + download: { enabled: false, root: roots.download }, + digest: { enabled: false, root: roots.digest }, + backfill: { enabled: false, root: roots.backfill }, + }; + const after = sanitizeAutoQueue(before); + for (const lane of LANES) { + assert.deepEqual(after[lane].root, roots[lane], `${lane} root changed`); + } + // And through JSON, which is what settings.json actually does to it. + const roundTripped = sanitizeAutoQueue(JSON.parse(JSON.stringify(before))); + for (const lane of LANES) { + assert.deepEqual(roundTripped[lane].root, roots[lane], `${lane} via JSON`); + } + // The override really did split the lanes, so the assertion above was not + // four copies of one comparison. + assert.notDeepEqual( + { ...after.download.root, id: "x" }, + { ...after.transcription.root, id: "x" }, + ); +}); + +test("no compiled id is reassigned by the sanitizer", () => { + const model = sanitizeChannelPriority({ + channels: { l: { tier: "low" }, p: { tier: "paused" } }, + }); + const root = compileLaneRoot("transcription", model, ["a", "b", "l", "p"], [ + "a", + ]); + const ids: string[] = []; + eachNode(root, (n) => ids.push(n.id)); + const after = sanitizeAutoQueue({ transcription: { root } }).transcription + .root; + const afterIds: string[] = []; + eachNode(after, (n) => afterIds.push(n.id)); + assert.deepEqual(afterIds, ids); + // Every id is stable and derived — `node-<n>` would mean the sanitizer had + // to invent one, which is exactly the fairness-memory reset this guards. + assert.ok(!afterIds.some((id) => id.startsWith("node-")), afterIds.join(",")); +}); + +test("an empty model still compiles to a tree the sanitizer accepts", () => { + const roots = compileLanes(sanitizeChannelPriority(undefined), [], []); + for (const lane of LANES) { + // No channels at all: just the catch-all net. + assert.deepEqual( + roots[lane].children.map((c) => c.id), + ["prio-all"], + ); + assert.deepEqual( + sanitizeAutoQueue({ [lane]: { root: roots[lane] } })[lane].root, + roots[lane], + ); + } +}); diff --git a/common/lib/channelPriority.test.ts b/common/lib/channelPriority.test.ts @@ -0,0 +1,992 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { + CHANNEL_TIERS, + PRIORITY_OPERATIONS, + PRIO_CATCH_ALL_ID, + STORED_CHANNEL_TIERS, + channelPriorityFromLegacy, + channelsForOperation, + compileLaneRoot, + compileLanes, + 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 } : {}, + })); +} + +test("the legacy read collapses two lane orders into one rank per channel", () => { + const model = channelPriorityFromLegacy(liveConfigs(), LIVE_AUTO_QUEUE); + // The three Quartering channels are in different orders in the two lanes; the + // LOWER index wins, so the transcription order survives for the one it ranks + // higher and the download order for the other. + assert.equal(rankOf(model, "quartering-live"), 0); + assert.equal(rankOf(model, "the-quartering-rumble"), 1); // t:1 vs d:2 + assert.equal(rankOf(model, "the-quartering"), 1); // t:2 vs d:1 + // Ranked in one lane only: that lane's index stands. + assert.equal(rankOf(model, "HasanAbiVODs3"), 3); + assert.equal(rankOf(model, "darlingstrawb"), 4); + assert.equal(rankOf(model, "leaflit-rumble"), 8); + assert.equal(rankOf(model, "piratesoftware"), 8); + // nuxanor: t:7 vs d:3. + assert.equal(rankOf(model, "nuxanor"), 3); + // 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 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", + rank: 7, + 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", + ]); +});