@@ -362,14 +362,16 @@ describe('CgateWebBridge', () => {
362362 } ) ;
363363
364364 describe ( 'Disconnection and Error Handlers' , ( ) => {
365- let mockClientDisconn , mockCommandSocketDisconn , mockEventSocketDisconn ; // Use different names
366- let scheduleReconnectSpy , consoleWarnSpyDisconn , consoleErrorSpyDisconn , processExitSpy ;
365+ // Rely on global console mocks defined at the top level
366+ let mockClientDisconn , mockCommandSocketDisconn , mockEventSocketDisconn ;
367+ let scheduleReconnectSpy , processExitSpy ; // Removed console spies specific to this block
367368 let clientRemoveListenersSpy , cmdRemoveListenersSpy , evtRemoveListenersSpy ;
368369 let cmdDestroySpy , evtDestroySpy ;
369370
370371 beforeEach ( ( ) => {
371- consoleWarnSpyDisconn = jest . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => { } ) ;
372- consoleErrorSpyDisconn = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
372+ // Remove console spy setup from here
373+ // consoleWarnSpyDisconn = jest.spyOn(console, 'warn').mockImplementation(() => { });
374+ // consoleErrorSpyDisconn = jest.spyOn(console, 'error').mockImplementation(() => { });
373375 processExitSpy = jest . spyOn ( process , 'exit' ) . mockImplementation ( ( ) => { throw new Error ( 'process.exit called' ) ; } ) ;
374376 mockClientDisconn = { removeAllListeners : jest . fn ( ) } ;
375377 mockCommandSocketDisconn = { removeAllListeners : jest . fn ( ) , destroy : jest . fn ( ) , destroyed : false } ;
@@ -389,18 +391,26 @@ describe('CgateWebBridge', () => {
389391 } ) ;
390392
391393 afterEach ( ( ) => {
392- consoleWarnSpyDisconn . mockRestore ( ) ;
393- consoleErrorSpyDisconn . mockRestore ( ) ;
394+ // Remove console spy restores from here
395+ // consoleWarnSpyDisconn.mockRestore();
396+ // consoleErrorSpyDisconn.mockRestore();
394397 processExitSpy . mockRestore ( ) ;
395398 scheduleReconnectSpy . mockRestore ( ) ;
399+ // Need to manually restore spies on the mock objects from beforeEach
400+ clientRemoveListenersSpy . mockRestore ( ) ;
401+ cmdRemoveListenersSpy . mockRestore ( ) ;
402+ evtRemoveListenersSpy . mockRestore ( ) ;
403+ cmdDestroySpy . mockRestore ( ) ;
404+ evtDestroySpy . mockRestore ( ) ;
396405 } ) ;
397406
407+ // --- Close Handlers ---
398408 it ( '_handleMqttClose should reset flag, null client, remove listeners and warn' , ( ) => {
399409 bridge . _handleMqttClose ( ) ;
400410 expect ( bridge . clientConnected ) . toBe ( false ) ;
401411 expect ( bridge . client ) . toBeNull ( ) ;
402412 expect ( clientRemoveListenersSpy ) . toHaveBeenCalledTimes ( 1 ) ;
403- expect ( consoleWarnSpyDisconn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'MQTT Client Closed' ) ) ;
413+ expect ( mockConsoleWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'MQTT Client Closed' ) ) ; // Use global mock
404414 expect ( scheduleReconnectSpy ) . not . toHaveBeenCalled ( ) ;
405415 } ) ;
406416
@@ -409,14 +419,14 @@ describe('CgateWebBridge', () => {
409419 expect ( bridge . commandConnected ) . toBe ( false ) ;
410420 expect ( bridge . commandSocket ) . toBeNull ( ) ;
411421 expect ( cmdRemoveListenersSpy ) . toHaveBeenCalledTimes ( 1 ) ;
412- expect ( consoleWarnSpyDisconn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'COMMAND PORT DISCONNECTED' ) ) ;
413- expect ( consoleWarnSpyDisconn ) . not . toHaveBeenCalledWith ( expect . stringContaining ( 'with error' ) ) ;
422+ expect ( mockConsoleWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'COMMAND PORT DISCONNECTED' ) ) ; // Use global mock
423+ expect ( mockConsoleWarn ) . not . toHaveBeenCalledWith ( expect . stringContaining ( 'with error' ) ) ;
414424 expect ( scheduleReconnectSpy ) . toHaveBeenCalledWith ( 'command' ) ;
415425 } ) ;
416426
417427 it ( '_handleCommandClose(hadError=true) should log warning with error' , ( ) => {
418428 bridge . _handleCommandClose ( true ) ;
419- expect ( consoleWarnSpyDisconn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'COMMAND PORT DISCONNECTED with error' ) ) ;
429+ expect ( mockConsoleWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'COMMAND PORT DISCONNECTED with error' ) ) ; // Use global mock
420430 expect ( scheduleReconnectSpy ) . toHaveBeenCalledWith ( 'command' ) ;
421431 } ) ;
422432
@@ -425,25 +435,26 @@ describe('CgateWebBridge', () => {
425435 expect ( bridge . eventConnected ) . toBe ( false ) ;
426436 expect ( bridge . eventSocket ) . toBeNull ( ) ;
427437 expect ( evtRemoveListenersSpy ) . toHaveBeenCalledTimes ( 1 ) ;
428- expect ( consoleWarnSpyDisconn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'EVENT PORT DISCONNECTED' ) ) ;
429- expect ( consoleWarnSpyDisconn ) . not . toHaveBeenCalledWith ( expect . stringContaining ( 'with error' ) ) ;
438+ expect ( mockConsoleWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'EVENT PORT DISCONNECTED' ) ) ; // Use global mock
439+ expect ( mockConsoleWarn ) . not . toHaveBeenCalledWith ( expect . stringContaining ( 'with error' ) ) ;
430440 expect ( scheduleReconnectSpy ) . toHaveBeenCalledWith ( 'event' ) ;
431441 } ) ;
432442
433443 it ( '_handleEventClose(hadError=true) should log warning with error' , ( ) => {
434444 bridge . _handleEventClose ( true ) ;
435- expect ( consoleWarnSpyDisconn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'EVENT PORT DISCONNECTED with error' ) ) ;
445+ expect ( mockConsoleWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'EVENT PORT DISCONNECTED with error' ) ) ; // Use global mock
436446 expect ( scheduleReconnectSpy ) . toHaveBeenCalledWith ( 'event' ) ;
437447 } ) ;
438448
449+ // --- Error Handlers ---
439450 it ( '_handleMqttError (Auth Error code 5) should log specific error and exit' , ( ) => {
440451 const authError = new Error ( 'Auth failed' ) ;
441452 authError . code = 5 ;
442453 expect ( ( ) => {
443454 bridge . _handleMqttError ( authError ) ;
444455 } ) . toThrow ( 'process.exit called' ) ;
445- expect ( consoleErrorSpyDisconn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'MQTT Connection Error: Authentication failed' ) ) ;
446- expect ( consoleErrorSpyDisconn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Exiting due to fatal MQTT authentication error.' ) ) ;
456+ expect ( mockConsoleError ) . toHaveBeenCalledWith ( expect . stringContaining ( 'MQTT Connection Error: Authentication failed' ) ) ; // Use global mock
457+ expect ( mockConsoleError ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Exiting due to fatal MQTT authentication error.' ) ) ; // Use global mock
447458 expect ( processExitSpy ) . toHaveBeenCalledWith ( 1 ) ;
448459 expect ( bridge . clientConnected ) . toBe ( true ) ;
449460 expect ( bridge . client ) . toBeNull ( ) ;
@@ -453,7 +464,7 @@ describe('CgateWebBridge', () => {
453464 it ( '_handleMqttError (Generic Error) should log, reset flag, null client, remove listeners' , ( ) => {
454465 const genericError = new Error ( 'Some MQTT error' ) ;
455466 bridge . _handleMqttError ( genericError ) ;
456- expect ( consoleErrorSpyDisconn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'MQTT Client Error:' ) , genericError ) ;
467+ expect ( mockConsoleError ) . toHaveBeenCalledWith ( expect . stringContaining ( 'MQTT Client Error:' ) , genericError ) ; // Use global mock
457468 expect ( bridge . clientConnected ) . toBe ( false ) ;
458469 expect ( bridge . client ) . toBeNull ( ) ;
459470 expect ( clientRemoveListenersSpy ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -464,7 +475,7 @@ describe('CgateWebBridge', () => {
464475 it ( '_handleCommandError should log error, reset flag, destroy socket, and null socket' , ( ) => {
465476 const cmdError = new Error ( 'Command socket failed' ) ;
466477 bridge . _handleCommandError ( cmdError ) ;
467- expect ( consoleErrorSpyDisconn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'C-Gate Command Socket Error:' ) , cmdError ) ;
478+ expect ( mockConsoleError ) . toHaveBeenCalledWith ( expect . stringContaining ( 'C-Gate Command Socket Error:' ) , cmdError ) ; // Use global mock
468479 expect ( bridge . commandConnected ) . toBe ( false ) ;
469480 expect ( cmdDestroySpy ) . toHaveBeenCalledTimes ( 1 ) ;
470481 expect ( bridge . commandSocket ) . toBeNull ( ) ;
@@ -475,7 +486,7 @@ describe('CgateWebBridge', () => {
475486 mockCommandSocketDisconn . destroyed = true ;
476487 const cmdError = new Error ( 'Command socket failed again' ) ;
477488 bridge . _handleCommandError ( cmdError ) ;
478- expect ( consoleErrorSpyDisconn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'C-Gate Command Socket Error:' ) , cmdError ) ;
489+ expect ( mockConsoleError ) . toHaveBeenCalledWith ( expect . stringContaining ( 'C-Gate Command Socket Error:' ) , cmdError ) ; // Use global mock
479490 expect ( bridge . commandConnected ) . toBe ( false ) ;
480491 expect ( cmdDestroySpy ) . not . toHaveBeenCalled ( ) ;
481492 expect ( bridge . commandSocket ) . toBeNull ( ) ;
@@ -484,7 +495,7 @@ describe('CgateWebBridge', () => {
484495 it ( '_handleEventError should log error, reset flag, destroy socket, and null socket' , ( ) => {
485496 const evtError = new Error ( 'Event socket failed' ) ;
486497 bridge . _handleEventError ( evtError ) ;
487- expect ( consoleErrorSpyDisconn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'C-Gate Event Socket Error:' ) , evtError ) ;
498+ expect ( mockConsoleError ) . toHaveBeenCalledWith ( expect . stringContaining ( 'C-Gate Event Socket Error:' ) , evtError ) ; // Use global mock
488499 expect ( bridge . eventConnected ) . toBe ( false ) ;
489500 expect ( evtDestroySpy ) . toHaveBeenCalledTimes ( 1 ) ;
490501 expect ( bridge . eventSocket ) . toBeNull ( ) ;
@@ -495,7 +506,7 @@ describe('CgateWebBridge', () => {
495506 mockEventSocketDisconn . destroyed = true ;
496507 const evtError = new Error ( 'Event socket failed again' ) ;
497508 bridge . _handleEventError ( evtError ) ;
498- expect ( consoleErrorSpyDisconn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'C-Gate Event Socket Error:' ) , evtError ) ;
509+ expect ( mockConsoleError ) . toHaveBeenCalledWith ( expect . stringContaining ( 'C-Gate Event Socket Error:' ) , evtError ) ; // Use global mock
499510 expect ( bridge . eventConnected ) . toBe ( false ) ;
500511 expect ( evtDestroySpy ) . not . toHaveBeenCalled ( ) ;
501512 expect ( bridge . eventSocket ) . toBeNull ( ) ;
@@ -709,6 +720,9 @@ describe('CgateWebBridge', () => {
709720 } ) ;
710721 // ... more _connectCommandSocket tests ...
711722 it ( 'should handle socket.connect error' , ( ) => {
723+ // --- Local console mock for this test ---
724+ const consoleErrorSpyLocal = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
725+
712726 const connectError = new Error ( 'Connection failed' ) ;
713727 mockCmdSocketFactory . mockImplementationOnce ( ( ) => {
714728 const socket = new EventEmitter ( ) ;
@@ -720,10 +734,15 @@ describe('CgateWebBridge', () => {
720734 return socket ;
721735 } ) ;
722736 const errorSpy = jest . spyOn ( bridge , '_handleCommandError' ) ;
737+
723738 bridge . _connectCommandSocket ( ) ;
739+
724740 expect ( lastMockCmdSocket . connect ) . toHaveBeenCalled ( ) ;
725741 expect ( errorSpy ) . toHaveBeenCalledWith ( connectError ) ;
742+
726743 errorSpy . mockRestore ( ) ;
744+ // --- Restore local console mock ---
745+ consoleErrorSpyLocal . mockRestore ( ) ;
727746 } ) ;
728747 } ) ;
729748
@@ -734,8 +753,11 @@ describe('CgateWebBridge', () => {
734753 } ) ;
735754 // ... more _connectEventSocket tests ...
736755 it ( 'should handle socket.connect error' , ( ) => {
737- const connectError = new Error ( 'Event Connection failed' ) ;
738- mockEvtSocketFactory . mockImplementationOnce ( ( ) => {
756+ // --- Local console mock for this test ---
757+ const consoleErrorSpyLocal = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
758+
759+ const connectError = new Error ( 'Event Connection failed' ) ;
760+ mockEvtSocketFactory . mockImplementationOnce ( ( ) => {
739761 const socket = new EventEmitter ( ) ;
740762 socket . connect = jest . fn ( ( ) => { throw connectError ; } ) ;
741763 socket . on = jest . fn ( ) ;
@@ -749,7 +771,9 @@ describe('CgateWebBridge', () => {
749771 expect ( lastMockEvtSocket . connect ) . toHaveBeenCalled ( ) ;
750772 expect ( errorSpy ) . toHaveBeenCalledWith ( connectError ) ;
751773 errorSpy . mockRestore ( ) ;
752- } ) ;
774+ // --- Restore local console mock ---
775+ consoleErrorSpyLocal . mockRestore ( ) ;
776+ } ) ;
753777 } ) ;
754778
755779 describe ( '_connectMqtt' , ( ) => {
0 commit comments