import { test } from "node:test"; import assert from "node:assert/strict"; import { acceptListing, SHRINK_ABS_FLOOR } from "./acceptListing"; import type { RosterSweep } from "./rosterStore"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test common/controller/acceptListing.test.ts const PCT = 10; function decide(input: { listedCount: number; previousListedCount?: number | null; lastSweep?: RosterSweep | null; shrinkGuardPercent?: number; }) { return acceptListing({ listedCount: input.listedCount, previousListedCount: input.previousListedCount ?? null, lastSweep: input.lastSweep ?? null, shrinkGuardPercent: input.shrinkGuardPercent ?? PCT, }); } function suspectAt(listedCount: number): RosterSweep { return { at: "2026-08-05T00:00:00.000Z", listedCount, verdict: "shrink-suspect" }; } test("an empty listing is rejected — the Part A behaviour, kept", () => { const d = decide({ listedCount: 0, previousListedCount: 10795 }); assert.equal(d.accept, false); assert.equal(d.verdict, "empty"); }); test("an empty listing is rejected even with no previous listing and the guard off", () => { const d = decide({ listedCount: 0, previousListedCount: null, shrinkGuardPercent: 0, }); assert.equal(d.accept, false); assert.equal(d.verdict, "empty"); }); test("the first listing for a channel is accepted — nothing to compare against", () => { const d = decide({ listedCount: 10795, previousListedCount: 0 }); assert.equal(d.accept, true); assert.equal(d.verdict, "ok"); }); test("a 1% drop is accepted", () => { const d = decide({ listedCount: 10687, previousListedCount: 10795 }); assert.equal(d.accept, true); assert.equal(d.verdict, "ok"); }); test("growth is always accepted", () => { const d = decide({ listedCount: 20000, previousListedCount: 10795 }); assert.equal(d.accept, true); assert.equal(d.verdict, "ok"); }); test("the reported bug: 100 of 10,795 is suspect, not acted on", () => { const d = decide({ listedCount: 100, previousListedCount: 10795 }); assert.equal(d.accept, false); assert.equal(d.verdict, "shrink-suspect"); assert.match(d.reason, /100 entries/); assert.match(d.reason, /10795/); }); test("a 40% drop is suspect, then confirmed by a second similar reading", () => { const first = decide({ listedCount: 60, previousListedCount: 100 }); assert.equal(first.accept, false); assert.equal(first.verdict, "shrink-suspect"); // The next enumeration agrees. A real mass deletion repeats. const second = decide({ listedCount: 60, previousListedCount: 100, lastSweep: suspectAt(60), }); assert.equal(second.accept, true); assert.equal(second.verdict, "shrink-confirmed"); }); test("a second reading that disagrees stays suspect", () => { const d = decide({ listedCount: 60, previousListedCount: 10795, // Last time the fetch said 5,000; this time 60. Two different failures are // not a confirmation. lastSweep: suspectAt(5000), }); assert.equal(d.accept, false); assert.equal(d.verdict, "shrink-suspect"); }); test("confirmation tolerates a small wobble between the two readings", () => { const d = decide({ listedCount: 6000, previousListedCount: 10795, lastSweep: suspectAt(6100), }); assert.equal(d.accept, true); assert.equal(d.verdict, "shrink-confirmed"); }); test("only a previous SUSPECT confirms — an ok or empty reading does not", () => { for (const verdict of ["ok", "empty", "shrink-confirmed"] as const) { const d = decide({ listedCount: 60, previousListedCount: 100, lastSweep: { at: "2026-08-05T00:00:00.000Z", listedCount: 60, verdict }, }); assert.equal(d.accept, false, verdict); assert.equal(d.verdict, "shrink-suspect", verdict); } }); test("the absolute floor keeps ordinary churn on a small channel from tripping", () => { // The live corpus has a 55-video channel; losing six of them is a 10.9% drop // and entirely unremarkable. A pure percentage would have flagged it. const d = decide({ listedCount: 49, previousListedCount: 55 }); assert.equal(d.accept, true); assert.equal(d.verdict, "ok"); }); test("the floor is exactly SHRINK_ABS_FLOOR entries, inclusive", () => { const prev = 100; const atFloor = decide({ listedCount: prev - SHRINK_ABS_FLOOR, previousListedCount: prev }); assert.equal(atFloor.accept, true, "a drop of exactly the floor is allowed"); const overFloor = decide({ listedCount: prev - SHRINK_ABS_FLOOR - 1, previousListedCount: prev, }); assert.equal(overFloor.accept, false); }); test("above the floor the percentage takes over", () => { // 10% of 10,795 is 1,079 — far above the floor, so a 1,000-entry drop passes. assert.equal(decide({ listedCount: 9795, previousListedCount: 10795 }).accept, true); assert.equal(decide({ listedCount: 9000, previousListedCount: 10795 }).accept, false); }); test("percent 0 disables the shrink test but not the empty rejection", () => { const d = decide({ listedCount: 1, previousListedCount: 10795, shrinkGuardPercent: 0, }); assert.equal(d.accept, true); assert.equal(d.verdict, "ok"); assert.equal( decide({ listedCount: 0, previousListedCount: 10795, shrinkGuardPercent: 0 }) .accept, false, ); }); test("a stricter percent catches what the default lets through", () => { // 500 of 10,795 is under the default 10% but over 1%. assert.equal(decide({ listedCount: 10295, previousListedCount: 10795 }).accept, true); assert.equal( decide({ listedCount: 10295, previousListedCount: 10795, shrinkGuardPercent: 1, }).accept, false, ); });