plugin-runtime-features.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. type: "stdio",
  53. });
  54. // 产物来自宿主自己的 root,不再来自 browser-use 的 root。
  55. expect(
  56. enabledServers.node_repl?.type === "stdio"
  57. ? enabledServers.node_repl.args?.at(-1)
  58. : undefined,
  59. ).toBe("/tmp/host/dist/mcp/server.js");
  60. expect(enabledServers.browser_use).toBeUndefined();
  61. });
  62. it("does not register node_repl when the shared host is missing", () => {
  63. // 宿主缺失只能安全地不注册:没有宿主可跑,绝不回退到某个插件包里的旧产物。
  64. expect(
  65. resolveBuiltInNodeReplMcpServers({
  66. pluginOutcome: outcome([plugin("browser-use@zcode-plugins-official", true)]),
  67. workingDirectory: "/workspace",
  68. }),
  69. ).toEqual({});
  70. });
  71. it("serves Computer Use without the Browser Use package", () => {
  72. // 回归(2026-09-11):宿主产物过去只从 browser-use 的 rootPath 取,browser-use 包缺失时
  73. // 即便 CUA 启用也拿不到宿主。三个包独立后两个能力对称。
  74. const servers = resolveBuiltInNodeReplMcpServers({
  75. pluginOutcome: outcome([
  76. plugin("node-repl-host@zcode-plugins-official", true, "/tmp/host"),
  77. plugin("computer-use@zcode-plugins-official", true, "/tmp/cua-plugin"),
  78. ]),
  79. workingDirectory: "/workspace",
  80. });
  81. expect(servers.node_repl).toMatchObject({
  82. env: { ZCODE_CUA_PLUGIN_ROOT: "/tmp/cua-plugin" },
  83. type: "stdio",
  84. });
  85. // browser-use 不在时不注入它的 root,宿主据此知道浏览器那半边不可用。
  86. expect(
  87. servers.node_repl?.type === "stdio" ? servers.node_repl.env?.ZCODE_PLUGIN_ROOT : undefined,
  88. ).toBeUndefined();
  89. });
  90. it("enables the CUA SDK without registering core node_repl tools", () => {
  91. expect(
  92. resolvePluginRuntimeFeatures(outcome([plugin("computer-use@zcode-plugins-official", true)])),
  93. ).toEqual({ computerUse: true });
  94. });
  95. // 回归:browser-use 关闭后 node_repl 仍然被注册,宿主照样拉起 stdio 子进程,
  96. // mcp__node_repl__js 等 3 个高权限工具照样进模型工具池(线上日志实证:
  97. // runtimeFeatureBrowserUse=false 且用户 MCP 配置为 0 时,mcp.startup 仍 serverCount=1 / toolCount=3)。
  98. // 根因是这里只判断「browser-use 包存在」,漏掉了 plugin.enabled;
  99. // 而同一份 pluginOutcome 在 resolvePluginRuntimeFeatures 里是判断 enabled 的,两条路径语义分叉。
  100. it("does not register node_repl when the browser-use plugin is disabled", () => {
  101. expect(
  102. resolveBuiltInNodeReplMcpServers({
  103. pluginOutcome: outcome([
  104. plugin("node-repl-host@zcode-plugins-official", true, "/tmp/host"),
  105. plugin("browser-use@zcode-plugins-official", false),
  106. ]),
  107. workingDirectory: "/workspace",
  108. }),
  109. ).toEqual({});
  110. });
  111. it("keeps the shared host and CUA runtime roots separate", () => {
  112. const servers = resolveBuiltInNodeReplMcpServers({
  113. pluginOutcome: outcome([
  114. plugin("node-repl-host@zcode-plugins-official", true, "/tmp/host"),
  115. plugin("browser-use@zcode-plugins-official", false, "/tmp/browser-plugin"),
  116. plugin("computer-use@zcode-plugins-official", true, "/tmp/cua-plugin"),
  117. ]),
  118. workingDirectory: "/workspace",
  119. });
  120. // browser-use 已停用:只注入 CUA 的 root,两个领域各自独立。
  121. expect(servers.node_repl).toMatchObject({
  122. env: { ZCODE_CUA_PLUGIN_ROOT: "/tmp/cua-plugin" },
  123. type: "stdio",
  124. });
  125. expect(
  126. servers.node_repl?.type === "stdio" ? servers.node_repl.env?.ZCODE_PLUGIN_ROOT : undefined,
  127. ).toBeUndefined();
  128. });
  129. it("does not register node_repl when the browser-use plugin is absent", () => {
  130. expect(
  131. resolveBuiltInNodeReplMcpServers({
  132. pluginOutcome: outcome([plugin("browser@third-party", true)]),
  133. workingDirectory: "/workspace",
  134. }),
  135. ).toEqual({});
  136. });
  137. it("keeps browser-use disabled for disabled or non-official plugins", () => {
  138. expect(
  139. resolvePluginRuntimeFeatures(outcome([plugin("browser-use@zcode-plugins-official", false)])),
  140. ).toEqual({});
  141. expect(resolvePluginRuntimeFeatures(outcome([plugin("browser@third-party", true)]))).toEqual(
  142. {},
  143. );
  144. });
  145. });