mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-06 05:51:15 +08:00
test: scope invoke prepare path env
This commit is contained in:
@@ -4,6 +4,7 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { GatewayClient } from "../gateway/client.js";
|
||||
import { withEnvAsync } from "../test-utils/env.js";
|
||||
import type { SkillBinsProvider } from "./invoke-types.js";
|
||||
import { handleInvoke } from "./invoke.js";
|
||||
|
||||
@@ -49,46 +50,44 @@ describe("node host invoke", () => {
|
||||
const toolPath = path.join(tempDir, "tool");
|
||||
fs.writeFileSync(toolPath, "#!/bin/sh\nexit 0\n");
|
||||
fs.chmodSync(toolPath, 0o755);
|
||||
const previousPath = process.env.PATH;
|
||||
process.env.PATH = `${tempDir}${path.delimiter}${previousPath ?? ""}`;
|
||||
|
||||
try {
|
||||
const request = vi.fn<GatewayClient["request"]>().mockResolvedValue(null);
|
||||
const skillBins: SkillBinsProvider = { current: async () => [] };
|
||||
await withEnvAsync(
|
||||
{ PATH: `${tempDir}${path.delimiter}${process.env.PATH ?? ""}` },
|
||||
async () => {
|
||||
const request = vi.fn<GatewayClient["request"]>().mockResolvedValue(null);
|
||||
const skillBins: SkillBinsProvider = { current: async () => [] };
|
||||
|
||||
await handleInvoke(
|
||||
{
|
||||
id: "invoke-prepare-env",
|
||||
nodeId: "node-1",
|
||||
command: "system.run.prepare",
|
||||
paramsJSON: JSON.stringify({
|
||||
command: ["tool", "--version"],
|
||||
rawCommand: "tool --version",
|
||||
env: { PATH: "/tmp/mismatch" },
|
||||
}),
|
||||
await handleInvoke(
|
||||
{
|
||||
id: "invoke-prepare-env",
|
||||
nodeId: "node-1",
|
||||
command: "system.run.prepare",
|
||||
paramsJSON: JSON.stringify({
|
||||
command: ["tool", "--version"],
|
||||
rawCommand: "tool --version",
|
||||
env: { PATH: "/tmp/mismatch" },
|
||||
}),
|
||||
},
|
||||
{ request } as unknown as GatewayClient,
|
||||
skillBins,
|
||||
);
|
||||
|
||||
expect(request).toHaveBeenCalledWith(
|
||||
"node.invoke.result",
|
||||
expect.objectContaining({
|
||||
id: "invoke-prepare-env",
|
||||
nodeId: "node-1",
|
||||
ok: false,
|
||||
error: expect.objectContaining({
|
||||
code: "INVALID_REQUEST",
|
||||
message: expect.stringContaining("blocked override keys: PATH"),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
},
|
||||
{ request } as unknown as GatewayClient,
|
||||
skillBins,
|
||||
);
|
||||
|
||||
expect(request).toHaveBeenCalledWith(
|
||||
"node.invoke.result",
|
||||
expect.objectContaining({
|
||||
id: "invoke-prepare-env",
|
||||
nodeId: "node-1",
|
||||
ok: false,
|
||||
error: expect.objectContaining({
|
||||
code: "INVALID_REQUEST",
|
||||
message: expect.stringContaining("blocked override keys: PATH"),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
} finally {
|
||||
if (previousPath === undefined) {
|
||||
delete process.env.PATH;
|
||||
} else {
|
||||
process.env.PATH = previousPath;
|
||||
}
|
||||
fs.rmSync(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user