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.3 KB
Raw Blame
import { PLACEHOLDER_HREF } from 'lib/utils/navigateToHref'

import { SETTINGS_THEME_ITEM_ID, canOpenInNewTab, filterSearchItems, shouldSearchTickets } from './utils'

interface TestItem {
    name: string
    displayName?: string
    category: string
    searchKeywords?: string[]
    matchedSearchKeyword?: string | null
}

const makeItem = (name: string, category = 'tools', extra: Partial<TestItem> = {}): TestItem => ({
    name,
    category,
    ...extra,
})

const items: TestItem[] = [
    makeItem('Event definitions', 'data-management'),
    makeItem('Property definitions', 'data-management'),
    makeItem('Feature flags', 'tools'),
    makeItem('Cohorts', 'tools'),
    makeItem('Dashboards', 'tools'),
    makeItem('Session recordings', 'tools'),
    makeItem('Web analytics', 'tools'),
    makeItem('Experiments', 'tools'),
    makeItem('Surveys', 'tools'),
    makeItem('Product analytics', 'tools'),
    makeItem('Error tracking', 'tools'),
    makeItem('Data warehouse', 'tools', { searchKeywords: ['sql', 'query', 'database'] }),
]

const names = (results: TestItem[]): string[] => results.map((r) => r.name)

describe('filterSearchItems', () => {
    it('returns all items for empty query', () => {
        expect(filterSearchItems(items, '')).toEqual(items)
        expect(filterSearchItems(items, '  ')).toEqual(items)
    })

    describe('exact and near-exact matching', () => {
        it('matches by exact name', () => {
            expect(names(filterSearchItems(items, 'Cohorts'))).toContain('Cohorts')
        })

        it('matches case-insensitively', () => {
            expect(names(filterSearchItems(items, 'cohorts'))).toContain('Cohorts')
        })

        it('matches partial name', () => {
            expect(names(filterSearchItems(items, 'dash'))).toContain('Dashboards')
        })
    })

    describe('fuzzy matching (the original bug)', () => {
        it('"events" matches "Event definitions"', () => {
            const results = filterSearchItems(items, 'events')
            expect(names(results)).toContain('Event definitions')
        })

        it('"event" matches "Event definitions"', () => {
            const results = filterSearchItems(items, 'event')
            expect(names(results)).toContain('Event definitions')
        })

        it('"properties" matches "Property definitions"', () => {
            const results = filterSearchItems(items, 'properties')
            expect(names(results)).toContain('Property definitions')
        })

        it('"flag" matches "Feature flags"', () => {
            const results = filterSearchItems(items, 'flag')
            expect(names(results)).toContain('Feature flags')
        })

        it('"experiment" matches "Experiments"', () => {
            const results = filterSearchItems(items, 'experiment')
            expect(names(results)).toContain('Experiments')
        })
    })

    describe('searchKeywords', () => {
        it('matches on searchKeywords', () => {
            const results = filterSearchItems(items, 'sql')
            expect(names(results)).toContain('Data warehouse')
        })

        it('matches on keyword "database"', () => {
            const results = filterSearchItems(items, 'database')
            expect(names(results)).toContain('Data warehouse')
        })

        it.each([
            ['sql', 'sql'],
            ['DATABASE', 'database'],
            ['data warehouse', null],
        ])('reports the keyword that matched %s', (query, expectedKeyword) => {
            const match = filterSearchItems(items, query).find((item) => item.name === 'Data warehouse')
            expect(match?.matchedSearchKeyword).toBe(expectedKeyword)
        })
    })

    describe('category matching', () => {
        it('matches on category', () => {
            const results = filterSearchItems(items, 'data-management')
            expect(names(results)).toContain('Event definitions')
            expect(names(results)).toContain('Property definitions')
        })
    })

    describe('no false positives', () => {
        it('does not match completely unrelated terms', () => {
            const results = filterSearchItems(items, 'xyzzyplugh')
            expect(results).toEqual([])
        })
    })
})

// Cmd/Ctrl+Enter routes through this, so a false positive on an action item runs that action
// instead of opening a background tab, which in the case of "Log out" ends the session.
describe('canOpenInNewTab', () => {
    const cases: [label: string, item: { id: string; href?: string; onSelect?: () => void }, expected: boolean][] = [
        ['allows a plain navigable result', { id: 'insight-1', href: '/insights/1' }, true],
        ['blocks an action-only result such as "Log out"', { id: 'misc-logout', onSelect: jest.fn() }, false],
        [
            'blocks an action that also carries an href',
            { id: 'new-sql-query-tab', href: '/sql', onSelect: jest.fn() },
            false,
        ],
        [
            'blocks the theme row, which navigates but toggles instead',
            { id: SETTINGS_THEME_ITEM_ID, href: '/settings/theme' },
            false,
        ],
        ['blocks a result with nothing to open', { id: 'no-href' }, false],
        ['blocks a result whose href is the placeholder', { id: 'starred-1', href: PLACEHOLDER_HREF }, false],
    ]

    it.each(cases)('%s', (_label, item, expected) => {
        expect(canOpenInNewTab(item)).toBe(expected)
    })
})

describe('shouldSearchTickets', () => {
    it('waits for a query worth scanning message content for', () => {
        expect(shouldSearchTickets('')).toBe(false)
        expect(shouldSearchTickets('b')).toBe(false)
        expect(shouldSearchTickets('bi')).toBe(false)
        expect(shouldSearchTickets('bil')).toBe(true)
        expect(shouldSearchTickets('  billing  ')).toBe(true)
    })

    // A number resolves to an indexed lookup by ticket number, so it is never too short.
    it('always searches a ticket number', () => {
        expect(shouldSearchTickets('7')).toBe(true)
        expect(shouldSearchTickets('#7')).toBe(true)
        expect(shouldSearchTickets('#')).toBe(false)
    })

    // The server drops an over-long search term, so sending one would answer a pasted log line
    // with unfiltered tickets.
    it('does not send a query the server would drop', () => {
        expect(shouldSearchTickets('x'.repeat(200))).toBe(true)
        expect(shouldSearchTickets('x'.repeat(201))).toBe(false)
    })
})