@@ -356,4 +356,102 @@ describe("AutoCollection/Statsbeat", () => {
356356 } ) . catch ( ( error ) => { done ( error ) ; } ) ;
357357 } ) ;
358358 } ) ;
359+
360+ describe ( "#enable() with long interval delay" , ( ) => {
361+ let clock : sinon . SinonFakeTimers ;
362+
363+ beforeEach ( ( ) => {
364+ clock = sinon . useFakeTimers ( ) ;
365+ } ) ;
366+
367+ afterEach ( ( ) => {
368+ clock . restore ( ) ;
369+ } ) ;
370+
371+ it ( "should delay first long interval statsbeat by 15 seconds" , ( done ) => {
372+ const trackLongIntervalSpy = sandbox . spy ( statsBeat , "trackLongIntervalStatsbeats" ) ;
373+
374+ // Enable statsbeat
375+ statsBeat . enable ( true ) ;
376+
377+ // Immediately after enabling, trackLongIntervalStatsbeats should not have been called
378+ assert . equal ( trackLongIntervalSpy . callCount , 0 , "trackLongIntervalStatsbeats should not be called immediately" ) ;
379+
380+ // Fast-forward 10 seconds - still should not be called
381+ clock . tick ( 10000 ) ;
382+ assert . equal ( trackLongIntervalSpy . callCount , 0 , "trackLongIntervalStatsbeats should not be called after 10 seconds" ) ;
383+
384+ // Fast-forward to 15 seconds - now it should be called
385+ clock . tick ( 5000 ) ;
386+ assert . equal ( trackLongIntervalSpy . callCount , 1 , "trackLongIntervalStatsbeats should be called after 15 seconds" ) ;
387+
388+ done ( ) ;
389+ } ) ;
390+
391+ it ( "should call trackLongIntervalStatsbeats on regular interval after initial delay" , ( done ) => {
392+ const trackLongIntervalSpy = sandbox . spy ( statsBeat , "trackLongIntervalStatsbeats" ) ;
393+
394+ // Enable statsbeat
395+ statsBeat . enable ( true ) ;
396+
397+ // Fast-forward to the first call (15 seconds)
398+ clock . tick ( 15000 ) ;
399+ assert . equal ( trackLongIntervalSpy . callCount , 1 , "First call should happen after 15 seconds" ) ;
400+
401+ // Fast-forward by the long interval (24 hours)
402+ clock . tick ( Statsbeat . STATS_COLLECTION_LONG_INTERVAL ) ;
403+ assert . equal ( trackLongIntervalSpy . callCount , 2 , "Second call should happen after the interval" ) ;
404+
405+ // Fast-forward by another long interval
406+ clock . tick ( Statsbeat . STATS_COLLECTION_LONG_INTERVAL ) ;
407+ assert . equal ( trackLongIntervalSpy . callCount , 3 , "Third call should happen after another interval" ) ;
408+
409+ done ( ) ;
410+ } ) ;
411+
412+ it ( "should not delay if enable is called multiple times" , ( done ) => {
413+ const trackLongIntervalSpy = sandbox . spy ( statsBeat , "trackLongIntervalStatsbeats" ) ;
414+
415+ // Enable statsbeat first time
416+ statsBeat . enable ( true ) ;
417+
418+ // Fast-forward 5 seconds
419+ clock . tick ( 5000 ) ;
420+ assert . equal ( trackLongIntervalSpy . callCount , 0 , "Should not be called yet" ) ;
421+
422+ // Disable and re-enable (simulating multiple enable calls)
423+ statsBeat . enable ( false ) ;
424+ statsBeat . enable ( true ) ;
425+
426+ // The second enable should start a new 15-second timer
427+ clock . tick ( 10000 ) ; // Total 15 seconds from first enable, but only 10 from second
428+ assert . equal ( trackLongIntervalSpy . callCount , 0 , "Should not be called yet after re-enable" ) ;
429+
430+ // Fast-forward another 5 seconds (15 seconds from second enable)
431+ clock . tick ( 5000 ) ;
432+ assert . equal ( trackLongIntervalSpy . callCount , 1 , "Should be called 15 seconds after re-enable" ) ;
433+
434+ done ( ) ;
435+ } ) ;
436+
437+ it ( "should handle disable before initial delay completes" , ( done ) => {
438+ const trackLongIntervalSpy = sandbox . spy ( statsBeat , "trackLongIntervalStatsbeats" ) ;
439+
440+ // Enable statsbeat
441+ statsBeat . enable ( true ) ;
442+
443+ // Fast-forward 10 seconds (before the 15-second delay completes)
444+ clock . tick ( 10000 ) ;
445+ assert . equal ( trackLongIntervalSpy . callCount , 0 , "Should not be called yet" ) ;
446+
447+ // Disable before the delay completes
448+ statsBeat . enable ( false ) ;
449+
450+ // Fast-forward past where the call would have happened
451+ clock . tick ( 10000 ) ; // Total 20 seconds
452+ assert . equal ( trackLongIntervalSpy . callCount , 0 , "Should not be called after disable" ) ;
453+
454+ done ( ) ;
455+ } ) ;
456+ } ) ;
359457} ) ;
0 commit comments