@@ -59,44 +59,56 @@ describe("TelemetryRetryManager", () => {
5959 it ( "should start retry timer" , async ( ) => {
6060 retryManager . start ( )
6161
62- // Should not process immediately
63- expect ( mockQueue . getEventsForRetry ) . toHaveBeenCalledTimes ( 0 )
62+ // Should process immediately on start
63+ await vi . waitFor ( ( ) => {
64+ expect ( mockQueue . getEventsForRetry ) . toHaveBeenCalledTimes ( 1 )
65+ } )
6466
6567 // Advance timer and run pending timers
6668 await vi . advanceTimersByTimeAsync ( 30000 )
67- expect ( mockQueue . getEventsForRetry ) . toHaveBeenCalledTimes ( 1 )
69+ expect ( mockQueue . getEventsForRetry ) . toHaveBeenCalledTimes ( 2 )
6870
6971 // Advance timer again
7072 await vi . advanceTimersByTimeAsync ( 30000 )
71- expect ( mockQueue . getEventsForRetry ) . toHaveBeenCalledTimes ( 2 )
73+ expect ( mockQueue . getEventsForRetry ) . toHaveBeenCalledTimes ( 3 )
7274 } )
7375
7476 it ( "should not start multiple timers" , async ( ) => {
7577 retryManager . start ( )
7678 retryManager . start ( )
7779
80+ // Should only process once on start
81+ await vi . waitFor ( ( ) => {
82+ expect ( mockQueue . getEventsForRetry ) . toHaveBeenCalledTimes ( 1 )
83+ } )
84+
7885 // Advance timer
7986 await vi . advanceTimersByTimeAsync ( 30000 )
80- expect ( mockQueue . getEventsForRetry ) . toHaveBeenCalledTimes ( 1 )
87+ expect ( mockQueue . getEventsForRetry ) . toHaveBeenCalledTimes ( 2 )
8188
8289 // Advance timer again
8390 await vi . advanceTimersByTimeAsync ( 30000 )
84- expect ( mockQueue . getEventsForRetry ) . toHaveBeenCalledTimes ( 2 )
91+ expect ( mockQueue . getEventsForRetry ) . toHaveBeenCalledTimes ( 3 )
8592 } )
8693
8794 it ( "should stop retry timer" , async ( ) => {
8895 retryManager . start ( )
8996
97+ // Should process immediately on start
98+ await vi . waitFor ( ( ) => {
99+ expect ( mockQueue . getEventsForRetry ) . toHaveBeenCalledTimes ( 1 )
100+ } )
101+
90102 // Advance timer once
91103 await vi . advanceTimersByTimeAsync ( 30000 )
92- expect ( mockQueue . getEventsForRetry ) . toHaveBeenCalledTimes ( 1 )
104+ expect ( mockQueue . getEventsForRetry ) . toHaveBeenCalledTimes ( 2 )
93105
94106 retryManager . stop ( )
95107
96108 // Advance timer again
97109 await vi . advanceTimersByTimeAsync ( 30000 )
98- // Should still only be called once
99- expect ( mockQueue . getEventsForRetry ) . toHaveBeenCalledTimes ( 1 )
110+ // Should still only be called twice
111+ expect ( mockQueue . getEventsForRetry ) . toHaveBeenCalledTimes ( 2 )
100112 } )
101113 } )
102114
@@ -156,13 +168,15 @@ describe("TelemetryRetryManager", () => {
156168
157169 retryManager . start ( )
158170
159- // Advance timer to trigger processing
160- await vi . advanceTimersByTimeAsync ( 30000 )
171+ // Wait for immediate processing to complete
172+ await vi . waitFor ( ( ) => {
173+ expect ( mockQueue . getEventsForRetry ) . toHaveBeenCalled ( )
174+ } )
161175
162176 // Should process in batches of 10
163177 // Filter out connection check events
164178 const actualEventCalls = sendEventMock . mock . calls . filter (
165- ( call ) => ( call [ 0 ] . event as string ) !== "telemetry_connection_check" ,
179+ ( call ) => call [ 0 ] . event !== TelemetryEventName . TELEMETRY_CONNECTION_CHECK ,
166180 )
167181 expect ( actualEventCalls ) . toHaveLength ( 25 )
168182 expect ( mockQueue . updateEventAfterRetry ) . toHaveBeenCalledTimes ( 25 )
@@ -184,8 +198,10 @@ describe("TelemetryRetryManager", () => {
184198
185199 retryManager . start ( )
186200
187- // Advance timer to trigger processing
188- await vi . advanceTimersByTimeAsync ( 30000 )
201+ // Wait for immediate processing to complete
202+ await vi . waitFor ( ( ) => {
203+ expect ( mockQueue . updateEventAfterRetry ) . toHaveBeenCalled ( )
204+ } )
189205
190206 expect ( mockQueue . updateEventAfterRetry ) . toHaveBeenCalledWith ( "event-1" , true , undefined )
191207 } )
@@ -206,8 +222,10 @@ describe("TelemetryRetryManager", () => {
206222
207223 retryManager . start ( )
208224
209- // Advance timer to trigger processing
210- await vi . advanceTimersByTimeAsync ( 30000 )
225+ // Wait for immediate processing to complete
226+ await vi . waitFor ( ( ) => {
227+ expect ( mockQueue . updateEventAfterRetry ) . toHaveBeenCalled ( )
228+ } )
211229
212230 expect ( mockQueue . updateEventAfterRetry ) . toHaveBeenCalledWith ( "event-1" , false , "Network error" )
213231 } )
@@ -232,15 +250,33 @@ describe("TelemetryRetryManager", () => {
232250
233251 retryManager . start ( )
234252
235- // Advance timer to trigger processing
236- await vi . advanceTimersByTimeAsync ( 30000 )
253+ // Wait for immediate processing to complete
254+ await vi . waitFor ( ( ) => {
255+ expect ( connectionStatusCallback ) . toHaveBeenCalled ( )
256+ } )
237257
238258 expect ( connectionStatusCallback ) . toHaveBeenCalledWith ( false )
239259
240- // Now succeed
241- sendEventMock . mockResolvedValueOnce ( undefined )
260+ // Reset mocks for next iteration
261+ connectionStatusCallback . mockClear ( )
262+ mockQueue . getEventsForRetry . mockClear ( )
263+ mockQueue . updateEventAfterRetry . mockClear ( )
264+ sendEventMock . mockClear ( )
265+
266+ // Now succeed - mock the send to succeed this time
267+ sendEventMock . mockResolvedValue ( undefined )
268+
269+ // Set up the queue to return the same event again
270+ mockQueue . getEventsForRetry . mockResolvedValue ( events )
271+
242272 await vi . advanceTimersByTimeAsync ( 30000 )
243273
274+ // Wait for the processing to complete
275+ await vi . waitFor ( ( ) => {
276+ expect ( mockQueue . updateEventAfterRetry ) . toHaveBeenCalled ( )
277+ } )
278+
279+ // Now check that connection status was updated
244280 expect ( connectionStatusCallback ) . toHaveBeenCalledWith ( true )
245281 } )
246282
@@ -249,8 +285,10 @@ describe("TelemetryRetryManager", () => {
249285
250286 retryManager . start ( )
251287
252- // Advance timer to trigger processing
253- await vi . advanceTimersByTimeAsync ( 30000 )
288+ // Wait for immediate processing to complete
289+ await vi . waitFor ( ( ) => {
290+ expect ( mockQueue . pruneFailedEvents ) . toHaveBeenCalled ( )
291+ } )
254292
255293 expect ( mockQueue . pruneFailedEvents ) . toHaveBeenCalled ( )
256294 } )
@@ -275,7 +313,7 @@ describe("TelemetryRetryManager", () => {
275313 it ( "should periodically check connection status" , async ( ) => {
276314 // Mock successful connection check
277315 sendEventMock . mockImplementation ( ( event : TelemetryEvent ) => {
278- if ( ( event . event as string ) === "telemetry_connection_check" ) {
316+ if ( event . event === TelemetryEventName . TELEMETRY_CONNECTION_CHECK ) {
279317 return Promise . resolve ( )
280318 }
281319 return Promise . reject ( new Error ( "Other error" ) )
@@ -296,16 +334,22 @@ describe("TelemetryRetryManager", () => {
296334
297335 retryManager . start ( )
298336
299- // First advance to trigger initial processing (30s)
300- await vi . advanceTimersByTimeAsync ( 30000 )
337+ // Wait for immediate processing to complete
338+ await vi . waitFor ( ( ) => {
339+ expect ( mockQueue . getEventsForRetry ) . toHaveBeenCalled ( )
340+ } )
301341
302- // Then advance past connection check interval (1 minute more)
303- await vi . advanceTimersByTimeAsync ( 35000 )
342+ // Clear previous calls from immediate processing
343+ sendEventMock . mockClear ( )
344+ mockQueue . getEventsForRetry . mockClear ( )
345+
346+ // Advance past connection check interval (1 minute + 30s)
347+ await vi . advanceTimersByTimeAsync ( 90000 )
304348
305349 // Should have sent a connection check event
306350 expect ( sendEventMock ) . toHaveBeenCalledWith (
307351 expect . objectContaining ( {
308- event : "telemetry_connection_check" ,
352+ event : TelemetryEventName . TELEMETRY_CONNECTION_CHECK ,
309353 } ) ,
310354 )
311355 } )
@@ -329,12 +373,12 @@ describe("TelemetryRetryManager", () => {
329373
330374 retryManager . start ( )
331375
332- // First advance to trigger initial processing (30s)
333- await vi . advanceTimersByTimeAsync ( 30000 )
334-
335- // Then advance past connection check interval (1 minute more)
336- await vi . advanceTimersByTimeAsync ( 35000 )
376+ // Wait for immediate processing to complete
377+ await vi . waitFor ( ( ) => {
378+ expect ( connectionStatusCallback ) . toHaveBeenCalled ( )
379+ } )
337380
381+ // Connection should already be marked as disconnected from immediate processing
338382 expect ( connectionStatusCallback ) . toHaveBeenCalledWith ( false )
339383 } )
340384 } )
0 commit comments