import { describe, expect, it } from 'vitest'; import { STREAMING_SERVICES, isQuietScrobbler, openUrl, searchUrl } from './services'; const track = { title: 'Midnight City', artist: 'M83', links: { spotify: 'https://open.spotify.com/track/abc' } }; describe('openUrl', () => { it('uses the direct link when the service has one', () => { expect(openUrl(track, 'spotify')).toEqual({ url: 'https://open.spotify.com/track/abc', exact: true }); }); it('falls back to a search on that service', () => { const result = openUrl(track, 'apple_music'); expect(result.exact).toBe(false); expect(result.url).toBe('https://music.apple.com/search?term=M83%20Midnight%20City'); }); }); describe('searchUrl', () => { it.each(STREAMING_SERVICES)('builds an https URL for %s with the query encoded', (service) => { const url = new URL(searchUrl(service, 'Café & Co', 'Ünïcode')); expect(url.protocol).toBe('https:'); expect(decodeURIComponent(url.href)).toContain('Ünïcode Café & Co'); }); }); describe('isQuietScrobbler', () => { const now = new Date('2026-10-04T12:00:00Z').getTime(); const conn = (provider: string, daysAgo: number | null) => ({ provider, status: 'active', lastActivityAt: daysAgo === null ? null : new Date(now - daysAgo * 86_400_000).toISOString(), }); it('flags Last.fm/ListenBrainz that went silent for over a week', () => { expect(isQuietScrobbler(conn('lastfm', 8), now)).toBe(true); expect(isQuietScrobbler(conn('listenbrainz', 30), now)).toBe(true); }); it('ignores recent activity, never-active connections, and other providers', () => { expect(isQuietScrobbler(conn('lastfm', 2), now)).toBe(false); expect(isQuietScrobbler(conn('lastfm', null), now)).toBe(false); expect(isQuietScrobbler(conn('spotify', 30), now)).toBe(false); }); });