mcpSettingsSharedProtocolVersion.test.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. EMPTY_FORM,
  4. formToConfig,
  5. jsonDraftToForm,
  6. serverToForm,
  7. } from "@/settings/mcpSettingsShared.js";
  8. import type { ZCodeMcpServer } from "@zcode/shared";
  9. describe("mcp settings shared protocolVersion round-trip", () => {
  10. it("preserves a configured protocolVersion across serverToForm -> formToConfig", () => {
  11. const server: ZCodeMcpServer = {
  12. config: { protocolVersion: "legacy", type: "http", url: "https://mcp.example.com/mcp" },
  13. name: "feishu",
  14. scope: "user",
  15. } as ZCodeMcpServer;
  16. const form = serverToForm(server);
  17. expect(form.protocolVersion).toBe("legacy");
  18. expect(formToConfig(form)).toMatchObject({ protocolVersion: "legacy", type: "http" });
  19. });
  20. it("omits protocolVersion from the config when the form leaves it unset (auto)", () => {
  21. const form = { ...EMPTY_FORM, type: "http" as const, url: "https://mcp.example.com/mcp" };
  22. expect("protocolVersion" in formToConfig(form)).toBe(false);
  23. });
  24. it("preserves protocolVersion when a JSON draft is converted back to the form", () => {
  25. const form = jsonDraftToForm(
  26. JSON.stringify({
  27. feishu: { protocolVersion: "2026-07-28", type: "http", url: "https://mcp.example.com/mcp" },
  28. }),
  29. EMPTY_FORM,
  30. );
  31. expect(form.protocolVersion).toBe("2026-07-28");
  32. });
  33. // SG-01:config/JSON 里的非法枚举值在进表单时归一为未设置(等价 auto),
  34. // 与 shared DTO 的 isMcpProtocolVersion 静默丢弃行为对齐,避免下拉显示空白。
  35. it("normalizes an invalid protocolVersion to unset instead of surfacing it in the form", () => {
  36. const server = {
  37. config: { protocolVersion: "invalid", type: "http", url: "https://mcp.example.com/mcp" },
  38. name: "feishu",
  39. scope: "user",
  40. } as unknown as ZCodeMcpServer;
  41. expect(serverToForm(server).protocolVersion).toBe("");
  42. const draft = jsonDraftToForm(
  43. JSON.stringify({
  44. feishu: { protocolVersion: 42, type: "http", url: "https://mcp.example.com/mcp" },
  45. }),
  46. EMPTY_FORM,
  47. );
  48. expect(draft.protocolVersion).toBe("");
  49. });
  50. });