Skip to content

The agent that grows with you

22.1 KB
Raw Blame
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'

import { DropdownMenu, DropdownMenuContent } from '@/components/ui/dropdown-menu'
import { $customModels } from '@/store/custom-models'
import { $collapsedProviders, toggleCollapsedProvider } from '@/store/provider-collapse'
import { $activeSessionId, $currentModel, $currentProvider, setCurrentModelSource } from '@/store/session'

import { ModelMenuPanel } from './model-menu-panel'

// Radix calls these on open; jsdom doesn't implement them.
beforeAll(() => {
  Element.prototype.scrollIntoView = vi.fn()
  Element.prototype.hasPointerCapture = vi.fn(() => false)
  Element.prototype.releasePointerCapture = vi.fn()
})

const getGlobalModelOptions = vi.fn()

vi.mock('@/hermes', () => ({
  getGlobalModelOptions: (...args: unknown[]) => getGlobalModelOptions(...args),
  setApiRequestProfile: vi.fn()
}))

// MoA presets now arrive as the catalog's virtual `moa` provider row (the same
// payload a remote gateway's model.options returns), not the /api/model/moa
// REST config.
const MOA_PROVIDER = { models: ['default', 'BeastMode'], name: 'Mixture of Agents', slug: 'moa' }

const DEEPSEEK_PROVIDER = {
  models: ['deepseek-v4-pro', 'deepseek-chat', 'deepseek-reasoner'],
  name: 'DeepSeek',
  slug: 'deepseek'
}

const GOOGLE_PROVIDER = {
  models: ['gemini-3.1-pro', 'gemini-2.5-flash', 'gemini-2.5-pro'],
  name: 'Google',
  slug: 'google'
}

const MOCK_PROVIDERS = [DEEPSEEK_PROVIDER, GOOGLE_PROVIDER, MOA_PROVIDER]

beforeEach(() => {
  $activeSessionId.set('runtime-1')
  $currentModel.set('')
  $currentProvider.set('')
  $collapsedProviders.set([])
  $customModels.set([])
  getGlobalModelOptions.mockResolvedValue({ providers: MOCK_PROVIDERS })
})

afterEach(() => {
  cleanup()
  vi.clearAllMocks()
})

function renderPanel(onSelectModel = vi.fn(), onFollowDefaultModel?: () => void) {
  const client = new QueryClient({ defaultOptions: { queries: { retry: false } } })

  const requestGateway = vi.fn(async (method: string) => {
    if (method === 'model.options') {
      return getGlobalModelOptions()
    }

    throw new Error(`unexpected gateway method: ${method}`)
  })

  const content = render(
    <QueryClientProvider client={client}>
      <DropdownMenu open>
        <DropdownMenuContent>
          <ModelMenuPanel
            onFollowDefaultModel={onFollowDefaultModel}
            onSelectModel={onSelectModel}
            requestGateway={requestGateway as never}
          />
        </DropdownMenuContent>
      </DropdownMenu>
    </QueryClientProvider>
  )

  return { onSelectModel, content }
}

