mcp-node-fallback.test.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, it } from "vitest";
  5. import {
  6. bundledNpmRootCandidates,
  7. resolveMcpNodeFallbackProvider,
  8. } from "../src/app/mcp-node-fallback.js";
  9. const tempRoots: string[] = [];
  10. afterEach(async () => {
  11. await Promise.all(tempRoots.splice(0).map((root) => rm(root, { force: true, recursive: true })));
  12. });
  13. async function writeNpmPackage(npmRoot: string): Promise<void> {
  14. await mkdir(join(npmRoot, "bin"), { recursive: true });
  15. await writeFile(join(npmRoot, "bin", "npm-cli.js"), "");
  16. await writeFile(join(npmRoot, "bin", "npx-cli.js"), "");
  17. }
  18. describe("bundledNpmRootCandidates", () => {
  19. it("looks next to the entry file first, then next to the bundle, without duplicates", () => {
  20. expect(
  21. bundledNpmRootCandidates(["/app/resources/glm", "/app/other", "/app/resources/glm"]),
  22. ).toEqual([
  23. join("/app/resources/glm", "node-runtime", "npm"),
  24. join("/app/other", "node-runtime", "npm"),
  25. ]);
  26. });
  27. });
  28. describe("resolveMcpNodeFallbackProvider", () => {
  29. it("returns no provider when the agent does not run on Electron Node", () => {
  30. expect(
  31. resolveMcpNodeFallbackProvider({
  32. cliStorageRoot: "/storage/cli",
  33. runtime: { baseDirs: ["/app"], electronVersion: undefined, execPath: "/usr/bin/node" },
  34. }),
  35. ).toBeUndefined();
  36. });
  37. it("uses the Electron executable and the staged npm next to the agent bundle", async () => {
  38. const root = await mkdtemp(join(tmpdir(), "zcode-bootstrap-node-fallback-"));
  39. tempRoots.push(root);
  40. const glmDir = join(root, "resources", "glm");
  41. await writeNpmPackage(join(glmDir, "node-runtime", "npm"));
  42. const provider = resolveMcpNodeFallbackProvider({
  43. cliStorageRoot: join(root, "cli"),
  44. runtime: {
  45. baseDirs: [join(root, "missing"), glmDir],
  46. electronVersion: "44.7.0",
  47. execPath: process.execPath,
  48. },
  49. });
  50. await expect(provider?.resolve()).resolves.toEqual({
  51. binDir: join(root, "cli", "node-fallback", "v1"),
  52. env: {
  53. ZCODE_NODE_FALLBACK_EXEC_PATH: process.execPath,
  54. ZCODE_NODE_FALLBACK_NPM_ROOT: join(glmDir, "node-runtime", "npm"),
  55. },
  56. });
  57. });
  58. });