|
| 1 | +import { BASE_TIMESTAMP } from '..'; |
| 2 | +import { resetSdkMock } from '../mocks/resetSdkMock'; |
| 3 | +import { useFakeTimers } from '../utils/use-fake-timers'; |
| 4 | + |
| 5 | +useFakeTimers(); |
| 6 | + |
| 7 | +describe('Integration | early events', () => { |
| 8 | + beforeEach(() => { |
| 9 | + vi.clearAllMocks(); |
| 10 | + }); |
| 11 | + |
| 12 | + it('updates initialTimestamp for early performance entries', async () => { |
| 13 | + const earlyTimeStampSeconds = BASE_TIMESTAMP / 1000 - 10; |
| 14 | + |
| 15 | + const { replay } = await resetSdkMock({ |
| 16 | + replayOptions: { |
| 17 | + stickySession: true, |
| 18 | + }, |
| 19 | + sentryOptions: { |
| 20 | + replaysSessionSampleRate: 0, |
| 21 | + replaysOnErrorSampleRate: 1.0, |
| 22 | + }, |
| 23 | + }); |
| 24 | + |
| 25 | + expect(replay.session).toBeDefined(); |
| 26 | + expect(replay['_requiresManualStart']).toBe(false); |
| 27 | + |
| 28 | + const initialTimestamp = replay.getContext().initialTimestamp; |
| 29 | + |
| 30 | + expect(initialTimestamp).not.toEqual(earlyTimeStampSeconds * 1000); |
| 31 | + |
| 32 | + // A performance entry that happend before should not extend the session when we manually started |
| 33 | + replay.replayPerformanceEntries.push({ |
| 34 | + type: 'largest-contentful-paint', |
| 35 | + name: 'largest-contentful-paint', |
| 36 | + start: earlyTimeStampSeconds, |
| 37 | + end: earlyTimeStampSeconds, |
| 38 | + data: { |
| 39 | + value: 100, |
| 40 | + size: 100, |
| 41 | + nodeId: undefined, |
| 42 | + }, |
| 43 | + }); |
| 44 | + |
| 45 | + // _too_ early events are always thrown away |
| 46 | + replay.replayPerformanceEntries.push({ |
| 47 | + type: 'largest-contentful-paint', |
| 48 | + name: 'largest-contentful-paint', |
| 49 | + start: earlyTimeStampSeconds - 999999, |
| 50 | + end: earlyTimeStampSeconds - 99999, |
| 51 | + data: { |
| 52 | + value: 100, |
| 53 | + size: 100, |
| 54 | + nodeId: undefined, |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + await replay.flushImmediate(); |
| 59 | + vi.runAllTimers(); |
| 60 | + |
| 61 | + expect(replay.getContext().initialTimestamp).toEqual(earlyTimeStampSeconds * 1000); |
| 62 | + }); |
| 63 | + |
| 64 | + it('does not change initialTimestamp when replay is manually started', async () => { |
| 65 | + const earlyTimeStampSeconds = Date.now() / 1000 - 5; |
| 66 | + |
| 67 | + const { replay } = await resetSdkMock({ |
| 68 | + replayOptions: { |
| 69 | + stickySession: true, |
| 70 | + }, |
| 71 | + sentryOptions: { |
| 72 | + replaysSessionSampleRate: 0.0, |
| 73 | + replaysOnErrorSampleRate: 0.0, |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + expect(replay.session).toBe(undefined); |
| 78 | + expect(replay['_requiresManualStart']).toBe(true); |
| 79 | + |
| 80 | + replay.start(); |
| 81 | + vi.runAllTimers(); |
| 82 | + |
| 83 | + const initialTimestamp = replay.getContext().initialTimestamp; |
| 84 | + |
| 85 | + expect(initialTimestamp).not.toEqual(earlyTimeStampSeconds * 1000); |
| 86 | + expect(replay.session).toBeDefined(); |
| 87 | + expect(replay['_requiresManualStart']).toBe(true); |
| 88 | + |
| 89 | + // A performance entry that happened before should not extend the session when we manually started |
| 90 | + replay.replayPerformanceEntries.push({ |
| 91 | + type: 'largest-contentful-paint', |
| 92 | + name: 'largest-contentful-paint', |
| 93 | + start: earlyTimeStampSeconds, |
| 94 | + end: earlyTimeStampSeconds, |
| 95 | + data: { |
| 96 | + value: 100, |
| 97 | + size: 100, |
| 98 | + nodeId: undefined, |
| 99 | + }, |
| 100 | + }); |
| 101 | + |
| 102 | + await replay.flushImmediate(); |
| 103 | + vi.runAllTimers(); |
| 104 | + |
| 105 | + expect(replay.getContext().initialTimestamp).toEqual(initialTimestamp); |
| 106 | + }); |
| 107 | +}); |
0 commit comments