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.

3.2 KB
Raw Blame
import { randomInt } from 'node:crypto'

import { KafkaProducerWrapper } from '~/common/kafka/producer'
import { closeHub, createHub } from '~/common/utils/db/hub'
import { captureIngestionWarning } from '~/ingestion/common/ingestion-warnings'
import { Clickhouse } from '~/tests/helpers/clickhouse'
import { Hub } from '~/types'

jest.setTimeout(60000) // 60 sec timeout

describe('captureIngestionWarning()', () => {
    let hub: Hub
    let clickhouse: Clickhouse
    let kafkaProducer: KafkaProducerWrapper
    let teamId: number

    beforeEach(async () => {
        hub = await createHub({ LOG_LEVEL: 'info' })
        kafkaProducer = await KafkaProducerWrapper.create(hub.KAFKA_CLIENT_RACK)
        clickhouse = Clickhouse.create()
        teamId = randomInt(1_000_000_000, 2_000_000_000)
    })

    afterEach(async () => {
        clickhouse.close()
        await kafkaProducer.disconnect()
        await closeHub(hub)
    })

    it('can read own writes', async () => {
        await captureIngestionWarning(kafkaProducer, teamId, {
            // registered as size/error in INGESTION_WARNING_TYPES
            type: 'message_size_too_large',
            // severity inside details must lose to the registry-derived field
            details: {
                foo: 'bar',
                distinctId: 'user-1',
                personId: '019831c9-6491-7000-8000-000000000000',
                severity: 'from-details',
            },
            pipelineStep: 'emit-event',
        })

        const warnings = await clickhouse.delayUntilEventIngested(
            async () => await clickhouse.query(`SELECT * FROM ingestion_warnings WHERE team_id = ${teamId}`)
        )

        expect(warnings).toEqual([
            expect.objectContaining({
                team_id: teamId,
                source: 'plugin-server',
                type: 'message_size_too_large',
                details: expect.any(String),
                timestamp: expect.any(String),
                _timestamp: expect.any(String),
            }),
        ])

        // v2 derives these columns (DEFAULT expressions) from the exact JSON key names
        // emitted by serializeIngestionWarning — a renamed key silently degrades them
        // to the defaults.
        const v2Warnings = await clickhouse.delayUntilEventIngested(
            async () =>
                await clickhouse.query<{
                    team_id: number
                    type: string
                    category: string
                    severity: string
                    pipeline_step: string
                    distinct_id: string | null
                    person_id: string | null
                }>(
                    `SELECT team_id, type, category, severity, pipeline_step, distinct_id, person_id FROM ingestion_warnings_v2 WHERE team_id = ${teamId}`
                )
        )

        expect(v2Warnings).toEqual([
            {
                team_id: teamId,
                type: 'message_size_too_large',
                category: 'size',
                severity: 'error',
                pipeline_step: 'emit-event',
                distinct_id: 'user-1',
                person_id: '019831c9-6491-7000-8000-000000000000',
            },
        ])
    })
})