import { describe, expect, it } from 'vitest'; import { db, schema } from '../src/db'; import { createRedis } from '../src/lib/redis'; import { applyPollResult } from '../src/services/nowPlaying'; import { resolveQueue } from '../src/services/queues'; import { upsertTrack } from '../src/services/tracks'; import { createUser, useTestApp } from './helpers'; const app = useTestApp(); /** Collect messages published for a user while `fn` runs. */ async function published(userId: string, fn: () => Promise): Promise<{ track: { title: string }; isPlaying: boolean }[]> { const sub = createRedis(); const messages: string[] = []; await sub.subscribe(`np:${userId}`); sub.on('message', (_c, m) => messages.push(m)); await fn(); await new Promise((r) => setTimeout(r, 50)); sub.disconnect(); return messages.map((m) => JSON.parse(m)); } const track = (title: string, extra = {}) => ({ track: { title, artist: 'Daft Punk', ...extra }, isPlaying: true, progressMs: 1000 }); describe('applyPollResult', () => { it('publishes on a new track and queues link resolution, but not on repeat polls without a position', async () => { const { user } = await createUser(app(), 'bob'); const first = await published(user.id, () => applyPollResult(user.id, 'spotify', track('One More Time'))); expect(first).toHaveLength(1); expect(first[0]).toMatchObject({ track: { title: 'One More Time' }, isPlaying: true }); expect(await resolveQueue.getJobCounts('waiting')).toMatchObject({ waiting: 1 }); // (polls that carry a position do republish, to realign progress bars: see progress.test.ts) const repeat = await published(user.id, () => applyPollResult(user.id, 'spotify', { ...track('One More Time'), progressMs: null }), ); expect(repeat).toHaveLength(0); }); it('only the source that reported playback can mark it stopped', async () => { const { user } = await createUser(app(), 'bob'); await applyPollResult(user.id, 'spotify', track('Digital Love')); // an idle Last.fm connection must not clobber the live Spotify session expect(await applyPollResult(user.id, 'lastfm', null)).toBe(false); let [np] = await db.select().from(schema.nowPlaying); expect(np?.isPlaying).toBe(true); const stopped = await published(user.id, () => applyPollResult(user.id, 'spotify', null)); [np] = await db.select().from(schema.nowPlaying); expect(np?.isPlaying).toBe(false); expect(stopped[0]?.isPlaying).toBe(false); }); it('treats a paused result as not playing', async () => { const { user } = await createUser(app(), 'bob'); await applyPollResult(user.id, 'plex', track('Digital Love')); await applyPollResult(user.id, 'plex', { ...track('Digital Love'), isPlaying: false }); const [np] = await db.select().from(schema.nowPlaying); expect(np?.isPlaying).toBe(false); }); }); describe('upsertTrack dedupe', () => { it('matches on ISRC first', async () => { const a = await upsertTrack({ title: 'Song', artist: 'X', isrc: 'USABC1234567' }); const b = await upsertTrack({ title: 'Song (Radio Edit)', artist: 'Someone Else', isrc: 'USABC1234567' }); expect(b).toEqual({ id: a.id, created: false }); }); it('matches on a known provider id', async () => { const a = await upsertTrack({ title: 'Song', artist: 'X', links: { spotify: { id: 'sp1', url: 'https://open.spotify.com/track/sp1' } } }); const b = await upsertTrack({ title: 'Totally Different Name', artist: 'Y', links: { spotify: { id: 'sp1', url: 'https://open.spotify.com/track/sp1' } } }); expect(b.id).toBe(a.id); }); it('matches on normalized artist|title and backfills missing fields', async () => { const a = await upsertTrack({ title: 'Midnight City', artist: 'M83' }); const b = await upsertTrack({ title: 'Midnight City (Remastered)', artist: 'M83', isrc: 'GB55H1100002', album: 'Hurry Up' }); expect(b.id).toBe(a.id); const [row] = await db.select().from(schema.tracks); expect(row).toMatchObject({ isrc: 'GB55H1100002', album: 'Hurry Up' }); }); it('keeps different songs separate', async () => { const a = await upsertTrack({ title: 'Midnight City', artist: 'M83' }); const b = await upsertTrack({ title: 'Wait', artist: 'M83' }); expect(b.id).not.toBe(a.id); }); });