Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

99.7 KB
Raw Blame
import { act, fireEvent, screen, waitFor, within } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { listConnections as listComposioConnections } from '../../../../lib/composio/composioApi';
import { getCoreStateSnapshot, setCoreStateSnapshot } from '../../../../lib/coreState/store';
import { I18nProvider } from '../../../../lib/i18n/I18nContext';
import {
  clearCloudProviderKey,
  completeOpenAiCodexOAuth,
  flushCloudProviders,
  importOpenAiCodexCliAuth,
  listProviderModels,
  loadAISettings,
  loadLocalProviderSnapshot,
  loadProviderAuthErrors,
  OPENAI_CODEX_OAUTH_MISSING_AUTH_URL,
  OPENAI_CODEX_OAUTH_MISSING_CALLBACK_URL,
  saveAISettings,
  setCloudProviderKey,
  startOpenAiCodexOAuth,
  testProviderModel,
} from '../../../../services/api/aiSettingsApi';
import { creditsApi } from '../../../../services/api/creditsApi';
import { callCoreRpc } from '../../../../services/coreRpcClient';
import { renderWithProviders } from '../../../../test/test-utils';
import { createLocalSessionToken } from '../../../../utils/localSession';
import { connectOpenRouterViaOAuth } from '../../../../utils/openrouterOAuth';
import { openUrl } from '../../../../utils/openUrl';
import { isTauri } from '../../../../utils/tauriCommands/common';
// Lazy import so the typed mock is available to individual tests.
import { openhumanUpdateLocalAiSettings as openhumanUpdateLocalAiSettingsMock } from '../../../../utils/tauriCommands/config';
import AIPanel, {
  BackgroundLoopControls,
  buildRoutingDiffSummary,
  type RoutingMap,
} from '../AIPanel';

vi.mock('../../../../services/api/aiSettingsApi', async importOriginal => {
  const actual = await importOriginal<typeof import('../../../../services/api/aiSettingsApi')>();
  return {
    ALL_WORKLOADS: ['chat', 'reasoning', 'agentic', 'coding', 'memory', 'embeddings'],
    loadAISettings: vi.fn(),
    saveAISettings: vi.fn(),
    loadLocalProviderSnapshot: vi.fn(),
    loadProviderAuthErrors: vi.fn().mockResolvedValue([]),
    testProviderModel: vi.fn(),
    // #5341: use the REAL classifier + describer (pure, translator-driven) instead
    // of a hand-copied third implementation that could drift from the source. The
    // classification rules (403/proxy handling, etc.) are unit-tested in
    // aiSettingsApi.test.ts; here they drive the real component branch.
    classifyProviderVerificationFailure: actual.classifyProviderVerificationFailure,
    describeProviderVerificationFailure: actual.describeProviderVerificationFailure,
    modelRegistryVision: vi.fn(() => false),
    upsertModelRegistryVision: vi.fn((registry: unknown[]) => registry),
    setCloudProviderKey: vi.fn().mockResolvedValue(undefined),
    clearCloudProviderKey: vi.fn().mockResolvedValue(undefined),
    serializeProviderRef: vi.fn((r: { kind: string; providerSlug?: string; model?: string }) =>
      r.kind === 'openhuman'
        ? 'openhuman'
        : r.kind === 'local'
          ? `ollama:${r.model}`
          : `${r.providerSlug}:${r.model}`
    ),
    localProvider: { download: vi.fn(), applyPreset: vi.fn() },
    flushCloudProviders: vi.fn().mockResolvedValue(undefined),
    importOpenAiCodexCliAuth: vi.fn().mockResolvedValue(undefined),
    listProviderModels: vi.fn().mockResolvedValue([]),
    OPENAI_CODEX_OAUTH_MISSING_AUTH_URL: 'OPENAI_CODEX_OAUTH_MISSING_AUTH_URL',
    OPENAI_CODEX_OAUTH_MISSING_CALLBACK_URL: 'OPENAI_CODEX_OAUTH_MISSING_CALLBACK_URL',
    startOpenAiCodexOAuth: vi.fn(),
    completeOpenAiCodexOAuth: vi.fn(),
  };
});

vi.mock('../../hooks/useSettingsNavigation', () => ({
  useSettingsNavigation: () => ({
    navigateBack: vi.fn(),
    navigateToSettings: vi.fn(),
    breadcrumbs: [],
  }),
}));

vi.mock('../../../../services/api/creditsApi', () => ({
  creditsApi: { getTeamUsage: vi.fn(), getTransactions: vi.fn() },
}));

vi.mock('../../../../lib/composio/composioApi', () => ({ listConnections: vi.fn() }));

// The Ollama / LM Studio toggle persists `local_ai.base_url` via this command.
// Mock it so tests can assert the call shape without crossing into Tauri IPC.
vi.mock('../../../../utils/tauriCommands/config', async () => {
  const actual = await vi.importActual<typeof import('../../../../utils/tauriCommands/config')>(
    '../../../../utils/tauriCommands/config'
  );
  return {
    ...actual,
    openhumanUpdateLocalAiSettings: vi
      .fn()
      .mockResolvedValue({ result: { config: {}, workspace_dir: '', config_path: '' }, logs: [] }),
  };
});

vi.mock('../../../../utils/openrouterOAuth', () => ({ connectOpenRouterViaOAuth: vi.fn() }));
vi.mock('../../../../utils/openUrl', () => ({ openUrl: vi.fn() }));

vi.mock('../../../../services/coreRpcClient', async importOriginal => ({
  ...(await importOriginal<typeof import('../../../../services/coreRpcClient')>()),
  callCoreRpc: vi.fn(),
}));
vi.mock('../../../../utils/tauriCommands/common', async importOriginal => ({
  ...(await importOriginal<typeof import('../../../../utils/tauriCommands/common')>()),
  isTauri: vi.fn(() => false),
}));

const baseSettings = {
  cloudProviders: [
    {
      id: 'p_oh_x',
      slug: 'openhuman',
      label: 'OpenHuman',
      endpoint: 'https://api.openhuman.ai/v1',
      auth_style: 'openhuman_jwt' as const,
      has_api_key: false,
    },
  ],
  routing: {
    chat: { kind: 'openhuman' as const },
    reasoning: { kind: 'openhuman' as const },
    agentic: { kind: 'openhuman' as const },
    coding: { kind: 'openhuman' as const },
    vision: { kind: 'openhuman' as const },
    memory: { kind: 'openhuman' as const },
    embeddings: { kind: 'openhuman' as const },
  },
  modelRegistry: [],
};

const baseLocalSnapshot = { status: null, diagnostics: null, presets: null, installedModels: [] };

const openGlobalModelPicker = async () => {
  const label = await screen.findByText('Provider and model');
  const button = label.closest('button');
  expect(button).not.toBeNull();
  fireEvent.click(button!);
};

/** Open the per-workload custom routing dialog for the row labelled `label`. */
const openWorkloadDialog = async (label: string) => {
  const rowEl = (await screen.findByText(label)).closest('[data-slot="workload-row"]');
  expect(rowEl).not.toBeNull();
  fireEvent.click(within(rowEl as HTMLElement).getByRole('button'));
  await screen.findByRole('dialog', { name: /Custom routing/i });
};

const selectPickerProvider = async (name: RegExp) => {
  fireEvent.click(await screen.findByRole('button', { name }));
};

const openCustomProviderEditor = async () => {
  fireEvent.click(await screen.findByTestId('add-provider-open'));
  fireEvent.click(await screen.findByTestId('add-provider-custom'));
};

const openProviderConnectDialog = async (
  slug: string,
  category: 'cloud' | 'local' | 'cli' = 'cloud'
) => {
  fireEvent.click(await screen.findByTestId('add-provider-open'));
  const trigger = await screen.findByTestId(`add-provider-select-${category}`);
  trigger.focus();
  fireEvent.keyDown(trigger, { key: 'Enter' });
  fireEvent.keyDown(await screen.findByTestId(`add-provider-option-${slug}`), { key: 'Enter' });
};

const openProviderRowAction = async (slug: string, action: RegExp) => {
  const row = await screen.findByTestId(`provider-row-${slug}`);
  const trigger = within(row).getByRole('button');
  trigger.focus();
  fireEvent.keyDown(trigger, { key: 'Enter' });
  fireEvent.click(await screen.findByRole('menuitem', { name: action }));
};

const baseUsage = {
  remainingUsd: 1.5,
  cycleBudgetUsd: 10,
  cycleSpentUsd: 8.5,
  cycleStartDate: '2026-05-14T00:00:00.000Z',
  cycleEndsAt: '2026-05-21T00:00:00.000Z',
  plan: {
    plan: 'BASIC',
    name: 'Basic',
    marginPercent: 25,
    payAsYouGoMarginPercent: 50,
    discountVsPayAsYouGoPercent: 50,
  },
  insights: {
    period: { startDate: '2026-05-14T00:00:00.000Z', endDate: '2026-05-21T00:00:00.000Z' },
    totals: {
      inferenceUsd: 6,
      integrationsUsd: 2.5,
      totalUsd: 8.5,
      inferenceCalls: 120,
      integrationCalls: 6,
    },
    dailySeries: [],
    topModels: [],
    topIntegrations: [],
  },
};

const baseTransactions = [
  {
    id: 'older',
    type: 'SPEND' as const,
    action: 'SPEND:USAGE_DEDUCTION:USER',
    amountUsd: -0.25,
    balanceAfterUsd: 9.75,
    createdAt: '2026-05-17T01:00:00.000Z',
  },
  {
    id: 'earn',
    type: 'EARN' as const,
    action: 'TOPUP',
    amountUsd: 1,
    balanceAfterUsd: 10.75,
    createdAt: '2026-05-17T02:00:00.000Z',
  },
  {
    id: 'latest',
    type: 'SPEND' as const,
    action: 'MEMORY_SUMMARY',
    amountUsd: -0.5,
    balanceAfterUsd: 9.25,
    createdAt: '2026-05-17T03:00:00.000Z',
  },
];

const baseConnections = [
  { id: 'cal-1', toolkit: 'googlecalendar', status: 'ACTIVE' },
  { id: 'cal-2', toolkit: 'calendar', status: 'CONNECTED' },
  { id: 'cal-3', toolkit: 'google_calendar', status: 'ACTIVE' },
  { id: 'slack-1', toolkit: 'slack', status: 'ACTIVE' },
  { id: 'pending-cal', toolkit: 'googlecalendar', status: 'PENDING' },
];

// AIPanel tests mount no CoreStateProvider, so the session hooks read the
// module-level snapshot store. Seed it with a real (non-local) session so the
// managed OpenHuman row — which only exists for real sessions — is available.
const originalCoreState = getCoreStateSnapshot();
function seedSessionToken(sessionToken: string | null) {
  setCoreStateSnapshot({
    ...originalCoreState,
    snapshot: { ...originalCoreState.snapshot, sessionToken },
  });
}

