plugin-load-isolation.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { expect, it, vi } from "vitest";
  5. import { createNodePluginAdapter } from "../src/plugins/index.js";
  6. const BROKEN_PLUGIN_NAME = "broken-plugin";
  7. // manifest 校验已把已知的坏字段(如 userConfig 项不是对象)拦在发现入口;这里模拟组件准备阶段
  8. // 仍然抛错的情况。修复前异常会中断整个启动加载,一个坏插件让会话无法创建。
  9. vi.mock("../src/plugins/ui-surfaces.js", async (importOriginal) => {
  10. const actual = await importOriginal<typeof import("../src/plugins/ui-surfaces.js")>();
  11. return {
  12. ...actual,
  13. parsePluginUiSurfaces: (input: Parameters<typeof actual.parsePluginUiSurfaces>[0]) => {
  14. if (input.loaded.manifest.name === BROKEN_PLUGIN_NAME)
  15. throw new Error("component preparation failed");
  16. return actual.parsePluginUiSurfaces(input);
  17. },
  18. };
  19. });
  20. it("isolates a plugin whose components cannot be prepared", async () => {
  21. const root = await mkdtemp(join(tmpdir(), "zcode-plugin-isolation-"));
  22. const good = join(root, "good");
  23. const broken = join(root, "broken");
  24. try {
  25. for (const [pluginRoot, manifest] of [
  26. [good, { name: "good-plugin", mcpServers: { demo: { command: "node" } } }],
  27. [broken, { name: BROKEN_PLUGIN_NAME, mcpServers: { broken: { command: "must-not-start" } } }],
  28. ] as const) {
  29. await mkdir(join(pluginRoot, ".zcode-plugin"), { recursive: true });
  30. await writeFile(join(pluginRoot, ".zcode-plugin", "plugin.json"), JSON.stringify(manifest));
  31. }
  32. const storageRoot = join(root, "storage");
  33. const outcome = await createNodePluginAdapter({ storageRoot }).discoverPlugins({
  34. config: { dirs: [broken, good], enabled: true, enabledPlugins: {}, options: {} },
  35. env: {},
  36. storageRoot,
  37. workingDirectory: root,
  38. });
  39. expect(Object.keys(outcome.mcpServers)).toEqual(["plugin:good-plugin:demo"]);
  40. expect(outcome.plugins.map((plugin) => plugin.id)).toEqual(["good-plugin@inline"]);
  41. expect(outcome.diagnostics).toContainEqual(
  42. expect.objectContaining({ code: "plugin_load_failed", pluginId: "broken-plugin@inline" }),
  43. );
  44. } finally {
  45. await rm(root, { recursive: true, force: true });
  46. }
  47. });