- Shared: link building, feed ordering, input schemas, API client - API unit: track matching, private-address blocking, token crypto, plan expiry, provider response parsing - API integration against Postgres and Redis: auth, follows and pins, now-playing state, webhooks, OAuth return URLs, realtime, rate limits - Tests use a dedicated *_test database and Redis DB, and refuse to wipe anything else; fetch is stubbed so they never hit the network - Pin Node to 22.13+ (.nvmrc), which Vite's native bindings require Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MzFywujuH9TiuXbr9vjoX8
88 lines
4.1 KiB
TypeScript
88 lines
4.1 KiB
TypeScript
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<unknown>): 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', 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 });
|
|
|
|
const repeat = await published(user.id, () => applyPollResult(user.id, 'spotify', track('One More Time')));
|
|
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);
|
|
});
|
|
});
|