describe('AIPanel', () => {
  afterEach(() => setCoreStateSnapshot(originalCoreState));

  beforeEach(() => {
    vi.clearAllMocks();
    seedSessionToken('header.payload.signature');
    vi.mocked(isTauri).mockReturnValue(false);
    vi.mocked(loadAISettings).mockResolvedValue(baseSettings);
    vi.mocked(loadLocalProviderSnapshot).mockResolvedValue(baseLocalSnapshot);
    vi.mocked(loadProviderAuthErrors).mockResolvedValue([]);
    vi.mocked(setCloudProviderKey).mockResolvedValue(undefined);
    vi.mocked(clearCloudProviderKey).mockResolvedValue(undefined);
    vi.mocked(importOpenAiCodexCliAuth).mockResolvedValue(undefined);
    vi.mocked(testProviderModel).mockResolvedValue({ reply: 'Hello from the selected model.' });
    vi.mocked(listProviderModels).mockResolvedValue([]);
    vi.mocked(startOpenAiCodexOAuth).mockResolvedValue({
      authUrl: 'https://auth.openai.com/oauth/authorize?client_id=test',
    });
    vi.mocked(completeOpenAiCodexOAuth).mockResolvedValue(undefined);
    vi.mocked(openUrl).mockResolvedValue(undefined);
    vi.mocked(connectOpenRouterViaOAuth).mockResolvedValue('sk-or-oauth');
    vi.mocked(creditsApi.getTeamUsage).mockResolvedValue(baseUsage);
    vi.mocked(creditsApi.getTransactions).mockResolvedValue({
      transactions: baseTransactions,
      total: baseTransactions.length,
    });
    vi.mocked(listComposioConnections).mockResolvedValue({ connections: baseConnections });
  });

  it('renders the LLM Providers + Routing top-level section headers', async () => {
    renderWithProviders(<AIPanel />);
    await waitFor(() => expect(screen.getAllByText(/^LLM Providers$/).length).toBeGreaterThan(0));
    // The Local provider sub-section was removed entirely.
    expect(screen.queryByText(/Local provider/i)).not.toBeInTheDocument();
    // The old "Auth" header was renamed to "LLM Providers"; "Cloud providers"
    // sub-label is gone in favour of the chip toggles.
    expect(screen.queryByText(/^Auth$/)).not.toBeInTheDocument();
    expect(screen.queryByText(/^Cloud providers$/)).not.toBeInTheDocument();
    expect(screen.getAllByText(/^Routing$/).length).toBeGreaterThan(0);
  });

  it('surfaces a provider-error notice when a BYO key was rejected at runtime', async () => {
    vi.mocked(loadProviderAuthErrors).mockResolvedValue([
      {
        provider: 'openrouter',
        status: 401,
        message:
          'openrouter rejected the API key (HTTP 401). Update your openrouter API key in Connections → API keys → LLM to restore it.',
        timestamp_ms: 1000,
      },
    ]);
    renderWithProviders(<AIPanel />);
    expect(await screen.findByText(/rejected the API key/i)).toBeInTheDocument();
  });

  it('renders no provider-error notice when there are no rejected keys', async () => {
    renderWithProviders(<AIPanel />);
    await waitFor(() => expect(screen.getAllByText(/^LLM Providers$/).length).toBeGreaterThan(0));
    expect(screen.queryByText(/rejected the API key/i)).not.toBeInTheDocument();
  });

  it('renders the OpenHuman primary card after load', async () => {
    renderWithProviders(<AIPanel />);
    // The OpenHuman label now appears in multiple places (provider card,
    // each workload routing row's "↳ OpenHuman" resolution hint), so we
    // assert at-least-one match rather than getByText.
    await waitFor(() => expect(screen.getAllByText(/OpenHuman/i).length).toBeGreaterThan(0));
  });

  it('renders Managed as an always-on badge, not a switchable toggle (#3760)', async () => {
    renderWithProviders(<AIPanel />);
    // The Managed chip must show an "Always on" indicator...
    expect(await screen.findByText(/^Always on$/i)).toBeInTheDocument();
    // ...and must NOT render a toggle switch users would try (and fail) to flip.
    expect(screen.queryByRole('switch', { name: /Managed/i })).toBeNull();
    // A hint points users wanting a different provider at the Routing tab.
    expect(
      screen.getByText(/Choose which provider each task uses on the Routing tab/i)
    ).toBeInTheDocument();
  });

  it('omits the managed OpenHuman row for a local ("Continue Locally") session', async () => {
    seedSessionToken(createLocalSessionToken());
    renderWithProviders(<AIPanel />);
    await waitFor(() => expect(screen.getAllByText(/^LLM Providers$/).length).toBeGreaterThan(0));
    expect(screen.queryByTestId('provider-row-openhuman')).not.toBeInTheDocument();
    expect(screen.queryByText(/^Always on$/i)).not.toBeInTheDocument();
  });

  it('shows the per-workload routing tables directly, with no mode selector', async () => {
    renderWithProviders(<AIPanel />);
    fireEvent.click(await screen.findByRole('tab', { name: /^Routing$/i }));
    await waitFor(() => expect(screen.getByText('Chat')).toBeInTheDocument());
    expect(screen.queryByRole('radio', { name: /Managed/i })).toBeNull();
    expect(screen.queryByRole('radio', { name: /Use Your Own Models/i })).toBeNull();
    expect(screen.queryByRole('radio', { name: /Advanced/i })).toBeNull();
  });

  it('pins a managed default model from the routing page', async () => {
    vi.mocked(listProviderModels).mockResolvedValue([
      { id: 'openrouter/deepseek/deepseek-v4-flash', display_name: 'DeepSeek V4 Flash' },
      { id: 'openrouter/z/other', display_name: 'Other' },
    ]);
    vi.mocked(saveAISettings).mockResolvedValue(undefined);
    renderWithProviders(<AIPanel />);
    fireEvent.click(await screen.findByRole('tab', { name: /^Routing$/i }));

    // Nothing pinned yet: the row says so rather than naming a tier.
    const row = await screen.findByTestId('default-model-row');
    expect(row).toHaveTextContent('Not set');
    fireEvent.click(within(row).getByTestId('default-model-change'));

    // The picker is the managed catalog only, opened on the recommended model.
    await waitFor(() => expect(listProviderModels).toHaveBeenCalledWith('openhuman'));
    const recommended = await screen.findByTestId(
      'model-picker-managed-option-openrouter/deepseek/deepseek-v4-flash'
    );
    expect(recommended).toHaveAttribute('aria-selected', 'true');
    fireEvent.click(screen.getByTestId('model-picker-managed-option-openrouter/z/other'));
    fireEvent.click(screen.getByRole('button', { name: /Use this model/i }));

    await waitFor(() => expect(saveAISettings).toHaveBeenCalled());
    const [, nextSettings] = vi.mocked(saveAISettings).mock.calls.at(-1) ?? [];
    expect(nextSettings?.defaultModel).toBe('openrouter/z/other');
    await waitFor(() =>
      expect(screen.getByTestId('default-model-row')).toHaveTextContent('openrouter/z/other')
    );
  });

  it('renders all visible advanced workload labels', async () => {
    renderWithProviders(<AIPanel />);
    fireEvent.click(await screen.findByRole('tab', { name: /^Routing$/i }));
    await waitFor(() => expect(screen.getByText('Chat')).toBeInTheDocument());
    for (const label of [
      'Chat',
      'Reasoning',
      'Agentic',
      'Coding',
      'Vision',
      'Memory summarization',
    ]) {
      expect(screen.getByText(label)).toBeInTheDocument();
    }
  });

  // ─── per-model vision flag (BYOK) ───────────────────────────────────────────

  // ─── Azure deployment names (#5213) ─────────────────────────────────────────

  // Regression: Azure's `/models` catalog lists *base model ids*, but Azure
  // routes inference by the user's *deployment name*. Before the fix a
  // non-empty catalog forced a closed <select>, so a deployment name that was
  // not in the catalog could not be entered at all and every request came back
  // "Model not found".
  const azureSettings = {
    ...baseSettings,
    cloudProviders: [
      ...baseSettings.cloudProviders,
      {
        id: 'p_azure_x',
        slug: 'azure-foundry',
        label: 'Azure Foundry',
        endpoint: 'https://my-resource.openai.azure.com/openai/v1',
        auth_style: 'bearer' as const,
        has_api_key: true,
      },
    ],
  };

  it('lets an Azure provider take a deployment name that is absent from the model catalog', async () => {
    vi.mocked(loadAISettings).mockResolvedValue(azureSettings);
    // A NON-empty catalog is the pre-fix blocker: it used to force a dropdown.
    vi.mocked(listProviderModels).mockResolvedValue([
      { id: 'gpt-5.6-terra-2026-07-09' },
      { id: 'gpt-4o' },
    ]);

    renderWithProviders(<AIPanel />);
    fireEvent.click(await screen.findByRole('tab', { name: /^Routing$/i }));
    await openWorkloadDialog('Chat');
    await openGlobalModelPicker();
    await selectPickerProvider(/Azure Foundry/i);

    // The field is a free-text "Deployment name" box, not a catalog dropdown.
    const deploymentInput = await screen.findByRole('textbox', { name: /Deployment name/i });
    fireEvent.change(deploymentInput, { target: { value: 'gpt-5.6-terra' } });
    fireEvent.click(screen.getByRole('button', { name: /Use this model/i }));

    fireEvent.click(screen.getByRole('button', { name: /^Save$/ }));

    await waitFor(() => expect(saveAISettings).toHaveBeenCalled());
    // The deployment name reaches the persisted routing verbatim, and the base
    // model id from the catalog is never substituted for it.
    const [, nextSettings] = vi.mocked(saveAISettings).mock.calls.at(-1) ?? [];
    expect(nextSettings?.routing.chat).toEqual({
      kind: 'cloud',
      providerSlug: 'azure-foundry',
      model: 'gpt-5.6-terra',
      // The dialog always carries the override slot; `null` means "no override"
      // and never reaches the wire — `joinModelAndTemp` emits the bare model id
      // unless a finite temperature is set.
      temperature: null,
    });
    expect(JSON.stringify(nextSettings)).not.toContain('gpt-5.6-terra-2026-07-09');
  });

  it('does not auto-select a catalog model id for an Azure provider', async () => {
    vi.mocked(loadAISettings).mockResolvedValue(azureSettings);
    vi.mocked(listProviderModels).mockResolvedValue([{ id: 'gpt-5.6-terra-2026-07-09' }]);

    renderWithProviders(<AIPanel />);
    fireEvent.click(await screen.findByRole('tab', { name: /^Routing$/i }));
    await openWorkloadDialog('Chat');
    await openGlobalModelPicker();
    await selectPickerProvider(/Azure Foundry/i);

    // Seeding the field with a base model id is what produced the bug, so the
    // deployment field must come up empty and wait for the user.
    const deploymentInput = await screen.findByRole('textbox', { name: /Deployment name/i });
    await waitFor(() => expect(deploymentInput).toHaveValue(''));
  });

  it('keeps the model dropdown for a non-Azure provider, with a manual-entry escape hatch', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({
      ...baseSettings,
      cloudProviders: [
        ...baseSettings.cloudProviders,
        {
          id: 'p_custom_openai',
          slug: 'openai',
          label: 'OpenAI',
          endpoint: 'https://api.openai.com/v1',
          auth_style: 'bearer' as const,
          has_api_key: true,
        },
      ],
    });
    vi.mocked(listProviderModels).mockResolvedValue([{ id: 'gpt-4o' }]);

    renderWithProviders(<AIPanel />);
    fireEvent.click(await screen.findByRole('tab', { name: /^Routing$/i }));
    await openWorkloadDialog('Chat');
    await openGlobalModelPicker();
    await selectPickerProvider(/OpenAI/i);

    // Existing behaviour is unchanged: a populated catalog still renders a
    // dropdown and there is no Azure-specific labelling.
    await waitFor(() => expect(screen.queryByText('Deployment name')).not.toBeInTheDocument());
    const toggle = await screen.findByRole('button', { name: /Enter model ID manually/i });

    // ...but the catalog is no longer a dead end for off-catalog model ids.
    fireEvent.click(toggle);
    const manualInput = await screen.findByRole('textbox', { name: /^Model$/i });
    fireEvent.change(manualInput, { target: { value: 'my-private-model' } });
    expect(manualInput).toHaveValue('my-private-model');
  });

  it('warns when a stored Azure value is verbatim a catalog base model id', async () => {
    // The fingerprint of a PRE-FIX Azure selection: the dropdown was the only
    // way to set the value, so catalog membership is exactly the signature of a
    // connection configured the broken way. It stays a hint, never a rewrite —
    // a user may legitimately name a deployment after its base model.
    vi.mocked(loadAISettings).mockResolvedValue({
      ...azureSettings,
      routing: {
        ...azureSettings.routing,
        chat: {
          kind: 'cloud' as const,
          providerSlug: 'azure-foundry',
          model: 'gpt-5.6-terra-2026-07-09',
        },
      },
    });
    vi.mocked(listProviderModels).mockResolvedValue([
      { id: 'gpt-5.6-terra-2026-07-09' },
      { id: 'gpt-4o' },
    ]);

    renderWithProviders(<AIPanel />);
    fireEvent.click(await screen.findByRole('tab', { name: /^Routing$/i }));
    await openWorkloadDialog('Chat');
    await openGlobalModelPicker();

    expect(
      await screen.findByText(/confirm this is the name you gave your deployment/i)
    ).toBeInTheDocument();
    // The always-on explainer sits alongside it for any Azure connection.
    expect(screen.getByText(/This is not the model ID/i)).toBeInTheDocument();
  });

  it('does not warn when the Azure deployment name is absent from the catalog', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({
      ...azureSettings,
      routing: {
        ...azureSettings.routing,
        chat: { kind: 'cloud' as const, providerSlug: 'azure-foundry', model: 'my-deployment' },
      },
    });
    vi.mocked(listProviderModels).mockResolvedValue([{ id: 'gpt-5.6-terra-2026-07-09' }]);

    renderWithProviders(<AIPanel />);
    fireEvent.click(await screen.findByRole('tab', { name: /^Routing$/i }));
    await openWorkloadDialog('Chat');
    await openGlobalModelPicker();

    expect(await screen.findByText(/This is not the model ID/i)).toBeInTheDocument();
    await waitFor(() =>
      expect(
        screen.queryByText(/confirm this is the name you gave your deployment/i)
      ).not.toBeInTheDocument()
    );
  });

  it('can toggle an Azure connection back to the catalog and out again', async () => {
    // The escape hatch has to work in BOTH directions: Azure opens on free
    // text, but a user whose deployment IS named after a catalog entry should
    // still be able to pick it, then return to typing.
    vi.mocked(loadAISettings).mockResolvedValue(azureSettings);
    vi.mocked(listProviderModels).mockResolvedValue([{ id: 'gpt-4o' }]);

    renderWithProviders(<AIPanel />);
    fireEvent.click(await screen.findByRole('tab', { name: /^Routing$/i }));
    await openWorkloadDialog('Chat');
    await openGlobalModelPicker();
    await selectPickerProvider(/Azure Foundry/i);

    await screen.findByRole('textbox', { name: /Deployment name/i });
    fireEvent.click(await screen.findByRole('button', { name: /Choose from list/i }));

    // Back on the catalog dropdown, with the manual escape hatch offered again.
    // The action is labelled for what the field actually holds on Azure — a
    // deployment name, not a model ID.
    await waitFor(() =>
      expect(screen.queryByRole('textbox', { name: /Deployment name/i })).not.toBeInTheDocument()
    );
    expect(
      screen.queryByRole('button', { name: /Enter model ID manually/i })
    ).not.toBeInTheDocument();
    fireEvent.click(await screen.findByRole('button', { name: /Enter deployment name manually/i }));
    expect(await screen.findByRole('textbox', { name: /Deployment name/i })).toBeInTheDocument();
  });

  it('still lets a provider be added when the live /models probe fails', async () => {
    // Regression: the `{base}/models` probe used to be a hard gate on creating
    // a provider. A gateway that serves no OpenAI-shaped listing could never be
    // connected at all, which put the model / deployment-name field permanently
    // out of reach. The probe now informs, it does not block.
    vi.mocked(loadAISettings).mockResolvedValue(baseSettings);
    vi.mocked(listProviderModels).mockRejectedValue(
      new Error('provider returned 404: no /models endpoint')
    );

    renderWithProviders(<AIPanel />);
    await openCustomProviderEditor();

    fireEvent.change(await screen.findByPlaceholderText('My Provider'), {
      target: { value: 'Azure Foundry' },
    });
    fireEvent.change(screen.getByPlaceholderText('https://api.openai.com/v1'), {
      target: { value: 'https://my-resource.openai.azure.com/openai/v1' },
    });

    fireEvent.click(screen.getByRole('button', { name: /^Add Provider$/i }));

    // The failure is explained rather than swallowed, and nothing is persisted
    // behind the user's back on the first attempt.
    expect(await screen.findByText(/could not read this provider/i)).toBeInTheDocument();
    expect(saveAISettings).not.toHaveBeenCalled();

    // The escape hatch is what makes the connection reachable at all.
    fireEvent.click(screen.getByRole('button', { name: /Add without verifying/i }));

    await waitFor(() => expect(saveAISettings).toHaveBeenCalled());
    const [, nextSettings] = vi.mocked(saveAISettings).mock.calls.at(-1) ?? [];
    expect(nextSettings?.cloudProviders.map(p => p.slug)).toContain('azure-foundry');
  });

  it('withholds the verification bypass for a legacy Azure base URL', async () => {
    // Skipping verification is a bet that the provider works anyway. On an
    // Azure host that is not the `/openai/v1` base that bet is already lost:
    // `{base}/chat/completions` is not a route Azure serves there and the
    // stored bearer auth is the wrong header. Offering the bypass would just
    // manufacture a dead provider, so the nudge has to be followed instead.
    vi.mocked(loadAISettings).mockResolvedValue(baseSettings);
    vi.mocked(listProviderModels).mockRejectedValue(
      new Error('provider returned 404: no /models endpoint')
    );

    renderWithProviders(<AIPanel />);
    await openCustomProviderEditor();

    fireEvent.change(await screen.findByPlaceholderText('My Provider'), {
      target: { value: 'Azure Legacy' },
    });
    fireEvent.change(screen.getByPlaceholderText('https://api.openai.com/v1'), {
      target: { value: 'https://my-resource.openai.azure.com/openai' },
    });

    fireEvent.click(screen.getByRole('button', { name: /^Add Provider$/i }));

    expect(await screen.findByText(/use the v1 base URL/i)).toBeInTheDocument();
    await waitFor(() =>
      expect(
        screen.queryByRole('button', { name: /Add without verifying/i })
      ).not.toBeInTheDocument()
    );
    expect(saveAISettings).not.toHaveBeenCalled();
  });

  it('nudges an Azure endpoint that is not the v1 base towards /openai/v1', async () => {
    // Only `/openai/v1` serves a `/models` listing and accepts the resource key
    // as a bearer token. A user who pastes the portal's bare resource URL would
    // otherwise fail the probe and then fail every inference call.
    vi.mocked(loadAISettings).mockResolvedValue(baseSettings);

    renderWithProviders(<AIPanel />);
    await openCustomProviderEditor();

    const urlField = screen.getByPlaceholderText('https://api.openai.com/v1');
    fireEvent.change(urlField, {
      target: { value: 'https://my-resource.openai.azure.com/openai' },
    });
    expect(await screen.findByText(/use the v1 base URL/i)).toBeInTheDocument();

    // Correcting the base URL clears the warning.
    fireEvent.change(urlField, {
      target: { value: 'https://my-resource.openai.azure.com/openai/v1' },
    });
    await waitFor(() => expect(screen.queryByText(/use the v1 base URL/i)).not.toBeInTheDocument());
    // The deployment-name pointer stays for any Azure endpoint.
    expect(screen.getByText(/Set your deployment name in the model field/i)).toBeInTheDocument();
  });

  it('leaves a non-Azure endpoint free of Azure guidance', async () => {
    vi.mocked(loadAISettings).mockResolvedValue(baseSettings);

    renderWithProviders(<AIPanel />);
    await openCustomProviderEditor();

    fireEvent.change(screen.getByPlaceholderText('https://api.openai.com/v1'), {
      target: { value: 'https://litellm.mycorp.dev/v1' },
    });
    expect(screen.queryByText(/use the v1 base URL/i)).not.toBeInTheDocument();
    expect(
      screen.queryByText(/Set your deployment name in the model field/i)
    ).not.toBeInTheDocument();
  });

  it('blocks a non-probe submit failure instead of offering to skip verification', async () => {
    // The escape hatch is scoped to a rejected `/models` probe. A slug that
    // collides with an existing provider is a different class of failure and
    // must still block, or the dialog would offer to create a broken entry.
    vi.mocked(loadAISettings).mockResolvedValue(azureSettings);
    vi.mocked(listProviderModels).mockResolvedValue([{ id: 'gpt-4o' }]);

    renderWithProviders(<AIPanel />);
    await openCustomProviderEditor();

    fireEvent.change(await screen.findByPlaceholderText('My Provider'), {
      target: { value: 'Azure Foundry' },
    });
    fireEvent.change(screen.getByPlaceholderText('https://api.openai.com/v1'), {
      target: { value: 'https://my-resource.openai.azure.com/openai/v1' },
    });

    // `azure-foundry` is already taken by the fixture, so the slug check trips.
    expect(screen.getByRole('button', { name: /^Add Provider$/i })).toBeDisabled();
    expect(
      screen.queryByRole('button', { name: /Add without verifying/i })
    ).not.toBeInTheDocument();
  });

  it('does not offer to skip verification when the key write is what failed', async () => {
    // The slug case above never reaches `submitProvider`'s catch. This one
    // does: the credential write rejects, so the failure travels the same path
    // as a probe rejection but must not be mistaken for one — only a typed
    // `ProviderProbeError` unlocks the bypass.
    vi.mocked(loadAISettings).mockResolvedValue(baseSettings);
    vi.mocked(listProviderModels).mockResolvedValue([{ id: 'gpt-4o' }]);
    vi.mocked(setCloudProviderKey).mockRejectedValueOnce(new Error('keyring is locked'));

    renderWithProviders(<AIPanel />);
    await openCustomProviderEditor();

    fireEvent.change(await screen.findByPlaceholderText('My Provider'), {
      target: { value: 'Azure Foundry' },
    });
    fireEvent.change(screen.getByPlaceholderText('https://api.openai.com/v1'), {
      target: { value: 'https://my-resource.openai.azure.com/openai/v1' },
    });
    fireEvent.change(screen.getByLabelText(/API Key/i), { target: { value: 'sk-test-key' } });

    fireEvent.click(screen.getByRole('button', { name: /^Add Provider$/i }));

    expect(await screen.findByText(/keyring is locked/i)).toBeInTheDocument();
    expect(
      screen.queryByRole('button', { name: /Add without verifying/i })
    ).not.toBeInTheDocument();
    expect(screen.queryByText(/could not read this provider/i)).not.toBeInTheDocument();
    expect(saveAISettings).not.toHaveBeenCalled();
  });

  it('clears the verification bypass once a later attempt fails for another reason', async () => {
    // `probeFailed` used to persist for the dialog's lifetime, so a probe
    // rejection left "Add without verifying" on screen even after the next
    // attempt failed for an unrelated reason.
    vi.mocked(loadAISettings).mockResolvedValue(baseSettings);
    vi.mocked(listProviderModels).mockRejectedValueOnce(new Error('provider returned 404'));

    renderWithProviders(<AIPanel />);
    await openCustomProviderEditor();

    fireEvent.change(await screen.findByPlaceholderText('My Provider'), {
      target: { value: 'Azure Foundry' },
    });
    fireEvent.change(screen.getByPlaceholderText('https://api.openai.com/v1'), {
      target: { value: 'https://my-resource.openai.azure.com/openai/v1' },
    });
    fireEvent.click(screen.getByRole('button', { name: /^Add Provider$/i }));
    expect(
      await screen.findByRole('button', { name: /Add without verifying/i })
    ).toBeInTheDocument();

    // Second attempt: the probe would now succeed, but the key write rejects.
    vi.mocked(setCloudProviderKey).mockRejectedValueOnce(new Error('keyring is locked'));
    fireEvent.change(screen.getByLabelText(/API Key/i), { target: { value: 'sk-test-key' } });
    fireEvent.click(screen.getByRole('button', { name: /^Add Provider$/i }));

    expect(await screen.findByText(/keyring is locked/i)).toBeInTheDocument();
    await waitFor(() =>
      expect(
        screen.queryByRole('button', { name: /Add without verifying/i })
      ).not.toBeInTheDocument()
    );
  });

  it('offers a deployment-name field in the per-workload custom routing dialog', async () => {
    // The workload override dialog is a second, independent model picker. It
    // needs the same Azure treatment, or per-workload routing stays stuck on
    // catalog base model ids even after the main selector is fixed.
    vi.mocked(loadAISettings).mockResolvedValue(azureSettings);
    vi.mocked(listProviderModels).mockResolvedValue([{ id: 'gpt-5.6-terra-2026-07-09' }]);

    renderWithProviders(<AIPanel />);
    fireEvent.click(await screen.findByRole('tab', { name: /^Routing$/i }));
    const chooseButtons = await screen.findAllByRole('button', { name: /Choose a model/i });
    fireEvent.click(chooseButtons[0]);

    // Selecting the Azure provider flips the dialog to free text and relabels.
    await openGlobalModelPicker();
    await selectPickerProvider(/Azure Foundry/i);

    const deploymentInput = await screen.findByRole('textbox', { name: /Deployment name/i });
    fireEvent.change(deploymentInput, { target: { value: 'workload-deployment' } });
    expect(deploymentInput).toHaveValue('workload-deployment');
    expect(screen.getByText(/This is not the model ID/i)).toBeInTheDocument();

    // A catalog base model id typed here is the pre-fix fingerprint, so the
    // dialog raises the same confirmation hint the main selector does.
    fireEvent.change(deploymentInput, { target: { value: 'gpt-5.6-terra-2026-07-09' } });
    expect(
      await screen.findByText(/confirm this is the name you gave your deployment/i)
    ).toBeInTheDocument();

    // Complete the flow: a working text field proves nothing if the
    // dialog-to-routing handoff drops the value. Put the deployment name back
    // and assert it reaches the persisted per-workload routing verbatim.
    fireEvent.change(deploymentInput, { target: { value: 'workload-deployment' } });
    fireEvent.click(screen.getByRole('button', { name: /Use this model/i }));
    fireEvent.click(screen.getByRole('button', { name: /^Save$/ }));

    await waitFor(() => expect(saveAISettings).toHaveBeenCalled());
    const [, nextSettings] = vi.mocked(saveAISettings).mock.calls.at(-1) ?? [];
    const workloadRefs = Object.values(nextSettings?.routing ?? {});
    expect(workloadRefs).toContainEqual(
      expect.objectContaining({
        kind: 'cloud',
        providerSlug: 'azure-foundry',
        model: 'workload-deployment',
      })
    );
  });

  it('keeps the catalog dropdown and its manual escape hatch for a non-Azure provider in the dialog', async () => {
    // The dialog's non-Azure path must be untouched by #5213: a populated
    // catalog still renders a dropdown, and the escape hatch still reaches an
    // off-catalog model id — labelled "Model", not "Deployment name".
    vi.mocked(loadAISettings).mockResolvedValue({
      ...azureSettings,
      cloudProviders: [
        ...azureSettings.cloudProviders,
        {
          id: 'p_custom_openai',
          slug: 'openai',
          label: 'OpenAI',
          endpoint: 'https://api.openai.com/v1',
          auth_style: 'bearer' as const,
          has_api_key: true,
        },
      ],
    });
    vi.mocked(listProviderModels).mockResolvedValue([{ id: 'gpt-4o' }]);

    renderWithProviders(<AIPanel />);
    fireEvent.click(await screen.findByRole('tab', { name: /^Routing$/i }));
    const chooseButtons = await screen.findAllByRole('button', { name: /Choose a model/i });
    fireEvent.click(chooseButtons[0]);

    await openGlobalModelPicker();
    await selectPickerProvider(/OpenAI/i);

    // Catalog dropdown, no Azure labelling.
    await waitFor(() =>
      expect(screen.queryByRole('textbox', { name: /Deployment name/i })).not.toBeInTheDocument()
    );
    expect(screen.queryByText(/This is not the model ID/i)).not.toBeInTheDocument();

    // The escape hatch still works for an off-catalog id.
    fireEvent.click(await screen.findByRole('button', { name: /Enter model ID manually/i }));
    const manualInput = await screen.findByRole('textbox', { name: /^Model$/i });
    fireEvent.change(manualInput, { target: { value: 'my-private-model' } });
    expect(manualInput).toHaveValue('my-private-model');
    // ...and back to the catalog.
    fireEvent.click(await screen.findByRole('button', { name: /Choose from list/i }));
    await waitFor(() =>
      expect(screen.queryByRole('textbox', { name: /^Model$/i })).not.toBeInTheDocument()
    );
  });

  // ─── auth_style preservation ────────────────────────────────────────────────

  it('preserves auth_style: "anthropic" through save when Anthropic provider is configured', async () => {
    const settingsWithAnthropic = {
      cloudProviders: [
        {
          id: 'p_anthropic_1',
          slug: 'anthropic',
          label: 'Anthropic',
          endpoint: 'https://api.anthropic.com/v1',
          auth_style: 'anthropic' as const,
          has_api_key: true,
        },
      ],
      routing: {
        chat: { kind: 'openhuman' as const },
        reasoning: {
          kind: 'cloud' as const,
          providerSlug: 'anthropic',
          model: 'claude-3-5-sonnet-20241022',
        },
        agentic: { kind: 'openhuman' as const },
        coding: { kind: 'openhuman' as const },
        vision: { kind: 'openhuman' as const },
        memory: { kind: 'openhuman' as const },
        embeddings: { kind: 'openhuman' as const },
      },
      modelRegistry: [],
    };

    vi.mocked(loadAISettings).mockResolvedValue(settingsWithAnthropic);
    vi.mocked(saveAISettings).mockResolvedValue(undefined);

    renderWithProviders(<AIPanel />);

    // Wait for load.
    await waitFor(() => expect(screen.getAllByText(/Anthropic/i).length).toBeGreaterThan(0));

    fireEvent.click(await screen.findByRole('tab', { name: /^Routing$/i }));
    await openWorkloadDialog('Chat');
    fireEvent.click(screen.getByRole('button', { name: /^Save$/ }));

    await waitFor(() => expect(vi.mocked(saveAISettings)).toHaveBeenCalled());

    // Verify auth_style was passed through correctly in the next AISettings arg.
    const [, nextSettings] = vi.mocked(saveAISettings).mock.calls[0];
    const anthropicProvider = nextSettings.cloudProviders.find(
      (p: { slug: string }) => p.slug === 'anthropic'
    );
    expect(anthropicProvider).toBeDefined();
    expect(anthropicProvider!.auth_style).toBe('anthropic');
  });

  // ─── chip toggle: toggle ON opens API-key dialog ────────────────────────────

  it('clicking the OpenAI chip toggle (when disabled) opens the API-key dialog', async () => {
    // Load with no openai provider → chip is off.
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });

    renderWithProviders(<AIPanel />);
    await openProviderConnectDialog('openai');

    // ProviderKeyDialog should appear.
    await waitFor(() =>
      expect(screen.getByRole('dialog', { name: /Connect OpenAI/i })).toBeInTheDocument()
    );
    // The input for the API key should be visible.
    const dialog = screen.getByRole('dialog', { name: /Connect OpenAI/i });
    expect(within(dialog).getByLabelText(/API key/i)).toBeInTheDocument();
    expect(
      within(dialog).queryByRole('button', { name: /Sign in with ChatGPT \/ Codex/i })
    ).not.toBeInTheDocument();
    expect(within(dialog).getByTestId('settings-openai-oauth-section')).toBeInTheDocument();
    expect(within(dialog).getByTestId('settings-openai-oauth-connect')).toBeInTheDocument();
  });

  it('registers OpenAI after completing ChatGPT sign-in in the provider dialog', async () => {
    vi.mocked(isTauri).mockReturnValue(true);
    vi.mocked(saveAISettings).mockResolvedValue(undefined);
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    vi.mocked(callCoreRpc)
      .mockResolvedValueOnce({ result: { connected: false } })
      .mockResolvedValueOnce({ result: { authUrl: 'https://auth.openai.com/oauth/authorize' } })
      .mockResolvedValueOnce({ result: {} });

    renderWithProviders(<AIPanel />);
    await openProviderConnectDialog('openai');
    fireEvent.click(await screen.findByTestId('settings-openai-oauth-connect'));
    const input = await screen.findByTestId('settings-openai-oauth-callback-input');
    const callbackUrl = 'http://localhost:1455/auth/callback?code=test&state=test';
    fireEvent.change(input, { target: { value: callbackUrl } });
    fireEvent.click(screen.getByTestId('settings-openai-oauth-complete'));

    await waitFor(() => expect(saveAISettings).toHaveBeenCalledTimes(1));
    expect(callCoreRpc).toHaveBeenCalledWith({
      method: 'openhuman.inference_openai_oauth_complete',
      params: { callback_url: callbackUrl },
    });
    const [previousSettings, nextSettings] = vi.mocked(saveAISettings).mock.calls[0];
    expect(nextSettings.cloudProviders).toEqual([expect.objectContaining({ slug: 'openai' })]);
    expect(nextSettings.routing).toEqual(previousSettings.routing);
    expect(setCloudProviderKey).not.toHaveBeenCalled();
    await waitFor(() => expect(clearCloudProviderKey).toHaveBeenCalledWith('openai'));
    await waitFor(() =>
      expect(screen.queryByTestId('settings-openai-oauth-section')).not.toBeInTheDocument()
    );
  });

  it('removes OpenAI and its workload routes after disconnecting ChatGPT', async () => {
    vi.mocked(isTauri).mockReturnValue(true);
    vi.mocked(saveAISettings).mockResolvedValue(undefined);
    vi.mocked(loadAISettings).mockResolvedValue({
      ...baseSettings,
      cloudProviders: [
        ...baseSettings.cloudProviders,
        {
          id: 'openai',
          slug: 'openai',
          label: 'OpenAI',
          endpoint: 'https://api.openai.com/v1',
          auth_style: 'bearer',
          has_api_key: false,
        },
      ],
      routing: {
        ...baseSettings.routing,
        chat: { kind: 'cloud', providerSlug: 'openai', model: 'gpt-5' },
      },
    });
    vi.mocked(callCoreRpc)
      .mockResolvedValueOnce({ result: { connected: true } })
      .mockResolvedValueOnce({ result: {} });

    renderWithProviders(<AIPanel />);
    await openProviderRowAction('openai', /Replace API key/i);
    fireEvent.click(await screen.findByTestId('settings-openai-oauth-disconnect'));

    await waitFor(() => expect(saveAISettings).toHaveBeenCalledTimes(1));
    expect(callCoreRpc).toHaveBeenCalledWith({
      method: 'openhuman.inference_openai_oauth_disconnect',
      params: {},
    });
    const [, nextSettings] = vi.mocked(saveAISettings).mock.calls[0];
    expect(nextSettings.cloudProviders.map(provider => provider.slug)).toEqual(['openhuman']);
    expect(nextSettings.routing.chat).toEqual({ kind: 'default' });
    await waitFor(() =>
      expect(screen.getByTestId('settings-openai-oauth-connect')).toBeInTheDocument()
    );
  });

  it('#5339: keeps a valid key and saves the provider when the add-time probe fails for a non-auth reason', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    // `/models` is momentarily unreachable — NOT an auth failure. The key is
    // plausibly valid and must not be discarded or the save blocked.
    vi.mocked(listProviderModels).mockRejectedValue(new Error('HTTP request failed'));

    renderWithProviders(<AIPanel />);
    await openProviderConnectDialog('deepseek');
    const dialog = await screen.findByRole('dialog', { name: /Connect DeepSeek/i });
    fireEvent.change(within(dialog).getByLabelText(/API key/i), {
      target: { value: 'sk-deepseek-123' },
    });
    fireEvent.click(within(dialog).getByRole('button', { name: /^Save$/i }));

    // Settings persisted, key kept (never cleared).
    await waitFor(() => expect(saveAISettings).toHaveBeenCalled());
    expect(setCloudProviderKey).toHaveBeenCalledWith('deepseek', 'sk-deepseek-123');
    expect(clearCloudProviderKey).not.toHaveBeenCalled();
    // A truthful advisory replaces the old "not saved" dead end. Non-auth copy
    // is the "a test call … failed" branch (not the auth "rejected it" branch).
    const advisory = await screen.findByText(/The key was saved, but a test call to 'deepseek'/);
    expect(advisory).toBeInTheDocument();
    // The advisory is dismissible.
    fireEvent.click(screen.getByRole('button', { name: /Dismiss/i }));
    await waitFor(() =>
      expect(
        screen.queryByText(/The key was saved, but a test call to 'deepseek'/)
      ).not.toBeInTheDocument()
    );
  });

  it('#5339: rejects and clears the key when the add-time probe fails with an auth error', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    // A 401 means the key itself is wrong — roll back and reject so the user fixes it.
    vi.mocked(listProviderModels).mockRejectedValue(new Error('HTTP 401 invalid api key'));

    renderWithProviders(<AIPanel />);
    await openProviderConnectDialog('deepseek');
    const dialog = await screen.findByRole('dialog', { name: /Connect DeepSeek/i });
    fireEvent.change(within(dialog).getByLabelText(/API key/i), { target: { value: 'sk-bad' } });
    fireEvent.click(within(dialog).getByRole('button', { name: /^Save$/i }));

    await waitFor(() => expect(clearCloudProviderKey).toHaveBeenCalledWith('deepseek'));
    expect(saveAISettings).not.toHaveBeenCalled();
  });

  it('#5341: treats a bare 403/Forbidden probe failure as auth — clears the key, no save', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    // A revoked / forbidden key surfaces as a bare 403 with no "401"/"unauthorized"
    // text. It must still be rejected, not kept behind a "saved" advisory.
    vi.mocked(listProviderModels).mockRejectedValue(new Error('provider returned 403: forbidden'));

    renderWithProviders(<AIPanel />);
    await openProviderConnectDialog('deepseek');
    const dialog = await screen.findByRole('dialog', { name: /Connect DeepSeek/i });
    fireEvent.change(within(dialog).getByLabelText(/API key/i), {
      target: { value: 'sk-revoked' },
    });
    fireEvent.click(within(dialog).getByRole('button', { name: /^Save$/i }));

    await waitFor(() => expect(clearCloudProviderKey).toHaveBeenCalledWith('deepseek'));
    expect(saveAISettings).not.toHaveBeenCalled();
    // No "key was saved" advisory for a rejected key.
    expect(screen.queryByText(/The key was saved, but/)).not.toBeInTheDocument();
  });

  it('#5341: logs (does not swallow) a rollback flush + key-clear failure on a rejected add', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    // Auth failure → rollback path. Both rollback legs then fail; the code must
    // log each instead of swallowing, and must not persist the provider.
    vi.mocked(listProviderModels).mockRejectedValue(new Error('HTTP 401 invalid api key'));
    // First flush (writing the new provider) succeeds; the SECOND flush (rollback
    // to the prior list) is the one that fails.
    vi.mocked(flushCloudProviders)
      .mockResolvedValueOnce(undefined)
      .mockRejectedValueOnce(new Error('flush boom'));
    vi.mocked(clearCloudProviderKey).mockRejectedValueOnce(new Error('clear boom'));
    const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

    try {
      renderWithProviders(<AIPanel />);
      await openProviderConnectDialog('deepseek');
      const dialog = await screen.findByRole('dialog', { name: /Connect DeepSeek/i });
      fireEvent.change(within(dialog).getByLabelText(/API key/i), { target: { value: 'sk-bad' } });
      fireEvent.click(within(dialog).getByRole('button', { name: /^Save$/i }));

      await waitFor(() =>
        expect(warnSpy).toHaveBeenCalledWith(
          expect.stringContaining('rollback clearCloudProviderKey failed'),
          expect.any(Error)
        )
      );
      expect(warnSpy).toHaveBeenCalledWith(
        expect.stringContaining('rollback flush failed'),
        expect.any(Error)
      );
      expect(saveAISettings).not.toHaveBeenCalled();
    } finally {
      warnSpy.mockRestore();
    }
  });

  it('#5341: logs (does not swallow) a rollback failure when a custom-provider add is rejected', async () => {
    // Same un-swallow guarantee on the custom-provider editor path.
    vi.mocked(loadAISettings).mockResolvedValue(baseSettings);
    vi.mocked(listProviderModels).mockRejectedValue(new Error('provider returned 500'));
    vi.mocked(flushCloudProviders)
      .mockResolvedValueOnce(undefined)
      .mockRejectedValueOnce(new Error('flush boom'));
    vi.mocked(clearCloudProviderKey).mockRejectedValueOnce(new Error('clear boom'));
    const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

    try {
      renderWithProviders(<AIPanel />);
      await openCustomProviderEditor();
      fireEvent.change(await screen.findByPlaceholderText('My Provider'), {
        target: { value: 'My Host' },
      });
      fireEvent.change(screen.getByPlaceholderText('https://api.openai.com/v1'), {
        target: { value: 'https://my-host.example.com/v1' },
      });
      fireEvent.change(screen.getByLabelText(/API Key/i), { target: { value: 'sk-test-key' } });
      fireEvent.click(screen.getByRole('button', { name: /^Add Provider$/i }));

      await waitFor(() =>
        expect(warnSpy).toHaveBeenCalledWith(
          expect.stringContaining('rollback clearCloudProviderKey failed'),
          expect.any(Error)
        )
      );
      expect(warnSpy).toHaveBeenCalledWith(
        expect.stringContaining('rollback flush failed'),
        expect.any(Error)
      );
      expect(saveAISettings).not.toHaveBeenCalled();
    } finally {
      warnSpy.mockRestore();
    }
  });

  it('shows a localized Kimi platform link and opens the supported .ai platform', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });

    renderWithProviders(
      <I18nProvider>
        <AIPanel />
      </I18nProvider>
    );

    await openProviderConnectDialog('moonshot');
    const dialog = await screen.findByRole('dialog', { name: /Kimi \(Moonshot\)/i });
    const link = within(dialog).getByRole('link', { name: /^Get API key$/i });

    expect(link).toHaveAttribute('href', 'https://platform.kimi.ai?aff=openhuman');

    fireEvent.click(link);

    expect(openUrl).toHaveBeenCalledWith('https://platform.kimi.ai?aff=openhuman');
  });

  it('logs Kimi platform link open failures without changing the dialog', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    vi.mocked(openUrl).mockRejectedValueOnce('blocked');
    const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

    try {
      renderWithProviders(
        <I18nProvider>
          <AIPanel />
        </I18nProvider>
      );

      await openProviderConnectDialog('moonshot');
      const dialog = await screen.findByRole('dialog', { name: /Kimi \(Moonshot\)/i });
      fireEvent.click(within(dialog).getByRole('link', { name: /^Get API key$/i }));

      await waitFor(() => {
        expect(warnSpy).toHaveBeenCalledWith('[ai-settings] provider platform link open failed', {
          slug: 'moonshot',
          error: 'blocked',
        });
      });
      expect(dialog).toBeInTheDocument();
    } finally {
      warnSpy.mockRestore();
    }
  });

  it('localizes the Kimi platform link text for Chinese', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });

    renderWithProviders(
      <I18nProvider>
        <AIPanel />
      </I18nProvider>,
      { preloadedState: { locale: { current: 'zh-CN' } } }
    );

    await openProviderConnectDialog('moonshot');
    const dialog = await screen.findByRole('dialog', { name: /Kimi \(Moonshot\)/i });

    expect(within(dialog).getByRole('link', { name: '获取 API Key' })).toBeInTheDocument();
  });

  it('reserves logical inline space for long translated Kimi link labels', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });

    renderWithProviders(
      <I18nProvider>
        <AIPanel />
      </I18nProvider>,
      { preloadedState: { locale: { current: 'fr' } } }
    );

    await openProviderConnectDialog('moonshot');
    const dialog = await screen.findByRole('dialog', { name: /Kimi \(Moonshot\)/i });
    const heading = within(dialog).getByRole('heading', {
      name: /Connecter un fournisseur Kimi \(Moonshot\)/i,
    });

    expect(heading.parentElement).toHaveStyle({ paddingInlineEnd: '9rem' });
  });

  it('positions the Kimi link at the logical inline end for RTL locales', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });

    renderWithProviders(
      <I18nProvider>
        <AIPanel />
      </I18nProvider>,
      { preloadedState: { locale: { current: 'ar' } } }
    );

    await openProviderConnectDialog('moonshot');
    const dialog = await screen.findByRole('dialog', { name: /Kimi \(Moonshot\)/i });
    const link = within(dialog).getByRole('link', { name: 'احصل على مفتاح API' });

    expect(document.documentElement).toHaveAttribute('dir', 'rtl');
    expect(link).toHaveStyle({ insetInlineEnd: '1.5rem' });
    expect(link).not.toHaveClass('right-6');
  });

  it('does not show the Kimi platform link for other providers', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });

    renderWithProviders(
      <I18nProvider>
        <AIPanel />
      </I18nProvider>
    );

    await openProviderConnectDialog('openai');
    const dialog = await screen.findByRole('dialog', { name: /Connect OpenAI/i });

    expect(within(dialog).queryByRole('link', { name: /^Get API key$/i })).not.toBeInTheDocument();
  });

  it('renders Phase 1 built-in providers in the add-provider catalog including SumoPod', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });

    renderWithProviders(<AIPanel />);

    fireEvent.click(await screen.findByTestId('add-provider-open'));
    const trigger = await screen.findByTestId('add-provider-select-cloud');
    trigger.focus();
    fireEvent.keyDown(trigger, { key: 'Enter' });
    for (const slug of ['groq', 'deepseek', 'minimax', 'sumopod']) {
      expect(await screen.findByTestId(`add-provider-option-${slug}`)).toBeInTheDocument();
    }
  });

  it('connects SumoPod with the native endpoint and provider:sumopod key', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });

    renderWithProviders(<AIPanel />);

    await openProviderConnectDialog('sumopod');
    const dialog = await screen.findByRole('dialog', { name: /Connect SumoPod/i });
    fireEvent.change(within(dialog).getByLabelText(/API key/i), {
      target: { value: 'sk-sumopod-test' },
    });
    fireEvent.click(within(dialog).getByRole('button', { name: /^Save$/i }));

    await waitFor(() =>
      expect(vi.mocked(setCloudProviderKey)).toHaveBeenCalledWith('sumopod', 'sk-sumopod-test')
    );
    await waitFor(() => expect(vi.mocked(listProviderModels)).toHaveBeenCalledWith('sumopod'));
    await waitFor(() => expect(vi.mocked(saveAISettings)).toHaveBeenCalled());

    const [, nextSettings] = vi.mocked(saveAISettings).mock.calls[0];
    expect(nextSettings.cloudProviders).toEqual(
      expect.arrayContaining([
        expect.objectContaining({
          slug: 'sumopod',
          label: 'SumoPod',
          endpoint: 'https://ai.sumopod.com/v1',
          auth_style: 'bearer',
          has_api_key: true,
        }),
      ])
    );
  });

  it('connects MiniMax via its OpenAI-compatible /v1 endpoint with bearer auth', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });

    renderWithProviders(<AIPanel />);

    await openProviderConnectDialog('minimax');
    const dialog = await screen.findByRole('dialog', { name: /Connect MiniMax/i });
    fireEvent.change(within(dialog).getByLabelText(/API key/i), {
      target: { value: 'sk-minimax-test' },
    });
    fireEvent.click(within(dialog).getByRole('button', { name: /^Save$/i }));

    await waitFor(() =>
      expect(vi.mocked(setCloudProviderKey)).toHaveBeenCalledWith('minimax', 'sk-minimax-test')
    );
    await waitFor(() => expect(vi.mocked(saveAISettings)).toHaveBeenCalled());

    const [, nextSettings] = vi.mocked(saveAISettings).mock.calls[0];
    // MiniMax speaks OpenAI on `/v1` (chat/completions + models). The old
    // `/anthropic` base + anthropic auth pointed at its Messages API, which
    // OpenHuman doesn't speak — both paths 404'd (Sentry TAURI-RUST-8X3).
    expect(nextSettings.cloudProviders).toEqual(
      expect.arrayContaining([
        expect.objectContaining({
          slug: 'minimax',
          label: 'MiniMax',
          endpoint: 'https://api.minimax.io/v1',
          auth_style: 'bearer',
        }),
      ])
    );
  });

  it('surfaces provider setup errors in an alert with technical details collapsed', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    vi.mocked(listProviderModels).mockRejectedValueOnce(
      new Error('Could not reach OpenAI: provider returned 401 Unauthorized')
    );

    renderWithProviders(<AIPanel />);

    await openProviderConnectDialog('openai');
    const dialog = await screen.findByRole('dialog', { name: /Connect OpenAI/i });
    fireEvent.change(within(dialog).getByLabelText(/API key/i), {
      target: { value: 'sk-bad-key' },
    });
    fireEvent.click(within(dialog).getByRole('button', { name: /^Save$/i }));

    const alert = await within(dialog).findByRole('alert');
    expect(alert).toHaveTextContent(/rejected the credentials/i);
  });

  it('clicking the OpenRouter chip shows both API key entry and the OAuth button', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });

    renderWithProviders(<AIPanel />);
    await openProviderConnectDialog('openrouter');

    const dialog = await screen.findByRole('dialog', { name: /Connect OpenRouter/i });
    expect(within(dialog).getByLabelText(/API key/i)).toBeInTheDocument();
    expect(
      within(dialog).getByRole('button', { name: /Sign in with OpenRouter/i })
    ).toBeInTheDocument();
  });

  it('stores the OpenRouter OAuth key and enables the provider chip', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    vi.mocked(connectOpenRouterViaOAuth).mockResolvedValue('sk-or-from-oauth');

    renderWithProviders(<AIPanel />);
    await openProviderConnectDialog('openrouter');
    const dialog = await screen.findByRole('dialog', { name: /Connect OpenRouter/i });
    fireEvent.click(within(dialog).getByRole('button', { name: /Sign in with OpenRouter/i }));

    await waitFor(() => expect(connectOpenRouterViaOAuth).toHaveBeenCalledTimes(1));
    await waitFor(() =>
      expect(setCloudProviderKey).toHaveBeenCalledWith('openrouter', 'sk-or-from-oauth')
    );
    await waitFor(() =>
      expect(screen.getByRole('switch', { name: /Disconnect OpenRouter/i })).toBeInTheDocument()
    );
  });

  // Regression: the dialog disabled Cancel (and blocked Esc / click-outside)
  // for the whole browser round-trip, so an abandoned sign-in left the user
  // stuck on "Connecting…" with no way out.
  it('keeps Cancel usable while OpenRouter sign-in is pending and aborts the flow', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    let signal: AbortSignal | undefined;
    vi.mocked(connectOpenRouterViaOAuth).mockImplementation(deps => {
      signal = deps?.signal;
      return new Promise((_, reject) =>
        signal?.addEventListener('abort', () =>
          reject(new Error('OpenRouter OAuth was cancelled.'))
        )
      );
    });

    renderWithProviders(<AIPanel />);
    await openProviderConnectDialog('openrouter');
    const dialog = await screen.findByRole('dialog', { name: /Connect OpenRouter/i });
    fireEvent.click(within(dialog).getByRole('button', { name: /Sign in with OpenRouter/i }));
    await waitFor(() => expect(connectOpenRouterViaOAuth).toHaveBeenCalledTimes(1));

    const cancel = within(dialog).getByRole('button', { name: /^Cancel$/i });
    expect(cancel).toBeEnabled();
    fireEvent.click(cancel);

    expect(signal?.aborted).toBe(true);
    await waitFor(() =>
      expect(screen.queryByRole('dialog', { name: /Connect OpenRouter/i })).not.toBeInTheDocument()
    );
    expect(setCloudProviderKey).not.toHaveBeenCalled();
  });

  // Regression (review on #6265): once the key exists, Cancel must not close the
  // dialog while it is being saved, or the provider is added after a "cancel".
  it('locks the dialog while an OpenRouter OAuth key is being saved', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    vi.mocked(connectOpenRouterViaOAuth).mockResolvedValue('sk-or-from-oauth');
    vi.mocked(setCloudProviderKey).mockImplementationOnce(() => new Promise(() => {}));

    renderWithProviders(<AIPanel />);
    await openProviderConnectDialog('openrouter');
    const dialog = await screen.findByRole('dialog', { name: /Connect OpenRouter/i });
    fireEvent.click(within(dialog).getByRole('button', { name: /Sign in with OpenRouter/i }));

    await waitFor(() =>
      expect(setCloudProviderKey).toHaveBeenCalledWith('openrouter', 'sk-or-from-oauth')
    );
    expect(within(dialog).getByRole('button', { name: /^Cancel$/i })).toBeDisabled();
  });

  // Regression: picking a provider in the add-provider modal has to hand off to
  // that provider's own connect dialog. Two Radix dialogs are involved (the
  // picker unmounts as the key dialog mounts), so this asserts the handoff
  // end to end rather than that `onOpenKeyDialog` was called.
  it('picking a cloud provider opens its connect dialog', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });

    renderWithProviders(<AIPanel />);
    fireEvent.click(await screen.findByTestId('add-provider-open'));

    // Keyboard, not pointer: a pointer open depends on pointer capture and a
    // popper measurement pass over a zero-sized jsdom layout. This is the path
    // `ui/Select.test.tsx` documents as the stable one.
    const trigger = await screen.findByTestId('add-provider-select-cloud');
    trigger.focus();
    fireEvent.keyDown(trigger, { key: 'Enter' });

    fireEvent.keyDown(await screen.findByTestId('add-provider-option-openai'), { key: 'Enter' });

    expect(await screen.findByRole('dialog', { name: /Connect OpenAI/i })).toBeInTheDocument();
  });

  it('clicking Add Custom Provider opens the CloudProviderEditor', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });

    renderWithProviders(<AIPanel />);
    await waitFor(() => expect(screen.getByTestId('add-provider-open')).toBeInTheDocument());
    await openCustomProviderEditor();

    await waitFor(() => expect(screen.getByText(/Add cloud provider/i)).toBeInTheDocument());
    expect(screen.getByLabelText(/^Name$/i)).toBeInTheDocument();
    expect(screen.getByLabelText(/OpenAI URL/i)).toBeInTheDocument();

    // The API-key row's visible label is associated with the field by `for`
    // rather than by wrapping it. The old markup wrapped the field's label
    // around the "clear stored key" button, so clicking that button also
    // counted as a click on the input.
    const apiKeyLabel = screen.getByText(/^API Key$/i);
    expect(apiKeyLabel.tagName).toBe('LABEL');
    expect(apiKeyLabel).toHaveAttribute('for', 'cloud-provider-api-key');
    expect(apiKeyLabel.querySelector('button')).toBeNull();
  });

  // ─── chip toggle: toggle OFF scrubs routing entries ──────────────────────────

  it('toggling OFF an enabled provider scrubs routing entries that reference it', async () => {
    const settingsWithOpenAI = {
      cloudProviders: [
        {
          id: 'p_openai_1',
          slug: 'openai',
          label: 'OpenAI',
          endpoint: 'https://api.openai.com/v1',
          auth_style: 'bearer' as const,
          has_api_key: true,
        },
      ],
      routing: {
        chat: { kind: 'openhuman' as const },
        reasoning: { kind: 'cloud' as const, providerSlug: 'openai', model: 'gpt-4o' },
        agentic: { kind: 'cloud' as const, providerSlug: 'openai', model: 'gpt-4o-mini' },
        coding: { kind: 'openhuman' as const },
        vision: { kind: 'openhuman' as const },
        memory: { kind: 'openhuman' as const },
        embeddings: { kind: 'openhuman' as const },
      },
      modelRegistry: [],
    };
    vi.mocked(loadAISettings).mockResolvedValue(settingsWithOpenAI);
    vi.mocked(saveAISettings).mockResolvedValue(undefined);

    renderWithProviders(<AIPanel />);

    // Wait for load — OpenAI chip should be ON.
    await waitFor(() =>
      expect(screen.getByRole('switch', { name: /Disconnect OpenAI/i })).toBeInTheDocument()
    );

    // Toggle OFF.
    fireEvent.click(screen.getByRole('switch', { name: /Disconnect OpenAI/i }));

    await waitFor(() => expect(vi.mocked(saveAISettings)).toHaveBeenCalled());

    const [, nextSettings] = vi.mocked(saveAISettings).mock.calls[0];

    // Provider should be gone.
    expect(
      nextSettings.cloudProviders.find((p: { slug: string }) => p.slug === 'openai')
    ).toBeUndefined();

    // Routing entries that were pinned to openai must be reset to the user default route.
    expect(nextSettings.routing.reasoning).toEqual({ kind: 'default' });
    expect(nextSettings.routing.agentic).toEqual({ kind: 'default' });
    // Entries that were already OpenHuman-managed remain unchanged.
    expect(nextSettings.routing.coding).toEqual({ kind: 'openhuman' });
  });

  // ─── chip toggle: local runtime toggle OFF scrubs orphaned local routing ─────

  it('toggling OFF a local runtime resets workloads routed to it back to default', async () => {
    const settingsWithOllama = {
      cloudProviders: [
        {
          id: 'p_ollama_1',
          slug: 'ollama',
          label: 'Ollama',
          endpoint: 'http://localhost:11434/v1',
          auth_style: 'bearer' as const,
          has_api_key: false,
        },
      ],
      routing: {
        chat: { kind: 'local' as const, model: 'llama3' },
        reasoning: { kind: 'local' as const, model: 'llama3' },
        agentic: { kind: 'openhuman' as const },
        coding: { kind: 'openhuman' as const },
        vision: { kind: 'openhuman' as const },
        memory: { kind: 'openhuman' as const },
        embeddings: { kind: 'openhuman' as const },
      },
      modelRegistry: [],
    };
    vi.mocked(loadAISettings).mockResolvedValue(settingsWithOllama);
    vi.mocked(saveAISettings).mockResolvedValue(undefined);

    renderWithProviders(<AIPanel />);

    await waitFor(() =>
      expect(screen.getByRole('switch', { name: /Disconnect Ollama/i })).toBeInTheDocument()
    );

    // Toggle Ollama OFF — no other local runtime remains, so its routed
    // workloads are orphaned and must reset to the user default route.
    fireEvent.click(screen.getByRole('switch', { name: /Disconnect Ollama/i }));

    await waitFor(() => expect(vi.mocked(saveAISettings)).toHaveBeenCalled());
    const [, nextSettings] = vi.mocked(saveAISettings).mock.calls[0];

    expect(
      nextSettings.cloudProviders.find((p: { slug: string }) => p.slug === 'ollama')
    ).toBeUndefined();
    // Local-routed workloads reset to default (the fix — previously left orphaned).
    expect(nextSettings.routing.chat).toEqual({ kind: 'default' });
    expect(nextSettings.routing.reasoning).toEqual({ kind: 'default' });
    // Already-managed entries unchanged.
    expect(nextSettings.routing.agentic).toEqual({ kind: 'openhuman' });
  });

  // ─── API-key dialog: failed setCloudProviderKey does not add provider ────────

  it('when setCloudProviderKey throws, the provider is NOT added to the draft', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    // Make setCloudProviderKey reject.
    vi.mocked(setCloudProviderKey).mockRejectedValue(new Error('key store failed'));

    renderWithProviders(<AIPanel />);

    // Count connected-provider toggles before dialog interaction.
    const chipsBefore = screen.queryAllByRole('switch').length;

    // Open the dialog.
    await openProviderConnectDialog('openai');
    await waitFor(() =>
      expect(screen.getByRole('dialog', { name: /Connect OpenAI/i })).toBeInTheDocument()
    );

    // Fill in a key and submit.
    fireEvent.change(screen.getByLabelText(/API key/i), { target: { value: 'sk-bad-key' } });
    fireEvent.click(screen.getByRole('button', { name: /^Save$/i }));

    // The panel silently catches the setCloudProviderKey error and does NOT
    // mutate the draft. Because the panel's onSubmit returns (doesn't throw),
    // the dialog's handleSave resolves without entering its catch block, leaving
    // the dialog in the 'saving' phase with the button showing "Saving…".
    //
    // Wait for setCloudProviderKey to have been called (confirms the flow ran).
    await waitFor(() => expect(vi.mocked(setCloudProviderKey)).toHaveBeenCalled());

    // The dialog must still be open (setKeyDialogFor was never set to null).
    expect(screen.getByRole('dialog', { name: /Connect OpenAI/i })).toBeInTheDocument();

    // The number of provider toggle switches must not have grown — the failed
    // provider was never added to the draft. `hidden: true` is required here:
    // the connect dialog is now a real Radix Dialog (migrated off a hand-rolled
    // `<div role="dialog">`), so Radix aria-hides the rest of the tree while it
    // is open and the default role query would otherwise see zero switches.
    expect(screen.queryAllByRole('switch', { hidden: true }).length).toBe(chipsBefore);

    // Specifically: no "Disconnect OpenAI" switch (chip is still in off state).
    expect(
      screen.queryByRole('switch', { name: /Disconnect OpenAI/i, hidden: true })
    ).not.toBeInTheDocument();
  });

  // Regression for #4852: the Codex auth button had a hardcoded Korean fallback
  // (`Codex 인증`) because the `settings.ai.codexAuthButton` key was missing from
  // every locale, so the English UI rendered Korean text. Assert the English
  // label renders and no Korean survives.
  it('renders the Codex auth button with the active-locale (English) label', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });

    renderWithProviders(<AIPanel />);
    fireEvent.click(await screen.findByTestId('add-provider-open'));
    const trigger = await screen.findByTestId('add-provider-select-cli');
    trigger.focus();
    fireEvent.keyDown(trigger, { key: 'Enter' });
    expect(await screen.findByTestId('add-provider-option-codex')).toBeInTheDocument();
    // The Korean fallback must be gone from the English onboarding screen.
    expect(screen.queryByText(/인증/)).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: /Codex 인증/i })).not.toBeInTheDocument();
  });

  it('connects OpenAI through Codex CLI auth without storing an API key', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });

    renderWithProviders(<AIPanel />);
    await openProviderConnectDialog('codex', 'cli');

    await waitFor(() => expect(vi.mocked(importOpenAiCodexCliAuth)).toHaveBeenCalledTimes(1));
    expect(vi.mocked(startOpenAiCodexOAuth)).not.toHaveBeenCalled();
    expect(vi.mocked(openUrl)).not.toHaveBeenCalled();
    expect(vi.mocked(completeOpenAiCodexOAuth)).not.toHaveBeenCalled();
    expect(vi.mocked(setCloudProviderKey)).not.toHaveBeenCalled();
    expect(vi.mocked(clearCloudProviderKey)).toHaveBeenCalledWith('openai');
    expect(vi.mocked(listProviderModels)).not.toHaveBeenCalledWith('openai');

    await waitFor(() => expect(vi.mocked(saveAISettings)).toHaveBeenCalled());
    expect(vi.mocked(saveAISettings).mock.invocationCallOrder[0]).toBeLessThan(
      vi.mocked(clearCloudProviderKey).mock.invocationCallOrder[0]
    );
    const [, nextSettings] = vi.mocked(saveAISettings).mock.calls[0];
    expect(nextSettings.cloudProviders).toEqual(
      expect.arrayContaining([
        expect.objectContaining({
          slug: 'openai',
          label: 'OpenAI',
          endpoint: 'https://api.openai.com/v1',
          auth_style: 'bearer',
          has_api_key: true,
        }),
      ])
    );
  });

  it.each([
    [OPENAI_CODEX_OAUTH_MISSING_AUTH_URL, /Codex OAuth did not return an authorization URL/i],
    [
      OPENAI_CODEX_OAUTH_MISSING_CALLBACK_URL,
      /Paste the redirect URL from your browser after signing in/i,
    ],
  ])('localizes Codex CLI auth error code %s', async (errorCode, expectedMessage) => {
    const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    vi.mocked(importOpenAiCodexCliAuth).mockRejectedValueOnce(new Error(errorCode));

    renderWithProviders(<AIPanel />);
    await openProviderConnectDialog('codex', 'cli');

    await waitFor(() => expect(screen.getByRole('alert')).toHaveTextContent(expectedMessage));
    expect(vi.mocked(setCloudProviderKey)).not.toHaveBeenCalled();
    expect(vi.mocked(clearCloudProviderKey)).not.toHaveBeenCalled();
    warnSpy.mockRestore();
  });

  it('wraps long provider setup errors and hides raw JSON behind technical details', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    vi.mocked(listProviderModels).mockRejectedValue(
      new Error(
        'provider returned 401: {"error":{"message":"Incorrect API key provided: sk-this-is-a-very-long-invalid-key-value-that-should-not-dominate-the-modal-or-force-horizontal-overflow. You can find your API key at https://platform.openai.com/account/api-keys.","type":"invalid_request_error","param":null,"code":"invalid_api_key"},"request_id":"req_1234567890abcdefghijklmnopqrstuvwxyz"}'
      )
    );

    renderWithProviders(<AIPanel />);
    await openProviderConnectDialog('openai');
    const dialog = await screen.findByRole('dialog', { name: /Connect OpenAI/i });
    fireEvent.change(within(dialog).getByLabelText(/API key/i), {
      target: { value: 'sk-bad-key' },
    });
    fireEvent.click(within(dialog).getByRole('button', { name: /^Save$/i }));

    const alert = await within(dialog).findByRole('alert');
    expect(alert).toHaveClass('max-w-full', 'min-w-0', 'overflow-hidden');
    expect(
      within(alert).getByText('OpenAI rejected the credentials. Check the API key and try again.')
    ).toBeInTheDocument();
    expect(within(alert).getByText('Technical details')).toBeInTheDocument();
    expect(within(alert).getByText(/provider returned 401/)).toBeInTheDocument();
    expect(screen.queryByRole('switch', { name: /Disconnect OpenAI/i })).not.toBeInTheDocument();
  });

  it('summarizes advanced provider editor JSON errors and preserves details', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    vi.mocked(listProviderModels).mockRejectedValue(
      new Error(
        'provider returned 418: {"error":{"message":"Provider teapot says no. Try another endpoint."},"request_id":"req_teapot"}'
      )
    );

    renderWithProviders(<AIPanel />);
    await waitFor(() => expect(screen.getByTestId('add-provider-open')).toBeInTheDocument());

    await openCustomProviderEditor();
    await waitFor(() => expect(screen.getByText(/Add cloud provider/i)).toBeInTheDocument());
    fireEvent.change(screen.getByLabelText(/^Name$/i), { target: { value: 'Team Gateway' } });
    fireEvent.change(screen.getByLabelText(/OpenAI URL/i), {
      target: { value: 'https://api.openai.com/v1' },
    });
    fireEvent.change(screen.getByPlaceholderText('sk-...'), { target: { value: 'sk-test-key' } });
    fireEvent.click(screen.getByRole('button', { name: /Add provider/i }));

    const alert = await screen.findByRole('alert');
    expect(
      within(alert).getByText(
        'Could not reach Team Gateway: Provider teapot says no. Try another endpoint.'
      )
    ).toBeInTheDocument();
    expect(within(alert).getByText('Technical details')).toBeInTheDocument();
    expect(within(alert).getByText(/provider returned 418/)).toBeInTheDocument();
    expect(
      screen.queryByRole('switch', { name: /Disconnect Team Gateway/i })
    ).not.toBeInTheDocument();
  });

  it('derives the custom provider slug from the entered name', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });

    renderWithProviders(<AIPanel />);
    await waitFor(() => expect(screen.getByTestId('add-provider-open')).toBeInTheDocument());

    await openCustomProviderEditor();
    await waitFor(() => expect(screen.getByText(/Add cloud provider/i)).toBeInTheDocument());

    fireEvent.change(screen.getByLabelText(/^Name$/i), { target: { value: 'My Team Gateway' } });
    expect(screen.getByText(/Slug:/i)).toHaveTextContent('Slug: my-team-gateway');

    fireEvent.change(screen.getByLabelText(/OpenAI URL/i), {
      target: { value: 'https://gateway.example.com/v1' },
    });
    fireEvent.change(screen.getByPlaceholderText('sk-...'), { target: { value: 'sk-team-key' } });
    fireEvent.click(screen.getByRole('button', { name: /Add provider/i }));

    await waitFor(() =>
      expect(vi.mocked(setCloudProviderKey)).toHaveBeenCalledWith('my-team-gateway', 'sk-team-key')
    );
    await waitFor(() =>
      expect(vi.mocked(listProviderModels)).toHaveBeenCalledWith('my-team-gateway')
    );
  });

  it('sends an uploaded CA certificate with the provider before probing models', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    renderWithProviders(<AIPanel />);
    await openCustomProviderEditor();

    fireEvent.change(screen.getByLabelText(/^Name$/i), { target: { value: 'Team Gateway' } });
    fireEvent.change(screen.getByLabelText(/OpenAI URL/i), {
      target: { value: 'https://gateway.example.com/v1' },
    });
    const pem = '-----BEGIN CERTIFICATE-----\nfixture\n-----END CERTIFICATE-----';
    fireEvent.change(screen.getByLabelText(/CA certificate/i), {
      target: { files: [new File([pem], 'team-ca.pem', { type: 'application/x-pem-file' })] },
    });
    await waitFor(() =>
      expect(screen.getByRole('button', { name: 'Remove CA certificate' })).toBeInTheDocument()
    );
    fireEvent.click(screen.getByRole('button', { name: /Add provider/i }));

    await waitFor(() =>
      expect(vi.mocked(flushCloudProviders)).toHaveBeenCalledWith(
        expect.arrayContaining([expect.objectContaining({ ca_cert_pem: pem })])
      )
    );
    await waitFor(() => expect(vi.mocked(listProviderModels)).toHaveBeenCalled());
    expect(vi.mocked(flushCloudProviders).mock.invocationCallOrder.at(-1)).toBeLessThan(
      vi.mocked(listProviderModels).mock.invocationCallOrder.at(-1) ?? Number.MAX_SAFE_INTEGER
    );
  });

  it('keeps the most recently selected CA when an older file read finishes later', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    renderWithProviders(<AIPanel />);
    await openCustomProviderEditor();

    fireEvent.change(screen.getByLabelText(/^Name$/i), { target: { value: 'Team Gateway' } });
    fireEvent.change(screen.getByLabelText(/OpenAI URL/i), {
      target: { value: 'https://gateway.example.com/v1' },
    });

    let resolveFirst!: (pem: string) => void;
    const first = new File(['first'], 'first-ca.pem', { type: 'application/x-pem-file' });
    Object.defineProperty(first, 'text', {
      value: () => new Promise<string>(resolve => (resolveFirst = resolve)),
    });
    const secondPem = '-----BEGIN CERTIFICATE-----\nsecond\n-----END CERTIFICATE-----';
    const second = new File([secondPem], 'second-ca.pem', { type: 'application/x-pem-file' });
    fireEvent.change(screen.getByLabelText(/CA certificate/i), { target: { files: [first] } });
    fireEvent.change(screen.getByLabelText(/CA certificate/i), { target: { files: [second] } });

    await waitFor(() =>
      expect(screen.getByRole('button', { name: 'Remove CA certificate' })).toBeInTheDocument()
    );
    await act(async () => {
      resolveFirst('-----BEGIN CERTIFICATE-----\nfirst\n-----END CERTIFICATE-----');
      await Promise.resolve();
    });
    fireEvent.click(screen.getByRole('button', { name: /Add provider/i }));

    await waitFor(() =>
      expect(vi.mocked(flushCloudProviders)).toHaveBeenCalledWith(
        expect.arrayContaining([expect.objectContaining({ ca_cert_pem: secondPem })])
      )
    );
  });

  // ─── local runtime: Ollama endpoint URL dialog ──────────────────────────────

  it('toggling Ollama ON shows an Endpoint URL field with localhost default', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    renderWithProviders(<AIPanel />);
    await openProviderConnectDialog('ollama', 'local');

    // ProviderKeyDialog renders in endpoint mode for local runtimes: the
    // input is labelled "Endpoint URL", not "API key".
    const dialog = await screen.findByRole('dialog', { name: /Connect Ollama/i });
    const urlInput = within(dialog).getByLabelText(/Endpoint URL/i) as HTMLInputElement;
    expect(urlInput).toBeInTheDocument();
    expect(urlInput.value).toBe('http://localhost:11434/v1');
    expect(within(dialog).queryByLabelText(/API key/i)).not.toBeInTheDocument();
  });

  it('rejects a non-http endpoint URL and keeps the dialog open', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    renderWithProviders(<AIPanel />);
    await openProviderConnectDialog('ollama', 'local');
    const dialog = await screen.findByRole('dialog', { name: /Connect Ollama/i });
    const urlInput = within(dialog).getByLabelText(/Endpoint URL/i);
    fireEvent.change(urlInput, { target: { value: 'ftp://nope' } });
    fireEvent.click(within(dialog).getByRole('button', { name: /^Save$/i }));

    // Inline error appears; dialog stays mounted; base_url persist never fires.
    await waitFor(() =>
      expect(within(dialog).getByText(/must start with http/i)).toBeInTheDocument()
    );
    expect(vi.mocked(openhumanUpdateLocalAiSettingsMock)).not.toHaveBeenCalled();
  });

  it('Ollama save normalizes the endpoint and persists local_ai.base_url', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    renderWithProviders(<AIPanel />);
    await openProviderConnectDialog('ollama', 'local');
    const dialog = await screen.findByRole('dialog', { name: /Connect Ollama/i });

    // Type a host with no path — the URL normalizer must append `/v1` for
    // the /models probe and the base_url derivation strips it back off.
    fireEvent.change(within(dialog).getByLabelText(/Endpoint URL/i), {
      target: { value: 'http://10.0.0.4:11434' },
    });
    fireEvent.click(within(dialog).getByRole('button', { name: /^Save$/i }));

    await waitFor(() => expect(openhumanUpdateLocalAiSettingsMock).toHaveBeenCalled());
    const [arg] = vi.mocked(openhumanUpdateLocalAiSettingsMock).mock.calls[0];
    expect(arg).toMatchObject({
      base_url: 'http://10.0.0.4:11434',
      provider: 'ollama',
      runtime_enabled: true,
      opt_in_confirmed: true,
    });
  });

  it('passes Ollama 0.0.0.0 endpoint through to the Rust normalizer', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    renderWithProviders(<AIPanel />);
    await openProviderConnectDialog('ollama', 'local');
    const dialog = await screen.findByRole('dialog', { name: /Connect Ollama/i });

    fireEvent.change(within(dialog).getByLabelText(/Endpoint URL/i), {
      target: { value: 'http://0.0.0.0:11434' },
    });
    fireEvent.click(within(dialog).getByRole('button', { name: /^Save$/i }));

    await waitFor(() => expect(openhumanUpdateLocalAiSettingsMock).toHaveBeenCalled());
    const [arg] = vi.mocked(openhumanUpdateLocalAiSettingsMock).mock.calls[0];
    expect(arg).toMatchObject({ base_url: 'http://0.0.0.0:11434' });
  });

  it('lets users edit an existing Ollama endpoint from the provider chip', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({
      ...baseSettings,
      cloudProviders: [
        {
          id: 'p_ollama_1',
          slug: 'ollama',
          label: 'Ollama',
          endpoint: 'http://127.0.0.1:11434/v1',
          auth_style: 'none' as const,
          has_api_key: true,
        },
      ],
    });
    renderWithProviders(<AIPanel />);
    await openProviderRowAction('ollama', /Edit endpoint/i);

    const dialog = await screen.findByRole('dialog', { name: /Connect Ollama/i });
    const urlInput = within(dialog).getByLabelText(/Endpoint URL/i) as HTMLInputElement;
    expect(urlInput.value).toBe('http://127.0.0.1:11434/v1');
  });

  it('LM Studio save persists the local_ai provider and endpoint', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    renderWithProviders(<AIPanel />);
    await openProviderConnectDialog('lmstudio', 'local');
    const dialog = await screen.findByRole('dialog', { name: /Connect LM Studio/i });

    fireEvent.change(within(dialog).getByLabelText(/Endpoint URL/i), {
      target: { value: 'http://127.0.0.1:1234/v1' },
    });
    fireEvent.click(within(dialog).getByRole('button', { name: /^Save$/i }));

    await waitFor(() => expect(openhumanUpdateLocalAiSettingsMock).toHaveBeenCalled());
    const [arg] = vi.mocked(openhumanUpdateLocalAiSettingsMock).mock.calls[0];
    expect(arg).toMatchObject({
      base_url: 'http://127.0.0.1:1234/v1',
      provider: 'lm_studio',
      runtime_enabled: true,
      opt_in_confirmed: true,
    });
  });

  // ─── local runtime: edit endpoint button on enabled chip ────────────────────

  it('shows an edit-endpoint button on enabled Ollama chip', async () => {
    const settingsWithOllama = {
      ...baseSettings,
      cloudProviders: [
        ...baseSettings.cloudProviders,
        {
          id: 'p_ollama_1',
          slug: 'ollama',
          label: 'Ollama',
          endpoint: 'http://192.168.1.5:11434/v1',
          auth_style: 'bearer' as const,
          has_api_key: false,
        },
      ],
      modelRegistry: [],
    };
    vi.mocked(loadAISettings).mockResolvedValue(settingsWithOllama);
    renderWithProviders(<AIPanel />);

    const row = await screen.findByTestId('provider-row-ollama');
    expect(within(row).getByRole('button')).toBeInTheDocument();
  });

  it('edit-endpoint button opens the dialog pre-populated with the saved URL', async () => {
    const settingsWithOllama = {
      ...baseSettings,
      cloudProviders: [
        ...baseSettings.cloudProviders,
        {
          id: 'p_ollama_1',
          slug: 'ollama',
          label: 'Ollama',
          endpoint: 'http://192.168.1.5:11434/v1',
          auth_style: 'bearer' as const,
          has_api_key: false,
        },
      ],
      modelRegistry: [],
    };
    vi.mocked(loadAISettings).mockResolvedValue(settingsWithOllama);
    renderWithProviders(<AIPanel />);

    await openProviderRowAction('ollama', /Edit endpoint/i);

    const dialog = await screen.findByRole('dialog', { name: /Connect Ollama/i });
    const urlInput = within(dialog).getByLabelText(/Endpoint URL/i) as HTMLInputElement;
    expect(urlInput.value).toBe('http://192.168.1.5:11434/v1');
  });

  it('does not show an edit-endpoint button when Ollama is disabled', async () => {
    vi.mocked(loadAISettings).mockResolvedValue({ ...baseSettings, cloudProviders: [] });
    renderWithProviders(<AIPanel />);

    fireEvent.click(await screen.findByTestId('add-provider-open'));
    const trigger = await screen.findByTestId('add-provider-select-local');
    trigger.focus();
    fireEvent.keyDown(trigger, { key: 'Enter' });
    expect(await screen.findByTestId('add-provider-option-ollama')).toBeInTheDocument();
    expect(screen.queryByRole('button', { name: /Edit endpoint/i })).not.toBeInTheDocument();
  });

  // ─── Custom routing dialog: per-workload temperature override ───────────────

  it('Custom routing dialog saves the routing change immediately from the modal', async () => {
    const settingsWithOpenAI = {
      cloudProviders: [
        {
          id: 'p_openai_1',
          slug: 'openai',
          label: 'OpenAI',
          endpoint: 'https://api.openai.com/v1',
          auth_style: 'bearer' as const,
          has_api_key: true,
        },
      ],
      routing: {
        ...baseSettings.routing,
        reasoning: { kind: 'cloud' as const, providerSlug: 'openai', model: 'gpt-4o' },
      },
      modelRegistry: [],
    };
    vi.mocked(loadAISettings).mockResolvedValue(settingsWithOpenAI);
    vi.mocked(saveAISettings).mockResolvedValue(undefined);
    renderWithProviders(<AIPanel />);

    fireEvent.click(await screen.findByRole('tab', { name: /^Routing$/i }));
    const reasoningRow = await screen.findByText('Reasoning');
    const rowEl = reasoningRow.closest('[data-slot="workload-row"]');
    expect(rowEl).not.toBeNull();
    fireEvent.click(within(rowEl as HTMLElement).getByRole('button'));

    const dialog = await screen.findByRole('dialog', { name: /Custom routing/i });

    // Enable temperature override; the slider + numeric input become visible.
    const tempToggle = within(dialog).getByLabelText(/Temperature override/i);
    fireEvent.click(tempToggle);

    const tempValueInput = within(dialog).getByLabelText(
      /Temperature override \(value\)/i
    ) as HTMLInputElement;
    expect(tempValueInput).toBeInTheDocument();
    fireEvent.change(tempValueInput, { target: { value: '0.2' } });

    // Save dialog → persists immediately without requiring the sticky Save bar.
    fireEvent.click(within(dialog).getByRole('button', { name: /^Save$/i }));
    await waitFor(() => expect(vi.mocked(saveAISettings)).toHaveBeenCalled());
    await waitFor(() =>
      expect(screen.queryByRole('dialog', { name: /Custom routing/i })).not.toBeInTheDocument()
    );
    expect(screen.queryByText(/unsaved change/i)).not.toBeInTheDocument();

    const [, next] = vi.mocked(saveAISettings).mock.calls[0];
    expect(next.routing.reasoning).toEqual({
      kind: 'cloud',
      providerSlug: 'openai',
      model: 'gpt-4o',
      temperature: 0.2,
    });
  });

  it('Custom routing dialog can test the selected cloud model and show its reply', async () => {
    const settingsWithOpenAI = {
      cloudProviders: [
        {
          id: 'p_openai_1',
          slug: 'openai',
          label: 'OpenAI',
          endpoint: 'https://api.openai.com/v1',
          auth_style: 'bearer' as const,
          has_api_key: true,
        },
      ],
      routing: {
        ...baseSettings.routing,
        reasoning: { kind: 'cloud' as const, providerSlug: 'openai', model: 'gpt-4o' },
      },
      modelRegistry: [],
    };
    vi.mocked(loadAISettings).mockResolvedValue(settingsWithOpenAI);
    vi.mocked(listProviderModels).mockResolvedValue([{ id: 'gpt-4o' }, { id: 'gpt-4o-mini' }]);
    vi.mocked(testProviderModel).mockResolvedValue({ reply: 'Hello from gpt-4o.' });

    renderWithProviders(<AIPanel />);

    fireEvent.click(await screen.findByRole('tab', { name: /^Routing$/i }));
    const reasoningRow = await screen.findByText('Reasoning');
    const rowEl = reasoningRow.closest('[data-slot="workload-row"]');
    expect(rowEl).not.toBeNull();
    fireEvent.click(within(rowEl as HTMLElement).getByRole('button'));

    const dialog = await screen.findByRole('dialog', { name: /Custom routing/i });
    fireEvent.click(within(dialog).getByRole('button', { name: /^Test$/i }));

    await waitFor(() =>
      expect(vi.mocked(testProviderModel)).toHaveBeenCalledWith(
        'reasoning',
        'openai:gpt-4o',
        'Hello world'
      )
    );
    expect(await within(dialog).findByText('Model response')).toBeInTheDocument();
    expect(within(dialog).getByText('Hello from gpt-4o.')).toBeInTheDocument();
  });

  it('Custom routing dialog shows in-flight test status immediately', async () => {
    const settingsWithOpenAI = {
      cloudProviders: [
        {
          id: 'p_openai_1',
          slug: 'openai',
          label: 'OpenAI',
          endpoint: 'https://api.openai.com/v1',
          auth_style: 'bearer' as const,
          has_api_key: true,
        },
      ],
      routing: {
        ...baseSettings.routing,
        reasoning: { kind: 'cloud' as const, providerSlug: 'openai', model: 'gpt-4o' },
      },
      modelRegistry: [],
    };
    vi.mocked(loadAISettings).mockResolvedValue(settingsWithOpenAI);
    vi.mocked(listProviderModels).mockResolvedValue([{ id: 'gpt-4o' }]);
    let resolveTest: (value: { reply: string }) => void = () => {};
    const pendingTest = new Promise<{ reply: string }>(resolve => {
      resolveTest = resolve;
    });
    vi.mocked(testProviderModel).mockReturnValue(pendingTest);

    renderWithProviders(<AIPanel />);

    fireEvent.click(await screen.findByRole('tab', { name: /^Routing$/i }));
    const reasoningRow = await screen.findByText('Reasoning');
    const rowEl = reasoningRow.closest('[data-slot="workload-row"]');
    expect(rowEl).not.toBeNull();
    fireEvent.click(within(rowEl as HTMLElement).getByRole('button'));

    const dialog = await screen.findByRole('dialog', { name: /Custom routing/i });
    fireEvent.click(within(dialog).getByRole('button', { name: /^Test$/i }));

    expect(await within(dialog).findByText('Testing model...')).toBeInTheDocument();
    expect(within(dialog).getByText(/Provider: openai:gpt-4o/i)).toBeInTheDocument();
    expect(within(dialog).getByText(/Prompt: Hello world/i)).toBeInTheDocument();

    resolveTest({ reply: 'Hello from gpt-4o.' });
    expect(await within(dialog).findByText('Model response')).toBeInTheDocument();
  });

  it('Custom routing dialog shows test errors inline', async () => {
    const settingsWithOpenAI = {
      cloudProviders: [
        {
          id: 'p_openai_1',
          slug: 'openai',
          label: 'OpenAI',
          endpoint: 'https://api.openai.com/v1',
          auth_style: 'bearer' as const,
          has_api_key: true,
        },
      ],
      routing: {
        ...baseSettings.routing,
        reasoning: { kind: 'cloud' as const, providerSlug: 'openai', model: 'gpt-4o' },
      },
      modelRegistry: [],
    };
    vi.mocked(loadAISettings).mockResolvedValue(settingsWithOpenAI);
    vi.mocked(listProviderModels).mockResolvedValue([{ id: 'gpt-4o' }]);
    vi.mocked(testProviderModel).mockRejectedValue(new Error('401 invalid api key'));

    renderWithProviders(<AIPanel />);

    fireEvent.click(await screen.findByRole('tab', { name: /^Routing$/i }));
    const reasoningRow = await screen.findByText('Reasoning');
    const rowEl = reasoningRow.closest('[data-slot="workload-row"]');
    expect(rowEl).not.toBeNull();
    fireEvent.click(within(rowEl as HTMLElement).getByRole('button'));

    const dialog = await screen.findByRole('dialog', { name: /Custom routing/i });
    fireEvent.click(within(dialog).getByRole('button', { name: /^Test$/i }));

    // The banner carries the actionable message keyed to the provider slug,
    // not the raw upstream string (which can echo request material).
    const alert = await within(dialog).findByRole('alert');
    expect(alert).toHaveTextContent('rejected it');
    expect(alert).toHaveTextContent('openai');
    expect(alert).not.toHaveTextContent('401 invalid api key');
  });

  it('renders background loop diagnostics with newest spend row and budget math', async () => {
    // BackgroundLoopControls was moved out of AIPanel into standalone panels.
    renderWithProviders(<BackgroundLoopControls view="all" />);

    await waitFor(() => expect(screen.getByText('Background loops')).toBeInTheDocument());

    expect(screen.getByText('Recent usage ledger')).toBeInTheDocument();
    expect(screen.getByText('Loop map')).toBeInTheDocument();
    expect(screen.getByText('Composio sync')).toBeInTheDocument();

    expect(screen.getByText('Week budget')).toBeInTheDocument();
    expect(screen.getByText('$10.0000')).toBeInTheDocument();
    expect(screen.getByText('Cycle remaining')).toBeInTheDocument();
    expect(screen.getByText('$1.5000')).toBeInTheDocument();
    expect(screen.getByText('Avg spend row')).toBeInTheDocument();
    expect(screen.getByText('Bg API reads')).toBeInTheDocument();
    expect(screen.getByText('Bg wakeups')).toBeInTheDocument();

    expect(screen.getByText('Rows left')).toBeInTheDocument();
    expect(screen.getByText('Rows per full week budget')).toBeInTheDocument();
    expect(screen.getByText('Sample burn rate')).toBeInTheDocument();
    expect(screen.getByText('Projected empty')).toBeInTheDocument();
    expect(screen.getByText('API reads per $ remaining')).toBeInTheDocument();
    expect(screen.getByText('Loop call budget')).toBeInTheDocument();
    expect(screen.getByText('Composio sync scans')).toBeInTheDocument();

    expect(screen.getByText('MEMORY_SUMMARY')).toBeInTheDocument();
    expect(screen.getByText('SPEND:USAGE_DEDUCTION:USER')).toBeInTheDocument();
    expect(screen.getByText(/Latest spend: \$0\.5000/)).toBeInTheDocument();
  });
});

