prepareAgentNodeBundle.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { execFile } from "node:child_process";
  2. import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join, resolve } from "node:path";
  5. import { promisify } from "node:util";
  6. import { expect, it } from "vitest";
  7. const execFileAsync = promisify(execFile);
  8. const scriptPath = resolve(import.meta.dirname, "../scripts/prepare-agent-node-bundle.mjs");
  9. it.each([
  10. { label: "有 tsconfig", hasTsconfig: true, runtimeBuildScript: "scripts/build-mcp.mjs" },
  11. {
  12. label: "无 tsconfig",
  13. hasTsconfig: false,
  14. runtimeBuildScript: "../office-runtime/scripts/build.mjs",
  15. },
  16. ])(
  17. "bootstrap 官方插件构建先清理旧 dist,缺少 dist 时也能继续构建($label)",
  18. async ({ hasTsconfig, runtimeBuildScript }) => {
  19. const source = (await readFile(scriptPath, "utf8")).replace(/\r\n/g, "\n");
  20. // 修复原因:只断言源码包含 rmSync 调用会漏掉缺少导入的 ReferenceError。
  21. // 保留脚本的真实 Node 导入与函数,在隔离目录执行清理,只拦截昂贵的构建子命令。
  22. const imports = source.match(/^import .+ from "node:[^"]+";$/gm)?.join("\n");
  23. const buildFunction = source.match(
  24. /function buildOfficialPluginRuntimeForBootstrap\(plugin\) \{[\s\S]*?\n\}(?=\n\nfunction)/u,
  25. )?.[0];
  26. expect(imports).toBeDefined();
  27. expect(buildFunction).toBeDefined();
  28. const repoRoot = await mkdtemp(join(tmpdir(), "agent-plugin-bootstrap-"));
  29. const pluginRoot = join(repoRoot, "plugin");
  30. const distPath = join(pluginRoot, "dist");
  31. try {
  32. await mkdir(distPath, { recursive: true });
  33. await writeFile(join(distPath, "stale.js"), "old runtime");
  34. // 缺陷原因:旧夹具未创建 tsconfig,却要求执行 tsc;分别覆盖编译型插件和共享 runtime 插件。
  35. if (hasTsconfig) await writeFile(join(pluginRoot, "tsconfig.json"), "{}");
  36. const probe = `
  37. ${imports}
  38. const repoRoot = ${JSON.stringify(repoRoot)};
  39. const calls = [];
  40. function runCommand(command, args, options) {
  41. if (existsSync(resolve(options.cwd, "dist"))) {
  42. throw new Error("旧 dist 必须在构建前清理");
  43. }
  44. calls.push({ command, args, cwd: options.cwd });
  45. }
  46. ${buildFunction}
  47. const plugin = { relativePath: "plugin", runtimeBuildScript: ${JSON.stringify(runtimeBuildScript)} };
  48. buildOfficialPluginRuntimeForBootstrap(plugin);
  49. buildOfficialPluginRuntimeForBootstrap(plugin);
  50. process.stdout.write(JSON.stringify(calls));
  51. `;
  52. const { stdout } = await execFileAsync(process.execPath, [
  53. "--input-type=module",
  54. "--eval",
  55. probe,
  56. ]);
  57. const expectedBuild = [
  58. ...(hasTsconfig
  59. ? [
  60. {
  61. command: process.execPath,
  62. args: ["../../node_modules/typescript/bin/tsc"],
  63. cwd: pluginRoot,
  64. },
  65. ]
  66. : []),
  67. {
  68. command: process.execPath,
  69. args: [runtimeBuildScript],
  70. cwd: pluginRoot,
  71. },
  72. ];
  73. expect(JSON.parse(stdout)).toEqual([...expectedBuild, ...expectedBuild]);
  74. } finally {
  75. await rm(repoRoot, { recursive: true, force: true });
  76. }
  77. },
  78. );