// The Twitch join, option (b) (release 23 F1): a record keeps yt-dlp's `v`, // its directory is the canonical ``, and the join maps one to the other on // the directory side. Cases from release 20 D2's `twitchId.test.ts` // (r20/twitch-ids 433815d4), without its re-keying of records. Synthetic ids. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/videoDirId.test.ts import { after, test } from "node:test"; import assert from "node:assert/strict"; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { channelConfigPlatform, extractVideoId, pickVideoDirId, siteVideoId, twitchVodDirId, twitchVodRecordId, } from "./videoId"; import { recordIdOfVideoDir, resolveVideoDir, resolveVideoDirId, resolveVideoDirIdSync, resolveVideoDirSync } from "./videoDir-server.mjs"; const N = "1234567890"; const META = { id: `v${N}`, webpage_url: `https://www.twitch.tv/videos/${N}` }; test("twitchVodDirId drops only a v before digits; twitchVodRecordId is its reverse", () => { assert.equal(twitchVodDirId(`v${N}`), N); assert.equal(twitchVodDirId(N), null); assert.equal(twitchVodDirId("vod-clip-slug"), null); assert.equal(twitchVodDirId("v"), null); assert.equal(twitchVodDirId("v12a"), null); // The id the URL gives, which is what data// is named. assert.equal(extractVideoId(META.webpage_url), twitchVodDirId(META.id)); assert.equal(twitchVodRecordId(N), `v${N}`); assert.equal(twitchVodRecordId(`v${N}`), null); assert.equal(twitchVodRecordId("abc"), null); assert.equal(twitchVodRecordId(twitchVodDirId(META.id)!), META.id); }); test("siteVideoId: the archive's id — v for a Twitch directory id only, siteVideo first, X digits untouched", () => { assert.equal(siteVideoId({ video: N }, "twitch"), `v${N}`); assert.equal(siteVideoId(`v${N}`, "twitch"), `v${N}`); assert.equal(siteVideoId({ video: N }, "x"), N); assert.equal(siteVideoId({ video: N }, null), N); assert.equal(siteVideoId({ video: N }, undefined), N); assert.equal(siteVideoId({ video: "a-url-slug", siteVideo: "v2embedid" }, "rumble"), "v2embedid"); assert.equal(siteVideoId(null, "twitch"), ""); }); test("channelConfigPlatform: the platform key, else the url's host", () => { assert.equal(channelConfigPlatform({ platform: "twitch" }), "twitch"); assert.equal(channelConfigPlatform({ url: "https://www.twitch.tv/example" }), "twitch"); assert.equal(channelConfigPlatform({ platform: "youtube", url: "https://www.twitch.tv/x" }), "youtube"); assert.equal(channelConfigPlatform({}), null); assert.equal(channelConfigPlatform(null), null); }); test("pickVideoDirId: a Twitch v prefers , then v, then the canonical ", () => { const has = (...ids: string[]) => (id: string) => ids.includes(id); assert.equal(pickVideoDirId(`v${N}`, { platform: "twitch", exists: has(N) }), N); assert.equal(pickVideoDirId(`v${N}`, { platform: "twitch", exists: has(N, `v${N}`) }), N); assert.equal(pickVideoDirId(`v${N}`, { platform: "twitch", exists: has(`v${N}`) }), `v${N}`); assert.equal(pickVideoDirId(`v${N}`, { platform: "twitch", exists: has() }), N); // Already the directory's id. assert.equal(pickVideoDirId(N, { platform: "twitch", exists: has(N) }), N); }); test("pickVideoDirId never guesses a v-prefixed id of another platform", () => { const all = () => true; assert.equal(pickVideoDirId(`v${N}`, { platform: "youtube", exists: all }), `v${N}`); assert.equal(pickVideoDirId("v4abcde", { platform: "rumble", exists: all }), "v4abcde"); assert.equal(pickVideoDirId(`v${N}`, { platform: null, exists: all }), `v${N}`); assert.equal(pickVideoDirId(`v${N}`, { exists: all }), `v${N}`); }); const ROOT = mkdtempSync(path.join(tmpdir(), "video-dir-id-")); after(() => rmSync(ROOT, { recursive: true, force: true })); function channel(slug: string, config: object, dirs: string[]): string { const channelsDir = path.join(ROOT, slug + "-root"); mkdirSync(path.join(channelsDir, slug, "data"), { recursive: true }); writeFileSync(path.join(channelsDir, slug, "config.json"), JSON.stringify(config)); for (const d of dirs) mkdirSync(path.join(channelsDir, slug, "data", d), { recursive: true }); return channelsDir; } test("resolveVideoDirId: a Twitch channel's v record is its data//", async () => { // Platform from the url (no platform key), and from the key. const byUrl = channel("tw-url", { handling: "transcribe", url: "https://www.twitch.tv/example" }, [N]); assert.equal(await resolveVideoDirId(byUrl, "tw-url", `v${N}`), N); assert.equal(await resolveVideoDir(byUrl, "tw-url", `v${N}`), path.join(byUrl, "tw-url", "data", N)); const byKey = channel("tw-key", { handling: "transcribe", platform: "twitch" }, [N]); assert.equal(await resolveVideoDirId(byKey, "tw-key", `v${N}`), N); // The id is never rewritten where it is the directory's own. assert.equal(await resolveVideoDirId(byKey, "tw-key", N), N); // A caller that knows the platform is believed over the config. assert.equal(await resolveVideoDirId(byKey, "tw-key", `v${N}`, { platform: "youtube" }), `v${N}`); }); test("resolveVideoDirId: other platforms and other ids pass through", async () => { const yt = channel("yt", { handling: "youtube", url: "https://www.youtube.com/@x" }, [N, `v${N}`]); assert.equal(await resolveVideoDirId(yt, "yt", `v${N}`), `v${N}`); assert.equal(await resolveVideoDirId(yt, "yt", "dQw4w9WgXcQ"), "dQw4w9WgXcQ"); // No config at all: never guessed. assert.equal(await resolveVideoDirId(path.join(ROOT, "none"), "nobody", `v${N}`), `v${N}`); }); test("the synchronous resolver gives the same answers (release 23 F1b)", () => { const tw = channel("tw-sync", { handling: "transcribe", url: "https://www.twitch.tv/example" }, [N]); assert.equal(resolveVideoDirIdSync(tw, "tw-sync", `v${N}`), N); assert.equal(resolveVideoDirSync(tw, "tw-sync", `v${N}`), path.join(tw, "tw-sync", "data", N)); assert.equal(resolveVideoDirIdSync(tw, "tw-sync", "dQw4w9WgXcQ"), "dQw4w9WgXcQ"); const yt = channel("yt-sync", { handling: "youtube", platform: "youtube" }, [N]); assert.equal(resolveVideoDirIdSync(yt, "yt-sync", `v${N}`), `v${N}`); }); // RUMBLE'S TWO IDS (lane one-clock): a manifest cites the EMBED id, the // directory is named for the URL slug; on a Rumble channel the embed id is // found through the directory's metadata.info.json `id`. test("a Rumble embed id resolves to the URL-slug directory whose metadata names it", async () => { const root = mkdtempSync(path.join(tmpdir(), "rumble-ids-")); try { const data = path.join(root, "rc", "data"); mkdirSync(path.join(data, "v7fxp3e"), { recursive: true }); writeFileSync(path.join(root, "rc", "config.json"), JSON.stringify({ platform: "rumble", url: "https://rumble.com/c/X" })); writeFileSync( path.join(data, "v7fxp3e", "metadata.info.json"), JSON.stringify({ id: "v7drc5w", title: "t", webpage_url: "https://rumble.com/v7fxp3e-t.html" }), ); assert.equal(await resolveVideoDirId(root, "rc", "v7drc5w"), "v7fxp3e"); assert.equal(resolveVideoDirIdSync(root, "rc", "v7drc5w"), "v7fxp3e"); assert.equal(await resolveVideoDir(root, "rc", "v7drc5w"), path.join(data, "v7fxp3e")); // The slug id is its own directory; an id nobody names stays as it is. assert.equal(await resolveVideoDirId(root, "rc", "v7fxp3e"), "v7fxp3e"); assert.equal(await resolveVideoDirId(root, "rc", "v9zzzzz"), "v9zzzzz"); // The inverse: the URL-slug directory's record id, the embed id the archive serves (lane output-modes). assert.equal(await recordIdOfVideoDir(root, "rc", "v7fxp3e"), "v7drc5w"); assert.equal(await recordIdOfVideoDir(root, "rc", "v9zzzzz"), "v9zzzzz", "no metadata: itself"); assert.equal(await recordIdOfVideoDir(root, "rc", "dQw4w9WgXcQ"), "dQw4w9WgXcQ", "not Rumble-shaped: no read"); assert.equal(await recordIdOfVideoDir(root, "rc", "v7fxp3e", { platform: "youtube" }), "v7fxp3e", "not a Rumble channel"); // A directory added later is found once data/ changes. mkdirSync(path.join(data, "v7gwn3w")); writeFileSync(path.join(data, "v7gwn3w", "metadata.info.json"), JSON.stringify({ id: "v7eqa6e" })); assert.equal(await resolveVideoDirId(root, "rc", "v7eqa6e"), "v7gwn3w"); assert.equal(resolveVideoDirIdSync(root, "rc", "v7eqa6e"), "v7gwn3w"); // Not a Rumble channel: never guessed at. writeFileSync(path.join(root, "rc", "config.json"), JSON.stringify({ platform: "youtube" })); assert.equal(await resolveVideoDirId(root, "rc", "v7drc5w"), "v7drc5w"); assert.equal(await resolveVideoDirId(root, "rc", "v7drc5w", { platform: "rumble" }), "v7fxp3e"); } finally { rmSync(root, { recursive: true, force: true }); } });