Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

11.6 KB
Raw Blame
import {
  fireEvent,
  render,
  screen,
  waitFor,
  within,
} from "@testing-library/react";
import type { ComponentProps, PropsWithChildren } from "react";
import { useForm } from "react-hook-form";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { ClaudeFormFields } from "@/components/providers/forms/ClaudeFormFields";
import { Form } from "@/components/ui/form";

const copilotApiMock = vi.hoisted(() => ({
  copilotGetModels: vi.fn(),
  copilotGetModelsForAccount: vi.fn(),
}));

const modelFetchApiMock = vi.hoisted(() => ({
  fetchCodexOauthModels: vi.fn(),
  fetchModelsForConfig: vi.fn(),
  modelFetchRequestHeaders: vi.fn(),
  showFetchModelsError: vi.fn(),
}));

vi.mock("@/lib/api/copilot", () => ({
  copilotGetModels: copilotApiMock.copilotGetModels,
  copilotGetModelsForAccount: copilotApiMock.copilotGetModelsForAccount,
}));

vi.mock("@/lib/api/model-fetch", async (importOriginal) => ({
  claudeModelFetchApiFormat: (
    await importOriginal<typeof import("@/lib/api/model-fetch")>()
  ).claudeModelFetchApiFormat,
  fetchCodexOauthModels: modelFetchApiMock.fetchCodexOauthModels,
  fetchModelsForConfig: modelFetchApiMock.fetchModelsForConfig,
  modelFetchRequestHeaders: modelFetchApiMock.modelFetchRequestHeaders,
  showFetchModelsError: modelFetchApiMock.showFetchModelsError,
}));

vi.mock("@/components/providers/forms/CopilotAuthSection", () => ({
  CopilotAuthSection: () => <div data-testid="copilot-auth-section" />,
}));

vi.mock("@/components/providers/forms/CodexOAuthSection", () => ({
  CodexOAuthSection: () => <div data-testid="codex-oauth-section" />,
}));

type ClaudeFormFieldsProps = ComponentProps<typeof ClaudeFormFields>;

const FormShell = ({ children }: PropsWithChildren) => {
  const form = useForm();

  return <Form {...form}>{children}</Form>;
};

const renderCopilotForm = (overrides: Partial<ClaudeFormFieldsProps> = {}) => {
  const props: ClaudeFormFieldsProps = {
    shouldShowApiKey: false,
    apiKey: "",
    onApiKeyChange: vi.fn(),
    category: "official",
    shouldShowApiKeyLink: false,
    websiteUrl: "",
    isCopilotPreset: true,
    usesOAuth: true,
    isCopilotAuthenticated: true,
    selectedGitHubAccountId: "gh-1",
    onGitHubAccountSelect: vi.fn(),
    isCodexOauthPreset: false,
    isCodexOauthAuthenticated: false,
    selectedCodexAccountId: null,
    onCodexAccountSelect: vi.fn(),
    codexFastMode: false,
    onCodexFastModeChange: vi.fn(),
    templateValueEntries: [],
    templateValues: {},
    templatePresetName: "",
    onTemplateValueChange: vi.fn(),
    shouldShowSpeedTest: false,
    baseUrl: "",
    onBaseUrlChange: vi.fn(),
    isEndpointModalOpen: false,
    onEndpointModalToggle: vi.fn(),
    onCustomEndpointsChange: vi.fn(),
    autoSelect: false,
    onAutoSelectChange: vi.fn(),
    showEndpointTools: true,
    shouldShowModelSelector: true,
    claudeModel: "",
    defaultHaikuModel: "",
    defaultHaikuModelName: "",
    defaultSonnetModel: "claude-sonnet",
    defaultSonnetModelName: "Claude Sonnet",
    defaultOpusModel: "",
    defaultOpusModelName: "",
    defaultFableModel: "",
    defaultFableModelName: "",
    subagentModel: "",
    onModelChange: vi.fn(),
    speedTestEndpoints: [],
    apiFormat: "anthropic",
    onApiFormatChange: vi.fn(),
    reasoningEffort: "auto",
    onReasoningEffortChange: vi.fn(),
    apiKeyField: "ANTHROPIC_AUTH_TOKEN",
    onApiKeyFieldChange: vi.fn(),
    isFullUrl: false,
    onFullUrlChange: vi.fn(),
    customUserAgent: "",
    onCustomUserAgentChange: vi.fn(),
    localProxyHeadersOverride: "",
    onLocalProxyHeadersOverrideChange: vi.fn(),
    localProxyBodyOverride: "",
    onLocalProxyBodyOverrideChange: vi.fn(),
    ...overrides,
  };

  const view = render(
    <FormShell>
      <ClaudeFormFields {...props} />
    </FormShell>,
  );

  return { ...view, props };
};

