68 lines
2.0 KiB
TypeScript
68 lines
2.0 KiB
TypeScript
|
|
const mockSetAudioModeAsync = jest.fn().mockResolvedValue(undefined);
|
||
|
|
|
||
|
|
jest.mock('expo-audio', () => ({
|
||
|
|
setAudioModeAsync: (...args: unknown[]) => mockSetAudioModeAsync(...args),
|
||
|
|
}));
|
||
|
|
|
||
|
|
import { audioFocus } from '@/core/audio/audio-focus';
|
||
|
|
|
||
|
|
describe('audioFocus', () => {
|
||
|
|
beforeEach(async () => {
|
||
|
|
mockSetAudioModeAsync.mockClear();
|
||
|
|
await audioFocus.release();
|
||
|
|
mockSetAudioModeAsync.mockClear();
|
||
|
|
});
|
||
|
|
|
||
|
|
test('acquires focus for recording', async () => {
|
||
|
|
const result = await audioFocus.acquireForRecording();
|
||
|
|
|
||
|
|
expect(result).toBe(true);
|
||
|
|
expect(audioFocus.isRecording()).toBe(true);
|
||
|
|
expect(audioFocus.getCurrentOwner()).toBe('recorder');
|
||
|
|
expect(mockSetAudioModeAsync).toHaveBeenCalledWith({
|
||
|
|
playsInSilentMode: true,
|
||
|
|
allowsRecording: true,
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
test('acquires focus for playback', async () => {
|
||
|
|
const result = await audioFocus.acquireForPlayback();
|
||
|
|
|
||
|
|
expect(result).toBe(true);
|
||
|
|
expect(audioFocus.isPlaying()).toBe(true);
|
||
|
|
expect(audioFocus.getCurrentOwner()).toBe('player');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('playback cannot steal focus from recorder', async () => {
|
||
|
|
await audioFocus.acquireForRecording();
|
||
|
|
const result = await audioFocus.acquireForPlayback();
|
||
|
|
|
||
|
|
expect(result).toBe(false);
|
||
|
|
expect(audioFocus.isRecording()).toBe(true);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('recording releases playback before acquiring', async () => {
|
||
|
|
await audioFocus.acquireForPlayback();
|
||
|
|
expect(audioFocus.isPlaying()).toBe(true);
|
||
|
|
|
||
|
|
await audioFocus.acquireForRecording();
|
||
|
|
expect(audioFocus.isRecording()).toBe(true);
|
||
|
|
expect(audioFocus.isPlaying()).toBe(false);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('notifies listeners on owner change', async () => {
|
||
|
|
const listener = jest.fn();
|
||
|
|
const unsub = audioFocus.onOwnerChange(listener);
|
||
|
|
|
||
|
|
await audioFocus.acquireForRecording();
|
||
|
|
expect(listener).toHaveBeenCalledWith('recorder');
|
||
|
|
|
||
|
|
await audioFocus.release();
|
||
|
|
expect(listener).toHaveBeenCalledWith(null);
|
||
|
|
|
||
|
|
unsub();
|
||
|
|
await audioFocus.acquireForPlayback();
|
||
|
|
expect(listener).toHaveBeenCalledTimes(2);
|
||
|
|
});
|
||
|
|
});
|