import { test } from "node:test"; import assert from "node:assert/strict"; import { DISK_SAVE_ABOVE_BYTES, MEMORY_LIMIT_BYTES, chooseOutput, clipFileName, describeClip, estimateBytes, fileRange, formatBytes, formatClock, refusalSentence, savePath, type OutputSupport, } from "./localClip"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/localClip.test.ts test("the marks go to the file through the offset, clamped to the file", () => { assert.deepEqual(fileRange(5, 9, 0, 0, 12), { ok: true, fileIn: 5, fileOut: 9 }); assert.deepEqual(fileRange(5, 9, 1, 0, 12), { ok: true, fileIn: 6, fileOut: 10 }); // A negative offset: the copy starts later than the cues. assert.deepEqual(fileRange(1, 4, -2, 0, 12), { ok: true, fileIn: 0, fileOut: 2 }); // Past the end: cut at the end. assert.deepEqual(fileRange(10, 20, 0, 0, 12), { ok: true, fileIn: 10, fileOut: 12 }); // An unknown end clamps nothing at the top. assert.deepEqual(fileRange(10, 20, 0, 0, null), { ok: true, fileIn: 10, fileOut: 20 }); // A copy whose first timestamp is not 0: clamped to where it begins. assert.deepEqual(fileRange(1, 6, 0, 3, 15), { ok: true, fileIn: 3, fileOut: 6 }); }); test("a range that is empty, backwards, too short or off the file is refused", () => { assert.deepEqual(fileRange(9, 5, 0, 0, 12), { ok: false, reason: "order" }); assert.deepEqual(fileRange(5, 5, 0, 0, 12), { ok: false, reason: "order" }); assert.deepEqual(fileRange(5, 5.05, 0, 0, 12), { ok: false, reason: "short" }); assert.deepEqual(fileRange(13, 20, 0, 0, 12), { ok: false, reason: "outside" }); assert.deepEqual(fileRange(1, 3, -5, 0, 12), { ok: false, reason: "outside" }); // Only a sliver left inside the file. assert.deepEqual(fileRange(11.95, 20, 0, 0, 12), { ok: false, reason: "outside" }); // Both marks before a copy that begins at 3 s. assert.deepEqual(fileRange(0.5, 2.5, 0, 3, 15), { ok: false, reason: "outside" }); }); test("times read H:MM:SS.s", () => { assert.equal(formatClock(0), "0:00:00.0"); assert.equal(formatClock(4), "0:00:04.0"); assert.equal(formatClock(309.64), "0:05:09.6"); assert.equal(formatClock(3599.96), "1:00:00.0"); assert.equal(formatClock(-3), "0:00:00.0"); }); const base = { title: "Synth stream", slug: "ch-abc", ext: ".webm" }; test("where the clip really starts is said before saving", () => { const at = (cueIn: number, cueOut: number, offset: number, startTs: number, endTs: number, fileStart = 0) => describeClip({ ...base, cueIn, cueOut, offset, fileStart, startTs, endTs }).start; assert.equal(at(5, 9, 0, 4, 9), "Starts 1.0 s earlier, at the nearest keyframe (0:00:04.0)."); assert.equal(at(312, 320, 0, 309.6, 320), "Starts 2.4 s earlier, at the nearest keyframe (0:05:09.6)."); // On the cue clock: the keyframe at 6.0 s into the file, offset +1 s, is the cues' 0:05. assert.equal(at(6, 9, 1, 6, 10), "Starts 1.0 s earlier, at the nearest keyframe (0:00:05.0)."); assert.equal(at(5, 9, 1, 6, 10), "Starts at your Clip in mark."); assert.equal(at(5.04, 9, 1, 6, 10), "Starts at your Clip in mark."); // A real start before the transcript's 0:00 (a copy with a lead-in). assert.equal(at(1, 5, 2, 0.6, 7), "Starts 2.4 s earlier, before the transcript's 0:00."); }); test("the range sentence names the offset only when there is one", () => { const range = (offset: number) => describeClip({ ...base, cueIn: 312, cueOut: 363.4, offset, fileStart: 0, startTs: 312 + offset, endTs: 363.4 + offset }).range; assert.equal(range(3), "From 0:05:12.0 to 0:06:03.4, with your offset +3.00 s applied."); assert.equal(range(0), "From 0:05:12.0 to 0:06:03.4."); }); test("a mark outside the copy: the page says what is really written, and the name agrees", () => { // A trimmed copy, offset -30 s, marks at cue 0:10-0:50: the copy begins at cue 0:30. const trimmed = describeClip({ ...base, cueIn: 10, cueOut: 50, offset: -30, fileStart: 0, startTs: 0, endTs: 20 }); assert.equal(trimmed.range, "From 0:00:30.0 to 0:00:50.0, with your offset −30.00 s applied."); assert.equal(trimmed.start, "Your copy begins at 0:00:30.0, after your Clip in mark; the clip starts there."); assert.equal(trimmed.end, null); assert.equal(trimmed.name, "Synth-stream_00h00m30s-00h00m50s.webm"); // The copy ends before the out mark. const short = describeClip({ ...base, cueIn: 5, cueOut: 20, offset: 0, fileStart: 0, startTs: 4, endTs: 12 }); assert.equal(short.range, "From 0:00:05.0 to 0:00:12.0."); assert.equal(short.start, "Starts 1.0 s earlier, at the nearest keyframe (0:00:04.0)."); assert.equal(short.end, "Your copy ends at 0:00:12.0, before your Clip out mark; the clip ends there."); assert.equal(short.name, "Synth-stream_00h00m04s-00h00m12s.webm"); // A copy whose first timestamp is 3 s, whose first keyframe is there too. const late = describeClip({ ...base, cueIn: 1, cueOut: 6, offset: 0, fileStart: 2.98, startTs: 3, endTs: 6 }); assert.equal(late.start, "Your copy begins at 0:00:03.0, after your Clip in mark; the clip starts there."); assert.equal(late.name, "Synth-stream_00h00m03s-00h00m06s.webm"); // The copy begins before the mark but its first keyframe comes after it. const key = describeClip({ ...base, cueIn: 1, cueOut: 6, offset: 0, fileStart: 0, startTs: 2.5, endTs: 6 }); assert.equal(key.start, "Starts 1.5 s later, at your copy's first keyframe (0:00:02.5)."); assert.equal(key.range, "From 0:00:02.5 to 0:00:06.0."); assert.equal(key.name, "Synth-stream_00h00m02s-00h00m06s.webm"); // The copy begins after the mark, and its first keyframe later still. const both = describeClip({ ...base, cueIn: 1, cueOut: 6, offset: 0, fileStart: 2, startTs: 2.5, endTs: 6 }); assert.equal(both.start, "Your copy begins at 0:00:02.0, after your Clip in mark; the clip starts at its first keyframe (0:00:02.5)."); }); test("the saved file is named from the title and the clip's real times, never the picked file", () => { assert.equal(clipFileName("Some Stream: Title?", "ch-abc", 309.6, 363.4, ".mp4"), "Some-Stream-Title_00h05m09s-00h06m04s.mp4"); assert.equal(clipFileName("Ünïcode — 日本語 / x", "s", 4, 9, ".webm"), "Ünïcode-日本語-x_00h00m04s-00h00m09s.webm"); // Windows-forbidden characters never survive. const name = clipFileName('ac:d"e/f\\g|h?i*j', "s", 0, 1, ".mkv"); assert.equal(name, "a-b-c-d-e-f-g-h-i-j_00h00m00s-00h00m01s.mkv"); // Empty title: the slug; neither: "clip". assert.equal(clipFileName(" ", "ch-abc", 0, 1, ".mp4"), "ch-abc_00h00m00s-00h00m01s.mp4"); assert.equal(clipFileName("", "", 0, 1, ".mp4"), "clip_00h00m00s-00h00m01s.mp4"); // Cut at 80 characters, never ending in a dash. const long = clipFileName("word ".repeat(40), "s", 0, 1, ".mp4"); const base = long.split("_")[0]; assert.ok(base.length <= 80, base); assert.ok(!base.endsWith("-")); // An hour boundary, and a real start before 0 clamped. assert.equal(clipFileName("t", "s", 3599.5, 3600, ".mp4"), "t_00h59m59s-01h00m00s.mp4"); assert.equal(clipFileName("t", "s", -2.4, 9, ".mp4"), "t_00h00m00s-00h00m09s.mp4"); // A float a hair over a whole second does not round the end up. assert.equal(clipFileName("t", "s", 4, 9.0000001, ".mp4"), "t_00h00m04s-00h00m09s.mp4"); }); const SUPPORT: OutputSupport = { mp4: ["avc", "hevc", "vp9", "av1", "vp8", "aac", "opus", "mp3", "flac"], mov: ["avc", "hevc", "vp9", "av1", "vp8", "aac", "opus", "mp3", "flac", "pcm-u8"], mkv: ["avc", "hevc", "vp9", "av1", "vp8", "aac", "opus", "mp3", "flac", "vorbis"], webm: ["vp9", "av1", "vp8", "opus", "vorbis"], }; test("the clip keeps its container family", () => { assert.deepEqual(chooseOutput("mp4", "avc", "aac", SUPPORT), { ok: true, format: "mp4", ext: ".mp4" }); assert.deepEqual(chooseOutput("mp4", null, "aac", SUPPORT), { ok: true, format: "mp4", ext: ".m4a" }); assert.deepEqual(chooseOutput("mov", "avc", "aac", SUPPORT), { ok: true, format: "mov", ext: ".mov" }); assert.deepEqual(chooseOutput("webm", "vp9", "opus", SUPPORT), { ok: true, format: "webm", ext: ".webm" }); assert.deepEqual(chooseOutput("webm", null, "opus", SUPPORT), { ok: true, format: "webm", ext: ".webm" }); assert.deepEqual(chooseOutput("mkv", "avc", "aac", SUPPORT), { ok: true, format: "mkv", ext: ".mkv" }); assert.deepEqual(chooseOutput("mkv", null, "aac", SUPPORT), { ok: true, format: "mkv", ext: ".mka" }); }); test("a codec the family cannot hold falls back once (MP4 → MOV, WebM → MKV), else is refused", () => { assert.deepEqual(chooseOutput("mp4", "avc", "pcm-u8", SUPPORT), { ok: true, format: "mov", ext: ".mov" }); assert.deepEqual(chooseOutput("webm", "avc", "opus", SUPPORT), { ok: true, format: "mkv", ext: ".mkv" }); assert.deepEqual(chooseOutput("mkv", "avc", "pcm-u8", SUPPORT), { ok: false, track: "audio", codec: "pcm-u8" }); assert.deepEqual(chooseOutput("mp4", "mpeg4", "aac", SUPPORT), { ok: false, track: "video", codec: "mpeg4" }); // An unknown codec (the library could not name it). assert.deepEqual(chooseOutput("mp4", "", "aac", SUPPORT), { ok: false, track: "video", codec: "" }); // Nothing to copy at all. assert.deepEqual(chooseOutput("mp4", null, null, SUPPORT), { ok: false, track: "none", codec: "" }); }); test("the size is estimated from the file's own byte rate", () => { assert.equal(estimateBytes(1_000_000, 100, 10), 100_000); assert.equal(estimateBytes(1_000_000, 0, 10), 1_000_000); assert.equal(estimateBytes(1_000_000, null, 10), 1_000_000); assert.equal(estimateBytes(1_000_000, 100, 1000), 1_000_000); assert.equal(formatBytes(512), "1 KB"); assert.equal(formatBytes(47_800_000), "48 MB"); assert.equal(formatBytes(4_780_000), "4.8 MB"); assert.equal(formatBytes(2_300_000_000), "2.3 GB"); }); test("memory for a small clip, the disk for a big one when the browser can, else a refusal over 256 MB", () => { assert.equal(MEMORY_LIMIT_BYTES, 256_000_000); assert.equal(DISK_SAVE_ABOVE_BYTES, 128_000_000); assert.equal(savePath(10_000_000, true), "memory"); assert.equal(savePath(10_000_000, false), "memory"); assert.equal(savePath(DISK_SAVE_ABOVE_BYTES + 1, true), "disk"); assert.equal(savePath(DISK_SAVE_ABOVE_BYTES + 1, false), "memory"); assert.equal(savePath(MEMORY_LIMIT_BYTES, false), "memory"); assert.equal(savePath(MEMORY_LIMIT_BYTES + 1, false), "too-big"); assert.equal(savePath(MEMORY_LIMIT_BYTES + 1, true), "disk"); }); test("every refusal is one plain sentence", () => { assert.equal(refusalSentence({ kind: "container" }), "This file can't be clipped here: clipping takes MP4, MOV, M4A, WebM or MKV."); assert.equal( refusalSentence({ kind: "codec", track: "video", codec: "mpeg4" }), "This file's video (mpeg4) can't be copied into a clip without re-encoding, which this page does not do.", ); assert.equal( refusalSentence({ kind: "codec", track: "audio", codec: "" }), "This file's audio (an unknown format) can't be copied into a clip without re-encoding, which this page does not do.", ); assert.equal(refusalSentence({ kind: "codec", track: "none", codec: "" }), "This file has no video or audio to clip."); assert.equal( refusalSentence({ kind: "outside", duration: 2530, offset: 3 }), "Both marks are outside your copy (it is 0:42:10.0 long, offset +3.00 s).", ); // A copy whose length is not known never reads "0:00:00.0 long". assert.equal(refusalSentence({ kind: "outside", duration: null, offset: 3 }), "Both marks are outside your copy (offset +3.00 s)."); assert.equal(refusalSentence({ kind: "short" }), "Mark a clip at least 0.1 s long."); assert.equal( refusalSentence({ kind: "too-big", bytes: 2_300_000_000 }), "This clip would be about 2.3 GB, too big to build in this browser's memory (at most 256 MB). Mark a shorter clip, or use a Chromium-based browser, which can save it straight to disk.", ); // Bigger than the estimate said, found while it was being made. assert.equal( refusalSentence({ kind: "grew", canSaveToDisk: false }), "This clip grew past 256 MB while it was being made, too big for this browser's memory; nothing was saved. Mark a shorter clip, or use a Chromium-based browser, which can save it straight to disk.", ); assert.equal( refusalSentence({ kind: "grew", canSaveToDisk: true }), "This clip grew past 256 MB while it was being made, too big for this browser's memory; nothing was saved. Save it to a file you choose instead, or mark a shorter clip.", ); assert.equal( refusalSentence({ kind: "read" }), "Your browser could not read the file. It may have moved or changed since you picked it; pick it again.", ); assert.equal( refusalSentence({ kind: "no-key" }), "Your copy has no keyframe before your Clip out mark, so no clip can start there. Mark a later Clip out.", ); assert.equal(refusalSentence({ kind: "copy" }), "This file could not be copied into a clip; nothing was saved."); });