describe('ModelMenuPanel MoA presets', () => {
  it('selecting a MoA preset switches PERSISTENTLY via onSelectModel (not the one-shot dispatch)', async () => {
    const { content, onSelectModel } = renderPanel()

    // moaOptions is async (useQuery) — wait for the preset row to mount.
    const row = await content.findByText('MoA: BeastMode')
    fireEvent.click(row)

    // #54670: must route through the persistent model-switch path
    // i.e. onSelectModel with provider 'moa' (which session-scopes live-session
    // switches), NOT a one-shot command.dispatch that reverts after a turn.
    expect(onSelectModel).toHaveBeenCalledWith({ model: 'BeastMode', provider: 'moa', sessionId: 'runtime-1' })
  })

  it('shows the check on the preset that matches the current moa selection', async () => {
    $currentProvider.set('moa')
    $currentModel.set('BeastMode')
    const { content } = renderPanel()

    const row = await content.findByText('MoA: BeastMode')
    // The check codicon renders as a sibling within the same row item.
    const item = row.closest('[role="menuitem"]') ?? row.parentElement
    expect(item?.querySelector('.codicon-check')).not.toBeNull()
  })

  it('keeps the virtual moa provider out of the main model groups (presets section only)', async () => {
    const { content } = renderPanel()

    await content.findByText('MoA: BeastMode')

    // The provider group header would read "Mixture of Agents"; the presets
    // section header reads "MoA presets". Only the latter should exist.
    // Radix DropdownMenu portals its content to document.body, so assert
    // against the body (not content.container) to see the rendered items.

    // eslint-disable-next-line no-restricted-globals
    expect(document.body.textContent).toContain('MoA presets')
    // eslint-disable-next-line no-restricted-globals
    expect(document.body.textContent).not.toContain('Mixture of Agents')
  })

  it('renders presets from the catalog even before a session exists', async () => {
    $activeSessionId.set('')
    const { onSelectModel, content } = renderPanel()

    const row = await content.findByText('MoA: BeastMode')
    fireEvent.click(row)

    // Pre-session picks are UI state shipped on the next session.create — the
    // row must not be disabled and must still route through onSelectModel.
    expect(onSelectModel).toHaveBeenCalledWith({ model: 'BeastMode', provider: 'moa', sessionId: null })
  })
})

describe('ModelMenuPanel current selection', () => {
  it('keeps the checkmark on the live SessionView model when a stale options response disagrees', async () => {
    $currentProvider.set('google')
    $currentModel.set('gemini-3.1-pro')
    getGlobalModelOptions.mockResolvedValue({
      model: 'deepseek-chat',
      provider: 'deepseek',
      providers: MOCK_PROVIDERS
    })

    const { content } = renderPanel()

    const currentRow = (await content.findByText(/Gemini 3\.1 Pro/i)).closest('[role="menuitem"]')
    const staleRow = content.getByText('DeepSeek Chat').closest('[role="menuitem"]')

    expect(currentRow?.querySelector('.codicon-check')).not.toBeNull()
    expect(staleRow?.querySelector('.codicon-check')).toBeNull()
  })
})

