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 { KeyStore } from '~/ingestion/pipelines/sessionreplay/shared/types'

import { CleartextRecordingEncryptor } from './cleartext-encryptor'

describe('CleartextRecordingEncryptor', () => {
    let mockKeyStore: jest.Mocked<KeyStore>

    beforeEach(() => {
        mockKeyStore = {
            start: jest.fn(),
            getKey: jest.fn(),
            generateKey: jest.fn(),
            deleteKey: jest.fn(),
            stop: jest.fn(),
        } as unknown as jest.Mocked<KeyStore>
    })

    it('should complete start without error', async () => {
        const encryptor = new CleartextRecordingEncryptor(mockKeyStore)
        await expect(encryptor.start()).resolves.toBeUndefined()
    })

    it('should return data unchanged with sessionState cleartext', async () => {
        const encryptor = new CleartextRecordingEncryptor(mockKeyStore)
        const clearText = Buffer.from('hello world')

        const result = await encryptor.encryptBlock('session-123', 1, clearText)

        expect(result).toEqual({ data: clearText, sessionState: 'cleartext' })
    })

    it('should return data unchanged in encryptBlockWithKey', () => {
        const encryptor = new CleartextRecordingEncryptor(mockKeyStore)
        const blockData = Buffer.from('some data')
        const mockSessionKey = {
            plaintextKey: Buffer.alloc(0),
            encryptedKey: Buffer.alloc(0),
            sessionState: 'cleartext' as const,
        }

        const result = encryptor.encryptBlockWithKey('session-123', 1, blockData, mockSessionKey)

        expect(result).toEqual({ data: blockData, sessionState: 'cleartext' })
    })
})