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.

6.2 KB
Raw Blame
import { createHash } from 'node:crypto'
import { beforeEach, describe, expect, it, vi } from 'vitest'

import { AnalyticsEvent } from '@/lib/posthog/analytics'

const mocks = vi.hoisted(() => ({
    capture: vi.fn(),
    getAnalyticsContext: vi.fn(),
    getApiKey: vi.fn(),
    getDistinctId: vi.fn(),
}))

vi.mock('@/lib/posthog', () => ({
    getPostHogClient: () => ({
        capture: mocks.capture,
    }),
}))

vi.mock('@/lib/StateManager', () => ({
    StateManager: class {
        getDistinctId = mocks.getDistinctId
        getAnalyticsContext = mocks.getAnalyticsContext
        getApiKey = mocks.getApiKey
    },
}))

import { buildCliContext } from '@/cli/context'

describe('CLI context', () => {
    beforeEach(() => {
        mocks.capture.mockClear()
        mocks.getDistinctId.mockReset()
        mocks.getAnalyticsContext.mockReset()
        mocks.getApiKey.mockReset()
        mocks.getDistinctId.mockRejectedValue(new Error('offline'))
        mocks.getAnalyticsContext.mockRejectedValue(new Error('offline'))
        mocks.getApiKey.mockRejectedValue(new Error('offline'))
    })

    it.each(['phx_secret-token', 'pha_test-token', undefined])(
        'retains telemetry when identity resolution fails with API key %s',
        async (apiKey) => {
            const context = await buildCliContext({ apiKey, host: 'https://us.posthog.com', version: 2 })

            await expect(context.trackEvent(AnalyticsEvent.MCP_TOOL_CALL)).resolves.toBeUndefined()

            const expectedId = apiKey
                ? `posthog-cli:${createHash('sha256').update(apiKey).digest('hex').slice(0, 16)}`
                : 'posthog-cli:anonymous'
            expect(mocks.capture).toHaveBeenCalledWith(
                expect.objectContaining({
                    distinctId: expectedId,
                    event: AnalyticsEvent.MCP_TOOL_CALL,
                    properties: expect.objectContaining({ is_impersonated: false, suppress_analytics: false }),
                })
            )
            if (apiKey) {
                expect(JSON.stringify(mocks.capture.mock.calls)).not.toContain(apiKey)
            }
        }
    )

    it.each([
        ['posthog-cli by default', undefined, 'posthog-cli', 'posthog_cli'],
        [
            'the CLI that passes its identity',
            { clientName: 'hog', userAgent: 'hog/1.0.0', source: 'hog_cli' },
            'hog',
            'hog_cli',
        ],
    ])('reports %s as the client in analytics', async (_name, identity, clientName, source) => {
        const context = await buildCliContext(
            { apiKey: 'phx_key', host: 'https://us.posthog.com', version: 2 },
            identity
        )

        await context.trackEvent(AnalyticsEvent.MCP_TOOL_CALL)

        expect(mocks.capture).toHaveBeenCalledWith(
            expect.objectContaining({
                distinctId: expect.stringMatching(new RegExp(`^${clientName}:`)),
                properties: expect.objectContaining({
                    $mcp_client_name: clientName,
                    $mcp_consumer: clientName,
                    $mcp_source: source,
                }),
            })
        )
    })

    it('uses an opaque analytics distinct ID for feedback when identity resolution fails', async () => {
        const apiKey = 'phx_secret-token'
        const context = await buildCliContext({ apiKey, host: 'https://us.posthog.com', version: 2 })

        await context.trackEvent(AnalyticsEvent.MCP_FEEDBACK_SUBMITTED)

        const expectedHash = createHash('sha256').update(apiKey).digest('hex').slice(0, 16)
        expect(mocks.capture).toHaveBeenCalledWith(
            expect.objectContaining({
                distinctId: `posthog-cli:${expectedHash}`,
                event: AnalyticsEvent.MCP_FEEDBACK_SUBMITTED,
            })
        )
        expect(JSON.stringify(mocks.capture.mock.calls)).not.toContain(apiKey)
    })

    it('suppresses only the CLI invocation that received private content', async () => {
        const privateContext = await buildCliContext({
            apiKey: 'phx_test-token',
            host: 'https://example.com',
            version: 2,
        })
        const ordinaryContext = await buildCliContext({
            apiKey: 'phx_test-token',
            host: 'https://example.com',
            version: 2,
        })
        privateContext.api.config.onPrivateResponse?.()

        await privateContext.trackEvent(AnalyticsEvent.MCP_TOOL_CALL)
        await ordinaryContext.trackEvent(AnalyticsEvent.MCP_TOOL_CALL)

        expect(mocks.capture.mock.calls.map(([event]) => event.properties.suppress_analytics)).toEqual([true, false])
    })

    it('uses an anonymous analytics distinct ID for feedback without an API key', async () => {
        const context = await buildCliContext({ host: 'https://us.posthog.com', version: 2 })

        await context.trackEvent(AnalyticsEvent.MCP_FEEDBACK_SUBMITTED)

        expect(mocks.capture).toHaveBeenCalledWith(
            expect.objectContaining({
                distinctId: 'posthog-cli:anonymous',
                event: AnalyticsEvent.MCP_FEEDBACK_SUBMITTED,
                properties: expect.objectContaining({ $mcp_scope_preset: 'user' }),
            })
        )
    })

    it.each(Object.values(AnalyticsEvent).flatMap((event) => [true, false].map((value) => [event, value] as const)))(
        'passes trusted capture policy to the SDK for %s with policy %s',
        async (event, impersonated) => {
            mocks.getDistinctId.mockResolvedValue('user-123')
            mocks.getApiKey.mockResolvedValue({
                scopes: [],
                is_impersonated: impersonated,
                suppress_analytics: impersonated,
            })
            const context = await buildCliContext({ host: 'https://us.posthog.com', version: 2 })

            await context.trackEvent(event, { is_impersonated: !impersonated, suppress_analytics: !impersonated })

            expect(mocks.capture).toHaveBeenCalledWith(
                expect.objectContaining({
                    distinctId: 'user-123',
                    event,
                    properties: expect.objectContaining({
                        is_impersonated: impersonated,
                        suppress_analytics: impersonated,
                    }),
                })
            )
        }
    )
})