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.

3.1 KB
Raw Blame
import { describe, expect, it } from 'vitest'

import { SessionManager } from '@/lib/SessionManager'
import { getToolsFromContext } from '@/tools'
import type { Context } from '@/tools/types'

const createMockContext = (): Context => ({
    api: {} as any,
    cache: {} as any,
    env: {
        MCP_APPS_BASE_URL: undefined,
        POSTHOG_ANALYTICS_API_KEY: undefined,
        POSTHOG_ANALYTICS_HOST: undefined,
        POSTHOG_API_BASE_URL: undefined,
        POSTHOG_PUBLIC_URL: undefined,
        POSTHOG_MCP_APPS_ANALYTICS_BASE_URL: undefined,
        POSTHOG_UI_APPS_TOKEN: undefined,
    },
    stateManager: {
        getApiKey: async () => ({ scopes: ['*'] }),
        getAiConsentGiven: async () => undefined,
    } as any,
    sessionManager: new SessionManager({} as any),
    getDistinctId: async () => 'test-distinct-id',
    trackEvent: async () => {},
})

describe('Feature Routing Integration', () => {
    const integrationTests = [
        {
            features: undefined,
            description: 'all tools when no features specified',
            expectedTools: [
                'feature-flag-get-definition',
                'dashboard-create',
                'insights-list',
                'organizations-list',
                'query-error-tracking-issues-list',
            ],
        },
        {
            features: ['dashboards'],
            description: 'only dashboard tools',
            expectedTools: [
                'dashboard-create',
                'dashboards-get-all',
                'dashboard-get',
                'dashboard-update',
                'dashboard-delete',
                'dashboard-reorder-tiles',
                'dashboard-transfer-tile',
            ],
        },
        {
            features: ['flags', 'workspace'],
            description: 'tools from multiple features',
            expectedTools: [
                'feature-flag-get-definition',
                'create-feature-flag',
                'feature-flag-get-all',
                'switch-organization',
                'projects-get',
            ],
        },
        {
            // Organization discovery must ship with the workspace navigation tools —
            // otherwise an agent can switch orgs but has no way to enumerate them,
            // stranding any project that lives in a different organization.
            features: ['workspace'],
            description: 'workspace feature includes organization discovery',
            expectedTools: ['organizations-get', 'switch-organization', 'projects-get', 'switch-project'],
        },
        {
            features: ['invalid', 'flags', 'unknown'],
            description: 'valid tools ignoring invalid features',
            expectedTools: ['feature-flag-get-definition', 'create-feature-flag'],
        },
    ]

    it.each(integrationTests)('should return $description', async ({ features, expectedTools }) => {
        const context = createMockContext()
        const tools = await getToolsFromContext(context, { features })
        const toolNames = tools.map((t) => t.name)

        for (const tool of expectedTools) {
            expect(toolNames).toContain(tool)
        }
    })
})