import { test } from "node:test"; import assert from "node:assert/strict"; import { ARCHIVE_ORG_AUTO_FORMAT_SELECTOR, DOWNLOAD_FORMAT_LABELS, DOWNLOAD_FORMAT_PRESETS, ORIGINAL_SOURCE_FORMAT_SELECTOR, SOURCE_VIDEO_QUALITIES, VIDEO_720_PRESET, clipFormatSelector, isDownloadFormatPreset, isSourceVideoQuality, resolveDownloadFormatSelector, resolveSourceVideoQuality, sourceVideoFormatSelector, sourceVideoQualityForMaxHeight, } from "./downloadFormat"; import { clipFormatSelector as reexported } from "./fetchWindowManaged"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test ytdlp/downloadFormat.test.ts const CLIP_720 = "bv*[vcodec^=avc1][height<=720]+ba[acodec^=mp4a]/" + "bv*[ext=mp4][height<=720]+ba[ext=m4a]/" + "b[ext=mp4][height<=720]/" + "b[height<=720]"; test("clipFormatSelector is the selector umtool uses, and the old import path still serves it", () => { assert.equal(clipFormatSelector(720), CLIP_720); assert.equal(reexported, clipFormatSelector); }); test("video_720: split H.264 at 720, then 480, then any codec split ≤720, then single-file ≤720, then the last resort", () => { assert.equal(VIDEO_720_PRESET, "video_720"); assert.ok(isDownloadFormatPreset("video_720")); assert.ok(DOWNLOAD_FORMAT_PRESETS.includes("video_720")); assert.equal(DOWNLOAD_FORMAT_LABELS.video_720, "Video 720p (H.264, for clips/editing)"); const expected = [ "bv*[vcodec^=avc1][height<=720]+ba[acodec^=mp4a]", "bv*[ext=mp4][height<=720]+ba[ext=m4a]", "bv*[vcodec^=avc1][height<=480]+ba[acodec^=mp4a]", "bv*[ext=mp4][height<=480]+ba[ext=m4a]", "bv*[height<=720]+ba", "b[ext=mp4][height<=720]", "b[height<=720]", "bv*+ba/b", ].join("/"); assert.equal(resolveDownloadFormatSelector("video_720", null), expected); // An explicit preset ignores the platform, as every explicit preset does. assert.equal(resolveDownloadFormatSelector("video_720", "odysee"), expected); }); test("the existing presets resolve exactly as before", () => { assert.equal(resolveDownloadFormatSelector("auto", "youtube"), "bestaudio/worst"); assert.equal(resolveDownloadFormatSelector("auto", "odysee"), "original/bestaudio/worst"); assert.equal(resolveDownloadFormatSelector("original", null), "original/bestaudio/worst"); assert.equal(resolveDownloadFormatSelector("bestaudio", null), "bestaudio/worst"); assert.equal(resolveDownloadFormatSelector("bestvideo_audio", null), "bestvideo*+bestaudio/best"); }); test("sourceVideoFormatSelector: original is today's persist selector, byte for byte", () => { assert.deepEqual([...SOURCE_VIDEO_QUALITIES], ["original", "video_720"]); assert.equal(ORIGINAL_SOURCE_FORMAT_SELECTOR, "bestvideo*+bestaudio/best"); assert.equal(sourceVideoFormatSelector("original"), "bestvideo*+bestaudio/best"); assert.equal( sourceVideoFormatSelector("video_720"), resolveDownloadFormatSelector("video_720", null), ); }); test("resolveSourceVideoQuality: override beats channel beats global; original when unset", () => { assert.equal(resolveSourceVideoQuality({}), "original"); assert.equal(resolveSourceVideoQuality({ global: "video_720" }), "video_720"); // The channel wins over the global, either way round. assert.equal( resolveSourceVideoQuality({ channel: "original", global: "video_720" }), "original", ); assert.equal( resolveSourceVideoQuality({ channel: "video_720", global: "original" }), "video_720", ); assert.equal( resolveSourceVideoQuality({ override: "original", channel: "video_720", global: "video_720" }), "original", ); // Junk at any level falls through to the next, never reaching yt-dlp. assert.equal( resolveSourceVideoQuality({ override: "1080p", channel: undefined, global: "video_720" }), "video_720", ); assert.equal(resolveSourceVideoQuality({ override: "", channel: 720 }), "original"); assert.ok(isSourceVideoQuality("video_720")); assert.ok(!isSourceVideoQuality("bestvideo_audio")); }); test("sourceVideoQualityForMaxHeight: a cap at or under 720 is video_720, above it original", () => { assert.equal(sourceVideoQualityForMaxHeight(144), "video_720"); assert.equal(sourceVideoQualityForMaxHeight(480), "video_720"); assert.equal(sourceVideoQualityForMaxHeight(720), "video_720"); assert.equal(sourceVideoQualityForMaxHeight(721), "original"); assert.equal(sourceVideoQualityForMaxHeight(1080), "original"); assert.equal(sourceVideoQualityForMaxHeight(2160), "original"); }); test("auto on archive.org takes the uploader's original, an audio item's mp3", () => { const sel = resolveDownloadFormatSelector("auto", "archiveorg"); assert.equal(sel, ARCHIVE_ORG_AUTO_FORMAT_SELECTOR); assert.match(sel, /^b\[format_note=original\]\[ext=mp4\]\//); assert.ok(sel.split("/").includes("mp3")); // An explicit preset still applies literally. assert.equal(resolveDownloadFormatSelector("bestaudio", "archiveorg"), "bestaudio/worst"); });