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.

7.6 KB
Raw Blame
import { Redis } from 'ioredis'

import { SessionSet } from '~/ingestion/pipelines/sessionreplay/shared/session-map'
import { RedisPool } from '~/types'

import { SessionBatchMetrics } from './metrics'
import { SessionTracker } from './session-tracker'

jest.mock('./metrics', () => ({
    SessionBatchMetrics: {
        incrementNewSessionsDetected: jest.fn(),
        incrementSessionTrackerCacheHit: jest.fn(),
        incrementSessionTrackerCacheMiss: jest.fn(),
        incrementSessionTrackerRedisErrors: jest.fn(),
        observeSessionTrackerRedisLatency: jest.fn(),
    },
}))

const sessionSet = (...pairs: [number, string][]): SessionSet => {
    const set = new SessionSet()
    pairs.forEach(([teamId, sessionId]) => set.add(teamId, sessionId))
    return set
}

const TTL_SECONDS = 48 * 60 * 60

describe('SessionTracker', () => {
    let sessionTracker: SessionTracker
    let mockRedisClient: jest.Mocked<Redis>
    let mockPipeline: { set: jest.Mock; exec: jest.Mock }
    let mockRedisPool: jest.Mocked<RedisPool>

    beforeEach(() => {
        jest.clearAllMocks()

        mockPipeline = { set: jest.fn().mockReturnThis(), exec: jest.fn().mockResolvedValue([]) }
        mockRedisClient = {
            mget: jest.fn().mockResolvedValue([]),
            pipeline: jest.fn().mockReturnValue(mockPipeline),
        } as unknown as jest.Mocked<Redis>

        mockRedisPool = {
            acquire: jest.fn().mockResolvedValue(mockRedisClient),
            release: jest.fn(),
        } as unknown as jest.Mocked<RedisPool>

        sessionTracker = new SessionTracker(mockRedisPool, 5 * 60 * 1000)
    })

    describe('hasSeen', () => {
        it('reports new and already-seen sessions from a single MGET', async () => {
            mockRedisClient.mget = jest.fn().mockResolvedValue([null, '1'])

            const result = await sessionTracker.hasSeen(sessionSet([1, 'new'], [1, 'old']))

            expect(result.get(1, 'new')).toBe(false)
            expect(result.get(1, 'old')).toBe(true)
            expect(mockRedisClient.mget).toHaveBeenCalledTimes(1)
            expect(mockRedisClient.mget).toHaveBeenCalledWith([
                '@posthog/replay/session-seen:1:new',
                '@posthog/replay/session-seen:1:old',
            ])
        })

        it('returns an empty map without touching Redis for an empty set', async () => {
            const result = await sessionTracker.hasSeen(sessionSet())

            expect(result.size).toBe(0)
            expect(mockRedisPool.acquire).not.toHaveBeenCalled()
        })

        it('serves a marked session from the local cache without an MGET', async () => {
            await sessionTracker.markSeen(sessionSet([1, 'a']))

            const result = await sessionTracker.hasSeen(sessionSet([1, 'a']))

            expect(result.get(1, 'a')).toBe(true)
            expect(mockRedisClient.mget).not.toHaveBeenCalled()
            expect(SessionBatchMetrics.incrementSessionTrackerCacheHit).toHaveBeenCalledTimes(1)
        })

        it('caches a positive hit so the next check skips Redis, but rechecks a negative', async () => {
            mockRedisClient.mget = jest.fn().mockResolvedValue(['1'])
            await sessionTracker.hasSeen(sessionSet([1, 'seen']))
            await sessionTracker.hasSeen(sessionSet([1, 'seen']))
            // Positive was cached: only the first call hit Redis.
            expect(mockRedisClient.mget).toHaveBeenCalledTimes(1)

            mockRedisClient.mget = jest.fn().mockResolvedValue([null])
            await sessionTracker.hasSeen(sessionSet([1, 'unseen']))
            await sessionTracker.hasSeen(sessionSet([1, 'unseen']))
            // Negatives aren't cached, so an unseen session is rechecked every time.
            expect(mockRedisClient.mget).toHaveBeenCalledTimes(2)
        })

        it('fails hard by throwing on a Redis error so the caller retries instead of guessing', async () => {
            mockRedisClient.mget = jest.fn().mockRejectedValue(new Error('Redis down'))

            // hasSeen drives the key generate-vs-get decision, so it must not guess "seen" (which would
            // record cleartext / switch keys). It throws for the step's retry wrapper to re-run.
            await expect(sessionTracker.hasSeen(sessionSet([1, 'a']))).rejects.toThrow('Redis down')
            expect(SessionBatchMetrics.incrementSessionTrackerRedisErrors).toHaveBeenCalled()
            expect(mockRedisPool.release).toHaveBeenCalledWith(mockRedisClient)
        })
    })

    describe('markSeen', () => {
        it('sets each session key with a 48h TTL in one pipeline', async () => {
            await sessionTracker.markSeen(sessionSet([1, 'a'], [2, 'b']))

            expect(mockPipeline.set).toHaveBeenCalledWith(
                '@posthog/replay/session-seen:1:a',
                'seen',
                'EX',
                TTL_SECONDS,
                'NX'
            )
            expect(mockPipeline.set).toHaveBeenCalledWith(
                '@posthog/replay/session-seen:2:b',
                'seen',
                'EX',
                TTL_SECONDS,
                'NX'
            )
            expect(mockPipeline.exec).toHaveBeenCalledTimes(1)
            expect(SessionBatchMetrics.incrementNewSessionsDetected).toHaveBeenCalledTimes(2)
        })

        it('does nothing for an empty set', async () => {
            await sessionTracker.markSeen(sessionSet())

            expect(mockRedisPool.acquire).not.toHaveBeenCalled()
            expect(mockPipeline.set).not.toHaveBeenCalled()
        })

        it('namespaces the Redis keys when a key namespace is given, isolating it from the main lane', async () => {
            const namespaced = new SessionTracker(mockRedisPool, 5 * 60 * 1000, undefined, 'ml-mirror')

            await namespaced.markSeen(sessionSet([1, 'a']))

            expect(mockPipeline.set).toHaveBeenCalledWith(
                '@posthog/replay/ml-mirror/session-seen:1:a',
                'seen',
                'EX',
                TTL_SECONDS,
                'NX'
            )
        })

        it('fails open on a pipeline error', async () => {
            mockPipeline.exec = jest.fn().mockRejectedValue(new Error('Redis down'))

            await expect(sessionTracker.markSeen(sessionSet([1, 'a']))).resolves.toBeUndefined()
            expect(SessionBatchMetrics.incrementSessionTrackerRedisErrors).toHaveBeenCalled()
            expect(mockRedisPool.release).toHaveBeenCalledWith(mockRedisClient)
        })

        it('still records the session in the local cache when the pipeline write fails', async () => {
            mockPipeline.exec = jest.fn().mockRejectedValue(new Error('Redis down'))

            await sessionTracker.markSeen(sessionSet([1, 'a']))

            // Redis never persisted the mark, but this consumer must still treat the session as seen —
            // partition affinity keeps it here, so failing open without the local record would re-key
            // and re-charge the new-session budget for every session during a Redis blip.
            const result = await sessionTracker.hasSeen(sessionSet([1, 'a']))
            expect(result.get(1, 'a')).toBe(true)
            expect(mockRedisClient.mget).not.toHaveBeenCalled()
        })
    })

    describe('markBilled', () => {
        it('counts a Redis error that the pipeline returns as a command result', async () => {
            mockPipeline.exec = jest.fn().mockResolvedValue([[new Error('OOM command not allowed'), null]])

            await sessionTracker.markBilled([
                { teamId: 1, sessionId: 'a', timestampMs: 1_700_000_000_000, ttlSeconds: 60 },
            ])

            expect(SessionBatchMetrics.incrementSessionTrackerRedisErrors).toHaveBeenCalledTimes(1)
        })
    })
})