11import * as SentryUtils from '@sentry/utils' ;
22
3- import { DEFAULT_FLUSH_MIN_DELAY , WINDOW } from '../../src/constants' ;
3+ import { DEFAULT_FLUSH_MIN_DELAY , MAX_SESSION_LIFE , WINDOW } from '../../src/constants' ;
44import type { ReplayContainer } from '../../src/replay' ;
55import { clearSession } from '../../src/session/clearSession' ;
66import type { EventBuffer } from '../../src/types' ;
@@ -286,15 +286,22 @@ describe('Integration | flush', () => {
286286
287287 expect ( mockFlush ) . toHaveBeenCalledTimes ( 20 ) ;
288288 expect ( mockSendReplay ) . toHaveBeenCalledTimes ( 1 ) ;
289+
290+ replay . getOptions ( ) . minReplayDuration = 0 ;
289291 } ) ;
290292
291293 it ( 'does not flush if session is too long' , async ( ) => {
292294 replay . timeouts . maxSessionLife = 100_000 ;
293- jest . setSystemTime ( new Date ( BASE_TIMESTAMP ) ) ;
295+ jest . setSystemTime ( BASE_TIMESTAMP ) ;
294296
295297 sessionStorage . clear ( ) ;
296298 clearSession ( replay ) ;
297299 replay [ '_loadAndCheckSession' ] ( ) ;
300+ // No-op _loadAndCheckSession to avoid us resetting the session for this test
301+ const _tmp = replay [ '_loadAndCheckSession' ] ;
302+ replay [ '_loadAndCheckSession' ] = ( ) => {
303+ return true ;
304+ } ;
298305
299306 await advanceTimers ( 120_000 ) ;
300307
@@ -308,7 +315,71 @@ describe('Integration | flush', () => {
308315 mockRecord . _emitter ( TEST_EVENT ) ;
309316
310317 await advanceTimers ( DEFAULT_FLUSH_MIN_DELAY ) ;
318+
311319 expect ( mockFlush ) . toHaveBeenCalledTimes ( 1 ) ;
312320 expect ( mockSendReplay ) . toHaveBeenCalledTimes ( 0 ) ;
321+
322+ replay . timeouts . maxSessionLife = MAX_SESSION_LIFE ;
323+ replay [ '_loadAndCheckSession' ] = _tmp ;
324+ } ) ;
325+
326+ it ( 'logs warning if flushing initial segment without checkout' , async ( ) => {
327+ replay . getOptions ( ) . _experiments . traceInternals = true ;
328+
329+ sessionStorage . clear ( ) ;
330+ clearSession ( replay ) ;
331+ replay [ '_loadAndCheckSession' ] ( ) ;
332+ await new Promise ( process . nextTick ) ;
333+ jest . setSystemTime ( BASE_TIMESTAMP ) ;
334+
335+ // Clear the event buffer to simulate no checkout happened
336+ replay . eventBuffer ! . clear ( ) ;
337+
338+ // click happens first
339+ domHandler ( {
340+ name : 'click' ,
341+ } ) ;
342+
343+ // no checkout!
344+ await advanceTimers ( DEFAULT_FLUSH_MIN_DELAY ) ;
345+
346+ expect ( mockFlush ) . toHaveBeenCalledTimes ( 1 ) ;
347+ expect ( mockSendReplay ) . toHaveBeenCalledTimes ( 1 ) ;
348+
349+ const replayData = mockSendReplay . mock . calls [ 0 ] [ 0 ] ;
350+
351+ expect ( JSON . parse ( replayData . recordingData ) ) . toEqual ( [
352+ {
353+ type : 5 ,
354+ timestamp : BASE_TIMESTAMP ,
355+ data : {
356+ tag : 'breadcrumb' ,
357+ payload : {
358+ timestamp : BASE_TIMESTAMP / 1000 ,
359+ type : 'default' ,
360+ category : 'ui.click' ,
361+ message : '<unknown>' ,
362+ data : { } ,
363+ } ,
364+ } ,
365+ } ,
366+ {
367+ type : 5 ,
368+ timestamp : BASE_TIMESTAMP + DEFAULT_FLUSH_MIN_DELAY ,
369+ data : {
370+ tag : 'breadcrumb' ,
371+ payload : {
372+ timestamp : ( BASE_TIMESTAMP + DEFAULT_FLUSH_MIN_DELAY ) / 1000 ,
373+ type : 'default' ,
374+ category : 'console' ,
375+ data : { logger : 'replay' } ,
376+ level : 'info' ,
377+ message : '[Replay] Flushing initial segment without checkout.' ,
378+ } ,
379+ } ,
380+ } ,
381+ ] ) ;
382+
383+ replay . getOptions ( ) . _experiments . traceInternals = false ;
313384 } ) ;
314385} ) ;
0 commit comments