|
| 1 | +import { expect } from '@playwright/test'; |
| 2 | + |
| 3 | +import { sentryTest } from '../../../utils/fixtures'; |
| 4 | +import { getExpectedReplayEvent } from '../../../utils/replayEventTemplates'; |
| 5 | +import { |
| 6 | + getFullRecordingSnapshots, |
| 7 | + getIncrementalRecordingSnapshots, |
| 8 | + getReplayEvent, |
| 9 | + getReplaySnapshot, |
| 10 | + normalize, |
| 11 | + shouldSkipReplayTest, |
| 12 | + waitForReplayRequest, |
| 13 | +} from '../../../utils/replayHelpers'; |
| 14 | + |
| 15 | +// Session should expire after 2s - keep in sync with init.js |
| 16 | +const SESSION_TIMEOUT = 2000; |
| 17 | + |
| 18 | +sentryTest('handles an expired session RUN', async ({ getLocalTestPath, page }) => { |
| 19 | + if (shouldSkipReplayTest()) { |
| 20 | + sentryTest.skip(); |
| 21 | + } |
| 22 | + |
| 23 | + const reqPromise0 = waitForReplayRequest(page, 0); |
| 24 | + const reqPromise1 = waitForReplayRequest(page, 1); |
| 25 | + |
| 26 | + await page.route('https://dsn.ingest.sentry.io/**/*', route => { |
| 27 | + return route.fulfill({ |
| 28 | + status: 200, |
| 29 | + contentType: 'application/json', |
| 30 | + body: JSON.stringify({ id: 'test-id' }), |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + const url = await getLocalTestPath({ testDir: __dirname }); |
| 35 | + |
| 36 | + await page.goto(url); |
| 37 | + const req0 = await reqPromise0; |
| 38 | + |
| 39 | + const replayEvent0 = getReplayEvent(req0); |
| 40 | + expect(replayEvent0).toEqual(getExpectedReplayEvent({})); |
| 41 | + |
| 42 | + const fullSnapshots0 = getFullRecordingSnapshots(req0); |
| 43 | + expect(fullSnapshots0.length).toEqual(1); |
| 44 | + const stringifiedSnapshot = normalize(fullSnapshots0[0]); |
| 45 | + expect(stringifiedSnapshot).toMatchSnapshot('snapshot-0.json'); |
| 46 | + |
| 47 | + // We wait for another segment 0 |
| 48 | + const reqPromise2 = waitForReplayRequest(page, 0); |
| 49 | + |
| 50 | + await page.click('#button1'); |
| 51 | + const req1 = await reqPromise1; |
| 52 | + |
| 53 | + const replayEvent1 = getReplayEvent(req1); |
| 54 | + expect(replayEvent1).toEqual(getExpectedReplayEvent({ replay_start_timestamp: undefined, segment_id: 1, urls: [] })); |
| 55 | + |
| 56 | + const fullSnapshots1 = getFullRecordingSnapshots(req1); |
| 57 | + expect(fullSnapshots1.length).toEqual(0); |
| 58 | + |
| 59 | + const incrementalSnapshots1 = getIncrementalRecordingSnapshots(req1); |
| 60 | + // The number of incremental snapshots depends on the browser |
| 61 | + expect(incrementalSnapshots1.length).toBeGreaterThanOrEqual(4); |
| 62 | + |
| 63 | + expect(incrementalSnapshots1).toEqual( |
| 64 | + expect.arrayContaining([ |
| 65 | + { |
| 66 | + source: 1, |
| 67 | + positions: [ |
| 68 | + { |
| 69 | + id: 9, |
| 70 | + timeOffset: expect.any(Number), |
| 71 | + x: expect.any(Number), |
| 72 | + y: expect.any(Number), |
| 73 | + }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + ]), |
| 77 | + ); |
| 78 | + |
| 79 | + const replay = await getReplaySnapshot(page); |
| 80 | + const oldSessionId = replay.session?.id; |
| 81 | + |
| 82 | + await new Promise(resolve => setTimeout(resolve, SESSION_TIMEOUT)); |
| 83 | + |
| 84 | + await page.click('#button2'); |
| 85 | + const req2 = await reqPromise2; |
| 86 | + |
| 87 | + const replay2 = await getReplaySnapshot(page); |
| 88 | + |
| 89 | + expect(replay2.session?.id).not.toEqual(oldSessionId); |
| 90 | + |
| 91 | + const replayEvent2 = getReplayEvent(req2); |
| 92 | + expect(replayEvent2).toEqual(getExpectedReplayEvent({})); |
| 93 | + |
| 94 | + const fullSnapshots2 = getFullRecordingSnapshots(req2); |
| 95 | + expect(fullSnapshots2.length).toEqual(1); |
| 96 | + const stringifiedSnapshot2 = normalize(fullSnapshots2[0]); |
| 97 | + expect(stringifiedSnapshot2).toMatchSnapshot('snapshot-2.json'); |
| 98 | +}); |
0 commit comments