- FastAPI: preset assets 01–08, GET list/static, PUT /me/avatar/preset, safer uploaded-avatar path validation, preset_avatars + HTTP tests. - Expo: personal-info (library + presets), profile tab avatar, resolveApiMediaUrl, auth hooks cache sync, Web multipart helper, partial-save messaging + profile i18n. - Includes existing edits to conversation screen and voice use-player. Co-authored-by: Cursor <cursoragent@cursor.com>
131 lines
3.4 KiB
TypeScript
131 lines
3.4 KiB
TypeScript
import { act, renderHook } from '@testing-library/react-native';
|
|
|
|
import { audioFocus } from '@/core/audio/audio-focus';
|
|
import { usePlayer } from '@/features/voice/hooks/use-player';
|
|
|
|
const mockUseAudioPlayer = jest.fn();
|
|
const mockUseAudioPlayerStatus = jest.fn();
|
|
|
|
jest.mock('expo-audio', () => ({
|
|
useAudioPlayer: (...args: unknown[]) => mockUseAudioPlayer(...args),
|
|
useAudioPlayerStatus: (...args: unknown[]) =>
|
|
mockUseAudioPlayerStatus(...args),
|
|
}));
|
|
|
|
jest.mock('@/core/audio/audio-focus', () => ({
|
|
audioFocus: {
|
|
acquireForPlayback: jest.fn(),
|
|
releaseIfOwnedBy: jest.fn(),
|
|
onOwnerChange: jest.fn(() => jest.fn()),
|
|
},
|
|
}));
|
|
|
|
describe('usePlayer', () => {
|
|
beforeEach(() => {
|
|
mockUseAudioPlayer.mockReset();
|
|
mockUseAudioPlayerStatus.mockReset();
|
|
|
|
mockUseAudioPlayer.mockReturnValue({
|
|
pause: jest.fn(),
|
|
play: jest.fn(),
|
|
});
|
|
mockUseAudioPlayerStatus.mockReturnValue({
|
|
isLoaded: false,
|
|
playing: false,
|
|
currentTime: 0,
|
|
duration: 0,
|
|
});
|
|
jest.mocked(audioFocus.acquireForPlayback).mockResolvedValue(true);
|
|
jest.mocked(audioFocus.releaseIfOwnedBy).mockResolvedValue(undefined);
|
|
});
|
|
|
|
test('keeps the native audio session active while app-level audio focus owns teardown', () => {
|
|
renderHook(() => usePlayer());
|
|
|
|
expect(mockUseAudioPlayer).toHaveBeenCalledWith(
|
|
null,
|
|
expect.objectContaining({
|
|
downloadFirst: false,
|
|
keepAudioSessionActive: true,
|
|
}),
|
|
);
|
|
});
|
|
|
|
test('pausePlayback toggles playing→paused and invokes native pause', async () => {
|
|
mockUseAudioPlayerStatus.mockReturnValue({
|
|
isLoaded: true,
|
|
playing: true,
|
|
currentTime: 0.1,
|
|
duration: 10,
|
|
});
|
|
const pause = jest.fn();
|
|
const play = jest.fn();
|
|
mockUseAudioPlayer.mockReturnValue({ pause, play });
|
|
|
|
const { result } = renderHook(() => usePlayer());
|
|
|
|
await act(async () => {
|
|
await result.current.enqueueExclusive({
|
|
uri: 'file:///fixture.mp3',
|
|
kind: 'voice',
|
|
});
|
|
});
|
|
|
|
expect(result.current.status).toBe('playing');
|
|
|
|
act(() => {
|
|
result.current.pausePlayback();
|
|
});
|
|
|
|
expect(pause).toHaveBeenCalled();
|
|
expect(result.current.status).toBe('paused');
|
|
});
|
|
|
|
test('resumePlayback toggles paused→playing and invokes native play', async () => {
|
|
mockUseAudioPlayerStatus.mockReturnValue({
|
|
isLoaded: true,
|
|
playing: false,
|
|
currentTime: 0.1,
|
|
duration: 10,
|
|
});
|
|
const pause = jest.fn();
|
|
const play = jest.fn();
|
|
mockUseAudioPlayer.mockReturnValue({ pause, play });
|
|
|
|
const { result } = renderHook(() => usePlayer());
|
|
|
|
await act(async () => {
|
|
await result.current.enqueueExclusive({
|
|
uri: 'file:///fixture.mp3',
|
|
kind: 'voice',
|
|
});
|
|
});
|
|
|
|
act(() => {
|
|
result.current.pausePlayback();
|
|
});
|
|
expect(result.current.status).toBe('paused');
|
|
|
|
await act(async () => {
|
|
await result.current.resumePlayback();
|
|
});
|
|
|
|
expect(play).toHaveBeenCalled();
|
|
expect(result.current.status).toBe('playing');
|
|
});
|
|
|
|
test('pausePlayback is a no-op while idle', async () => {
|
|
const pause = jest.fn();
|
|
mockUseAudioPlayer.mockReturnValue({ pause, play: jest.fn() });
|
|
|
|
const { result } = renderHook(() => usePlayer());
|
|
|
|
act(() => {
|
|
result.current.pausePlayback();
|
|
});
|
|
|
|
expect(pause).not.toHaveBeenCalled();
|
|
expect(result.current.status).toBe('idle');
|
|
});
|
|
});
|