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.

52.6 KB
Raw Blame
import { MOCK_DEFAULT_ORGANIZATION, MOCK_DEFAULT_USER, MOCK_TEAM_ID } from 'lib/api.mock'

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

import { ApiError } from 'lib/api-error'
import { FIRST_REPORT_SCOUT_NAME } from 'lib/constants'
import { organizationLogic } from 'scenes/organizationLogic'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'
import { userLogic } from 'scenes/userLogic'

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

import {
    signalsScoutChatTasksCreate,
    signalsScoutConfigList,
    signalsScoutConfigRun,
    signalsScoutConfigSync,
    signalsScoutConfigUpdate,
    signalsScoutRunsRecentPerScout,
} from 'products/signals/frontend/generated/api'
import type { SignalScoutConfigApi, UserBasicApi } from 'products/signals/frontend/generated/api.schemas'
import { llmSkillsNamePublishCommunityCreate } from 'products/skills/frontend/generated/api'

import { SignalScoutRunSummary } from '../types'
import { SCOUT_RUNS_FLEET_COVERAGE_LIMIT } from '../utils/scoutRunsWindow'
import { ScoutRosterSort, scoutFleetLogic } from './scoutFleetLogic'

jest.mock('posthog-js')
jest.mock('products/signals/frontend/generated/api', () => ({
    signalsScoutChatTasksCreate: jest.fn(),
    signalsScoutConfigDestroy: jest.fn(),
    signalsScoutConfigList: jest.fn(),
    signalsScoutConfigRun: jest.fn(),
    signalsScoutConfigSync: jest.fn(),
    signalsScoutConfigUpdate: jest.fn(),
    signalsScoutRunsFindingsSummary: jest.fn(),
    signalsScoutRunsList: jest.fn(),
    signalsScoutRunsRecentPerScout: jest.fn(),
}))
jest.mock('products/skills/frontend/generated/api', () => ({
    llmSkillsNameArchiveCreate: jest.fn(),
    llmSkillsNamePublishCommunityCreate: jest.fn(),
}))

const mockSignalsScoutChatTasksCreate = signalsScoutChatTasksCreate as jest.MockedFunction<
    typeof signalsScoutChatTasksCreate
>
const mockSignalsScoutConfigList = signalsScoutConfigList as jest.MockedFunction<typeof signalsScoutConfigList>
const mockSignalsScoutConfigRun = signalsScoutConfigRun as jest.MockedFunction<typeof signalsScoutConfigRun>
const mockSignalsScoutConfigSync = signalsScoutConfigSync as jest.MockedFunction<typeof signalsScoutConfigSync>
const mockSignalsScoutConfigUpdate = signalsScoutConfigUpdate as jest.MockedFunction<typeof signalsScoutConfigUpdate>
const mockSignalsScoutRunsRecentPerScout = signalsScoutRunsRecentPerScout as jest.MockedFunction<
    typeof signalsScoutRunsRecentPerScout
>
const mockPublishCommunity = llmSkillsNamePublishCommunityCreate as jest.MockedFunction<
    typeof llmSkillsNamePublishCommunityCreate
>

const BASE_CONFIG: SignalScoutConfigApi = {
    id: 'config-1',
    skill_name: 'signals-scout-errors',
    description: 'Finds error trends.',
    scout_origin: 'canonical',
    scout_role: 'specialist',
    owners: [],
    enabled: true,
    status: 'active',
    pause_reason: null,
    managed_by: 'team',
    deprecation: null,
    emit: true,
    run_interval_minutes: 1440,
    run_cron_schedule: null,
    output_destinations: {},
    structured_output_schema: null,
    mcp_gateway_server_ids: [],
    write_scopes: [],
    allowed_mcp_tools: null,
    tool_preset: null,
    last_run_at: null,
    consecutive_failure_count: 0,
    status_changed_at: null,
    status_changed_by: null,
    auto_pause_exempt: false,
    lifecycle_locked: false,
    network_access: 'trusted',
    model: null,
    precheck_query: null,
    precheck_disabled: false,
    effective_precheck_query: null,
    precheck_query_source: 'off',
    last_precheck_at: null,
    last_precheck_outcome: null,
    last_precheck_row_count: null,
    source_product: null,
    source_id: null,
    created_at: '2026-07-22T00:00:00Z',
    updated_at: '2026-07-22T00:00:00Z',
}

const OWNER: UserBasicApi = {
    id: 7,
    uuid: '00000007-0000-0000-0000-000000000000',
    distinct_id: 'owner-7',
    first_name: 'Ada',
    last_name: 'Ellis',
    email: '[email protected]',
    is_email_verified: true,
    hedgehog_config: null,
}

// Ownership is only shown (and so only filtered on) for a scout a team wrote itself.
const OWNED_CONFIG: SignalScoutConfigApi = { ...BASE_CONFIG, scout_origin: 'custom', owners: [OWNER] }

function makeRun(overrides: Partial<SignalScoutRunSummary> = {}): SignalScoutRunSummary {
    return {
        run_id: 'run-1',
        total_spend: null,
        skill_name: BASE_CONFIG.skill_name,
        skill_version: 1,
        status: 'completed',
        metadata: {},
        created_at: '2026-07-22T01:00:00Z',
        started_at: '2026-07-22T01:00:00Z',
        completed_at: '2026-07-22T01:02:00Z',
        summary: '',
        emitted_count: 0,
        emitted_finding_ids: [],
        emitted_report_ids: [],
        edited_report_ids: [],
        ...overrides,
    }
}

