import { describe, it, expect } from 'vitest'; /** * Live integration tests against the real plyr.fm API. * These make real network requests — they verify the API contract * matches what our client code expects. * * Run with: npx vitest run tests/api-live.test.ts * Skip in CI or offline with: VITEST_SKIP_LIVE=1 npx vitest run */ const shouldSkip = !!process.env.VITEST_SKIP_LIVE; describe.skipIf(shouldSkip)('plyr.fm live API', () => { const BASE = 'https://api.plyr.fm'; // --- Search --- it('should return search results with expected shape', async () => { const res = await fetch(`${BASE}/search/?q=ai&limit=3`); expect(res.ok).toBe(true); const data = await res.json(); expect(data).toHaveProperty('results'); expect(data).toHaveProperty('counts'); expect(data.counts).toHaveProperty('tracks'); expect(data.counts).toHaveProperty('artists'); expect(data.counts).toHaveProperty('albums'); expect(data.counts).toHaveProperty('tags'); expect(data.counts).toHaveProperty('playlists'); }); it('should return search results with correct types', async () => { const res = await fetch(`${BASE}/search/?q=ai&limit=5`); const data = await res.json(); if (data.results.length > 0) { const item = data.results[0]; expect(item).toHaveProperty('type'); expect(['track', 'artist', 'album', 'tag', 'playlist']).toContain(item.type); } }); it('should handle empty search gracefully', async () => { const res = await fetch(`${BASE}/search/?q=zzzzzzzzznonexistent&limit=3`); expect(res.ok).toBe(true); const data = await res.json(); expect(data.results).toBeInstanceOf(Array); }); // --- Top tracks --- it('should return top tracks as array', async () => { const res = await fetch(`${BASE}/tracks/top/?limit=3`); expect(res.ok).toBe(true); const data = await res.json(); expect(Array.isArray(data)).toBe(true); if (data.length > 0) { expect(data[0]).toHaveProperty('id'); expect(data[0]).toHaveProperty('title'); } }); it('should respect limit parameter on top tracks', async () => { const res = await fetch(`${BASE}/tracks/top/?limit=2`); const data = await res.json(); expect(data.length).toBeLessThanOrEqual(2); }); // --- Tracks by tag --- it('should return tracks by tag (ai)', async () => { const res = await fetch(`${BASE}/tracks/tags/ai?limit=3`); expect(res.ok).toBe(true); const data = await res.json(); expect(data).toHaveProperty('tag'); expect(data).toHaveProperty('tracks'); expect(data.tag).toHaveProperty('name'); expect(data.tag.name).toBe('ai'); }); it('should return tag with track_count', async () => { const res = await fetch(`${BASE}/tracks/tags/ai?limit=1`); const data = await res.json(); expect(data.tag).toHaveProperty('track_count'); expect(data.tag.track_count).toBeGreaterThan(0); }); it('should handle multi-word tag names', async () => { // Tags with spaces/commas should work const res = await fetch(`${BASE}/tracks/tags/${encodeURIComponent('folk, world, & country')}?limit=1`); expect(res.ok).toBe(true); const data = await res.json(); expect(data).toHaveProperty('tracks'); }); // --- Single track --- it('should return a single track with full detail', async () => { const topRes = await fetch(`${BASE}/tracks/top/?limit=1`); const tracks = await topRes.json(); if (tracks.length === 0) return; const trackId = tracks[0].id; const res = await fetch(`${BASE}/tracks/${trackId}`); expect(res.ok).toBe(true); const track = await res.json(); expect(track).toHaveProperty('id'); expect(track).toHaveProperty('title'); expect(track).toHaveProperty('artist'); expect(track.id).toBe(trackId); }); it('should return 404 for nonexistent track', async () => { const res = await fetch(`${BASE}/tracks/999999999`); expect(res.status).toBe(404); }); // --- Audio --- it('should have r2_url on tracks (audio delivery)', async () => { const topRes = await fetch(`${BASE}/tracks/top/?limit=1`); const tracks = await topRes.json(); if (tracks.length === 0) return; const track = tracks[0]; expect(track).toHaveProperty('r2_url'); if (track.r2_url) { const audioRes = await fetch(track.r2_url, { method: 'HEAD' }); expect(audioRes.status).toBeLessThan(400); } }); // --- Tags endpoint (used by Discover screen) --- it('should return list of tags', async () => { const res = await fetch(`${BASE}/tracks/tags`); expect(res.ok).toBe(true); const data = await res.json(); expect(Array.isArray(data)).toBe(true); if (data.length > 0) { expect(data[0]).toHaveProperty('name'); expect(data[0]).toHaveProperty('track_count'); } }); it('should have the "ai" tag in the tags list', async () => { const res = await fetch(`${BASE}/tracks/tags`); const data = await res.json(); const tagNames = data.map((t: any) => t.name.toLowerCase()); expect(tagNames).toContain('ai'); }); // --- Track field consistency --- it('should have consistent fields across top tracks and single track', async () => { const topRes = await fetch(`${BASE}/tracks/top/?limit=1`); const tracks = await topRes.json(); if (tracks.length === 0) return; const trackId = tracks[0].id; const singleRes = await fetch(`${BASE}/tracks/${trackId}`); const single = await singleRes.json(); // Both should have the same core fields expect(single.id).toBe(tracks[0].id); expect(single.title).toBe(tracks[0].title); }); });