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.

31.0 KB
Raw Blame
import '~/tests/helpers/mocks/consumer.mock'
import { mockFetch } from '~/tests/helpers/mocks/request.mock'

import { DateTime } from 'luxon'
import { Message } from 'node-rdkafka'
import { register } from 'prom-client'

import { closeHub, createHub } from '~/common/utils/db/hub'
import { PostgresUse } from '~/common/utils/db/postgres'
import { createCdpConsumerDeps } from '~/tests/helpers/cdp'
import { forSnapshot } from '~/tests/helpers/snapshots'
import { createTestTeamFixture } from '~/tests/helpers/sql'
import { INVALID_TEAM_ID_KEYS } from '~/tests/helpers/team'

import { Hub, Team } from '../../types'
import {
    createHogExecutionGlobals,
    createIncomingEvent,
    createKafkaMessage,
    insertHogFunction,
} from '../_tests/fixtures'
import { DESTINATION_PLUGINS_BY_ID } from '../legacy-plugins'
import { LegacyPluginExecutorService } from '../services/legacy-plugin-executor.service'
import { HogFunctionInvocationGlobals } from '../types'
import { CdpLegacyEventsConsumer, LightweightPluginConfig } from './cdp-legacy-event.consumer'

jest.setTimeout(5000)

describe('CdpLegacyEventsConsumer', () => {
    let consumer: CdpLegacyEventsConsumer
    let legacyPluginExecutor: LegacyPluginExecutorService
    let hub: Hub
    let team: Team
    let pluginConfig: LightweightPluginConfig
    let invocation: HogFunctionInvocationGlobals
    const customerIoPlugin = DESTINATION_PLUGINS_BY_ID['plugin-customerio-plugin']
    const customerIoPluginUrl = 'https://github.com/PostHog/customerio-plugin'

    beforeEach(async () => {
        hub = await createHub()
        team = (await createTestTeamFixture(hub.postgres)).team
        consumer = new CdpLegacyEventsConsumer(hub, createCdpConsumerDeps(hub))
        legacyPluginExecutor = new LegacyPluginExecutorService(hub.postgres, hub.geoipService)

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

        // Create a plugin in the database with onEvent capability
        const { rows: pluginRows } = await hub.postgres.query(
            PostgresUse.COMMON_WRITE,
            `INSERT INTO posthog_plugin (organization_id, name, plugin_type, is_global, url, config_schema, from_json, from_web, created_at, updated_at, is_preinstalled, capabilities)
             VALUES ($1, $2, $3, $4, $5, $6::jsonb, $7, $8, $9, $10, $11, $12::jsonb)
             ON CONFLICT (url) DO NOTHING
             RETURNING *`,
            [
                team.organization_id,
                'Customer.io',
                'custom',
                false,
                customerIoPluginUrl,
                JSON.stringify({}),
                false,
                false,
                new Date().toISOString(),
                new Date().toISOString(),
                false,
                JSON.stringify({ methods: ['onEvent'] }),
            ],
            'insertPlugin'
        )
        const plugin =
            pluginRows[0] ??
            (
                await hub.postgres.query(
                    PostgresUse.COMMON_READ,
                    'SELECT * FROM posthog_plugin WHERE url = $1',
                    [customerIoPluginUrl],
                    'getPlugin'
                )
            ).rows[0]

        if (!plugin) {
            throw new Error('Customer.io plugin was not created')
        }

        // Create a plugin config with actual config values
        const pluginConfigData = {
            name: 'Customer.io Plugin',
            team_id: team.id,
            plugin_id: plugin.id,
            enabled: true,
            order: 0,
            config: {
                customerioSiteId: '1234567890',
                customerioToken: 'cio-token',
                email: '[email protected]',
            },
            created_at: new Date().toISOString(),
            updated_at: new Date().toISOString(),
        }

        const { rows: pluginConfigRows } = await hub.postgres.query<{ id: number }>(
            PostgresUse.COMMON_WRITE,
            'INSERT INTO posthog_pluginconfig (team_id, plugin_id, enabled, "order", config, created_at, updated_at, deleted) VALUES ($1, $2, true, $3, $4::jsonb, $5, $6, false) RETURNING id',
            [
                pluginConfigData.team_id,
                pluginConfigData.plugin_id,
                pluginConfigData.order,
                JSON.stringify(pluginConfigData.config),
                pluginConfigData.created_at,
                pluginConfigData.updated_at,
            ],
            'insertPluginConfig'
        )

        pluginConfig = { ...pluginConfigData, id: pluginConfigRows[0].id, plugin } as LightweightPluginConfig

        // Verify the plugin config was created with capability check
        const { rows } = await hub.postgres.query(
            PostgresUse.COMMON_READ,
            `SELECT
                posthog_pluginconfig.id,
                posthog_pluginconfig.team_id,
                posthog_plugin.capabilities
            FROM posthog_pluginconfig
            LEFT JOIN posthog_plugin ON posthog_plugin.id = posthog_pluginconfig.plugin_id
            WHERE posthog_pluginconfig.id = $1
                AND posthog_pluginconfig.enabled = 't'
                AND (posthog_pluginconfig.deleted IS NULL OR posthog_pluginconfig.deleted != 't')
                AND posthog_plugin.capabilities->'methods' @> '["onEvent"]'::jsonb`,
            [pluginConfig.id],
            'verifyPluginConfig'
        )
        expect(rows.length).toBe(1)

        mockFetch.mockImplementation((_url, _options) =>
            Promise.resolve({
                status: 200,
                json: () =>
                    Promise.resolve({
                        status: 200,
                    }),
                text: () =>
                    Promise.resolve(
                        JSON.stringify({
                            status: 200,
                        })
                    ),
                headers: {},
                dump: () => Promise.resolve(),
            })
        )

        invocation = createHogExecutionGlobals({
            project: {
                id: team.id,
                name: team.name,
                url: `http://localhost:8000/projects/${team.id}`,
            } as any,
            event: {
                uuid: 'b3a1fe86-b10c-43cc-acaf-d208977608d0',
                event: '$pageview',
                distinct_id: 'distinct_id',
                properties: {
                    $current_url: 'https://posthog.com',
                    $lib_version: '1.0.0',
                },
                timestamp: fixedTime.toISO(),
                url: 'http://localhost:8000/events/event_id',
            } as any,
        })
    })

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

    afterAll(() => {
        jest.useRealTimers()
    })

    describe('convertPluginConfigToHogFunction', () => {
        it('should convert a lightweight plugin config to a hog function', () => {
            const lightweightConfig = {
                id: pluginConfig.id,
                team_id: team.id,
                plugin_id: pluginConfig.plugin_id,
                enabled: true,
                config: {
                    customerioSiteId: '1234567890',
                    customerioToken: 'cio-token',
                    email: '[email protected]',
                },
                created_at: '2025-01-01T00:00:00.000Z',
                updated_at: '2025-01-01T00:00:00.000Z',
                plugin: {
                    id: pluginConfig.plugin_id,
                    url: 'https://github.com/PostHog/customerio-plugin',
                },
            }

            const result = consumer['convertPluginConfigToHogFunction'](lightweightConfig)

            expect(result).toBeTruthy()
            expect(result?.template_id).toBe('plugin-customerio-plugin')
            expect(result?.type).toBe('destination')
            expect(result?.team_id).toBe(team.id)
            expect(result?.inputs).toMatchObject({
                customerioSiteId: { value: '1234567890' },
                customerioToken: { value: 'cio-token' },
                email: { value: '[email protected]' },
            })
        })

        it('should include attachments in inputs when provided', () => {
            const lightweightConfig = {
                id: pluginConfig.id,
                team_id: team.id,
                plugin_id: pluginConfig.plugin_id,
                enabled: true,
                config: {
                    customerioSiteId: '1234567890',
                    customerioToken: 'cio-token',
                },
                created_at: '2025-01-01T00:00:00.000Z',
                updated_at: '2025-01-01T00:00:00.000Z',
                plugin: {
                    id: pluginConfig.plugin_id,
                    url: 'https://github.com/PostHog/customerio-plugin',
                },
            }

            const attachments = {
                mappings: {
                    event1: 'action1',
                    event2: 'action2',
                },
                customField: 'value123',
            }

            const result = consumer['convertPluginConfigToHogFunction'](lightweightConfig, attachments)

            expect(result).toBeTruthy()
            expect(result?.inputs).toMatchObject({
                customerioSiteId: { value: '1234567890' },
                customerioToken: { value: 'cio-token' },
                mappings: {
                    value: {
                        event1: 'action1',
                        event2: 'action2',
                    },
                },
                customField: { value: 'value123' },
            })
        })

        it('should not include attachments when config is empty', () => {
            const lightweightConfig = {
                id: pluginConfig.id,
                team_id: team.id,
                plugin_id: pluginConfig.plugin_id,
                enabled: true,
                config: {},
                created_at: '2025-01-01T00:00:00.000Z',
                updated_at: '2025-01-01T00:00:00.000Z',
                plugin: {
                    id: pluginConfig.plugin_id,
                    url: 'https://github.com/PostHog/customerio-plugin',
                },
            }

            const attachments = {
                mappings: {
                    event1: 'action1',
                },
            }

            const result = consumer['convertPluginConfigToHogFunction'](lightweightConfig, attachments)

            expect(result).toBeTruthy()
            expect(result?.inputs).toEqual({})
            expect(result?.inputs?.mappings).toBeUndefined()
        })

        it('should handle inline plugin URLs correctly', () => {
            const lightweightConfig = {
                id: 1,
                team_id: team.id,
                plugin_id: 1,
                enabled: true,
                config: {
                    properties: 'version',
                },
                created_at: '2025-01-01T00:00:00.000Z',
                plugin: {
                    id: 1,
                    url: 'inline://semver-flattener',
                },
            }

            const result = consumer['convertPluginConfigToHogFunction'](lightweightConfig)

            expect(result?.template_id).toBe('plugin-semver-flattener')
        })

        it('should return null if plugin has no URL', () => {
            const lightweightConfig = {
                id: 1,
                team_id: team.id,
                plugin_id: 1,
                enabled: true,
                config: {},
                created_at: '2025-01-01T00:00:00.000Z',
            }

            const result = consumer['convertPluginConfigToHogFunction'](lightweightConfig)

            expect(result).toBeNull()
        })
    })

    describe('getLegacyPluginHogFunctionInvocations', () => {
        it('should load hog functions and create invocations', async () => {
            // This test validates the full flow
            const invocations = await consumer['getLegacyPluginHogFunctionInvocations'](invocation)

            expect(invocations).toBeTruthy()
            expect(invocations.length).toBeGreaterThan(0)
            expect(invocations[0].invocation.hogFunction.template_id).toBe('plugin-customerio-plugin')

            // Check that the loader was called and cached
            const cachedConfigs = consumer['pluginConfigsLoader'].getCache()[team.id.toString()]
            expect(cachedConfigs).toBeTruthy()
            expect(cachedConfigs?.length).toBeGreaterThan(0)
        })

        it('should handle teams with no plugin configs', async () => {
            // Create invocation for a team that doesn't exist
            const emptyInvocation = {
                ...invocation,
                project: {
                    ...invocation.project,
                    id: 99999,
                },
            }

            const invocations = await consumer['getLegacyPluginHogFunctionInvocations'](emptyInvocation)
            expect(invocations).toEqual([])
        })

        it('should load attachments and include them in hog function inputs', async () => {
            // Insert an attachment for the plugin config
            await hub.postgres.query(
                PostgresUse.COMMON_WRITE,
                `INSERT INTO posthog_pluginattachment (plugin_config_id, key, contents, content_type, file_size, file_name)
                 VALUES ($1, $2, $3, $4, $5, $6)`,
                [
                    pluginConfig.id,
                    'mappings',
                    JSON.stringify({ event1: 'action1', event2: 'action2' }),
                    'application/json',
                    50,
                    'mappings.json',
                ],
                'insertPluginAttachment'
            )

            // Clear cache to force reload
            consumer['pluginConfigsLoader'].clear()

            // Get invocations
            const invocations = await consumer['getLegacyPluginHogFunctionInvocations'](invocation)

            expect(invocations).toBeTruthy()
            expect(invocations.length).toBeGreaterThan(0)

            // Check that the attachment was loaded into inputs
            const hogFunction = invocations[0].invocation.hogFunction
            expect(hogFunction.inputs).toBeTruthy()
            expect(hogFunction.inputs?.mappings).toBeTruthy()
            expect(hogFunction.inputs?.mappings?.value).toEqual({
                event1: 'action1',
                event2: 'action2',
            })

            // Verify other inputs are still present
            expect(hogFunction.inputs?.customerioSiteId?.value).toBe('1234567890')
            expect(hogFunction.inputs?.customerioToken?.value).toBe('cio-token')
        })
    })

    describe('integration with LegacyPluginExecutorService', () => {
        it('should create invocations that can be executed by the legacy plugin executor', async () => {
            jest.spyOn(customerIoPlugin, 'onEvent')

            // Execute the invocation with the legacy plugin executor
            invocation.event.event = '$identify'

            mockFetch.mockResolvedValue({
                status: 200,
                json: () =>
                    Promise.resolve({
                        total_count: 1,
                    }),
                text: () =>
                    Promise.resolve(
                        JSON.stringify({
                            total_count: 1,
                        })
                    ),
                headers: {},
                dump: () => Promise.resolve(),
            })

            // Get invocations from the consumer - these are just logged, not executed
            const invocations = await consumer['getLegacyPluginHogFunctionInvocations'](invocation)
            expect(invocations).toBeTruthy()
            expect(invocations.length).toBeGreaterThan(0)

            const hogFunctionInvocation = invocations[0].invocation
            expect(hogFunctionInvocation.hogFunction.template_id).toBe('plugin-customerio-plugin')

            // Verify the invocation structure is correct by executing it manually
            const result = await legacyPluginExecutor.execute(hogFunctionInvocation)

            expect(result.finished).toBe(true)
            expect(result.error).toBeUndefined()
            expect(customerIoPlugin.onEvent).toHaveBeenCalledTimes(1)

            // Verify the event passed to the plugin
            expect(
                forSnapshot(jest.mocked(customerIoPlugin.onEvent!).mock.calls[0][0], {
                    overrides: { team_id: '<REPLACED-TEAM-ID>' },
                })
            ).toMatchInlineSnapshot(`
                {
                  "$set": undefined,
                  "$set_once": undefined,
                  "distinct_id": "distinct_id",
                  "event": "$identify",
                  "ip": null,
                  "properties": {
                    "$current_url": "https://posthog.com",
                    "$lib_version": "1.0.0",
                  },
                  "team_id": "<REPLACED-TEAM-ID>",
                  "timestamp": "2025-01-01T00:00:00.000Z",
                  "uuid": "<REPLACED-UUID-0>",
                }
            `)

            // Verify fetch was called (setup + 2 calls from onEvent)
            expect(mockFetch).toHaveBeenCalledTimes(3)
        })

        it('should properly handle object attachments without converting them to "[object Object]"', async () => {
            // Insert an attachment with a complex object
            const attachmentObject = {
                event1: 'action1',
                event2: 'action2',
                nested: {
                    key: 'value',
                    array: [1, 2, 3],
                },
            }

            await hub.postgres.query(
                PostgresUse.COMMON_WRITE,
                `INSERT INTO posthog_pluginattachment (plugin_config_id, key, contents, content_type, file_size, file_name)
                 VALUES ($1, $2, $3, $4, $5, $6)`,
                [
                    pluginConfig.id,
                    'complexMapping',
                    JSON.stringify(attachmentObject),
                    'application/json',
                    100,
                    'complex-mapping.json',
                ],
                'insertComplexAttachment'
            )

            // Clear cache to force reload
            consumer['pluginConfigsLoader'].clear()

            // Get invocations
            const invocations = await consumer['getLegacyPluginHogFunctionInvocations'](invocation)

            expect(invocations).toBeTruthy()
            expect(invocations.length).toBeGreaterThan(0)

            // Verify that the inputs contain the actual object, not "[object Object]"
            const inputs = invocations[0].invocation.state.globals.inputs as Record<string, any>

            expect(inputs.complexMapping).toBeDefined()
            expect(typeof inputs.complexMapping).not.toBe('string')
            expect(inputs.complexMapping).toEqual(attachmentObject)

            // Verify it's not the string "[object Object]"
            expect(inputs.complexMapping).not.toBe('[object Object]')

            // Verify other string inputs are still strings
            expect(typeof inputs.customerioSiteId).toBe('string')
            expect(inputs.customerioSiteId).toBe('1234567890')
        })
    })

    describe('LazyLoader caching', () => {
        it('should cache hog functions and batch requests', async () => {
            // Clear any existing cache
            consumer['pluginConfigsLoader'].clear()

            // Make multiple requests in parallel
            const results = await Promise.all([
                consumer['pluginConfigsLoader'].get(team.id.toString()),
                consumer['pluginConfigsLoader'].get(team.id.toString()),
                consumer['pluginConfigsLoader'].get(team.id.toString()),
            ])

            // All should return the same result
            expect(results[0]).toEqual(results[1])
            expect(results[1]).toEqual(results[2])

            // Check cache is populated
            const cachedHogFunctions = consumer['pluginConfigsLoader'].getCache()[team.id.toString()]
            expect(cachedHogFunctions).toBeTruthy()
            expect(cachedHogFunctions?.length).toBeGreaterThan(0)
            expect(cachedHogFunctions![0].hogFunction).toBeTruthy()
            expect(cachedHogFunctions![0].pluginConfigId).toBe(pluginConfig.id)
        })

        it('should return empty array for teams with no configs', async () => {
            const hogFunctions = await consumer['pluginConfigsLoader'].get('99999')
            expect(hogFunctions).toEqual([])
        })

        it.each(INVALID_TEAM_ID_KEYS)(
            'returns an empty array for invalid team id %p without failing the valid team in the same load',
            async (key) => {
                const result = await consumer['pluginConfigsLoader'].getMany([team.id.toString(), key])

                expect(result[team.id.toString()]?.map((x) => x.pluginConfigId)).toEqual([pluginConfig.id])
                expect(result[key]).toEqual([])
            }
        )
    })

    describe('migrated legacy_destination hog functions', () => {
        const migrate = async (overrides: Record<string, any> = {}) =>
            insertHogFunction(hub.postgres, team.id, {
                type: 'legacy_destination',
                template_id: 'plugin-customerio-plugin',
                name: 'Migrated Customer.io',
                enabled: true,
                inputs: {
                    customerioSiteId: { value: '1234567890' },
                    customerioToken: { value: 'cio-token' },
                },
                ...overrides,
            })

        it('runs the migrated hog function instead of the plugin config it replaces', async () => {
            const migrated = await migrate()

            const invocations = await consumer['getLegacyPluginHogFunctionInvocations'](invocation)

            expect(invocations).toHaveLength(1)
            expect(invocations[0].invocation.hogFunction.id).toBe(migrated.id)
            expect(invocations[0].pluginConfigId).toBeNull()
        })

        it('still runs a plugin config whose template has not been migrated', async () => {
            await migrate({ template_id: 'plugin-hubspot-plugin' })

            const invocations = await consumer['getLegacyPluginHogFunctionInvocations'](invocation)
            const templateIds = invocations.map((x) => x.invocation.hogFunction.template_id)

            expect(templateIds).toEqual(expect.arrayContaining(['plugin-customerio-plugin', 'plugin-hubspot-plugin']))
            expect(templateIds).toHaveLength(2)
        })

        it('invokes the underlying processor exactly once when both representations exist', async () => {
            await migrate()
            const onEvent = jest.spyOn(customerIoPlugin, 'onEvent')

            await consumer.processEvent(invocation)

            expect(onEvent).toHaveBeenCalledTimes(1)
        })

        it('does not run a disabled migrated hog function, and does not let it suppress the plugin config', async () => {
            await migrate({ enabled: false })

            const invocations = await consumer['getLegacyPluginHogFunctionInvocations'](invocation)

            expect(invocations).toHaveLength(1)
            expect(invocations[0].pluginConfigId).toBe(pluginConfig.id)
        })

        it('passes secrets that live in encrypted_inputs to the processor', async () => {
            await migrate({
                inputs: { customerioSiteId: { value: '1234567890' } },
                encrypted_inputs: hub.encryptedFields.encrypt(
                    JSON.stringify({ customerioToken: { value: 'cio-token' } })
                ) as any,
            })

            const invocations = await consumer['getLegacyPluginHogFunctionInvocations'](invocation)

            expect(invocations[0].invocation.state.globals.inputs).toMatchObject({
                customerioSiteId: '1234567890',
                customerioToken: 'cio-token',
            })
        })

        it('runs one of two migrated rows sharing a template, not both', async () => {
            await migrate()
            await migrate()

            const invocations = await consumer['getLegacyPluginHogFunctionInvocations'](invocation)

            expect(invocations).toHaveLength(1)
        })

        it('labels the execution with which representation ran it', async () => {
            const metricName = 'cdp_legacy_event_consumer_execution_result_total'
            register.getSingleMetric(metricName)?.reset()

            await consumer.processEvent(invocation)

            await migrate()
            // A fresh consumer stands in for the reload that pubsub triggers in production, which
            // invalidates the hog function manager's caches as well as this consumer's own
            const migratedConsumer = new CdpLegacyEventsConsumer(hub, createCdpConsumerDeps(hub))
            await migratedConsumer.processEvent(invocation)

            const metric = (await register.getMetricsAsJSON()).find((m) => m.name === metricName) as any
            const sources = metric.values.map((v: any) => v.labels.source)

            expect(sources).toEqual(['plugin_config', 'hog_function'])
        })

        it('reports the migrated row against its own hog function id rather than the plugin config', async () => {
            const migrated = await migrate()
            const queueAppMetric = jest.spyOn(consumer['hogFunctionMonitoringService'], 'queueAppMetric')

            await consumer.processEvent(invocation)

            expect(queueAppMetric).toHaveBeenCalledWith(
                expect.objectContaining({ app_source_id: migrated.id, team_id: team.id }),
                'hog_function'
            )
        })
    })

    describe('batch handler', () => {
        const lookupError = new Error('canceling statement due to conflict with recovery')

        beforeEach(async () => {
            consumer['legacyWebhookService']['actionMatcher'].hasWebhooks = jest.fn().mockReturnValue(true)
            jest.spyOn(consumer['deps'].teamManager, 'hasAvailableFeature').mockResolvedValue(true)
            await consumer.start()
        })

        afterEach(async () => {
            await consumer.stop()
        })

        it.each([
            { lookup: 'the team lookup', spy: () => jest.spyOn(consumer['deps'].teamManager, 'getTeam') },
            { lookup: 'the plugin config lookup', spy: () => jest.spyOn(consumer['pluginConfigsLoader'], 'get') },
            {
                lookup: 'the webhook feature lookup',
                spy: () => jest.spyOn(consumer['deps'].teamManager, 'hasAvailableFeature'),
            },
        ])(
            'fails the batch when $lookup throws without sending anything, and sends once when the batch is read again',
            async ({ spy }) => {
                const parses = [
                    jest.spyOn(consumer['legacyWebhookService'], 'parseBatch'),
                    jest.spyOn(consumer, '_parseKafkaBatch'),
                ]
                const sendWebhooks = jest.spyOn(consumer['legacyWebhookService'], 'processEvents')
                const runPlugins = jest.spyOn(consumer, 'processBatch')
                spy().mockRejectedValueOnce(lookupError)
                const handleBatch = (consumer['kafkaConsumer'].connect as jest.Mock).mock.calls[0][0]
                const messages = [createKafkaMessage(createIncomingEvent(team.id, {}))]

                await expect(handleBatch(messages)).rejects.toBe(lookupError)
                // The other side's parse can still be running when the batch fails. It must not send afterwards.
                await Promise.allSettled(parses.flatMap((parse) => parse.mock.results.map((result) => result.value)))
                expect(sendWebhooks).not.toHaveBeenCalled()
                expect(runPlugins).not.toHaveBeenCalled()

                const { backgroundTask } = await handleBatch(messages)
                await backgroundTask
                expect(sendWebhooks).toHaveBeenCalledTimes(1)
                expect(sendWebhooks).toHaveBeenCalledWith([expect.objectContaining({ teamId: team.id })])
                expect(runPlugins).toHaveBeenCalledTimes(1)
                expect(runPlugins).toHaveBeenCalledWith([
                    expect.objectContaining({ project: expect.objectContaining({ id: team.id }) }),
                ])
            }
        )

        it.each([
            {
                name: 'a message that is not JSON',
                bad: (): Message => ({ ...createKafkaMessage({}), value: Buffer.from('not json') }),
            },
            {
                name: 'an event whose properties are not JSON',
                bad: (teamId: number): Message => createKafkaMessage(createIncomingEvent(teamId, { properties: '{' })),
            },
            {
                // The team lookup sends a team_id that is not a number to its token query, which Postgres
                // rejects when the text holds a NUL byte.
                name: 'an event whose team_id is a string with a NUL byte',
                bad: (teamId: number): Message =>
                    createKafkaMessage({ ...createIncomingEvent(teamId, {}), team_id: `${teamId}\u0000` }),
            },
        ])('skips $name and sends the rest of the batch on both sides', async ({ bad }) => {
            const sendWebhooks = jest.spyOn(consumer['legacyWebhookService'], 'processEvents')
            const runPlugins = jest.spyOn(consumer, 'processBatch')
            const handleBatch = (consumer['kafkaConsumer'].connect as jest.Mock).mock.calls[0][0]

            const { backgroundTask } = await handleBatch([
                bad(team.id),
                createKafkaMessage(createIncomingEvent(team.id, {})),
            ])
            await backgroundTask

            expect(sendWebhooks).toHaveBeenCalledWith([expect.objectContaining({ teamId: team.id })])
            expect(runPlugins).toHaveBeenCalledWith([
                expect.objectContaining({ project: expect.objectContaining({ id: team.id }) }),
            ])
        })
    })

    describe('plugin execution after the batch handler parses', () => {
        beforeEach(async () => {
            await consumer.start()
        })

        afterEach(async () => {
            await consumer.stop()
        })

        it('runs plugins with the configs the parse loaded, so a later config lookup cannot fail the batch', async () => {
            const loader = consumer['pluginConfigsLoader']
            const loadConfigs = loader.get.bind(loader)
            jest.spyOn(loader, 'get')
                .mockImplementationOnce(loadConfigs)
                .mockRejectedValue(new Error('canceling statement due to conflict with recovery'))
            const handleBatch = (consumer['kafkaConsumer'].connect as jest.Mock).mock.calls[0][0]

            const { backgroundTask } = await handleBatch([createKafkaMessage(createIncomingEvent(team.id, {}))])
            await backgroundTask

            expect(mockFetch).toHaveBeenCalled()
        })
    })

    describe('shutdown behavior', () => {
        it('should flush invocation results when stopping', async () => {
            const flushSpy = jest.spyOn(consumer['invocationResultsService'], 'flush')

            await consumer.stop()

            expect(flushSpy).toHaveBeenCalled()
        })
    })
})