function deferred<T>(): { promise: Promise<T>; resolve: (value: T) => void; reject: (error: Error) => void } {
    let resolvePromise!: (value: T) => void
    let rejectPromise!: (error: Error) => void
    const promise = new Promise<T>((resolve, reject) => {
        resolvePromise = resolve
        rejectPromise = reject
    })
    return { promise, resolve: resolvePromise, reject: rejectPromise }
}

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

    beforeEach(async () => {
        // The roster filters persist to localStorage, which jsdom keeps between tests.
        localStorage.clear()
        initKeaTests()
        mockSignalsScoutChatTasksCreate.mockReset()
        mockSignalsScoutConfigList.mockReset().mockResolvedValue([])
        mockSignalsScoutConfigRun.mockReset()
        mockSignalsScoutConfigSync.mockReset().mockResolvedValue([])
        mockSignalsScoutConfigUpdate.mockReset()
        mockSignalsScoutRunsRecentPerScout.mockReset().mockResolvedValue([])
        mockPublishCommunity.mockReset().mockResolvedValue({
            pr_url: 'https://github.com/PostHog/community-skills/pull/1',
            pr_number: 1,
            branch: 'community-skill/signals-scout-errors',
        })
        logic = scoutFleetLogic()
        logic.mount()
        await expectLogic(logic).toFinishAllListeners()
        logic.actions.loadScoutConfigsSuccess([BASE_CONFIG])
    })

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

    it.each([false, true])('keeps the first-report scout out of the normal UI unless staff=%s', async (isStaff) => {
        userLogic.actions.loadUserSuccess({ ...MOCK_DEFAULT_USER, is_staff: isStaff })
        const firstReport = { ...BASE_CONFIG, id: 'first-report', skill_name: FIRST_REPORT_SCOUT_NAME }
        const configs = [BASE_CONFIG, firstReport]
        mockSignalsScoutConfigList.mockResolvedValue(configs)
        mockSignalsScoutConfigSync.mockResolvedValue(configs)
        const expected = isStaff ? configs : [BASE_CONFIG]

        await expectLogic(logic, () => logic.actions.loadScoutConfigs()).toFinishAllListeners()
        expect(logic.values.scoutConfigs).toEqual(expected)
        await expectLogic(logic, () => logic.actions.syncScoutFleet()).toFinishAllListeners()
        expect(logic.values.scoutConfigs).toEqual(expected)
    })

    it('queues updates for a scout while its previous update is in flight', async () => {
        const firstRequest = deferred<SignalScoutConfigApi>()
        const queuedUpdates = {
            run_interval_minutes: 60,
            output_destinations: {
                slack: { integration_id: 42, channel: 'CSCOUTS|#scout-findings' },
            },
        }
        const finalConfig: SignalScoutConfigApi = {
            ...BASE_CONFIG,
            enabled: false,
            ...queuedUpdates,
        }
        mockSignalsScoutConfigUpdate.mockReturnValueOnce(firstRequest.promise).mockResolvedValueOnce(finalConfig)

        logic.actions.updateScoutConfig(BASE_CONFIG.id, { enabled: false })
        logic.actions.updateScoutConfig(BASE_CONFIG.id, { run_interval_minutes: 60 })
        logic.actions.updateScoutConfig(BASE_CONFIG.id, { output_destinations: queuedUpdates.output_destinations })

        expect(mockSignalsScoutConfigUpdate).toHaveBeenCalledTimes(1)
        expect(logic.values.scoutConfigs?.[0]).toMatchObject({ enabled: false, ...queuedUpdates })

        firstRequest.resolve({ ...BASE_CONFIG, enabled: false })
        await expectLogic(logic).toFinishAllListeners()

        expect(mockSignalsScoutConfigUpdate).toHaveBeenNthCalledWith(1, String(MOCK_TEAM_ID), BASE_CONFIG.id, {
            enabled: false,
        })
        expect(mockSignalsScoutConfigUpdate).toHaveBeenNthCalledWith(
            2,
            String(MOCK_TEAM_ID),
            BASE_CONFIG.id,
            queuedUpdates
        )
        expect(logic.values.scoutConfigs?.[0]).toEqual(finalConfig)
        expect(logic.values.updatingScoutIds).toEqual([])
    })

    it.each([
        [
            'a tool list after write access',
            { write_scopes: ['dashboard:write'] },
            { allowed_mcp_tools: ['insight-get'] },
        ],
        [
            'write access after a tool preset',
            { tool_preset: 'read_only' as const },
            { write_scopes: ['insight:write'] },
        ],
    ])('never queues write access and a tool selection into one request, for %s', async (_name, earlier, later) => {
        // The API rejects a request that carries both, which would drop the later edit too.
        const firstRequest = deferred<SignalScoutConfigApi>()
        mockSignalsScoutConfigUpdate.mockReturnValueOnce(firstRequest.promise).mockResolvedValueOnce(BASE_CONFIG)

        logic.actions.updateScoutConfig(BASE_CONFIG.id, { enabled: false })
        logic.actions.updateScoutConfig(BASE_CONFIG.id, earlier)
        logic.actions.updateScoutConfig(BASE_CONFIG.id, later)
        firstRequest.resolve({ ...BASE_CONFIG, enabled: false })
        await expectLogic(logic).toFinishAllListeners()

        expect(mockSignalsScoutConfigUpdate).toHaveBeenNthCalledWith(2, String(MOCK_TEAM_ID), BASE_CONFIG.id, later)
    })

    it('publishes a scout against its canonical project and preserves its tags', async () => {
        const config = { ...BASE_CONFIG, scout_origin: 'custom' as const, tags: ['errors'] }
        logic.actions.loadScoutConfigsSuccess([config])

        const reviewedSkill = { expected_skill_id: 'skill-1', expected_version: 3, expected_category: 'scout' }
        logic.actions.publishScoutToCommunity(config.id, reviewedSkill)
        await expectLogic(logic).toDispatchActions(['publishScoutToCommunityFinished'])

        expect(mockPublishCommunity).toHaveBeenCalledWith(
            String(teamLogic.values.currentProjectId),
            config.skill_name,
            expect.objectContaining({
                ...reviewedSkill,
                tags: ['errors'],
                scout_config: expect.objectContaining({ tags: ['errors'] }),
            })
        )
    })

    it.each([
        {
            name: 'sends a trimmed note and clears the draft once the run starts',
            note: '  focus on checkout  ',
            fails: false,
            body: { note: 'focus on checkout' },
            draftAfter: undefined,
            popoverOpenAfter: false,
            capturedHasNote: [true],
        },
        {
            name: 'sends no body for a blank note',
            note: '   ',
            fails: false,
            body: undefined,
            draftAfter: '   ',
            popoverOpenAfter: true,
            capturedHasNote: [false],
        },
        {
            name: 'keeps the draft and the popover when the run is refused',
            note: 'focus on checkout',
            fails: true,
            body: { note: 'focus on checkout' },
            draftAfter: 'focus on checkout',
            popoverOpenAfter: true,
            capturedHasNote: [],
        },
    ])('run now with a note: $name', async ({ note, fails, body, draftAfter, popoverOpenAfter, capturedHasNote }) => {
        if (fails) {
            mockSignalsScoutConfigRun.mockRejectedValue({ detail: 'You need skill editor access' })
        } else {
            mockSignalsScoutConfigRun.mockResolvedValue({ status: 'started' } as any)
        }
        logic.actions.setRunNotePopoverConfigId(BASE_CONFIG.id)
        logic.actions.setRunNoteDraft(BASE_CONFIG.id, note)

        logic.actions.runScoutNow(BASE_CONFIG.id, note)
        await expectLogic(logic).toDispatchActions(['runScoutNowFinished'])

        expect(mockSignalsScoutConfigRun).toHaveBeenCalledWith(String(MOCK_TEAM_ID), BASE_CONFIG.id, body)
        expect(logic.values.runNoteDrafts[BASE_CONFIG.id]).toEqual(draftAfter)
        expect(logic.values.runNotePopoverConfigId).toEqual(popoverOpenAfter ? BASE_CONFIG.id : null)
        const runNowEvents = jest
            .mocked(posthog.capture)
            .mock.calls.filter(([, properties]) => properties?.action_type === 'run_now')
        expect(runNowEvents.map(([, properties]) => properties?.has_note)).toEqual(capturedHasNote)
        expect(JSON.stringify(runNowEvents)).not.toContain('checkout')
    })

    // The roster is the only consumer of the tag filter, so assert through the rows it renders.
    const rosterConfigIds = (): string[] => logic.values.rosterScouts.map((row) => row.config.id)

    it('filters scouts by any selected tag and stops applying tags that are no longer in use', () => {
        const revenueScout = { ...BASE_CONFIG, tags: ['revenue'] }
        const onCallScout = {
            ...BASE_CONFIG,
            id: 'config-2',
            skill_name: 'signals-scout-on-call',
            tags: ['on-call'],
        }
        const untaggedScout = {
            ...BASE_CONFIG,
            id: 'config-3',
            skill_name: 'signals-scout-product',
            tags: [],
        }
        logic.actions.loadScoutConfigsSuccess([revenueScout, onCallScout, untaggedScout])

        logic.actions.setScoutTagFilter(['revenue', 'on-call'])

        expect(logic.values.activeScoutTags).toEqual(['revenue', 'on-call'])
        expect(rosterConfigIds()).toEqual(['config-1', 'config-2'])

        logic.actions.patchScoutConfigLocally(revenueScout.id, { tags: [] })
        logic.actions.patchScoutConfigLocally(onCallScout.id, { tags: [] })

        expect(logic.values.selectedScoutTags).toEqual(['revenue', 'on-call'])
        expect(logic.values.activeScoutTags).toEqual([])
        expect(rosterConfigIds()).toHaveLength(3)
    })

    it('filters scouts by owner and stops applying an owner who no longer owns one', () => {
        const otherScout = { ...BASE_CONFIG, id: 'config-2', skill_name: 'signals-scout-on-call' }
        logic.actions.loadScoutConfigsSuccess([OWNED_CONFIG, otherScout])

        logic.actions.setScoutOwnerFilter(OWNER.uuid)

        expect(logic.values.activeScoutOwner).toEqual(OWNER.uuid)
        expect(rosterConfigIds()).toEqual(['config-1'])

        // Handing the scout over empties the owner control, so the roster has to read as unfiltered
        // rather than as an empty fleet.
        logic.actions.patchScoutConfigLocally(OWNED_CONFIG.id, { owners: [] })

        expect(logic.values.selectedScoutOwner).toEqual(OWNER.uuid)
        expect(logic.values.activeScoutOwner).toBeNull()
        expect(rosterConfigIds()).toHaveLength(2)
    })

    // The card shows `display_name`, falling back to the prettified `skill_name`. Matching anything
    // else returns cards whose names miss the query, which reads as a broken search.
    const CHECKOUT_WATCH = {
        ...BASE_CONFIG,
        id: 'checkout-watch',
        skill_name: 'signals-scout-logs',
        display_name: 'Checkout watch',
        description: 'error spikes and new failure patterns in application logs',
    }
    const FALLBACK_NAME = { ...BASE_CONFIG, id: 'fallback-name', skill_name: 'signals-scout-error-tracking' }

    it.each([
        ['a partial name', 'check', ['checkout-watch']],
        ['a mixed-case name', 'ChEcKoUt', ['checkout-watch']],
        ['a padded query', '  check  ', ['checkout-watch']],
        // "Error tracking" is the fallback name; the raw slug it comes from has no space in it.
        ['the fallback name', 'error track', ['fallback-name']],
        ['a hidden skill_name', 'logs', []],
        ['a description', 'failure patterns', []],
        ['a whitespace-only query', '   ', ['checkout-watch', 'fallback-name']],
        ['an empty query', '', ['checkout-watch', 'fallback-name']],
    ])('resolves %s to the cards whose name matches', (_label, search, expected) => {
        logic.actions.loadScoutConfigsSuccess([CHECKOUT_WATCH, FALLBACK_NAME])

        logic.actions.setScoutSearch(search)

        expect(rosterConfigIds()).toEqual(expected)
    })

    it('narrows the same sorted rows as it searches, and keeps the other filters when cleared', () => {
        logic.actions.loadScoutConfigsSuccess([
            CHECKOUT_WATCH,
            FALLBACK_NAME,
            { ...BASE_CONFIG, id: 'checkout-off', skill_name: 'signals-scout-checkout-health', enabled: false },
        ])
        logic.actions.setScoutEnabledFilter('enabled')
        const unsearched = logic.values.rosterScouts
        const matchingRow = unsearched.find((row) => row.config.id === 'checkout-watch')

        logic.actions.setScoutSearch('check')

        // The turned-off scout stays out, so search narrows the filtered roster rather than the fleet.
        expect(rosterConfigIds()).toEqual(['checkout-watch'])
        // Re-sorting or rebuilding the rows on a keystroke would hand the list new objects and
        // re-render every card that did not change.
        expect(logic.values.rosterScouts[0]).toBe(matchingRow)

        logic.actions.setScoutSearch('')

        expect(logic.values.rosterScouts).toBe(unsearched)
        expect(logic.values.scoutEnabledFilter).toEqual('enabled')
    })

    it('lists the whole roster A→Z and tags each row with its lifecycle group', () => {
        // The runs poll re-pins `rosterEvaluatedAt` to the wall clock when real time has moved a
        // scout out of the pause window, so the fixture dates only hold with the clock pinned too.
        jest.useFakeTimers()
        try {
            jest.setSystemTime(new Date('2026-08-28T12:00:00Z'))
            logic.actions.setRosterEvaluatedAt(new Date('2026-08-28T12:00:00Z').valueOf())
            logic.actions.loadScoutConfigsSuccess([
                { ...BASE_CONFIG, id: 'quiet', skill_name: 'signals-scout-quiet' },
                { ...BASE_CONFIG, id: 'busy', skill_name: 'signals-scout-busy' },
                {
                    ...BASE_CONFIG,
                    id: 'off',
                    skill_name: 'signals-scout-off',
                    enabled: false,
                    status: 'paused_by_user',
                },
                {
                    ...BASE_CONFIG,
                    id: 'broken',
                    skill_name: 'signals-scout-broken',
                    enabled: false,
                    status: 'paused_by_system',
                    pause_reason: 'repeated_failures',
                    status_changed_at: '2026-08-27T12:00:00Z',
                },
                {
                    ...BASE_CONFIG,
                    id: 'stale-pause',
                    skill_name: 'signals-scout-stale-pause',
                    enabled: false,
                    status: 'paused_by_system',
                    pause_reason: 'no_output',
                    status_changed_at: '2026-08-20T12:00:00Z',
                },
                {
                    ...BASE_CONFIG,
                    id: 'warned',
                    skill_name: 'signals-scout-warned',
                    status: 'pending_pause',
                    pause_reason: 'ignored',
                },
                {
                    ...BASE_CONFIG,
                    id: 'quiet-warning',
                    skill_name: 'signals-scout-quiet-warning',
                    status: 'pending_pause',
                    pause_reason: 'no_output',
                },
            ])
            // `busy` filed a report in the window, which is what separates Working from Watching.
            logic.actions.loadScoutRunsSuccess([
                makeRun({ skill_name: 'signals-scout-busy', emitted_report_ids: ['r-1'] }),
            ])

            // One flat list ordered by name, not split across lifecycle sections.
            expect(logic.values.rosterScouts.map((row) => [row.config.id, row.group])).toEqual([
                ['broken', 'needs_you'],
                ['busy', 'working'],
                ['off', 'off'],
                ['quiet', 'watching'],
                ['quiet-warning', 'needs_you'],
                ['stale-pause', 'needs_you'],
                ['warned', 'needs_you'],
            ])
            // The stats tell a warning apart from a recent pause; human and stale pauses are neither.
            expect(logic.values.pauseAttentionCounts).toEqual({ pausingSoon: 1, recentlyPaused: 1 })
        } finally {
            jest.useRealTimers()
        }
    })

    const RECENCY_FLEET: SignalScoutConfigApi[] = [
        {
            ...BASE_CONFIG,
            id: 'alpha',
            skill_name: 'signals-scout-alpha',
            created_at: '2026-01-01T00:00:00Z',
            updated_at: '2026-08-20T00:00:00Z',
            last_run_at: '2026-08-01T00:00:00Z',
        },
        {
            ...BASE_CONFIG,
            id: 'zulu',
            skill_name: 'signals-scout-zulu',
            created_at: '2026-08-10T00:00:00Z',
            updated_at: '2026-08-11T00:00:00Z',
            last_run_at: '2026-08-25T00:00:00Z',
        },
        {
            ...BASE_CONFIG,
            id: 'mike',
            skill_name: 'signals-scout-mike',
            created_at: '2026-05-05T00:00:00Z',
            updated_at: '2026-08-30T00:00:00Z',
            last_run_at: null,
        },
        {
            ...BASE_CONFIG,
            id: 'bravo',
            skill_name: 'signals-scout-bravo',
            created_at: '2026-08-10T00:00:00Z',
            updated_at: '2026-01-02T00:00:00Z',
            last_run_at: '2026-08-25T00:00:00Z',
        },
    ]

    it.each<[ScoutRosterSort, string[]]>([
        ['name', ['alpha', 'bravo', 'mike', 'zulu']],
        ['created', ['bravo', 'zulu', 'mike', 'alpha']],
        ['updated', ['mike', 'alpha', 'zulu', 'bravo']],
        ['last_run', ['bravo', 'zulu', 'alpha', 'mike']],
    ])('orders the roster by %s', (sort, expected) => {
        logic.actions.loadScoutConfigsSuccess(RECENCY_FLEET)

        logic.actions.setScoutRosterSort(sort)

        expect(rosterConfigIds()).toEqual(expected)
    })

    it('keeps configs unresolved until the current team is available', async () => {
        logic.unmount()
        teamLogic.actions.loadCurrentTeamSuccess(null)
        mockSignalsScoutConfigList.mockClear()
        logic = scoutFleetLogic()
        logic.mount()

        await expectLogic(logic).toFinishAllListeners()

        expect(mockSignalsScoutConfigList).not.toHaveBeenCalled()
        expect(logic.values.scoutConfigs).toBeNull()
    })

    // The roster mounts from short-lived components, so an unmount mid-request is routine. The
    // loader reconciles against `values.scoutConfigs`, and that read throws once the reducer branch
    // leaves the store — a silent error report from a page the user has already left.
    it('reports nothing when the roster unmounts while its config request is in flight', async () => {
        const request = deferred<SignalScoutConfigApi[]>()
        mockSignalsScoutConfigList.mockReturnValueOnce(request.promise)

        logic.actions.loadScoutConfigs()
        logic.unmount()
        request.resolve([BASE_CONFIG])
        // Drain the microtasks the loader resumes on, so the assertion sees its full continuation.
        await new Promise(setImmediate)

        expect(posthog.captureException).not.toHaveBeenCalled()
    })

    // The 500 row is the point of this case: a guard wide enough to swallow it would leave a real
    // scout-configs outage looking identical to a project the user simply cannot reach.
    it.each([
        [403, 'loadScoutConfigsSuccess'],
        [404, 'loadScoutConfigsSuccess'],
        [500, 'loadScoutConfigsFailure'],
    ])('resolves a %s from the config list to %s', async (status, expectedAction) => {
        mockSignalsScoutConfigList.mockRejectedValueOnce(new ApiError('nope', status))

        logic.actions.loadScoutConfigs()

        await expectLogic(logic).toDispatchActions([expectedAction])
    })

    // The whole point of materializing on open: a project the coordinator never reached has no
    // configs at all, and the roster has no other way to get any.
    it.each(['roster', 'onboarding'] as const)('fills an empty fleet for %s', async (surface) => {
        mockSignalsScoutConfigSync.mockResolvedValue([BASE_CONFIG])
        logic.actions.loadScoutConfigsSuccess([])

        logic.actions.materializeScoutFleet(surface)

        await expectLogic(logic).toDispatchActions(['syncScoutFleetSuccess'])
        expect(mockSignalsScoutConfigSync).toHaveBeenCalledWith(String(MOCK_TEAM_ID), { surface })
        expect(logic.values.scoutConfigs).toEqual([BASE_CONFIG])
        expect(logic.values.scoutFleetSynced).toBe(true)
        expect(logic.values.scoutFleetSyncOutcome).toBe('synced')
    })

    it('materializes the fleet at most once per roster session', async () => {
        logic.actions.materializeScoutFleet()
        await expectLogic(logic).toDispatchActions(['syncScoutFleetSuccess'])

        logic.actions.materializeScoutFleet()
        await expectLogic(logic).toFinishAllListeners()

        expect(mockSignalsScoutConfigSync).toHaveBeenCalledTimes(1)
    })

    // Syncing on top of an in-flight list read would let the older read land last and blank the
    // roster the sync just filled.
    it('waits for an in-flight config read before materializing the fleet', async () => {
        const listRequest = deferred<SignalScoutConfigApi[]>()
        mockSignalsScoutConfigList.mockReturnValueOnce(listRequest.promise)
        mockSignalsScoutConfigSync.mockResolvedValue([BASE_CONFIG])
        logic.actions.loadScoutConfigs()

        logic.actions.materializeScoutFleet()
        await new Promise(setImmediate)
        expect(mockSignalsScoutConfigSync).not.toHaveBeenCalled()

        listRequest.resolve([])
        await expectLogic(logic).toDispatchActions(['loadScoutConfigsSuccess', 'syncScoutFleetSuccess'])
        expect(logic.values.scoutConfigs).toEqual([BASE_CONFIG])
    })

    // Materializing is a write, so a member without write access gets a 403. Either way the sync
    // has to settle: a roster stuck unsynced shows a skeleton over its empty state forever. The 500
    // row keeps a real outage from being swallowed into the same silent branch. Each refusal has to
    // report its own outcome — a roster kept because the sync was refused reads as an empty fleet
    // otherwise, and `Scout fleet viewed` can't tell that from a project with no scouts.
    it.each([
        [403, 'syncScoutFleetSuccess', 'skipped_permission'],
        [404, 'syncScoutFleetSuccess', 'not_found'],
        [500, 'syncScoutFleetFailure', 'failed'],
    ])(
        'keeps the roster and settles the sync when it is refused with %s',
        async (status, expectedAction, expectedOutcome) => {
            mockSignalsScoutConfigSync.mockRejectedValueOnce(new ApiError('nope', status))

            logic.actions.materializeScoutFleet()

            await expectLogic(logic).toDispatchActions([expectedAction])
            expect(logic.values.scoutConfigs).toEqual([BASE_CONFIG])
            expect(logic.values.scoutFleetSynced).toBe(true)
            expect(logic.values.scoutFleetSyncOutcome).toBe(expectedOutcome)
        }
    )

    it('sends newer queued updates after an earlier request fails', async () => {
        const firstRequest = deferred<SignalScoutConfigApi>()
        const failingRequest = deferred<SignalScoutConfigApi>()
        const outputDestinations = {
            slack: { integration_id: 42, channel: 'CSCOUTS|#scout-findings' },
        }
        const finalConfig: SignalScoutConfigApi = {
            ...BASE_CONFIG,
            enabled: false,
            output_destinations: outputDestinations,
        }
        mockSignalsScoutConfigUpdate
            .mockReturnValueOnce(firstRequest.promise)
            .mockReturnValueOnce(failingRequest.promise)
            .mockResolvedValueOnce(finalConfig)

        logic.actions.updateScoutConfig(BASE_CONFIG.id, { enabled: false })
        logic.actions.updateScoutConfig(BASE_CONFIG.id, { run_interval_minutes: 60 })

        firstRequest.resolve({ ...BASE_CONFIG, enabled: false })
        await expectLogic(logic).toDispatchActions(['patchScoutConfigLocally'])
        expect(mockSignalsScoutConfigUpdate).toHaveBeenCalledTimes(2)

        logic.actions.updateScoutConfig(BASE_CONFIG.id, { output_destinations: outputDestinations })
        failingRequest.reject(new Error('request failed'))
        await expectLogic(logic).toFinishAllListeners()

        expect(mockSignalsScoutConfigUpdate).toHaveBeenNthCalledWith(2, String(MOCK_TEAM_ID), BASE_CONFIG.id, {
            run_interval_minutes: 60,
        })
        expect(mockSignalsScoutConfigUpdate).toHaveBeenNthCalledWith(3, String(MOCK_TEAM_ID), BASE_CONFIG.id, {
            output_destinations: outputDestinations,
        })
        expect(logic.values.scoutConfigs?.[0]).toEqual(finalConfig)
        expect(logic.values.updatingScoutIds).toEqual([])
    })

    it('starts the chat task server-side once and navigates to it', async () => {
        mockSignalsScoutChatTasksCreate.mockResolvedValue({ task_id: 'task-1' })

        logic.actions.startScoutChatTask('author_scout', 'scout authoring task')
        // A second press while the first request is out must not mint a second paid task.
        logic.actions.startScoutChatTask('author_scout', 'scout authoring task')
        await expectLogic(logic).toDispatchActions(['startScoutChatTaskSuccess'])

        expect(mockSignalsScoutChatTasksCreate).toHaveBeenCalledTimes(1)
        expect(mockSignalsScoutChatTasksCreate).toHaveBeenCalledWith(String(MOCK_TEAM_ID), {
            chat_type: 'author_scout',
        })
        expect(router.values.location.pathname).toContain('task-1')
    })

    it('starts nothing when the organization has not approved AI data processing', async () => {
        organizationLogic.actions.loadCurrentOrganizationSuccess({
            ...MOCK_DEFAULT_ORGANIZATION,
            is_ai_data_processing_approved: false,
        })

        logic.actions.startScoutChatTask('author_scout', 'scout authoring task')
        await expectLogic(logic).toDispatchActions(['startScoutChatTaskFailure'])

        // The endpoint enforces no consent check of its own, so dropping the guard here would
        // start an agent sandbox for an organization that declined AI data processing.
        expect(mockSignalsScoutChatTasksCreate).not.toHaveBeenCalled()
    })

    it('disables every scout chat after the daily cap refusal until its retry time', async () => {
        jest.useFakeTimers()
        try {
            mockSignalsScoutChatTasksCreate.mockRejectedValue(
                new ApiError('Request was throttled.', 429, new Headers({ 'Retry-After': '3600' }), {
                    code: 'throttled',
                })
            )

            logic.actions.startScoutChatTask('author_scout', 'scout authoring task')
            await jest.advanceTimersByTimeAsync(0)
            expect(logic.values.scoutChatDisabledReason).toMatch(/^You've reached today's limit for scout chats/)

            logic.actions.startScoutChatTask('fleet_overview', 'fleet overview task')
            await jest.advanceTimersByTimeAsync(0)
            expect(mockSignalsScoutChatTasksCreate).toHaveBeenCalledTimes(1)

            await jest.advanceTimersByTimeAsync(3600 * 1000)
            expect(logic.values.scoutChatDisabledReason).toBeNull()
        } finally {
            jest.useRealTimers()
        }
    })

    it('reports roster filtering with the search length, not the term', async () => {
        jest.useFakeTimers()
        try {
            const capture = posthog.capture as jest.Mock
            capture.mockClear()
            logic.actions.loadScoutConfigsSuccess([
                BASE_CONFIG,
                { ...BASE_CONFIG, id: 'config-2', skill_name: 'signals-scout-revenue', enabled: false },
            ])

            logic.actions.setScoutEnabledFilter('disabled')
            logic.actions.setScoutSearch('rev')
            logic.actions.setScoutSearch('reve')
            await jest.advanceTimersByTimeAsync(600)
            logic.actions.setScoutSearch('')
            await jest.advanceTimersByTimeAsync(600)

            const scoutActions = capture.mock.calls
                .filter(([event]) => event === 'Scout action')
                .map(([, properties]) => properties)
            expect(scoutActions).toEqual([
                expect.objectContaining({ action_type: 'filter_enabled', filter: 'disabled', filter_match_count: 1 }),
                expect.objectContaining({ action_type: 'search_scouts', search_length: 4, filter_match_count: 1 }),
            ])
            expect(JSON.stringify(scoutActions)).not.toContain('reve')
        } finally {
            jest.useRealTimers()
        }
    })

    it('drops the delayed search write when the user navigates before the debounce settles', async () => {
        jest.useFakeTimers()
        try {
            router.actions.push(urls.inbox('scouts'))
            logic.actions.setScoutSearch('rev')
            // Open a scout's detail before the debounce elapses; the roster logic stays mounted beside it.
            router.actions.push(urls.inboxScout('signals-scout-revenue'))
            await jest.advanceTimersByTimeAsync(600)

            // The stale roster filter must not land on the detail route it does not own.
            expect(router.values.location.pathname).toContain('signals-scout-revenue')
            expect(router.values.searchParams.scoutSearch).toBeUndefined()
        } finally {
            jest.useRealTimers()
        }
    })

    it('writes non-default roster filters to the URL and keeps the bare view clean', async () => {
        jest.useFakeTimers()
        try {
            // The URL only carries tags and owners the fleet still uses, so the roster needs a scout
            // wearing the tag and answering for it.
            logic.actions.loadScoutConfigsSuccess([{ ...OWNED_CONFIG, tags: ['revenue'] }])
            logic.actions.setScoutEnabledFilter('disabled')
            logic.actions.setScoutTagFilter(['revenue'])
            logic.actions.setScoutOwnerFilter(OWNER.uuid)
            logic.actions.setScoutSearch('rev')
            // The search param is written on a debounce, so let its pause elapse.
            await jest.advanceTimersByTimeAsync(600)

            expect(router.values.searchParams).toMatchObject({
                scoutEnabled: 'disabled',
                scoutTags: 'revenue',
                scoutOwner: OWNER.uuid,
                scoutSearch: 'rev',
            })

            logic.actions.setScoutEnabledFilter('all')
            logic.actions.setScoutTagFilter([])
            logic.actions.setScoutOwnerFilter(null)
            logic.actions.setScoutSearch('')
            await jest.advanceTimersByTimeAsync(600)

            expect(router.values.searchParams.scoutEnabled).toBeUndefined()
            expect(router.values.searchParams.scoutTags).toBeUndefined()
            expect(router.values.searchParams.scoutOwner).toBeUndefined()
            expect(router.values.searchParams.scoutSearch).toBeUndefined()
        } finally {
            jest.useRealTimers()
        }
    })

    it('restores roster filters from a shared URL', async () => {
        router.actions.push(urls.inbox('scouts'), {
            scoutEnabled: 'enabled',
            scoutTags: 'revenue,on-call',
            scoutOwner: OWNER.uuid,
            scoutSearch: 'rev',
        })
        await expectLogic(logic).toDispatchActions(['hydrateRosterFilters'])

        expect(logic.values.scoutEnabledFilter).toEqual('enabled')
        expect(logic.values.selectedScoutTags).toEqual(['revenue', 'on-call'])
        expect(logic.values.selectedScoutOwner).toEqual(OWNER.uuid)
        expect(logic.values.scoutSearch).toEqual('rev')
    })

    it('resets the roster filters when Back or Forward reaches a bare URL', async () => {
        // A filtered roster, reached by a shared link or by toggling the controls.
        router.actions.push(urls.inbox('scouts'), {
            scoutEnabled: 'enabled',
            scoutTags: 'revenue',
            scoutOwner: OWNER.uuid,
        })
        await expectLogic(logic).toDispatchActions(['hydrateRosterFilters'])
        expect(logic.values.scoutEnabledFilter).toEqual('enabled')
        expect(logic.values.selectedScoutTags).toEqual(['revenue'])
        expect(logic.values.selectedScoutOwner).toEqual(OWNER.uuid)

        // Back or Forward onto the bare entry arrives as a POP carrying no roster params.
        await expectLogic(logic, () => {
            router.actions.locationChanged({
                method: 'POP',
                pathname: urls.inbox('scouts'),
                search: '',
                searchParams: {},
                hash: '',
                hashParams: {},
                url: urls.inbox('scouts'),
            })
        }).toDispatchActions(['hydrateRosterFilters'])

        expect(logic.values.scoutEnabledFilter).toEqual('all')
        expect(logic.values.selectedScoutTags).toEqual([])
        expect(logic.values.selectedScoutOwner).toBeNull()
        expect(logic.values.scoutSearch).toEqual('')
        // The bare entry is left as-is, so a second Back still reaches the entries beneath it.
        expect(router.values.searchParams.scoutEnabled).toBeUndefined()
        expect(router.values.searchParams.scoutTags).toBeUndefined()
        expect(router.values.searchParams.scoutOwner).toBeUndefined()
    })

    it('reflects a persisted filter back into a bare roster URL on fresh navigation', async () => {
        // The user filtered the roster, then opened the bare roster URL afresh (a PUSH, not Back).
        router.actions.push(urls.inbox('scouts'), { scoutEnabled: 'enabled' })
        await expectLogic(logic).toDispatchActions(['hydrateRosterFilters'])
        expect(logic.values.scoutEnabledFilter).toEqual('enabled')

        router.actions.push(urls.inbox('scouts'))
        await expectLogic(logic).toFinishAllListeners()

        // The filter persists and is written back so the restored view stays shareable.
        expect(logic.values.scoutEnabledFilter).toEqual('enabled')
        expect(router.values.searchParams.scoutEnabled).toEqual('enabled')
    })

    it('restores the persisted roster filters and sort on a fresh visit to the bare roster URL', async () => {
        logic.actions.setScoutEnabledFilter('enabled')
        logic.actions.setScoutTagFilter(['revenue'])
        logic.actions.setScoutOwnerFilter(OWNER.uuid)
        logic.actions.setScoutRosterSort('last_run')
        logic.actions.setScoutSearch('rev')
        logic.unmount()

        // A new tab: a fresh store mounts the roster on the bare URL, which kea-router reports as a POP.
        initKeaTests()
        router.actions.push(urls.inbox('scouts'))
        logic = scoutFleetLogic()
        logic.mount()

        expect(logic.values).toMatchObject({
            scoutEnabledFilter: 'enabled',
            selectedScoutTags: ['revenue'],
            selectedScoutOwner: OWNER.uuid,
            scoutRosterSort: 'last_run',
            scoutSearch: '',
        })
        expect(router.values.searchParams).toEqual({
            scoutEnabled: 'enabled',
            scoutTags: 'revenue',
            scoutOwner: OWNER.uuid,
        })
    })

    it('drops a typed search that a URL hydration has already replaced', async () => {
        jest.useFakeTimers()
        try {
            const capture = posthog.capture as jest.Mock
            router.actions.push(urls.inbox('scouts'))
            logic.actions.setScoutSearch('reve')
            capture.mockClear()

            // A shared link or Back reaches the same route with a different search before the
            // debounce settles. Hydration does not abort the breakpoint, so the stale query must
            // not report itself or write itself back over the hydrated URL.
            router.actions.push(urls.inbox('scouts'), { scoutSearch: 'rev' })
            await jest.advanceTimersByTimeAsync(600)

            expect(capture.mock.calls.filter(([event]) => event === 'Scout action')).toEqual([])
            expect(router.values.searchParams.scoutSearch).toEqual('rev')
        } finally {
            jest.useRealTimers()
        }
    })

    it.each([
        ['a trailing space', 'checkout ', 'checkout'],
        ['a trailing space after a number', '123 ', '123'],
        ['only spaces', '  ', undefined],
    ])('keeps %s in the search box after the debounced URL write', async (_, typed, written) => {
        jest.useFakeTimers()
        try {
            // The enabled filter keeps a roster param in the URL, so the search write reaches the
            // shared-link hydration path.
            router.actions.push(urls.inbox('scouts'), { scoutEnabled: 'enabled' })
            logic.actions.setScoutSearch(typed)
            await jest.advanceTimersByTimeAsync(600)

            expect(router.values.searchParams.scoutSearch).toEqual(written)
            expect(logic.values.scoutSearch).toEqual(typed)

            // The filter controls push the URL, so a push must not trim the search either.
            logic.actions.setScoutEnabledFilter('disabled')
            expect(logic.values.scoutSearch).toEqual(typed)
        } finally {
            jest.useRealTimers()
        }
    })

    it('keeps the roster filters out of a bare URL that a sibling logic replaces', async () => {
        router.actions.push(urls.inbox('scouts'), { scoutEnabled: 'enabled' })
        await expectLogic(logic).toDispatchActions(['hydrateRosterFilters'])

        // A sibling inbox logic restores its own params by replacing the bare URL, which happens
        // while a Back onto that bare entry is still being handled.
        await expectLogic(logic, () => {
            router.actions.locationChanged({
                method: 'REPLACE',
                pathname: urls.inbox('scouts'),
                search: '?scope=all',
                searchParams: { scope: 'all' },
                hash: '',
                hashParams: {},
                url: `${urls.inbox('scouts')}?scope=all`,
            })
        }).toFinishAllListeners()

        // Riding along here would leave the URL filtered once the Back resets the controls.
        expect(router.values.searchParams.scoutEnabled).toBeUndefined()
    })

    it('restores a search that the router parsed as a number', async () => {
        // Opening the link parses the query string, and kea-router turns `scoutSearch=123` into the
        // number 123, which a string-only read drops.
        router.actions.push(`${urls.inbox('scouts')}?scoutSearch=123`)
        await expectLogic(logic).toDispatchActions(['hydrateRosterFilters'])

        expect(logic.values.scoutSearch).toEqual('123')
    })

    it('drops a tag from the URL once no scout uses it', async () => {
        jest.useFakeTimers()
        try {
            const revenueScout = { ...BASE_CONFIG, tags: ['revenue'] }
            logic.actions.loadScoutConfigsSuccess([revenueScout])
            logic.actions.setScoutTagFilter(['revenue'])
            expect(router.values.searchParams.scoutTags).toEqual('revenue')

            // Retagging the last scout empties the tag control, so the roster is visibly unfiltered.
            // A URL that still carries the tag would re-filter on refresh if the tag came back.
            logic.actions.patchScoutConfigLocally(revenueScout.id, { tags: [] })
            logic.actions.setScoutSearch('rev')
            await jest.advanceTimersByTimeAsync(600)

            expect(logic.values.activeScoutTags).toEqual([])
            expect(router.values.searchParams.scoutTags).toBeUndefined()
        } finally {
            jest.useRealTimers()
        }
    })

    it('resets the in-flight chat type when the kickoff fails', async () => {
        mockSignalsScoutChatTasksCreate.mockRejectedValue(new Error('over the usage limit'))

        logic.actions.startScoutChatTask('author_scout', 'scout authoring task')
        await expectLogic(logic).toDispatchActions(['startScoutChatTaskFailure'])

        expect(logic.values.runningChatType).toBeNull()
    })

    // The 60s roster poll returns freshly parsed objects every cycle. Without per-item
    // reconciliation, every poll replaces every reference and the memoized roster re-renders on
    // an idle page. With it, an unchanged response must preserve identity end-to-end.
    describe('poll identity stability', () => {
        // Mirror what a real poll gets from the API: every response is a fresh JSON parse,
        // so identical content still arrives as all-new object references.
        const freshConfigs = (...configs: SignalScoutConfigApi[]): SignalScoutConfigApi[] =>
            configs.map((config) => JSON.parse(JSON.stringify(config)))

        it('keeps the configs array and every config reference when a poll changes nothing', async () => {
            mockSignalsScoutConfigList.mockImplementation(async () => freshConfigs(BASE_CONFIG))

            logic.actions.loadScoutConfigs()
            await expectLogic(logic).toDispatchActions(['loadScoutConfigs', 'loadScoutConfigsSuccess'])
            const first = logic.values.scoutConfigs

            logic.actions.loadScoutConfigs()
            await expectLogic(logic).toDispatchActions(['loadScoutConfigs', 'loadScoutConfigsSuccess'])
            const second = logic.values.scoutConfigs

            expect(second).toBe(first)
            expect(second?.[0]).toBe(first?.[0])
        })

        it('keeps unchanged config references when a poll changes one config', async () => {
            const otherConfig = { ...BASE_CONFIG, id: 'config-2', skill_name: 'signals-scout-revenue' }
            mockSignalsScoutConfigList.mockImplementation(async () => freshConfigs(BASE_CONFIG, otherConfig))

            logic.actions.loadScoutConfigs()
            await expectLogic(logic).toDispatchActions(['loadScoutConfigs', 'loadScoutConfigsSuccess'])
            const first = logic.values.scoutConfigs

            mockSignalsScoutConfigList.mockImplementation(async () =>
                freshConfigs({ ...BASE_CONFIG, enabled: false }, otherConfig)
            )
            logic.actions.loadScoutConfigs()
            await expectLogic(logic).toDispatchActions(['loadScoutConfigs', 'loadScoutConfigsSuccess'])
            const second = logic.values.scoutConfigs

            expect(second).not.toBe(first)
            expect(second?.[0].enabled).toBe(false)
            expect(second?.[1]).toBe(first?.[1])
        })

        it('advances time-sensitive roster states when an unchanged runs poll crosses a boundary', async () => {
            jest.useFakeTimers()
            try {
                jest.setSystemTime(Date.UTC(2026, 7, 4))
                const settledRun = makeRun({ run_id: 'run-settled', status: 'completed' })
                mockSignalsScoutRunsRecentPerScout.mockImplementation(async () => [
                    JSON.parse(JSON.stringify(settledRun)),
                ])
                logic.unmount()
                logic = scoutFleetLogic()
                logic.mount()
                await expectLogic(logic).toFinishAllListeners()
                logic.actions.loadScoutConfigsSuccess([
                    BASE_CONFIG,
                    {
                        ...BASE_CONFIG,
                        id: 'paused',
                        skill_name: 'signals-scout-paused',
                        enabled: false,
                        status: 'paused_by_system',
                        pause_reason: 'repeated_failures',
                        status_changed_at: '2026-07-29T00:00:00Z',
                    },
                ])
                logic.actions.loadScoutRuns()
                await expectLogic(logic).toDispatchActions(['loadScoutRuns', 'loadScoutRunsSuccess'])
                const firstRuns = logic.values.scoutRuns

                expect(logic.values.rosterScouts[0].group).toBe('settling_in')
                expect(logic.values.pauseAttentionCounts.recentlyPaused).toBe(1)

                jest.setSystemTime(Date.UTC(2026, 7, 6))
                logic.actions.loadScoutRuns()
                await expectLogic(logic).toDispatchActions(['loadScoutRuns', 'loadScoutRunsSuccess'])

                expect(logic.values.scoutRuns).toBe(firstRuns)
                expect(logic.values.rosterScouts[0].group).toBe('watching')
                expect(logic.values.pauseAttentionCounts.recentlyPaused).toBe(0)
            } finally {
                jest.useRealTimers()
            }
        })

        it('keeps a newer single-scout read when an earlier fleet read lands after it', async () => {
            const fleetRequest = deferred<SignalScoutRunSummary[]>()
            mockSignalsScoutRunsRecentPerScout.mockImplementationOnce(() => fleetRequest.promise)
            logic.actions.loadScoutRuns()
            mockSignalsScoutRunsRecentPerScout.mockResolvedValueOnce([makeRun({ run_id: 'run-fresh' })])
            logic.actions.loadScoutRunsForSkill(BASE_CONFIG.skill_name)
            await expectLogic(logic).toDispatchActions(['loadScoutRunsForSkillSuccess'])

            fleetRequest.resolve([
                makeRun({ run_id: 'run-stale' }),
                makeRun({ run_id: 'run-other', skill_name: 'signals-scout-surveys' }),
            ])
            await expectLogic(logic).toDispatchActions(['loadScoutRunsSuccess'])

            expect(logic.values.scoutRuns.map((run) => run.run_id).sort()).toEqual(['run-fresh', 'run-other'])
        })

        it('keeps a single-scout read that a fleet read past the probe bound did not cover', async () => {
            const otherConfigs = Array.from({ length: SCOUT_RUNS_FLEET_COVERAGE_LIMIT }, (_, index) => ({
                ...BASE_CONFIG,
                id: `config-other-${index}`,
                skill_name: `signals-scout-other-${index}`,
            }))
            logic.actions.loadScoutConfigsSuccess([BASE_CONFIG, ...otherConfigs])
            expect(logic.values.scoutRunsCoverFleet).toBe(false)
            mockSignalsScoutRunsRecentPerScout.mockResolvedValueOnce([makeRun({ run_id: 'run-own' })])
            logic.actions.loadScoutRunsForSkill(BASE_CONFIG.skill_name)
            await expectLogic(logic).toDispatchActions(['loadScoutRunsForSkillSuccess'])

            mockSignalsScoutRunsRecentPerScout.mockResolvedValueOnce([
                makeRun({ run_id: 'run-other', skill_name: 'signals-scout-other-0' }),
            ])
            logic.actions.loadScoutRuns()
            await expectLogic(logic).toDispatchActions(['loadScoutRunsSuccess'])

            expect(logic.values.scoutRuns.map((run) => run.run_id).sort()).toEqual(['run-other', 'run-own'])
        })

        it('keeps the runs array and settled run references when a poll changes nothing', async () => {
            const settledRun = makeRun({ run_id: 'run-settled', status: 'completed' })
            mockSignalsScoutRunsRecentPerScout.mockImplementation(async () => [JSON.parse(JSON.stringify(settledRun))])

            logic.actions.loadScoutRuns()
            await expectLogic(logic).toDispatchActions(['loadScoutRuns', 'loadScoutRunsSuccess'])
            const first = logic.values.scoutRuns
            const firstRoster = logic.values.rosterScouts

            logic.actions.loadScoutRuns()
            await expectLogic(logic).toDispatchActions(['loadScoutRuns', 'loadScoutRunsSuccess'])
            const second = logic.values.scoutRuns

            expect(second).toBe(first)
            expect(second[0]).toBe(first[0])
            expect(logic.values.rosterScouts).toBe(firstRoster)
        })

        it('keeps settled run references but not live ones when a poll reruns with a live run', async () => {
            const settledRun = makeRun({ run_id: 'run-settled', status: 'completed' })
            const liveRun = makeRun({ run_id: 'run-live', status: 'in_progress' })
            mockSignalsScoutRunsRecentPerScout.mockImplementation(async () =>
                [settledRun, liveRun].map((run) => JSON.parse(JSON.stringify(run)))
            )

            logic.actions.loadScoutRuns()
            await expectLogic(logic).toDispatchActions(['loadScoutRuns', 'loadScoutRunsSuccess'])
            const first = logic.values.scoutRuns

            logic.actions.loadScoutRuns()
            await expectLogic(logic).toDispatchActions(['loadScoutRuns', 'loadScoutRunsSuccess'])
            const second = logic.values.scoutRuns

            // A live run must refresh identity — its rows render wall-clock durations that have
            // to advance with each poll. Settled neighbours stay reference-stable.
            expect(second).not.toBe(first)
            expect(second.find((run) => run.run_id === 'run-settled')).toBe(
                first.find((run) => run.run_id === 'run-settled')
            )
            expect(second.find((run) => run.run_id === 'run-live')).not.toBe(
                first.find((run) => run.run_id === 'run-live')
            )
        })
    })
})