bundle-boundary.test.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { resolve } from "node:path";
  2. import { fileURLToPath } from "node:url";
  3. import { build } from "esbuild";
  4. import { describe, expect, it } from "vitest";
  5. const packageRoot = fileURLToPath(new URL("..", import.meta.url));
  6. function normalizePath(value: string): string {
  7. return value.replaceAll("\\", "/");
  8. }
  9. // 这份依赖边界守卫原先长在 browser-use-plugin/test/bundle-boundary.test.ts 上(entry 是
  10. // browser-use 的 src/mcp/server.ts)。node_repl 宿主抽成本包后 entry 随之搬家,守卫必须跟着
  11. // 搬:留在 browser-use 侧会指向已删除的 src/mcp/,而本包才是真正被 plugin host import 的产物。
  12. // 相对路径深度不变(两个包同为 apps/zcode-cli/packages/*),因此下面的 ../contracts、
  13. // ../../../../packages/shared 等前缀原样有效。
  14. describe("node_repl MCP bundle boundary", () => {
  15. it("只依赖 core 的 REPL 引擎,不带入 Agent、工具或工作流编译器", async () => {
  16. const result = await build({
  17. absWorkingDir: packageRoot,
  18. bundle: true,
  19. entryPoints: ["src/server.ts"],
  20. format: "esm",
  21. legalComments: "none",
  22. metafile: true,
  23. platform: "node",
  24. target: "node24",
  25. write: false,
  26. });
  27. const inputs = Object.keys(result.metafile.inputs).map((path) =>
  28. normalizePath(resolve(packageRoot, path)),
  29. );
  30. const coreRoot = `${normalizePath(resolve(packageRoot, "../core/dist"))}/`;
  31. const coreInputs = inputs.filter((path) => path.startsWith(coreRoot));
  32. expect(coreInputs.length).toBeGreaterThan(0);
  33. // Bug 原因:仅从总入口取 NodeReplSession,也会因顶层副作用带入整套业务依赖。
  34. // 检查依赖边界而非固定包体积,允许 REPL 自身增加实现文件。
  35. expect(coreInputs.find((path) => !path.startsWith(`${coreRoot}repl/`))).toBeUndefined();
  36. expect(
  37. inputs.find((path) => /(?:^|\/)(?:dynamic-workflow|typescript)\//u.test(path)),
  38. ).toBeUndefined();
  39. const contractRoot = `${normalizePath(resolve(packageRoot, "../contracts/dist"))}/`;
  40. const contractInputs = inputs
  41. .filter((path) => path.startsWith(contractRoot))
  42. .map((path) => path.slice(contractRoot.length));
  43. expect(contractInputs.sort()).toEqual([
  44. "interfaces/mcp.port.js",
  45. "tools/json-schema.js",
  46. "tools/node-repl.js",
  47. ]);
  48. const sharedRoot = `${normalizePath(resolve(packageRoot, "../../../../packages/shared/src"))}/`;
  49. const sharedInputs = inputs.filter((path) => path.startsWith(sharedRoot));
  50. expect(sharedInputs.length).toBeGreaterThan(0);
  51. // 同一个总入口即使只取常量,也会初始化其它领域的 schema;Worker 会重复承担这份开销。
  52. expect(
  53. sharedInputs.find((path) => !path.startsWith(`${sharedRoot}browser-use/`)),
  54. ).toBeUndefined();
  55. });
  56. });