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.

15.7 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest'

import { isFeatureFlagEnabled } from '@/lib/posthog/flags'
import type { Context } from '@/tools/types'
import { workflowsBlastRadius, workflowsRunBatch, workflowsScheduleCreate } from '@/tools/workflows/batch'

vi.mock('@/lib/posthog/flags', () => ({
    isFeatureFlagEnabled: vi.fn().mockResolvedValue(false),
}))

// Default per-team audience cap the mocked blast-radius endpoint returns unless a test overrides it.
const DEFAULT_LIMIT = 50_000

interface RequestArgs {
    method: string
    path: string
    body?: Record<string, unknown>
}

const BATCH_FILTERS = { properties: [{ key: 'email', value: 'is_set', type: 'person' }] }

/**
 * Mock context whose api.request dispatches by method+path:
 *  - GET  .../hog_flows/{id}/         -> the workflow (with the given trigger)
 *  - POST .../hog_flows/user_blast_radius/ -> the given blast radius
 *  - POST .../batch_jobs/ or .../schedules/ -> echoes the created resource
 */
function createMockContext(opts: {
    trigger: unknown
    blastRadius: { affected: number; total: number; limit?: number }
    status?: string
    actions?: unknown[]
}): { context: Context; request: ReturnType<typeof vi.fn> } {
    const request = vi.fn(async ({ method, path }: RequestArgs) => {
        if (method === 'GET' && /\/hog_flows\/[^/]+\/$/.test(path)) {
            return { id: 'wf-1', status: opts.status ?? 'active', trigger: opts.trigger, actions: opts.actions ?? [] }
        }
        if (method === 'POST' && path.endsWith('/user_blast_radius/')) {
            return { limit: DEFAULT_LIMIT, confirm_token: 'tok-preview', ...opts.blastRadius }
        }
        if (method === 'POST' && path.endsWith('/batch_jobs/')) {
            return { id: 'batch-1', status: 'queued' }
        }
        if (method === 'POST' && path.endsWith('/schedules/')) {
            return { id: 'sched-1', status: 'active' }
        }
        throw new Error(`unexpected request: ${method} ${path}`)
    })

    const context = {
        api: { request },
        stateManager: {
            getProjectId: vi.fn().mockResolvedValue('1'),
            getAnalyticsContext: vi.fn().mockResolvedValue({ organizationId: 'org-1', projectUuid: 'project-uuid-1' }),
        },
        getDistinctId: vi.fn().mockResolvedValue('user-1'),
    } as unknown as Context

    return { context, request }
}

function calls(request: ReturnType<typeof vi.fn>): RequestArgs[] {
    return request.mock.calls.map((c) => c[0] as RequestArgs)
}

const blastRadiusTool = workflowsBlastRadius()
const runBatchTool = workflowsRunBatch()
const scheduleCreateTool = workflowsScheduleCreate()

