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.

77.0 KB
Raw Blame
import { mockProducer, mockProducerObserver } from '~/tests/helpers/mocks/producer.mock'

import { DateTime } from 'luxon'

import { closeHub, createHub } from '~/common/utils/db/hub'
import { parseJSON } from '~/common/utils/json-parse'
import { PluginEvent } from '~/plugin-scaffold'

import { posthogFilterOutPlugin } from '../../../src/cdp/legacy-plugins/_transformations/posthog-filter-out-plugin/template'
import { template as defaultTemplate } from '../../../src/cdp/templates/_transformations/default/default.template'
import { template as geoipTemplate } from '../../../src/cdp/templates/_transformations/geoip/geoip.template'
import { compileHog } from '../../../src/cdp/templates/compiler'
import { createTestMonitoringOutputs } from '../../../tests/helpers/ingestion-outputs'
import { forSnapshot } from '../../../tests/helpers/snapshots'
import { createTestTeamFixture } from '../../../tests/helpers/sql'
import { Hub } from '../../types'
import { createHogFunction, insertHogFunction } from '../_tests/fixtures'
import { posthogPluginGeoip } from '../legacy-plugins/_transformations/posthog-plugin-geoip/template'
import { propertyFilterPlugin } from '../legacy-plugins/_transformations/property-filter-plugin/template'
import { HogFunctionTemplate, HogFunctionType } from '../types'
import type { HogTransformerServiceConfig } from './hog-transformer.service'
import { HogTransformerService, createHogTransformerService } from './hog-transformer.service'
import { resetHogvmNodeModuleCacheForTests } from './rust-vm'

jest.mock('@posthog/hogvm-node', () => ({
    init: jest.fn(),
    executeSync: jest.fn(),
    executeBatch: jest.fn(),
}))

const mockHogvmNode = jest.mocked(jest.requireMock<typeof import('@posthog/hogvm-node')>('@posthog/hogvm-node'))

const createPluginEvent = (event: Partial<PluginEvent> = {}, teamId: number = 1): PluginEvent => {
    return {
        ip: '216.160.83.56',
        site_url: 'http://localhost',
        team_id: teamId,
        now: '2024-06-07T12:00:00.000Z',
        uuid: 'event-id',
        event: 'event-name',
        distinct_id: 'distinct-id',
        properties: { $current_url: 'https://example.com', $ip: '216.160.83.56' },
        timestamp: '2024-01-01T00:00:00Z',
        ...event,
    }
}

