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.

7.1 KB
Raw Blame
import { FEATURE_FLAGS } from 'lib/constants'

import { matchesFlagDefinition } from './flagGating'
import { SETTINGS_MAP } from './SettingsMap'
import { buildSettingsSearchIndex, createSettingsSearchFuse, searchSettingsIndex } from './settingsSearch'
import { Setting, SettingSection } from './types'

const SECTIONS: SettingSection[] = [
    {
        level: 'environment',
        id: 'environment-details',
        title: 'General',
        settings: [
            {
                id: 'variables',
                title: 'Project token & ID',
                description: 'Your project token and ID used to connect SDKs and APIs to this environment.',
                component: <div />,
                keywords: ['api key', 'token', 'project id'],
            },
            {
                id: 'js-snippet-version',
                title: (
                    <>
                        Snippet version <span>Experimental</span>
                    </>
                ),
                description: 'Pin the snippet to a specific version of posthog-js.',
                component: <div />,
                keywords: ['pin', 'posthog-js'],
            },
        ],
    },
    {
        level: 'environment',
        id: 'environment-replay',
        title: 'Session replay',
        settings: [
            {
                id: 'replay-retention',
                title: 'Data retention',
                description: 'Control how long your recordings are stored.',
                component: <div />,
                keywords: ['storage', 'retention', 'delete', 'days', 'months'],
            },
        ],
    },
    {
        level: 'organization',
        id: 'organization-details',
        title: 'General',
        settings: [
            {
                id: 'organization-ai-consent',
                title: 'AI service providers',
                component: <div />,
                keywords: ['ai', 'max', 'llm', 'consent', 'approve', 'enable', 'opt-in', 'data sharing'],
                searchDescription: 'PostHog AI features use external AI services for data analysis.',
            },
            {
                id: 'organization-ai-training-opt-out',
                title: 'Internal AI training',
                component: <div />,
                keywords: ['ai', 'training', 'opt-out', 'opt-in', 'model', 'max'],
            },
        ],
    },
    {
        level: 'organization',
        id: 'organization-billing',
        title: 'Billing',
        to: '/organization/billing',
        settings: [],
        keywords: ['usage', 'subscription', 'invoice', 'plan', 'payment', 'spend', 'quota', 'credits', 'card'],
    },
]

const search = (term: string, isSettingVisible: (setting: Setting) => boolean = () => true): string[] => {
    const fuse = createSettingsSearchFuse(buildSettingsSearchIndex(SECTIONS, isSettingVisible))
    return searchSettingsIndex(fuse, term).map((entry) => entry.settingId)
}

describe('settingsSearch', () => {
    test.each([
        ['a single word', 'retention', 'replay-retention'],
        ['a trailing space', 'project id ', 'variables'],
        ['a leading space', ' project id', 'variables'],
        ['a repeated space', 'project  id', 'variables'],
        ['trailing punctuation', 'project id?', 'variables'],
        ['words split across the title and the section title', 'session replay retention', 'replay-retention'],
        ['a keyword late in the keyword list', 'max', 'organization-ai-training-opt-out'],
        ['an action phrased query', 'enable AI', 'organization-ai-consent'],
        ['a synonym on a section that has no settings', 'usage', 'organization-billing'],
        ['a word the index does not carry alongside one it does', 'usage dashboard', 'organization-billing'],
    ])('finds a setting by %s', (_description, term, expectedSettingId) => {
        expect(search(term)).toContain(expectedSettingId)
    })

    it('indexes a JSX title by the words it shows, not by its id', () => {
        const entries = buildSettingsSearchIndex(SECTIONS, () => true)
        const snippetVersion = entries.find((entry) => entry.settingId === 'js-snippet-version')

        expect(snippetVersion?.settingTitle).toBe('Snippet version Experimental')
        expect(search('snippet version')).toContain('js-snippet-version')
    })

    it('leaves out a setting that is not visible to this user', () => {
        const isVisible = (setting: Setting): boolean => setting.id !== 'organization-ai-training-opt-out'

        expect(search('internal AI training', isVisible)).not.toContain('organization-ai-training-opt-out')
    })

    it('returns nothing for a term that is only whitespace', () => {
        expect(search('   ')).toEqual([])
    })

    // Integration guides ask for the public project token under several names, and without
    // those synonyms a search for one of them puts personal API keys on top instead. Searches
    // the shipped map, not a fixture, because the synonyms have to stay on the setting itself.
    test.each(['client api key', 'public api key', 'write key'])('puts the project token first for "%s"', (term) => {
        const visible = (definition: Pick<Setting, 'flag'>): boolean => matchesFlagDefinition(definition.flag, {})
        const fuse = createSettingsSearchFuse(buildSettingsSearchIndex(SETTINGS_MAP.filter(visible), visible))

        expect(searchSettingsIndex(fuse, term)[0]?.settingId).toBe('variables')
    })
    // The today-rail-nav redesign renames some sections by keeping their id and gating two copies on
    // the flag and its negation. Both copies visible at once would render the section twice.
    test.each([
        ['on', { [FEATURE_FLAGS.TODAY_RAIL_NAV]: true }],
        ['off', {}],
    ])('shows each section once with today-rail-nav %s', (_state, flags) => {
        const allOtherFlags = Object.fromEntries(
            Object.values(FEATURE_FLAGS)
                .filter((flag) => flag !== FEATURE_FLAGS.TODAY_RAIL_NAV)
                .map((flag) => [flag, true])
        )
        const visibleIds = SETTINGS_MAP.filter((section) =>
            matchesFlagDefinition(section.flag, { ...allOtherFlags, ...flags })
        ).map((section) => section.id)

        expect(visibleIds.length).toBe(new Set(visibleIds).size)
    })

    test.each([
        ['model preferences', 'task-agent-my-preference'],
        ['subscriptions', 'ai-subscription-codex'],
        ['personalization', 'task-agent-my-instructions'],
        ['plan & usage', 'ai-usage-spend'],
        ['worktrees', 'task-agent-other-settings'],
        ['self-driving', 'task-agent-other-settings'],
    ])('finds a setting by its PostHog Desktop name "%s" with today-rail-nav on', (term, expectedSettingId) => {
        const flags = {
            [FEATURE_FLAGS.TODAY_RAIL_NAV]: true,
            [FEATURE_FLAGS.POSTHOG_CODE_CODEX_OWN_SUBSCRIPTION_CLOUD]: true,
        }
        const visible = (definition: Pick<Setting, 'flag'>): boolean => matchesFlagDefinition(definition.flag, flags)
        const fuse = createSettingsSearchFuse(buildSettingsSearchIndex(SETTINGS_MAP.filter(visible), visible))

        expect(searchSettingsIndex(fuse, term).map((entry) => entry.settingId)).toContain(expectedSettingId)
    })
})