import { test } from "node:test"; import assert from "node:assert/strict"; import { MEDIA_SCAN_OVERRIDES_VERSION, mediaScanKey, mediaScanTotals, sanitizeMediaScanOverrides, unreviewedFindings, type MediaScanFinding, type MediaScanReport, } from "./mediaScan"; function finding(over: Partial = {}): MediaScanFinding { return { slug: "chan/vid", channelSlug: "chan", videoId: "vid", file: "audio.mp3", bytes: 100, verdict: "unreadable", tier: 1, containerSeconds: null, metadataSeconds: null, ...over, }; } function report(over: Partial = {}): MediaScanReport { return { version: 1, generatedAt: "2026-08-08T00:00:00.000Z", runConfig: { deepProbe: false, toleranceSeconds: 30, toleranceRatio: 0.02 }, channels: {}, findings: [], ...over, }; } const CHANNEL_TOTALS = { videosScanned: 2, filesScanned: 3, ok: 2, unreadable: 1, truncated: 0, stray: 0, unknown: 0, deepProbed: 0, bytesAtRisk: 500, scannedAt: "2026-08-08T00:00:00.000Z", }; test("corpus totals are the SUM of the per-channel entries", () => { // Derived rather than stored, which is what lets a per-channel re-scan replace // one entry without the headline claiming to describe a scan it did not do. const r = report({ channels: { a: { ...CHANNEL_TOTALS }, b: { ...CHANNEL_TOTALS, videosScanned: 5, unreadable: 0, truncated: 2, bytesAtRisk: 700, scannedAt: "2026-08-09T00:00:00.000Z", }, }, }); const t = mediaScanTotals(r); assert.equal(t.videosScanned, 7); assert.equal(t.filesScanned, 6); assert.equal(t.unreadable, 1); assert.equal(t.truncated, 2); assert.equal(t.bytesAtRisk, 1200); // The freshest channel scan is what the report is "as of". assert.equal(t.scannedAt, "2026-08-09T00:00:00.000Z"); }); test("an empty report totals to zero rather than throwing", () => { assert.equal(mediaScanTotals(report()).filesScanned, 0); // A report written by an older build has no `channels` at all. assert.equal( mediaScanTotals({ ...report(), channels: undefined as never }).filesScanned, 0, ); }); test("a reviewed finding drops out of the work list", () => { const f = finding(); const r = report({ findings: [f, finding({ file: "source-media.mp4" })] }); const overrides = sanitizeMediaScanOverrides({ reviewed: { [mediaScanKey(f)]: { reviewedAt: "2026-08-08T00:00:00.000Z" } }, }); const left = unreviewedFindings(r, overrides); assert.equal(left.length, 1); assert.equal(left[0].file, "source-media.mp4"); }); test("the key is per FILE, not per video", () => { // A video dir can hold both a good container and a corrupt scratch copy; // reviewing one must not silently dismiss the other. assert.notEqual( mediaScanKey({ slug: "chan/vid", file: "source-media.mp4" }), mediaScanKey({ slug: "chan/vid", file: "source-media.temp.mp4" }), ); }); test("a malformed overrides file reads as no decisions, never throws", () => { // Hand-editable, so it will eventually be hand-broken. The page still has to // render. for (const junk of [null, undefined, 7, "{}", { reviewed: 3 }, {}]) { const o = sanitizeMediaScanOverrides(junk); assert.deepEqual(o.reviewed, {}); assert.equal(o.version, MEDIA_SCAN_OVERRIDES_VERSION); } // Entries missing the timestamp are dropped, the rest survive. const mixed = sanitizeMediaScanOverrides({ reviewed: { good: { reviewedAt: "2026-08-08T00:00:00.000Z", note: "known clip" }, bad: { note: "no timestamp" }, alsoBad: 5, }, }); assert.deepEqual(Object.keys(mixed.reviewed), ["good"]); assert.equal(mixed.reviewed.good.note, "known clip"); }); test("no report means no findings, not a crash", () => { assert.deepEqual(unreviewedFindings(null, null), []); assert.equal(unreviewedFindings(report({ findings: [finding()] }), null).length, 1); });