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.5 KB
Raw Blame
import { billingJson } from '~/mocks/fixtures/_billing'
import { BillingProductV2Type } from '~/types'

import {
    billingErrorGuidance,
    buildSpendTrackingProperties,
    createGaugeItems,
    filterSpendUsageTypes,
    getSpendTypeOptions,
    getUsageTypeOptions,
} from './billing-utils'
import { BillingGaugeItemKind } from './types'

describe('getUsageTypeOptions', () => {
    it('includes informational Desktop component metrics in Usage but not Spend', () => {
        const usageOptions = getUsageTypeOptions()
        const spendOptions = getSpendTypeOptions()
        const componentTypes = [
            'posthog_code_token_credits_used_in_period',
            'sandbox_compute_credits_used_in_period',
            'sandbox_compute_cpu_millicore_seconds_in_period',
            'sandbox_compute_memory_mib_seconds_in_period',
        ]

        for (const usageType of componentTypes) {
            expect(usageOptions.some((option) => option.key === usageType)).toBe(true)
            expect(spendOptions.some((option) => option.key === usageType)).toBe(false)
        }
    })

    it('reports only selectable Spend types in interaction analytics', () => {
        const properties = buildSpendTrackingProperties('filters_changed', {
            filters: {},
            dateFrom: '2026-08-01',
            dateTo: '2026-08-06',
            excludeEmptySeries: false,
            teamOptions: [],
        })

        expect(properties.usage_types_total).toBe(getSpendTypeOptions().length)
    })

    it('removes Usage-only types when switching to Spend', () => {
        expect(
            filterSpendUsageTypes([
                'posthog_code_token_credits_used_in_period',
                'sandbox_compute_credits_used_in_period',
                'event_count_in_period',
            ])
        ).toEqual(['event_count_in_period'])
        expect(filterSpendUsageTypes(['sandbox_compute_cpu_millicore_seconds_in_period'])).toEqual([])
    })
})

describe('billingErrorGuidance', () => {
    it('says what the page can do, in its own words, for the codes it knows', () => {
        expect(billingErrorGuidance({ code: 'usage_query_timeout', detail: 'api text' })).toMatch(/took too long/)
        expect(billingErrorGuidance({ code: 'usage_breakdown_too_large', detail: 'api text' })).toMatch(
            /too large to show/
        )
    })

    it('never tells a person to ask for pages, which is advice for an API caller', () => {
        for (const code of ['usage_query_timeout', 'usage_breakdown_too_large']) {
            expect(billingErrorGuidance({ code, detail: 'Ask for it a page at a time' })).not.toMatch(/page at a time/)
        }
    })

    it('falls back to billing text for a code it does not know', () => {
        expect(billingErrorGuidance({ code: 'something_new', detail: 'billing said this' })).toBe('billing said this')
    })
})

describe('createGaugeItems', () => {
    const productAnalytics = billingJson.products.find((p) => p.type === 'product_analytics') as BillingProductV2Type

    it.each([
        ['draws the billing limit marker for a product that allows a limit', undefined, true],
        ['draws no billing limit marker for a product without billing limits', true, false],
    ])('%s', (_name, noBillingLimit, expectMarker) => {
        const product: BillingProductV2Type = { ...productAnalytics, no_billing_limit: noBillingLimit }

        const kinds = createGaugeItems(product, { billingLimitAsUsage: 1000000 }).map((item) => item.type)

        expect(kinds.includes(BillingGaugeItemKind.BillingLimit)).toBe(expectMarker)
    })
})