|
| 1 | +import { PostHog } from '../../posthog-core' |
| 2 | +import { assignableWindow, document } from '../../utils/globals' |
| 3 | +import { Autocapture } from '../../extensions/autocapture' |
| 4 | +import { AUTOCAPTURE_DISABLED_SERVER_SIDE } from '../../constants' |
| 5 | + |
| 6 | +describe('Autocapture wrapper', () => { |
| 7 | + let mockCaptureEvent: jest.Mock |
| 8 | + let loadCallback: ((err?: Error) => void) | null = null |
| 9 | + |
| 10 | + const createMockInstance = (overrides: Partial<PostHog> = {}): PostHog => { |
| 11 | + return { |
| 12 | + config: { |
| 13 | + autocapture: true, |
| 14 | + ...overrides.config, |
| 15 | + }, |
| 16 | + persistence: { |
| 17 | + props: { |
| 18 | + [AUTOCAPTURE_DISABLED_SERVER_SIDE]: false, |
| 19 | + }, |
| 20 | + register: jest.fn(), |
| 21 | + }, |
| 22 | + _shouldDisableFlags: () => false, |
| 23 | + capture: jest.fn(), |
| 24 | + ...overrides, |
| 25 | + } as unknown as PostHog |
| 26 | + } |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + mockCaptureEvent = jest.fn() |
| 30 | + loadCallback = null |
| 31 | + |
| 32 | + assignableWindow.__PosthogExtensions__ = assignableWindow.__PosthogExtensions__ || {} |
| 33 | + // Don't set initAutocapture initially - simulate script not loaded yet |
| 34 | + assignableWindow.__PosthogExtensions__.initAutocapture = undefined |
| 35 | + |
| 36 | + // Mock loadExternalDependency to capture the callback instead of calling it immediately |
| 37 | + assignableWindow.__PosthogExtensions__.loadExternalDependency = jest |
| 38 | + .fn() |
| 39 | + .mockImplementation((_ph: PostHog, _name: string, cb: (err?: Error) => void) => { |
| 40 | + loadCallback = cb |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + const completeScriptLoad = () => { |
| 45 | + // Simulate the script loading and registering initAutocapture |
| 46 | + assignableWindow.__PosthogExtensions__.initAutocapture = jest.fn().mockReturnValue({ |
| 47 | + _captureEvent: mockCaptureEvent, |
| 48 | + setElementSelectors: jest.fn(), |
| 49 | + getElementSelectors: jest.fn(), |
| 50 | + }) |
| 51 | + loadCallback?.() |
| 52 | + } |
| 53 | + |
| 54 | + afterEach(() => { |
| 55 | + document.getElementsByTagName('html')[0].innerHTML = '' |
| 56 | + }) |
| 57 | + |
| 58 | + describe('event queuing', () => { |
| 59 | + it('queues events before lazy load completes', () => { |
| 60 | + const instance = createMockInstance() |
| 61 | + const autocapture = new Autocapture(instance) |
| 62 | + |
| 63 | + // Simulate server enabling autocapture |
| 64 | + autocapture.onRemoteConfig({ autocapture_opt_out: false } as any) |
| 65 | + |
| 66 | + // Access the private queue for testing |
| 67 | + expect((autocapture as any)._eventQueue).toHaveLength(0) |
| 68 | + |
| 69 | + // Simulate click events before lazy load completes |
| 70 | + const button = document.createElement('button') |
| 71 | + document.body.appendChild(button) |
| 72 | + |
| 73 | + const clickEvent = new MouseEvent('click', { bubbles: true }) |
| 74 | + ;(autocapture as any)._captureEvent(clickEvent) |
| 75 | + |
| 76 | + // Events should be queued |
| 77 | + expect((autocapture as any)._eventQueue).toHaveLength(1) |
| 78 | + expect((autocapture as any)._eventQueue[0].event).toBe(clickEvent) |
| 79 | + |
| 80 | + // Lazy-loaded implementation should not have been called yet |
| 81 | + expect(mockCaptureEvent).not.toHaveBeenCalled() |
| 82 | + }) |
| 83 | + |
| 84 | + it('processes queued events when lazy load completes', () => { |
| 85 | + const instance = createMockInstance() |
| 86 | + const autocapture = new Autocapture(instance) |
| 87 | + |
| 88 | + autocapture.onRemoteConfig({ autocapture_opt_out: false } as any) |
| 89 | + |
| 90 | + // Queue some events |
| 91 | + const event1 = new MouseEvent('click', { bubbles: true }) |
| 92 | + const event2 = new MouseEvent('click', { bubbles: true }) |
| 93 | + ;(autocapture as any)._captureEvent(event1) |
| 94 | + ;(autocapture as any)._captureEvent(event2) |
| 95 | + |
| 96 | + expect((autocapture as any)._eventQueue).toHaveLength(2) |
| 97 | + |
| 98 | + // Complete the lazy load |
| 99 | + completeScriptLoad() |
| 100 | + |
| 101 | + // Queue should be empty now |
| 102 | + expect((autocapture as any)._eventQueue).toHaveLength(0) |
| 103 | + |
| 104 | + // Events should have been processed with correct timestamps |
| 105 | + expect(mockCaptureEvent).toHaveBeenCalledTimes(2) |
| 106 | + expect(mockCaptureEvent).toHaveBeenNthCalledWith(1, event1, undefined, expect.any(Date)) |
| 107 | + expect(mockCaptureEvent).toHaveBeenNthCalledWith(2, event2, undefined, expect.any(Date)) |
| 108 | + }) |
| 109 | + |
| 110 | + it('limits queue size to prevent unbounded growth', () => { |
| 111 | + const instance = createMockInstance() |
| 112 | + const autocapture = new Autocapture(instance) |
| 113 | + |
| 114 | + autocapture.onRemoteConfig({ autocapture_opt_out: false } as any) |
| 115 | + |
| 116 | + // Try to queue more than MAX_QUEUED_EVENTS (1000) |
| 117 | + for (let i = 0; i < 1100; i++) { |
| 118 | + const event = new MouseEvent('click', { bubbles: true }) |
| 119 | + ;(autocapture as any)._captureEvent(event) |
| 120 | + } |
| 121 | + |
| 122 | + // Queue should be capped at 1000 |
| 123 | + expect((autocapture as any)._eventQueue).toHaveLength(1000) |
| 124 | + }) |
| 125 | + |
| 126 | + it('passes events directly to lazy implementation after load', () => { |
| 127 | + const instance = createMockInstance() |
| 128 | + const autocapture = new Autocapture(instance) |
| 129 | + |
| 130 | + autocapture.onRemoteConfig({ autocapture_opt_out: false } as any) |
| 131 | + |
| 132 | + // Complete lazy load first |
| 133 | + completeScriptLoad() |
| 134 | + |
| 135 | + // Now capture an event |
| 136 | + const event = new MouseEvent('click', { bubbles: true }) |
| 137 | + ;(autocapture as any)._captureEvent(event) |
| 138 | + |
| 139 | + // Should go directly to lazy implementation, not queue |
| 140 | + expect((autocapture as any)._eventQueue).toHaveLength(0) |
| 141 | + expect(mockCaptureEvent).toHaveBeenCalledWith(event, undefined) |
| 142 | + }) |
| 143 | + |
| 144 | + it('does not queue events if autocapture is disabled', () => { |
| 145 | + const instance = createMockInstance({ |
| 146 | + config: { autocapture: false } as any, |
| 147 | + }) |
| 148 | + const autocapture = new Autocapture(instance) |
| 149 | + |
| 150 | + const event = new MouseEvent('click', { bubbles: true }) |
| 151 | + ;(autocapture as any)._captureEvent(event) |
| 152 | + |
| 153 | + expect((autocapture as any)._eventQueue).toHaveLength(0) |
| 154 | + expect(mockCaptureEvent).not.toHaveBeenCalled() |
| 155 | + }) |
| 156 | + }) |
| 157 | +}) |
0 commit comments