1
- import { captureException } from '@sentry/core' ;
2
-
3
- import { DEFAULT_FLUSH_MIN_DELAY , REPLAY_SESSION_KEY , VISIBILITY_CHANGE_TIMEOUT , WINDOW } from '../../src/constants' ;
1
+ import { captureException , getCurrentHub } from '@sentry/core' ;
2
+
3
+ import {
4
+ DEFAULT_FLUSH_MIN_DELAY ,
5
+ MAX_SESSION_LIFE ,
6
+ REPLAY_SESSION_KEY ,
7
+ VISIBILITY_CHANGE_TIMEOUT ,
8
+ WINDOW ,
9
+ } from '../../src/constants' ;
4
10
import type { ReplayContainer } from '../../src/replay' ;
5
11
import { addEvent } from '../../src/util/addEvent' ;
6
12
import { PerformanceEntryResource } from '../fixtures/performanceEntry/resource' ;
@@ -258,12 +264,10 @@ describe('Integration | errorSampleRate', () => {
258
264
expect ( replay ) . not . toHaveLastSentReplay ( ) ;
259
265
} ) ;
260
266
261
- it ( 'does not upload if user has been idle for more than 15 minutes and comes back to move their mouse' , async ( ) => {
267
+ it ( 'stops replay if user has been idle for more than 15 minutes and comes back to move their mouse' , async ( ) => {
262
268
// Idle for 15 minutes
263
269
jest . advanceTimersByTime ( 15 * 60000 ) ;
264
270
265
- // TBD: We are currently deciding that this event will get dropped, but
266
- // this could/should change in the future.
267
271
const TEST_EVENT = {
268
272
data : { name : 'lost event' } ,
269
273
timestamp : BASE_TIMESTAMP ,
@@ -275,15 +279,11 @@ describe('Integration | errorSampleRate', () => {
275
279
jest . runAllTimers ( ) ;
276
280
await new Promise ( process . nextTick ) ;
277
281
278
- // Instead of recording the above event, a full snapshot will occur.
279
- //
280
- // TODO: We could potentially figure out a way to save the last session,
281
- // and produce a checkout based on a previous checkout + updates, and then
282
- // replay the event on top. Or maybe replay the event on top of a refresh
283
- // snapshot.
282
+ // We stop recording after 15 minutes of inactivity in error mode
284
283
285
284
expect ( replay ) . not . toHaveLastSentReplay ( ) ;
286
- expect ( mockRecord . takeFullSnapshot ) . toHaveBeenCalledWith ( true ) ;
285
+ expect ( replay . isEnabled ( ) ) . toBe ( false ) ;
286
+ expect ( mockRecord . takeFullSnapshot ) . not . toHaveBeenCalled ( ) ;
287
287
} ) ;
288
288
289
289
it ( 'has the correct timestamps with deferred root event and last replay update' , async ( ) => {
@@ -369,6 +369,52 @@ describe('Integration | errorSampleRate', () => {
369
369
] ) ,
370
370
} ) ;
371
371
} ) ;
372
+
373
+ it ( 'stops replay when session expires' , async ( ) => {
374
+ jest . setSystemTime ( BASE_TIMESTAMP ) ;
375
+
376
+ const TEST_EVENT = { data : { } , timestamp : BASE_TIMESTAMP , type : 3 } ;
377
+ mockRecord . _emitter ( TEST_EVENT ) ;
378
+
379
+ expect ( mockRecord . takeFullSnapshot ) . not . toHaveBeenCalled ( ) ;
380
+ expect ( replay ) . not . toHaveLastSentReplay ( ) ;
381
+
382
+ jest . runAllTimers ( ) ;
383
+ await new Promise ( process . nextTick ) ;
384
+
385
+ captureException ( new Error ( 'testing' ) ) ;
386
+
387
+ jest . advanceTimersByTime ( DEFAULT_FLUSH_MIN_DELAY ) ;
388
+ await new Promise ( process . nextTick ) ;
389
+
390
+ expect ( replay ) . toHaveLastSentReplay ( ) ;
391
+
392
+ // Wait a bit, shortly before session expires
393
+ jest . advanceTimersByTime ( MAX_SESSION_LIFE - 1000 ) ;
394
+ await new Promise ( process . nextTick ) ;
395
+
396
+ mockRecord . _emitter ( TEST_EVENT ) ;
397
+ replay . triggerUserActivity ( ) ;
398
+
399
+ expect ( replay ) . toHaveLastSentReplay ( ) ;
400
+
401
+ // Now wait after session expires - should stop recording
402
+ mockRecord . takeFullSnapshot . mockClear ( ) ;
403
+ ( getCurrentHub ( ) . getClient ( ) ! . getTransport ( ) ! . send as unknown as jest . SpyInstance < any > ) . mockClear ( ) ;
404
+
405
+ jest . advanceTimersByTime ( 10_000 ) ;
406
+ await new Promise ( process . nextTick ) ;
407
+
408
+ mockRecord . _emitter ( TEST_EVENT ) ;
409
+ replay . triggerUserActivity ( ) ;
410
+
411
+ jest . advanceTimersByTime ( DEFAULT_FLUSH_MIN_DELAY ) ;
412
+ await new Promise ( process . nextTick ) ;
413
+
414
+ expect ( replay ) . not . toHaveLastSentReplay ( ) ;
415
+ expect ( mockRecord . takeFullSnapshot ) . toHaveBeenCalledTimes ( 0 ) ;
416
+ expect ( replay . isEnabled ( ) ) . toBe ( false ) ;
417
+ } ) ;
372
418
} ) ;
373
419
374
420
/**
0 commit comments