Something went wrong. Try again.
An AI-music permissible frontend for plyr.fm.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194import { describe, it, expect, vi, beforeEach } from 'vitest';
// Mock global fetchconst mockFetch = vi.fn();vi.stubGlobal('fetch', mockFetch);
// Import after mockingimport { searchTracks, getTrack, getTopTracks, getTracksByTag, getStreamUrl, getAudioUrl,} from '../lib/api';
beforeEach(() => { mockFetch.mockReset();});
describe('api.ts', () => { describe('searchTracks', () => { it('should call the search endpoint with correct params', async () => { const mockResponse = { results: [ { type: 'track', id: 1, title: 'Test Track', artist_handle: 'test' }, ], counts: { tracks: 1, artists: 0, albums: 0, tags: 0, playlists: 0 }, }; mockFetch.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve(mockResponse), });
const result = await searchTracks('ai music');
expect(mockFetch).toHaveBeenCalledWith( 'https://api.plyr.fm/search/?q=ai+music&limit=20&page=0', expect.objectContaining({ headers: expect.objectContaining({ 'Content-Type': 'application/json' }), }), ); expect(result.results).toHaveLength(1); expect(result.results[0].title).toBe('Test Track'); });
it('should pass custom limit and page params', async () => { mockFetch.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve({ results: [], counts: { tracks: 0, artists: 0, albums: 0, tags: 0, playlists: 0 } }), });
await searchTracks('test', 5, 2);
expect(mockFetch).toHaveBeenCalledWith( 'https://api.plyr.fm/search/?q=test&limit=5&page=2', expect.anything(), ); });
it('should throw on non-ok response', async () => { mockFetch.mockResolvedValueOnce({ ok: false, status: 500, statusText: 'Internal Server Error', });
await expect(searchTracks('fail')).rejects.toThrow('plyr.fm API error: 500 Internal Server Error'); }); });
describe('getTrack', () => { it('should fetch a single track by id', async () => { const mockTrack = { id: 42, title: 'AI Dreams', artist: 'SunoBot', tags: ['ai', 'suno'], }; mockFetch.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve(mockTrack), });
const result = await getTrack(42);
expect(mockFetch).toHaveBeenCalledWith( 'https://api.plyr.fm/tracks/42', expect.objectContaining({ headers: expect.objectContaining({ 'Content-Type': 'application/json' }), }), ); expect(result.id).toBe(42); expect(result.title).toBe('AI Dreams'); });
it('should throw on 404', async () => { mockFetch.mockResolvedValueOnce({ ok: false, status: 404, statusText: 'Not Found', });
await expect(getTrack(99999)).rejects.toThrow('plyr.fm API error: 404 Not Found'); }); });
describe('getTopTracks', () => { it('should fetch top tracks with default limit', async () => { const mockTracks = [ { id: 1, title: 'Top 1' }, { id: 2, title: 'Top 2' }, ]; mockFetch.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve(mockTracks), });
const result = await getTopTracks();
expect(mockFetch).toHaveBeenCalledWith( 'https://api.plyr.fm/tracks/top/?limit=20', expect.anything(), ); expect(result).toHaveLength(2); });
it('should respect custom limit', async () => { mockFetch.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve([]), });
await getTopTracks(5);
expect(mockFetch).toHaveBeenCalledWith( 'https://api.plyr.fm/tracks/top/?limit=5', expect.anything(), ); }); });
describe('getTracksByTag', () => { it('should fetch tracks by tag name', async () => { const mockTagResult = { tag: { name: 'ai', track_count: 31 }, tracks: [{ id: 1, title: 'AI Track', tags: ['ai'] }], }; mockFetch.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve(mockTagResult), });
const result = await getTracksByTag('ai');
expect(mockFetch).toHaveBeenCalledWith( 'https://api.plyr.fm/tracks/tags/ai?limit=20', expect.anything(), ); expect(result.tag.name).toBe('ai'); expect(result.tracks).toHaveLength(1); });
it('should URL-encode tag names with special characters', async () => { mockFetch.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve({ tag: { name: 'hip-hop', track_count: 0 }, tracks: [] }), });
await getTracksByTag('hip-hop');
expect(mockFetch).toHaveBeenCalledWith( 'https://api.plyr.fm/tracks/tags/hip-hop?limit=20', expect.anything(), ); }); });
describe('getStreamUrl / getAudioUrl', () => { it('should return correct stream URL', () => { expect(getStreamUrl(42)).toBe('https://api.plyr.fm/tracks/42/stream'); });
it('should return correct audio URL', () => { expect(getAudioUrl(42)).toBe('https://api.plyr.fm/tracks/42/audio'); });
it('should handle different track IDs', () => { expect(getStreamUrl(1)).toBe('https://api.plyr.fm/tracks/1/stream'); expect(getStreamUrl(99999)).toBe('https://api.plyr.fm/tracks/99999/stream'); }); });});