describe('workflows batch handlers', () => {
    beforeEach(() => {
        vi.clearAllMocks()
        vi.mocked(isFeatureFlagEnabled).mockResolvedValue(false)
    })

    describe('workflows-blast-radius', () => {
        it.each([
            ['an email step', [{ type: 'function_email' }], true],
            ['an email template step', [{ type: 'function', config: { template_id: 'template-email' } }], true],
            ['only webhook steps', [{ type: 'function', config: { template_id: 'template-webhook' } }], false],
        ])('sizes the trigger filters for a workflow with %s', async (_, actions, sendsEmail) => {
            const { context, request } = createMockContext({
                trigger: { type: 'batch', filters: BATCH_FILTERS },
                blastRadius: { affected: 42, total: 100 },
                actions,
            })

            const result = await blastRadiusTool.handler(context, { workflow_id: 'wf-1' })

            const c = calls(request)
            expect(c[0]).toMatchObject({ method: 'GET', path: '/api/projects/1/hog_flows/wf-1/' })
            expect(c[1]).toMatchObject({
                method: 'POST',
                path: '/api/projects/1/hog_flows/user_blast_radius/',
                body: { filters: BATCH_FILTERS, sends_email: sendsEmail },
            })
            // confirm_token must surface to the agent: it is a required workflows-run-batch input.
            expect(result).toEqual({ affected: 42, total: 100, limit: DEFAULT_LIMIT, confirm_token: 'tok-preview' })
        })
    })

    describe('workflows-run-batch', () => {
        it('fires the batch job with the trigger filters when the acknowledged count matches', async () => {
            const { context, request } = createMockContext({
                trigger: { type: 'batch', filters: BATCH_FILTERS },
                blastRadius: { affected: 10, total: 100 },
            })

            const result = await runBatchTool.handler(context, {
                workflow_id: 'wf-1',
                acknowledged_affected_count: 10,
                confirm_token: 'tok-123',
                variables: { plan: 'pro' },
            })

            const batchCall = calls(request).find((c) => c.path.endsWith('/batch_jobs/'))
            expect(batchCall).toMatchObject({
                method: 'POST',
                path: '/api/projects/1/hog_flows/wf-1/batch_jobs/',
                body: { filters: BATCH_FILTERS, confirm_token: 'tok-123', variables: { plan: 'pro' } },
            })
            expect(result).toMatchObject({ id: 'batch-1' })
        })

        it('omits variables from the body when none are given', async () => {
            const { context, request } = createMockContext({
                trigger: { type: 'batch', filters: BATCH_FILTERS },
                blastRadius: { affected: 10, total: 100 },
            })

            await runBatchTool.handler(context, {
                workflow_id: 'wf-1',
                acknowledged_affected_count: 10,
                confirm_token: 'tok-123',
            })

            const batchCall = calls(request).find((c) => c.path.endsWith('/batch_jobs/'))
            expect(batchCall!.body).toEqual({ filters: BATCH_FILTERS, confirm_token: 'tok-123' })
        })

        it('rejects and does not fire when the acknowledged count is stale, surfacing the fresh count', async () => {
            const { context, request } = createMockContext({
                trigger: { type: 'batch', filters: BATCH_FILTERS },
                blastRadius: { affected: 25, total: 100 },
            })

            await expect(
                runBatchTool.handler(context, {
                    workflow_id: 'wf-1',
                    acknowledged_affected_count: 10,
                    confirm_token: 'tok-123',
                })
            ).rejects.toThrow(/25/)
            expect(calls(request).some((c) => c.path.endsWith('/batch_jobs/'))).toBe(false)
        })

        it.each([
            {
                name: 'an email workflow with the flag on for its project',
                actions: [{ type: 'function_email' }],
                flagOn: true,
                pointsAtTier: true,
            },
            {
                name: 'an email workflow with the flag off',
                actions: [{ type: 'function_email' }],
                flagOn: false,
                pointsAtTier: false,
            },
            { name: 'a non-email workflow', actions: [], flagOn: true, pointsAtTier: false },
        ])(
            'rejects and does not fire when the audience exceeds the per-team cap for $name',
            async ({ actions, flagOn, pointsAtTier }) => {
                // The flag targets the workspace, so it reads as on only when the project group is passed.
                vi.mocked(isFeatureFlagEnabled).mockImplementation(
                    async (_key, _distinctId, groups) => flagOn && groups?.project === 'project-uuid-1'
                )
                const { context, request } = createMockContext({
                    trigger: { type: 'batch', filters: BATCH_FILTERS },
                    blastRadius: { affected: 5001, total: 1_000_000, limit: 5000 },
                    actions,
                })

                const error = await runBatchTool
                    .handler(context, {
                        workflow_id: 'wf-1',
                        acknowledged_affected_count: 5001,
                        confirm_token: 'tok-123',
                    })
                    .catch((e: Error) => e)
                expect(String(error)).toMatch(/5000/)
                expect(String(error).includes('workflows-reputation-get')).toBe(pointsAtTier)
                expect(calls(request).some((c) => c.path.endsWith('/batch_jobs/'))).toBe(false)
            }
        )

        it('fires for a large audience that is within an elevated per-team cap', async () => {
            const { context, request } = createMockContext({
                trigger: { type: 'batch', filters: BATCH_FILTERS },
                blastRadius: { affected: 40_000, total: 1_000_000, limit: 50_000 },
            })

            const result = await runBatchTool.handler(context, {
                workflow_id: 'wf-1',
                acknowledged_affected_count: 40_000,
                confirm_token: 'tok-123',
            })

            expect(calls(request).some((c) => c.path.endsWith('/batch_jobs/'))).toBe(true)
            expect(result).toMatchObject({ id: 'batch-1' })
        })

        it('rejects workflows whose trigger is not a batch trigger', async () => {
            const { context, request } = createMockContext({
                trigger: { type: 'event', filters: BATCH_FILTERS },
                blastRadius: { affected: 10, total: 100 },
            })

            await expect(
                runBatchTool.handler(context, {
                    workflow_id: 'wf-1',
                    acknowledged_affected_count: 10,
                    confirm_token: 'tok-123',
                })
            ).rejects.toThrow(/batch/)
            expect(calls(request).some((c) => c.path.endsWith('/batch_jobs/'))).toBe(false)
        })

        it('rejects a workflow that is not active, without firing', async () => {
            const { context, request } = createMockContext({
                trigger: { type: 'batch', filters: BATCH_FILTERS },
                blastRadius: { affected: 10, total: 100 },
                status: 'draft',
            })

            await expect(
                runBatchTool.handler(context, {
                    workflow_id: 'wf-1',
                    acknowledged_affected_count: 10,
                    confirm_token: 'tok-123',
                })
            ).rejects.toThrow(/enable/i)
            expect(calls(request).some((c) => c.path.endsWith('/batch_jobs/'))).toBe(false)
        })
    })

    describe('workflows-schedule-create', () => {
        it('creates the schedule for a batch trigger when the acknowledged count matches', async () => {
            const { context, request } = createMockContext({
                trigger: { type: 'batch', filters: BATCH_FILTERS },
                blastRadius: { affected: 3, total: 100 },
            })

            const result = await scheduleCreateTool.handler(context, {
                workflow_id: 'wf-1',
                rrule: 'FREQ=DAILY;INTERVAL=1',
                starts_at: '2026-06-01T00:00:00Z',
                timezone: 'UTC',
                acknowledged_affected_count: 3,
                confirm_token: 'tok-preview',
                variables: { plan: 'pro' },
            })

            const schedCall = calls(request).find((c) => c.path.endsWith('/schedules/'))
            expect(schedCall).toMatchObject({
                method: 'POST',
                path: '/api/projects/1/hog_flows/wf-1/schedules/',
                body: {
                    rrule: 'FREQ=DAILY;INTERVAL=1',
                    starts_at: '2026-06-01T00:00:00Z',
                    timezone: 'UTC',
                    confirm_token: 'tok-preview',
                    variables: { plan: 'pro' },
                },
            })
            expect(result).toMatchObject({ id: 'sched-1' })
        })

        it('rejects a stale acknowledged count on a batch trigger without creating the schedule', async () => {
            const { context, request } = createMockContext({
                trigger: { type: 'batch', filters: BATCH_FILTERS },
                blastRadius: { affected: 50, total: 100 },
            })

            await expect(
                scheduleCreateTool.handler(context, {
                    workflow_id: 'wf-1',
                    rrule: 'FREQ=DAILY;INTERVAL=1',
                    starts_at: '2026-06-01T00:00:00Z',
                    acknowledged_affected_count: 3,
                    confirm_token: 'unused',
                })
            ).rejects.toThrow(/50/)
            expect(calls(request).some((c) => c.path.endsWith('/schedules/'))).toBe(false)
        })

        it('rejects scheduling a draft (non-active) workflow without creating the schedule', async () => {
            const { context, request } = createMockContext({
                trigger: { type: 'batch', filters: BATCH_FILTERS },
                blastRadius: { affected: 3, total: 100 },
                status: 'draft',
            })

            await expect(
                scheduleCreateTool.handler(context, {
                    workflow_id: 'wf-1',
                    rrule: 'FREQ=DAILY;INTERVAL=1',
                    starts_at: '2026-06-01T00:00:00Z',
                    acknowledged_affected_count: 3,
                    confirm_token: 'unused',
                })
            ).rejects.toThrow(/active/i)
            expect(calls(request).some((c) => c.path.endsWith('/schedules/'))).toBe(false)
        })

        it('creates the schedule for a schedule trigger without sizing an audience', async () => {
            const { context, request } = createMockContext({
                trigger: { type: 'schedule' },
                blastRadius: { affected: 1, total: 1 },
                status: 'draft',
            })

            const result = await scheduleCreateTool.handler(context, {
                workflow_id: 'wf-1',
                rrule: 'FREQ=WEEKLY;INTERVAL=1;BYDAY=MO',
                starts_at: '2026-06-01T09:00:00Z',
                timezone: 'Europe/Lisbon',
            })

            expect(calls(request).some((c) => c.path.endsWith('/user_blast_radius/'))).toBe(false)
            const schedCall = calls(request).find((c) => c.path.endsWith('/schedules/'))
            expect(schedCall).toMatchObject({
                method: 'POST',
                body: {
                    rrule: 'FREQ=WEEKLY;INTERVAL=1;BYDAY=MO',
                    starts_at: '2026-06-01T09:00:00Z',
                    timezone: 'Europe/Lisbon',
                },
            })
            expect(schedCall?.body).not.toHaveProperty('confirm_token')
            expect(result).toMatchObject({ id: 'sched-1' })
        })

        it('rejects a batch trigger scheduled without the blast-radius confirmation', async () => {
            const { context, request } = createMockContext({
                trigger: { type: 'batch', filters: BATCH_FILTERS },
                blastRadius: { affected: 3, total: 100 },
            })

            await expect(
                scheduleCreateTool.handler(context, {
                    workflow_id: 'wf-1',
                    rrule: 'FREQ=DAILY;INTERVAL=1',
                    starts_at: '2026-06-01T00:00:00Z',
                })
            ).rejects.toThrow(/workflows-blast-radius/)
            expect(calls(request).some((c) => c.path.endsWith('/schedules/'))).toBe(false)
        })

        it('rejects a trigger that cannot be scheduled', async () => {
            const { context, request } = createMockContext({
                trigger: { type: 'event' },
                blastRadius: { affected: 1, total: 1 },
            })

            await expect(
                scheduleCreateTool.handler(context, {
                    workflow_id: 'wf-1',
                    rrule: 'FREQ=DAILY;INTERVAL=1',
                    starts_at: '2026-06-01T00:00:00Z',
                })
            ).rejects.toThrow(/batch.*schedule/)
            expect(calls(request).some((c) => c.path.endsWith('/schedules/'))).toBe(false)
        })
    })
})