// The audio-integrity rule (release 31, lane A): the verdict from a scan's // numbers, when a record still speaks for a file, and the one-line summary. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/audioIntegrity.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { AUDIO_INTEGRITY_MIN_DECODED_RATIO, describeIntegrity, integrityMatches, integrityVerdict, isDamagedIntegrity, } from "./audioIntegrity"; import { coerceAudioIntegrity, integrityRecord } from "./audioIntegrity-server"; test("the verdict: nothing decodes, a gap, under the ratio, or clean", () => { assert.equal(AUDIO_INTEGRITY_MIN_DECODED_RATIO, 0.98); assert.equal(integrityVerdict({ decodedSeconds: 0, durationSeconds: 600, gaps: [] }), "undecodable"); assert.equal(integrityVerdict({ decodedSeconds: 590, durationSeconds: 600, gaps: [[100, 110]] }), "damaged"); // 98 % exactly is clean; a hair under is damaged (a short gap can be under // the listing's 0.5 s floor and still add up). assert.equal(integrityVerdict({ decodedSeconds: 588, durationSeconds: 600, gaps: [] }), "clean"); assert.equal(integrityVerdict({ decodedSeconds: 587.9, durationSeconds: 600, gaps: [] }), "damaged"); // No container duration: only gaps and "nothing at all" can say damaged. assert.equal(integrityVerdict({ decodedSeconds: 12, durationSeconds: null, gaps: [] }), "clean"); assert.equal(isDamagedIntegrity({ decodedSeconds: 300, durationSeconds: 600, gaps: [[300, 600]] }), true); }); test("a record speaks for one file: its name, its size, its mtime when known", () => { const r = { file: "audio.mp3", size: 1000, mtime: 1_700_000_000_123 }; assert.equal(integrityMatches(r, { name: "audio.mp3", size: 1000, mtime: 1_700_000_000_123.9 }), true); assert.equal(integrityMatches(r, { name: "audio.mp3", size: 1000 }), true, "the snapshot has no mtime"); assert.equal(integrityMatches(r, { name: "audio.mp3" }), true, "an unknown size counts the name"); assert.equal(integrityMatches(r, { name: "audio.mp3", size: 1001 }), false); assert.equal(integrityMatches(r, { name: "audio.mp3", size: 1000, mtime: 1_700_000_001_000 }), false); assert.equal(integrityMatches(r, { name: "audio.m4a", size: 1000 }), false); }); test("the record is built from a scan; the coercion refuses a malformed file", () => { const rec = integrityRecord( "audio.m4a", { size: 42, mtime: 7 }, { decodedSeconds: 3600, totalSeconds: 6000, gaps: [[3600, 6000]] }, { by: "audit", ffmpeg: "ffmpeg version 7", jobId: "j1", now: new Date("2026-10-16T00:00:00Z") }, ); assert.deepEqual(rec, { version: 1, checkedAt: "2026-10-16T00:00:00.000Z", file: "audio.m4a", size: 42, mtime: 7, durationSeconds: 6000, decodedSeconds: 3600, gaps: [[3600, 6000]], verdict: "damaged", tool: { scan: "scanDecodable/1", ffmpeg: "ffmpeg version 7" }, by: "audit", jobId: "j1", }); assert.deepEqual(coerceAudioIntegrity(JSON.parse(JSON.stringify(rec))), rec); assert.equal(coerceAudioIntegrity({ ...rec, verdict: "fine" }), null); assert.equal(coerceAudioIntegrity({ ...rec, gaps: [[1]] }), null); assert.equal(coerceAudioIntegrity({ ...rec, size: "42" }), null); assert.equal(coerceAudioIntegrity(null), null); assert.equal(describeIntegrity(rec), "damaged: 1:00:00 of 1:40:00 decodes (60%), 1 gap"); });