diff --git a/src/node-host/invoke.test.ts b/src/node-host/invoke.test.ts index 66bff97e3179..7343aa7a0f89 100644 --- a/src/node-host/invoke.test.ts +++ b/src/node-host/invoke.test.ts @@ -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().mockResolvedValue(null); - const skillBins: SkillBinsProvider = { current: async () => [] }; + await withEnvAsync( + { PATH: `${tempDir}${path.delimiter}${process.env.PATH ?? ""}` }, + async () => { + const request = vi.fn().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 }); } },