Files
openclaw/extensions/qa-lab/src/node-exec.test.ts
2026-06-21 12:14:39 +02:00

80 lines
2.6 KiB
TypeScript

// Qa Lab tests cover node exec plugin behavior.
import path from "node:path";
import { describe, expect, it } from "vitest";
import { resolveQaNodeExecPath } from "./node-exec.js";
describe("resolveQaNodeExecPath", () => {
it("reuses the current exec path when already running under Node", async () => {
await expect(
resolveQaNodeExecPath({
execPath: "/opt/homebrew/bin/node",
platform: "darwin",
versions: { ...process.versions, bun: undefined },
}),
).resolves.toBe("/opt/homebrew/bin/node");
});
it("reuses nodejs as a valid current Node executable", async () => {
await expect(
resolveQaNodeExecPath({
execPath: "/usr/bin/nodejs",
platform: "linux",
versions: { ...process.versions, bun: undefined },
execFileImpl: async () => {
throw new Error("should not search PATH");
},
}),
).resolves.toBe("/usr/bin/nodejs");
});
it("resolves node from PATH when the parent runtime is bun", async () => {
await expect(
resolveQaNodeExecPath({
execPath: "/opt/homebrew/bin/bun",
platform: "darwin",
versions: { ...process.versions, bun: "1.2.3" },
execFileImpl: async () => ({
stdout: "/usr/local/bin/node\n",
stderr: "",
}),
}),
).resolves.toBe("/usr/local/bin/node");
});
it("uses trusted Windows where.exe when resolving node from PATH", async () => {
await expect(
resolveQaNodeExecPath({
execPath: String.raw`D:\Tools\bun.exe`,
platform: "win32",
versions: { ...process.versions, bun: "1.2.3" },
env: { SystemRoot: String.raw`D:\Windows` },
execFileImpl: async (file, args, options) => {
expect(file).toBe(path.win32.join(String.raw`D:\Windows`, "System32", "where.exe"));
expect(args).toEqual(["node"]);
expect(options).toEqual({
encoding: "utf8",
env: { SystemRoot: String.raw`D:\Windows` },
});
return {
stdout: String.raw`D:\nodejs\node.exe` + "\r\n",
stderr: "",
};
},
}),
).resolves.toBe(String.raw`D:\nodejs\node.exe`);
});
it("throws a clear error when node is unavailable", async () => {
await expect(
resolveQaNodeExecPath({
execPath: "/opt/homebrew/bin/bun",
platform: "darwin",
versions: { ...process.versions, bun: "1.2.3" },
execFileImpl: async () => {
throw new Error("missing");
},
}),
).rejects.toThrow("Node not found in PATH");
});
});