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.7 KB
Raw Blame
import { drop, ok } from '~/ingestion/framework/results'
import { createTestEventHeaders } from '~/tests/helpers/event-headers'
import { EventHeaders } from '~/types'

import { createValidateEventMetadataStep } from './validate-event-metadata'

describe('createValidateEventMetadataStep', () => {
    let mockHeaders: EventHeaders
    let step: ReturnType<typeof createValidateEventMetadataStep>

    beforeEach(() => {
        mockHeaders = createTestEventHeaders({
            token: 'test-token-123',
            distinct_id: 'test-user-456',
        })

        step = createValidateEventMetadataStep()
        jest.clearAllMocks()
    })

    it('should return success when distinct_id is valid', async () => {
        const input = { headers: mockHeaders }
        const result = await step(input)

        expect(result).toEqual(ok(input))
    })

    it('should return success when distinct_id is exactly 400 characters', async () => {
        mockHeaders.distinct_id = 'a'.repeat(400)
        const input = { headers: mockHeaders }

        const result = await step(input)

        expect(result).toEqual(ok(input))
    })

    it('should return success when distinct_id is undefined', async () => {
        mockHeaders.distinct_id = undefined
        const input = { headers: mockHeaders }

        const result = await step(input)

        expect(result).toEqual(ok(input))
    })

    it('should return drop when distinct_id is longer than 400 characters', async () => {
        const longDistinctId = 'a'.repeat(401)
        mockHeaders.distinct_id = longDistinctId
        const input = { headers: mockHeaders }

        const result = await step(input)

        expect(result).toEqual(
            drop(
                'distinct_id_too_long',
                [],
                [
                    {
                        type: 'skipping_event_invalid_distinct_id',
                        details: {
                            distinctId: 'a'.repeat(100),
                            distinctIdLength: 401,
                            maxLength: 400,
                        },
                    },
                ]
            )
        )
    })

    it('should return drop when distinct_id is much longer than 400 characters', async () => {
        const veryLongDistinctId = 'x'.repeat(1000)
        mockHeaders.distinct_id = veryLongDistinctId
        const input = { headers: mockHeaders }

        const result = await step(input)

        expect(result).toEqual(
            drop(
                'distinct_id_too_long',
                [],
                [
                    {
                        type: 'skipping_event_invalid_distinct_id',
                        details: {
                            distinctId: 'x'.repeat(100),
                            distinctIdLength: 1000,
                            maxLength: 400,
                        },
                    },
                ]
            )
        )
    })

    it('should truncate distinct_id to 100 characters in warning details', async () => {
        const longDistinctId = 'b'.repeat(500)
        mockHeaders.distinct_id = longDistinctId
        const input = { headers: mockHeaders }

        const result = await step(input)

        expect(result).toEqual(
            drop(
                'distinct_id_too_long',
                [],
                [
                    {
                        type: 'skipping_event_invalid_distinct_id',
                        details: {
                            distinctId: 'b'.repeat(100),
                            distinctIdLength: 500,
                            maxLength: 400,
                        },
                    },
                ]
            )
        )
    })
})