import { describe, it, expect, vi, beforeEach } from 'vitest'; // Mock global fetch const mockFetch = vi.fn(); vi.stubGlobal('fetch', mockFetch); // Import after mocking import { 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'); }); }); });