mcpFailurePresentation.test.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { describe, expect, it } from "vitest";
  2. import { createElement } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { ZCodeIntlProvider } from "../src/i18n/IntlProvider.js";
  5. import {
  6. McpFailurePresentation,
  7. resolveMcpFailureMessageId,
  8. } from "../src/settings/McpFailurePresentation.js";
  9. describe("MCP failure presentation", () => {
  10. it("maps every stable failure kind to a localized message id", () => {
  11. expect(resolveMcpFailureMessageId("server_not_found")).toBe(
  12. "settings.mcp.failure.server_not_found",
  13. );
  14. expect(resolveMcpFailureMessageId("official_origin_untrusted")).toBe(
  15. "settings.mcp.failure.official_origin_untrusted",
  16. );
  17. expect(resolveMcpFailureMessageId(undefined)).toBe("settings.mcp.failure.connection_failed");
  18. expect(resolveMcpFailureMessageId("not_authenticated")).toBe(
  19. "settings.mcp.failure.not_authenticated",
  20. );
  21. expect(resolveMcpFailureMessageId("coding_plan_required")).toBe(
  22. "settings.mcp.failure.coding_plan_required",
  23. );
  24. });
  25. it.each([
  26. ["zh-CN" as const, "MCP 服务暂时不可用,请稍后重试。"],
  27. ["en-US" as const, "The MCP service is temporarily unavailable. Try again later."],
  28. ])("keeps diagnostics inside the click-only popover for %s", (locale, message) => {
  29. const html = renderToStaticMarkup(
  30. createElement(
  31. ZCodeIntlProvider,
  32. { initialLocale: locale },
  33. createElement(McpFailurePresentation, {
  34. error: "raw sdk error",
  35. failureKind: "server_unavailable",
  36. }),
  37. ),
  38. );
  39. expect(html).toContain(message);
  40. expect(html).toContain('data-slot="popover-trigger"');
  41. expect(html).not.toContain("req-1000");
  42. expect(html).not.toContain("Request ID");
  43. expect(html).not.toContain("请求 ID");
  44. expect(html).not.toContain("<details");
  45. expect(html).not.toContain("text-destructive");
  46. expect(html).toContain("text-foreground-subtle");
  47. expect(html).not.toContain("raw sdk error");
  48. });
  49. it.each([
  50. ["zh-CN" as const, "当前未登录,请先登录 ZCode。"],
  51. ["en-US" as const, "You are not signed in. Sign in to ZCode to use this MCP server."],
  52. ])("localizes the not-authenticated diagnosis for %s", (locale, message) => {
  53. const html = renderToStaticMarkup(
  54. createElement(
  55. ZCodeIntlProvider,
  56. { initialLocale: locale },
  57. createElement(McpFailurePresentation, { failureKind: "not_authenticated" }),
  58. ),
  59. );
  60. expect(html).toContain(message);
  61. });
  62. it.each([
  63. ["zh-CN" as const, "当前账号没有 Coding Plan,请先购买或配置 Coding Plan。"],
  64. [
  65. "en-US" as const,
  66. "This account has no Coding Plan. Purchase or configure a Coding Plan to use this MCP server.",
  67. ],
  68. ])("localizes the coding-plan diagnosis for %s", (locale, message) => {
  69. const html = renderToStaticMarkup(
  70. createElement(
  71. ZCodeIntlProvider,
  72. { initialLocale: locale },
  73. createElement(McpFailurePresentation, { failureKind: "coding_plan_required" }),
  74. ),
  75. );
  76. expect(html).toContain(message);
  77. });
  78. });