plugin-runtime-features.test.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { describe, expect, it } from "vitest";
  2. import type { PluginLoadOutcome, PluginMetadata } from "@zcode/contracts";
  3. import { resolvePluginRuntimeFeatures } from "../src/app/plugin-runtime-features.js";
  4. import { resolveBuiltInNodeReplMcpServers } from "../src/app/built-in-node-repl.js";
  5. function plugin(id: string, enabled: boolean, rootPath = "/tmp/plugin"): PluginMetadata {
  6. const [name = id, marketplace = "test"] = id.split("@");
  7. return {
  8. commandRootCount: 0,
  9. components: [],
  10. dataPath: "/tmp/plugin-data",
  11. declaredMcpServerNames: [],
  12. enabled,
  13. hookDetails: [],
  14. id,
  15. manifestPath: `${rootPath}/.zcode-plugin/plugin.json`,
  16. marketplace,
  17. mcpServerNames: [],
  18. name,
  19. rootPath,
  20. skillCount: 0,
  21. skillRootCount: 0,
  22. source: "official",
  23. };
  24. }
  25. function outcome(plugins: PluginMetadata[]): Pick<PluginLoadOutcome, "plugins"> {
  26. return { plugins };
  27. }
  28. describe("resolvePluginRuntimeFeatures", () => {
  29. it("enables browser-use without exposing the legacy core node_repl tools", () => {
  30. expect(
  31. resolvePluginRuntimeFeatures(outcome([plugin("browser-use@zcode-plugins-official", true)])),
  32. ).toEqual({
  33. browserUse: true,
  34. browserDocumentationRoot: "/tmp/plugin/docs",
  35. });
  36. });
  37. it("exposes only the stateless modern node_repl runtime, served from the host root", () => {
  38. const enabledServers = resolveBuiltInNodeReplMcpServers({
  39. pluginOutcome: outcome([
  40. plugin("node-repl-host@zcode-plugins-official", true, "/tmp/host"),
  41. plugin("browser-use@zcode-plugins-official", true),
  42. ]),
  43. workingDirectory: "/workspace",
  44. });
  45. expect(enabledServers.node_repl).toMatchObject({
  46. command: process.execPath,
  47. cwd: "/workspace",
  48. env: { ELECTRON_RUN_AS_NODE: "1", ZCODE_PLUGIN_ROOT: "/tmp/plugin" },
  49. timeoutMs: 600_000,
  50. isolation: "workspace",
  51. protocolVersion: "2026-07-28",
  52. // 内建来源决定进程意外退出后由 adapter 自动重启(docs/mcp-stateless-node-repl.md)。
  53. source: { kind: "builtin" },
  54. type: "stdio",
  55. });
  56. // 产物来自宿主自己的 root,不再来自 browser-use 的 root。
  57. expect(
  58. enabledServers.node_repl?.type === "stdio"
  59. ? enabledServers.node_repl.args?.at(-1)
  60. : undefined,
  61. ).toBe("/tmp/host/dist/mcp/server.js");
  62. expect(enabledServers.browser_use).toBeUndefined();
  63. });
  64. it("does not register node_repl when the shared host is missing", () => {
  65. // 宿主缺失只能安全地不注册:没有宿主可跑,绝不回退到某个插件包里的旧产物。
  66. expect(
  67. resolveBuiltInNodeReplMcpServers({
  68. pluginOutcome: outcome([plugin("browser-use@zcode-plugins-official", true)]),
  69. workingDirectory: "/workspace",
  70. }),
  71. ).toEqual({});
  72. });
  73. it("serves Computer Use without the Browser Use package", () => {
  74. // 回归(2026-09-11):宿主产物过去只从 browser-use 的 rootPath 取,browser-use 包缺失时
  75. // 即便 CUA 启用也拿不到宿主。三个包独立后两个能力对称。
  76. const servers = resolveBuiltInNodeReplMcpServers({
  77. pluginOutcome: outcome([
  78. plugin("node-repl-host@zcode-plugins-official", true, "/tmp/host"),
  79. plugin("computer-use@zcode-plugins-official", true, "/tmp/cua-plugin"),
  80. ]),
  81. workingDirectory: "/workspace",
  82. });
  83. expect(servers.node_repl).toMatchObject({
  84. env: { ZCODE_CUA_PLUGIN_ROOT: "/tmp/cua-plugin" },
  85. type: "stdio",
  86. });
  87. // browser-use 不在时不注入它的 root,宿主据此知道浏览器那半边不可用。
  88. expect(
  89. servers.node_repl?.type === "stdio" ? servers.node_repl.env?.ZCODE_PLUGIN_ROOT : undefined,
  90. ).toBeUndefined();
  91. });
  92. it("enables the CUA SDK without registering core node_repl tools", () => {
  93. expect(
  94. resolvePluginRuntimeFeatures(outcome([plugin("computer-use@zcode-plugins-official", true)])),
  95. ).toEqual({ computerUse: true });
  96. });
  97. // 回归:browser-use 关闭后 node_repl 仍然被注册,宿主照样拉起 stdio 子进程,
  98. // mcp__node_repl__js 等 3 个高权限工具照样进模型工具池(线上日志实证:
  99. // runtimeFeatureBrowserUse=false 且用户 MCP 配置为 0 时,mcp.startup 仍 serverCount=1 / toolCount=3)。
  100. // 根因是这里只判断「browser-use 包存在」,漏掉了 plugin.enabled;
  101. // 而同一份 pluginOutcome 在 resolvePluginRuntimeFeatures 里是判断 enabled 的,两条路径语义分叉。
  102. it("does not register node_repl when the browser-use plugin is disabled", () => {
  103. expect(
  104. resolveBuiltInNodeReplMcpServers({
  105. pluginOutcome: outcome([
  106. plugin("node-repl-host@zcode-plugins-official", true, "/tmp/host"),
  107. plugin("browser-use@zcode-plugins-official", false),
  108. ]),
  109. workingDirectory: "/workspace",
  110. }),
  111. ).toEqual({});
  112. });
  113. it("keeps the shared host and CUA runtime roots separate", () => {
  114. const servers = resolveBuiltInNodeReplMcpServers({
  115. pluginOutcome: outcome([
  116. plugin("node-repl-host@zcode-plugins-official", true, "/tmp/host"),
  117. plugin("browser-use@zcode-plugins-official", false, "/tmp/browser-plugin"),
  118. plugin("computer-use@zcode-plugins-official", true, "/tmp/cua-plugin"),
  119. ]),
  120. workingDirectory: "/workspace",
  121. });
  122. // browser-use 已停用:只注入 CUA 的 root,两个领域各自独立。
  123. expect(servers.node_repl).toMatchObject({
  124. env: { ZCODE_CUA_PLUGIN_ROOT: "/tmp/cua-plugin" },
  125. type: "stdio",
  126. });
  127. expect(
  128. servers.node_repl?.type === "stdio" ? servers.node_repl.env?.ZCODE_PLUGIN_ROOT : undefined,
  129. ).toBeUndefined();
  130. });
  131. it("does not register node_repl when the browser-use plugin is absent", () => {
  132. expect(
  133. resolveBuiltInNodeReplMcpServers({
  134. pluginOutcome: outcome([plugin("browser@third-party", true)]),
  135. workingDirectory: "/workspace",
  136. }),
  137. ).toEqual({});
  138. });
  139. it("keeps browser-use disabled for disabled or non-official plugins", () => {
  140. expect(
  141. resolvePluginRuntimeFeatures(outcome([plugin("browser-use@zcode-plugins-official", false)])),
  142. ).toEqual({});
  143. expect(resolvePluginRuntimeFeatures(outcome([plugin("browser@third-party", true)]))).toEqual(
  144. {},
  145. );
  146. });
  147. });