describe('ModelMenuPanel search', () => {
  // The pinned current model must NOT ride along on a query it doesn't match:
  // it reads like the top result, so Enter/click picks the wrong model (the
  // "type grok, get fable" bug). Every surveyed picker (VS Code, Zed, Open
  // WebUI, Cherry Studio) drops the pin while filtering.
  // Highlighted labels are split across <mark> nodes, so single-text-node
  // queries miss them — match on the row span's composed textContent. Badge
  // chips (#51833) live in a wrapper span around the name, so require a leaf
  // span: the wrapper's composed textContent matches too.
  const rowWithText = (content: ReturnType<typeof renderPanel>['content'], pattern: RegExp) =>
    content.queryByText(
      (_, element) =>
        element?.tagName === 'SPAN' && !element.querySelector('span') && pattern.test(element.textContent ?? '')
    )

  it('hides the non-matching current model while a query is active', async () => {
    $currentProvider.set('deepseek')
    $currentModel.set('deepseek-v4-pro')
    const { content } = renderPanel()

    await content.findByText(/DeepSeek V4 Pro/i)

    const input = screen.getByRole('textbox', { name: 'Search models' })
    fireEvent.change(input, { target: { value: 'gemini' } })

    await vi.waitFor(() => {
      expect(rowWithText(content, /Gemini 3\.1 Pro/i)).not.toBeNull()
    })
    expect(rowWithText(content, /DeepSeek V4 Pro/i)).toBeNull()
  })

  it('Enter in the search field commits the first match', async () => {
    const { content, onSelectModel } = renderPanel()

    await content.findByText('DeepSeek')

    const input = screen.getByRole('textbox', { name: 'Search models' })
    fireEvent.change(input, { target: { value: 'gemini' } })

    await vi.waitFor(() => {
      expect(rowWithText(content, /Gemini 3\.1 Pro/i)).not.toBeNull()
    })

    fireEvent.keyDown(input, { key: 'Enter' })

    // First matching family of the first (alphabetical) matching provider.
    await vi.waitFor(() => {
      expect(onSelectModel).toHaveBeenCalledWith({
        model: 'gemini-3.1-pro',
        provider: 'google',
        sessionId: 'runtime-1'
      })
    })
  })

  it('Enter on an id nothing lists selects it as a custom model', async () => {
    const { content, onSelectModel } = renderPanel()

    await content.findByText('DeepSeek')

    const input = screen.getByRole('textbox', { name: 'Search models' })
    fireEvent.change(input, { target: { value: 'zzz-no-such-model' } })

    // One row per configured provider (MoA excluded).
    await vi.waitFor(() => {
      expect(content.getAllByText(/^zzz-no-such-model/)).toHaveLength(2)
    })

    fireEvent.keyDown(input, { key: 'Enter' })

    // First configured provider, since no provider is current.
    await vi.waitFor(() => {
      expect(onSelectModel).toHaveBeenCalledWith({
        model: 'zzz-no-such-model',
        provider: 'deepseek',
        sessionId: 'runtime-1'
      })
    })
  })

  it('a query that still matches catalog rows offers no custom model', async () => {
    const { content } = renderPanel()

    await content.findByText('DeepSeek')

    const input = screen.getByRole('textbox', { name: 'Search models' })
    fireEvent.change(input, { target: { value: 'gemini' } })

    await vi.waitFor(() => {
      expect(rowWithText(content, /Gemini 3\.1 Pro/i)).not.toBeNull()
    })
    expect(rowWithText(content, /^gemini$/)).toBeNull()
  })

  it('arrows move the selection without leaving the input; Enter commits the stepped row', async () => {
    const { content, onSelectModel } = renderPanel()

    await content.findByText('DeepSeek')

    const input = screen.getByRole('textbox', { name: 'Search models' })
    fireEvent.change(input, { target: { value: 'gemini' } })

    await vi.waitFor(() => {
      expect(rowWithText(content, /Gemini 3\.1 Pro/i)).not.toBeNull()
    })

    // First match auto-selected; ↓ steps to the second match.
    fireEvent.keyDown(input, { key: 'ArrowDown' })
    fireEvent.keyDown(input, { key: 'Enter' })

    await vi.waitFor(() => {
      expect(onSelectModel).toHaveBeenCalledWith({
        model: 'gemini-2.5-flash',
        provider: 'google',
        sessionId: 'runtime-1'
      })
    })
  })

  it('keeps model rows hit-testable before any pointer movement (#123040)', async () => {
    const { content, onSelectModel } = renderPanel()

    await content.findByText('DeepSeek')

    const row = rowWithText(content, /Gemini 3\.1 Pro/i)!.closest('[role="menuitem"]')!

    // CSS hit-testing is the regression boundary: the previous catalog put a
    // pointer-events-none ancestor around every row until mousemove/wheel.
    expect(row.closest('.pointer-events-none')).toBeNull()

    fireEvent.click(row)

    await vi.waitFor(() => {
      expect(onSelectModel).toHaveBeenCalledWith({
        model: 'gemini-3.1-pro',
        provider: 'google',
        sessionId: 'runtime-1'
      })
    })
  })

  it('hovering a model row leaves focus in the search field and arrows still drive the list (#53980)', async () => {
    const { content, onSelectModel } = renderPanel()

    await content.findByText('DeepSeek')

    const input = screen.getByRole('textbox', { name: 'Search models' })
    input.focus()
    fireEvent.change(input, { target: { value: 'gemini' } })

    await vi.waitFor(() => {
      expect(rowWithText(content, /Gemini 2\.5 Pro/i)).not.toBeNull()
    })

    // No wake event is needed: DropdownMenu's search-safe hover suppresses
    // Radix focus theft directly while leaving deliberate clicks enabled.
    fireEvent.pointerMove(rowWithText(content, /Gemini 2\.5 Pro/i)!, { pointerType: 'mouse' })

    expect(input.ownerDocument.activeElement).toBe(input)

    fireEvent.keyDown(input, { key: 'ArrowDown' })
    fireEvent.keyDown(input, { key: 'Enter' })

    await vi.waitFor(() => {
      expect(onSelectModel).toHaveBeenCalledWith({
        model: 'gemini-2.5-flash',
        provider: 'google',
        sessionId: 'runtime-1'
      })
    })
  })

  it('with no query the selection sits on the current model, so Enter closes without switching', async () => {
    $currentProvider.set('google')
    $currentModel.set('gemini-3.1-pro')
    const { content, onSelectModel } = renderPanel()

    await content.findByText('DeepSeek')

    const input = screen.getByRole('textbox', { name: 'Search models' })
    fireEvent.keyDown(input, { key: 'Enter' })

    expect(onSelectModel).not.toHaveBeenCalled()
  })

  it('filters MoA presets by the query instead of leaving them as phantom first matches', async () => {
    const { content, onSelectModel } = renderPanel()

    await content.findByText('MoA: BeastMode')

    const input = screen.getByRole('textbox', { name: 'Search models' })
    fireEvent.change(input, { target: { value: 'beast' } })

    await vi.waitFor(() => {
      expect(rowWithText(content, /MoA: BeastMode/)).not.toBeNull()
    })
    expect(rowWithText(content, /MoA: default/)).toBeNull()

    // The surviving preset IS the first row, so Enter commits it.
    fireEvent.keyDown(input, { key: 'Enter' })

    await vi.waitFor(() => {
      expect(onSelectModel).toHaveBeenCalledWith({ model: 'BeastMode', provider: 'moa', sessionId: 'runtime-1' })
    })
  })
})

