@@ -17,6 +17,16 @@ jest.mock('../../../../services/callkeep.service.ios', () => ({
1717 } ,
1818} ) ) ;
1919
20+ // Mock logger
21+ jest . mock ( '../../../../lib/logging' , ( ) => ( {
22+ logger : {
23+ debug : jest . fn ( ) ,
24+ info : jest . fn ( ) ,
25+ warn : jest . fn ( ) ,
26+ error : jest . fn ( ) ,
27+ } ,
28+ } ) ) ;
29+
2030// Mock livekit-client
2131const mockRoom = {
2232 on : jest . fn ( ) ,
@@ -47,9 +57,11 @@ jest.mock('livekit-client', () => ({
4757} ) ) ;
4858
4959import { useLiveKitCallStore } from '../useLiveKitCallStore' ;
60+ import { logger } from '../../../../lib/logging' ;
5061
5162// Get the mocked service after the import
5263const mockCallKeepService = require ( '../../../../services/callkeep.service.ios' ) . callKeepService ;
64+ const mockLogger = logger as jest . Mocked < typeof logger > ;
5365
5466describe ( 'useLiveKitCallStore with CallKeep Integration' , ( ) => {
5567 // Mock environment variable for successful token fetching
@@ -82,6 +94,12 @@ describe('useLiveKitCallStore with CallKeep Integration', () => {
8294 mockRoom . localParticipant . setMicrophoneEnabled . mockResolvedValue ( undefined ) ;
8395 mockRoom . localParticipant . setCameraEnabled . mockResolvedValue ( undefined ) ;
8496
97+ // Clear logger mocks
98+ mockLogger . debug . mockClear ( ) ;
99+ mockLogger . info . mockClear ( ) ;
100+ mockLogger . warn . mockClear ( ) ;
101+ mockLogger . error . mockClear ( ) ;
102+
85103 // Reset the store to initial state
86104 useLiveKitCallStore . setState ( {
87105 availableRooms : [
@@ -110,8 +128,8 @@ describe('useLiveKitCallStore with CallKeep Integration', () => {
110128
111129 expect ( mockCallKeepService . setup ) . toHaveBeenCalledWith ( {
112130 appName : 'Resgrid Unit' ,
113- maximumCallGroups : '1' ,
114- maximumCallsPerCallGroup : '1' ,
131+ maximumCallGroups : 1 ,
132+ maximumCallsPerCallGroup : 1 ,
115133 includesCallsInRecents : false ,
116134 supportsVideo : false ,
117135 } ) ;
@@ -181,8 +199,6 @@ describe('useLiveKitCallStore with CallKeep Integration', () => {
181199 const error = new Error ( 'Failed to start call' ) ;
182200 mockCallKeepService . startCall . mockRejectedValueOnce ( error ) ;
183201
184- const consoleSpy = jest . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => { } ) ;
185-
186202 const { result } = renderHook ( ( ) => useLiveKitCallStore ( ) ) ;
187203
188204 await act ( async ( ) => {
@@ -191,12 +207,10 @@ describe('useLiveKitCallStore with CallKeep Integration', () => {
191207 await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ;
192208 } ) ;
193209
194- expect ( consoleSpy ) . toHaveBeenCalledWith (
195- 'Failed to start CallKeep call (background audio may not work):' ,
196- error
197- ) ;
198-
199- consoleSpy . mockRestore ( ) ;
210+ expect ( mockLogger . warn ) . toHaveBeenCalledWith ( {
211+ message : 'Failed to start CallKeep call (background audio may not work)' ,
212+ context : { error, roomId : 'emergency-channel' } ,
213+ } ) ;
200214 } ) ;
201215 } ) ;
202216
@@ -240,8 +254,6 @@ describe('useLiveKitCallStore with CallKeep Integration', () => {
240254 const error = new Error ( 'Failed to end call' ) ;
241255 mockCallKeepService . endCall . mockRejectedValueOnce ( error ) ;
242256
243- const consoleSpy = jest . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => { } ) ;
244-
245257 const { result } = renderHook ( ( ) => useLiveKitCallStore ( ) ) ;
246258
247259 // First set up a connected state
@@ -255,12 +267,10 @@ describe('useLiveKitCallStore with CallKeep Integration', () => {
255267 } ) ;
256268
257269 expect ( mockRoom . disconnect ) . toHaveBeenCalled ( ) ;
258- expect ( consoleSpy ) . toHaveBeenCalledWith (
259- 'Failed to end CallKeep call:' ,
260- error
261- ) ;
262-
263- consoleSpy . mockRestore ( ) ;
270+ expect ( mockLogger . warn ) . toHaveBeenCalledWith ( {
271+ message : 'Failed to end CallKeep call' ,
272+ context : { error } ,
273+ } ) ;
264274 } ) ;
265275
266276 it ( 'should handle disconnection when no room instance exists' , async ( ) => {
@@ -411,8 +421,6 @@ describe('useLiveKitCallStore with CallKeep Integration', () => {
411421 const error = new Error ( 'Microphone error' ) ;
412422 mockRoom . localParticipant . setMicrophoneEnabled . mockRejectedValueOnce ( error ) ;
413423
414- const consoleSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
415-
416424 const { result } = renderHook ( ( ) => useLiveKitCallStore ( ) ) ;
417425
418426 // Set up connected state
@@ -425,10 +433,11 @@ describe('useLiveKitCallStore with CallKeep Integration', () => {
425433 await result . current . actions . setMicrophoneEnabled ( true ) ;
426434 } ) ;
427435
428- expect ( consoleSpy ) . toHaveBeenCalledWith ( 'Error setting microphone state:' , error ) ;
436+ expect ( mockLogger . error ) . toHaveBeenCalledWith ( {
437+ message : 'Error setting microphone state' ,
438+ context : { error, enabled : true } ,
439+ } ) ;
429440 expect ( result . current . error ) . toBe ( 'Could not change microphone state.' ) ;
430-
431- consoleSpy . mockRestore ( ) ;
432441 } ) ;
433442
434443 it ( 'should handle microphone control when not connected' , async ( ) => {
@@ -446,11 +455,9 @@ describe('useLiveKitCallStore with CallKeep Integration', () => {
446455 it ( 'should prevent connection when already connecting' , async ( ) => {
447456 const { result } = renderHook ( ( ) => useLiveKitCallStore ( ) ) ;
448457
449- const consoleSpy = jest . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => { } ) ;
450-
451458 // Set connecting state
452459 act ( ( ) => {
453- result . current . actions . _setIsConnected ( false ) ;
460+ result . current . actions . _setIsConnecting ( true ) ;
454461 } ) ;
455462
456463 // First connection attempt should succeed
@@ -463,16 +470,20 @@ describe('useLiveKitCallStore with CallKeep Integration', () => {
463470 await result . current . actions . connectToRoom ( 'test-room' , 'test-participant' ) ;
464471 } ) ;
465472
466- expect ( consoleSpy ) . toHaveBeenCalledWith ( 'Connection attempt while already connecting or connected.' ) ;
467-
468- consoleSpy . mockRestore ( ) ;
473+ expect ( mockLogger . warn ) . toHaveBeenCalledWith ( {
474+ message : 'Connection attempt while already connecting or connected' ,
475+ context : {
476+ roomId : 'test-room' ,
477+ participantIdentity : 'test-participant' ,
478+ isConnecting : true ,
479+ isConnected : false
480+ } ,
481+ } ) ;
469482 } ) ;
470483
471484 it ( 'should prevent connection when already connected' , async ( ) => {
472485 const { result } = renderHook ( ( ) => useLiveKitCallStore ( ) ) ;
473486
474- const consoleSpy = jest . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => { } ) ;
475-
476487 // Set connected state
477488 act ( ( ) => {
478489 result . current . actions . _setIsConnected ( true ) ;
@@ -483,9 +494,15 @@ describe('useLiveKitCallStore with CallKeep Integration', () => {
483494 await result . current . actions . connectToRoom ( 'test-room' , 'test-participant' ) ;
484495 } ) ;
485496
486- expect ( consoleSpy ) . toHaveBeenCalledWith ( 'Connection attempt while already connecting or connected.' ) ;
487-
488- consoleSpy . mockRestore ( ) ;
497+ expect ( mockLogger . warn ) . toHaveBeenCalledWith ( {
498+ message : 'Connection attempt while already connecting or connected' ,
499+ context : {
500+ roomId : 'test-room' ,
501+ participantIdentity : 'test-participant' ,
502+ isConnecting : false ,
503+ isConnected : true
504+ } ,
505+ } ) ;
489506 } ) ;
490507 } ) ;
491508
@@ -494,27 +511,24 @@ describe('useLiveKitCallStore with CallKeep Integration', () => {
494511 const error = new Error ( 'Connection failed' ) ;
495512 mockRoom . connect . mockRejectedValueOnce ( error ) ;
496513
497- const consoleSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
498-
499514 const { result } = renderHook ( ( ) => useLiveKitCallStore ( ) ) ;
500515
501516 await act ( async ( ) => {
502517 await result . current . actions . connectToRoom ( 'test-room' , 'test-participant' ) ;
503518 } ) ;
504519
505- expect ( consoleSpy ) . toHaveBeenCalledWith ( 'Failed to connect to LiveKit room:' , error ) ;
520+ expect ( mockLogger . error ) . toHaveBeenCalledWith ( {
521+ message : 'Failed to connect to LiveKit room' ,
522+ context : { error, roomId : 'test-room' , participantIdentity : 'test-participant' } ,
523+ } ) ;
506524 expect ( result . current . error ) . toBe ( 'Connection failed' ) ;
507-
508- consoleSpy . mockRestore ( ) ;
509525 } ) ;
510526
511527 it ( 'should handle token fetch errors' , async ( ) => {
512528 // Mock environment to simulate missing token
513529 const originalEnv = process . env . STORYBOOK_LIVEKIT_TOKEN ;
514530 delete process . env . STORYBOOK_LIVEKIT_TOKEN ;
515531
516- const consoleSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
517-
518532 const { result } = renderHook ( ( ) => useLiveKitCallStore ( ) ) ;
519533
520534 await act ( async ( ) => {
@@ -531,8 +545,6 @@ describe('useLiveKitCallStore with CallKeep Integration', () => {
531545 if ( originalEnv ) {
532546 process . env . STORYBOOK_LIVEKIT_TOKEN = originalEnv ;
533547 }
534-
535- consoleSpy . mockRestore ( ) ;
536548 } ) ;
537549 } ) ;
538550} ) ;
0 commit comments