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.

4.1 KB
Raw Blame
import { MOCK_DEFAULT_USER } from 'lib/api.mock'
import { installMockEventSource, MockEventSource } from 'lib/wizard-sync/eventSource.mock'

import { expectLogic } from 'kea-test-utils'
import posthog from 'posthog-js'

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

import type { WizardRunApi } from '../../generated/api.schemas'
import { wizardRunSyncLogic } from '../../logics/wizardRunSyncLogic'
import { wizardRunSidebarLogic } from './wizardRunSidebarLogic'

jest.mock('../../generated/api', () => ({
    wizardRunsList: jest.fn().mockResolvedValue({ results: [], next: null }),
    getWizardRunsStreamRetrieveUrl: (_: string, id: string) => `/stream/${id}`,
}))

const props = { projectId: '1', userId: MOCK_DEFAULT_USER.id }
function run(id: string): WizardRunApi {
    return { id, status: 'completed', created_by_id: props.userId } as WizardRunApi
}

describe('wizardRunSidebarLogic', () => {
    let logic: ReturnType<typeof wizardRunSidebarLogic.build>
    let restoreEventSource: () => void

    beforeEach(() => {
        localStorage.clear()
        initKeaTests()
        restoreEventSource = installMockEventSource()
        logic = wizardRunSidebarLogic(props)
        logic.mount()
    })
    afterEach(() => {
        logic.unmount()
        restoreEventSource()
        localStorage.clear()
    })

    it('dismisses one, several, or all known runs without hiding future runs, including after reload', async () => {
        await expectLogic(logic).toFinishAllListeners()
        const sync = wizardRunSyncLogic({ projectId: '1' })
        const runs = ['first', 'second', 'third'].map((id) => ({ ...run(id), status: 'running' as const }))
        sync.actions.runsLoaded(runs)
        logic.actions.clickState('navbar_sidebar')
        expect(posthog.capture).toHaveBeenCalledWith('wizard sidebar state clicked', {
            event_source: 'wizard_ui',
            project_id: '1',
            location: 'navbar_sidebar',
            run_count: 3,
            active_run_count: 3,
        })
        logic.actions.dismissRuns(['first'])
        expect(posthog.capture).toHaveBeenCalledWith('wizard sidebar state dismissed', {
            event_source: 'wizard_ui',
            project_id: '1',
            run_count: 1,
            remaining_run_count: 2,
        })
        expect(logic.values.visibleRuns.map((run) => run.id)).toEqual(['second', 'third'])
        logic.actions.dismissRuns(logic.values.visibleRuns.map((run) => run.id))
        expect(posthog.capture).toHaveBeenLastCalledWith('wizard sidebar state dismissed', {
            event_source: 'wizard_ui',
            project_id: '1',
            run_count: 2,
            remaining_run_count: 0,
        })
        expect(logic.values.visibleRuns).toEqual([])
        logic.actions.clickState('today_more_menu')
        expect(posthog.capture).toHaveBeenLastCalledWith('wizard sidebar state clicked', {
            event_source: 'wizard_ui',
            project_id: '1',
            location: 'today_more_menu',
            run_count: 3,
            active_run_count: 3,
        })
        expect(MockEventSource.instances.every((stream) => stream.readyState === MockEventSource.CLOSED)).toBe(true)
        sync.actions.runsLoaded([{ ...run('new'), status: 'running' }, ...runs])
        expect(logic.values.visibleRuns.map((run) => run.id)).toEqual(['new'])
        expect(MockEventSource.last().url).toBe('/stream/new')
        logic.unmount()

        initKeaTests()
        logic = wizardRunSidebarLogic(props)
        logic.mount()
        await expectLogic(logic).toFinishAllListeners()
        wizardRunSyncLogic({ projectId: '1' }).actions.runsLoaded([run('new'), ...runs])
        expect(logic.values.visibleRuns.map((run) => run.id)).toEqual(['new'])
    })

    it('scopes dismissals to the current project and user', async () => {
        await expectLogic(logic).toFinishAllListeners()
        logic.actions.dismissRuns(['first'])
        logic.unmount()
        logic = wizardRunSidebarLogic({ projectId: '2', userId: props.userId + 1 })
        logic.mount()
        await expectLogic(logic).toFinishAllListeners()
        expect(logic.values.dismissedRunIds).toEqual([])
    })
})