Skip to content

The agent that grows with you

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

import type * as ConfigApi from '@/api/config'
import { I18nProvider, TRANSLATIONS } from '@/i18n'
import { $notifications, clearNotifications } from '@/store/notifications'

import { ModelSettings } from './model-settings'

// Radix Select calls scrollIntoView on its items when the content opens; jsdom
// doesn't implement it (nor hasPointerCapture / releasePointerCapture), so stub
// them to let the dropdown open in tests.
beforeAll(() => {
  Element.prototype.scrollIntoView = vi.fn()
  Element.prototype.hasPointerCapture = vi.fn(() => false)
  Element.prototype.releasePointerCapture = vi.fn()
})

const getGlobalModelInfo = vi.fn()
const getGlobalModelOptions = vi.fn()
const getAuxiliaryModels = vi.fn()
const getMoaModels = vi.fn()
const setModelAssignment = vi.fn()
const getRecommendedDefaultModel = vi.fn()
const saveMoaModels = vi.fn()
const setEnvVar = vi.fn()
const getHermesConfigRecord = vi.fn()
const saveHermesConfig = vi.fn()
const startManualLocalEndpoint = vi.fn()
const startManualOnboarding = vi.fn()
const startManualProviderOAuth = vi.fn()
let profileSwitchHandler: (() => void) | null = null

// Keep the real read-origin helpers (WeakMap peek/bind) live: the shared
// config hook reaches them through the barrel, and a bare mock would throw.
vi.mock('@/hermes', async () => ({
  ...(await vi.importActual<typeof ConfigApi>('@/api/config')),
  getGlobalModelInfo: (profile?: null | string) => getGlobalModelInfo(profile),
  getGlobalModelOptions: (opts?: unknown, profile?: null | string) => getGlobalModelOptions(opts, profile),
  getAuxiliaryModels: (profile?: null | string) => getAuxiliaryModels(profile),
  getApiRequestProfile: () => 'default',
  getMoaModels: (profile?: null | string) => getMoaModels(profile),
  profileScopeKey: (scope?: null | string) => (scope ?? '').trim() || 'default',
  setModelAssignment: (body: unknown) => setModelAssignment(body),
  getRecommendedDefaultModel: (slug: string) => getRecommendedDefaultModel(slug),
  saveMoaModels: (body: unknown) => saveMoaModels(body),
  setEnvVar: (key: string, value: string) => setEnvVar(key, value),
  getHermesConfigRecord: () => getHermesConfigRecord(),
  saveHermesConfig: (config: unknown) => saveHermesConfig(config),
  setApiRequestProfile: () => {}
}))

vi.mock('@/store/onboarding', () => ({
  startManualLocalEndpoint: (...args: unknown[]) => startManualLocalEndpoint(...args),
  startManualOnboarding: (...args: unknown[]) => startManualOnboarding(...args),
  startManualProviderOAuth: (...args: unknown[]) => startManualProviderOAuth(...args)
}))

vi.mock('../hooks/use-on-profile-switch', () => ({
  useOnProfileSwitch: (handler: () => void) => {
    profileSwitchHandler = handler
  }
}))

beforeEach(() => {
  getGlobalModelInfo.mockResolvedValue({ provider: 'nous', model: 'hermes-4' })
  getGlobalModelOptions.mockResolvedValue({
    providers: [
      {
        name: 'Nous',
        slug: 'nous',
        models: ['hermes-4', 'hermes-4-mini'],
        authenticated: true,
        capabilities: { 'hermes-4': { reasoning: true, fast: true } }
      }
    ]
  })
  getAuxiliaryModels.mockResolvedValue({
    main: { provider: 'nous', model: 'hermes-4' },
    tasks: [{ task: 'vision', provider: 'auto', model: '', base_url: '' }]
  })
  getMoaModels.mockResolvedValue(null)
  setModelAssignment.mockResolvedValue({ ok: true, provider: 'nous', model: 'hermes-4', gateway_tools: [] })
  getRecommendedDefaultModel.mockResolvedValue({ provider: 'nous', model: 'hermes-4', free_tier: null })
  setEnvVar.mockResolvedValue({ ok: true })
  getHermesConfigRecord.mockResolvedValue({ agent: { reasoning_effort: 'medium', service_tier: 'normal' } })
  saveHermesConfig.mockResolvedValue({ ok: true })
})

afterEach(() => {
  cleanup()
  // A success toast arms a 5 s auto-dismiss timer; left running, it fires after this file's
  // jsdom window is gone and vitest reports "window is not defined" as an unhandled error.
  clearNotifications()
  vi.clearAllMocks()
  profileSwitchHandler = null
})

function renderModelSettings(scopeProfile?: string) {
  const client = new QueryClient({ defaultOptions: { queries: { retry: false } } })

  return render(
    // The aux-task deep-link highlight reads useSearchParams, so the page
    // needs a router context in tests (the app provides HashRouter at root).
    <MemoryRouter>
      <QueryClientProvider client={client}>
        <ModelSettings scopeProfile={scopeProfile} />
      </QueryClientProvider>
    </MemoryRouter>
  )
}

