Files
AeroFetch/test/clipboardLink.test.ts
T
debont80 d050e48af6 H2 (URL half): one canonical youtubeId in urlHelpers
Consolidates the scattered YouTube-URL parsing (the clean, renderer-contained
half of H2):

- Move the complete youtubeId (watch/shorts/embed/live/youtu.be) into
  lib/urlHelpers.ts as the single home.
- thumb.ts, queueStats.ts (sameVideo), and store/downloads.ts (titleFromUrl)
  now import it; the three ad-hoc reimplementations are removed.
- titleFromUrl gains correctness: it previously labeled ANY host with a ?v=
  param as "YouTube video (…)" and missed youtu.be/shorts; it's now host-correct
  and covers every YouTube shape. sameVideo now also dedupes /shorts/ID against
  /watch?v=ID.
- Unit-tested in test/clipboardLink.test.ts (+4 cases, 247 pass).

The formatter half of H2 is deferred to travel with H4: the renderer re-parses
speed/eta strings only because raw numbers aren't carried across the IPC boundary
(H4), and fmtBytes (1024) vs formatSpeed (1000) differ intentionally.

typecheck + 247 tests + eslint + prettier green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-30 20:54:09 -04:00

106 lines
4.7 KiB
TypeScript

import { describe, it, expect } from 'vitest'
// Import from the pure utility module directly — no Zustand store init needed (L40).
import { looksLikeUrl, looksLikeSingleVideo, 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)
})
})