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.

2.5 KB
Raw Blame
import { PipelineResultType } from '~/ingestion/framework/results'
import { createTestPipelineEvent } from '~/tests/helpers/pipeline-event'

import {
    SURVEY_EVENTS,
    SURVEY_PERSON_PROPERTIES,
    createEnrichSurveyPersonPropertiesStep,
} from './enrich-survey-person-properties'

describe('createEnrichSurveyPersonPropertiesStep', () => {
    const step = createEnrichSurveyPersonPropertiesStep()

    const createEvent = (eventName: string, properties?: Record<string, unknown>, timestamp?: string) => ({
        event: createTestPipelineEvent({
            event: eventName,
            distinct_id: 'user123',
            team_id: 1,
            timestamp: timestamp || '2021-01-01T12:00:00Z',
            properties,
        }),
    })

    it('adds $survey_last_seen_date to $set for survey shown events', async () => {
        const input = createEvent(SURVEY_EVENTS.SHOWN, { $survey_id: 'test-survey' }, '2021-01-01T12:00:00Z')
        const result = await step(input)

        expect(result.type).toBe(PipelineResultType.OK)
        expect(input.event.properties!['$set']).toEqual({
            [SURVEY_PERSON_PROPERTIES.LAST_SEEN_DATE]: '2021-01-01T12:00:00Z',
        })
    })

    it('preserves existing $set properties and allows them to override', async () => {
        const input = createEvent(
            SURVEY_EVENTS.SHOWN,
            {
                $survey_id: 'test-survey',
                $set: { existing_prop: 'value', [SURVEY_PERSON_PROPERTIES.LAST_SEEN_DATE]: 'custom-override' },
            },
            '2021-01-01T12:00:00Z'
        )
        const result = await step(input)

        expect(result.type).toBe(PipelineResultType.OK)
        expect(input.event.properties!['$set']).toEqual({
            [SURVEY_PERSON_PROPERTIES.LAST_SEEN_DATE]: 'custom-override',
            existing_prop: 'value',
        })
    })

    it('does not modify non-survey events', async () => {
        const input = createEvent('$pageview', { url: '/home' })
        const result = await step(input)

        expect(result.type).toBe(PipelineResultType.OK)
        expect(input.event.properties!['$set']).toBeUndefined()
    })

    it('handles survey shown events with no properties', async () => {
        const input = createEvent(SURVEY_EVENTS.SHOWN, undefined, '2021-01-01T12:00:00Z')
        const result = await step(input)

        expect(result.type).toBe(PipelineResultType.OK)
        expect(input.event.properties!['$set']).toEqual({
            [SURVEY_PERSON_PROPERTIES.LAST_SEEN_DATE]: '2021-01-01T12:00:00Z',
        })
    })
})