diagnostics.test.ts 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { expect, it } from "vitest";
  2. import expected from "./provider-diagnostics.json";
  3. import diagnosticText from "./diagnostic-text.json";
  4. import {
  5. compareMcpServerNames,
  6. renderMcpStatus,
  7. renderMcpSearchMiss,
  8. sanitizeMcpDiagnostic,
  9. } from "../../src/tool/search/mcp-diagnostics.js";
  10. const failure = { name: "demo", errorCode: "ECONNREFUSED", error: "Connection refused" };
  11. it("renders complete provider-visible pending, failure and authorization diagnostics", () => {
  12. expect(renderMcpStatus({ pending: ["demo"], failed: [] }).join("\n\n")).toBe(expected.pending);
  13. expect(renderMcpStatus({ pending: [], failed: [failure] }).join("\n\n")).toBe(expected.failed);
  14. expect(renderMcpStatus({ pending: [], needsAuth: ["demo"], failed: [] }).join("\n\n")).toBe(
  15. expected.needsAuth,
  16. );
  17. expect(renderMcpSearchMiss({ pending: [], failed: [failure] })).toBe(expected.failedSearch);
  18. });
  19. it("orders status paragraphs as authorization, failure, then pending", () => {
  20. const paragraphs = renderMcpStatus({
  21. pending: ["waiting"],
  22. needsAuth: ["locked"],
  23. failed: [{ name: "broken" }],
  24. });
  25. expect(paragraphs.map((text) => text.split("\n")[0])).toEqual([
  26. "The following MCP servers require authentication before their tools can be used:",
  27. "The following MCP servers are configured but failed to connect — their tools (typically named mcp__<server>__*) are unavailable for this session:",
  28. "The following MCP servers are still connecting — their tools (typically named mcp__<server>__*) are not yet available but will appear shortly:",
  29. ]);
  30. });
  31. it("sorts server names with ASCII names first and non-ASCII names by code unit", () => {
  32. // 非 ASCII 名称若用 localeCompare,排序随运行环境区域设置变化,公告字节不稳定。
  33. const names = ["zeta", "ä-server", "Alpha", "日本", "beta", "Ångström"];
  34. expect([...names].sort(compareMcpServerNames)).toEqual([
  35. "Alpha",
  36. "beta",
  37. "zeta",
  38. "Ångström",
  39. "ä-server",
  40. "日本",
  41. ]);
  42. });
  43. it("limits diagnostics and keeps endpoint text on a bounded data-only line", () => {
  44. const names = Array.from({ length: 31 }, (_, index) => `server-${index}`);
  45. expect(renderMcpStatus({ pending: names, failed: [] }).join("\n\n")).toContain(
  46. "server-29, …and 1 more",
  47. );
  48. expect(
  49. renderMcpSearchMiss({
  50. pending: [],
  51. failed: names.map((name) => ({ name, error: 'bad\n<instruction> "run"' })),
  52. }),
  53. ).not.toContain("server-30");
  54. expect(sanitizeMcpDiagnostic('bad\n<instruction> "run"')).toBe("bad instruction run");
  55. expect(sanitizeMcpDiagnostic("token=abc123456789")).toBe("token [redacted]");
  56. expect(sanitizeMcpDiagnostic("bad\ud800 text")).toBe("bad text");
  57. expect(sanitizeMcpDiagnostic("x".repeat(201))).toBe(`${"x".repeat(200)}…`);
  58. });
  59. it.each(diagnosticText)("normalizes endpoint diagnostics: $input", ({ input, output }) => {
  60. expect(sanitizeMcpDiagnostic(input)).toBe(output);
  61. });
  62. it.each([
  63. [String.raw`\nBearer abc123456`, String.raw`\nBearer [redacted]`],
  64. [String.raw`\nToken=abc123456`, String.raw`\nToken [redacted]`],
  65. ["token==api_key", "token==api_key"],
  66. ["token===api_key=", "token===api_key="],
  67. ["Bearer api_key=", "Bearer api_key="],
  68. ["token=abc12345-token=def123456", "token [redacted] token [redacted]"],
  69. ["token=abc12345.password=def123456", "token [redacted] password [redacted]"],
  70. ["token=' 'abc123456789", "token=' 'abc123456789"],
  71. [String.raw`token=\ \abc123456789`, String.raw`token=\ \abc123456789`],
  72. ])("preserves credential boundaries: %s", (input, expected) => {
  73. expect(sanitizeMcpDiagnostic(input)).toBe(expected);
  74. });
  75. it("redacts quoted credentials and formats normalized diagnostics only once", () => {
  76. expect(sanitizeMcpDiagnostic("Bearer 'abc123456789'")).toBe("Bearer [redacted]'");
  77. expect(sanitizeMcpDiagnostic("token='abc123456789'")).toBe("token [redacted]'");
  78. const error = sanitizeMcpDiagnostic("x".repeat(199) + "😀more");
  79. const rendered = renderMcpSearchMiss({ pending: [], failed: [{ name: "demo", error }] });
  80. expect(rendered).toContain(`demo: "${"x".repeat(199)}…"`);
  81. expect(renderMcpStatus({ pending: ["token=abc123456789"], failed: [] }).join("\n")).toContain(
  82. "token=abc123456789",
  83. );
  84. });