utils.test.js
1 import { describe, expect, it, vi } from 'vitest'; 2 import { extractJsonLd, forceEnglishUrl, formatDuration, getCurrentImdbId, isChallengePage, normalizeImdbTitleType, normalizeImdbId, waitForImdbPath, waitForImdbReviewsReady, waitForImdbSearchReady, } from './utils.js'; 3 describe('normalizeImdbId', () => { 4 it('passes through bare ids', () => { 5 expect(normalizeImdbId('tt1375666', 'tt')).toBe('tt1375666'); 6 expect(normalizeImdbId('nm0634240', 'nm')).toBe('nm0634240'); 7 }); 8 it('extracts ids from supported urls', () => { 9 expect(normalizeImdbId('https://www.imdb.com/title/tt1375666/', 'tt')).toBe('tt1375666'); 10 expect(normalizeImdbId('https://m.imdb.com/title/tt1375666/', 'tt')).toBe('tt1375666'); 11 expect(normalizeImdbId('https://www.imdb.com/de/title/tt1375666/?ref_=nv_sr_srsg_0', 'tt')).toBe('tt1375666'); 12 expect(normalizeImdbId('https://www.imdb.com/name/nm0634240/', 'nm')).toBe('nm0634240'); 13 }); 14 it('throws on invalid or mismatched ids', () => { 15 expect(() => normalizeImdbId('invalid', 'tt')).toThrow('Invalid IMDb ID'); 16 expect(() => normalizeImdbId('tt1', 'tt')).toThrow('Invalid IMDb ID'); 17 expect(() => normalizeImdbId('nm0634240', 'tt')).toThrow('Invalid IMDb ID'); 18 }); 19 }); 20 describe('formatDuration', () => { 21 it('converts ISO-8601 durations to a short human format', () => { 22 expect(formatDuration('PT2H28M')).toBe('2h 28m'); 23 expect(formatDuration('PT1H')).toBe('1h'); 24 expect(formatDuration('PT45M')).toBe('45m'); 25 expect(formatDuration('PT2H')).toBe('2h'); 26 }); 27 it('returns an empty string for invalid input', () => { 28 expect(formatDuration('')).toBe(''); 29 expect(formatDuration('invalid')).toBe(''); 30 }); 31 }); 32 describe('forceEnglishUrl', () => { 33 it('adds the English language parameter', () => { 34 expect(forceEnglishUrl('https://www.imdb.com/title/tt1375666/')).toContain('language=en-US'); 35 }); 36 it('preserves existing query parameters', () => { 37 const result = forceEnglishUrl('https://www.imdb.com/title/tt1375666/?ref_=nv'); 38 expect(result).toContain('language=en-US'); 39 expect(result).toContain('ref_=nv'); 40 }); 41 }); 42 describe('normalizeImdbTitleType', () => { 43 it('maps internal imdb ids to readable labels', () => { 44 expect(normalizeImdbTitleType({ id: 'movie', text: '' })).toBe('Movie'); 45 expect(normalizeImdbTitleType({ id: 'tvSeries', text: '' })).toBe('TV Series'); 46 expect(normalizeImdbTitleType('short')).toBe('Short'); 47 }); 48 it('preserves explicit text labels', () => { 49 expect(normalizeImdbTitleType({ id: 'movie', text: 'Feature Film' })).toBe('Feature Film'); 50 }); 51 }); 52 describe('extractJsonLd', () => { 53 it('returns the evaluated JSON-LD payload', async () => { 54 const page = { 55 evaluate: vi.fn().mockResolvedValue({ '@type': 'Movie', name: 'Inception' }), 56 }; 57 await expect(extractJsonLd(page, 'Movie')).resolves.toEqual({ '@type': 'Movie', name: 'Inception' }); 58 expect(page.evaluate).toHaveBeenCalledTimes(1); 59 expect(page.evaluate).toHaveBeenCalledWith(expect.stringContaining('"Movie"')); 60 }); 61 }); 62 describe('isChallengePage', () => { 63 it('returns true when the page evaluation matches a challenge', async () => { 64 const page = { 65 evaluate: vi.fn().mockResolvedValue(true), 66 }; 67 await expect(isChallengePage(page)).resolves.toBe(true); 68 expect(page.evaluate).toHaveBeenCalledTimes(1); 69 }); 70 }); 71 describe('imdb browser helpers', () => { 72 it('reads the current imdb id from page metadata', async () => { 73 const page = { 74 evaluate: vi.fn().mockResolvedValue('nm0634240'), 75 }; 76 await expect(getCurrentImdbId(page, 'nm')).resolves.toBe('nm0634240'); 77 expect(page.evaluate).toHaveBeenCalledTimes(1); 78 }); 79 it('wait helpers resolve mocked readiness booleans', async () => { 80 const page = { 81 evaluate: vi.fn().mockResolvedValue(true), 82 }; 83 await expect(waitForImdbPath(page, '^/find/?$')).resolves.toBe(true); 84 await expect(waitForImdbSearchReady(page)).resolves.toBe(true); 85 await expect(waitForImdbReviewsReady(page)).resolves.toBe(true); 86 expect(page.evaluate).toHaveBeenCalledTimes(3); 87 }); 88 });