official-plugin-runtime.test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. 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. 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("keeps invalid mcpServers structural errors fatal", async () => {
  44. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-runtime-invalid-"));
  45. cleanups.push(root);
  46. await writeJson(join(root, ".zcode-plugin", "plugin.json"), {
  47. mcpServers: "invalid",
  48. name: "browser-use",
  49. });
  50. process.argv[1] = join(root, "zcode.cjs");
  51. expect(() =>
  52. writeOfficialPluginRuntimeManifest({
  53. pluginName: "browser-use",
  54. rootPath: root,
  55. }),
  56. ).toThrow("Official plugin manifest has invalid mcpServers.");
  57. });
  58. });
  59. async function writeJson(path: string, value: unknown): Promise<void> {
  60. await import("node:fs/promises").then(({ mkdir }) => mkdir(dirname(path), { recursive: true }));
  61. await writeFile(path, `${JSON.stringify(value, null, 2)}\n`);
  62. }