import { test } from "node:test"; import assert from "node:assert/strict"; import { channelSkipsShorts, isShortsUrl, skippedShortIds, skipsAsShort, } from "./skipShorts"; import { extractVideoId } from "./videoId"; import { parseChannelConfig } from "./channelConfig"; import { channelConfigSchema } from "./channelConfigSchema"; // Run with: node_modules/.bin/tsx --test common/lib/skipShorts.test.ts test("isShortsUrl: a youtube.com /shorts/ path, as the listing spells it", () => { for (const u of [ "https://www.youtube.com/shorts/EZNHvnapR_s", "https://youtube.com/shorts/EZNHvnapR_s", "https://m.youtube.com/shorts/EZNHvnapR_s?feature=share", "https://www.youtube.com/Shorts/EZNHvnapR_s", ]) { assert.equal(isShortsUrl(u), true, u); } for (const u of [ "https://www.youtube.com/watch?v=EZNHvnapR_s", "https://www.youtube.com/shorts", "https://www.youtube.com/@x/shorts", "https://youtu.be/EZNHvnapR_s", "https://rumble.com/shorts/v123-x.html", "https://notyoutube.com/shorts/EZNHvnapR_s", "not a url", "", ]) { assert.equal(isShortsUrl(u), false, u); } }); test("only `skipShorts: true` skips; absent, false or junk keep today's behaviour", () => { const short = "https://www.youtube.com/shorts/EZNHvnapR_s"; assert.equal(channelSkipsShorts({ skipShorts: true }), true); assert.equal(skipsAsShort({ skipShorts: true }, short), true); assert.equal(skipsAsShort({ skipShorts: true }, "https://www.youtube.com/watch?v=EZNHvnapR_s"), false); for (const c of [null, undefined, {}, { skipShorts: false }]) { assert.equal(channelSkipsShorts(c), false); assert.equal(skipsAsShort(c, short), false); } }); test("skippedShortIds: the Shorts' ids, and none on a channel that keeps them", () => { const urls = [ "https://www.youtube.com/watch?v=aaaaaaaaaaa", "https://www.youtube.com/shorts/bbbbbbbbbbb", "", ]; assert.deepEqual([...skippedShortIds({ skipShorts: true }, urls, extractVideoId)], ["bbbbbbbbbbb"]); assert.equal(skippedShortIds({}, urls, extractVideoId).size, 0); }); test("the config key: true is kept, anything else is dropped (both compositions)", () => { const base = { handling: "youtube", url: "https://www.youtube.com/@x" }; assert.equal(parseChannelConfig({ ...base, skipShorts: true })?.skipShorts, true); assert.equal(channelConfigSchema.parse({ ...base, skipShorts: true })?.skipShorts, true); for (const v of [false, "true", 1, null]) { assert.equal("skipShorts" in (parseChannelConfig({ ...base, skipShorts: v }) ?? {}), false, String(v)); assert.equal("skipShorts" in (channelConfigSchema.parse({ ...base, skipShorts: v }) ?? {}), false, String(v)); } });