UNPKG

opex-yt-id

Version:

Extracts YouTube video, channel, and playlist IDs from various URLs

202 lines (195 loc) 10.3 kB
import { test, describe } from 'node:test'; import assert from 'node:assert/strict'; import { getYouTubeVideoId, getYouTubeChannelId, getYouTubePlaylistId } from './index.js'; // Assuming test.js is in the root directory // --- Test Data --- const videoId = 'dQw4w9WgXcQ'; const channelId = 'UCBR8-60-B28hp2BmDPdntcQ'; const playlistId = 'PLBCF2DAC6FFB574DE'; const altVideoId = 'jNQXAC9IVRw'; const shortVideoId = 'abcdefghijk'; const thirdPartyVideoId = 'lmnopqrs_tu'; const genericVideoId = '123456789_A'; const altPlaylistId = 'PLIivdWyY5sqL9mXChOLu_U4Q431r9fIGN'; // --- Video ID Tests --- describe('getYouTubeVideoId', () => { test('should return ID from standard watch URL', () => { assert.strictEqual(getYouTubeVideoId(`https://www.youtube.com/watch?v=${videoId}`), videoId); }); test('should return ID from short URL (youtu.be)', () => { assert.strictEqual(getYouTubeVideoId(`https://youtu.be/${altVideoId}`), altVideoId); }); test('should return ID from short URL with params (youtu.be)', () => { assert.strictEqual(getYouTubeVideoId(`https://youtu.be/${altVideoId}?t=15`), altVideoId); }); test('should return ID from alternative short URL (y2u.be)', () => { assert.strictEqual(getYouTubeVideoId(`https://y2u.be/${altVideoId}`), altVideoId); }); test('should return ID from embed URL', () => { assert.strictEqual(getYouTubeVideoId(`https://www.youtube.com/embed/${videoId}`), videoId); }); test('should return ID from nocookie embed URL', () => { assert.strictEqual(getYouTubeVideoId(`https://www.youtube-nocookie.com/embed/${shortVideoId}`), shortVideoId); }); test('should return ID from shorts URL', () => { assert.strictEqual(getYouTubeVideoId(`https://www.youtube.com/shorts/${altVideoId}`), altVideoId); }); test('should return ID from live URL', () => { assert.strictEqual(getYouTubeVideoId(`https://www.youtube.com/live/${altVideoId}?feature=share`), altVideoId); }); test('should return ID from music URL', () => { assert.strictEqual(getYouTubeVideoId(`https://music.youtube.com/watch?v=${videoId}&list=RDAMVMdQw4w9WgXcQ`), videoId); }); test('should return ID from kids URL', () => { assert.strictEqual(getYouTubeVideoId(`https://kids.youtube.com/watch?v=${videoId}`), videoId); }); test('should return ID from image URL (vi)', () => { assert.strictEqual(getYouTubeVideoId(`https://i.ytimg.com/vi/${videoId}/hqdefault.jpg`), videoId); }); test('should return ID from image URL (an_webp)', () => { assert.strictEqual(getYouTubeVideoId(`https://i.ytimg.com/an_webp/${videoId}/mqdefault_6s.webp?du=3000&sqp=CPSSj7AG&rs=AOn4CLDxZ`), videoId); }); test('should return ID from legacy /v/ URL', () => { assert.strictEqual(getYouTubeVideoId(`https://www.youtube.com/v/${videoId}`), videoId); }); test('should return ID from legacy /vi/ URL', () => { assert.strictEqual(getYouTubeVideoId(`https://www.youtube.com/vi/${videoId}`), videoId); }); test('should return ID from legacy /e/ URL', () => { assert.strictEqual(getYouTubeVideoId(`https://www.youtube.com/e/${videoId}`), videoId); }); test('should return ID from iframe src', () => { assert.strictEqual(getYouTubeVideoId(`<iframe width="560" height="315" src="https://www.youtube.com/embed/${shortVideoId}" title="YouTube video player" frameborder="0"></iframe>`), shortVideoId); }); test('should return ID from iframe src (nocookie)', () => { assert.strictEqual(getYouTubeVideoId(`<iframe src='https://www.youtube-nocookie.com/embed/${videoId}'></iframe>`), videoId); }); // *** Removed Google Redirect Tests *** test('should return ID from youtube:// scheme', () => { assert.strictEqual(getYouTubeVideoId(`youtube://watch?v=${videoId}`), videoId); }); test('should return ID from youtube:// scheme (short)', () => { assert.strictEqual(getYouTubeVideoId(`youtube://youtu.be/${videoId}`), videoId); }); test('should return ID from third-party frontend (Invidious)', () => { assert.strictEqual(getYouTubeVideoId(`https://yewtu.be/watch?v=${thirdPartyVideoId}`), thirdPartyVideoId); }); test('should return ID from third-party frontend (Piped)', () => { assert.strictEqual(getYouTubeVideoId(`https://piped.kavin.rocks/watch?v=${videoId}`), videoId); }); test('should return ID from generic fallback domain', () => { assert.strictEqual(getYouTubeVideoId(`https://some-random-proxy.net/watch?v=${genericVideoId}`), genericVideoId); }); test('should return ID from attribution link', () => { const url = `https://www.youtube.com/attribution_link?a=8_I2p3oL4pI&u=%2Fwatch%3Fv%3D${videoId}%26feature%3Dshare`; assert.strictEqual(getYouTubeVideoId(url), videoId); }); test('should return ID from user page fragment', () => { const url = `https://www.youtube.com/user/UserName/videos#p/u/1/${videoId}`; assert.strictEqual(getYouTubeVideoId(url), videoId); }); test('should return null for invalid ID format', () => { assert.strictEqual(getYouTubeVideoId('https://www.youtube.com/watch?v=invalidID'), null); }); test('should return null for non-URL string', () => { assert.strictEqual(getYouTubeVideoId('this is not a url'), null); }); test('should return null for empty string', () => { assert.strictEqual(getYouTubeVideoId(''), null); }); test('should return null for null input', () => { assert.strictEqual(getYouTubeVideoId(null), null); }); test('should return null for undefined input', () => { assert.strictEqual(getYouTubeVideoId(undefined), null); }); test('should return null for channel URL', () => { assert.strictEqual(getYouTubeVideoId(`https://www.youtube.com/channel/${channelId}`), null); }); test('should return null for playlist URL', () => { assert.strictEqual(getYouTubeVideoId(`https://www.youtube.com/playlist?list=${playlistId}`), null); }); }); // --- Channel ID Tests --- describe('getYouTubeChannelId', () => { test('should return ID from standard channel URL', () => { assert.strictEqual(getYouTubeChannelId(`https://www.youtube.com/channel/${channelId}`), channelId); }); test('should return ID from mobile channel URL', () => { assert.strictEqual(getYouTubeChannelId(`https://m.youtube.com/channel/${channelId}`), channelId); }); test('should return ID from third-party frontend channel URL', () => { assert.strictEqual(getYouTubeChannelId(`https://vid.puffyan.us/channel/${channelId}`), channelId); }); // *** Removed Google Redirect Test *** test('should return null for custom URL (/c/)', () => { assert.strictEqual(getYouTubeChannelId('https://www.youtube.com/c/YouTubeCreators'), null); }); test('should return null for legacy user URL (/user/)', () => { assert.strictEqual(getYouTubeChannelId('https://www.youtube.com/user/YouTube'), null); }); test('should return null for handle URL (/@/)', () => { assert.strictEqual(getYouTubeChannelId('https://www.youtube.com/@youtube'), null); }); test('should return null for video URL', () => { assert.strictEqual(getYouTubeChannelId(`https://www.youtube.com/watch?v=${videoId}`), null); }); test('should return null for playlist URL', () => { assert.strictEqual(getYouTubeChannelId(`https://www.youtube.com/playlist?list=${playlistId}`), null); }); test('should return null for invalid ID format', () => { assert.strictEqual(getYouTubeChannelId('https://www.youtube.com/channel/invalid'), null); }); test('should return null for non-URL string', () => { assert.strictEqual(getYouTubeChannelId('this is not a url'), null); }); test('should return null for empty string', () => { assert.strictEqual(getYouTubeChannelId(''), null); }); test('should return null for null input', () => { assert.strictEqual(getYouTubeChannelId(null), null); }); test('should return null for undefined input', () => { assert.strictEqual(getYouTubeChannelId(undefined), null); }); }); // --- Playlist ID Tests --- describe('getYouTubePlaylistId', () => { test('should return ID from standard playlist URL', () => { assert.strictEqual(getYouTubePlaylistId(`https://www.youtube.com/playlist?list=${playlistId}`), playlistId); }); test('should return ID from watch URL with list parameter', () => { assert.strictEqual(getYouTubePlaylistId(`https://www.youtube.com/watch?v=${videoId}&list=${altPlaylistId}&index=2`), altPlaylistId); }); test('should return ID from music playlist URL', () => { assert.strictEqual(getYouTubePlaylistId(`https://music.youtube.com/playlist?list=${playlistId}`), playlistId); }); // *** Removed Google Redirect Tests *** test('should return null for video URL without list', () => { assert.strictEqual(getYouTubePlaylistId(`https://www.youtube.com/watch?v=${videoId}`), null); }); test('should return null for channel URL', () => { assert.strictEqual(getYouTubePlaylistId(`https://www.youtube.com/channel/${channelId}`), null); }); test('should return null for invalid list ID format', () => { assert.strictEqual(getYouTubePlaylistId('https://www.youtube.com/playlist?list=invalid'), null); }); test('should return null for list ID too short', () => { assert.strictEqual(getYouTubePlaylistId('https://www.youtube.com/playlist?list=PLshort'), null); }); test('should return null for non-URL string', () => { assert.strictEqual(getYouTubePlaylistId('this is not a url'), null); }); test('should return null for empty string', () => { assert.strictEqual(getYouTubePlaylistId(''), null); }); test('should return null for null input', () => { assert.strictEqual(getYouTubePlaylistId(null), null); }); test('should return null for undefined input', () => { assert.strictEqual(getYouTubePlaylistId(undefined), null); }); });