describe('HogTransformer', () => {
    let hub: Hub
    let hogTransformer: HogTransformerService
    let teamId: number

    beforeEach(async () => {
        hub = await createHub()

        const fixedTime = DateTime.fromObject({ year: 2025, month: 1, day: 1 }, { zone: 'UTC' })
        jest.spyOn(Date, 'now').mockReturnValue(fixedTime.toMillis())

        // Create a team first before inserting hog functions
        const { team } = await createTestTeamFixture(hub.postgres)
        teamId = team.id

        hogTransformer = createHogTransformerService(hub, {
            ...hub,
            monitoringOutputs: createTestMonitoringOutputs(mockProducer),
        })
    })

    afterEach(async () => {
        await closeHub(hub)

        jest.spyOn(hogTransformer['pluginExecutor'], 'execute')
    })

    it('constructs the synchronous executor from only transformation config', async () => {
        const config: HogTransformerServiceConfig = {
            SITE_URL: hub.SITE_URL,
            CDP_HOG_RUST_VM_EXECUTION_ENABLED: false,
            CDP_HOG_RUST_VM_BATCH_EXECUTION_ENABLED: false,
            MMDB_FILE_LOCATION: hub.MMDB_FILE_LOCATION,
            // Deliberately not the default, so the assertion below proves the value is plumbed
            // through rather than hardcoded in the factory.
            TRANSFORMATIONS_HOG_TIMEOUT_MS: 123,
            CDP_INTERNAL_LOGS_SOURCES: '',
            LOGS_INTERNAL_INGESTION_SIGNING_KEYS: '',
            LOGS_INTERNAL_PRODUCER_URL: '',
            LOGS_INTERNAL_PRODUCER_TEAMS: '',
            LOGS_INTERNAL_PRODUCER_MAX_IN_FLIGHT: 20,
            LOGS_INTERNAL_PRODUCER_MAX_BUFFERED_BYTES: 1024 * 1024,
            LOGS_INTERNAL_PRODUCER_RETRY_TIMEOUT_MS: 1_000,
        }
        const transformer = createHogTransformerService(config, {
            ...hub,
            monitoringOutputs: createTestMonitoringOutputs(mockProducer),
        })

        expect(transformer['hogExecutor']['hogInputsService']['recipientTokensService']).toBeUndefined()
        expect(transformer['hogExecutor']['config'].executionTimeoutMs).toBe(123)

        await transformer.stop()
    })

    it('prefetchHogFunctionsForTeams leaves the transformation caches warm for the event path', async () => {
        const fn = createHogFunction({
            type: 'transformation',
            name: defaultTemplate.name,
            team_id: teamId,
            enabled: true,
            bytecode: await compileHog(defaultTemplate.code),
        })
        await insertHogFunction(hub.postgres, teamId, fn)

        await hogTransformer.prefetchHogFunctionsForTeams([teamId])

        const querySpy = jest.spyOn(hub.postgres, 'query')
        const result = await hogTransformer.transformEventAndProduceMessages(createPluginEvent({}, teamId))

        expect(result.invocationResults).toHaveLength(1)
        const hogFunctionQueries = querySpy.mock.calls
            .map((call) => call[3])
            .filter((tag) => tag === 'fetchAllTeamHogFunctions' || tag === 'fetchHogFunctions')
        expect(hogFunctionQueries).toEqual([])
    })

    describe('transformEvent', () => {
        it('handles geoip lookup transformation', async () => {
            // Setup the hog function
            const hogByteCode = await compileHog(geoipTemplate.code)
            const geoIpFunction = createHogFunction({
                type: 'transformation',
                name: geoipTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: hogByteCode,
                execution_order: 1,
            })
            await insertHogFunction(hub.postgres, teamId, geoIpFunction)

            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [geoIpFunction.id])

            const event: PluginEvent = createPluginEvent({}, teamId)
            const result = await hogTransformer.transformEventAndProduceMessages(event)

            expect(result.event?.properties).toMatchInlineSnapshot(`
                {
                  "$current_url": "https://example.com",
                  "$geoip_accuracy_radius": 22,
                  "$geoip_city_name": "Milton",
                  "$geoip_continent_code": "NA",
                  "$geoip_continent_name": "North America",
                  "$geoip_country_code": "US",
                  "$geoip_country_name": "United States",
                  "$geoip_latitude": 47.2513,
                  "$geoip_longitude": -122.3149,
                  "$geoip_postal_code": "98354",
                  "$geoip_subdivision_1_code": "WA",
                  "$geoip_subdivision_1_name": "Washington",
                  "$geoip_time_zone": "America/Los_Angeles",
                  "$ip": "216.160.83.56",
                  "$set": {
                    "$geoip_accuracy_radius": 22,
                    "$geoip_city_confidence": null,
                    "$geoip_city_name": "Milton",
                    "$geoip_continent_code": "NA",
                    "$geoip_continent_name": "North America",
                    "$geoip_country_code": "US",
                    "$geoip_country_name": "United States",
                    "$geoip_latitude": 47.2513,
                    "$geoip_longitude": -122.3149,
                    "$geoip_postal_code": "98354",
                    "$geoip_subdivision_1_code": "WA",
                    "$geoip_subdivision_1_name": "Washington",
                    "$geoip_subdivision_2_code": null,
                    "$geoip_subdivision_2_name": null,
                    "$geoip_time_zone": "America/Los_Angeles",
                  },
                  "$set_once": {
                    "$initial_geoip_accuracy_radius": 22,
                    "$initial_geoip_city_confidence": null,
                    "$initial_geoip_city_name": "Milton",
                    "$initial_geoip_continent_code": "NA",
                    "$initial_geoip_continent_name": "North America",
                    "$initial_geoip_country_code": "US",
                    "$initial_geoip_country_name": "United States",
                    "$initial_geoip_latitude": 47.2513,
                    "$initial_geoip_longitude": -122.3149,
                    "$initial_geoip_postal_code": "98354",
                    "$initial_geoip_subdivision_1_code": "WA",
                    "$initial_geoip_subdivision_1_name": "Washington",
                    "$initial_geoip_subdivision_2_code": null,
                    "$initial_geoip_subdivision_2_name": null,
                    "$initial_geoip_time_zone": "America/Los_Angeles",
                  },
                }
            `)
        })

        it('should expose elements_chain from $elements_chain property', async () => {
            const fn = createHogFunction({
                type: 'transformation',
                name: 'Elements Chain Reader',
                team_id: teamId,
                enabled: true,
                bytecode: [],
                execution_order: 1,
                hog: `
                    let returnEvent := event
                    if (event.elements_chain ilike '%button%') {
                        returnEvent.event := 'button_click'
                    }
                    return returnEvent
                `,
            })
            fn.bytecode = await compileHog(fn.hog)
            await insertHogFunction(hub.postgres, teamId, fn)
            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [fn.id])

            const event: PluginEvent = createPluginEvent(
                {
                    event: '$autocapture',
                    properties: {
                        $current_url: 'https://example.com',
                        $elements_chain: 'button.btn:attr__class="btn-primary"',
                    },
                },
                teamId
            )
            const result = await hogTransformer.transformEventAndProduceMessages(event)

            expect(result.event?.event).toBe('button_click')
        })

        it('only allow modifying certain properties', async () => {
            const fn = createHogFunction({
                type: 'transformation',
                name: 'Modifier',
                team_id: teamId,
                enabled: true,
                bytecode: [],
                execution_order: 1,
                hog: `
                    let returnEvent := event
                    returnEvent.distinct_id := 'modified-distinct-id'
                    returnEvent.event := 'modified-event'
                    returnEvent.properties.test_property := 'modified-test-value'
                    returnEvent.something_else := 'should not be allowed'
                    returnEvent.timestamp := 'should not be allowed'
                    return returnEvent
                `,
            })
            fn.bytecode = await compileHog(fn.hog)
            await insertHogFunction(hub.postgres, teamId, fn)
            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [fn.id])

            const event: PluginEvent = createPluginEvent({}, teamId)
            const result = await hogTransformer.transformEventAndProduceMessages(event)

            expect(forSnapshot(result.event, { overrides: { team_id: '<REPLACED-TEAM-ID>' } })).toMatchInlineSnapshot(`
                {
                  "distinct_id": "modified-distinct-id",
                  "event": "modified-event",
                  "ip": "216.160.83.56",
                  "now": "2024-06-07T12:00:00.000Z",
                  "properties": {
                    "$current_url": "https://example.com",
                    "$ip": "216.160.83.56",
                    "test_property": "modified-test-value",
                  },
                  "site_url": "http://localhost",
                  "team_id": "<REPLACED-TEAM-ID>",
                  "timestamp": "2024-01-01T00:00:00Z",
                  "uuid": "event-id",
                }
            `)
        })
        it('should execute multiple transformations and produce messages', async () => {
            const testTemplate: HogFunctionTemplate = {
                free: true,
                status: 'beta',
                type: 'transformation',
                id: 'template-test',
                name: 'Test Template',
                description: 'A simple test template that adds a test property',
                category: ['Custom'],
                code_language: 'hog',
                code: `
                    let returnEvent := event
                    returnEvent.properties.test_property := 'test_value'
                    return returnEvent
                `,
                inputs_schema: [],
            }

            const geoTransformationIpByteCode = await compileHog(geoipTemplate.code)
            const geoIpTransformationFunction = createHogFunction({
                type: 'transformation',
                name: geoipTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: geoTransformationIpByteCode,
                execution_order: 1,
            })

            const defaultTransformationByteCode = await compileHog(defaultTemplate.code)
            const defaultTransformationFunction = createHogFunction({
                type: 'transformation',
                name: defaultTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: defaultTransformationByteCode,
                execution_order: 2,
            })

            const testTransformationByteCode = await compileHog(testTemplate.code)
            const testTransformationFunction = createHogFunction({
                type: 'transformation',
                name: testTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: testTransformationByteCode,
                execution_order: 3,
            })

            await insertHogFunction(hub.postgres, teamId, testTransformationFunction)
            await insertHogFunction(hub.postgres, teamId, defaultTransformationFunction)
            await insertHogFunction(hub.postgres, teamId, geoIpTransformationFunction)

            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [
                geoIpTransformationFunction.id,
                defaultTransformationFunction.id,
                testTransformationFunction.id,
            ])

            const executeHogFunctionSpy = jest.spyOn(hogTransformer as any, 'executeHogFunction')

            const event: PluginEvent = {
                ip: '89.160.20.129',
                site_url: 'http://localhost',
                team_id: teamId,
                now: '2024-06-07T12:00:00.000Z',
                uuid: 'event-id',
                event: 'event-name',
                distinct_id: 'distinct-id',
                properties: { $ip: '89.160.20.129' },
                timestamp: '2024-01-01T00:00:00Z',
            }

            await hogTransformer.transformEventAndProduceMessages(event)

            expect(executeHogFunctionSpy).toHaveBeenCalledTimes(3)
            expect(executeHogFunctionSpy.mock.calls[0][0]).toMatchObject({ execution_order: 1 })
            expect(executeHogFunctionSpy.mock.calls[1][0]).toMatchObject({ execution_order: 2 })
            expect(executeHogFunctionSpy.mock.calls[2][0]).toMatchObject({ execution_order: 3 })
            expect(event.properties?.test_property).toEqual('test_value')

            await hogTransformer.processInvocationResults()

            const messages = mockProducerObserver.getProducedKafkaMessages()
            // Replace certain messages that have changeable values
            messages.forEach((x) => {
                if (typeof x.value.message === 'string' && x.value.message.includes('Function completed in')) {
                    x.value.message = 'Function completed in [REPLACED]'
                }
                if (typeof x.value.message === 'string' && x.value.message.includes('geoip location data for ip')) {
                    x.value.message = 'geoip location data for ip: [REPLACED]'
                }
            })
            expect(forSnapshot(messages, { overrides: { team_id: '<REPLACED-TEAM-ID>' } })).toMatchSnapshot()
        })

        it('should delete a property from previous transformation', async () => {
            const addingTemplate: HogFunctionTemplate = {
                free: true,
                status: 'alpha',
                type: 'transformation',
                id: 'template-test',
                name: 'Test Template',
                description: 'A simple test template that adds a test property',
                category: ['Custom'],
                code_language: 'hog',
                code: `
                    let returnEvent := event
                    returnEvent.properties.test_property := 'test_value'
                    return returnEvent
                `,
                inputs_schema: [],
            }

            const deletingTemplate: HogFunctionTemplate = {
                free: true,
                status: 'alpha',
                type: 'transformation',
                id: 'template-test',
                name: 'Test Template',
                description: 'A simple test template that adds a test property',
                category: ['Custom'],
                code_language: 'hog',
                code: `
                    let returnEvent := event
                    returnEvent.properties.test_property := null
                    return returnEvent
                `,
                inputs_schema: [],
            }

            const addingTransformationFunction = createHogFunction({
                type: 'transformation',
                name: addingTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: await compileHog(addingTemplate.code),
                execution_order: 1,
            })

            const deletingTransformationFunction = createHogFunction({
                type: 'transformation',
                name: deletingTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: await compileHog(deletingTemplate.code),
                execution_order: 2,
            })

            await insertHogFunction(hub.postgres, teamId, deletingTransformationFunction)
            await insertHogFunction(hub.postgres, teamId, addingTransformationFunction)

            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [
                addingTransformationFunction.id,
                deletingTransformationFunction.id,
            ])

            const executeHogFunctionSpy = jest.spyOn(hogTransformer as any, 'executeHogFunction')

            const event: PluginEvent = {
                ip: '89.160.20.129',
                site_url: 'http://localhost',
                team_id: teamId,
                now: '2024-06-07T12:00:00.000Z',
                uuid: 'event-id',
                event: 'event-name',
                distinct_id: 'distinct-id',
                properties: { $ip: '89.160.20.129' },
                timestamp: '2024-01-01T00:00:00Z',
            }

            const result = await hogTransformer.transformEventAndProduceMessages(event)

            /*
             * First call is the adding the test property
             * Second call is the deleting the test property
             * hence the result is null
             */
            expect(executeHogFunctionSpy).toHaveBeenCalledTimes(2)
            expect(result?.event?.properties?.test_property).toEqual(null)
        })

        it('should allow second transformation to read property added by first transformation', async () => {
            const firstTemplate: HogFunctionTemplate = {
                free: true,
                status: 'alpha',
                type: 'transformation',
                id: 'template-first',
                name: 'First Template',
                description: 'Adds a property that the second transformation will read',
                category: ['Custom'],
                code_language: 'hog',
                code: `
                    let returnEvent := event
                    returnEvent.properties.added_by_first := 'value_from_first'
                    returnEvent.properties.counter := 1
                    return returnEvent
                `,
                inputs_schema: [],
            }

            const secondTemplate: HogFunctionTemplate = {
                free: true,
                status: 'alpha',
                type: 'transformation',
                id: 'template-second',
                name: 'Second Template',
                description: 'Reads property from first transformation and creates a derived property',
                category: ['Custom'],
                code_language: 'hog',
                code: `
                    let returnEvent := event
                    // This should be able to read the property added by the first transformation
                    returnEvent.properties.derived_from_first := f'derived_from_{event.properties.added_by_first}'
                    returnEvent.properties.counter := event.properties.counter + 1
                    return returnEvent
                `,
                inputs_schema: [],
            }

            const firstTransformationFunction = createHogFunction({
                type: 'transformation',
                name: firstTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: await compileHog(firstTemplate.code),
                execution_order: 1,
            })

            const secondTransformationFunction = createHogFunction({
                type: 'transformation',
                name: secondTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: await compileHog(secondTemplate.code),
                execution_order: 2,
            })

            await insertHogFunction(hub.postgres, teamId, firstTransformationFunction)
            await insertHogFunction(hub.postgres, teamId, secondTransformationFunction)

            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [
                firstTransformationFunction.id,
                secondTransformationFunction.id,
            ])

            const executeHogFunctionSpy = jest.spyOn(hogTransformer as any, 'executeHogFunction')

            const event: PluginEvent = {
                ip: '89.160.20.129',
                site_url: 'http://localhost',
                team_id: teamId,
                now: '2024-06-07T12:00:00.000Z',
                uuid: 'event-id',
                event: 'event-name',
                distinct_id: 'distinct-id',
                properties: { $ip: '89.160.20.129' },
                timestamp: '2024-01-01T00:00:00Z',
            }

            const result = await hogTransformer.transformEventAndProduceMessages(event)

            expect(executeHogFunctionSpy).toHaveBeenCalledTimes(2)
            expect(result?.event?.properties?.added_by_first).toEqual('value_from_first')
            expect(result?.event?.properties?.derived_from_first).toEqual('derived_from_value_from_first')
            expect(result?.event?.properties?.counter).toEqual(2)
        })

        it('should execute tranformation without execution_order last', async () => {
            const firstTemplate: HogFunctionTemplate = {
                free: true,
                status: 'alpha',
                type: 'transformation',
                id: 'template-test',
                name: 'Test Template',
                description: 'A simple test template that adds a test property',
                category: ['Custom'],
                code_language: 'hog',
                code: `
                    return event
                `,
                inputs_schema: [],
            }

            const secondTemplate: HogFunctionTemplate = {
                free: true,
                status: 'alpha',
                type: 'transformation',
                id: 'template-test',
                name: 'Test Template',
                description: 'A simple test template that adds a test property',
                category: ['Custom'],
                code_language: 'hog',
                code: `
                    return event
                `,
                inputs_schema: [],
            }

            const thirdTemplate: HogFunctionTemplate = {
                free: true,
                status: 'alpha',
                type: 'transformation',
                id: 'template-test',
                name: 'Test Template',
                description: 'A simple test template that adds a test property',
                category: ['Custom'],
                code_language: 'hog',
                code: `
                    return event
                `,
                inputs_schema: [],
            }

            const firstTransformationByteCode = await compileHog(firstTemplate.code)
            const firstTransformationFunction = createHogFunction({
                type: 'transformation',
                name: firstTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: firstTransformationByteCode,
                execution_order: 1,
            })

            const secondTransformationByteCode = await compileHog(secondTemplate.code)
            const secondTransformationFunction = createHogFunction({
                type: 'transformation',
                name: secondTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: secondTransformationByteCode,
                execution_order: 2,
            })

            const thirdTransformationByteCode = await compileHog(thirdTemplate.code)
            const thirdTransformationFunction = createHogFunction({
                type: 'transformation',
                name: thirdTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: thirdTransformationByteCode,
                execution_order: undefined,
            })

            await insertHogFunction(hub.postgres, teamId, thirdTransformationFunction)
            await insertHogFunction(hub.postgres, teamId, secondTransformationFunction)
            await insertHogFunction(hub.postgres, teamId, firstTransformationFunction)

            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [
                thirdTransformationFunction.id,
                secondTransformationFunction.id,
                firstTransformationFunction.id,
            ])

            const executeHogFunctionSpy = jest.spyOn(hogTransformer as any, 'executeHogFunction')

            const event: PluginEvent = {
                ip: '89.160.20.129',
                site_url: 'http://localhost',
                team_id: teamId,
                now: '2024-06-07T12:00:00.000Z',
                uuid: 'event-id',
                event: 'event-name',
                distinct_id: 'distinct-id',
                properties: { $ip: '89.160.20.129' },
                timestamp: '2024-01-01T00:00:00Z',
            }

            await hogTransformer.transformEventAndProduceMessages(event)
            expect(executeHogFunctionSpy).toHaveBeenCalledTimes(3)
            expect(executeHogFunctionSpy.mock.calls[0][0]).toMatchObject({ execution_order: 1 })
            expect(executeHogFunctionSpy.mock.calls[1][0]).toMatchObject({ execution_order: 2 })
            expect(executeHogFunctionSpy.mock.calls[2][0]).toMatchObject({ execution_order: null })
        })

        it('should keep applying transformations after one returns an invalid result', async () => {
            // Create a successful transformation
            const successTemplate: HogFunctionTemplate = {
                free: true,
                status: 'beta',
                type: 'transformation',
                id: 'template-success',
                name: 'Success Template',
                description: 'A template that should succeed',
                category: ['Custom'],
                code_language: 'hog',
                code: `
                    let returnEvent := event
                    returnEvent.properties.success := true
                    return returnEvent
                `,
                inputs_schema: [],
            }

            // Create a failing transformation
            const failingTemplate: HogFunctionTemplate = {
                free: true,
                status: 'beta',
                type: 'transformation',
                id: 'template-fail',
                name: 'Failing Template',
                description: 'A template that should fail',
                category: ['Custom'],
                code_language: 'hog',
                code: `
                    // Return invalid result (not an object with properties)
                    return "invalid"
                `,
                inputs_schema: [],
            }

            const successByteCode = await compileHog(successTemplate.code)
            const successFunction = createHogFunction({
                type: 'transformation',
                name: successTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: successByteCode,
                execution_order: 1,
            })

            const failByteCode = await compileHog(failingTemplate.code)
            const failFunction = createHogFunction({
                type: 'transformation',
                name: failingTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: failByteCode,
                execution_order: 2,
            })

            await insertHogFunction(hub.postgres, teamId, successFunction)
            await insertHogFunction(hub.postgres, teamId, failFunction)

            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [
                successFunction.id,
                failFunction.id,
            ])

            const event = createPluginEvent(
                {
                    event: 'test',
                    properties: {},
                },
                teamId
            )

            const result = await hogTransformer.transformEventAndProduceMessages(event)

            // The failing transformation's invalid result is discarded, the successful one still applies
            expect(result.event?.properties).toEqual({
                success: true,
            })
        })

        it('should pull from inputs and encrypted_inputs', async () => {
            // Create a successful transformation
            const inputSetter: HogFunctionTemplate = {
                free: true,
                status: 'beta',
                type: 'transformation',
                id: 'template-input-setter',
                name: 'Input Setter',
                description: 'A template that sets the inputs',
                category: ['Custom'],
                code_language: 'hog',
                code: `
                    let returnEvent := event
                    returnEvent.properties.inputs := {
                        'not_encrypted': inputs.not_encrypted,
                        'encrypted': inputs.encrypted,
                    }
                    return returnEvent
                `,
                inputs_schema: [],
            }

            const inputSetterByteCode = await compileHog(inputSetter.code)

            const inputSetterFunction = createHogFunction({
                type: 'transformation',
                name: inputSetter.name,
                team_id: teamId,
                enabled: true,
                bytecode: inputSetterByteCode,
                inputs_schema: [
                    {
                        key: 'not_encrypted',
                        type: 'string',
                    },
                    {
                        key: 'encrypted',
                        type: 'string',
                        secret: true,
                    },
                ],
                inputs: {
                    not_encrypted: {
                        value: 'from not encrypted: {event.event}',
                        bytecode: await compileHog("return f'from not encrypted: {event.event}'"),
                    },
                },
                encrypted_inputs: hub.encryptedFields.encrypt(
                    JSON.stringify({
                        encrypted: {
                            value: 'from encrypted: {event.event}',
                            bytecode: await compileHog("return f'from encrypted: {event.event}'"),
                        },
                    })
                ) as any,
            })

            await insertHogFunction(hub.postgres, teamId, inputSetterFunction)

            const event = createPluginEvent(
                {
                    event: 'test',
                    properties: {},
                },
                teamId
            )

            const result = await hogTransformer.transformEventAndProduceMessages(event)

            // Verify the event has both success and failure tracking
            expect(result.event?.properties?.inputs).toMatchObject({
                not_encrypted: 'from not encrypted: test',
                encrypted: 'from encrypted: test',
            })
        })

        it('should leave the event untouched if no transformations run', async () => {
            const event = createPluginEvent(
                {
                    event: 'test',
                    properties: { original: true },
                },
                teamId
            )

            const result = await hogTransformer.transformEventAndProduceMessages(event)

            expect(result.event?.properties).toEqual({
                original: true,
            })
        })

        it('should strip incoming transformation tracking properties', async () => {
            const successTemplate: HogFunctionTemplate = {
                free: true,
                status: 'beta',
                type: 'transformation',
                id: 'template-success',
                name: 'Success Template',
                description: 'A template that should succeed',
                category: ['Custom'],
                code_language: 'hog',
                code: `
                    let returnEvent := event
                    returnEvent.properties.success := true
                    return returnEvent
                `,
                inputs_schema: [],
            }

            const successByteCode = await compileHog(successTemplate.code)
            const successFunction = createHogFunction({
                type: 'transformation',
                name: successTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: successByteCode,
                execution_order: 1,
            })

            await insertHogFunction(hub.postgres, teamId, successFunction)

            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [successFunction.id])

            const event = createPluginEvent(
                {
                    event: 'test',
                    properties: {
                        $transformations_succeeded: ['Previous Success (prev-id)'],
                        $transformations_failed: {}, // malformed value
                        $transformations_skipped: ['Previous Skip (prev-id)'],
                    },
                },
                teamId
            )

            const result = await hogTransformer.transformEventAndProduceMessages(event)

            // A client cannot inject these retired properties, and nothing re-adds them
            expect(result?.event?.properties).toEqual({
                success: true,
            })
        })

        it('should catch thrown executeHogFunction errors without crashing and queue app metric', async () => {
            const successTemplate: HogFunctionTemplate = {
                free: true,
                status: 'beta',
                type: 'transformation',
                id: 'template-success',
                name: 'Success Template',
                description: 'A template that should succeed',
                category: ['Custom'],
                code_language: 'hog',
                code: `
                    let returnEvent := event
                    returnEvent.properties.success := true
                    return returnEvent
                `,
                inputs_schema: [],
            }

            const brokenFunction = createHogFunction({
                type: 'transformation',
                name: 'Broken Template',
                team_id: teamId,
                enabled: true,
                bytecode: await compileHog('return event'),
                execution_order: 1,
            })

            const successFunction = createHogFunction({
                type: 'transformation',
                name: successTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: await compileHog(successTemplate.code),
                execution_order: 2,
            })

            await insertHogFunction(hub.postgres, teamId, brokenFunction)
            await insertHogFunction(hub.postgres, teamId, successFunction)

            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [
                brokenFunction.id,
                successFunction.id,
            ])

            const executeHogFunctionSpy = jest.spyOn(hogTransformer as any, 'executeHogFunction')
            executeHogFunctionSpy.mockRejectedValueOnce(
                new Error('Could not execute bytecode for input field: person_id')
            )

            const queueAppMetricSpy = jest.spyOn(hogTransformer['hogFunctionMonitoringService'], 'queueAppMetric')

            const event = createPluginEvent({ event: 'test', properties: {} }, teamId)
            const result = await hogTransformer.transformEventAndProduceMessages(event)

            // The thrown error is swallowed and the next transformation still runs
            expect(result.event?.properties?.success).toBe(true)

            expect(queueAppMetricSpy).toHaveBeenCalledWith(
                {
                    team_id: teamId,
                    app_source_id: brokenFunction.id,
                    metric_kind: 'failure',
                    metric_name: 'failed',
                    count: 1,
                },
                'hog_function'
            )

            executeHogFunctionSpy.mockRestore()
            queueAppMetricSpy.mockRestore()
        })

        it('logs a deleted integration on the transformation and leaves the event unchanged', async () => {
            const hogFunction = createHogFunction({
                type: 'transformation',
                name: 'Uses an integration',
                team_id: teamId,
                enabled: true,
                bytecode: await compileHog(`
                    let returnEvent := event
                    returnEvent.properties.transformed := true
                    return returnEvent
                `),
                inputs_schema: [{ key: 'oauth', type: 'integration', required: true }],
                inputs: { oauth: { value: 99999999 } },
                execution_order: 1,
            })
            await insertHogFunction(hub.postgres, teamId, hogFunction)
            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [hogFunction.id])

            const event = createPluginEvent({ event: 'test', properties: {} }, teamId)
            const result = await hogTransformer.transformEventAndProduceMessages(event)

            expect(result.event?.properties).toEqual({})
            expect(result.invocationResults).toHaveLength(1)
            expect(result.invocationResults[0].logs).toEqual([
                expect.objectContaining({
                    level: 'error',
                    message: expect.stringContaining("The integration for input 'oauth' no longer exists"),
                }),
            ])
        })

        it('should apply a matching transformation and skip a filtered one in sequence', async () => {
            const successTemplate = {
                free: true,
                status: 'beta',
                type: 'transformation',
                id: 'template-success',
                name: 'Success Template',
                description: 'A template that should succeed',
                category: ['Custom'],
                hog: `
                    let returnEvent := event
                    returnEvent.properties.success := true
                    return returnEvent
                `,
                inputs_schema: [],
            }

            const skippedTemplate = {
                free: true,
                status: 'beta',
                type: 'transformation',
                id: 'template-skipped',
                name: 'Skipped Template',
                description: 'A template that should be skipped',
                category: ['Custom'],
                hog: `
                    let returnEvent := event
                    returnEvent.properties.should_not_be_set := true
                    return returnEvent
                `,
                inputs_schema: [],
            }

            const successFunction = createHogFunction({
                type: 'transformation',
                name: successTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: await compileHog(successTemplate.hog),
                execution_order: 1,
            })

            const skippedFunction = createHogFunction({
                type: 'transformation',
                name: skippedTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: await compileHog(skippedTemplate.hog),
                execution_order: 2,
                filters: {
                    bytecode: await compileHog(`
                        return event = 'match-me'
                    `),
                    events: [{ id: 'match-me', name: 'match-me', type: 'events', order: 0 }],
                },
            })

            await insertHogFunction(hub.postgres, teamId, successFunction)
            await insertHogFunction(hub.postgres, teamId, skippedFunction)

            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [
                successFunction.id,
                skippedFunction.id,
            ])

            const event = createPluginEvent({ event: 'does-not-match' }, teamId)
            const result = await hogTransformer.transformEventAndProduceMessages(event)

            // First transformation ran, second was filtered out
            expect(result.event?.properties?.success).toBe(true)
            expect(result.event?.properties?.should_not_be_set).toBeUndefined()
        })

        describe('capture-owned $ai_gateway properties', () => {
            const insertTransformation = async (hog: string) => {
                const fn = createHogFunction({
                    type: 'transformation',
                    name: 'gateway writer',
                    team_id: teamId,
                    enabled: true,
                    bytecode: await compileHog(hog),
                })
                await insertHogFunction(hub.postgres, teamId, fn)
                hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [fn.id])
            }

            it('discards gateway markers a transformation adds', async () => {
                await insertTransformation(`
                    let returnEvent := event
                    returnEvent.properties['$ai_gateway_verified'] := true
                    returnEvent.properties['$ai_gateway_request_id'] := 'forged'
                    returnEvent.properties['$ai_gateway_relay'] := false
                    returnEvent.properties.touched := true
                    return returnEvent
                `)

                const event = createPluginEvent(
                    { event: '$ai_generation', properties: { $ai_model: 'claude' } },
                    teamId
                )
                const result = await hogTransformer.transformEventAndProduceMessages(event)

                expect(result.event?.properties).toEqual({ $ai_model: 'claude', touched: true })
            })

            it('restores gateway markers a transformation changes or removes', async () => {
                await insertTransformation(`
                    let returnEvent := event
                    returnEvent.properties['$ai_gateway_request_id'] := 'forged'
                    returnEvent.properties['$ai_gateway_verified'] := null
                    return returnEvent
                `)

                const event = createPluginEvent(
                    {
                        event: '$ai_generation',
                        properties: {
                            $ai_model: 'claude',
                            $ai_gateway_verified: true,
                            $ai_gateway_request_id: 'req-1',
                        },
                    },
                    teamId
                )
                const result = await hogTransformer.transformEventAndProduceMessages(event)

                expect(result.event?.properties).toEqual({
                    $ai_model: 'claude',
                    $ai_gateway_verified: true,
                    $ai_gateway_request_id: 'req-1',
                })
            })

            it('leaves untouched gateway markers alone', async () => {
                await insertTransformation(`
                    let returnEvent := event
                    returnEvent.properties.touched := true
                    return returnEvent
                `)

                const event = createPluginEvent(
                    {
                        event: '$ai_generation',
                        properties: { $ai_gateway_verified: true, $ai_gateway_request_id: 'req-1' },
                    },
                    teamId
                )
                const result = await hogTransformer.transformEventAndProduceMessages(event)

                expect(result.event?.properties).toEqual({
                    $ai_gateway_verified: true,
                    $ai_gateway_request_id: 'req-1',
                    touched: true,
                })
            })
        })
    })

    describe('legacy plugins', () => {
        let executeSpy: jest.SpyInstance

        beforeEach(async () => {
            const filterOutPlugin = createHogFunction({
                type: 'transformation',
                name: posthogFilterOutPlugin.template.name,
                template_id: 'plugin-posthog-filter-out-plugin',
                inputs: {
                    eventsToDrop: {
                        value: 'drop-me',
                    },
                },
                team_id: teamId,
                enabled: true,
                hog: posthogFilterOutPlugin.template.code,
                inputs_schema: posthogFilterOutPlugin.template.inputs_schema,
            })

            await insertHogFunction(hub.postgres, teamId, filterOutPlugin)
            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [filterOutPlugin.id])

            executeSpy = jest.spyOn(hogTransformer['pluginExecutor'], 'execute')
        })

        afterEach(() => {
            executeSpy.mockRestore()
        })

        it('handles legacy plugin transformation to drop events', async () => {
            const event: PluginEvent = createPluginEvent({ event: 'drop-me', team_id: teamId })
            const result = await hogTransformer.transformEventAndProduceMessages(event)
            expect(executeSpy).toHaveBeenCalledTimes(1)
            expect(result.event).toMatchInlineSnapshot(`null`)
        })

        it('handles legacy plugin transformation to keep events', async () => {
            const event: PluginEvent = createPluginEvent({ event: 'keep-me', team_id: teamId })
            const result = await hogTransformer.transformEventAndProduceMessages(event)

            expect(executeSpy).toHaveBeenCalledTimes(1)
            expect(forSnapshot(result.event, { overrides: { team_id: '<REPLACED-TEAM-ID>' } })).toMatchInlineSnapshot(`
                {
                  "distinct_id": "distinct-id",
                  "event": "keep-me",
                  "ip": "216.160.83.56",
                  "now": "2024-06-07T12:00:00.000Z",
                  "properties": {
                    "$current_url": "https://example.com",
                    "$ip": "216.160.83.56",
                  },
                  "site_url": "http://localhost",
                  "team_id": "<REPLACED-TEAM-ID>",
                  "timestamp": "2024-01-01T00:00:00Z",
                  "uuid": "event-id",
                }
            `)
        })
    })

    describe('long event chain', () => {
        it('should handle a long chain of transformations', async () => {
            const geoIp = createHogFunction({
                type: 'transformation',
                name: posthogPluginGeoip.template.name,
                template_id: posthogPluginGeoip.template.id,
                inputs: {},
                team_id: teamId,
                enabled: true,
                hog: posthogPluginGeoip.template.code,
                inputs_schema: posthogPluginGeoip.template.inputs_schema,
            })

            const filterPlugin = createHogFunction({
                type: 'transformation',
                name: propertyFilterPlugin.template.name,
                template_id: propertyFilterPlugin.template.id,
                inputs: {
                    properties: {
                        value: '$ip,$geoip_country_code,$geoip_latitude,$geoip_longitude',
                    },
                },
                team_id: teamId,
                enabled: true,
                hog: propertyFilterPlugin.template.code,
                inputs_schema: propertyFilterPlugin.template.inputs_schema,
            })

            await insertHogFunction(hub.postgres, teamId, geoIp)
            await insertHogFunction(hub.postgres, teamId, filterPlugin)

            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [geoIp.id, filterPlugin.id])

            const event: PluginEvent = createPluginEvent({ event: 'keep-me', team_id: teamId })
            const result = await hogTransformer.transformEventAndProduceMessages(event)

            expect(forSnapshot(result.event, { overrides: { team_id: '<REPLACED-TEAM-ID>' } })).toMatchInlineSnapshot(`
                {
                  "distinct_id": "distinct-id",
                  "event": "keep-me",
                  "ip": null,
                  "now": "2024-06-07T12:00:00.000Z",
                  "properties": {
                    "$current_url": "https://example.com",
                    "$geoip_accuracy_radius": 22,
                    "$geoip_city_name": "Milton",
                    "$geoip_continent_code": "NA",
                    "$geoip_continent_name": "North America",
                    "$geoip_country_name": "United States",
                    "$geoip_postal_code": "98354",
                    "$geoip_subdivision_1_code": "WA",
                    "$geoip_subdivision_1_name": "Washington",
                    "$geoip_time_zone": "America/Los_Angeles",
                    "$set": {
                      "$geoip_accuracy_radius": 22,
                      "$geoip_city_confidence": null,
                      "$geoip_city_name": "Milton",
                      "$geoip_continent_code": "NA",
                      "$geoip_continent_name": "North America",
                      "$geoip_country_code": "US",
                      "$geoip_country_name": "United States",
                      "$geoip_latitude": 47.2513,
                      "$geoip_longitude": -122.3149,
                      "$geoip_postal_code": "98354",
                      "$geoip_subdivision_1_code": "WA",
                      "$geoip_subdivision_1_name": "Washington",
                      "$geoip_subdivision_2_code": null,
                      "$geoip_subdivision_2_name": null,
                      "$geoip_time_zone": "America/Los_Angeles",
                    },
                    "$set_once": {
                      "$initial_geoip_accuracy_radius": 22,
                      "$initial_geoip_city_confidence": null,
                      "$initial_geoip_city_name": "Milton",
                      "$initial_geoip_continent_code": "NA",
                      "$initial_geoip_continent_name": "North America",
                      "$initial_geoip_country_code": "US",
                      "$initial_geoip_country_name": "United States",
                      "$initial_geoip_latitude": 47.2513,
                      "$initial_geoip_longitude": -122.3149,
                      "$initial_geoip_postal_code": "98354",
                      "$initial_geoip_subdivision_1_code": "WA",
                      "$initial_geoip_subdivision_1_name": "Washington",
                      "$initial_geoip_subdivision_2_code": null,
                      "$initial_geoip_subdivision_2_name": null,
                      "$initial_geoip_time_zone": "America/Los_Angeles",
                    },
                  },
                  "site_url": "http://localhost",
                  "team_id": "<REPLACED-TEAM-ID>",
                  "timestamp": "2024-01-01T00:00:00Z",
                  "uuid": "event-id",
                }
            `)
        })
    })

    describe('filter-based transformations', () => {
        it('should skip transformation when filter does not match', async () => {
            const filterTemplate = {
                free: true,
                status: 'beta',
                type: 'transformation',
                id: 'template-test',
                name: 'Filter Template',
                description: 'A template that should be skipped when filter does not match',
                category: ['Custom'],
                hog: `
                    let returnEvent := event
                    returnEvent.properties.should_not_be_set := true
                    return returnEvent
                `,
                inputs_schema: [],
            }

            const hogFunction = createHogFunction({
                type: 'transformation',
                name: filterTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: await compileHog(filterTemplate.hog),
                filters: {
                    bytecode: await compileHog(`
                        return event = 'match-me'
                    `),
                    events: [{ id: 'match-me', name: 'match-me', type: 'events', order: 0 }],
                },
            })

            await insertHogFunction(hub.postgres, teamId, hogFunction)
            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [hogFunction.id])

            const event = createPluginEvent({ event: 'does-not-match-me', properties: { original: true } }, teamId)
            const result = await hogTransformer.transformEventAndProduceMessages(event)

            // The transformation never ran, and the event is passed through untouched
            expect(result.event?.properties).toEqual({ original: true })
        })

        it('should apply transformation when filter matches', async () => {
            const filterMatchingTemplate = {
                free: true,
                status: 'beta',
                type: 'transformation',
                id: 'template-test',
                name: 'Test Template',
                description: 'A template that adds a property when filter matches',
                category: ['Custom'],
                hog: `
                    let returnEvent := event
                    returnEvent.properties.test_property := 'test_value'
                    return returnEvent
                `,
                inputs_schema: [],
            }

            const hogFunction = createHogFunction({
                type: 'transformation',
                name: filterMatchingTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: await compileHog(filterMatchingTemplate.hog),
                filters: {
                    bytecode: await compileHog(`
                        // Filter that matches events with event name 'match-me'
                        return event = 'match-me'
                    `),
                    events: [{ id: 'match-me', name: 'match-me', type: 'events', order: 0 }],
                },
            })

            await insertHogFunction(hub.postgres, teamId, hogFunction)
            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [hogFunction.id])

            // Test event that should match the filter
            const matchingEvent = createPluginEvent({ event: 'match-me' }, teamId)
            const matchResult = await hogTransformer.transformEventAndProduceMessages(matchingEvent)

            // Verify transformation was applied
            expect(matchResult.event?.properties?.test_property).toBe('test_value')

            // Test event that shouldn't match the filter
            const nonMatchingEvent = createPluginEvent({ event: 'dont-match-me' }, teamId)
            const nonMatchResult = await hogTransformer.transformEventAndProduceMessages(nonMatchingEvent)

            // Verify transformation was skipped
            expect(nonMatchResult.event?.properties?.test_property).toBeUndefined()
        })

        it('should apply transformation when no filters are defined', async () => {
            const noFilterTemplate = {
                free: true,
                status: 'beta',
                type: 'transformation',
                id: 'template-test',
                name: 'No Filter Template',
                description: 'A template without filters',
                category: ['Custom'],
                hog: `
                    let returnEvent := event
                    returnEvent.properties.no_filter_property := 'applied'
                    return returnEvent
                `,
                inputs_schema: [],
            }

            const hogFunction = createHogFunction({
                type: 'transformation',
                name: noFilterTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: await compileHog(noFilterTemplate.hog),
                // No filters defined
            })

            await insertHogFunction(hub.postgres, teamId, hogFunction)
            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [hogFunction.id])

            const event = createPluginEvent({ event: 'any-event' }, teamId)
            const result = await hogTransformer.transformEventAndProduceMessages(event)

            // Verify transformation was applied
            expect(result.event?.properties?.no_filter_property).toBe('applied')
        })

        it('should skip transformation when filter errors and not continue processing', async () => {
            const errorFilterTemplate = {
                free: true,
                status: 'beta',
                type: 'transformation',
                id: 'template-test',
                name: 'Error Filter Template',
                description: 'A template with an erroring filter',
                category: ['Custom'],
                hog: `
                    let returnEvent := event
                    returnEvent.properties.error_filter_property := 'should_not_be_set'
                    return returnEvent
                `,
                inputs_schema: [],
            }

            const workingTemplate = {
                free: true,
                status: 'beta',
                type: 'transformation',
                id: 'template-working',
                name: 'Working Template',
                description: 'A template that should work',
                category: ['Custom'],
                hog: `
                    let returnEvent := event
                    returnEvent.properties.working_property := 'working'
                    return returnEvent
                `,
                inputs_schema: [],
            }

            const errorFunction = createHogFunction({
                type: 'transformation',
                name: errorFilterTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: await compileHog(errorFilterTemplate.hog),
                filters: {
                    bytecode: await compileHog(`
                        // Invalid filter that will throw an error
                        lol
                    `),
                    events: [{ id: 'test-event', name: 'test-event', type: 'events', order: 0 }],
                },
            })

            const workingFunction = createHogFunction({
                type: 'transformation',
                name: workingTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: await compileHog(workingTemplate.hog),
            })

            await insertHogFunction(hub.postgres, teamId, errorFunction)
            await insertHogFunction(hub.postgres, teamId, workingFunction)

            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [
                errorFunction.id,
                workingFunction.id,
            ])

            const queueAppMetricsSpy = jest.spyOn(hogTransformer['hogFunctionMonitoringService'], 'queueAppMetrics')
            const queueLogsSpy = jest.spyOn(hogTransformer['hogFunctionMonitoringService'], 'queueLogs')

            const event = createPluginEvent({ event: 'test-event' }, teamId)
            const result = await hogTransformer.transformEventAndProduceMessages(event)

            // The erroring filter skips its transformation without setting its property
            expect(result.event?.properties?.error_filter_property).toBeUndefined()
            expect(queueAppMetricsSpy).toHaveBeenCalledWith(
                expect.arrayContaining([
                    expect.objectContaining({
                        metric_name: 'filtering_failed',
                    }),
                ]),
                'hog_function'
            )
            expect(queueLogsSpy).toHaveBeenCalledWith(
                expect.arrayContaining([
                    expect.objectContaining({
                        message: expect.stringContaining('Global variable not found'),
                    }),
                ]),
                'hog_function'
            )

            expect(result.event?.properties?.working_property).toBe('working')

            queueAppMetricsSpy.mockRestore()
            queueLogsSpy.mockRestore()
        })

        it('should skip transformation when none of multiple filters match', async () => {
            const multiFilterTemplate = {
                free: true,
                status: 'beta',
                type: 'transformation',
                id: 'template-test',
                name: 'Multi Filter Template',
                description: 'A template with multiple filters that should all not match',
                category: ['Custom'],
                hog: `
                    let returnEvent := event
                    returnEvent.properties.should_not_be_set := true
                    return returnEvent
                `,
                inputs_schema: [],
            }

            const hogFunction = createHogFunction({
                type: 'transformation',
                name: multiFilterTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: await compileHog(multiFilterTemplate.hog),
                filters: {
                    bytecode: await compileHog(`
                        // First filter checks for 'match-me-1'
                        let filter1 := event = 'match-me-1'
                        // Second filter checks for 'match-me-2'
                        let filter2 := event = 'match-me-2'
                        // Only transform if at least one filter matches
                        return filter1 or filter2
                    `),
                    events: [
                        { id: 'match-me-1', name: 'match-me-1', type: 'events', order: 0 },
                        { id: 'match-me-2', name: 'match-me-2', type: 'events', order: 1 },
                    ],
                },
            })

            await insertHogFunction(hub.postgres, teamId, hogFunction)
            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [hogFunction.id])

            const event = createPluginEvent({ event: 'does-not-match-any' }, teamId)
            const result = await hogTransformer.transformEventAndProduceMessages(event)

            // Verify transformation was skipped since no filters matched
            expect(result.event?.properties?.should_not_be_set).toBeUndefined()
        })

        it('should apply transformation when at least one of multiple filters match', async () => {
            const multiFilterTemplate = {
                free: true,
                status: 'beta',
                type: 'transformation',
                id: 'template-test',
                name: 'Multi Filter Template',
                description: 'A template with multiple filters where one should match',
                category: ['Custom'],
                hog: `
                    let returnEvent := event
                    returnEvent.properties.should_be_set := true
                    return returnEvent
                `,
                inputs_schema: [],
            }

            const hogFunction = createHogFunction({
                type: 'transformation',
                name: multiFilterTemplate.name,
                team_id: teamId,
                enabled: true,
                bytecode: await compileHog(multiFilterTemplate.hog),
                filters: {
                    bytecode: await compileHog(`
                        // First filter checks for 'match-me-1'
                        let filter1 := event = 'match-me-1'
                        // Second filter checks for 'match-me-2'
                        let filter2 := event = 'match-me-2'
                        // Only transform if at least one filter matches
                        return filter1 or filter2
                    `),
                },
            })

            await insertHogFunction(hub.postgres, teamId, hogFunction)
            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [hogFunction.id])

            const event = createPluginEvent({ event: 'match-me-1' }, teamId)
            const result = await hogTransformer.transformEventAndProduceMessages(event)

            // Verify transformation was applied since one filter matched
            expect(result.event?.properties?.should_be_set).toBe(true)
        })
    })

    describe('geoip template no-op', () => {
        const geoipFunction = async (overrides: Partial<HogFunctionType> = {}): Promise<HogFunctionType> =>
            createHogFunction({
                type: 'transformation',
                name: geoipTemplate.name,
                team_id: teamId,
                template_id: 'template-geoip',
                hog: geoipTemplate.code,
                bytecode: await compileHog(geoipTemplate.code),
                ...overrides,
            })

        const transform = async (hogFunctions: HogFunctionType[], properties: Record<string, any>) => {
            const executeSpy = jest.spyOn(hogTransformer['hogExecutor'], 'execute')
            const queueAppMetricSpy = jest.spyOn(hogTransformer['hogFunctionMonitoringService'], 'queueAppMetric')
            const result = await hogTransformer.transformEvent(createPluginEvent({ properties }, teamId), hogFunctions)
            hogTransformer['hogFunctionMonitoringService'].queueInvocationResults(result.invocationResults)
            const outcome = {
                vmCalls: executeSpy.mock.calls.length,
                event: JSON.stringify(result.event),
                logs: result.invocationResults.flatMap((r) => r.logs.map(({ level, message }) => ({ level, message }))),
                metrics: queueAppMetricSpy.mock.calls.map(([{ metric_kind, metric_name, count }]) => ({
                    metric_kind,
                    metric_name,
                    count,
                })),
            }
            executeSpy.mockRestore()
            queueAppMetricSpy.mockRestore()
            return outcome
        }

        it.each([
            { name: '$geoip_disable is true', properties: { $ip: '216.160.83.56', $geoip_disable: true } },
            { name: '$ip is missing', properties: { $current_url: 'https://example.com' } },
            { name: '$ip is empty', properties: { $ip: '', $geoip_disable: false } },
            { name: '$ip is an empty object', properties: { $ip: {} } },
        ])('answers without the vm and matches its output when $name', async ({ properties }) => {
            const onVm = await transform([await geoipFunction({ template_id: undefined })], properties)
            const skipped = await transform([await geoipFunction()], properties)

            expect(onVm.vmCalls).toBe(1)
            expect(skipped.vmCalls).toBe(0)
            expect(skipped.event).toBe(onVm.event)
            expect(skipped.metrics).toEqual(onVm.metrics)
            expect(skipped.metrics).toEqual([{ metric_kind: 'success', metric_name: 'succeeded', count: 1 }])
            for (const outcome of [onVm, skipped]) {
                expect(outcome.logs).toEqual([
                    { level: 'info', message: 'geoip disabled or no ip.' },
                    { level: 'debug', message: expect.stringMatching(/^Function completed in [\d.]+ms\./) },
                ])
            }
        })

        it.each([
            { name: 'the event has an ip', code: geoipTemplate.code, properties: { $ip: '216.160.83.56' } },
            {
                name: 'the function code differs from the template',
                code: geoipTemplate.code.replace('geoip disabled or no ip.', 'geoip skipped.'),
                properties: { $ip: '216.160.83.56', $geoip_disable: true },
            },
        ])('runs the vm when $name', async ({ code, properties }) => {
            const outcome = await transform(
                [await geoipFunction({ hog: code, bytecode: await compileHog(code) })],
                properties
            )

            expect(outcome.vmCalls).toBe(1)
        })

        it('decides on the event as changed by an earlier transformation', async () => {
            const disableGeoipCode = `
                let returnEvent := event
                returnEvent.properties.$geoip_disable := true
                return returnEvent
            `
            const disableGeoip = createHogFunction({
                type: 'transformation',
                team_id: teamId,
                hog: disableGeoipCode,
                bytecode: await compileHog(disableGeoipCode),
            })
            const properties = { $ip: '216.160.83.56' }

            const onVm = await transform([disableGeoip, await geoipFunction({ template_id: undefined })], properties)
            const skipped = await transform([disableGeoip, await geoipFunction()], properties)

            expect(onVm.vmCalls).toBe(2)
            expect(skipped.vmCalls).toBe(1)
            expect(skipped.event).toBe(onVm.event)
            expect(parseJSON(skipped.event).properties).toEqual({ $ip: '216.160.83.56', $geoip_disable: true })
        })
    })

    it('should throw when trying to capture events in transformations', async () => {
        // Create a transformation function that captures an event
        const captureTemplate: HogFunctionTemplate = {
            free: true,
            status: 'beta',
            type: 'transformation',
            id: 'template-capture',
            name: 'Capture Template',
            description: 'A template that captures an event',
            category: ['Custom'],
            code_language: 'hog',
            code: `
                    let returnEvent := event
                    returnEvent.properties.captured := true

                    // Capture a new event
                    postHogCapture({
                        'event': 'captured_event',
                        'distinct_id': 'captured_user',
                        'properties': {
                            'source': 'hog_function',
                            'original_event': event.event,
                            'original_distinct_id': event.distinct_id,
                            'captured_at': '2024-01-01T00:00:00Z'
                        }
                    })

                    return returnEvent
                `,
            inputs_schema: [],
        }

        const hogFunction = createHogFunction({
            type: 'transformation',
            name: captureTemplate.name,
            team_id: teamId,
            enabled: true,
            bytecode: await compileHog(captureTemplate.code),
        })

        await insertHogFunction(hub.postgres, teamId, hogFunction)
        hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [hogFunction.id])

        const event = createPluginEvent({ event: 'original-event', distinct_id: 'original_user' }, teamId)
        const result = await hogTransformer.transformEventAndProduceMessages(event)

        expect(result.invocationResults[0].error).toContain('posthogCapture is not supported in transformations')
    })

    describe('rust vm primary execution', () => {
        let bytecode: any[]

        beforeEach(async () => {
            resetHogvmNodeModuleCacheForTests()

            hub.CDP_HOG_RUST_VM_EXECUTION_ENABLED = true
            hogTransformer = createHogTransformerService(hub, {
                ...hub,
                monitoringOutputs: createTestMonitoringOutputs(mockProducer),
            })

            bytecode = await compileHog(defaultTemplate.code)
            const hogFunction = createHogFunction({
                type: 'transformation',
                name: 'Rust routed',
                team_id: teamId,
                enabled: true,
                bytecode,
            })
            await insertHogFunction(hub.postgres, teamId, hogFunction)
            hogTransformer['hogFunctionManager']['onHogFunctionsReloaded'](teamId, [hogFunction.id])
        })

        it('executes transformations on the rust vm when the flag is enabled', async () => {
            mockHogvmNode.executeSync.mockReturnValue({
                result: { properties: { from_rust: true } },
                durationUs: 100,
                logs: [],
                logsTruncated: false,
            })

            const result = await hogTransformer.transformEventAndProduceMessages(createPluginEvent({}, teamId))

            expect(mockHogvmNode.executeSync).toHaveBeenCalledTimes(1)
            expect(mockHogvmNode.executeSync.mock.calls[0][0]).toEqual(bytecode)
            expect(result.event?.properties).toEqual({
                from_rust: true,
            })
        })

        it('falls back to the node vm when the rust vm cannot run the program', async () => {
            mockHogvmNode.executeSync.mockReturnValue({
                error: 'Native call failed: unsupported_ext_fn:geoipLookup',
                durationUs: 100,
                logs: [],
                logsTruncated: false,
            })

            const result = await hogTransformer.transformEventAndProduceMessages(createPluginEvent({}, teamId))

            expect(mockHogvmNode.executeSync).toHaveBeenCalledTimes(1)
            // The node vm ran the real bytecode: the invocation succeeded and the event survives
            // with its original properties, rather than carrying the rust vm's error.
            expect(result.invocationResults).toHaveLength(1)
            expect(result.invocationResults[0].error).toBeFalsy()
            expect(result.event?.properties).toMatchObject({
                $current_url: 'https://example.com',
            })
        })

        it('answers a geoip no-op without calling the rust vm', async () => {
            const geoipFunction = createHogFunction({
                type: 'transformation',
                team_id: teamId,
                template_id: 'template-geoip',
                hog: geoipTemplate.code,
                bytecode: await compileHog(geoipTemplate.code),
            })
            const properties = { $ip: '216.160.83.56', $geoip_disable: true }

            const result = await hogTransformer.transformEvent(createPluginEvent({ properties }, teamId), [
                geoipFunction,
            ])

            expect(mockHogvmNode.executeSync).not.toHaveBeenCalled()
            expect(result.event?.properties).toEqual(properties)
        })

        it('routes through executeBatch instead of executeSync when batch execution is enabled', async () => {
            hub.CDP_HOG_RUST_VM_BATCH_EXECUTION_ENABLED = true
            hogTransformer = createHogTransformerService(hub, {
                ...hub,
                monitoringOutputs: createTestMonitoringOutputs(mockProducer),
            })
            mockHogvmNode.executeBatch.mockResolvedValue([
                {
                    result: { properties: { from_rust_batch: true } },
                    durationUs: 100,
                    logs: [],
                    logsTruncated: false,
                },
            ])

            const result = await hogTransformer.transformEventAndProduceMessages(createPluginEvent({}, teamId))

            expect(mockHogvmNode.executeSync).not.toHaveBeenCalled()
            expect(mockHogvmNode.executeBatch).toHaveBeenCalledTimes(1)
            expect(mockHogvmNode.executeBatch.mock.calls[0][0]).toEqual(bytecode)
            expect(result.event?.properties).toEqual({ from_rust_batch: true })
        })
    })
})