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.

1.6 KB
Raw Blame
import { Message } from 'node-rdkafka'

import { PromiseScheduler } from '~/common/utils/promise-scheduler'

import { BatchStages } from './batch-stages'
import { SessionRecordingIngesterMetrics } from './metrics'
import { SessionReplayPipeline, SessionReplayPipelineConfig } from './session-replay-pipeline'
import { SessionReplayPipelineRunner } from './session-replay-pipeline-runner'
import { BatchCommitter } from './staged-batch'

describe('SessionReplayPipelineRunner', () => {
    it.each([
        ['one commit when the limit is off', 0, 2, 1, []],
        ['one commit for a poll under the limit', 100, 2, 1, [[1]]],
        ['one commit for each part of a poll past the limit', 15, 2, 2, [[2]]],
        ['one commit and no part count for an empty poll', 100, 0, 1, []],
    ])('makes %s', async (_, maxDecodedBytesPerWrite, messageCount, commits, observedParts) => {
        const partsSpy = jest.spyOn(SessionRecordingIngesterMetrics, 'observeKafkaBatchWriteParts').mockImplementation()
        const runner = new SessionReplayPipelineRunner(() => ({}) as SessionReplayPipeline, new PromiseScheduler())
        runner.start({ maxDecodedBytesPerWrite } as SessionReplayPipelineConfig)
        const committer: BatchCommitter = {
            knownRetention: () => undefined,
            commit: jest.fn().mockResolvedValue(undefined),
        }
        const messages = Array.from({ length: messageCount }, () => ({ value: Buffer.alloc(10) })) as Message[]

        await runner.run(messages, new BatchStages(runner.stages).admit(), committer)

        expect(committer.commit).toHaveBeenCalledTimes(commits)
        expect(partsSpy.mock.calls).toEqual(observedParts)
    })
})