|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 6 | + |
| 7 | +import { WINDOW } from '../../src/constants'; |
| 8 | +import type { Replay } from '../../src/integration'; |
| 9 | +import type { ReplayContainer } from '../../src/replay'; |
| 10 | +import { addEvent } from '../../src/util/addEvent'; |
| 11 | + |
| 12 | +// mock functions need to be imported first |
| 13 | +import { BASE_TIMESTAMP, mockSdk } from '../index'; |
| 14 | +import { getTestEventCheckout, getTestEventIncremental } from '../utils/getTestEvent'; |
| 15 | +import { useFakeTimers } from '../utils/use-fake-timers'; |
| 16 | + |
| 17 | +useFakeTimers(); |
| 18 | + |
| 19 | +describe('Integration | eventBuffer | Event Buffer Max Size', () => { |
| 20 | + let replay: ReplayContainer; |
| 21 | + let integration: Replay; |
| 22 | + const prevLocation = WINDOW.location; |
| 23 | + |
| 24 | + beforeEach(async () => { |
| 25 | + vi.setSystemTime(new Date(BASE_TIMESTAMP)); |
| 26 | + |
| 27 | + ({ replay, integration } = await mockSdk()); |
| 28 | + |
| 29 | + await vi.runAllTimersAsync(); |
| 30 | + vi.clearAllMocks(); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(async () => { |
| 34 | + vi.setSystemTime(new Date(BASE_TIMESTAMP)); |
| 35 | + integration && (await integration.stop()); |
| 36 | + Object.defineProperty(WINDOW, 'location', { |
| 37 | + value: prevLocation, |
| 38 | + writable: true, |
| 39 | + }); |
| 40 | + vi.clearAllMocks(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('does not add replay breadcrumb when stopped due to event buffer limit', async () => { |
| 44 | + const TEST_EVENT = getTestEventIncremental({ timestamp: BASE_TIMESTAMP }); |
| 45 | + |
| 46 | + vi.mock('../../src/constants', async requireActual => ({ |
| 47 | + ...(await requireActual<any>()), |
| 48 | + REPLAY_MAX_EVENT_BUFFER_SIZE: 500, |
| 49 | + })); |
| 50 | + |
| 51 | + await integration.stop(); |
| 52 | + integration.startBuffering(); |
| 53 | + |
| 54 | + await addEvent(replay, TEST_EVENT); |
| 55 | + |
| 56 | + expect(replay.eventBuffer?.hasEvents).toBe(true); |
| 57 | + expect(replay.eventBuffer?.['hasCheckout']).toBe(true); |
| 58 | + |
| 59 | + // This should should go over max buffer size |
| 60 | + await addEvent(replay, TEST_EVENT); |
| 61 | + // buffer should be cleared and wait for next checkout |
| 62 | + expect(replay.eventBuffer?.hasEvents).toBe(false); |
| 63 | + expect(replay.eventBuffer?.['hasCheckout']).toBe(false); |
| 64 | + |
| 65 | + await addEvent(replay, TEST_EVENT); |
| 66 | + expect(replay.eventBuffer?.hasEvents).toBe(false); |
| 67 | + expect(replay.eventBuffer?.['hasCheckout']).toBe(false); |
| 68 | + |
| 69 | + await addEvent(replay, getTestEventCheckout({ timestamp: Date.now() }), true); |
| 70 | + expect(replay.eventBuffer?.hasEvents).toBe(true); |
| 71 | + expect(replay.eventBuffer?.['hasCheckout']).toBe(true); |
| 72 | + |
| 73 | + vi.resetAllMocks(); |
| 74 | + }); |
| 75 | +}); |
0 commit comments