import { test } from "node:test"; import assert from "node:assert/strict"; import { KEEP_LONGER_TOLERANCE_SECONDS, appendMediaImportEntry, archiveLineFor, chooseKept, coerceMediaImport, finalizeFor, importIdFromFilename, manualRecordInfo, parseImportRequest, readProbe, sourceExt, type MediaImportEntry, type MediaProbe, } from "./mediaImport"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/mediaImport.test.ts test("keep the longer: a new file replaces a container only when longer by more than the slack", () => { assert.equal(chooseKept({ newSeconds: 100 }), "new", "nothing stored"); assert.equal(chooseKept({ newSeconds: 100, existingSeconds: 50 }), "new"); assert.equal(chooseKept({ newSeconds: 100, existingSeconds: 100 - KEEP_LONGER_TOLERANCE_SECONDS }), "existing", "a tie stays"); assert.equal(chooseKept({ newSeconds: 50, existingSeconds: 100 }), "existing"); assert.equal(chooseKept({ newSeconds: 50, existingSeconds: 100, replace: true }), "new", "replace"); assert.equal(chooseKept({ newSeconds: 100, existingSeconds: null }), "existing", "an unmeasured store stays"); assert.equal(chooseKept({ newSeconds: null, existingSeconds: 10 }), "existing"); }); const probe = (formatName: string, v: string | null, a: string | null, seconds = 60): MediaProbe => ({ formatName, seconds, videoCodec: v, audioCodec: a, }); test("finalize: a stored container is copied, MPEG-TS and partials are remuxed, never re-encoded", () => { assert.deepEqual(finalizeFor("x.mp4", probe("mov,mp4,m4a,3gp,3g2,mj2", "h264", "aac")), { action: "copy", ext: "mp4", reason: "mov,mp4,m4a,3gp,3g2,mj2 as it is", }); const part = finalizeFor("v7f2czy.from-start.mp4.part", probe("mpegts", "h264", "aac")); assert.ok(!("refused" in part) && part.action === "remux" && part.ext === "mp4"); assert.ok(!("refused" in part) && part.remuxArgs!.includes("+faststart") && part.remuxArgs!.includes("copy")); const ts = finalizeFor("rec.ts", probe("mpegts", "h264", "aac")); assert.ok(!("refused" in ts) && ts.ext === "mp4"); const odd = finalizeFor("rec.flv", probe("flv", "vp6f", "nellymoser")); assert.ok(!("refused" in odd) && odd.ext === "mkv", "codecs mp4 cannot hold go to mkv"); assert.ok(!("refused" in finalizeFor("x.webm", probe("matroska,webm", "vp9", "opus")))); assert.deepEqual(finalizeFor("x.mp4", probe("mov,mp4", "h264", null)), { refused: "x.mp4 holds no audio stream: nothing to transcribe" }); assert.equal(sourceExt("a.MP4.part"), "mp4"); }); test("readProbe reads ffprobe's JSON", () => { const p = readProbe({ format: { format_name: "mpegts", duration: "1100.04" }, streams: [{ codec_type: "video", codec_name: "h264" }, { codec_type: "audio", codec_name: "aac" }], }); assert.deepEqual(p, { formatName: "mpegts", seconds: 1100.04, videoCodec: "h264", audioCodec: "aac" }); assert.equal(readProbe({}), null); }); test("the request: one record named, absolute paths, booleans", () => { const ok = parseImportRequest({ slug: "the-quartering-rumble", path: "/home/user/Podcasts/v7f2czy.from-start.mp4", url: "https://rumble.com/v7h8pxg-x.html", move: true, doNotClean: true, note: "keep", }); assert.ok(!("error" in ok)); assert.equal((ok as { move: boolean }).move, true); assert.equal((ok as { note: string }).note, "keep"); const err = (b: Record) => (parseImportRequest(b) as { error?: string }).error ?? ""; assert.match(err({ slug: "s", path: "rel/x.mp4", id: "a" }), /absolute/); assert.match(err({ slug: "s", path: "/x.mp4" }), /Say which record/); assert.match(err({ slug: "s", path: "/x.mp4", url: "https://x/y", meta: { title: "t" } }), /not both/); assert.match(err({ slug: "s", path: "/x.mp4", meta: { title: "t", date: "May 1" } }), /YYYY-MM-DD/); assert.match(err({ slug: "s", path: "/x.mp4", id: "a", move: "yes" }), /true or false/); assert.match(err({ slug: "s", path: "/x.mp4", id: "../a" }), /not a record id/); }); test("a record by hand, an id from a file name, the archive line", () => { assert.equal(importIdFromFilename("My Talk (2019) FINAL.mp4"), "my-talk-2019-final"); assert.equal(importIdFromFilename("rec.mp4.part"), "rec"); const info = manualRecordInfo({ id: "my-talk", meta: { title: "My talk", date: "2019-05-01" }, seconds: 123.456, fileName: "x.mkv" }); assert.equal(info.upload_date, "20190501"); assert.equal(info.duration, 123.46); assert.equal(info.extractor, "import"); assert.equal(archiveLineFor(info), null); assert.equal(archiveLineFor({ extractor: "RumbleEmbed", id: "v7f2czy" }), "RumbleEmbed v7f2czy"); }); test("media-import.json keeps its entries and challenges", () => { const e = { at: "t", via: "import-media" } as MediaImportEntry; const r = appendMediaImportEntry( { entries: [], challenges: [{ at: "c", by: "download", what: "audio", newSeconds: 1, landedSeconds: 2, kept: "landed" }] }, e, ); assert.equal(r.entries.length, 1); assert.equal(r.challenges?.length, 1); assert.equal(coerceMediaImport({ entries: [e, { bogus: 1 }] })?.entries.length, 1); assert.equal(coerceMediaImport({}), null); });