describe('buildRoutingDiffSummary', () => {
  const allDefault = (): RoutingMap => ({
    chat: { kind: 'default' },
    reasoning: { kind: 'default' },
    agentic: { kind: 'default' },
    coding: { kind: 'default' },
    vision: { kind: 'default' },
    memory: { kind: 'default' },
  });

  it('emits one "<label> → <target>" entry per changed workload and skips unchanged ones', () => {
    // Identity `t` so we can assert the workload's i18n label key is used.
    const t = (key: string) => key;
    const saved = allDefault();
    saved.coding = { kind: 'cloud', providerSlug: 'x', model: 'y' };
    const draft = allDefault();
    draft.chat = { kind: 'cloud', providerSlug: 'openai', model: 'gpt-4o', temperature: 0.3 };
    draft.reasoning = { kind: 'openhuman' };
    draft.agentic = { kind: 'local', model: 'llama3' };
    // coding: saved=cloud, draft=default → changed; describe(default) === 'cloud'.

    expect(buildRoutingDiffSummary(saved, draft, t)).toEqual([
      'settings.ai.routing.workload.chat.label → openai:[email protected]',
      'settings.ai.routing.workload.reasoning.label → openhuman',
      'settings.ai.routing.workload.agentic.label → local:llama3',
      'settings.ai.routing.workload.coding.label → cloud',
    ]);
  });

  it('returns an empty list when draft matches saved', () => {
    const routing = allDefault();
    expect(buildRoutingDiffSummary(routing, { ...routing }, k => k)).toEqual([]);
  });
});