import { test } from "node:test"; import assert from "node:assert/strict"; import { MAX_PIECE_LENGTH, MIN_PIECE_LENGTH, decidePlayable, magnetFor, parsePlayableManifest, parseProbe, pieceLengthFor, type MediaProbe, } from "./playableMedia"; // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/playableMedia.test.ts const p = (ext: string, vcodec: string | null, acodec: string | null): MediaProbe => ({ formatName: ext === "mp4" ? "mov,mp4,m4a,3gp,3g2,mj2" : "matroska,webm", ext, vcodec, acodec, }); // The whole table: video × audio × source container → the playable container. // The pilot archive's measured mix is every row marked ★. const TABLE: [string, string, string | null, "mp4" | "webm" | null][] = [ ["mp4", "h264", "aac", "mp4"], // ★ 145 ["mp4", "h264", "opus", "mp4"], ["mp4", "vp9", "aac", "mp4"], ["mp4", "vp9", "opus", "mp4"], ["mp4", "av1", "aac", "mp4"], // ★ 1 ["mp4", "av1", "opus", "mp4"], ["mkv", "h264", "aac", "mp4"], ["mkv", "h264", "opus", "mp4"], // ★ 2 ["mkv", "vp9", "aac", "mp4"], // ★ 11 ["mkv", "vp9", "opus", "mp4"], ["mkv", "av1", "aac", "mp4"], ["mkv", "av1", "opus", "mp4"], // ★ 1 ["webm", "vp9", "opus", "webm"], // ★ 9 ["webm", "av1", "opus", "webm"], ["webm", "vp8", "vorbis", "webm"], ["webm", "vp9", null, "webm"], ["webm", "h264", "aac", "mp4"], // not a webm pair: mp4 ["webm", "vp9", "aac", "mp4"], ["mp4", "h264", null, "mp4"], ["mp4", "h264", "mp3", "mp4"], ["mkv", "hevc", "aac", null], // no lossless remux a browser plays ["mkv", "mpeg4", "mp3", null], ["mkv", "h264", "ac3", null], ["webm", "vp8", "aac", null], ]; test("the remux decision table", () => { for (const [ext, v, a, want] of TABLE) { const d = decidePlayable(p(ext, v, a)); const label = `${v}/${a ?? "none"} in ${ext}`; if (want === null) { assert.equal(d.playable, false, label); continue; } assert.equal(d.playable, true, label); if (!d.playable) continue; assert.equal(d.ext, want, label); assert.equal(d.mime, `video/${want}`, label); // Lossless always; the index in front always. assert.ok(d.args.includes("copy"), label); assert.ok(!d.args.some((x) => /^lib|^-crf$|^-b:/.test(x)), `${label}: no encoder`); if (want === "mp4") assert.ok(d.args.join(" ").includes("-movflags +faststart"), label); if (want === "webm") assert.ok(d.args.join(" ").includes("-cues_to_front 1"), label); } }); test("no video is not playable; metadata and extra streams are dropped", () => { assert.equal(decidePlayable(p("mp4", null, "aac")).playable, false); const d = decidePlayable(p("mp4", "h264", "aac")); assert.ok(d.playable); if (!d.playable) return; const a = d.args.join(" "); assert.match(a, /-map 0:V:0 -map 0:a:0\?/, "first real video (not cover art), first audio if any"); assert.match(a, /-map_metadata -1/); assert.match(a, /-map_chapters -1/); assert.match(a, /-sn -dn/); assert.match(a, /-fflags \+bitexact/); }); test("parseProbe skips cover art and reads the extension", () => { const probe = parseProbe( { format: { format_name: "matroska,webm" }, streams: [ { codec_type: "video", codec_name: "mjpeg", disposition: { attached_pic: 1 } }, { codec_type: "video", codec_name: "vp9", disposition: { attached_pic: 0 } }, { codec_type: "audio", codec_name: "opus" }, { codec_type: "subtitle", codec_name: "ass" }, ], }, ".WEBM", ); assert.deepEqual(probe, { formatName: "matroska,webm", ext: "webm", vcodec: "vp9", acodec: "opus" }); assert.deepEqual(parseProbe(null, "mp4"), { formatName: "", ext: "mp4", vcodec: null, acodec: null }); }); test("piece length: a power of two, 256 KiB–1 MiB, scaled to the size", () => { assert.equal(pieceLengthFor(1), MIN_PIECE_LENGTH); assert.equal(pieceLengthFor(20 * 1024 ** 2), MIN_PIECE_LENGTH); assert.equal(pieceLengthFor(800 * 1024 ** 2), 512 * 1024); assert.equal(pieceLengthFor(2 * 1024 ** 3), MAX_PIECE_LENGTH); assert.equal(pieceLengthFor(50 * 1024 ** 3), MAX_PIECE_LENGTH); for (const n of [1e6, 3e8, 7e8, 1.5e9]) { const len = pieceLengthFor(n); assert.equal(len & (len - 1), 0, `${len} is a power of two`); } }); test("a magnet names the infohash, the file and only the trackers given", () => { const hash = "a".repeat(40); assert.equal(magnetFor({ infoHash: hash, name: "abc.mp4" }), `magnet:?xt=urn:btih:${hash}&dn=abc.mp4`); assert.equal( magnetFor({ infoHash: hash, name: "a b.mp4", trackers: ["wss://t.example/announce"] }), `magnet:?xt=urn:btih:${hash}&dn=a%20b.mp4&tr=wss%3A%2F%2Ft.example%2Fannounce`, ); }); test("the manifest parse is tolerant: a bad entry is dropped, not fatal", () => { const good = { sourceSha256: "b".repeat(64), file: "x/x.mp4", torrent: "x/x.torrent", ext: "mp4", mime: "video/mp4", bytes: 10, infoHash: "c".repeat(40), pieceLength: 262144, vcodec: "h264", acodec: "aac", preparedAt: "2026-10-09T00:00:00Z", }; const m = parsePlayableManifest({ version: 1, slug: "s", entries: { x: good, y: { ...good, infoHash: "nope" } } }, "s"); assert.deepEqual(Object.keys(m.entries), ["x"]); assert.deepEqual(parsePlayableManifest("garbage", "s").entries, {}); });