import { describe, it, expect } from 'vitest' // Import from the pure utility module directly — no Zustand store init needed (L40). import { looksLikeUrl, looksLikeSingleVideo, looksLikeChannelOrPlaylist, youtubeId } from '../src/renderer/src/lib/urlHelpers' describe('youtubeId', () => { it('extracts the id from a watch URL', () => { expect(youtubeId('https://www.youtube.com/watch?v=dQw4w9WgXcQ')).toBe('dQw4w9WgXcQ') expect(youtubeId('https://youtube.com/watch?v=abc123&list=xyz')).toBe('abc123') expect(youtubeId('https://music.youtube.com/watch?v=musicId')).toBe('musicId') }) it('extracts the id from a youtu.be short link', () => { expect(youtubeId('https://youtu.be/dQw4w9WgXcQ')).toBe('dQw4w9WgXcQ') expect(youtubeId('https://youtu.be/abc123?t=42')).toBe('abc123') }) it('extracts the id from shorts / embed / live / v paths', () => { expect(youtubeId('https://www.youtube.com/shorts/shortId1')).toBe('shortId1') expect(youtubeId('https://youtube.com/embed/embedId2')).toBe('embedId2') expect(youtubeId('https://www.youtube.com/live/liveId3')).toBe('liveId3') expect(youtubeId('https://youtube.com/v/vId4?fs=1')).toBe('vId4') }) it('returns null for non-YouTube, unparseable, or empty input', () => { expect(youtubeId('https://vimeo.com/12345')).toBeNull() expect(youtubeId('https://notyoutube.com/watch?v=x')).toBeNull() expect(youtubeId('https://www.youtube.com/@somechannel')).toBeNull() expect(youtubeId('not a url')).toBeNull() expect(youtubeId(undefined)).toBeNull() expect(youtubeId('')).toBeNull() }) }) describe('looksLikeUrl', () => { it('accepts http(s) URLs', () => { expect(looksLikeUrl('https://youtube.com/watch?v=abc')).toBe(true) expect(looksLikeUrl('http://example.com')).toBe(true) expect(looksLikeUrl('https://example.com/a/b?c=d#frag')).toBe(true) }) it('is scheme-case-insensitive', () => { expect(looksLikeUrl('HTTPS://example.com')).toBe(true) expect(looksLikeUrl('HtTp://example.com')).toBe(true) }) it('trims surrounding whitespace', () => { expect(looksLikeUrl(' https://example.com\n')).toBe(true) expect(looksLikeUrl('\t http://example.com \t')).toBe(true) }) it('rejects non-http(s) schemes', () => { expect(looksLikeUrl('ftp://example.com')).toBe(false) expect(looksLikeUrl('file:///c:/x')).toBe(false) expect(looksLikeUrl('magnet:?xt=urn:btih:abc')).toBe(false) expect(looksLikeUrl('javascript:alert(1)')).toBe(false) expect(looksLikeUrl('mailto:a@b.com')).toBe(false) }) it('rejects bare domains and scheme-less text', () => { expect(looksLikeUrl('www.example.com')).toBe(false) expect(looksLikeUrl('example.com/watch')).toBe(false) expect(looksLikeUrl('see http://example.com')).toBe(false) // must start with the scheme }) it('rejects empty, whitespace, and malformed input', () => { expect(looksLikeUrl('')).toBe(false) expect(looksLikeUrl(' ')).toBe(false) expect(looksLikeUrl('not a link')).toBe(false) expect(looksLikeUrl('http://')).toBe(false) // passes the prefix test but is not a valid URL }) }) describe('looksLikeSingleVideo', () => { it('flags YouTube single-video URLs', () => { expect(looksLikeSingleVideo('https://www.youtube.com/watch?v=dQw4w9WgXcQ')).toBe(true) expect(looksLikeSingleVideo('https://youtube.com/watch?v=abc123')).toBe(true) expect(looksLikeSingleVideo('https://m.youtube.com/watch?v=abc123')).toBe(true) expect(looksLikeSingleVideo('https://music.youtube.com/watch?v=abc123')).toBe(true) expect(looksLikeSingleVideo('https://youtu.be/dQw4w9WgXcQ')).toBe(true) }) it('keeps anything with a playlist context (?list=)', () => { // A video opened in a playlist → the user likely wants the whole playlist. expect(looksLikeSingleVideo('https://www.youtube.com/watch?v=abc&list=PL123')).toBe(false) expect(looksLikeSingleVideo('https://youtu.be/abc?list=PL123')).toBe(false) }) it('keeps channels and playlists', () => { expect(looksLikeSingleVideo('https://www.youtube.com/@SomeChannel')).toBe(false) expect(looksLikeSingleVideo('https://www.youtube.com/channel/UC123')).toBe(false) expect(looksLikeSingleVideo('https://www.youtube.com/c/SomeChannel')).toBe(false) expect(looksLikeSingleVideo('https://www.youtube.com/playlist?list=PL123')).toBe(false) }) it('does not flag single videos on other sites (conservative)', () => { // We can't reliably classify arbitrary hosts, so never reject them. expect(looksLikeSingleVideo('https://vimeo.com/123456789')).toBe(false) expect(looksLikeSingleVideo('https://example.com/watch?v=abc')).toBe(false) }) it('returns false for non-URLs', () => { expect(looksLikeSingleVideo('not a url')).toBe(false) expect(looksLikeSingleVideo('')).toBe(false) }) }) describe('looksLikeChannelOrPlaylist', () => { it('flags YouTube channel URLs', () => { expect(looksLikeChannelOrPlaylist('https://www.youtube.com/@SomeChannel')).toBe(true) expect(looksLikeChannelOrPlaylist('https://www.youtube.com/@SomeChannel/videos')).toBe(true) expect(looksLikeChannelOrPlaylist('https://www.youtube.com/channel/UC123')).toBe(true) expect(looksLikeChannelOrPlaylist('https://www.youtube.com/c/SomeChannel')).toBe(true) expect(looksLikeChannelOrPlaylist('https://www.youtube.com/user/SomeUser')).toBe(true) expect(looksLikeChannelOrPlaylist('https://m.youtube.com/@SomeChannel')).toBe(true) }) it('flags a dedicated playlist page', () => { expect(looksLikeChannelOrPlaylist('https://www.youtube.com/playlist?list=PL123')).toBe(true) expect(looksLikeChannelOrPlaylist('https://music.youtube.com/playlist?list=PL123')).toBe(true) }) it('does not flag single videos, even inside a playlist context', () => { // A /watch video belongs in the Downloads bar, not the Library — even with a list. expect(looksLikeChannelOrPlaylist('https://www.youtube.com/watch?v=abc')).toBe(false) expect(looksLikeChannelOrPlaylist('https://www.youtube.com/watch?v=abc&list=PL123')).toBe(false) expect(looksLikeChannelOrPlaylist('https://youtu.be/dQw4w9WgXcQ')).toBe(false) }) it('does not flag non-YouTube hosts or a bare playlist path without a list', () => { expect(looksLikeChannelOrPlaylist('https://vimeo.com/channels/staffpicks')).toBe(false) expect(looksLikeChannelOrPlaylist('https://example.com/@handle')).toBe(false) expect(looksLikeChannelOrPlaylist('https://www.youtube.com/playlist')).toBe(false) }) it('returns false for non-URLs', () => { expect(looksLikeChannelOrPlaylist('not a url')).toBe(false) expect(looksLikeChannelOrPlaylist('')).toBe(false) }) })