|
5 | 5 | * Priority: P1 - Voice input support. |
6 | 6 | */ |
7 | 7 |
|
8 | | -import { initWhisper } from 'whisper.rn'; |
| 8 | +import { initWhisper, AudioSessionIos } from 'whisper.rn'; |
9 | 9 | import { Platform, PermissionsAndroid } from 'react-native'; |
10 | 10 | import RNFS from 'react-native-fs'; |
11 | 11 | import { whisperService, WHISPER_MODELS } from '../../../src/services/whisperService'; |
12 | 12 |
|
| 13 | +const mockedAudioSessionIos = AudioSessionIos as jest.Mocked<typeof AudioSessionIos>; |
| 14 | + |
13 | 15 | const mockedRNFS = RNFS as jest.Mocked<typeof RNFS>; |
14 | 16 | const mockedInitWhisper = initWhisper as jest.MockedFunction<typeof initWhisper>; |
15 | 17 |
|
@@ -256,35 +258,108 @@ describe('WhisperService', () => { |
256 | 258 | Object.defineProperty(Platform, 'OS', { get: () => originalOS }); |
257 | 259 | }); |
258 | 260 |
|
259 | | - it('returns true on Android when granted', async () => { |
260 | | - Object.defineProperty(Platform, 'OS', { get: () => 'android' }); |
261 | | - jest.spyOn(PermissionsAndroid, 'request').mockResolvedValue( |
262 | | - PermissionsAndroid.RESULTS.GRANTED |
263 | | - ); |
| 261 | + describe('Android', () => { |
| 262 | + beforeEach(() => { |
| 263 | + Object.defineProperty(Platform, 'OS', { get: () => 'android' }); |
| 264 | + }); |
264 | 265 |
|
265 | | - expect(await whisperService.requestPermissions()).toBe(true); |
266 | | - }); |
| 266 | + it('returns true when granted', async () => { |
| 267 | + jest.spyOn(PermissionsAndroid, 'request').mockResolvedValue( |
| 268 | + PermissionsAndroid.RESULTS.GRANTED |
| 269 | + ); |
267 | 270 |
|
268 | | - it('returns false on Android when denied', async () => { |
269 | | - Object.defineProperty(Platform, 'OS', { get: () => 'android' }); |
270 | | - jest.spyOn(PermissionsAndroid, 'request').mockResolvedValue( |
271 | | - PermissionsAndroid.RESULTS.DENIED |
272 | | - ); |
| 271 | + expect(await whisperService.requestPermissions()).toBe(true); |
| 272 | + }); |
273 | 273 |
|
274 | | - expect(await whisperService.requestPermissions()).toBe(false); |
275 | | - }); |
| 274 | + it('returns false when denied', async () => { |
| 275 | + jest.spyOn(PermissionsAndroid, 'request').mockResolvedValue( |
| 276 | + PermissionsAndroid.RESULTS.DENIED |
| 277 | + ); |
276 | 278 |
|
277 | | - it('returns true on iOS without requesting', async () => { |
278 | | - Object.defineProperty(Platform, 'OS', { get: () => 'ios' }); |
| 279 | + expect(await whisperService.requestPermissions()).toBe(false); |
| 280 | + }); |
| 281 | + |
| 282 | + it('returns false on permission error', async () => { |
| 283 | + jest.spyOn(PermissionsAndroid, 'request').mockRejectedValue(new Error('Permission error')); |
| 284 | + |
| 285 | + expect(await whisperService.requestPermissions()).toBe(false); |
| 286 | + }); |
| 287 | + |
| 288 | + it('does not call AudioSessionIos', async () => { |
| 289 | + jest.spyOn(PermissionsAndroid, 'request').mockResolvedValue( |
| 290 | + PermissionsAndroid.RESULTS.GRANTED |
| 291 | + ); |
| 292 | + |
| 293 | + await whisperService.requestPermissions(); |
| 294 | + |
| 295 | + expect(mockedAudioSessionIos.setCategory).not.toHaveBeenCalled(); |
| 296 | + expect(mockedAudioSessionIos.setMode).not.toHaveBeenCalled(); |
| 297 | + expect(mockedAudioSessionIos.setActive).not.toHaveBeenCalled(); |
| 298 | + }); |
279 | 299 |
|
280 | | - expect(await whisperService.requestPermissions()).toBe(true); |
| 300 | + it('requests RECORD_AUDIO permission with correct message', async () => { |
| 301 | + const requestSpy = jest.spyOn(PermissionsAndroid, 'request').mockResolvedValue( |
| 302 | + PermissionsAndroid.RESULTS.GRANTED |
| 303 | + ); |
| 304 | + |
| 305 | + await whisperService.requestPermissions(); |
| 306 | + |
| 307 | + expect(requestSpy).toHaveBeenCalledWith( |
| 308 | + PermissionsAndroid.PERMISSIONS.RECORD_AUDIO, |
| 309 | + expect.objectContaining({ |
| 310 | + title: 'Microphone Permission', |
| 311 | + buttonPositive: 'OK', |
| 312 | + }) |
| 313 | + ); |
| 314 | + }); |
281 | 315 | }); |
282 | 316 |
|
283 | | - it('returns false on Android permission error', async () => { |
284 | | - Object.defineProperty(Platform, 'OS', { get: () => 'android' }); |
285 | | - jest.spyOn(PermissionsAndroid, 'request').mockRejectedValue(new Error('Permission error')); |
| 317 | + describe('iOS', () => { |
| 318 | + beforeEach(() => { |
| 319 | + Object.defineProperty(Platform, 'OS', { get: () => 'ios' }); |
| 320 | + }); |
| 321 | + |
| 322 | + it('configures audio session and returns true', async () => { |
| 323 | + expect(await whisperService.requestPermissions()).toBe(true); |
286 | 324 |
|
287 | | - expect(await whisperService.requestPermissions()).toBe(false); |
| 325 | + expect(mockedAudioSessionIos.setCategory).toHaveBeenCalledWith( |
| 326 | + 'PlayAndRecord', |
| 327 | + ['AllowBluetooth', 'MixWithOthers'] |
| 328 | + ); |
| 329 | + expect(mockedAudioSessionIos.setMode).toHaveBeenCalledWith('Default'); |
| 330 | + expect(mockedAudioSessionIos.setActive).toHaveBeenCalledWith(true); |
| 331 | + }); |
| 332 | + |
| 333 | + it('calls setCategory before setMode before setActive', async () => { |
| 334 | + const callOrder: string[] = []; |
| 335 | + mockedAudioSessionIos.setCategory.mockImplementation(async () => { callOrder.push('setCategory'); }); |
| 336 | + mockedAudioSessionIos.setMode.mockImplementation(async () => { callOrder.push('setMode'); }); |
| 337 | + mockedAudioSessionIos.setActive.mockImplementation(async () => { callOrder.push('setActive'); }); |
| 338 | + |
| 339 | + await whisperService.requestPermissions(); |
| 340 | + |
| 341 | + expect(callOrder).toEqual(['setCategory', 'setMode', 'setActive']); |
| 342 | + }); |
| 343 | + |
| 344 | + it('returns false when audio session setup fails', async () => { |
| 345 | + mockedAudioSessionIos.setCategory.mockRejectedValue(new Error('Audio session error')); |
| 346 | + |
| 347 | + expect(await whisperService.requestPermissions()).toBe(false); |
| 348 | + }); |
| 349 | + |
| 350 | + it('returns false when setActive fails (permission denied)', async () => { |
| 351 | + mockedAudioSessionIos.setActive.mockRejectedValue(new Error('Microphone permission denied')); |
| 352 | + |
| 353 | + expect(await whisperService.requestPermissions()).toBe(false); |
| 354 | + }); |
| 355 | + |
| 356 | + it('does not call PermissionsAndroid', async () => { |
| 357 | + const requestSpy = jest.spyOn(PermissionsAndroid, 'request'); |
| 358 | + |
| 359 | + await whisperService.requestPermissions(); |
| 360 | + |
| 361 | + expect(requestSpy).not.toHaveBeenCalled(); |
| 362 | + }); |
288 | 363 | }); |
289 | 364 | }); |
290 | 365 |
|
@@ -376,6 +451,60 @@ describe('WhisperService', () => { |
376 | 451 | ); |
377 | 452 | }); |
378 | 453 |
|
| 454 | + it('includes audioSessionOnStartIos options on iOS', async () => { |
| 455 | + const mockContext = { |
| 456 | + id: 'ctx', |
| 457 | + release: jest.fn(), |
| 458 | + transcribeRealtime: jest.fn(() => Promise.resolve({ |
| 459 | + stop: jest.fn(), |
| 460 | + subscribe: jest.fn(), |
| 461 | + })), |
| 462 | + transcribe: jest.fn(), |
| 463 | + }; |
| 464 | + mockedInitWhisper.mockResolvedValueOnce(mockContext as any); |
| 465 | + await whisperService.loadModel('/path/model.bin'); |
| 466 | + |
| 467 | + Object.defineProperty(Platform, 'OS', { get: () => 'ios' }); |
| 468 | + |
| 469 | + await whisperService.startRealtimeTranscription(jest.fn()); |
| 470 | + |
| 471 | + expect(mockContext.transcribeRealtime).toHaveBeenCalledWith( |
| 472 | + expect.objectContaining({ |
| 473 | + audioSessionOnStartIos: expect.objectContaining({ |
| 474 | + category: 'PlayAndRecord', |
| 475 | + options: ['AllowBluetooth', 'MixWithOthers'], |
| 476 | + mode: 'Default', |
| 477 | + }), |
| 478 | + audioSessionOnStopIos: 'restore', |
| 479 | + }) |
| 480 | + ); |
| 481 | + }); |
| 482 | + |
| 483 | + it('does not include audioSession options on Android', async () => { |
| 484 | + const mockContext = { |
| 485 | + id: 'ctx', |
| 486 | + release: jest.fn(), |
| 487 | + transcribeRealtime: jest.fn(() => Promise.resolve({ |
| 488 | + stop: jest.fn(), |
| 489 | + subscribe: jest.fn(), |
| 490 | + })), |
| 491 | + transcribe: jest.fn(), |
| 492 | + }; |
| 493 | + mockedInitWhisper.mockResolvedValueOnce(mockContext as any); |
| 494 | + await whisperService.loadModel('/path/model.bin'); |
| 495 | + |
| 496 | + Object.defineProperty(Platform, 'OS', { get: () => 'android' }); |
| 497 | + jest.spyOn(PermissionsAndroid, 'request').mockResolvedValue( |
| 498 | + PermissionsAndroid.RESULTS.GRANTED |
| 499 | + ); |
| 500 | + |
| 501 | + await whisperService.startRealtimeTranscription(jest.fn()); |
| 502 | + |
| 503 | + const callArgs = mockContext.transcribeRealtime.mock.calls[0][0]; |
| 504 | + expect(callArgs.audioSessionOnStartIos).toBeUndefined(); |
| 505 | + expect(callArgs.audioSessionOnStopIos).toBeUndefined(); |
| 506 | + }); |
| 507 | + |
379 | 508 | it('forwards events to callback via subscribe', async () => { |
380 | 509 | let subscribeFn: any; |
381 | 510 | const mockContext = { |
|
0 commit comments