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 { FacetOption } from './Facet'
import { FacetGroupItem, filterFacetsByName, mergeSelectedIntoOptions } from './facetGroups'

const facet = (key: string, title: string, group: string): FacetGroupItem => ({ key, title, group })

const FACETS: FacetGroupItem[] = [
    facet('level', 'Level', 'Standard'),
    facet('service', 'Service', 'Standard'),
    facet('namespace', 'Namespace', 'Kubernetes'),
    facet('pod', 'Pod', 'Kubernetes'),
    facet('host', 'Host', 'Infrastructure'),
]

describe('facetGroups', () => {
    describe('filterFacetsByName', () => {
        it.each([
            ['blank query returns all', '', ['level', 'service', 'namespace', 'pod', 'host']],
            ['whitespace-only returns all', '   ', ['level', 'service', 'namespace', 'pod', 'host']],
            ['matches a field title', 'namespace', ['namespace']],
            ['title match is case-insensitive', 'NAMESPACE', ['namespace']],
            ['partial title match', 'serv', ['service']],
            ['matches a whole group by name', 'kubernetes', ['namespace', 'pod']],
            ['group match is case-insensitive', 'INFRA', ['host']],
            ['no match returns empty', 'zzz', []],
        ])('%s', (_, query, expectedKeys) => {
            expect(filterFacetsByName(FACETS, query).map((f) => f.key)).toEqual(expectedKeys)
        })
    })

    describe('mergeSelectedIntoOptions', () => {
        const fetched: FacetOption[] = [
            { value: 'api', label: 'api', count: 10 },
            { value: 'worker', label: 'worker', count: 5 },
        ]

        it.each<[string, string[], string | undefined, string[]]>([
            ['no selected values leaves fetched unchanged', [], undefined, ['api', 'worker']],
            ['selected value already fetched is not duplicated', ['api'], undefined, ['api', 'worker']],
            ['missing selected value is prepended', ['batch-exports'], undefined, ['batch-exports', 'api', 'worker']],
            ['multiple missing values keep selection order', ['b', 'a'], undefined, ['b', 'a', 'api', 'worker']],
            // A value hand-edited into both polarities reaches here twice (included ++ excluded).
            [
                'a value repeated across polarities is not duplicated',
                ['dup', 'dup'],
                undefined,
                ['dup', 'api', 'worker'],
            ],
            ['missing selected value not matching the search is omitted', ['batch-exports'], 'api', ['api', 'worker']],
            [
                'search match is a case-insensitive substring',
                ['Batch-Exports'],
                'batch',
                ['Batch-Exports', 'api', 'worker'],
            ],
        ])('%s', (_, selected, search, expectedValues) => {
            expect(mergeSelectedIntoOptions(fetched, selected, search).map((o) => o.value)).toEqual(expectedValues)
        })

        it('injects missing selected values with a zero count', () => {
            expect(mergeSelectedIntoOptions(fetched, ['batch-exports'], undefined)[0]).toEqual({
                value: 'batch-exports',
                label: 'batch-exports',
                count: 0,
            })
        })
    })
})