@@ -82,3 +82,99 @@ sentryTest('handles session that exceeds max age', async ({ getLocalTestPath, pa
82
82
const stringifiedSnapshot2 = normalize ( fullSnapshots2 [ 0 ] ) ;
83
83
expect ( stringifiedSnapshot2 ) . toMatchSnapshot ( 'snapshot-2.json' ) ;
84
84
} ) ;
85
+
86
+ sentryTest ( 'handles many consequitive events in session that exceeds max age' , async ( { getLocalTestPath, page } ) => {
87
+ if ( shouldSkipReplayTest ( ) ) {
88
+ sentryTest . skip ( ) ;
89
+ }
90
+
91
+ const reqPromise0 = waitForReplayRequest ( page , 0 ) ;
92
+ const reqPromise1 = waitForReplayRequest ( page , 1 ) ;
93
+
94
+ await page . route ( 'https://dsn.ingest.sentry.io/**/*' , route => {
95
+ return route . fulfill ( {
96
+ status : 200 ,
97
+ contentType : 'application/json' ,
98
+ body : JSON . stringify ( { id : 'test-id' } ) ,
99
+ } ) ;
100
+ } ) ;
101
+
102
+ const url = await getLocalTestPath ( { testDir : __dirname } ) ;
103
+
104
+ await page . goto ( url ) ;
105
+
106
+ const replay0 = await getReplaySnapshot ( page ) ;
107
+ // We use the `initialTimestamp` of the replay to do any time based calculations
108
+ const startTimestamp = replay0 . _context . initialTimestamp ;
109
+
110
+ const req0 = await reqPromise0 ;
111
+
112
+ const replayEvent0 = getReplayEvent ( req0 ) ;
113
+ expect ( replayEvent0 ) . toEqual ( getExpectedReplayEvent ( { } ) ) ;
114
+
115
+ const fullSnapshots0 = getFullRecordingSnapshots ( req0 ) ;
116
+ expect ( fullSnapshots0 . length ) . toEqual ( 1 ) ;
117
+ const stringifiedSnapshot = normalize ( fullSnapshots0 [ 0 ] ) ;
118
+ expect ( stringifiedSnapshot ) . toMatchSnapshot ( 'snapshot-0.json' ) ;
119
+
120
+ // Wait again for a new segment 0 (=new session)
121
+ const reqPromise2 = waitForReplayRequest ( page , 0 ) ;
122
+
123
+ // Wait for an incremental snapshot
124
+ // Wait half of the session max age (after initial flush), but account for potentially slow runners
125
+ const timePassed1 = Date . now ( ) - startTimestamp ;
126
+ await new Promise ( resolve => setTimeout ( resolve , Math . max ( SESSION_MAX_AGE / 2 - timePassed1 , 0 ) ) ) ;
127
+
128
+ await page . click ( '#button1' ) ;
129
+
130
+ const req1 = await reqPromise1 ;
131
+ const replayEvent1 = getReplayEvent ( req1 ) ;
132
+
133
+ expect ( replayEvent1 ) . toEqual ( getExpectedReplayEvent ( { segment_id : 1 , urls : [ ] } ) ) ;
134
+
135
+ const replay1 = await getReplaySnapshot ( page ) ;
136
+ const oldSessionId = replay1 . session ?. id ;
137
+
138
+ // Wait for session to expire
139
+ const timePassed2 = Date . now ( ) - startTimestamp ;
140
+ await new Promise ( resolve => setTimeout ( resolve , Math . max ( SESSION_MAX_AGE - timePassed2 , 0 ) ) ) ;
141
+
142
+ await page . evaluate ( `
143
+ Object.defineProperty(document, 'visibilityState', {
144
+ configurable: true,
145
+ get: function () {
146
+ return 'hidden';
147
+ },
148
+ });
149
+ document.dispatchEvent(new Event('visibilitychange'));
150
+ ` ) ;
151
+
152
+ // Many things going on at the same time...
153
+ void page . evaluate ( `
154
+ Object.defineProperty(document, 'visibilityState', {
155
+ configurable: true,
156
+ get: function () {
157
+ return 'visible';
158
+ },
159
+ });
160
+ document.dispatchEvent(new Event('visibilitychange'));
161
+ ` ) ;
162
+ void page . click ( '#button1' ) ;
163
+ void page . click ( '#button2' ) ;
164
+ await new Promise ( resolve => setTimeout ( resolve , 1 ) ) ;
165
+ void page . click ( '#button1' ) ;
166
+ void page . click ( '#button2' ) ;
167
+
168
+ const req2 = await reqPromise2 ;
169
+ const replay2 = await getReplaySnapshot ( page ) ;
170
+
171
+ expect ( replay2 . session ?. id ) . not . toEqual ( oldSessionId ) ;
172
+
173
+ const replayEvent2 = getReplayEvent ( req2 ) ;
174
+ expect ( replayEvent2 ) . toEqual ( getExpectedReplayEvent ( { } ) ) ;
175
+
176
+ const fullSnapshots2 = getFullRecordingSnapshots ( req2 ) ;
177
+ expect ( fullSnapshots2 . length ) . toEqual ( 1 ) ;
178
+ const stringifiedSnapshot2 = normalize ( fullSnapshots2 [ 0 ] ) ;
179
+ expect ( stringifiedSnapshot2 ) . toMatchSnapshot ( 'snapshot-2.json' ) ;
180
+ } ) ;
0 commit comments