Something went wrong. Try again.
An AI-music permissible frontend for plyr.fm.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237import { describe, it, expect, vi, beforeEach } from 'vitest';
/** * Tests for the player module's pure logic and state machine. * * The player module uses module-level state (not a class), so we test * it by importing and calling its functions. We mock expo-audio since * it's a native dependency, but the player logic itself is real. */
// Mock expo-audio before importing playervi.mock('expo-audio', () => ({ createAudioPlayer: vi.fn(() => ({ play: vi.fn(), pause: vi.fn(), release: vi.fn(), addListener: vi.fn(), })),}));
// Mock the API's getStreamUrl so we don't hit the real APIvi.mock('../lib/api', () => ({ getStreamUrl: (id: number) => `https://api.plyr.fm/tracks/${id}/stream`,}));
import { subscribe, getPlayerState, playTrack, stopPlayback, togglePlayback, pausePlayback, resumePlayback, playFromQueue, playNext, playPrevious, getCurrentTrackId, getQueue, getQueueIndex,} from '../lib/player';
describe('player state machine', () => { beforeEach(async () => { // Reset player state between tests await stopPlayback(); });
it('should start with no track playing', () => { const state = getPlayerState(); expect(state.trackId).toBeNull(); expect(state.isPlaying).toBe(false); expect(state.title).toBe(''); });
it('should update state when a track is played', async () => { await playTrack(1, 'Test Track', 'Test Artist', null); const state = getPlayerState(); expect(state.trackId).toBe(1); expect(state.isPlaying).toBe(true); expect(state.title).toBe('Test Track'); expect(state.artist).toBe('Test Artist'); });
it('should generate default title from track ID', async () => { await playTrack(42); const state = getPlayerState(); expect(state.title).toBe('Track #42'); });
it('should clear state when playback stops', async () => { await playTrack(1, 'Test', 'Artist', null); await stopPlayback(); const state = getPlayerState(); expect(state.trackId).toBeNull(); expect(state.isPlaying).toBe(false); });
it('should toggle from playing to paused', async () => { await playTrack(1, 'Test', 'Artist', null); togglePlayback(); const state = getPlayerState(); expect(state.isPlaying).toBe(false); });
it('should toggle from paused to playing', async () => { await playTrack(1, 'Test', 'Artist', null); pausePlayback(); togglePlayback(); const state = getPlayerState(); expect(state.isPlaying).toBe(true); });
it('should notify subscribers on state change', async () => { const listener = vi.fn(); const unsub = subscribe(listener); // subscribe immediately calls listener with current state expect(listener).toHaveBeenCalledOnce();
await playTrack(1, 'Test', 'Artist', null); // Should have been called again with the new state expect(listener).toHaveBeenCalledTimes(2);
unsub(); });
it('should unsubscribe correctly', async () => { const listener = vi.fn(); const unsub = subscribe(listener); unsub(); listener.mockClear();
await playTrack(1, 'Test', 'Artist', null); // Listener should not be called after unsubscribe expect(listener).not.toHaveBeenCalled(); });
it('should replace current track when playing a new one', async () => { await playTrack(1, 'First', 'Artist', null); await playTrack(2, 'Second', 'Artist', null); const state = getPlayerState(); expect(state.trackId).toBe(2); expect(state.title).toBe('Second'); });
it('should return current track ID', async () => { await playTrack(99, 'Test', 'Artist', null); expect(getCurrentTrackId()).toBe(99); });});
describe('player queue management', () => { beforeEach(async () => { await stopPlayback(); });
it('should play from queue at specified index', async () => { const queue = [ { id: 1, title: 'Track 1', artist: 'A', imageUrl: null }, { id: 2, title: 'Track 2', artist: 'B', imageUrl: null }, { id: 3, title: 'Track 3', artist: 'C', imageUrl: null }, ]; await playFromQueue(queue, 0); const state = getPlayerState(); expect(state.trackId).toBe(1); expect(state.queue.length).toBe(3); expect(state.queueIndex).toBe(0); });
it('should advance to next track in queue', async () => { const queue = [ { id: 1, title: 'Track 1', artist: 'A', imageUrl: null }, { id: 2, title: 'Track 2', artist: 'B', imageUrl: null }, ]; await playFromQueue(queue, 0); await playNext(); const state = getPlayerState(); expect(state.trackId).toBe(2); expect(state.queueIndex).toBe(1); });
it('should go to previous track in queue', async () => { const queue = [ { id: 1, title: 'Track 1', artist: 'A', imageUrl: null }, { id: 2, title: 'Track 2', artist: 'B', imageUrl: null }, ]; await playFromQueue(queue, 1); await playPrevious(); const state = getPlayerState(); expect(state.trackId).toBe(1); expect(state.queueIndex).toBe(0); });
it('should report hasNext correctly', async () => { const queue = [ { id: 1, title: 'Track 1', artist: 'A', imageUrl: null }, { id: 2, title: 'Track 2', artist: 'B', imageUrl: null }, ]; await playFromQueue(queue, 0); const state = getPlayerState(); expect(state.hasNext).toBe(true); });
it('should report hasPrevious correctly', async () => { const queue = [ { id: 1, title: 'Track 1', artist: 'A', imageUrl: null }, { id: 2, title: 'Track 2', artist: 'B', imageUrl: null }, ]; await playFromQueue(queue, 1); const state = getPlayerState(); expect(state.hasPrevious).toBe(true); });
it('should not advance past end of queue', async () => { const queue = [ { id: 1, title: 'Track 1', artist: 'A', imageUrl: null }, ]; await playFromQueue(queue, 0); await playNext(); // should be no-op const state = getPlayerState(); expect(state.trackId).toBe(1); });
it('should not go before start of queue', async () => { const queue = [ { id: 1, title: 'Track 1', artist: 'A', imageUrl: null }, ]; await playFromQueue(queue, 0); await playPrevious(); // should be no-op const state = getPlayerState(); expect(state.trackId).toBe(1); });
it('should clear queue when playing a track not in queue', async () => { const queue = [ { id: 1, title: 'Track 1', artist: 'A', imageUrl: null }, { id: 2, title: 'Track 2', artist: 'B', imageUrl: null }, ]; await playFromQueue(queue, 0); // Play a track NOT in the queue await playTrack(99, 'Unrelated', 'Artist', null); const state = getPlayerState(); expect(state.trackId).toBe(99); expect(state.queue.length).toBe(0); expect(state.queueIndex).toBe(-1); });
it('should get queue and queue index', async () => { const queue = [ { id: 1, title: 'Track 1', artist: 'A', imageUrl: null }, { id: 2, title: 'Track 2', artist: 'B', imageUrl: null }, ]; await playFromQueue(queue, 1); expect(getQueue().length).toBe(2); expect(getQueueIndex()).toBe(1); });});