const renderCodexOauthForm = (overrides: Partial<ClaudeFormFieldsProps> = {}) =>
  renderCopilotForm({
    isCopilotPreset: false,
    isCopilotAuthenticated: false,
    selectedGitHubAccountId: null,
    isCodexOauthPreset: true,
    isCodexOauthAuthenticated: true,
    selectedCodexAccountId: "chatgpt-1",
    ...overrides,
  });

describe("ClaudeFormFields", () => {
  beforeEach(() => {
    copilotApiMock.copilotGetModels.mockResolvedValue({
      models: [],
      autoRoutingSupported: false,
    });
    copilotApiMock.copilotGetModelsForAccount.mockResolvedValue({
      models: [],
      autoRoutingSupported: false,
    });
    modelFetchApiMock.fetchCodexOauthModels.mockResolvedValue([]);
    modelFetchApiMock.fetchModelsForConfig.mockResolvedValue([]);
  });

  it("不会在 Copilot 表单打开时自动获取模型列表", () => {
    renderCopilotForm();

    expect(copilotApiMock.copilotGetModels).not.toHaveBeenCalled();
    expect(copilotApiMock.copilotGetModelsForAccount).not.toHaveBeenCalled();
  });

  it.each(["classic", "stack"] as const)(
    "Copilot %s 布局显示自动协议并禁用无效的手选格式",
    (variant) => {
      renderCopilotForm({ variant, apiFormat: "openai_responses" });
      const control = screen.getByRole("combobox", { name: "上游格式" });
      expect(control).toBeDisabled();
      expect(control).toHaveTextContent("copilot.automaticFormat");
      expect(screen.getByText(/copilot.automaticFormatHint/)).toBeVisible();
    },
  );

  it.each(["classic", "stack"] as const)(
    "%s 布局只在转成 OpenAI 格式时显示思考档位",
    (variant) => {
      const { unmount } = renderCopilotForm({
        variant,
        isCopilotPreset: false,
        apiFormat: "openai_chat",
      });
      expect(screen.getByRole("combobox", { name: "思考档位" })).toBeVisible();
      unmount();

      renderCopilotForm({
        variant,
        isCopilotPreset: false,
        apiFormat: "anthropic",
      });
      expect(screen.queryByRole("combobox", { name: "思考档位" })).toBeNull();
    },
  );

  it("Copilot 卡不显示思考档位", () => {
    renderCopilotForm({ apiFormat: "openai_chat" });
    expect(screen.queryByRole("combobox", { name: "思考档位" })).toBeNull();
  });

  it("点击获取模型列表后才请求当前 Copilot 账号的模型", async () => {
    renderCopilotForm();

    fireEvent.click(
      screen.getByRole("button", {
        name: "providerForm.fetchModels",
      }),
    );

    await waitFor(() => {
      expect(copilotApiMock.copilotGetModelsForAccount).toHaveBeenCalledWith(
        "gh-1",
      );
    });
    expect(copilotApiMock.copilotGetModels).not.toHaveBeenCalled();
  });

  it("不会在 Codex OAuth 表单打开时自动获取模型列表", () => {
    renderCodexOauthForm();

    expect(modelFetchApiMock.fetchCodexOauthModels).not.toHaveBeenCalled();
  });

  it("点击获取模型列表后才请求当前 Codex OAuth 账号的模型", async () => {
    renderCodexOauthForm();

    fireEvent.click(
      screen.getByRole("button", {
        name: "providerForm.fetchModels",
      }),
    );

    await waitFor(() => {
      expect(modelFetchApiMock.fetchCodexOauthModels).toHaveBeenCalledWith(
        "chatgpt-1",
      );
    });
  });

  it("Genesis 使用当前 Key 拉取可用对话模型", async () => {
    Element.prototype.scrollIntoView ??= vi.fn();
    const onModelChange = vi.fn();
    modelFetchApiMock.fetchModelsForConfig.mockResolvedValueOnce([
      { id: "chat-model-a" },
      { id: "chat-model-b" },
    ]);
    renderCopilotForm({
      isCopilotPreset: false,
      usesOAuth: false,
      category: "aggregator",
      shouldShowApiKey: true,
      apiKey: "test-key",
      baseUrl: "https://token.moi.matrixorigin.cn",
      defaultSonnetModel: "",
      defaultSonnetModelName: "",
      onModelChange,
    });

    fireEvent.click(
      screen.getByRole("button", {
        name: "providerForm.advancedOptionsToggle",
      }),
    );
    fireEvent.click(
      screen.getByRole("button", { name: "providerForm.fetchModels" }),
    );

    await waitFor(() => {
      expect(modelFetchApiMock.fetchModelsForConfig).toHaveBeenCalledWith(
        "https://token.moi.matrixorigin.cn",
        "test-key",
        false,
        "https://token.moi.matrixorigin.cn/v1/models?type=chat",
        "",
        { requestHeaders: undefined },
      );
    });

    fireEvent.click(screen.getAllByRole("button", { name: "Select model" })[0]);
    fireEvent.click(screen.getByText("chat-model-b"));
    expect(onModelChange).toHaveBeenCalledWith(
      "ANTHROPIC_DEFAULT_SONNET_MODEL",
      "chat-model-b",
    );
  });

  it("Gemini Native 拉取模型时用 Google 的 API Key 头", async () => {
    modelFetchApiMock.fetchModelsForConfig.mockResolvedValueOnce([]);
    renderCopilotForm({
      isCopilotPreset: false,
      usesOAuth: false,
      category: "custom",
      shouldShowApiKey: true,
      apiKey: "AIza-test",
      baseUrl: "https://generativelanguage.googleapis.com",
      apiFormat: "gemini_native",
    });

    // 非 Anthropic 格式的高级选项默认展开,直接点获取模型
    fireEvent.click(
      screen.getByRole("button", { name: "providerForm.fetchModels" }),
    );

    await waitFor(() => {
      expect(modelFetchApiMock.fetchModelsForConfig).toHaveBeenCalledWith(
        "https://generativelanguage.googleapis.com",
        "AIza-test",
        false,
        undefined,
        "",
        { apiFormat: "google-generative-ai", requestHeaders: undefined },
      );
    });
  });

  it("一键设置会同时写入 Subagent 模型", () => {
    const onModelChange = vi.fn();
    renderCopilotForm({
      claudeModel: "shared-model[1M]",
      defaultSonnetModel: "",
      defaultSonnetModelName: "",
      onModelChange,
    });

    fireEvent.click(
      screen.getByRole("button", {
        name: "一键设置",
      }),
    );

    expect(onModelChange).toHaveBeenCalledWith(
      "CLAUDE_CODE_SUBAGENT_MODEL",
      "shared-model[1M]",
    );
  });

  it("Haiku 行也能勾 1M:勾选状态即模型名的 [1M] 后缀", () => {
    const onModelChange = vi.fn();
    renderCopilotForm({
      isCopilotPreset: false,
      usesOAuth: false,
      defaultHaikuModel: "glm-5.3-flash[1M]",
      defaultHaikuModelName: "",
      onModelChange,
    });

    const haikuRow = screen.getByText("Haiku").parentElement!;
    expect(within(haikuRow).getByDisplayValue("glm-5.3-flash")).toBeTruthy();
    const checkbox = within(haikuRow).getByRole("checkbox");
    expect(checkbox).toBeChecked();

    fireEvent.click(checkbox);
    expect(onModelChange).toHaveBeenCalledWith(
      "ANTHROPIC_DEFAULT_HAIKU_MODEL",
      "glm-5.3-flash",
    );
  });

  // #6308: the model display name is free-form text that CJK users type through
  // an IME. #6333 fixed the equivalent Hermes / OpenClaw rows; this editor kept
  // a raw controlled input, so a parent render mid-composition could replace the
  // marked text the platform IME still owns.
  it("keeps model display name composition local until the IME commits", () => {
    const onModelChange = vi.fn();
    const { props, rerender } = renderCopilotForm({ onModelChange });
    const displayNameInput = screen.getByDisplayValue("Claude Sonnet");

    fireEvent.compositionStart(displayNameInput);
    fireEvent.change(displayNameInput, { target: { value: "深度求索 V4" } });

    expect(displayNameInput).toHaveValue("深度求索 V4");
    expect(onModelChange).not.toHaveBeenCalled();

    rerender(
      <FormShell>
        <ClaudeFormFields {...props} />
      </FormShell>,
    );
    expect(displayNameInput).toHaveValue("深度求索 V4");

    fireEvent.compositionEnd(displayNameInput, {
      data: "深度求索 V4",
      target: { value: "深度求索 V4" },
    });

    expect(onModelChange).toHaveBeenCalledTimes(1);
    expect(onModelChange).toHaveBeenCalledWith(
      "ANTHROPIC_DEFAULT_SONNET_MODEL_NAME",
      "深度求索 V4",
    );
  });
});