Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

56.2 KB
Raw Blame
import { DateTime } from 'luxon'
import { brotliDecompressSync } from 'node:zlib'
import snappy from 'snappy'

import { parseJSON } from '~/common/utils/json-parse'
import { ParsedMessageData } from '~/ingestion/pipelines/sessionreplay/kafka/types'
import { RRWebEventType } from '~/ingestion/pipelines/sessionreplay/rrweb-types'

import { compressBlock } from './block-compression'
import { SessionBatchMetrics } from './metrics'
import { SessionBlockRecorder } from './session-block-recorder'

jest.mock('./block-compression', () => {
    const actual = jest.requireActual('./block-compression')
    return { ...actual, compressBlock: jest.fn(actual.compressBlock) }
})

describe('SessionBlockRecorder', () => {
    let recorder: SessionBlockRecorder

    beforeEach(() => {
        recorder = new SessionBlockRecorder('test_session_id', 1, 'test_batch_id')
    })

    const createMessage = (windowId: string, events: any[], capturedAtMs = 0): ParsedMessageData => ({
        distinct_id: 'distinct_id',
        session_id: 'session_id',
        token: null,
        eventsByWindowId: {
            [windowId]: events,
        },
        eventsRange: {
            start: DateTime.fromMillis(events[0]?.timestamp || 0),
            end: DateTime.fromMillis(events[events.length - 1]?.timestamp || 0),
        },
        snapshot_source: null,
        snapshot_library: null,
        metadata: {
            partition: 1,
            topic: 'test',
            offset: 0,
            timestamp: capturedAtMs,
            rawSize: 0,
        },
    })

    const readSnappyBuffer = async (buffer: Buffer): Promise<string> => {
        const decompressed = await snappy.uncompress(buffer)
        return decompressed.toString()
    }

    const parseSnappyBuffer = async (buffer: Buffer): Promise<any[]> => {
        const decompressed = await snappy.uncompress(buffer)
        return decompressed
            .toString()
            .trim()
            .split('\n')
            .map((line) => line.trim())
            .filter(Boolean)
            .map((line) => parseJSON(line))
    }

    describe('compression', () => {
        it('packs with the declared codec at the declared level, and reports it', async () => {
            const recorder = new SessionBlockRecorder('test_session_id', 1, 'test_batch_id', {
                codec: 'brotli',
                level: 5,
            })
            recorder.recordMessage(createMessage('window1', [{ type: 3, timestamp: 1000, data: {} }]))

            const result = await recorder.end()

            expect(brotliDecompressSync(result.buffer).toString()).toContain('"timestamp":1000')
        })

        it('packs with snappy when a lane declares nothing', async () => {
            const recorder = new SessionBlockRecorder('test_session_id', 1, 'test_batch_id')
            recorder.recordMessage(createMessage('window1', [{ type: 3, timestamp: 1000, data: {} }]))

            const result = await recorder.end()

            expect(snappy.uncompressSync(result.buffer, { asBuffer: true }).toString()).toContain('"timestamp":1000')
        })

        it('stays callable after packing fails, so a retried flush can still write the block', async () => {
            jest.mocked(compressBlock).mockRejectedValueOnce(new Error('out of memory'))
            const recorder = new SessionBlockRecorder('test_session_id', 1, 'test_batch_id')
            recorder.recordMessage(createMessage('window1', [{ type: 3, timestamp: 1000, data: {} }]))

            await expect(recorder.end()).rejects.toThrow('out of memory')

            const result = await recorder.end()

            expect(jest.mocked(compressBlock)).toHaveBeenCalledTimes(2)
            expect(result.eventCount).toBe(1)
            expect((await recorder.end()).buffer).toEqual(result.buffer)
        })

        it('refuses a message once a block is being built, so no event is counted but left out', async () => {
            let release!: () => void
            jest.mocked(compressBlock).mockImplementationOnce(
                (data: Buffer) =>
                    new Promise<Buffer>((resolve) => {
                        release = () => resolve(data)
                    })
            )
            const recorder = new SessionBlockRecorder('test_session_id', 1, 'test_batch_id')
            const message = createMessage('window1', [{ type: 3, timestamp: 1000, data: {} }])
            recorder.recordMessage(message)
            const building = recorder.end()

            expect(() => recorder.recordMessage(message)).toThrow('Cannot record message after end() has been called')

            release()
            expect((await building).eventCount).toBe(1)
        })

        it('packs once when two callers end the same block', async () => {
            const recorder = new SessionBlockRecorder('test_session_id', 1, 'test_batch_id')
            recorder.recordMessage(createMessage('window1', [{ type: 3, timestamp: 1000, data: {} }]))

            const [a, b] = await Promise.all([recorder.end(), recorder.end()])

            expect(jest.mocked(compressBlock)).toHaveBeenCalledTimes(1)
            expect(a.buffer).toEqual(b.buffer)
        })

        it('reports the codec and both byte counts, so the saving is measurable', async () => {
            const observe = jest.spyOn(SessionBatchMetrics, 'observeBlockCompression').mockImplementation()
            const recorder = new SessionBlockRecorder('test_session_id', 1, 'test_batch_id', {
                codec: 'brotli',
                level: 9,
            })
            recorder.recordMessage(createMessage('window1', [{ type: 3, timestamp: 1000, data: {} }]))

            const result = await recorder.end()

            expect(observe).toHaveBeenCalledTimes(1)
            const [codec, rawBytes, packedBytes] = observe.mock.calls[0]
            expect(codec).toBe('brotli')
            expect(packedBytes).toBe(result.buffer.length)
            expect(rawBytes).toBeGreaterThan(packedBytes)
            observe.mockRestore()
        })
    })

    describe('recordMessage', () => {
        it('should record events in snappy-compressed JSONL format', async () => {
            const events = [
                {
                    type: RRWebEventType.FullSnapshot,
                    timestamp: new Date('2025-01-01T01:00:00Z').getTime(),
                    data: {
                        source: 1,
                        adds: [{ parentId: 1, nextId: 2, node: { tag: 'div', attrs: { class: 'test' } } }],
                    },
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: new Date('2025-01-01T01:00:01Z').getTime(),
                    data: { source: 2, texts: [{ id: 1, value: 'Updated text' }] },
                },
            ]
            const message = createMessage('window1', events)

            const rawBytesWritten = recorder.recordMessage(message)
            expect(rawBytesWritten).toBeGreaterThan(0)

            const { buffer, eventCount } = await recorder.end()
            const lines = await parseSnappyBuffer(buffer)

            expect(lines).toEqual([
                ['window1', events[0]],
                ['window1', events[1]],
            ])
            expect(eventCount).toBe(2)
        })

        it('should handle multiple windows with multiple events', async () => {
            const events = {
                window1: [
                    {
                        type: RRWebEventType.Meta,
                        timestamp: new Date('2025-01-01T01:00:00Z').getTime(),
                        data: { href: 'https://example.com', width: 1024, height: 768 },
                    },
                    {
                        type: RRWebEventType.FullSnapshot,
                        timestamp: new Date('2025-01-01T01:00:01Z').getTime(),
                        data: {
                            source: 1,
                            adds: [{ parentId: 1, nextId: null, node: { tag: 'h1', attrs: { id: 'title' } } }],
                        },
                    },
                ],
                window2: [
                    {
                        type: RRWebEventType.Custom,
                        timestamp: new Date('2025-01-01T01:00:02Z').getTime(),
                        data: { tag: 'user-interaction', payload: { type: 'click', target: '#submit-btn' } },
                    },
                    {
                        type: RRWebEventType.IncrementalSnapshot,
                        timestamp: new Date('2025-01-01T01:00:03Z').getTime(),
                        data: { source: 3, mousemove: [{ x: 100, y: 200, id: 1 }] },
                    },
                ],
            }
            const message: ParsedMessageData = {
                ...createMessage('', []),
                eventsByWindowId: events,
            }

            recorder.recordMessage(message)
            const { buffer, eventCount } = await recorder.end()
            const lines = await parseSnappyBuffer(buffer)

            expect(lines).toEqual([
                ['window1', events.window1[0]],
                ['window1', events.window1[1]],
                ['window2', events.window2[0]],
                ['window2', events.window2[1]],
            ])
            expect(eventCount).toBe(4)
        })

        it('should handle empty events array', async () => {
            const message = createMessage('window1', [])
            recorder.recordMessage(message)

            const { buffer, eventCount } = await recorder.end()
            const lines = await parseSnappyBuffer(buffer)

            expect(lines).toEqual([])
            expect(eventCount).toBe(0)
        })

        it('should handle large amounts of data', async () => {
            const events = Array.from({ length: 10000 }, (_, i) => ({
                type: RRWebEventType.Custom,
                timestamp: new Date('2025-01-01T01:00:00Z').getTime() + i * 100,
                data: { value: 'x'.repeat(1000) },
            }))

            // Split events into 100 messages of 100 events each
            for (let i = 0; i < events.length; i += 100) {
                const messageEvents = events.slice(i, i + 100)
                const message = createMessage('window1', messageEvents)
                recorder.recordMessage(message)
            }

            const { buffer, eventCount } = await recorder.end()
            const lines = await parseSnappyBuffer(buffer)

            expect(lines.length).toBe(10000)
            expect(eventCount).toBe(10000)

            // Verify first and last events
            expect(lines[0]).toEqual(['window1', events[0]])
            expect(lines[lines.length - 1]).toEqual(['window1', events[events.length - 1]])
        })

        it('should throw error when recording after end', async () => {
            const message = createMessage('window1', [
                { type: RRWebEventType.Custom, timestamp: new Date('2025-01-01T01:00:00Z').getTime(), data: {} },
            ])
            recorder.recordMessage(message)
            await recorder.end()

            expect(() => recorder.recordMessage(message)).toThrow('Cannot record message after end() has been called')
        })

        it('returns the same block when end is called again, so a retried flush re-emits it', async () => {
            const message = createMessage('window1', [
                { type: RRWebEventType.Custom, timestamp: new Date('2025-01-01T01:00:00Z').getTime(), data: {} },
            ])
            recorder.recordMessage(message)
            const first = await recorder.end()

            const second = await recorder.end()

            expect(second.buffer).toEqual(first.buffer)
            expect(second.eventCount).toBe(first.eventCount)
        })
    })

    describe('timestamps', () => {
        it('should track start and end timestamps from events range', async () => {
            const events = [
                {
                    type: RRWebEventType.FullSnapshot,
                    timestamp: new Date('2025-01-01T01:00:00Z').getTime(),
                    data: { source: 1 },
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: new Date('2025-01-01T01:00:01Z').getTime(),
                    data: { source: 2 },
                },
            ]
            const message = createMessage('window1', events)

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.startDateTime).toEqual(DateTime.fromMillis(new Date('2025-01-01T01:00:00Z').getTime()))
            expect(result.endDateTime).toEqual(DateTime.fromMillis(new Date('2025-01-01T01:00:01Z').getTime()))
        })

        it('should track min/max timestamps across multiple messages', async () => {
            const messages = [
                createMessage(
                    'window1',
                    [
                        { type: RRWebEventType.Meta, timestamp: new Date('2025-01-01T01:00:00Z').getTime() },
                        { type: RRWebEventType.FullSnapshot, timestamp: new Date('2025-01-01T01:00:01Z').getTime() },
                    ],
                    1_700_000_002_000
                ),
                createMessage(
                    'window2',
                    [
                        { type: RRWebEventType.FullSnapshot, timestamp: new Date('2025-01-01T01:00:02Z').getTime() },
                        {
                            type: RRWebEventType.IncrementalSnapshot,
                            timestamp: new Date('2025-01-01T01:00:03Z').getTime(),
                        },
                    ],
                    1_700_000_001_000
                ),
            ]

            messages.forEach((message) => recorder.recordMessage(message))
            const result = await recorder.end()

            expect(result.startDateTime).toEqual(DateTime.fromMillis(new Date('2025-01-01T01:00:00Z').getTime())) // Min from all messages
            expect(result.endDateTime).toEqual(DateTime.fromMillis(new Date('2025-01-01T01:00:03Z').getTime())) // Max from all messages
            expect(result.earliestCapturedAtMs).toBe(1_700_000_001_000)
        })

        it('should handle empty events array', async () => {
            const message = createMessage('window1', [])
            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.startDateTime).toEqual(DateTime.fromMillis(new Date('1970-01-01T00:00:00Z').getTime()))
            expect(result.endDateTime).toEqual(DateTime.fromMillis(new Date('1970-01-01T00:00:00Z').getTime()))
        })
    })

    describe('metadata', () => {
        it('should return empty metadata', async () => {
            const result = await recorder.end()

            expect(result.firstUrl).toBeNull()
            expect(result.urls).toEqual([])
            expect(result.clickCount).toBe(0)
            expect(result.keypressCount).toBe(0)
            expect(result.mouseActivityCount).toBe(0)
            expect(result.activeMilliseconds).toBe(0)
            expect(result.size).toBe(0)
            expect(result.messageCount).toBe(0)
            expect(result.snapshotSource).toBeNull()
            expect(result.snapshotLibrary).toBeNull()
        })
    })

    describe('distinctId', () => {
        it('should throw error when accessing distinctId before recording any messages', () => {
            expect(() => recorder.distinctId).toThrow('No distinct_id set. No messages recorded yet.')
        })

        it('should store distinctId from first message', () => {
            const message = createMessage('window1', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: new Date('2025-01-01T01:00:00Z').getTime(),
                    data: {},
                },
            ])
            recorder.recordMessage(message)

            expect(recorder.distinctId).toBe('distinct_id')
        })

        it('should keep first message distinctId even if later messages have different distinctId', () => {
            recorder.recordMessage(
                createMessage('window1', [
                    {
                        type: RRWebEventType.Meta,
                        timestamp: new Date('2025-01-01T01:00:00Z').getTime(),
                        data: {},
                    },
                ])
            )

            const message2 = {
                ...createMessage('window1', [
                    {
                        type: RRWebEventType.Meta,
                        timestamp: new Date('2025-01-01T01:00:01Z').getTime(),
                        data: {},
                    },
                ]),
                distinct_id: 'different_distinct_id',
            }
            recorder.recordMessage(message2)

            expect(recorder.distinctId).toBe('distinct_id')
        })

        it('should maintain distinctId after end() is called', async () => {
            const message = createMessage('window1', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: new Date('2025-01-01T01:00:00Z').getTime(),
                    data: {},
                },
            ])
            recorder.recordMessage(message)
            await recorder.end()

            expect(recorder.distinctId).toBe('distinct_id')
        })
    })

    describe('URL accumulation', () => {
        it('should accumulate URLs from a single message', async () => {
            const events = [
                {
                    type: RRWebEventType.Meta,
                    timestamp: new Date('2025-01-01T01:00:00Z').getTime(),
                    data: { href: 'https://example.com' },
                },
            ]
            const message = createMessage('window1', events)

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.firstUrl).toBe('https://example.com')
            expect(result.urls).toEqual(['https://example.com'])
        })

        it('should limit URL length to 4KB', async () => {
            // Create a URL that exceeds the 4KB limit
            const longUrl = 'https://example.com/' + 'a'.repeat(5000)

            const events = [
                {
                    type: RRWebEventType.Meta,
                    timestamp: new Date('2025-01-01T01:00:00Z').getTime(),
                    data: { href: longUrl },
                },
            ]
            const message = createMessage('window1', events)

            recorder.recordMessage(message)
            const result = await recorder.end()

            // URL should be truncated to 4KB (4096 characters)
            expect(result.firstUrl?.length).toBe(4096)
            expect(result.urls?.[0].length).toBe(4096)
        })

        it('should limit the number of URLs to 25', async () => {
            // Create 30 different URLs
            const events = Array.from({ length: 30 }, (_, i) => ({
                type: RRWebEventType.Meta,
                timestamp: new Date('2025-01-01T01:00:00Z').getTime() + i * 100,
                data: { href: `https://example${i}.com` },
            }))

            const message = createMessage('window1', events)

            recorder.recordMessage(message)
            const result = await recorder.end()

            // Only the first 25 URLs should be stored
            expect(result.urls?.length).toBe(25)
            // First URL should be the first one in the events array
            expect(result.firstUrl).toBe('https://example0.com')
        })

        it('should accumulate URLs from multiple messages', async () => {
            const message1 = createMessage('window1', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: new Date('2025-01-01T01:00:00Z').getTime(),
                    data: { href: 'https://example1.com' },
                },
            ])
            const message2 = createMessage('window2', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: new Date('2025-01-01T01:00:01Z').getTime(),
                    data: { href: 'https://example2.com' },
                },
            ])

            recorder.recordMessage(message1)
            recorder.recordMessage(message2)
            const result = await recorder.end()

            expect(result.firstUrl).toBe('https://example1.com')
            expect(result.urls).toEqual(['https://example1.com', 'https://example2.com'])
        })

        it('should not overwrite first URL with subsequent messages', async () => {
            const message1 = createMessage('window1', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: new Date('2025-01-01T01:00:00Z').getTime(),
                    data: { href: 'https://first-url.com' },
                },
            ])
            const message2 = createMessage('window2', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: new Date('2025-01-01T01:00:01Z').getTime(),
                    data: { href: 'https://second-url.com' },
                },
            ])

            recorder.recordMessage(message1)
            recorder.recordMessage(message2)
            const result = await recorder.end()

            expect(result.firstUrl).toBe('https://first-url.com')
            expect(result.urls).toEqual(['https://first-url.com', 'https://second-url.com'])
        })

        it('should handle a message without URLs followed by one with URLs', async () => {
            const message1 = createMessage('window1', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: new Date('2025-01-01T01:00:00Z').getTime(),
                    data: {},
                },
            ])
            const message2 = createMessage('window2', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: new Date('2025-01-01T01:00:01Z').getTime(),
                    data: { href: 'https://example.com' },
                },
            ])

            recorder.recordMessage(message1)
            recorder.recordMessage(message2)
            const result = await recorder.end()

            expect(result.firstUrl).toBe('https://example.com')
            expect(result.urls).toEqual(['https://example.com'])
        })

        it('should handle messages with no URLs at all', async () => {
            const message = createMessage('window1', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: new Date('2025-01-01T01:00:00Z').getTime(),
                    data: {},
                },
            ])

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.firstUrl).toBeNull()
            expect(result.urls).toEqual([])
        })

        it('should accumulate URLs from multiple events within a single message', async () => {
            const events = [
                {
                    type: RRWebEventType.Meta,
                    timestamp: new Date('2025-01-01T01:00:00Z').getTime(),
                    data: { href: 'https://example1.com' },
                },
                {
                    type: RRWebEventType.Meta,
                    timestamp: new Date('2025-01-01T01:00:01Z').getTime(),
                    data: { href: 'https://example2.com' },
                },
                {
                    type: RRWebEventType.Meta,
                    timestamp: new Date('2025-01-01T01:00:02Z').getTime(),
                    data: { href: 'https://example3.com' },
                },
            ]
            const message = createMessage('window1', events)

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.firstUrl).toBe('https://example1.com')
            expect(result.urls).toEqual(['https://example1.com', 'https://example2.com', 'https://example3.com'])
        })
    })

    describe('Click counting', () => {
        it('should count single click events', async () => {
            const events = [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: new Date('2025-01-01T01:00:00Z').getTime(),
                    data: { source: 2, type: 2 }, // MouseInteraction, Click
                },
            ]
            const message = createMessage('window1', events)

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.clickCount).toBe(1)
        })

        it('should count multiple click events in a single message', async () => {
            const events = [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: new Date('2025-01-01T01:00:00Z').getTime(),
                    data: { source: 2, type: 2 }, // MouseInteraction, Click
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:01Z').toMillis(),
                    data: { source: 2, type: 4 }, // MouseInteraction, DblClick
                },
            ]
            const message = createMessage('window1', events)

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.clickCount).toBe(2)
        })

        it('should count click events across multiple messages', async () => {
            const message1 = createMessage('window1', [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: { source: 2, type: 2 }, // MouseInteraction, Click
                },
            ])
            const message2 = createMessage('window2', [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:01Z').toMillis(),
                    data: { source: 2, type: 3 }, // MouseInteraction, ContextMenu
                },
            ])

            recorder.recordMessage(message1)
            recorder.recordMessage(message2)
            const result = await recorder.end()

            expect(result.clickCount).toBe(2)
        })

        it('should not count non-click events', async () => {
            const events = [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: { source: 2, type: 0 }, // MouseInteraction, MouseUp
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:01Z').toMillis(),
                    data: { source: 2, type: 1 }, // MouseInteraction, MouseDown
                },
            ]
            const message = createMessage('window1', events)

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.clickCount).toBe(0)
        })

        it('should handle mixed click and non-click events', async () => {
            const events = [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: { source: 2, type: 2 }, // MouseInteraction, Click
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:01Z').toMillis(),
                    data: { source: 2, type: 0 }, // MouseInteraction, MouseUp
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:02Z').toMillis(),
                    data: { source: 2, type: 4 }, // MouseInteraction, DblClick
                },
            ]
            const message = createMessage('window1', events)

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.clickCount).toBe(2)
        })
    })

    describe('Keypress counting', () => {
        it('should count single keypress events', async () => {
            const events = [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: { source: 5 }, // Input
                },
            ]
            const message = createMessage('window1', events)

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.keypressCount).toBe(1)
        })

        it('should count multiple keypress events in a single message', async () => {
            const events = [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: { source: 5 }, // Input
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:01Z').toMillis(),
                    data: { source: 5 }, // Input
                },
            ]
            const message = createMessage('window1', events)

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.keypressCount).toBe(2)
        })

        it('should count keypress events across multiple messages', async () => {
            const message1 = createMessage('window1', [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: { source: 5 }, // Input
                },
            ])
            const message2 = createMessage('window2', [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:01Z').toMillis(),
                    data: { source: 5 }, // Input
                },
            ])

            recorder.recordMessage(message1)
            recorder.recordMessage(message2)
            const result = await recorder.end()

            expect(result.keypressCount).toBe(2)
        })

        it('should not count non-keypress events', async () => {
            const events = [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: { source: 2 }, // MouseInteraction
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:01Z').toMillis(),
                    data: { source: 3 }, // Scroll
                },
            ]
            const message = createMessage('window1', events)

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.keypressCount).toBe(0)
        })

        it('should handle mixed keypress and non-keypress events', async () => {
            const events = [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: { source: 5 }, // Input
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:01Z').toMillis(),
                    data: { source: 2 }, // MouseInteraction
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:02Z').toMillis(),
                    data: { source: 5 }, // Input
                },
            ]
            const message = createMessage('window1', events)

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.keypressCount).toBe(2)
        })
    })

    describe('Mouse activity counting', () => {
        it('should count single mouse activity events', async () => {
            const events = [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: { source: 1 }, // MouseMove
                },
            ]
            const message = createMessage('window1', events)

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.mouseActivityCount).toBe(1)
        })

        it('should count multiple mouse activity events in a single message', async () => {
            const events = [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: { source: 1 }, // MouseMove
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:01Z').toMillis(),
                    data: { source: 6 }, // TouchMove
                },
            ]
            const message = createMessage('window1', events)

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.mouseActivityCount).toBe(2)
        })

        it('should count mouse activity events across multiple messages', async () => {
            const message1 = createMessage('window1', [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: { source: 1 }, // MouseMove
                },
            ])
            const message2 = createMessage('window2', [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:01Z').toMillis(),
                    data: { source: 6 }, // TouchMove
                },
            ])

            recorder.recordMessage(message1)
            recorder.recordMessage(message2)
            const result = await recorder.end()

            expect(result.mouseActivityCount).toBe(2)
        })

        it('should not count non-mouse activity events', async () => {
            const events = [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: { source: 3 }, // Scroll - not mouse activity
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:01Z').toMillis(),
                    data: { source: 4 }, // ViewportResize - not mouse activity
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:02Z').toMillis(),
                    data: { source: 5 }, // Input - not mouse activity
                },
            ]
            const message = createMessage('window1', events)

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.mouseActivityCount).toBe(0)
        })

        it('should handle mixed mouse and non-mouse activity events', async () => {
            const events = [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: { source: 1 }, // MouseMove - counts as mouse activity
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:01Z').toMillis(),
                    data: { source: 3 }, // Scroll - not mouse activity
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:02Z').toMillis(),
                    data: { source: 6 }, // TouchMove - counts as mouse activity
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:03Z').toMillis(),
                    data: { source: 4 }, // ViewportResize - not mouse activity
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:04Z').toMillis(),
                    data: { source: 5 }, // Input - not mouse activity
                },
            ]
            const message = createMessage('window1', events)

            recorder.recordMessage(message)
            const result = await recorder.end()

            // Only MouseMove and TouchMove should be counted
            expect(result.mouseActivityCount).toBe(2)
        })
    })

    describe('Message counting', () => {
        it('should count a single message', async () => {
            const message = createMessage('window1', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: {},
                },
            ])

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.messageCount).toBe(1)
        })

        it('should count multiple messages', async () => {
            const message1 = createMessage('window1', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: {},
                },
            ])
            const message2 = createMessage('window2', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:01Z').toMillis(),
                    data: {},
                },
            ])

            recorder.recordMessage(message1)
            recorder.recordMessage(message2)
            const result = await recorder.end()

            expect(result.messageCount).toBe(2)
        })

        it('should count zero messages when none are recorded', async () => {
            const result = await recorder.end()

            expect(result.messageCount).toBe(0)
        })
    })

    describe('Snapshot source and library tracking', () => {
        it('should set default values when no snapshot source or library is provided', async () => {
            const message = createMessage('window1', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: {},
                },
            ])

            // Message already has null values for snapshot_source and snapshot_library

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.snapshotSource).toBe('web') // Default value for source
            expect(result.snapshotLibrary).toBeNull() // Default value for library
        })

        it('should set snapshot source and library from message', async () => {
            const message = createMessage('window1', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: {},
                },
            ])

            message.snapshot_source = 'mobile'
            message.snapshot_library = 'posthog-android'

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.snapshotSource).toBe('mobile')
            expect(result.snapshotLibrary).toBe('posthog-android')
        })

        it('should limit snapshot source and library fields to 1000 characters', async () => {
            const message = createMessage('window1', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: {},
                },
            ])

            const longString = 'a'.repeat(2000)
            message.snapshot_source = longString
            message.snapshot_library = longString

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.snapshotSource).toBe('a'.repeat(1000))
            expect(result.snapshotLibrary).toBe('a'.repeat(1000))
        })

        it('should use values from first message when multiple messages have different values', async () => {
            const message1 = createMessage('window1', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: {},
                },
            ])

            const message2 = createMessage('window2', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:01Z').toMillis(),
                    data: {},
                },
            ])

            message1.snapshot_source = 'web'
            message1.snapshot_library = 'posthog-js'

            message2.snapshot_source = 'mobile'
            message2.snapshot_library = 'posthog-android'

            recorder.recordMessage(message1)
            recorder.recordMessage(message2)
            const result = await recorder.end()

            expect(result.snapshotSource).toBe('web')
            expect(result.snapshotLibrary).toBe('posthog-js')
        })

        it('should use "web" as default for snapshot source if not provided', async () => {
            const message = createMessage('window1', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: {},
                },
            ])

            // Keep snapshot_source as null and set library
            message.snapshot_library = 'posthog-js'

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.snapshotSource).toBe('web')
            expect(result.snapshotLibrary).toBe('posthog-js')
        })
    })

    describe('Snapshot mode tracking', () => {
        it.each([
            ['full screenshot', 2, { wireframes: [{ id: 1, type: 'screenshot' }] }, 'screenshot'],
            ['full text', 2, { wireframes: [{ id: 1, type: 'text' }] }, 'wireframe'],
            ['untyped container', 2, { wireframes: [{ id: 1 }] }, 'wireframe'],
            ['null type', 2, { wireframes: [{ id: 1, type: null }] }, 'wireframe'],
            ['image with base64', 2, { wireframes: [{ id: 1, type: 'image', base64: 'fake-image' }] }, 'wireframe'],
            [
                'incremental screenshot add',
                3,
                { source: 0, adds: [{ wireframe: { id: 1, type: 'screenshot' } }] },
                'screenshot',
            ],
            [
                'incremental screenshot update',
                3,
                { source: 0, updates: [{ wireframe: { id: 1, type: 'screenshot' } }] },
                'screenshot',
            ],
            ['incremental wireframe update', 3, { source: 0, updates: [{ wireframe: { id: 1 } }] }, 'wireframe'],
            ['meta', 4, { width: 100, height: 200 }, null],
            ['touch', 3, { source: 2, type: 7, id: 1 }, null],
            ['removal', 3, { source: 0, removes: [{ id: 1 }] }, null],
            ['empty full snapshot', 2, { wireframes: [] }, null],
            ['malformed wireframes', 2, { wireframes: [null, {}, 'invalid'] }, null],
            ['web DOM mutation', 3, { source: 0, adds: [{ node: { id: 1, type: 2 } }] }, null],
        ])('classifies %s', async (_name, type, data, expectedMode) => {
            const message = createMessage('window1', [{ type, data, timestamp: 1000 }])
            message.snapshot_source = 'mobile'
            recorder.recordMessage(message)
            expect((await recorder.end()).snapshotMode).toBe(expectedMode)
        })

        it.each(['web', null, 'Mobile'])('does not classify source %s', async (source) => {
            const message = createMessage('window1', [
                { type: 2, data: { wireframes: [{ id: 1, type: 'screenshot' }] }, timestamp: 1000 },
            ])
            message.snapshot_source = source
            recorder.recordMessage(message)
            expect((await recorder.end()).snapshotMode).toBeNull()
        })

        it('waits for a visual snapshot in a later message and keeps its classification', async () => {
            const events = [
                { type: 4, data: { width: 100, height: 200 }, timestamp: 1000 },
                {
                    type: 3,
                    data: { source: 0, updates: [{ wireframe: { id: 1, type: 'screenshot' } }] },
                    timestamp: 2000,
                },
                { type: 3, data: { source: 2, type: 7, id: 1 }, timestamp: 3000 },
            ]
            for (const event of events) {
                const message = createMessage('window1', [event])
                message.snapshot_source = 'mobile'
                recorder.recordMessage(message)
            }
            expect((await recorder.end()).snapshotMode).toBe('screenshot')
        })
    })

    describe('Buffer size reporting', () => {
        it('should report the uncompressed buffer size', async () => {
            // Create a message with a known event
            const events = [
                {
                    type: RRWebEventType.Meta,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: { href: 'https://example.com' },
                },
            ]

            const message = createMessage('window1', events)
            recorder.recordMessage(message)
            const result = await recorder.end()

            const decompressedBuffer = await readSnappyBuffer(result.buffer)
            expect(result.size).toBe(decompressedBuffer.length)
        })
    })

    describe('Batch ID', () => {
        it('should include batch ID in end result', async () => {
            const batchId = 'test-batch-123'
            const recorder = new SessionBlockRecorder('test_session_id', 1, batchId)
            const message = createMessage('window1', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: {},
                },
            ])

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.batchId).toBe(batchId)
        })

        it('should maintain batch ID across multiple messages', async () => {
            const batchId = 'test-batch-456'
            const recorder = new SessionBlockRecorder('test_session_id', 1, batchId)

            const message1 = createMessage('window1', [
                { type: RRWebEventType.Meta, timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(), data: {} },
            ])
            const message2 = createMessage('window2', [
                { type: RRWebEventType.Meta, timestamp: DateTime.fromISO('2025-01-01T01:00:01Z').toMillis(), data: {} },
            ])

            recorder.recordMessage(message1)
            recorder.recordMessage(message2)
            const result = await recorder.end()

            expect(result.batchId).toBe(batchId)
        })

        it('should include batch ID even with no messages', async () => {
            const batchId = 'test-batch-789'
            const recorder = new SessionBlockRecorder('test_session_id', 1, batchId)
            const result = await recorder.end()

            expect(result.batchId).toBe(batchId)
        })
    })

    describe('Active time calculation', () => {
        it('should calculate active time from events', async () => {
            // Create events with timestamps that would result in active time
            const events = [
                {
                    type: RRWebEventType.Meta,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: { href: 'https://example.com' },
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:01Z').toMillis(),
                    data: { source: 1 }, // MouseMove - active event
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:02Z').toMillis(),
                    data: { source: 2, type: 2 }, // MouseInteraction, Click - active event
                },
            ]
            const message = createMessage('window1', events)

            recorder.recordMessage(message)
            const result = await recorder.end()

            expect(result.activeMilliseconds).toEqual(1000)
        })

        it('should handle multiple windows when calculating active time', async () => {
            // Create events for first window
            const message1 = createMessage('window1', [
                {
                    type: RRWebEventType.Meta,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:00Z').toMillis(),
                    data: { href: 'https://example.com' },
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:01Z').toMillis(),
                    data: { source: 1 }, // MouseMove - active event
                },
            ])

            // Create events for second window
            const message2 = createMessage('window2', [
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:02Z').toMillis(),
                    data: { source: 2, type: 2 }, // MouseInteraction, Click - active event
                },
                {
                    type: RRWebEventType.IncrementalSnapshot,
                    timestamp: DateTime.fromISO('2025-01-01T01:00:03Z').toMillis(),
                    data: { source: 5 }, // Input - active event
                },
            ])

            recorder.recordMessage(message1)
            recorder.recordMessage(message2)
            const result = await recorder.end()

            // The active time should be calculated based on events from both windows
            expect(result.activeMilliseconds).toEqual(2000)
        })
    })

    it('bounds index metadata without dropping replay events', async () => {
        const t = 1_700_000_000_000
        const href = `https://example.com/${'a'.repeat(4000)}`
        const events = Array.from({ length: 100 }, () => ({ ts: t, flags: 0, href }))
        recorder.recordMessage({
            ...createMessage('w1', []),
            eventsRange: { start: DateTime.fromMillis(t), end: DateTime.fromMillis(t) },
            preSerialized: {
                windowId: 'w1',
                events,
                lines: Buffer.from(
                    `${JSON.stringify(['w1', { type: 4, timestamp: t, data: { href } }])}\n`.repeat(events.length)
                ),
                consoleLogCount: 0,
                consoleWarnCount: 0,
                consoleErrorCount: 0,
            },
        })
        const result = await recorder.end()
        expect(result.eventCount).toBe(100)
        expect(result.replayIndexTruncated).toBe(true)
        expect(result.replayIndexEntries!.length).toBeGreaterThan(0)
        expect(result.replayIndexEntries!.length).toBeLessThan(100)
        expect(Buffer.byteLength(JSON.stringify(result.replayIndexEntries))).toBeLessThan(128 * 1024 + 2)
    })

    it('keeps index ordinals across payloads and windows', async () => {
        const t = 1_700_000_000_000.5
        const eventBatches = [
            { windowId: 'w1', events: [{ ts: t, flags: 16 }] },
            {
                windowId: 'w2',
                events: [
                    { ts: t, flags: 16 },
                    { ts: t, flags: 0, jsonLd: { rootTypes: ['Product'], fullSnapshotTimestamp: t } },
                ],
            },
        ]
        for (const batch of eventBatches) {
            recorder.recordMessage({
                ...createMessage(batch.windowId, []),
                eventsRange: { start: DateTime.fromMillis(t), end: DateTime.fromMillis(t) },
                preSerialized: {
                    ...batch,
                    lines: Buffer.from('[]\n'.repeat(batch.events.length)),
                    consoleLogCount: 0,
                    consoleWarnCount: 0,
                    consoleErrorCount: 0,
                },
            })
        }
        const result = await recorder.end()
        expect(result.replayIndexEntries).toEqual([
            { kind: 'full_snapshot', windowId: 'w1', eventTimestamp: t, eventIndex: 0 },
            { kind: 'full_snapshot', windowId: 'w2', eventTimestamp: t, eventIndex: 1 },
            {
                kind: 'json_ld',
                windowId: 'w2',
                eventTimestamp: t,
                eventIndex: 2,
                rootTypes: ['Product'],
                fullSnapshotTimestamp: t,
            },
        ])
    })

    describe('Pre-serialized messages (native anonymizer fast path)', () => {
        it('appends the block lines verbatim and derives counts from the per-event metadata', async () => {
            // Flag bits mirror rust/replay-anonymizer `snapshot.rs`: 1=active 2=click 4=keypress 8=mouse.
            const t0 = DateTime.fromISO('2025-01-01T01:00:00Z').toMillis()
            const lines = Buffer.from(
                `["w1",{"type":4,"timestamp":${t0},"data":{"href":"https://example.com/[redacted]"}}]\n` +
                    `["w1",{"type":3,"timestamp":${t0 + 1000},"data":{"source":2,"type":2}}]\n` +
                    `["w1",{"type":3,"timestamp":${t0 + 2000},"data":{"source":5,"text":"****"}}]\n`
            )
            const message: ParsedMessageData = {
                ...createMessage('w1', []),
                eventsByWindowId: {},
                eventsRange: { start: DateTime.fromMillis(t0), end: DateTime.fromMillis(t0 + 2000) },
                preSerialized: {
                    lines,
                    windowId: 'w1',
                    events: [
                        { ts: t0, flags: 0, href: 'https://example.com/[redacted]' },
                        { ts: t0 + 1000, flags: 1 | 2 | 8 }, // click: active + click + mouse
                        { ts: t0 + 2000, flags: 1 | 4 }, // input: active + keypress
                    ],
                    consoleLogCount: 0,
                    consoleWarnCount: 0,
                    consoleErrorCount: 0,
                },
            }

            const rawBytesWritten = recorder.recordMessage(message)
            expect(rawBytesWritten).toBe(lines.length)

            const result = await recorder.end()
            expect(await readSnappyBuffer(result.buffer)).toBe(lines.toString())
            expect(result.eventCount).toBe(3)
            expect(result.replayIndexEntries).toEqual([
                {
                    kind: 'page',
                    windowId: 'w1',
                    eventTimestamp: t0,
                    eventIndex: 0,
                    url: 'https://example.com/[redacted]',
                },
            ])
            expect(result.clickCount).toBe(1)
            expect(result.keypressCount).toBe(1)
            expect(result.mouseActivityCount).toBe(1)
            expect(result.firstUrl).toBe('https://example.com/[redacted]')
            expect(result.urls).toEqual(['https://example.com/[redacted]'])
            expect(result.size).toBe(lines.length)
            // Two active events 1s apart, then activity times out: one 1000ms active segment.
            expect(result.activeMilliseconds).toBe(1000)
            expect(result.startDateTime.toMillis()).toBe(t0)
            expect(result.endDateTime.toMillis()).toBe(t0 + 2000)
        })
    })
})