mcpSettingsSharedProtocolVersion.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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(form.protocolVersion).toBe("");
  23. expect("protocolVersion" in formToConfig(form)).toBe(false);
  24. });
  25. it("preserves protocolVersion when a JSON draft is converted back to the form", () => {
  26. const form = jsonDraftToForm(
  27. JSON.stringify({
  28. feishu: { protocolVersion: "2026-07-28", type: "http", url: "https://mcp.example.com/mcp" },
  29. }),
  30. EMPTY_FORM,
  31. );
  32. expect(form.protocolVersion).toBe("2026-07-28");
  33. });
  34. // SG-01:config/JSON 里的非法枚举值在进表单时归一为未设置(等价 auto),
  35. // 与 shared DTO 的 isMcpProtocolVersion 静默丢弃行为对齐,避免下拉显示空白。
  36. it("normalizes an invalid protocolVersion to unset instead of surfacing it in the form", () => {
  37. const server = {
  38. config: { protocolVersion: "invalid", type: "http", url: "https://mcp.example.com/mcp" },
  39. name: "feishu",
  40. scope: "user",
  41. } as unknown as ZCodeMcpServer;
  42. expect(serverToForm(server).protocolVersion).toBe("");
  43. const draft = jsonDraftToForm(
  44. JSON.stringify({
  45. feishu: { protocolVersion: 42, type: "http", url: "https://mcp.example.com/mcp" },
  46. }),
  47. EMPTY_FORM,
  48. );
  49. expect(draft.protocolVersion).toBe("");
  50. });
  51. });