mcp-failure-guidance-fixture.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { mkdir, readFile, writeFile } from "node:fs/promises";
  2. import { dirname, join, resolve } from "node:path";
  3. import { prepareWebRemoteControlSessionEventsFixture } from "#e2e/helpers/web-remote-control-session-events-fixture";
  4. export const MCP_DIAGNOSTIC_PYTHON = "e2e_python_dependency";
  5. export const MCP_DIAGNOSTIC_COMMAND = "e2e_missing_command";
  6. export const MCP_DIAGNOSTIC_NODE = "plugin:e2e-startup-diagnostics:node";
  7. export async function prepareMcpFailureGuidanceFixture(options: {
  8. specs: string[];
  9. credentialsFile: string;
  10. settingsFile: string;
  11. workspacePath: string;
  12. }): Promise<(() => Promise<void>) | null> {
  13. if (!options.specs.some((spec) => spec.endsWith("mcp-failure-guidance.test.ts"))) return null;
  14. const stopRemote = await prepareWebRemoteControlSessionEventsFixture({
  15. ...options,
  16. webUrl: "http://127.0.0.1:0/remote/v4",
  17. });
  18. try {
  19. const storageRoot = dirname(dirname(options.credentialsFile));
  20. const cliRoot = join(storageRoot, "cli");
  21. const marker = join(storageRoot, "mcp-diagnostic-recovered");
  22. const script = resolve("test/e2e/fixtures/mcp/startup-diagnostic-server.mjs");
  23. const pluginRoot = join(cliRoot, "e2e-fixtures", "e2e-startup-diagnostics");
  24. await mkdir(join(pluginRoot, ".zcode-plugin"), { recursive: true });
  25. await writeFile(
  26. join(pluginRoot, ".zcode-plugin", "plugin.json"),
  27. JSON.stringify({
  28. name: "e2e-startup-diagnostics",
  29. version: "0.0.0",
  30. mcpServers: {
  31. node: {
  32. type: "stdio",
  33. command: process.execPath,
  34. args: [script, "node"],
  35. protocolVersion: "legacy",
  36. },
  37. },
  38. }),
  39. );
  40. const pluginId = "e2e-startup-diagnostics@e2e-fixture";
  41. await mkdir(join(cliRoot, "plugins"), { recursive: true });
  42. await writeFile(
  43. join(cliRoot, "plugins", "installed_plugins.json"),
  44. JSON.stringify({
  45. version: 1,
  46. plugins: [
  47. {
  48. id: pluginId,
  49. name: "e2e-startup-diagnostics",
  50. installPath: pluginRoot,
  51. marketplace: "e2e-fixture",
  52. scope: "user",
  53. version: "0.0.0",
  54. installedAt: "2026-10-30T00:00:00.000Z",
  55. updatedAt: "2026-10-30T00:00:00.000Z",
  56. },
  57. ],
  58. }),
  59. );
  60. const configPath = join(cliRoot, "config.json");
  61. const config = JSON.parse(
  62. await readFile(configPath, "utf8").catch((error: NodeJS.ErrnoException) => {
  63. if (error.code !== "ENOENT") throw error;
  64. return "{}";
  65. }),
  66. );
  67. config.plugins = {
  68. ...config.plugins,
  69. enabledPlugins: { ...config.plugins?.enabledPlugins, [pluginId]: true },
  70. };
  71. config.mcp = {
  72. ...config.mcp,
  73. servers: {
  74. ...config.mcp?.servers,
  75. [MCP_DIAGNOSTIC_PYTHON]: {
  76. type: "stdio",
  77. command: process.execPath,
  78. args: [script, "python", marker],
  79. protocolVersion: "legacy",
  80. },
  81. [MCP_DIAGNOSTIC_COMMAND]: {
  82. type: "stdio",
  83. command: join(cliRoot, "missing-mcp-executable"),
  84. protocolVersion: "legacy",
  85. },
  86. },
  87. };
  88. await writeFile(configPath, JSON.stringify(config));
  89. return stopRemote;
  90. } catch (error) {
  91. await stopRemote();
  92. throw error;
  93. }
  94. }