import { test } from "node:test"; import assert from "node:assert/strict"; import { orderPlanByRecency } from "./planOrder"; // Run with: node_modules/.bin/tsx --test common/controller/planOrder.test.ts type Entry = { slug: string; weight: number; newestPending: string; oldestPending: string; }; // Weight order and date order disagree on purpose: `heavy` is the biggest // channel but its work is old, `fresh` is the smallest but holds today's upload. // Any comparator that quietly ignores one of the two axes shows up here. const PLAN: Entry[] = [ { slug: "heavy", weight: 9000, newestPending: "20240101", oldestPending: "20170101" }, { slug: "fresh", weight: 12, newestPending: "20260820", oldestPending: "20260101" }, { slug: "mid", weight: 400, newestPending: "20250601", oldestPending: "20200101" }, ]; const byWeight = (a: Entry, b: Entry): number => b.weight - a.weight || a.slug.localeCompare(b.slug); const slugs = (entries: Entry[]): string[] => entries.map((e) => e.slug); test('order "listed" reproduces the weight order exactly', () => { // The compatibility claim for cross-channel ordering. An armed sweep on a // 78,000-video corpus must visit channels in exactly the order it does today // unless someone asked otherwise. const before = [...PLAN].sort(byWeight); const after = orderPlanByRecency([...PLAN], "listed", byWeight); assert.deepEqual(slugs(before), ["heavy", "mid", "fresh"]); assert.deepEqual(after, before); }); test("newest visits the channel holding the freshest pending video first", () => { const out = orderPlanByRecency([...PLAN], "newest", byWeight); assert.deepEqual(slugs(out), ["fresh", "mid", "heavy"]); }); test("oldest reads the OLDEST date, not the newest one", () => { // heavy's oldest is 2017 and fresh's is 2026, so oldest-first inverts the // newest-first list here. Reading newestPending for both directions would // give the same answer by accident on this fixture only if the two orders // happened to agree — they do not, which is the point of the fixture. const out = orderPlanByRecency([...PLAN], "oldest", byWeight); assert.deepEqual(slugs(out), ["heavy", "mid", "fresh"]); }); test("an all-undated plan is byte-identical to today, in both directions", () => { // The degradation case, and the one that has to be right: a cold stats cache // or an index mid-rebuild gives every channel "". Every recency comparison // then ties and the whole plan falls through to weight — today's order — // rather than shuffling into an arbitrary one. const undated = PLAN.map((e) => ({ ...e, newestPending: "", oldestPending: "", })); const today = [...undated].sort(byWeight); for (const order of ["newest", "oldest"] as const) { assert.deepEqual( orderPlanByRecency([...undated], order, byWeight), today, `${order} over an undated plan must reproduce the weight order`, ); } }); test("an undated channel sorts last under newest and first under oldest", () => { // The same rule makeRecencyComparator applies to an individual video, so the // two levels of the feature cannot disagree about what "no date" means. const withUndated: Entry[] = [ ...PLAN, { slug: "undated", weight: 5, newestPending: "", oldestPending: "" }, ]; assert.equal( slugs(orderPlanByRecency([...withUndated], "newest", byWeight)).at(-1), "undated", ); assert.equal( slugs(orderPlanByRecency([...withUndated], "oldest", byWeight))[0], "undated", ); }); test("weight is the tiebreak, never discarded", () => { // Two channels whose freshest pending video landed the same day are still // visited heaviest-first — the ordering the sweep was built on, and still the // right answer once recency has nothing left to say. const sameDay: Entry[] = [ { slug: "light", weight: 1, newestPending: "20260820", oldestPending: "20260820" }, { slug: "heavy", weight: 900, newestPending: "20260820", oldestPending: "20260820" }, ]; assert.deepEqual(slugs(orderPlanByRecency(sameDay, "newest", byWeight)), [ "heavy", "light", ]); });