official-plugin-runtime.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { dirname, join } from "node:path";
  4. import { setTimeout as delay } from "node:timers/promises";
  5. import { afterEach, describe, expect, it } from "vitest";
  6. import { writeOfficialPluginRuntimeManifest } from "../src/app/official-plugin-runtime.js";
  7. describe("official plugin runtime manifest", () => {
  8. const cleanups: string[] = [];
  9. const originalEntrypoint = process.argv[1];
  10. afterEach(async () => {
  11. process.argv[1] = originalEntrypoint;
  12. await Promise.all(cleanups.splice(0).map((root) => rm(root, { force: true, recursive: true })));
  13. });
  14. it("does not rewrite an unchanged runtime manifest", async () => {
  15. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-runtime-manifest-"));
  16. cleanups.push(root);
  17. const manifestPath = join(root, ".zcode-plugin", "plugin.json");
  18. await writeJson(manifestPath, {
  19. mcpServers: {
  20. "browser-use": {
  21. args: [],
  22. command: "old-node",
  23. },
  24. },
  25. name: "browser-use",
  26. });
  27. process.argv[1] = join(root, "zcode.cjs");
  28. await writeOfficialPluginRuntimeManifest({
  29. pluginName: "browser-use",
  30. rootPath: root,
  31. });
  32. const expectedContents = await readFile(manifestPath, "utf8");
  33. const firstStat = await stat(manifestPath);
  34. await delay(30);
  35. await writeOfficialPluginRuntimeManifest({
  36. pluginName: "browser-use",
  37. rootPath: root,
  38. });
  39. const secondStat = await stat(manifestPath);
  40. expect(await readFile(manifestPath, "utf8")).toBe(expectedContents);
  41. expect(secondStat.mtimeMs).toBe(firstStat.mtimeMs);
  42. });
  43. it("repairs cached entries from the seed and leaves HTTP servers untouched", async () => {
  44. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-entry-"));
  45. cleanups.push(root);
  46. const manifestPath = join(root, ".zcode-plugin", "plugin.json");
  47. await writeJson(manifestPath, { mcpServers: { pdf: { command: "node", args: ["wrong.js"] } } });
  48. process.argv[1] = join(root, "zcode.cjs");
  49. const remote = { type: "http", url: "https://example.com/mcp" };
  50. const sourceManifestContents = JSON.stringify({
  51. mcpServers: {
  52. pdf: { command: "node", args: ["${ZCODE_PLUGIN_ROOT}/dist/server.mjs", "--flag"] },
  53. search: remote,
  54. },
  55. });
  56. const input = { pluginName: "pdf", rootPath: root, sourceManifestContents };
  57. await writeOfficialPluginRuntimeManifest(input);
  58. const contents = await readFile(manifestPath, "utf8");
  59. const manifest = JSON.parse(contents);
  60. expect(manifest.mcpServers.pdf.args.slice(-2)).toEqual([
  61. join(root, "dist/server.mjs"),
  62. "--flag",
  63. ]);
  64. expect(manifest.mcpServers.pdf.env.ELECTRON_RUN_AS_NODE).toBe("1");
  65. expect(manifest.mcpServers.search).toEqual(remote);
  66. await writeOfficialPluginRuntimeManifest(input);
  67. expect(await readFile(manifestPath, "utf8")).toBe(contents);
  68. });
  69. it("keeps invalid mcpServers structural errors fatal", async () => {
  70. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-runtime-invalid-"));
  71. cleanups.push(root);
  72. await writeJson(join(root, ".zcode-plugin", "plugin.json"), {
  73. mcpServers: "invalid",
  74. name: "browser-use",
  75. });
  76. process.argv[1] = join(root, "zcode.cjs");
  77. await expect(
  78. (async () =>
  79. await writeOfficialPluginRuntimeManifest({
  80. pluginName: "browser-use",
  81. rootPath: root,
  82. }))(),
  83. ).rejects.toThrow("Official plugin manifest has invalid mcpServers.");
  84. });
  85. });
  86. async function writeJson(path: string, value: unknown): Promise<void> {
  87. await import("node:fs/promises").then(({ mkdir }) => mkdir(dirname(path), { recursive: true }));
  88. await writeFile(path, `${JSON.stringify(value, null, 2)}\n`);
  89. }