Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

58.8 KB
Raw Blame
import { apId } from '@activepieces/core-utils'
import { AgentActionExecutedEvent, AgentRunSource, ApplicationEventName, EventDestinationFormat, EventDestinationScope, FlowCreatedEvent, FlowDeletedEvent, FlowRunEvent, WorkerJobType } from '@activepieces/shared'
import { FastifyInstance } from 'fastify'
import { eventDestinationService } from '../../../../src/app/event-destinations/event-destinations.service'
import { applicationEvents } from '../../../../src/app/helper/application-events'
import { domainHelper } from '../../../../src/app/helper/domain-helper'
import { encryptUtils } from '../../../../src/app/helper/encryption'
import { WebhookFlowVersionToRun, webhookService } from '../../../../src/app/webhooks/webhook.service'
import * as jobQueueModule from '../../../../src/app/workers/job-queue/job-queue'
import { db } from '../../../helpers/db'
import { createMockEventDestination } from '../../../helpers/mocks'
import { createTestContext } from '../../../helpers/test-context'
import { setupTestEnvironment, teardownTestEnvironment } from '../../../helpers/test-setup'

type FlowEventAction = FlowCreatedEvent['action'] | FlowDeletedEvent['action']

const buildEnvelope = ({ platformId, projectId }: { platformId: string, projectId?: string }) => {
    const isoNow = new Date().toISOString()
    return {
        id: apId(),
        created: isoNow,
        updated: isoNow,
        ip: '127.0.0.1',
        platformId,
        projectId,
        userId: apId(),
    }
}

const buildFlowEvent = (action: FlowEventAction, params: { platformId: string, projectId?: string, flowId?: string }): FlowCreatedEvent | FlowDeletedEvent => {
    const isoNow = new Date().toISOString()
    const flow = { id: params.flowId ?? apId(), created: isoNow, updated: isoNow }
    const project = { displayName: 'Test' }
    if (action === ApplicationEventName.FLOW_CREATED) {
        return { ...buildEnvelope(params), action, data: { flow, project } }
    }
    return {
        ...buildEnvelope(params),
        action,
        data: {
            flow,
            project,
            flowVersion: { id: apId(), displayName: 'Sample', flowId: flow.id, created: isoNow, updated: isoNow },
        },
    }
}

const buildAgentActionEvent = ({ platformId, projectId, flow }: { platformId: string, projectId?: string, flow?: { id: string, runId: string } }): AgentActionExecutedEvent => ({
    ...buildEnvelope({ platformId, projectId }),
    action: ApplicationEventName.AGENT_ACTION_EXECUTED,
    data: {
        source: AgentRunSource.FLOW_STEP,
        ...(flow ? { flow } : {}),
        action: {
            pieceName: '@activepieces/piece-gmail',
            pieceDisplayName: 'Gmail',
            actionName: 'send_email',
            displayName: 'Send Email',
        },
    },
})

const buildFlowRunEvent = (params: { platformId: string, projectId?: string, flowId?: string, status?: 'SUCCEEDED' | 'FAILED' | 'RUNNING' }): FlowRunEvent => {
    return {
        ...buildEnvelope(params),
        action: ApplicationEventName.FLOW_RUN_FINISHED,
        data: {
            flowRun: {
                id: apId(),
                environment: 'PRODUCTION',
                flowId: params.flowId ?? apId(),
                flowVersionId: apId(),
                stepNameToTest: null,
                status: params.status ?? 'SUCCEEDED',
            },
            project: { displayName: 'Test' },
        },
    }
}

const ENTITLED_PLAN = { plan: { eventStreamingEnabled: true } }

const INTERNAL_PATH_SECRET = 'Bearer internal-path-secret'

const QUEUED_JOB_SECRET = 'Bearer queued-job-secret'

const DESTINATION_URL = 'https://collector.example.com/v1/logs'

let app: FastifyInstance

beforeAll(async () => {
    app = await setupTestEnvironment({ fresh: true })
})

afterAll(async () => {
    await teardownTestEnvironment()
})

const originalJobQueue = jobQueueModule.jobQueue

