Files
cc-switch/tests/utils/errorUtils.test.ts
Owlbay 1cc2c73213 fix(mcp): follow up on Pi MCP sync review notes (#7888)
Follow-up to #7862.

- Import from Pi follows Pi's rules: SSE entries, invalid names, names that differ only by -/_ from another entry or from a server already enabled for Pi are skipped and reported instead of being imported enabled and failing every sync.
- When the Pi agent directory does not exist, nothing is written (no more creating ~/.pi/agent/mcp.json); Pi's rules are still enforced, and the -/_ collision check against servers enabled for Pi runs under the Pi write lock so concurrent toggles cannot enable both.
- Map the Pi SSE / invalid-name / name-collision errors to zh, zh-TW, en and ja.
- Document Pi MCP limits, sync condition and import rules in the zh/en/ja user manual.
- Pi integration tests clear PI_CODING_AGENT_DIR and clean .pi.
2026-11-02 13:03:03 +08:00

80 lines
2.5 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it, vi } from "vitest";
import {
extractErrorMessage,
translateMcpBackendError,
translateKimiProviderMutationError,
translatePiProviderMutationError,
} from "@/utils/errorUtils";
describe("error utilities", () => {
it("extracts Tauri string errors", () => {
expect(extractErrorMessage("backend failed")).toBe("backend failed");
});
it("maps a simultaneous models.json write to a concise error", () => {
const t = vi.fn((key: string) => key);
expect(
translatePiProviderMutationError(
"Pi models.json changed outside CC Switch",
t,
),
).toBe("pi.provider.writeConflict");
});
it("maps a duplicate Pi provider key to validation feedback", () => {
const t = vi.fn((key: string) => key);
expect(
translatePiProviderMutationError(
"无效输入: Pi provider key 'duplicate' already exists in models.json",
t,
),
).toBe("pi.form.providerKeyDuplicate");
});
it("maps the MCP servers Pi refuses to localized messages", () => {
const t = vi.fn(
(key: string, opts?: Record<string, unknown>) =>
`${key}${opts ? JSON.stringify(opts) : ""}`,
);
expect(
translateMcpBackendError(
"MCP 校验失败: Pi 不支持 SSE 连接方式的 MCP 服务器,请改用 HTTP(streamable HTTP)",
t,
),
).toBe("mcp.error.piSseUnsupported");
expect(
translateMcpBackendError(
"MCP 校验失败: Pi 的 MCP 服务器名只能包含字母、数字、_ 和 -:bad name",
t,
),
).toBe('mcp.error.piInvalidName{"id":"bad name"}');
expect(
translateMcpBackendError(
"MCP 校验失败: Pi 把只差 - 和 _ 的服务器名视为同一个:dev_tools 与已有的 dev-tools 冲突",
t,
),
).toBe('mcp.error.piNameConflict{"id":"dev_tools","other":"dev-tools"}');
});
it("explains Kimi removals refused because Kimi Code still uses the model", () => {
const t = vi.fn((key: string) => key);
expect(
translateKimiProviderMutationError(
"无效输入: Select another default model in Kimi Code before removing this model or provider",
t,
),
).toBe("kimi.error.defaultModelInUse");
expect(
translateKimiProviderMutationError(
"无效输入: Update the secondary model selection in Kimi Code before removing this model or provider",
t,
),
).toBe("kimi.error.secondaryModelInUse");
expect(translateKimiProviderMutationError("other failure", t)).toBe("");
});
});