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.

2.5 KB
Raw Blame
import { expectLogic } from 'kea-test-utils'

import { initKeaTests } from '~/test/init'

import { logsGroupByCreate } from 'products/logs/frontend/generated/api'

import { logsViewerConfigLogic } from '../LogsViewer/config/logsViewerConfigLogic'
import { logsViewerFiltersLogic } from '../LogsViewer/Filters/logsViewerFiltersLogic'
import { logsGroupByLogic } from './logsGroupByLogic'

jest.mock('lib/api')
jest.mock('products/logs/frontend/generated/api')

describe('logsGroupByLogic', () => {
    let logic: ReturnType<typeof logsGroupByLogic.build>
    let configLogic: ReturnType<typeof logsViewerConfigLogic.build>
    let filtersLogic: ReturnType<typeof logsViewerFiltersLogic.build>

    beforeEach(() => {
        localStorage.clear()
        initKeaTests()
        jest.clearAllMocks()
        ;(logsGroupByCreate as jest.Mock).mockResolvedValue({
            groups: [],
            total_groups: 0,
            total_logs: 0,
            truncated: false,
        })
        configLogic = logsViewerConfigLogic({ id: 'test' })
        configLogic.mount()
        filtersLogic = logsViewerFiltersLogic({ id: 'test' })
        filtersLogic.mount()
        logic = logsGroupByLogic({ id: 'test' })
        logic.mount()
    })

    it('queries with the ordered dimension list and not at all without dimensions', async () => {
        // Mounting with no dimensions picked must not fire the aggregation.
        await expectLogic(logic).toFinishAllListeners()
        expect(logsGroupByCreate).not.toHaveBeenCalled()

        configLogic.actions.setGroupBys([
            { key: 'service.name', source: 'resource' },
            { key: 'severity_level', source: 'column' },
        ])
        await expectLogic(logic).toFinishAllListeners()

        expect(logsGroupByCreate).toHaveBeenCalledTimes(1)
        const body = (logsGroupByCreate as jest.Mock).mock.calls[0][1]
        expect(body.query.groupBys).toEqual([
            { key: 'service.name', source: 'resource' },
            { key: 'severity_level', source: 'column' },
        ])
        expect(body.query.groupBy).toBeUndefined()
    })

    it('re-aggregates when the refresh button runs the query', async () => {
        configLogic.actions.setGroupBys([{ key: 'service.name', source: 'resource' }])
        await expectLogic(logic).toFinishAllListeners()
        expect(logsGroupByCreate).toHaveBeenCalledTimes(1)

        filtersLogic.actions.bumpFacetRefresh()
        await expectLogic(logic).toFinishAllListeners()

        expect(logsGroupByCreate).toHaveBeenCalledTimes(2)
    })
})