describe('Event Destination Trigger', () => {
    let addSpy: ReturnType<typeof vi.fn>
    let handleWebhookSpy: ReturnType<typeof vi.spyOn<typeof webhookService, 'handleWebhook'>>

    beforeEach(() => {
        addSpy = vi.fn()
        vi.spyOn(jobQueueModule, 'jobQueue').mockImplementation((log) => {
            const real = originalJobQueue(log)
            return {
                ...real,
                add: addSpy,
            }
        })
        handleWebhookSpy = vi.spyOn(webhookService, 'handleWebhook').mockResolvedValue({
            status: 200,
            body: {},
            headers: {},
        })
    })

    afterEach(() => {
        vi.restoreAllMocks()
    })

    it('should never put a stored header, plaintext or ciphertext, into the queued job', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const storedHeader = await encryptUtils.encryptString(QUEUED_JOB_SECRET)
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
            url: DESTINATION_URL,
            headers: { url: DESTINATION_URL, values: { Authorization: storedHeader } },
        })
        await db.save('event_destination', destination)

        await eventDestinationService(app.log).trigger({
            event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
        })

        const jobData = addSpy.mock.calls[0][0].data
        expect(jobData).not.toHaveProperty('headers')
        expect(JSON.stringify(jobData)).not.toContain(QUEUED_JOB_SECRET)
        expect(JSON.stringify(jobData)).not.toContain(storedHeader.data)
    })

    it('should hand the decrypted headers to the worker only through the delivery resolver', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const otherCtx = await createTestContext(app, ENTITLED_PLAN)
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
            url: DESTINATION_URL,
            headers: { url: DESTINATION_URL, values: { Authorization: await encryptUtils.encryptString(QUEUED_JOB_SECRET) } },
        })
        await db.save('event_destination', destination)

        const resolved = await eventDestinationService(app.log).resolveDeliveryHeaders({
            platformId: ctx.platform.id,
            destinationId: destination.id,
            destinationUrl: destination.url,
        })
        expect(resolved).toEqual({ Authorization: QUEUED_JOB_SECRET })

        const crossTenant = await eventDestinationService(app.log).resolveDeliveryHeaders({
            platformId: otherCtx.platform.id,
            destinationId: destination.id,
            destinationUrl: destination.url,
        })
        expect(crossTenant).toBeNull()
    })

    it('should refuse the stored headers to a job queued for a URL the destination no longer has', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
            url: 'https://old.example.com/collect',
            headers: { url: 'https://old.example.com/collect', values: { Authorization: await encryptUtils.encryptString(QUEUED_JOB_SECRET) } },
        })
        await db.save('event_destination', destination)
        await eventDestinationService(app.log).trigger({
            event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
        })
        const queuedJob = addSpy.mock.calls[0][0].data

        await db.update('event_destination', destination.id, {
            url: 'https://new.example.com/collect',
            headers: { url: 'https://new.example.com/collect', values: { Authorization: await encryptUtils.encryptString('Bearer bound-to-the-new-url') } },
        })

        const resolved = await eventDestinationService(app.log).resolveDeliveryHeaders({
            platformId: queuedJob.platformId,
            destinationId: queuedJob.webhookId,
            destinationUrl: queuedJob.webhookUrl,
        })
        expect(resolved).toBeNull()
    })

    it('should refuse the stored headers when the destination URL changed without them, as an older build would do', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
            url: 'https://moved-by-an-older-build.example/collect',
            headers: { url: DESTINATION_URL, values: { Authorization: await encryptUtils.encryptString(QUEUED_JOB_SECRET) } },
        })
        await db.save('event_destination', destination)

        const resolved = await eventDestinationService(app.log).resolveDeliveryHeaders({
            platformId: ctx.platform.id,
            destinationId: destination.id,
            destinationUrl: destination.url,
        })

        expect(resolved).toBeNull()
    })

    it('should drop the event when the platform is not entitled to event streaming', async () => {
        const ctx = await createTestContext(app, { plan: { eventStreamingEnabled: false } })
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
        })
        await db.save('event_destination', destination)

        await eventDestinationService(app.log).trigger({
            event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
        })

        expect(addSpy).not.toHaveBeenCalled()
        expect(handleWebhookSpy).not.toHaveBeenCalled()
    })

    it('should not queue a job for a disabled destination', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
            enabled: false,
        })
        await db.save('event_destination', destination)

        await eventDestinationService(app.log).trigger({
            event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
        })

        expect(addSpy).not.toHaveBeenCalled()
    })

    it('should not queue a job for a destination that belongs to another platform', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const otherCtx = await createTestContext(app, ENTITLED_PLAN)
        const destination = createMockEventDestination({
            platformId: otherCtx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
        })
        await db.save('event_destination', destination)

        await eventDestinationService(app.log).trigger({
            event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
        })

        expect(addSpy).not.toHaveBeenCalled()
    })

    it('should queue job for matching PLATFORM scope destination', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
        })
        await db.save('event_destination', destination)

        const event = buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id })
        await eventDestinationService(app.log).trigger({
            event,
        })

        expect(addSpy).toHaveBeenCalledTimes(1)
        expect(addSpy).toHaveBeenCalledWith(
            expect.objectContaining({
                data: expect.objectContaining({
                    platformId: ctx.platform.id,
                    webhookUrl: destination.url,
                    jobType: WorkerJobType.EVENT_DESTINATION,
                    payload: event,
                }),
            }),
        )
    })

    it('should NOT queue job when event action does not match', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
        })
        await db.save('event_destination', destination)

        await eventDestinationService(app.log).trigger({
            event: buildFlowEvent(ApplicationEventName.FLOW_DELETED, { platformId: ctx.platform.id }),
        })

        expect(addSpy).not.toHaveBeenCalled()
    })

    it('should NOT trigger destinations from a different platform', async () => {
        const ctx1 = await createTestContext(app, ENTITLED_PLAN)
        const ctx2 = await createTestContext(app, ENTITLED_PLAN)
        const destination = createMockEventDestination({
            platformId: ctx1.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
        })
        await db.save('event_destination', destination)

        await eventDestinationService(app.log).trigger({
            event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx2.platform.id }),
        })

        expect(addSpy).not.toHaveBeenCalled()
    })

    it('should trigger all matching destinations', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const dest1 = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
            url: 'https://example.com/hook1',
        })
        const dest2 = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
            url: 'https://example.com/hook2',
        })
        await db.save('event_destination', [dest1, dest2])

        const event = buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id })
        await eventDestinationService(app.log).trigger({
            event,
        })

        expect(addSpy).toHaveBeenCalledTimes(2)
        const queuedJobs = addSpy.mock.calls.map((call: unknown[]) => (call[0] as { data: { webhookUrl: string, payload: unknown } }).data)
        expect(queuedJobs.map(j => j.webhookUrl)).toEqual(expect.arrayContaining(['https://example.com/hook1', 'https://example.com/hook2']))
        for (const job of queuedJobs) {
            expect(job.payload).toEqual(event)
        }
    })

    it('should match destination when events array has multiple entries', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED, ApplicationEventName.FLOW_DELETED],
            scope: EventDestinationScope.PLATFORM,
        })
        await db.save('event_destination', destination)

        const event = buildFlowEvent(ApplicationEventName.FLOW_DELETED, { platformId: ctx.platform.id })
        await eventDestinationService(app.log).trigger({
            event,
        })

        expect(addSpy).toHaveBeenCalledTimes(1)
        expect(addSpy).toHaveBeenCalledWith(
            expect.objectContaining({
                data: expect.objectContaining({
                    platformId: ctx.platform.id,
                    webhookUrl: destination.url,
                    jobType: WorkerJobType.EVENT_DESTINATION,
                    payload: event,
                }),
            }),
        )
    })

    it('should NOT dispatch FLOW_RUN_FINISHED to a destination whose URL is the same flow webhook (recursion guard)', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const flowId = apId()
        const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
            path: 'v1/webhooks',
        })
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_RUN_FINISHED],
            scope: EventDestinationScope.PLATFORM,
            url: `${webhookUrlPrefix}/${flowId}`,
        })
        await db.save('event_destination', destination)

        await eventDestinationService(app.log).trigger({
            event: buildFlowRunEvent({ platformId: ctx.platform.id, flowId }),
        })

        expect(addSpy).not.toHaveBeenCalled()
    })

    it('should NOT dispatch FLOW_RUN_FINISHED to a destination targeting the same flow webhook on a different origin (recursion guard, embed domain)', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const flowId = apId()
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_RUN_FINISHED],
            scope: EventDestinationScope.PLATFORM,
            url: `https://tenant.embed.example.com/api/v1/webhooks/${flowId}`,
        })
        await db.save('event_destination', destination)

        await eventDestinationService(app.log).trigger({
            event: buildFlowRunEvent({ platformId: ctx.platform.id, flowId }),
        })

        expect(addSpy).not.toHaveBeenCalled()
        expect(handleWebhookSpy).not.toHaveBeenCalled()
    })

    it('should NOT dispatch FLOW_RUN_FINISHED when the self-targeting destination percent-encodes the flow id in its URL', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const flowId = apId()
        const encodedFlowId = `%${flowId.charCodeAt(0).toString(16).padStart(2, '0')}${flowId.slice(1)}`
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_RUN_FINISHED],
            scope: EventDestinationScope.PLATFORM,
            url: `https://tenant.embed.example.com/api/v1/webhooks/${encodedFlowId}`,
        })
        await db.save('event_destination', destination)

        await eventDestinationService(app.log).trigger({
            event: buildFlowRunEvent({ platformId: ctx.platform.id, flowId }),
        })

        expect(addSpy).not.toHaveBeenCalled()
        expect(handleWebhookSpy).not.toHaveBeenCalled()
    })

    it('should still dispatch FLOW_RUN_FINISHED to a different-origin destination that targets a different flow (as an outbound job)', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const finishedFlowId = apId()
        const otherFlowId = apId()
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_RUN_FINISHED],
            scope: EventDestinationScope.PLATFORM,
            url: `https://tenant.embed.example.com/api/v1/webhooks/${otherFlowId}`,
        })
        await db.save('event_destination', destination)

        await eventDestinationService(app.log).trigger({
            event: buildFlowRunEvent({ platformId: ctx.platform.id, flowId: finishedFlowId }),
        })

        expect(addSpy).toHaveBeenCalledTimes(1)
        expect(addSpy).toHaveBeenCalledWith(
            expect.objectContaining({
                data: expect.objectContaining({ webhookUrl: destination.url }),
            }),
        )
        expect(handleWebhookSpy).not.toHaveBeenCalled()
    })

    it('should skip a different-origin self-targeting destination but keep dispatching to external destinations on the same FLOW_RUN_FINISHED event', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const flowId = apId()
        const selfTargetingDestination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_RUN_FINISHED],
            scope: EventDestinationScope.PLATFORM,
            url: `https://tenant.embed.example.com/api/v1/webhooks/${flowId}`,
        })
        const webhookShapedDestination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_RUN_FINISHED],
            scope: EventDestinationScope.PLATFORM,
            url: `https://other-instance.example.com/api/v1/webhooks/${apId()}`,
        })
        const externalDestination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_RUN_FINISHED],
            scope: EventDestinationScope.PLATFORM,
            url: 'https://hooks.slack.example.com/services/abc',
        })
        await db.save('event_destination', [selfTargetingDestination, webhookShapedDestination, externalDestination])

        await eventDestinationService(app.log).trigger({
            event: buildFlowRunEvent({ platformId: ctx.platform.id, flowId, status: 'FAILED' }),
        })

        expect(addSpy).toHaveBeenCalledTimes(1)
        expect(addSpy).toHaveBeenCalledWith(
            expect.objectContaining({
                data: expect.objectContaining({ webhookUrl: externalDestination.url }),
            }),
        )
        expect(handleWebhookSpy).not.toHaveBeenCalled()
    })

    it('should still dispatch FLOW_RUN_FINISHED to a same-host destination that targets a different flow (internally, without an outbound job)', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const finishedFlowId = apId()
        const otherFlowId = apId()
        const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
            path: 'v1/webhooks',
        })
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_RUN_FINISHED],
            scope: EventDestinationScope.PLATFORM,
            url: `${webhookUrlPrefix}/${otherFlowId}`,
        })
        await db.save('event_destination', destination)

        await eventDestinationService(app.log).trigger({
            event: buildFlowRunEvent({ platformId: ctx.platform.id, flowId: finishedFlowId }),
        })

        expect(addSpy).not.toHaveBeenCalled()
        expect(handleWebhookSpy).toHaveBeenCalledTimes(1)
        expect(handleWebhookSpy).toHaveBeenCalledWith(
            expect.objectContaining({ flowId: otherFlowId, async: true }),
        )
    })

    it('should skip self-targeting destination but keep dispatching to other destinations on the same FLOW_RUN_FINISHED event', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const flowId = apId()
        const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
            path: 'v1/webhooks',
        })
        const selfTargetingDestination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_RUN_FINISHED],
            scope: EventDestinationScope.PLATFORM,
            url: `${webhookUrlPrefix}/${flowId}`,
        })
        const externalDestination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_RUN_FINISHED],
            scope: EventDestinationScope.PLATFORM,
            url: 'https://hooks.slack.example.com/services/abc',
        })
        await db.save('event_destination', [selfTargetingDestination, externalDestination])

        await eventDestinationService(app.log).trigger({
            event: buildFlowRunEvent({ platformId: ctx.platform.id, flowId, status: 'FAILED' }),
        })

        expect(addSpy).toHaveBeenCalledTimes(1)
        expect(addSpy).toHaveBeenCalledWith(
            expect.objectContaining({
                data: expect.objectContaining({ webhookUrl: externalDestination.url }),
            }),
        )
    })

    it('should drop both internal destinations when two flows are mutually wired (A↔B cycle), and still fire externals', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const flowAId = apId()
        const flowBId = apId()
        const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
            path: 'v1/webhooks',
        })
        const internalDestinationA = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_RUN_FINISHED],
            scope: EventDestinationScope.PLATFORM,
            url: `${webhookUrlPrefix}/${flowAId}`,
        })
        const internalDestinationB = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_RUN_FINISHED],
            scope: EventDestinationScope.PLATFORM,
            url: `${webhookUrlPrefix}/${flowBId}`,
        })
        const externalDestination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_RUN_FINISHED],
            scope: EventDestinationScope.PLATFORM,
            url: 'https://hooks.slack.example.com/services/abc',
        })
        await db.save('event_destination', [internalDestinationA, internalDestinationB, externalDestination])

        await eventDestinationService(app.log).trigger({
            event: buildFlowRunEvent({ platformId: ctx.platform.id, flowId: flowAId }),
        })

        expect(addSpy).toHaveBeenCalledTimes(1)
        expect(addSpy).toHaveBeenCalledWith(
            expect.objectContaining({
                data: expect.objectContaining({ webhookUrl: externalDestination.url }),
            }),
        )
    })

    it('should not fire any destination when an A↔B cycle has no external destinations', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const flowAId = apId()
        const flowBId = apId()
        const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
            path: 'v1/webhooks',
        })
        const internalDestinationA = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_RUN_FINISHED],
            scope: EventDestinationScope.PLATFORM,
            url: `${webhookUrlPrefix}/${flowAId}`,
        })
        const internalDestinationB = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_RUN_FINISHED],
            scope: EventDestinationScope.PLATFORM,
            url: `${webhookUrlPrefix}/${flowBId}`,
        })
        await db.save('event_destination', [internalDestinationA, internalDestinationB])

        await eventDestinationService(app.log).trigger({
            event: buildFlowRunEvent({ platformId: ctx.platform.id, flowId: flowAId }),
        })

        expect(addSpy).not.toHaveBeenCalled()
    })

    it('should dispatch FLOW_RUN_FINISHED to a PROJECT scope destination matching the project', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            projectId: ctx.project.id,
            events: [ApplicationEventName.FLOW_RUN_FINISHED],
            scope: EventDestinationScope.PROJECT,
            url: 'https://example.com/project-scope',
        })
        await db.save('event_destination', destination)

        await eventDestinationService(app.log).trigger({
            projectId: ctx.project.id,
            event: buildFlowRunEvent({ platformId: ctx.platform.id, projectId: ctx.project.id }),
        })

        expect(addSpy).toHaveBeenCalledTimes(1)
        expect(addSpy).toHaveBeenCalledWith(
            expect.objectContaining({
                data: expect.objectContaining({ webhookUrl: destination.url }),
            }),
        )
    })

    it('should NOT dispatch to a PROJECT scope destination when projectId differs', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const otherCtx = await createTestContext(app, { platform: { id: ctx.platform.id } })
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            projectId: ctx.project.id,
            events: [ApplicationEventName.FLOW_RUN_FINISHED],
            scope: EventDestinationScope.PROJECT,
            url: 'https://example.com/project-a',
        })
        await db.save('event_destination', destination)

        await eventDestinationService(app.log).trigger({
            projectId: otherCtx.project.id,
            event: buildFlowRunEvent({ platformId: ctx.platform.id, projectId: otherCtx.project.id }),
        })

        expect(addSpy).not.toHaveBeenCalled()
    })

    it('should NOT fan out non-FLOW_RUN_FINISHED events to PROJECT scope destinations', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            projectId: ctx.project.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PROJECT,
            url: 'https://example.com/project-non-finished',
        })
        await db.save('event_destination', destination)

        await eventDestinationService(app.log).trigger({
            projectId: ctx.project.id,
            event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id, projectId: ctx.project.id }),
        })

        expect(addSpy).not.toHaveBeenCalled()
    })

    it('should ship the full event (action + data + envelope) as the queued payload', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
        })
        await db.save('event_destination', destination)

        const flowId = apId()
        const event = buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id, flowId })
        await eventDestinationService(app.log).trigger({
            event,
        })

        expect(addSpy).toHaveBeenCalledTimes(1)
        expect(addSpy).toHaveBeenCalledWith(
            expect.objectContaining({
                data: expect.objectContaining({
                    payload: expect.objectContaining({
                        action: ApplicationEventName.FLOW_CREATED,
                        data: expect.objectContaining({ flow: expect.objectContaining({ id: flowId }) }),
                        platformId: ctx.platform.id,
                    }),
                }),
            }),
        )
    })

    it('should queue a RAW destination without headers with the same job fields as before formats existed', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
        })
        await db.save('event_destination', destination)

        const event = buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id })
        await eventDestinationService(app.log).trigger({ event })

        const jobData = addSpy.mock.calls[0][0].data
        expect(jobData).not.toHaveProperty('contentType')
        expect(jobData).not.toHaveProperty('hasHeaders')
        expect(jobData.payload).toEqual(event)
    })

    it('should queue the OTLP/JSON request without a content type for an OTLP_JSON destination', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
            format: EventDestinationFormat.OTLP_JSON,
        })
        await db.save('event_destination', destination)

        const event = buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id })
        await eventDestinationService(app.log).trigger({ event })

        const jobData = addSpy.mock.calls[0][0].data
        expect(jobData).not.toHaveProperty('contentType')
        const record = jobData.payload.resourceLogs[0].scopeLogs[0].logRecords[0]
        expect(record.eventName).toBe(ApplicationEventName.FLOW_CREATED)
        expect(JSON.parse(record.body.stringValue)).toEqual(event)
    })

    it('should mark an OTLP_PROTOBUF job with the protobuf content type and keep the JSON request as its payload', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
            format: EventDestinationFormat.OTLP_PROTOBUF,
        })
        await db.save('event_destination', destination)

        await eventDestinationService(app.log).trigger({
            event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
        })

        const jobData = addSpy.mock.calls[0][0].data
        expect(jobData.contentType).toBe('application/x-protobuf')
        expect(jobData.payload.resourceLogs).toHaveLength(1)
    })

    it('should tell the worker to resolve headers only for a destination that stores them', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const withHeaders = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
            url: 'https://example.com/with-headers',
            headers: { url: 'https://example.com/with-headers', values: { Authorization: await encryptUtils.encryptString(QUEUED_JOB_SECRET) } },
        })
        const withoutHeaders = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
            url: 'https://example.com/without-headers',
        })
        await db.save('event_destination', [withHeaders, withoutHeaders])

        await eventDestinationService(app.log).trigger({
            event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
        })

        const jobsByUrl = new Map(addSpy.mock.calls.map(([job]) => [job.data.webhookUrl, job.data]))
        expect(jobsByUrl.get(withHeaders.url)).toMatchObject({ hasHeaders: true })
        expect(jobsByUrl.get(withoutHeaders.url)).not.toHaveProperty('hasHeaders')
    })

    it('regression: ensure that we have setup the event streaming listeners', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const workerDestination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_RUN_FINISHED],
            scope: EventDestinationScope.PLATFORM,
            url: 'https://example.com/worker-listener-regression',
        })
        const userDestination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
            url: 'https://example.com/user-listener-regression',
        })
        await db.save('event_destination', [workerDestination, userDestination])

        applicationEvents(app.log).sendWorkerEvent({
            projectId: ctx.project.id,
            platformId: ctx.platform.id,
            action: ApplicationEventName.FLOW_RUN_FINISHED,
            data: {
                flowRun: {
                    id: apId(),
                    environment: 'PRODUCTION',
                    flowId: apId(),
                    flowVersionId: apId(),
                    stepNameToTest: null,
                    status: 'SUCCEEDED',
                },
                project: { displayName: 'Test' },
            },
        })
        applicationEvents(app.log).sendUserEvent({ platformId: ctx.platform.id }, {
            action: ApplicationEventName.FLOW_CREATED,
            data: {
                flow: {
                    id: apId(),
                    created: new Date().toISOString(),
                    updated: new Date().toISOString(),
                },
                project: { displayName: 'Test' },
            },
        })

        await vi.waitFor(() => expect(addSpy).toHaveBeenCalledTimes(2), { timeout: 2000 })
        expect(addSpy).toHaveBeenCalledWith(
            expect.objectContaining({
                data: expect.objectContaining({ webhookUrl: workerDestination.url }),
            }),
        )
        expect(addSpy).toHaveBeenCalledWith(
            expect.objectContaining({
                data: expect.objectContaining({ webhookUrl: userDestination.url }),
            }),
        )
    })

    it('should keep every webhook-flow destination when the flow that ran an agent action is not wired as one', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const anotherFlowDestination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.AGENT_ACTION_EXECUTED],
            scope: EventDestinationScope.PLATFORM,
            url: `https://other-instance.example.com/api/v1/webhooks/${apId()}`,
        })
        const externalDestination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.AGENT_ACTION_EXECUTED],
            scope: EventDestinationScope.PLATFORM,
            url: 'https://hooks.slack.example.com/services/abc',
        })
        await db.save('event_destination', [anotherFlowDestination, externalDestination])

        await eventDestinationService(app.log).trigger({
            event: buildAgentActionEvent({ platformId: ctx.platform.id, flow: { id: apId(), runId: apId() } }),
        })

        const dispatchedUrls = addSpy.mock.calls.map(([job]) => job.data.webhookUrl)
        expect(dispatchedUrls.sort()).toEqual([anotherFlowDestination.url, externalDestination.url].sort())
    })

    it('should drop both webhook-flow destinations when two flows running agent actions are mutually wired (A<->B cycle), and still fire externals', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const flowAId = apId()
        const flowBId = apId()
        const flowADestination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.AGENT_ACTION_EXECUTED],
            scope: EventDestinationScope.PLATFORM,
            url: `https://other-instance.example.com/api/v1/webhooks/${flowAId}`,
        })
        const flowBDestination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.AGENT_ACTION_EXECUTED],
            scope: EventDestinationScope.PLATFORM,
            url: `https://other-instance.example.com/api/v1/webhooks/${flowBId}`,
        })
        const externalDestination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.AGENT_ACTION_EXECUTED],
            scope: EventDestinationScope.PLATFORM,
            url: 'https://hooks.slack.example.com/services/abc',
        })
        await db.save('event_destination', [flowADestination, flowBDestination, externalDestination])

        await eventDestinationService(app.log).trigger({
            event: buildAgentActionEvent({ platformId: ctx.platform.id, flow: { id: flowBId, runId: apId() } }),
        })

        expect(addSpy).toHaveBeenCalledTimes(1)
        expect(addSpy).toHaveBeenCalledWith(expect.objectContaining({
            data: expect.objectContaining({ webhookUrl: externalDestination.url }),
        }))
    })

    it('should drop every webhook-flow destination when an agent action cannot name the flow it ran in', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const webhookFlowDestination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.AGENT_ACTION_EXECUTED],
            scope: EventDestinationScope.PLATFORM,
            url: `https://other-instance.example.com/api/v1/webhooks/${apId()}`,
        })
        const externalDestination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.AGENT_ACTION_EXECUTED],
            scope: EventDestinationScope.PLATFORM,
            url: 'https://hooks.slack.example.com/services/abc',
        })
        await db.save('event_destination', [webhookFlowDestination, externalDestination])

        await eventDestinationService(app.log).trigger({
            event: buildAgentActionEvent({ platformId: ctx.platform.id }),
        })

        expect(addSpy).toHaveBeenCalledTimes(1)
        expect(addSpy).toHaveBeenCalledWith(expect.objectContaining({
            data: expect.objectContaining({ webhookUrl: externalDestination.url }),
        }))
    })

    it('should NOT skip a same-host destination for non flow-run events (e.g. FLOW_CREATED)', async () => {
        const ctx = await createTestContext(app, ENTITLED_PLAN)
        const flowId = apId()
        const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
            path: 'v1/webhooks',
        })
        const destination = createMockEventDestination({
            platformId: ctx.platform.id,
            events: [ApplicationEventName.FLOW_CREATED],
            scope: EventDestinationScope.PLATFORM,
            url: `${webhookUrlPrefix}/${flowId}`,
        })
        await db.save('event_destination', destination)

        await eventDestinationService(app.log).trigger({
            event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id, flowId }),
        })

        expect(addSpy).not.toHaveBeenCalled()
        expect(handleWebhookSpy).toHaveBeenCalledTimes(1)
        expect(handleWebhookSpy).toHaveBeenCalledWith(
            expect.objectContaining({ flowId }),
        )
    })

    describe('internal same-origin dispatch (GIT-1539)', () => {
        it('should dispatch internally to the handler flow instead of queueing an outbound HTTP job', async () => {
            const ctx = await createTestContext(app, ENTITLED_PLAN)
            const flowId = apId()
            const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
                path: 'v1/webhooks',
            })
            const destination = createMockEventDestination({
                platformId: ctx.platform.id,
                events: [ApplicationEventName.FLOW_CREATED],
                scope: EventDestinationScope.PLATFORM,
                url: `${webhookUrlPrefix}/${flowId}`,
            })
            await db.save('event_destination', destination)

            const event = buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id })
            await eventDestinationService(app.log).trigger({ event })

            expect(addSpy).not.toHaveBeenCalled()
            expect(handleWebhookSpy).toHaveBeenCalledTimes(1)
            expect(handleWebhookSpy).toHaveBeenCalledWith(
                expect.objectContaining({
                    flowId,
                    async: true,
                    execute: true,
                    saveSampleData: false,
                    failParentOnFailure: false,
                }),
            )
            const handleWebhookParams = handleWebhookSpy.mock.calls[0][0]
            const payload = await handleWebhookParams.data(ctx.project.id)
            expect(payload).toEqual({
                method: 'POST',
                headers: { 'content-type': 'application/json' },
                body: event,
                queryParams: {},
            })
        })

        it('should send the OTLP/JSON request to an internal handler flow for the OTLP_JSON format', async () => {
            const ctx = await createTestContext(app, ENTITLED_PLAN)
            const flowId = apId()
            const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
                path: 'v1/webhooks',
            })
            const destination = createMockEventDestination({
                platformId: ctx.platform.id,
                events: [ApplicationEventName.FLOW_CREATED],
                scope: EventDestinationScope.PLATFORM,
                url: `${webhookUrlPrefix}/${flowId}`,
                format: EventDestinationFormat.OTLP_JSON,
            })
            await db.save('event_destination', destination)

            await eventDestinationService(app.log).trigger({
                event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
            })

            expect(addSpy).not.toHaveBeenCalled()
            const payload = await handleWebhookSpy.mock.calls[0][0].data(ctx.project.id)
            expect(payload.headers).toEqual({ 'content-type': 'application/json' })
            expect(payload.body.resourceLogs[0].scopeLogs[0].logRecords[0].eventName).toBe(ApplicationEventName.FLOW_CREATED)
        })

        it('should forward the stored headers to the internal handler flow with lowercase names, as an HTTP delivery would', async () => {
            const ctx = await createTestContext(app, ENTITLED_PLAN)
            const flowId = apId()
            const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
                path: 'v1/webhooks',
            })
            const handlerFlowUrl = `${webhookUrlPrefix}/${flowId}`
            const destination = createMockEventDestination({
                platformId: ctx.platform.id,
                events: [ApplicationEventName.FLOW_CREATED],
                scope: EventDestinationScope.PLATFORM,
                url: handlerFlowUrl,
                headers: { url: handlerFlowUrl, values: { Authorization: await encryptUtils.encryptString(INTERNAL_PATH_SECRET) } },
            })
            await db.save('event_destination', destination)

            await eventDestinationService(app.log).trigger({
                event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
            })

            const payload = await handleWebhookSpy.mock.calls[0][0].data(ctx.project.id)
            expect(payload.headers).toEqual({
                authorization: INTERNAL_PATH_SECRET,
                'content-type': 'application/json',
            })
            expect(addSpy).not.toHaveBeenCalled()
        })

        it('should not hand the stored headers to an internal handler flow when they belong to another URL', async () => {
            const ctx = await createTestContext(app, ENTITLED_PLAN)
            const flowId = apId()
            const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
                path: 'v1/webhooks',
            })
            const destination = createMockEventDestination({
                platformId: ctx.platform.id,
                events: [ApplicationEventName.FLOW_CREATED],
                scope: EventDestinationScope.PLATFORM,
                url: `${webhookUrlPrefix}/${flowId}`,
                headers: { url: DESTINATION_URL, values: { Authorization: await encryptUtils.encryptString(INTERNAL_PATH_SECRET) } },
            })
            await db.save('event_destination', destination)

            await eventDestinationService(app.log).trigger({
                event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
            })

            expect(handleWebhookSpy).not.toHaveBeenCalled()
            expect(addSpy).not.toHaveBeenCalled()
        })

        it('should dispatch internally when the internal URL carries a path suffix (e.g. /sync)', async () => {
            const ctx = await createTestContext(app, ENTITLED_PLAN)
            const flowId = apId()
            const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
                path: 'v1/webhooks',
            })
            const destination = createMockEventDestination({
                platformId: ctx.platform.id,
                events: [ApplicationEventName.FLOW_CREATED],
                scope: EventDestinationScope.PLATFORM,
                url: `${webhookUrlPrefix}/${flowId}/sync`,
            })
            await db.save('event_destination', destination)

            await eventDestinationService(app.log).trigger({
                event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
            })

            expect(addSpy).not.toHaveBeenCalled()
            expect(handleWebhookSpy).toHaveBeenCalledTimes(1)
            expect(handleWebhookSpy).toHaveBeenCalledWith(
                expect.objectContaining({ flowId }),
            )
        })

        it('should dispatch same-origin /draft and /test route URLs internally with their own version/execute semantics', async () => {
            const ctx = await createTestContext(app, ENTITLED_PLAN)
            const draftFlowId = apId()
            const testFlowId = apId()
            const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
                path: 'v1/webhooks',
            })
            const draftDestination = createMockEventDestination({
                platformId: ctx.platform.id,
                events: [ApplicationEventName.FLOW_CREATED],
                scope: EventDestinationScope.PLATFORM,
                url: `${webhookUrlPrefix}/${draftFlowId}/draft`,
            })
            const testRouteDestination = createMockEventDestination({
                platformId: ctx.platform.id,
                events: [ApplicationEventName.FLOW_CREATED],
                scope: EventDestinationScope.PLATFORM,
                url: `${webhookUrlPrefix}/${testFlowId}/test`,
            })
            await db.save('event_destination', [draftDestination, testRouteDestination])

            await eventDestinationService(app.log).trigger({
                event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
            })

            expect(addSpy).not.toHaveBeenCalled()
            expect(handleWebhookSpy).toHaveBeenCalledTimes(2)
            expect(handleWebhookSpy).toHaveBeenCalledWith(
                expect.objectContaining({
                    flowId: draftFlowId,
                    flowVersionToRun: WebhookFlowVersionToRun.LATEST,
                    saveSampleData: true,
                    execute: true,
                }),
            )
            expect(handleWebhookSpy).toHaveBeenCalledWith(
                expect.objectContaining({
                    flowId: testFlowId,
                    flowVersionToRun: WebhookFlowVersionToRun.LATEST,
                    saveSampleData: true,
                    execute: false,
                }),
            )
        })

        it('should drop a self-targeting same-origin destination on its own flow-run event (cycle guard)', async () => {
            const ctx = await createTestContext(app, ENTITLED_PLAN)
            const flowId = apId()
            const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
                path: 'v1/webhooks',
            })
            const destination = createMockEventDestination({
                platformId: ctx.platform.id,
                events: [ApplicationEventName.FLOW_RUN_FINISHED],
                scope: EventDestinationScope.PLATFORM,
                url: `${webhookUrlPrefix}/${flowId}/draft`,
            })
            await db.save('event_destination', destination)

            await eventDestinationService(app.log).trigger({
                event: buildFlowRunEvent({ platformId: ctx.platform.id, flowId }),
            })

            expect(addSpy).not.toHaveBeenCalled()
            expect(handleWebhookSpy).not.toHaveBeenCalled()
        })

        it('should forward the destination URL query params to the internal handler flow', async () => {
            const ctx = await createTestContext(app, ENTITLED_PLAN)
            const flowId = apId()
            const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
                path: 'v1/webhooks',
            })
            const destination = createMockEventDestination({
                platformId: ctx.platform.id,
                events: [ApplicationEventName.FLOW_CREATED],
                scope: EventDestinationScope.PLATFORM,
                url: `${webhookUrlPrefix}/${flowId}?token=abc&channel=alerts`,
            })
            await db.save('event_destination', destination)

            const event = buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id })
            await eventDestinationService(app.log).trigger({ event })

            expect(addSpy).not.toHaveBeenCalled()
            expect(handleWebhookSpy).toHaveBeenCalledTimes(1)
            const handleWebhookParams = handleWebhookSpy.mock.calls[0][0]
            const payload = await handleWebhookParams.data(ctx.project.id)
            expect(payload).toEqual({
                method: 'POST',
                headers: { 'content-type': 'application/json' },
                body: event,
                queryParams: { token: 'abc', channel: 'alerts' },
            })
        })

        it('should NOT bypass outbound delivery for a webhook-shaped path on a different origin', async () => {
            const ctx = await createTestContext(app, ENTITLED_PLAN)
            const destination = createMockEventDestination({
                platformId: ctx.platform.id,
                events: [ApplicationEventName.FLOW_CREATED],
                scope: EventDestinationScope.PLATFORM,
                url: `https://attacker.example.com/api/v1/webhooks/${apId()}`,
            })
            await db.save('event_destination', destination)

            await eventDestinationService(app.log).trigger({
                event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
            })

            expect(handleWebhookSpy).not.toHaveBeenCalled()
            expect(addSpy).toHaveBeenCalledTimes(1)
            expect(addSpy).toHaveBeenCalledWith(
                expect.objectContaining({
                    data: expect.objectContaining({
                        webhookUrl: destination.url,
                        jobType: WorkerJobType.EVENT_DESTINATION,
                    }),
                }),
            )
        })

        it('should NOT bypass outbound delivery for a same-origin URL outside the webhook path prefix', async () => {
            const ctx = await createTestContext(app, ENTITLED_PLAN)
            const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
                path: 'v1/webhooks',
            })
            const origin = new URL(webhookUrlPrefix).origin
            const lookalikePathUrl = `${origin}/api/v1/webhooks-evil/${apId()}`
            const destination = createMockEventDestination({
                platformId: ctx.platform.id,
                events: [ApplicationEventName.FLOW_CREATED],
                scope: EventDestinationScope.PLATFORM,
                url: lookalikePathUrl,
            })
            await db.save('event_destination', destination)

            await eventDestinationService(app.log).trigger({
                event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
            })

            expect(handleWebhookSpy).not.toHaveBeenCalled()
            expect(addSpy).toHaveBeenCalledTimes(1)
            expect(addSpy).toHaveBeenCalledWith(
                expect.objectContaining({
                    data: expect.objectContaining({ webhookUrl: lookalikePathUrl }),
                }),
            )
        })

        it('should surface (not throw) when the internal handler flow rejects the event', async () => {
            const ctx = await createTestContext(app, ENTITLED_PLAN)
            const flowId = apId()
            const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
                path: 'v1/webhooks',
            })
            handleWebhookSpy.mockResolvedValue({ status: 410, body: {}, headers: {} })
            const destination = createMockEventDestination({
                platformId: ctx.platform.id,
                events: [ApplicationEventName.FLOW_CREATED],
                scope: EventDestinationScope.PLATFORM,
                url: `${webhookUrlPrefix}/${flowId}`,
            })
            await db.save('event_destination', destination)

            await expect(eventDestinationService(app.log).trigger({
                event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
            })).resolves.not.toThrow()

            expect(handleWebhookSpy).toHaveBeenCalledTimes(1)
            expect(addSpy).not.toHaveBeenCalled()
        })

        it('test() should dispatch internally for an internal URL and deliver synchronously for an external URL', async () => {
            const ctx = await createTestContext(app, ENTITLED_PLAN)
            const flowId = apId()
            const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
                path: 'v1/webhooks',
            })

            const internalResult = await eventDestinationService(app.log).test({
                platformId: ctx.platform.id,
                url: `${webhookUrlPrefix}/${flowId}`,
            })
            expect(handleWebhookSpy).toHaveBeenCalledTimes(1)
            expect(handleWebhookSpy).toHaveBeenCalledWith(
                expect.objectContaining({ flowId, async: true }),
            )
            expect(internalResult.renderedBody).toMatchObject({ action: ApplicationEventName.FLOW_CREATED })
            expect(addSpy).not.toHaveBeenCalled()

            const externalResult = await eventDestinationService(app.log).test({
                platformId: ctx.platform.id,
                url: 'http://127.0.0.1:1/external-hook',
            })
            expect(addSpy).not.toHaveBeenCalled()
            expect(externalResult.errorCode).toBeDefined()
            expect(externalResult.status).toBeUndefined()
            expect(externalResult.renderedBody).toMatchObject({ action: ApplicationEventName.FLOW_CREATED })
        })

        it('test() should forward the typed headers to an internal handler flow with lowercase names', async () => {
            const ctx = await createTestContext(app, ENTITLED_PLAN)
            const flowId = apId()
            const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
                path: 'v1/webhooks',
            })

            await eventDestinationService(app.log).test({
                platformId: ctx.platform.id,
                url: `${webhookUrlPrefix}/${flowId}`,
                headers: { 'X-Handler-Key': 'typed-by-the-caller' },
            })

            const payload = await handleWebhookSpy.mock.calls[0][0].data(ctx.project.id)
            expect(payload.headers).toEqual({
                'x-handler-key': 'typed-by-the-caller',
                'content-type': 'application/json',
            })
        })
    })
})