import { describe, expect, test } from 'vitest'; import type { Playlist } from '#lib/api/types.js'; import { assignShelfCovers, byShelfRank, refreshedLabel, shelfMeta, shelfTileWidth, songsLikeArtist, washTone } from './shelf.js'; function pl(over: Partial): Playlist { return { id: 'p', user_id: 'u', owner_username: 'me', name: 'Mix', description: '', is_public: false, kind: 'system', system_variant: 'for_you', refreshable: true, seed_artist_id: null, cover_url: '', track_count: 100, duration_sec: 0, created_at: '2026-11-23T08:00:00Z', updated_at: '2026-11-23T08:00:00Z', ...over }; } describe('assignShelfCovers', () => { test('each tile takes the first album no earlier tile shows', () => { const got = assignShelfCovers( [ pl({ cover_album_ids: ['a', 'b', 'c'] }), pl({ cover_album_ids: ['a', 'd'] }), pl({ cover_album_ids: ['a', 'd', 'e'] }) ], 1 ); expect(got).toEqual([['a'], ['d'], ['e']]); }); test('a band takes three unused albums', () => { const got = assignShelfCovers( [pl({ cover_album_ids: ['a', 'b', 'c', 'd'] }), pl({ cover_album_ids: ['b', 'c', 'e', 'f', 'g'] })], 3 ); expect(got).toEqual([['a', 'b', 'c'], ['e', 'f', 'g']]); }); test('a tile whose albums are all taken reuses its own rather than showing nothing', () => { const got = assignShelfCovers([pl({ cover_album_ids: ['a'] }), pl({ cover_album_ids: ['a'] })], 1); expect(got).toEqual([['a'], ['a']]); }); test('a playlist with no cover albums gets none', () => { expect(assignShelfCovers([pl({})], 1)).toEqual([[]]); }); }); describe('shelfMeta', () => { const now = new Date('2026-11-23T15:00:00Z'); test('equal sizes and the same refresh read as one line', () => { const mixes = [pl({ track_count: 25 }), pl({ track_count: 25 })]; expect(shelfMeta(mixes, now)).toBe('25 songs each · refreshed today'); }); test('sizes within a tenth of each other read as "about"', () => { const mixes = [95, 97, 99, 100].map((n) => pl({ track_count: n })); expect(shelfMeta(mixes, now)).toBe('About 100 songs each · refreshed today'); }); test('sizes that differ widely are left to the tiles', () => { const mixes = [pl({ track_count: 20 }), pl({ track_count: 100 })]; expect(shelfMeta(mixes, now)).toBe('Refreshed today'); }); test('user playlists do not count, and a shelf of only them says nothing', () => { const mine = pl({ kind: 'user', system_variant: null, track_count: 3, created_at: '2025-01-01T00:00:00Z' }); expect(shelfMeta([mine], now)).toBe(''); expect(shelfMeta([pl({ track_count: 50 }), mine], now)).toBe('50 songs each · refreshed today'); }); test('different refresh days are not summarised', () => { const mixes = [pl({}), pl({ created_at: '2026-11-20T08:00:00Z' })]; expect(shelfMeta(mixes, now)).toBe('100 songs each'); }); }); describe('refreshedLabel', () => { test('yesterday and older', () => { const now = new Date('2026-11-23T12:00:00'); expect(refreshedLabel('2026-11-22T12:00:00', now)).toBe('Refreshed yesterday'); expect(refreshedLabel('2026-11-20T12:00:00', now)).toBe('Refreshed 3 days ago'); }); }); test('songsLikeArtist drops the shelf-wide prefix', () => { expect(songsLikeArtist(pl({ name: 'Songs like Low Tide Choir' }))).toBe('Low Tide Choir'); expect(songsLikeArtist(pl({ name: 'Something else' }))).toBe('Something else'); }); describe('washTone', () => { // WCAG relative luminance and contrast, to check the promise in the doc. const lum = ({ r, g, b }: { r: number; g: number; b: number }) => { const c = (v: number) => { const s = v / 255; return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4; }; return 0.2126 * c(r) + 0.7152 * c(g) + 0.0722 * c(b); }; // The card mixes the tone 62% toward #0e1013 at its lightest end, and its // faintest text is parchment at 80% over that. const mix = (a: { r: number; g: number; b: number }, t: number, b = { r: 14, g: 16, b: 19 }) => ({ r: a.r * t + b.r * (1 - t), g: a.g * t + b.g * (1 - t), b: a.b * t + b.b * (1 - t) }); const parchment = { r: 232, g: 228, b: 216 }; test.each([ ['beige', { r: 200, g: 190, b: 160 }], ['near white', { r: 245, g: 245, b: 245 }], ['acid yellow', { r: 240, g: 240, b: 20 }], ['light grey', { r: 170, g: 175, b: 180 }] ])('a %s cover still leaves the faintest text at 4.5:1', (_, cover) => { const ground = mix(washTone(cover), 0.62); const text = mix(parchment, 0.8, ground); const ratio = (lum(text) + 0.05) / (lum(ground) + 0.05); expect(ratio).toBeGreaterThanOrEqual(4.5); }); test('a dark cover keeps its own colour', () => { expect(washTone({ r: 30, g: 60, b: 90 })).toEqual({ r: 30, g: 60, b: 90 }); }); }); describe('shelfTileWidth', () => { test('a long shelf shows whole tiles that fill its width', () => { const w = shelfTileWidth(1000, 20, 150, 16); const cols = Math.floor((1000 + 16) / (150 + 16)); expect(cols * w + (cols - 1) * 16).toBeLessThanOrEqual(1000); expect(cols * w + (cols - 1) * 16).toBeGreaterThan(1000 - cols); }); test('a short shelf splits the whole width between its items', () => { expect(shelfTileWidth(1000, 2, 150, 16)).toBe(492); expect(shelfTileWidth(400, 2, 150, 16)).toBe(192); }); test('a narrow window still gets one tile at the min', () => { expect(shelfTileWidth(100, 5, 150, 16)).toBe(150); }); }); describe('byShelfRank', () => { test('ranked mixes lead in rank order, the unranked follow by name', () => { const shelf = [ pl({ name: 'Ambient mix', shelf_rank: 3 }), pl({ name: 'Zydeco mix' }), pl({ name: 'Pop mix', shelf_rank: 1 }), pl({ name: 'Blues mix' }), pl({ name: 'Jazz mix', shelf_rank: 2 }) ]; expect([...shelf].sort(byShelfRank).map((p) => p.name)).toEqual([ 'Pop mix', 'Jazz mix', 'Ambient mix', 'Blues mix', 'Zydeco mix' ]); }); });