Files
musicshare/packages/shared/src/services.test.ts
T
chrisandClaude Opus 5.5 63d618f0e3 Unlock everything, add a donation link, credit Last.fm, nudge re-linking
- Every feature is free for everyone (the plan system stays for later);
  remaining limits are abuse guards
- Optional website donation link (NEXT_PUBLIC_DONATION_URL). Not shown in
  the mobile apps, where tips to the developer generally need in-app
  purchase
- Last.fm/ListenBrainz-sourced plays link to the user's profile, as
  Last.fm's API terms require
- Connections track when they last saw activity; a scrobbler silent for
  over a week prompts re-linking Spotify (its link expires every 6 months)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MzFywujuH9TiuXbr9vjoX8
2026-10-04 08:13:54 -04:00

43 lines
1.8 KiB
TypeScript

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);
});
});