import { test } from "node:test"; import assert from "node:assert/strict"; import type { ChannelSnapshot } from "../controller/channelSnapshot"; import type { OperationSnapshotEntry } from "../lib/operations"; import { normalizeBuckets } from "./pipeline/stageStatus"; import { cleanExtraFormatsBytesOf, cleanExtraFormatsCountOf, cleanTranscribedBytesOf, cleanTranscribedCountOf, digestReachableCountOf, digestWarningsCountOf, incompleteTranscriptCountOf, metadataScanCountOf, missingNeverFetchedCountOf, shortAudioCountOf, undownloadedCountOf, untranscribedCountOf, } from "./actionableCounts"; function snapshotOf(patch: Partial = {}): ChannelSnapshot { return { generatedAt: "2026-08-01T00:00:00.000Z", totals: { videos: 10, transcribed: 4, downloaded: 6 }, buckets: normalizeBuckets(undefined), undownloadedIds: [], ...patch, }; } const ALL = [ undownloadedCountOf, missingNeverFetchedCountOf, metadataScanCountOf, untranscribedCountOf, incompleteTranscriptCountOf, shortAudioCountOf, cleanTranscribedCountOf, cleanExtraFormatsCountOf, digestReachableCountOf, digestWarningsCountOf, cleanTranscribedBytesOf, cleanExtraFormatsBytesOf, ]; test("a channel with no report counts zero everywhere", () => { for (const f of ALL) { assert.equal(f(null), 0, f.name); assert.equal(f(undefined), 0, f.name); } }); test("a snapshot written before the optional buckets existed counts zero", () => { // The on-disk shape of an old report: no optional buckets, no cleanupBytes, // no metadataScan, no missingNeverFetched, no backfill entries. const old = { generatedAt: "2026-01-01T00:00:00.000Z", totals: { videos: 3, transcribed: 0, downloaded: 0 }, buckets: { downloadedNoTranscript: [] }, undownloadedIds: [], } as unknown as ChannelSnapshot; for (const f of ALL) assert.equal(f(old), 0, f.name); }); test("undownloaded and untranscribed drop the availability exclusions", () => { const snap = snapshotOf({ undownloadedIds: ["a", "b", "c", "d"], buckets: { ...normalizeBuckets(undefined), downloadedNoTranscript: ["t1", "t2", "b"], }, excludedFromDownload: { membersOnly: ["a"], deleted: ["b"], private: [] }, }); assert.equal(undownloadedCountOf(snap), 2); assert.equal(untranscribedCountOf(snap), 2); // With nothing excluded the lengths are the counts. const plain = snapshotOf({ undownloadedIds: ["a", "b"] }); assert.equal(undownloadedCountOf(plain), 2); }); test("the bucket counts are the bucket lengths; the bytes are cleanupBytes", () => { const snap = snapshotOf({ buckets: { ...normalizeBuckets(undefined), incompleteTranscript: ["i"], shortAudio: ["s1", "s2"], transcribedWithAudio: ["w1", "w2", "w3"], multipleAudioFormats: ["m"], digestWarnings: ["d1", "d2"], }, cleanupBytes: { transcribedWithAudio: 300, multipleAudioFormats: 40 }, metadataScan: { unscanned: 7 } as ChannelSnapshot["metadataScan"], missingNeverFetched: [{}, {}] as ChannelSnapshot["missingNeverFetched"], }); assert.equal(incompleteTranscriptCountOf(snap), 1); assert.equal(shortAudioCountOf(snap), 2); assert.equal(cleanTranscribedCountOf(snap), 3); assert.equal(cleanExtraFormatsCountOf(snap), 1); assert.equal(digestWarningsCountOf(snap), 2); assert.equal(cleanTranscribedBytesOf(snap), 300); assert.equal(cleanExtraFormatsBytesOf(snap), 40); assert.equal(metadataScanCountOf(snap), 7); assert.equal(missingNeverFetchedCountOf(snap), 2); }); test("digest reachable is the digest registry entry's reachable", () => { const entry = { ids: [], missing: 5, stale: 2, missingInput: 0, partial: 1, } as unknown as OperationSnapshotEntry; const snap = snapshotOf({ backfill: { digest: entry } }); assert.equal(digestReachableCountOf(snap), 8); });