plugin-mcp-isolation.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { ZCODE_OFFICIAL_PLUGIN_MARKETPLACE } from "@zcode/contracts";
  5. import { describe, expect, it } from "vitest";
  6. import { createNodePluginAdapter } from "../src/plugins/index.js";
  7. const PLUGIN_NAME = "isolation-plugin";
  8. const SERVERS = {
  9. shared: { type: "stdio", command: "node", isolation: "workspace" },
  10. perSession: { type: "stdio", command: "node", isolation: "session" },
  11. invalid: { type: "stdio", command: "node", isolation: "machine" },
  12. omitted: { type: "stdio", command: "node" },
  13. };
  14. // 只有官方市场插件能声明工作区共享 server(文档插件查看面板 spec §8);
  15. // 其他插件的声明被忽略,保持 session isolation。
  16. describe.each([
  17. {
  18. source: "official",
  19. expected: {
  20. shared: "workspace",
  21. perSession: "session",
  22. invalid: undefined,
  23. omitted: undefined,
  24. },
  25. },
  26. {
  27. source: "inline",
  28. expected: { shared: undefined, perSession: undefined, invalid: undefined, omitted: undefined },
  29. },
  30. ] as const)("plugin MCP isolation from an $source plugin", ({ source, expected }) => {
  31. it("passes isolation through only for official plugins", async () => {
  32. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-isolation-"));
  33. const pluginRoot = join(root, PLUGIN_NAME);
  34. try {
  35. await mkdir(join(pluginRoot, ".zcode-plugin"), { recursive: true });
  36. await writeFile(
  37. join(pluginRoot, ".zcode-plugin", "plugin.json"),
  38. JSON.stringify({ name: PLUGIN_NAME, mcpServers: SERVERS }),
  39. );
  40. const storageRoot = join(root, "storage");
  41. const official = source === "official";
  42. const outcome = await createNodePluginAdapter({ storageRoot }).discoverPlugins({
  43. config: {
  44. dirs: official ? [] : [pluginRoot],
  45. enabled: true,
  46. enabledPlugins: official
  47. ? { [`${PLUGIN_NAME}@${ZCODE_OFFICIAL_PLUGIN_MARKETPLACE}`]: true }
  48. : {},
  49. options: {},
  50. suppressedBuiltins: [],
  51. },
  52. env: {},
  53. ...(official ? { officialPluginRoots: [pluginRoot] } : {}),
  54. storageRoot,
  55. workingDirectory: root,
  56. });
  57. expect(outcome.diagnostics).toEqual([]);
  58. for (const [name, isolation] of Object.entries(expected)) {
  59. const config = outcome.mcpServers[`plugin:${PLUGIN_NAME}:${name}`];
  60. expect(config, name).toBeDefined();
  61. expect(config?.isolation, name).toBe(isolation);
  62. }
  63. } finally {
  64. await rm(root, { recursive: true, force: true });
  65. }
  66. });
  67. });