describe('ModelMenuPanel provider collapse', () => {
  it('expands provider models when header is clicked again', async () => {
    const { content } = renderPanel()

    const header = await content.findByText('DeepSeek')
    // Collapse
    fireEvent.click(header)
    expect(content.queryByText('DeepSeek V4 Pro')).toBeNull()
    // Expand
    fireEvent.click(header)
    await vi.waitFor(() => {
      expect(content.queryByText('DeepSeek V4 Pro')).not.toBeNull()
    })
  })

  it('collapses the active provider too (no forced auto-expand)', async () => {
    $currentProvider.set('deepseek')
    $currentModel.set('deepseek-v4-pro')
    const { content } = renderPanel()

    const header = await content.findByText('DeepSeek')
    fireEvent.click(header)

    // The current provider is collapsible like any other — clicking its header
    // hides its models rather than forcing them to stay open.
    await vi.waitFor(() => {
      expect(content.queryByText('DeepSeek V4 Pro')).toBeNull()
    })
  })

  it('bypasses collapse when search is active', async () => {
    const { content } = renderPanel()

    const header = await content.findByText('DeepSeek')
    fireEvent.click(header)
    expect(content.queryByText('DeepSeek V4 Pro')).toBeNull()

    // Type in the search bar (auto-focused by DropdownMenuSearch)
    const input = screen.getByRole('textbox', { name: 'Search models' })
    expect(input).not.toBeNull()
    fireEvent.change(input, { target: { value: 'deepseek' } })

    // Should show models — search bypasses collapse. The matched letters render
    // inside a <mark>, splitting the label across nodes, so match on the row
    // span's composed textContent instead of a single text node. Badge chips
    // (#51833) add a wrapper span whose composed textContent matches too, so
    // require a leaf span.
    await vi.waitFor(() => {
      expect(
        content.queryByText(
          (_, element) =>
            element?.tagName === 'SPAN' &&
            !element.querySelector('span') &&
            (element.textContent ?? '').startsWith('DeepSeek V4 Pro')
        )
      ).not.toBeNull()
    })
  })

  it('toggles collapse via keyboard Enter on header', async () => {
    const { content } = renderPanel()

    const header = await content.findByText('DeepSeek')
    // Radix DropdownMenuItem fires onSelect on Enter from the onKeyDown handler
    fireEvent.keyDown(header.closest('[role="menuitem"]') ?? header, { key: 'Enter' })

    expect(content.queryByText('DeepSeek V4 Pro')).toBeNull()
  })

  // The collapsed-providers set is a global presentation preference
  // (`hermes.desktop.collapsed-providers`), but the catalog the picker renders
  // is profile-scoped (`getGlobalModelOptions` routes through
  // `profileScoped()`). Pruning the global set against only the active catalog
  // would silently delete a user's collapse preference on every profile switch
  // whose configured providers don't include the slug — the bug the maintainer
  // flagged. The set must survive catalog changes; if the same provider shows
  // up again later, the previous collapse is preserved.
  it('preserves the collapsed set across a profile switch whose catalog lacks the slug', async () => {
    toggleCollapsedProvider('deepseek')
    toggleCollapsedProvider('google')
    expect($collapsedProviders.get()).toEqual(['deepseek', 'google'])

    // Profile A: both providers present, render + unmount.
    getGlobalModelOptions.mockResolvedValueOnce({ providers: MOCK_PROVIDERS })
    const a = renderPanel()
    await a.content.findByText('DeepSeek')
    a.content.unmount()

    // Profile B: google is not in the catalog (simulates a profile whose
    // configured providers differ). The previously-collapsed 'google' slug
    // must survive — pruning it would lose state across a profile switch.
    getGlobalModelOptions.mockResolvedValueOnce({ providers: [DEEPSEEK_PROVIDER, MOA_PROVIDER] })
    const b = renderPanel()
    await b.content.findByText('DeepSeek')

    expect($collapsedProviders.get()).toEqual(['deepseek', 'google'])
  })

  it('preserves the collapsed set when Refresh Models drops a provider', async () => {
    toggleCollapsedProvider('deepseek')
    toggleCollapsedProvider('google')

    // First load: both providers present.
    getGlobalModelOptions.mockResolvedValueOnce({ providers: MOCK_PROVIDERS })
    const a = renderPanel()
    await a.content.findByText('DeepSeek')
    a.content.unmount()

    // Refresh Models returns a catalog that drops google (revoked key,
    // plugin disabled, backend policy change). 'google' must survive — the
    // user explicitly collapsed it, and the global set is not tied to any
    // single refresh.
    getGlobalModelOptions.mockResolvedValueOnce({ providers: [DEEPSEEK_PROVIDER, MOA_PROVIDER] })
    const b = renderPanel()
    await b.content.findByText('DeepSeek')

    expect($collapsedProviders.get()).toContain('google')
    expect($collapsedProviders.get()).toContain('deepseek')
  })

  it('keeps the current pick when Refresh Models no longer lists it', async () => {
    // Rows are hints (discovered / curated / capped); a custom slug the row
    // lacks is still what the user selected. Only the gateway may reject it.
    $currentProvider.set('zhipu')
    $currentModel.set('glm-4.5-air')
    getGlobalModelOptions
      .mockResolvedValueOnce({
        model: 'glm-4.5-air',
        provider: 'zhipu',
        providers: [{ models: ['glm-4.5-air', 'glm-5-turbo'], name: '智谱2', slug: 'zhipu' }, MOA_PROVIDER]
      })
      .mockResolvedValueOnce({
        model: 'glm-4.5-air',
        provider: 'zhipu',
        providers: [DEEPSEEK_PROVIDER, MOA_PROVIDER]
      })

    const { content, onSelectModel } = renderPanel()

    await content.findByText(/GLM 4.5 Air/i)

    fireEvent.click(await content.findByText('Refresh models'))

    await vi.waitFor(() => {
      expect(getGlobalModelOptions).toHaveBeenCalledTimes(2)
    })
    expect(onSelectModel).not.toHaveBeenCalled()
    expect($currentModel.get()).toBe('glm-4.5-air')
    expect($currentProvider.get()).toBe('zhipu')
  })

  it('does not rewrite the provider when Refresh Models lists the same model id elsewhere', async () => {
    $currentProvider.set('zhipu')
    $currentModel.set('glm-4.5-air')

    const catalog = {
      model: 'glm-4.5-air',
      provider: 'zhipu',
      providers: [
        { models: ['glm-4.5-air', 'gpt-5.5'], name: 'OpenRouter', slug: 'openrouter' },
        { models: ['glm-4.5-air', 'glm-5-turbo'], name: '智谱2', slug: 'zhipu' },
        MOA_PROVIDER
      ]
    }

    getGlobalModelOptions.mockResolvedValue(catalog)

    const { content, onSelectModel } = renderPanel()

    await content.findAllByText(/GLM 4.5 Air/i)
    fireEvent.click(await content.findByText('Refresh models'))

    await vi.waitFor(() => {
      expect(getGlobalModelOptions).toHaveBeenCalledTimes(2)
    })
    expect(onSelectModel).not.toHaveBeenCalled()
  })

  it('marks only the matching provider row current when two providers share a model id', async () => {
    $currentProvider.set('zhipu')
    $currentModel.set('glm-4.5-air')
    getGlobalModelOptions.mockResolvedValue({
      model: 'glm-4.5-air',
      provider: 'zhipu',
      providers: [
        { models: ['glm-4.5-air', 'gpt-5.5'], name: 'OpenRouter', slug: 'openrouter' },
        { models: ['glm-4.5-air', 'glm-5-turbo'], name: '智谱2', slug: 'zhipu' },
        MOA_PROVIDER
      ]
    })

    const { content, onSelectModel } = renderPanel()

    const rows = await content.findAllByText(/GLM 4.5 Air/i)
    const items = [...new Set(rows.map(row => row.closest('[role="menuitem"]')))]

    expect(items).toHaveLength(2)

    const checked = items.filter(item => item?.querySelector('.codicon-check'))
    expect(checked).toHaveLength(1)
    expect(checked[0]?.closest('[role="group"]')?.textContent).toContain('智谱2')
    expect(
      items.find(item => !item?.querySelector('.codicon-check'))?.closest('[role="group"]')?.textContent
    ).toContain('OpenRouter')

    const input = screen.getByRole('textbox', { name: 'Search models' })
    fireEvent.keyDown(input, { key: 'Enter' })
    expect(onSelectModel).not.toHaveBeenCalled()
  })
})

describe('ModelMenuPanel pinned draft', () => {
  afterEach(() => setCurrentModelSource(''))

  it('offers the way back to the Settings default only while a draft carries a manual pick (#107410)', async () => {
    $activeSessionId.set(null)
    setCurrentModelSource('manual')
    const onFollowDefaultModel = vi.fn()
    const { content } = renderPanel(vi.fn(), onFollowDefaultModel)

    fireEvent.click(await content.findByText('Use Settings default'))
    expect(onFollowDefaultModel).toHaveBeenCalledTimes(1)
    cleanup()

    setCurrentModelSource('default')
    const unpinned = renderPanel(vi.fn(), vi.fn())
    await unpinned.content.findByText('Refresh models')
    expect(unpinned.content.queryByText('Use Settings default')).toBeNull()
    cleanup()

    // A live session runs its own model; the pin only decides the NEXT new chat.
    $activeSessionId.set('runtime-1')
    setCurrentModelSource('manual')
    const live = renderPanel(vi.fn(), vi.fn())
    await live.content.findByText('Refresh models')
    expect(live.content.queryByText('Use Settings default')).toBeNull()
  })
})