import { describe, expect, it } from 'vitest'; import { parseItem, parseScores } from './parse'; import { isLocalAddress } from './account'; import { partAt } from './playback'; import { headerSafe } from './client'; import { duration, formatBadges, isWatched, remaining, washGradient } from '../app/format'; import { buildFeatures } from '../screens/discover'; import type { Hub } from './types'; const movie = (over: Record = {}) => parseItem({ ratingKey: '9776', key: '/library/metadata/9776', type: 'movie', title: '2 Fast 2 Furious', summary: 'An ex-cop…', art: '/art', year: 2003, duration: 6000000, Image: [{ type: 'clearLogo', url: '/logo' }], UltraBlurColors: [{ topLeft: '162e4f', topRight: '30618f', bottomRight: '345e96', bottomLeft: '1c2c56' }], Media: [ { id: '1', videoResolution: '4k', audioChannels: 8, Part: [ { id: '10', key: '/library/parts/10/file.mkv', duration: 3000000, Stream: [ { id: 's1', streamType: 1, codec: 'hevc', DOVIPresent: true, index: 0 }, { id: 's2', streamType: 2, codec: 'truehd', displayTitle: 'English (TrueHD 7.1 Atmos)', index: 1 }, ], }, { id: '11', key: '/library/parts/11/file.mkv', duration: 3000000 }, ], }, ], ...over, }); describe('parsing', () => { it('reads logos, UltraBlur colors and format facts', () => { const m = movie(); expect(m.logo).toBe('/logo'); expect(m.ultraBlur?.topRight).toBe('30618f'); expect(m.media[0].dynamicRange).toBe('Dolby Vision'); expect(m.media[0].atmos).toBe(true); expect(formatBadges(m)).toEqual(['4K', 'DOLBY VISION', 'ATMOS']); }); it('keeps audience scores and never substitutes critic ratings', () => { const scores = parseScores({ Rating: [ { image: 'rottentomatoes://image.rating.upright', value: 7.9, type: 'audience' }, { image: 'rottentomatoes://image.rating.ripe', value: 9.1, type: 'critic' }, { image: 'imdb://image.rating', value: 7.2, type: 'audience' }, ], }); expect(scores.filter((s) => s.kind === 'audience').map((s) => s.source)).toEqual(['Rotten Tomatoes', 'IMDb']); expect(scores.find((s) => s.kind === 'critic')?.value).toBe(9.1); }); }); describe('watched rules', () => { it('treats a resumed title as watched only past the threshold', () => { expect(isWatched(movie({ viewCount: 1, viewOffset: 3000000 }), 90)).toBe(false); expect(isWatched(movie({ viewCount: 1, viewOffset: 5700000 }), 90)).toBe(true); expect(isWatched(movie({ viewCount: 1 }), 90)).toBe(true); }); it('uses episode counts for shows', () => { expect(isWatched(parseItem({ type: 'show', leafCount: 10, viewedLeafCount: 10 }))).toBe(true); expect(isWatched(parseItem({ type: 'show', leafCount: 10, viewedLeafCount: 3 }))).toBe(false); }); it('formats durations and time left', () => { expect(duration(6000000)).toBe('1h 40m'); expect(duration(45 * 60000)).toBe('45m'); expect(remaining(movie({ viewOffset: 3000000 }))).toBe('50m left'); }); }); describe('connections', () => { it('recognizes local plex.direct and private addresses', () => { expect(isLocalAddress('https://192-168-50-81.abc.plex.direct:32400')).toBe(true); expect(isLocalAddress('http://10.0.0.5:32400')).toBe(true); expect(isLocalAddress('https://38-130-3-235.abc.plex.direct:27015')).toBe(false); }); }); describe('playback', () => { it('maps a position to the right part of a multi-part title', () => { const m = movie().media[0]; expect(partAt(m, 1000).part.id).toBe('10'); expect(partAt(m, 4000000)).toMatchObject({ local: 1000000 }); }); }); describe('features', () => { it('interleaves sources, skips in-progress titles and needs artwork', () => { const hub = (id: string, items: Record[]): Hub => ({ id, key: '', title: id, type: 'movie', context: '', size: items.length, more: false, style: '', items: items.map((i) => movie(i)) }); const features = buildFeatures([ hub('movie.recentlyreleased.1', [{ ratingKey: 'a' }, { ratingKey: 'b', viewOffset: 100 }]), hub('movie.topunwatched.1', [{ ratingKey: 'c' }, { ratingKey: 'd', art: '' }]), ]); expect(features.map((f) => f.item.id).sort()).toEqual(['a', 'c']); }); it('builds an artwork wash from UltraBlur colors', () => { expect(washGradient(movie().ultraBlur)).toContain('rgba(48, 97, 143'); }); }); describe('headerSafe', () => { it('turns Mac computer names into ASCII header values', () => { expect(headerSafe('Elijah’s MacBook Pro')).toBe("Elijah's MacBook Pro"); expect(headerSafe('Zoë’s iMac — Studio')).toBe("Zoe's iMac - Studio"); }); });