describe('ModelSettings profile scope', () => {
  // #90549: the API helpers treat `null` as "deliberately target the
  // primary/default profile". A page following the active profile must pass
  // `undefined`, or every read repaints the primary's model and the user's
  // change looks reverted.
  it('follows the active profile (undefined, never null) when unscoped', async () => {
    renderModelSettings()

    await waitFor(() => expect(getGlobalModelInfo).toHaveBeenCalledWith(undefined))
    expect(getGlobalModelOptions).toHaveBeenCalledWith(undefined, undefined)
    expect(getAuxiliaryModels).toHaveBeenCalledWith(undefined)
    expect(getMoaModels).toHaveBeenCalledWith(undefined)
  })

  it('reads through the explicit scope override when one is set', async () => {
    renderModelSettings('research')

    await waitFor(() => expect(getGlobalModelInfo).toHaveBeenCalledWith('research'))
    expect(getGlobalModelOptions).toHaveBeenCalledWith(undefined, 'research')
    expect(getAuxiliaryModels).toHaveBeenCalledWith('research')
    expect(getMoaModels).toHaveBeenCalledWith('research')
  })
})

describe('ModelSettings', () => {
  it('labels the main provider, model and default controls independently', async () => {
    renderModelSettings()

    expect(await screen.findByRole('combobox', { name: 'Provider' })).toBeTruthy()
    expect(screen.getByRole('combobox', { name: 'Model' })).toBeTruthy()
    expect(await screen.findByRole('combobox', { name: 'Reasoning' })).toBeTruthy()
    expect(screen.getByText('Provider', { exact: true })).toBeTruthy()
    expect(screen.getByText('Model', { exact: true })).toBeTruthy()
    expect(setModelAssignment).not.toHaveBeenCalled()
  })
  it.each(['custom', 'local', 'custom:lab'])(
    'opens local endpoint setup when %s has no inventory row',
    async provider => {
      getGlobalModelInfo.mockResolvedValueOnce({ provider, model: '' })
      getGlobalModelOptions.mockResolvedValueOnce({ providers: [] })

      renderModelSettings('leverage-ai')

      const providerSelect = (await screen.findAllByRole('combobox'))[0]

      expect(providerSelect.textContent).toContain(provider)
      expect(screen.queryByText(/undefined/)).toBeNull()
      expect(screen.queryByText(/signs in through your browser/)).toBeNull()

      fireEvent.click(await screen.findByRole('button', { name: 'Set up provider' }))

      expect(startManualLocalEndpoint).toHaveBeenCalledOnce()
      expect(startManualLocalEndpoint).toHaveBeenCalledWith(null, 'leverage-ai')
      expect(startManualOnboarding).not.toHaveBeenCalled()
      expect(startManualProviderOAuth).not.toHaveBeenCalled()
    }
  )

  it('opens the generic provider picker for an unknown provider with no inventory row', async () => {
    getGlobalModelInfo.mockResolvedValueOnce({ provider: 'retired-provider', model: '' })
    getGlobalModelOptions.mockResolvedValueOnce({ providers: [] })

    renderModelSettings('leverage-ai')

    fireEvent.click(await screen.findByRole('button', { name: 'Set up provider' }))

    expect(startManualOnboarding).toHaveBeenCalledOnce()
    expect(startManualOnboarding).toHaveBeenCalledWith(undefined, 'leverage-ai')
    expect(startManualLocalEndpoint).not.toHaveBeenCalled()
    expect(startManualProviderOAuth).not.toHaveBeenCalled()
  })

  it('deep-links a known OAuth provider row into its scoped setup flow', async () => {
    getGlobalModelInfo.mockResolvedValueOnce({ provider: 'anthropic', model: '' })
    getGlobalModelOptions.mockResolvedValueOnce({
      providers: [
        {
          name: 'Anthropic',
          slug: 'anthropic',
          models: [],
          authenticated: false,
          auth_type: 'oauth'
        }
      ]
    })

    renderModelSettings('leverage-ai')

    fireEvent.click(await screen.findByRole('button', { name: 'Set up Anthropic' }))

    expect(startManualProviderOAuth).toHaveBeenCalledWith('anthropic', 'leverage-ai')
    expect(startManualLocalEndpoint).not.toHaveBeenCalled()
    expect(startManualOnboarding).not.toHaveBeenCalled()
  })

  it('replaces the selected provider and model when the active profile changes', async () => {
    getGlobalModelInfo
      .mockResolvedValueOnce({ provider: 'custom', model: 'local-a' })
      .mockResolvedValueOnce({ provider: 'nous', model: 'hermes-4' })
    getGlobalModelOptions
      .mockResolvedValueOnce({
        providers: [
          {
            name: 'Custom A',
            slug: 'custom',
            models: ['local-a'],
            authenticated: true
          }
        ]
      })
      .mockResolvedValueOnce({
        providers: [
          {
            name: 'Nous',
            slug: 'nous',
            models: ['hermes-4'],
            authenticated: true,
            capabilities: { 'hermes-4': { reasoning: true, fast: true } }
          }
        ]
      })

    renderModelSettings()
    expect((await screen.findAllByRole('combobox'))[0].textContent).toContain('Custom A')

    await act(async () => {
      profileSwitchHandler?.()
    })

    await waitFor(() => expect(getGlobalModelInfo).toHaveBeenCalledTimes(2))
    await waitFor(() => expect(screen.getAllByRole('combobox')[0].textContent).toContain('Nous'))
    expect(screen.queryByRole('button', { name: 'Set up provider' })).toBeNull()
  })

  it('preserves a user-defined provider endpoint when applying the main model', async () => {
    getGlobalModelOptions.mockResolvedValueOnce({
      providers: [
        {
          name: 'Nous',
          slug: 'nous',
          models: ['hermes-4'],
          authenticated: true
        },
        {
          name: 'Ollama',
          slug: 'local-ollama',
          models: ['qwen3:latest'],
          authenticated: true,
          is_user_defined: true,
          api_url: 'http://localhost:11434/v1'
        }
      ]
    })
    setModelAssignment.mockResolvedValueOnce({
      ok: true,
      provider: 'local-ollama',
      model: 'qwen3:latest',
      gateway_tools: []
    })

    renderModelSettings()

    const providerSelect = (await screen.findAllByRole('combobox'))[0]
    fireEvent.click(providerSelect)
    fireEvent.click(await screen.findByRole('option', { name: 'Ollama' }))

    const modelSelect = (await screen.findAllByRole('combobox'))[1]
    fireEvent.click(modelSelect)
    fireEvent.click(await screen.findByRole('option', { name: 'qwen3:latest' }))

    fireEvent.click(await screen.findByRole('button', { name: 'Apply' }))

    await waitFor(() =>
      expect(setModelAssignment).toHaveBeenCalledWith({
        model: 'qwen3:latest',
        provider: 'local-ollama',
        scope: 'main',
        base_url: 'http://localhost:11434/v1'
      })
    )
  })

  it('matches a saved custom:<key> main provider to its catalog row', async () => {
    // model.info reports a user-defined provider as `custom:<key>`, while the
    // catalog row carries the bare key as its slug plus the alias list.
    getGlobalModelInfo.mockResolvedValueOnce({ provider: 'custom:lab', model: 'lab-large' })
    getGlobalModelOptions.mockResolvedValueOnce({
      providers: [
        {
          name: 'Lab',
          slug: 'lab',
          aliases: ['custom:lab', 'lab'],
          models: ['lab-small', 'lab-large'],
          authenticated: true,
          is_user_defined: true,
          api_url: 'http://lab.local/v1'
        }
      ]
    })

    renderModelSettings()

    await waitFor(() => expect(screen.getAllByRole('combobox')[0].textContent).toBe('Lab'))
    expect(screen.queryByRole('button', { name: 'Set up provider' })).toBeNull()

    fireEvent.click(await screen.findByRole('button', { name: 'Apply' }))

    await waitFor(() =>
      expect(setModelAssignment).toHaveBeenCalledWith({
        model: 'lab-large',
        provider: 'custom:lab',
        scope: 'main',
        base_url: 'http://lab.local/v1'
      })
    )
  })

  it('writes the profile default speed (service_tier) as a sparse patch, never the cached snapshot', async () => {
    // The cached record is a default-expanded snapshot; a CLI pin made after it
    // loaded is not in it. Echoing the whole record back would reset that
    // auxiliary slot to auto/'' (#95460) — only the edited key may be sent.
    getHermesConfigRecord.mockResolvedValue({
      agent: { reasoning_effort: 'medium', service_tier: 'normal' },
      auxiliary: { curator: { provider: 'auto', model: '', reasoning_effort: 'high' } }
    })
    renderModelSettings()
    await waitFor(() => expect(getHermesConfigRecord).toHaveBeenCalled())

    const fastSwitch = await screen.findByRole('switch')
    fireEvent.click(fastSwitch)

    await waitFor(() => expect(saveHermesConfig).toHaveBeenCalledWith({ agent: { service_tier: 'fast' } }))
  })

  it('hides the reasoning/speed defaults when the main model reports no capabilities', async () => {
    getGlobalModelOptions.mockResolvedValueOnce({
      providers: [
        {
          name: 'Nous',
          slug: 'nous',
          models: ['hermes-4'],
          authenticated: true,
          capabilities: { 'hermes-4': { reasoning: false, fast: false } }
        }
      ]
    })

    renderModelSettings()
    await waitFor(() => expect(getHermesConfigRecord).toHaveBeenCalled())

    expect(screen.queryByRole('switch')).toBeNull()
  })

  it('renders plugin-registered auxiliary tasks after the built-ins with the server label (#40880)', async () => {
    getAuxiliaryModels.mockResolvedValueOnce({
      main: { provider: 'nous', model: 'hermes-4' },
      tasks: [
        { task: 'vision', provider: 'auto', model: '', base_url: '' },
        {
          task: 'grill_tab',
          provider: 'openrouter',
          model: 'openai/gpt-5-mini',
          base_url: '',
          label: 'Grill Tab',
          hint: 'Tab-to-grill questions and brief synthesis',
          plugin: 'grill-tab'
        }
      ]
    })
    await renderModelSettings()

    const pluginRow = await screen.findByText('Grill Tab')
    expect(pluginRow).toBeTruthy()
    expect(screen.getByText('Tab-to-grill questions and brief synthesis')).toBeTruthy()
    // Server-declared label is used for the plugin row only; built-ins keep i18n labels.
    expect(screen.getByText('Vision')).toBeTruthy()
    // Built-ins render first; the plugin row is appended.
    const vision = document.getElementById('aux-task-vision')
    const grill = document.getElementById('aux-task-grill_tab')
    expect(vision && grill && vision.compareDocumentPosition(grill) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy()
  })

  it('shows an unpinned inheriting plugin task as following its base, and offers Follow once pinned', async () => {
    getAuxiliaryModels.mockResolvedValueOnce({
      main: { provider: 'nous', model: 'hermes-4' },
      tasks: [
        { task: 'compression', provider: 'openrouter', model: 'vendor/fast', base_url: '' },
        {
          task: 'side_task',
          provider: 'auto',
          model: '',
          base_url: '',
          label: 'Side model',
          hint: 'side model for side',
          plugin: 'side',
          inherit_from: 'compression',
          effective: { provider: 'openrouter', model: 'vendor/fast', base_url: '' }
        },
        {
          task: 'pinned_task',
          provider: 'nous',
          model: 'hermes-4',
          base_url: '',
          label: 'Pinned side',
          hint: 'pinned',
          plugin: 'side',
          inherit_from: 'compression',
          effective: { provider: 'nous', model: 'hermes-4', base_url: '' }
        }
      ]
    })
    await renderModelSettings()

    expect(await screen.findByText('inherits Compression · openrouter · vendor/fast')).toBeTruthy()
    // Only the pinned inheriting row offers the way back to its base.
    const follow = screen.getAllByRole('button', { name: 'Follow Compression' })
    expect(follow).toHaveLength(1)
    expect(document.getElementById('aux-task-pinned_task')?.contains(follow[0])).toBe(true)

    fireEvent.click(follow[0])
    await waitFor(() =>
      expect(setModelAssignment).toHaveBeenCalledWith({
        model: '',
        provider: 'auto',
        reasoning_effort: null,
        scope: 'auxiliary',
        task: 'pinned_task'
      })
    )
  })

  it('edits auxiliary reasoning effort and applies it with the assignment', async () => {
    getAuxiliaryModels.mockResolvedValueOnce({
      main: { provider: 'nous', model: 'hermes-4' },
      tasks: [{ task: 'vision', provider: 'nous', model: 'hermes-4', base_url: '', reasoning_effort: null }]
    })

    renderModelSettings()

    expect(screen.queryByRole('combobox', { name: 'Vision reasoning effort' })).toBeNull()

    fireEvent.click((await screen.findAllByRole('button', { name: 'Change' }))[0])

    fireEvent.click(await screen.findByRole('combobox', { name: 'Vision reasoning effort' }))
    fireEvent.click(await screen.findByRole('option', { name: 'High' }))

    const applyButtons = await screen.findAllByRole('button', { name: 'Apply' })
    fireEvent.click(applyButtons.at(-1)!)

    await waitFor(() =>
      expect(setModelAssignment).toHaveBeenCalledWith({
        model: 'hermes-4',
        provider: 'nous',
        scope: 'auxiliary',
        task: 'vision',
        reasoning_effort: 'high'
      })
    )
  })

  it('assigns an auxiliary task to the main model via setModelAssignment', async () => {
    renderModelSettings()

    // One "Set to main" button per task slot; the first is Vision.
    const setToMainButtons = await screen.findAllByRole('button', { name: 'Set to main' })
    fireEvent.click(setToMainButtons[0])

    await waitFor(() =>
      expect(setModelAssignment).toHaveBeenCalledWith({
        model: 'hermes-4',
        provider: 'nous',
        scope: 'auxiliary',
        task: 'vision'
      })
    )
  })

  it('keeps config-backed settings usable when live model metadata times out (#63214)', async () => {
    getGlobalModelInfo.mockRejectedValueOnce(new Error('Model metadata request timed out'))

    renderModelSettings()

    // Auxiliary assignments are a config-file read: they must still render
    // instead of the whole page waiting on the hung metadata probe.
    expect((await screen.findAllByRole('button', { name: 'Set to main' })).length).toBeGreaterThan(0)
    // The failure surfaces in the load banner rather than skeletons forever.
    await waitFor(() => expect(screen.getByText('Model metadata request timed out')).toBeTruthy())
    // The main-model selector still resolves from the config-backed auxiliary
    // read, so the page is interactive, not just an error shell.
    await waitFor(() => expect(screen.getAllByRole('combobox')[0].textContent).toContain('Nous'))
  })

  it('carries the user-defined endpoint when an aux slot is set to a local main model', async () => {
    getGlobalModelOptions.mockResolvedValueOnce({
      providers: [
        {
          name: 'Ollama',
          slug: 'local-ollama',
          models: ['qwen3:latest'],
          authenticated: true,
          is_user_defined: true,
          api_url: 'http://localhost:11434/v1'
        }
      ]
    })
    getGlobalModelInfo.mockResolvedValueOnce({ provider: 'local-ollama', model: 'qwen3:latest' })
    getAuxiliaryModels.mockResolvedValueOnce({
      main: { provider: 'local-ollama', model: 'qwen3:latest' },
      tasks: [{ task: 'vision', provider: 'auto', model: '', base_url: '' }]
    })

    renderModelSettings()

    const setToMainButtons = await screen.findAllByRole('button', { name: 'Set to main' })
    fireEvent.click(setToMainButtons[0])

    await waitFor(() =>
      expect(setModelAssignment).toHaveBeenCalledWith({
        model: 'qwen3:latest',
        provider: 'local-ollama',
        scope: 'auxiliary',
        task: 'vision',
        base_url: 'http://localhost:11434/v1'
      })
    )
  })

  it('confirms a main model apply with a success notification', async () => {
    clearNotifications()
    setModelAssignment.mockResolvedValueOnce({
      ok: true,
      provider: 'nous',
      model: 'hermes-4',
      gateway_tools: [],
      stale_aux: []
    })

    renderModelSettings()
    await waitFor(() => expect(getGlobalModelInfo).toHaveBeenCalled())

    fireEvent.click(await screen.findByRole('button', { name: 'Apply' }))

    await waitFor(() =>
      expect($notifications.get()).toContainEqual(
        expect.objectContaining({
          kind: 'success',
          title: 'Main model updated',
          message: 'New sessions will use hermes-4.'
        })
      )
    )
  })

  it('warns when a main switch leaves auxiliary tasks pinned to another provider', async () => {
    setModelAssignment.mockResolvedValueOnce({
      ok: true,
      provider: 'openrouter',
      model: 'anthropic/claude-opus-4.7',
      gateway_tools: [],
      stale_aux: [{ task: 'compression', provider: 'nous', model: 'hermes-4' }]
    })

    renderModelSettings()
    await waitFor(() => expect(getGlobalModelInfo).toHaveBeenCalled())

    const applyButton = await screen.findByRole('button', { name: 'Apply' })
    fireEvent.click(applyButton)

    // The switch-time notice names the pinned provider and offers a reset.
    expect(await screen.findByText(/still run on/)).toBeTruthy()
    expect(screen.getByText('nous')).toBeTruthy()
  })

  it.each(['zh', 'zh-hant'] as const)(
    'localizes stale auxiliary warnings in %s without resetting assignments',
    async locale => {
      getAuxiliaryModels.mockResolvedValueOnce({
        main: { provider: 'nous', model: 'hermes-4' },
        tasks: [{ task: 'curator', provider: 'openrouter', model: 'fixture-model', base_url: '' }]
      })
      const client = new QueryClient({ defaultOptions: { queries: { retry: false } } })
      render(
        <MemoryRouter>
          <I18nProvider configClient={null} initialLocale={locale}>
            <QueryClientProvider client={client}>
              <ModelSettings />
            </QueryClientProvider>
          </I18nProvider>
        </MemoryRouter>
      )
      expect(await screen.findByText(/仍由/)).toBeTruthy()
      expect(screen.getByText('openrouter')).toBeTruthy()
      expect(
        screen.getAllByRole('button', { name: TRANSLATIONS[locale].settings.model.resetAllToMain }).length
      ).toBeGreaterThan(0)
      expect(screen.queryByText(/still run on/)).toBeNull()
      expect(setModelAssignment).not.toHaveBeenCalled()
      client.clear()
    }
  )

  it('shows a persistent banner when a loaded aux slot mismatches the main provider', async () => {
    getAuxiliaryModels.mockResolvedValueOnce({
      main: { provider: 'nous', model: 'hermes-4' },
      tasks: [{ task: 'curator', provider: 'openrouter', model: 'anthropic/claude-opus-4.7', base_url: '' }]
    })

    renderModelSettings()

    // Banner present on load, no switch required.
    expect(await screen.findByText(/still run on/)).toBeTruthy()
  })

  it('does not warn when an aux slot uses the main alias', async () => {
    getAuxiliaryModels.mockResolvedValueOnce({
      main: { provider: 'nous', model: 'hermes-4' },
      tasks: [{ task: 'vision', provider: 'main', model: 'kimi-k3', base_url: '' }]
    })

    renderModelSettings()
    await screen.findAllByRole('button', { name: 'Set to main' })

    // 'main' is a backend-supported alias that tracks the active main provider
    // (auxiliary_client._normalize_aux_provider) — it can never be a stale pin. #97310
    expect(screen.queryByText(/still run on/)).toBeNull()
  })

  it('does not flag an aux slot pinned to a local/LAN endpoint and shows its base_url', async () => {
    getAuxiliaryModels.mockResolvedValueOnce({
      main: { provider: 'ollama-cloud', model: 'glm-5.3-flash' },
      tasks: [
        {
          task: 'title_generation',
          provider: 'openai',
          model: 'llama3.2:3b',
          base_url: 'http://byron.local:11434/v1',
          local_endpoint: true
        },
        {
          task: 'vision',
          provider: 'openai',
          model: 'gpt-4o-mini',
          base_url: 'https://api.example.com/v1',
          local_endpoint: false
        }
      ]
    })

    renderModelSettings()

    // The public custom endpoint still bills a provider, so the banner stays —
    // but it names only that one task, not the free LAN pin.
    expect(await screen.findByText(/1 auxiliary task \(/)).toBeTruthy()
    // The row shows where the pinned task actually points.
    expect(screen.getByText(/http:\/\/byron\.local:11434\/v1/)).toBeTruthy()
  })
})

describe('ModelSettings provider switch', () => {
  // #59063: a provider's model list is per-provider. Switching must not carry
  // the previous provider's model over (ModelSelect's withActive() would keep
  // painting it), and an empty freshly-selected custom provider must be probed
  // via a refresh-scoped options fetch rather than left at 0 models.
  it('clears the selected model when the user switches provider', async () => {
    getGlobalModelOptions.mockResolvedValue({
      providers: [
        { name: 'Nous', slug: 'nous', models: ['hermes-4'], authenticated: true },
        {
          name: 'Custom endpoint',
          slug: 'custom:lab',
          api_url: 'http://10.0.0.2:8080/v1',
          is_user_defined: true,
          source: 'user-config',
          models: ['gemma-4-12b-omni'],
          authenticated: true
        }
      ]
    })

    renderModelSettings()
    await screen.findAllByRole('combobox')

    // The model select paints the current provider's model.
    expect(await screen.findByText('hermes-4')).toBeTruthy()

    // Switch provider through the main selector (first combobox opens the
    // provider dropdown).
    fireEvent.click(screen.getAllByRole('combobox')[0])
    fireEvent.click(await screen.findByRole('option', { name: /Custom endpoint/ }))

    // The previous provider's model must not carry over.
    await waitFor(() => expect(screen.queryByText('hermes-4')).toBeNull())
  })

  it('probes an empty custom provider with a refresh-scoped options fetch on switch', async () => {
    getGlobalModelOptions.mockResolvedValue({
      providers: [
        { name: 'Nous', slug: 'nous', models: ['hermes-4'], authenticated: true },
        {
          name: 'Lab',
          slug: 'custom:lab',
          api_url: 'http://10.0.0.2:8080/v1',
          is_user_defined: true,
          source: 'user-config',
          models: [],
          authenticated: true
        }
      ]
    })

    renderModelSettings()
    await screen.findAllByRole('combobox')

    // The refresh-scoped fetch repopulates the empty provider's models.
    getGlobalModelOptions.mockResolvedValueOnce({
      providers: [
        { name: 'Nous', slug: 'nous', models: ['hermes-4'], authenticated: true },
        {
          name: 'Lab',
          slug: 'custom:lab',
          api_url: 'http://10.0.0.2:8080/v1',
          is_user_defined: true,
          source: 'user-config',
          models: ['gemma-4-12b-omni'],
          authenticated: true
        }
      ]
    })

    fireEvent.click(screen.getAllByRole('combobox')[0])
    fireEvent.click(await screen.findByRole('option', { name: /Lab/ }))

    await waitFor(() => {
      // refresh-scoped, not the plain read the mount effect already made
      expect(getGlobalModelOptions).toHaveBeenCalledWith({ refresh: true }, undefined)
    })

    // The refreshed catalog repopulates the empty provider's models: open the
    // model select and the discovered model is offered.
    fireEvent.click(screen.getAllByRole('combobox')[1])
    expect(await screen.findByRole('option', { name: 'gemma-4-12b-omni' })).toBeTruthy()
  })
})

describe('ModelSettings MoA preset editor', () => {
  const moaConfig = () => ({
    default_preset: 'default',
    active_preset: '',
    presets: {
      default: {
        reference_models: [
          { provider: 'nous', model: 'hermes-4' },
          { provider: 'openrouter', model: 'deepseek/deepseek-v4-pro' }
        ],
        aggregator: { provider: 'openrouter', model: 'anthropic/claude-opus-4.8' },
        reference_temperature: 0,
        aggregator_temperature: 0,

        enabled: true
      }
    },
    reference_models: [
      { provider: 'nous', model: 'hermes-4' },
      { provider: 'openrouter', model: 'deepseek/deepseek-v4-pro' }
    ],
    aggregator: { provider: 'openrouter', model: 'anthropic/claude-opus-4.8' },
    reference_temperature: 0,
    aggregator_temperature: 0,

    enabled: true
  })

  beforeEach(() => {
    getGlobalModelOptions.mockResolvedValue({
      providers: [
        {
          name: 'Nous',
          slug: 'nous',
          models: ['hermes-4', 'hermes-4-mini'],
          authenticated: true,
          capabilities: { 'hermes-4': { reasoning: true, fast: true } }
        },
        {
          name: 'OpenRouter',
          slug: 'openrouter',
          models: ['deepseek/deepseek-v4-pro', 'anthropic/claude-opus-4.8'],
          authenticated: true
        }
      ]
    })
    getMoaModels.mockResolvedValue(moaConfig())
    saveMoaModels.mockImplementation((body: unknown) => Promise.resolve(body))
  })

  it.each(['zh', 'zh-hant', 'ja'] as const)(
    'localizes MoA preset and reference controls in %s without changing their saved identities',
    async locale => {
      const client = new QueryClient({ defaultOptions: { queries: { retry: false } } })
      const m = TRANSLATIONS[locale].settings.model
      render(
        <MemoryRouter>
          <I18nProvider configClient={null} initialLocale={locale}>
            <QueryClientProvider client={client}>
              <ModelSettings subpage="moa" />
            </QueryClientProvider>
          </I18nProvider>
        </MemoryRouter>
      )
      expect(m.moaDescription).not.toBe(TRANSLATIONS.en.settings.model.moaDescription)
      expect(m.moaReferenceHint).not.toBe(TRANSLATIONS.en.settings.model.moaReferenceHint)
      expect(m.moaAggregatorBilled).not.toBe(TRANSLATIONS.en.settings.model.moaAggregatorBilled)
      await screen.findByText(m.moaDescription)
      expect(screen.getByText(m.moaReferenceTitle(1))).toBeTruthy()
      expect(screen.getByText(m.moaAggregator)).toBeTruthy()
      expect(screen.getByRole('button', { name: m.moaAddReference })).toBeTruthy()
      expect(screen.getByRole('button', { name: m.moaSetDefault })).toBeTruthy()
      expect(screen.getByPlaceholderText(m.moaNewPresetPlaceholder)).toBeTruthy()
      fireEvent.click(screen.getByRole('switch', { name: m.moaReferenceToggle(true, 1) }))
      expect(screen.getByRole('switch', { name: m.moaReferenceToggle(false, 1) }).getAttribute('aria-checked')).toBe(
        'false'
      )
      await waitFor(() => expect(saveMoaModels).toHaveBeenCalled())
      const saved = saveMoaModels.mock.calls.at(-1)![0] as ReturnType<typeof moaConfig>
      expect(saved.default_preset).toBe('default')
      expect(saved.presets.default.reference_models[0]).toMatchObject({
        provider: 'nous',
        model: 'hermes-4',
        enabled: false
      })
    }
  )

  async function openReferenceEditor() {
    renderModelSettings()
    expect(await screen.findByText('Reference 1')).toBeTruthy()
  }

  function slotSelects() {
    // Combobox order in the MoA section (last 7 on the page): preset select,
    // then provider+model per reference (2 refs), then aggregator
    // provider+model. Reference 1's pair is therefore at -6 / -5.
    const all = screen.getAllByRole('combobox')

    return { ref1Provider: all.at(-6)!, ref1Model: all.at(-5)! }
  }

  it('holds the autosave while a slot is half-filled (provider changed, model pending)', async () => {
    vi.useFakeTimers({ shouldAdvanceTime: true })

    try {
      await openReferenceEditor()

      fireEvent.click(slotSelects().ref1Provider)
      fireEvent.click(await screen.findByRole('option', { name: 'OpenRouter' }))

      // Model was cleared by the provider change → config incomplete → the
      // debounced autosave must NOT fire, even well past the 600ms window.
      await vi.advanceTimersByTimeAsync(2000)
      expect(saveMoaModels).not.toHaveBeenCalled()
    } finally {
      vi.useRealTimers()
    }
  })

  it('saves once the model pick completes the slot', async () => {
    vi.useFakeTimers({ shouldAdvanceTime: true })

    try {
      await openReferenceEditor()

      fireEvent.click(slotSelects().ref1Provider)
      fireEvent.click(await screen.findByRole('option', { name: 'OpenRouter' }))
      await vi.advanceTimersByTimeAsync(700)

      fireEvent.click(slotSelects().ref1Model)
      fireEvent.click(await screen.findByRole('option', { name: 'anthropic/claude-opus-4.8' }))
      await vi.advanceTimersByTimeAsync(700)

      expect(saveMoaModels).toHaveBeenCalledTimes(1)
      const sent = saveMoaModels.mock.calls[0][0] as ReturnType<typeof moaConfig>
      expect(sent.presets.default.reference_models[0]).toEqual({
        provider: 'openrouter',
        model: 'anthropic/claude-opus-4.8'
      })
      // The untouched slots ride along unchanged — nothing reverts to defaults.
      expect(sent.presets.default.reference_models[1]).toEqual({
        provider: 'openrouter',
        model: 'deepseek/deepseek-v4-pro'
      })
      expect(sent.presets.default.aggregator).toEqual({
        provider: 'openrouter',
        model: 'anthropic/claude-opus-4.8'
      })
    } finally {
      vi.useRealTimers()
    }
  })

  it('autosaves the selected preset when its enabled switch is toggled', async () => {
    vi.useFakeTimers({ shouldAdvanceTime: true })

    try {
      await openReferenceEditor()

      fireEvent.click(screen.getByRole('switch', { name: 'Enabled' }))
      await vi.advanceTimersByTimeAsync(700)

      expect(saveMoaModels).toHaveBeenCalledWith(
        expect.objectContaining({
          presets: expect.objectContaining({
            default: expect.objectContaining({ enabled: false })
          })
        })
      )
    } finally {
      vi.useRealTimers()
    }
  })

  it('saves a disabled reference model without removing it (per-slot enabled toggle)', async () => {
    vi.useFakeTimers({ shouldAdvanceTime: true })

    try {
      await openReferenceEditor()

      fireEvent.click(screen.getByRole('switch', { name: 'Disable reference 1' }))
      await vi.advanceTimersByTimeAsync(700)

      expect(saveMoaModels).toHaveBeenCalledWith(
        expect.objectContaining({
          presets: expect.objectContaining({
            default: expect.objectContaining({
              reference_models: [
                expect.objectContaining({ provider: 'nous', model: 'hermes-4', enabled: false }),
                expect.objectContaining({ provider: 'openrouter', model: 'deepseek/deepseek-v4-pro' })
              ]
            })
          })
        })
      )
    } finally {
      vi.useRealTimers()
    }
  })
})

describe('ModelSettings stale-aux banner dismissal', () => {
  beforeEach(() => {
    window.localStorage.clear()
  })

  afterEach(() => {
    window.localStorage.clear()
  })

  const staleAux = {
    main: { provider: 'nous', model: 'hermes-4' },
    tasks: [{ task: 'vision', provider: 'alibaba', model: 'qwen3.6-flash', base_url: '' }]
  }

  it('hides the persistent stale-aux banner after acknowledging it, and re-arms when the main provider changes', async () => {
    getAuxiliaryModels.mockResolvedValue(staleAux)

    // First visit: the deliberate cross-provider pin surfaces the banner.
    renderModelSettings()
    expect(await screen.findByText(/still run on/)).toBeTruthy()
    fireEvent.click(await screen.findByRole('button', { name: TRANSLATIONS.en.settings.model.staleAuxDismiss }))

    await waitFor(() => expect(screen.queryByText(/still run on/)).toBeNull())

    // Second visit (fresh mount): the acknowledgement persists.
    cleanup()
    renderModelSettings()
    await waitFor(() => expect(getAuxiliaryModels).toHaveBeenCalled())
    await waitFor(() => expect(screen.queryByText(/still run on/)).toBeNull())

    // A main-provider switch re-arms the banner: the acknowledged
    // configuration no longer matches what is running.
    cleanup()
    getGlobalModelInfo.mockResolvedValue({ provider: 'openrouter', model: 'hermes-4' })
    renderModelSettings()
    expect(await screen.findByText(/still run on/)).toBeTruthy()
    // The dismiss affordance is offered again for the new configuration.
    expect(screen.getByRole('button', { name: TRANSLATIONS.en.settings.model.staleAuxDismiss })).toBeTruthy()
  })

  it('keeps the post-switch notice undismissable — it announces a change that just happened', async () => {
    setModelAssignment.mockResolvedValueOnce({
      ok: true,
      provider: 'openrouter',
      model: 'anthropic/claude-opus-4.7',
      gateway_tools: [],
      stale_aux: [{ task: 'compression', provider: 'nous', model: 'hermes-4' }]
    })

    renderModelSettings()
    await waitFor(() => expect(getGlobalModelInfo).toHaveBeenCalled())

    fireEvent.click(await screen.findByRole('button', { name: 'Apply' }))

    expect(await screen.findByText(/still run on/)).toBeTruthy()
    expect(screen.queryByRole('button', { name: TRANSLATIONS.en.settings.model.staleAuxDismiss })).toBeNull()
  })
})

describe('ModelSettings code-skew 503', () => {
  const skewError = new Error(
    'Error invoking remote method \'hermes:api\': Error: 503: {"detail":"Restart required: This process is running code from 08b4875f4a but the checkout on disk is now 48d2528066. The model picker would risk a stale-module crash — restart the Desktop-owned backend to load the new code (use Restart backend in Hermes Desktop, or quit and reopen the app)"}'
  )

  afterEach(() => {
    delete (window as unknown as { hermesDesktop?: unknown }).hermesDesktop
  })

  it('unwraps the stale-backend 503 instead of dumping IPC JSON', async () => {
    getGlobalModelOptions.mockRejectedValueOnce(skewError)

    renderModelSettings()

    await waitFor(() => {
      expect(screen.getByText(/running old code after an update/i)).toBeTruthy()
    })
    expect(screen.getByRole('button', { name: 'Restart backend' })).toBeTruthy()
    expect(screen.queryByText(/hermes:api/)).toBeNull()
    expect(screen.queryByText(/systemctl/)).toBeNull()
  })

  it('recycles the Desktop-owned backend and reloads the catalog', async () => {
    const recycleBackend = vi.fn().mockResolvedValue({ ok: true })

    ;(window as unknown as { hermesDesktop: { recycleBackend: typeof recycleBackend } }).hermesDesktop = {
      recycleBackend
    }

    getGlobalModelOptions.mockRejectedValueOnce(skewError)

    renderModelSettings()
    await waitFor(() => expect(screen.getByRole('button', { name: 'Restart backend' })).toBeTruthy())

    fireEvent.click(screen.getByRole('button', { name: 'Restart backend' }))

    await waitFor(() => expect(recycleBackend).toHaveBeenCalledWith(undefined))
    await waitFor(() => expect(getGlobalModelOptions.mock.calls.length).toBeGreaterThan(1))
  })
})