@@ -139,6 +139,111 @@ describe("Signaling connect method", () => {
139139 } ) ;
140140} ) ;
141141
142+ describe ( "Signaling websocket event handlers" , ( ) => {
143+ let signaling : Signaling ;
144+ beforeEach ( async ( ) => {
145+ signaling = new Signaling ( ) ;
146+ await signaling . connect ( { endpointToken : "test-token" } ) ;
147+ } ) ;
148+
149+ function getWsCallback ( event : string ) {
150+ const ws = ( signaling as any ) . ws ;
151+ return ws . on . mock . calls . find ( ( call : any ) => call [ 0 ] === event ) ?. [ 1 ] ;
152+ }
153+
154+ test ( "should emit init and set up ping interval on open" , async ( ) => {
155+ const emitSpy = jest . spyOn ( signaling , "emit" ) ;
156+ const openCallback = getWsCallback ( "open" ) ;
157+ expect ( openCallback ) . toBeDefined ( ) ;
158+
159+ await openCallback ( ) ;
160+
161+ expect ( emitSpy ) . toHaveBeenCalledWith ( "init" , expect . anything ( ) ) ;
162+ expect ( ( signaling as any ) . pingInterval ) . toBeDefined ( ) ;
163+ } ) ;
164+
165+ test ( "should reject with error and disconnect on 403 error" , async ( ) => {
166+ const errorCallback = getWsCallback ( "error" ) ;
167+ expect ( errorCallback ) . toBeDefined ( ) ;
168+
169+ const ws = ( signaling as any ) . ws ;
170+ errorCallback ( { message : "Unexpected server response: 403" } ) ;
171+
172+ expect ( ws . close ) . toHaveBeenCalledWith ( 403 ) ;
173+ expect ( ws . setAutoReconnect ) . toHaveBeenCalledWith ( false ) ;
174+ } ) ;
175+
176+ test ( "should handle non-403 error without throwing" , async ( ) => {
177+ const errorCallback = getWsCallback ( "error" ) ;
178+ expect ( errorCallback ) . toBeDefined ( ) ;
179+
180+ // Should not throw on a generic error
181+ expect ( ( ) => errorCallback ( { message : "some other error" } ) ) . not . toThrow ( ) ;
182+
183+ // ws should not be closed on non-403 errors
184+ const ws = ( signaling as any ) . ws ;
185+ expect ( ws . setAutoReconnect ) . not . toHaveBeenCalled ( ) ;
186+ } ) ;
187+
188+ test ( "should clear ping interval and set isReady false on close" , async ( ) => {
189+ // Trigger open first to set up pingInterval
190+ const openCallback = getWsCallback ( "open" ) ;
191+ await openCallback ( ) ;
192+
193+ const closeCallback = getWsCallback ( "close" ) ;
194+ expect ( closeCallback ) . toBeDefined ( ) ;
195+
196+ closeCallback ( 4000 ) ;
197+
198+ expect ( ( signaling as any ) . isReady ) . toBe ( false ) ;
199+ } ) ;
200+
201+ test ( "should call _disconnect on close with code 1000" , async ( ) => {
202+ const closeCallback = getWsCallback ( "close" ) ;
203+ expect ( closeCallback ) . toBeDefined ( ) ;
204+
205+ closeCallback ( 1000 ) ;
206+
207+ // After _disconnect(false), ws should be null
208+ expect ( ( signaling as any ) . ws ) . toBeNull ( ) ;
209+ expect ( ( signaling as any ) . isReady ) . toBe ( false ) ;
210+ } ) ;
211+ } ) ;
212+
213+ describe ( "Signaling disconnect" , ( ) => {
214+ test ( "should call leave notification and close ws on disconnect" , async ( ) => {
215+ const signaling = new Signaling ( ) ;
216+ await signaling . connect ( { endpointToken : "test-token" } ) ;
217+
218+ const ws = ( signaling as any ) . ws ;
219+ signaling . disconnect ( ) ;
220+
221+ expect ( ws . notify ) . toHaveBeenCalledWith ( "leave" ) ;
222+ expect ( ws . close ) . toHaveBeenCalled ( ) ;
223+ expect ( ws . removeAllListeners ) . toHaveBeenCalled ( ) ;
224+ expect ( ( signaling as any ) . ws ) . toBeNull ( ) ;
225+ expect ( ( signaling as any ) . isReady ) . toBe ( false ) ;
226+ } ) ;
227+
228+ test ( "should handle disconnect with diagnosticsBatcher" , async ( ) => {
229+ const diagnosticsBatcher = new DiagnosticsBatcher ( ) ;
230+ const shutdownSpy = jest . spyOn ( diagnosticsBatcher , "shutdown" ) ;
231+ const signaling = new Signaling ( diagnosticsBatcher ) ;
232+ await signaling . connect ( { endpointToken : "test-token" } ) ;
233+
234+ signaling . disconnect ( ) ;
235+
236+ expect ( shutdownSpy ) . toHaveBeenCalled ( ) ;
237+ expect ( ( signaling as any ) . ws ) . toBeNull ( ) ;
238+ } ) ;
239+
240+ test ( "should not throw when disconnect called without active ws" , ( ) => {
241+ const signaling = new Signaling ( ) ;
242+ // Never connected, ws is null
243+ expect ( ( ) => signaling . disconnect ( ) ) . not . toThrow ( ) ;
244+ } ) ;
245+ } ) ;
246+
142247describe ( "Signaling test all the smaller functions" , ( ) => {
143248 let signaling : Signaling ;
144249 beforeEach ( async ( ) => {
0 commit comments