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.

5.0 KB
Raw Blame
import { HogFlowActionSchema, HogFlowSchema } from './hogflow'

describe('hogflow schema', () => {
    const commonActionFields = {
        id: 'action-1',
        name: 'Wait',
        description: '',
        created_at: 0,
        updated_at: 0,
        filters: {},
    }

    describe('batch account filters', () => {
        it('preserves assignment status', () => {
            const parsed = HogFlowActionSchema.parse({
                ...commonActionFields,
                type: 'trigger',
                config: {
                    type: 'batch',
                    filters: { audience_type: 'accounts', properties: [], assignment_status: 'assigned' },
                },
            })

            expect((parsed.config as any).filters.assignment_status).toBe('assigned')
        })
    })

    describe('function_push', () => {
        it('accepts a UUIDT message category id', () => {
            const result = HogFlowActionSchema.safeParse({
                ...commonActionFields,
                type: 'function_push',
                config: {
                    template_id: 'template-native-push',
                    message_category_id: '0192a1b2-c3d4-0000-8e5f-0a1b2c3d4e5f',
                    template_uuid: '0192a1b2-c3d4-0000-8e5f-0a1b2c3d4e60',
                    inputs: {},
                },
            })

            expect(result).toMatchObject({ success: true })
        })
    })

    describe('wait_until_condition events', () => {
        const baseConfig = {
            condition: { filters: {} },
            max_wait_duration: '5m',
        }

        it('accepts a wait_until_condition action with an events list', () => {
            const parsed = HogFlowActionSchema.parse({
                ...commonActionFields,
                type: 'wait_until_condition',
                config: {
                    ...baseConfig,
                    events: [{ filters: { bytecode: ['_H', 1] }, name: 'pricing_viewed' }],
                },
            })
            expect(parsed.type).toBe('wait_until_condition')
            expect((parsed.config as any).events).toHaveLength(1)
        })

        it('accepts a wait_until_condition action without an events list (optional)', () => {
            const parsed = HogFlowActionSchema.parse({
                ...commonActionFields,
                type: 'wait_until_condition',
                config: baseConfig,
            })
            expect((parsed.config as any).events).toBeUndefined()
        })
    })

    describe('output_variable legacy string normalization', () => {
        it.each([
            ['bare string', 'greeting', { key: 'greeting' }],
            ['string in a list', ['greeting', { key: 'other' }], [{ key: 'greeting' }, { key: 'other' }]],
            ['canonical object', { key: 'greeting', result_path: 'r' }, { key: 'greeting', result_path: 'r' }],
        ])('parses %s to the canonical shape', (_name, stored, expected) => {
            const parsed = HogFlowActionSchema.parse({
                ...commonActionFields,
                type: 'delay',
                config: { delay_duration: '5m' },
                output_variable: stored,
            })
            expect(parsed.output_variable).toEqual(expected)
        })
    })

    describe('conversion events', () => {
        const baseHogFlow = {
            id: 'flow-1',
            team_id: 1,
            version: 1,
            name: 'Test',
            status: 'active' as const,
            trigger: { type: 'event' as const, filters: {} },
            exit_condition: 'exit_only_at_end' as const,
            actions: [],
            edges: [],
        }

        it('accepts a conversion goal with an events list', () => {
            const parsed = HogFlowSchema.parse({
                ...baseHogFlow,
                conversion: {
                    window: '1h',
                    filters: {},
                    bytecode: [],
                    events: [{ filters: { bytecode: ['_H', 1] }, name: 'subscribed' }],
                },
            })
            expect(parsed.conversion?.events).toHaveLength(1)
        })

        it('accepts a conversion goal without an events list (optional)', () => {
            const parsed = HogFlowSchema.parse({
                ...baseHogFlow,
                conversion: { window: '1h', filters: {}, bytecode: [] },
            })
            expect(parsed.conversion?.events).toBeUndefined()
        })

        it('still parses a row that carries the removed legacy field', () => {
            // Rows keep window_minutes until the backfill strips it. Zod drops an unknown key, so the
            // flow must still parse; failing here would stop those workflows running altogether.
            const parsed = HogFlowSchema.parse({
                ...baseHogFlow,
                conversion: { window: '60d', window_minutes: 604800, filters: {}, bytecode: [] },
            })
            expect(parsed.conversion?.window).toBe('60d')
            expect(parsed.conversion).not.toHaveProperty('window_minutes')
        })
    })
})