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.

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

import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'

import { taskAgentDefaultsLogic } from './taskAgentDefaultsLogic'

describe('taskAgentDefaultsLogic', () => {
    let logic: ReturnType<typeof taskAgentDefaultsLogic.build>
    let posted: any[]

    function useConfigMocks(myPreferences: Record<string, any> | null): void {
        posted = []
        useMocks({
            get: {
                '/api/projects/:team_id/tasks/config/': () => [200, { ai_run_preferences: null }],
                '/api/projects/:team_id/tasks/@me/config/': () => [
                    200,
                    { ai_run_preferences: myPreferences, resolved_ai_run_defaults: null },
                ],
            },
            post: {
                '/api/projects/:team_id/tasks/@me/config/': async ({ request }) => {
                    posted.push(await request.json())
                    return [200, { ai_run_preferences: null, resolved_ai_run_defaults: null }]
                },
                '/api/projects/:team_id/tasks/config/': async ({ request }) => {
                    const body = await request.json()
                    return [200, { ai_run_preferences: body }]
                },
            },
        })
    }

    function mount(): void {
        logic = taskAgentDefaultsLogic()
        logic.mount()
    }

    beforeEach(() => {
        initKeaTests()
    })

    afterEach(() => {
        logic?.unmount()
    })

    // Resetting is the only way back to inheriting once a personal default is stored, so it has to clear
    // the stored preference server-side rather than just blank the pickers.
    it('clears the stored preference and drops back to the project default', async () => {
        useConfigMocks({ runtime_adapter: 'claude', model: 'claude-opus-5', reasoning_effort: 'high' })
        mount()
        await expectLogic(logic)
            .toDispatchActions(['loadMyConfigSuccess'])
            .toMatchValues({
                canResetMyPreference: true,
                myDraft: { model: 'claude-opus-5', reasoning_effort: 'high' },
            })

        logic.actions.resetMyPreference()

        await expectLogic(logic)
            .toDispatchActions(['saveMyPreferencesSuccess'])
            .toMatchValues({
                myDraft: { model: null, reasoning_effort: null },
                canResetMyPreference: false,
                myDraftDirty: false,
            })
        expect(posted).toEqual([{ runtime: null, runtime_adapter: null, model: null, reasoning_effort: null }])
    })

    // Nothing stored and nothing picked means there's nothing to fall back to — the button has to say so
    // rather than post an empty preference over an already-empty one.
    it('has nothing to reset while the project default is already what applies', async () => {
        useConfigMocks(null)
        mount()

        await expectLogic(logic).toDispatchActions(['loadMyConfigSuccess']).toMatchValues({
            canResetMyPreference: false,
            myDraftDirty: false,
        })

        // An unsaved pick is resettable too: reset discards it as well as anything stored.
        logic.actions.setMyDraft({ model: 'claude-opus-5' })
        await expectLogic(logic).toMatchValues({ canResetMyPreference: true, myDraftDirty: true })
    })

    it('does not seed the editor from a Pi default, but still offers to clear it', async () => {
        useConfigMocks({ runtime: 'pi', model: 'gpt-5.6-terra', reasoning_effort: 'off' })
        mount()

        await expectLogic(logic)
            .toDispatchActions(['loadMyConfigSuccess'])
            .toMatchValues({
                myDraft: { model: null, reasoning_effort: null },
                myDraftDirty: false,
                canResetMyPreference: true,
            })
    })

    it('moves a Pi default onto the ACP harness when an ACP model is picked', async () => {
        useConfigMocks({ runtime: 'pi', model: 'gpt-5.6-terra', reasoning_effort: 'off' })
        mount()
        await expectLogic(logic).toDispatchActions(['loadMyConfigSuccess'])

        logic.actions.setMyDraft({ model: 'claude-opus-5', reasoning_effort: null })
        logic.actions.submitMyDraft()

        await expectLogic(logic).toDispatchActions(['saveMyPreferencesSuccess'])
        expect(posted).toEqual([
            { runtime: 'acp', runtime_adapter: 'claude', model: 'claude-opus-5', reasoning_effort: null },
        ])
    })

    // Saving the project default refetches the personal config; that load result used to
    // overwrite whatever the person was editing in the other card, and the Save button then
    // read "No changes to save" over the silently discarded pick.
    it('keeps an in-progress personal edit across the refetch a project save triggers', async () => {
        useConfigMocks(null)
        mount()
        await expectLogic(logic).toDispatchActions(['loadMyConfigSuccess'])

        logic.actions.setMyDraft({ model: 'claude-opus-5' })
        logic.actions.setTeamDraft({ model: 'claude-fable-5' })
        logic.actions.submitTeamDraft()

        // Anchor on submitTeamDraft so the matched loadMyConfigSuccess is the refetch,
        // not the mount-time load already in the action history.
        await expectLogic(logic)
            .toDispatchActions(['submitTeamDraft', 'saveTeamPreferencesSuccess', 'loadMyConfigSuccess'])
            .toFinishAllListeners()
            .toMatchValues({
                myDraft: { model: 'claude-opus-5', reasoning_effort: null },
                myDraftDirty: true,
            })
    })
})