From 742fc47be27540fd0f19c80e67d3afcd9a08b474 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 12 Apr 2026 09:57:34 +0100 Subject: [PATCH] test(agents): share sandbox spawn fixture helpers --- src/agents/sandbox/ssh.spawn-env.test.ts | 65 +++++++----------------- 1 file changed, 18 insertions(+), 47 deletions(-) diff --git a/src/agents/sandbox/ssh.spawn-env.test.ts b/src/agents/sandbox/ssh.spawn-env.test.ts index 17837fa55d0..eeee82bec96 100644 --- a/src/agents/sandbox/ssh.spawn-env.test.ts +++ b/src/agents/sandbox/ssh.spawn-env.test.ts @@ -32,6 +32,21 @@ vi.mock("node:child_process", async () => { }; }); +function mockSuccessfulSpawnCalls(times = 1) { + let chain = spawnMock; + for (let i = 0; i < times; i += 1) { + chain = chain.mockImplementationOnce( + (_command: string, _args: readonly string[], _options: SpawnOptions): ChildProcess => { + const child = createMockChildProcess(); + process.nextTick(() => { + child.emit("close", 0); + }); + return child as unknown as ChildProcess; + }, + ); + } +} + let runSshSandboxCommand: typeof import("./ssh.js").runSshSandboxCommand; let uploadDirectoryToSshTarget: typeof import("./ssh.js").uploadDirectoryToSshTarget; @@ -60,15 +75,7 @@ describe("ssh subprocess env sanitization", () => { }); it("filters blocked secrets before spawning ssh commands", async () => { - spawnMock.mockImplementationOnce( - (_command: string, _args: readonly string[], _options: SpawnOptions): ChildProcess => { - const child = createMockChildProcess(); - process.nextTick(() => { - child.emit("close", 0); - }); - return child as unknown as ChildProcess; - }, - ); + mockSuccessfulSpawnCalls(); process.env.OPENAI_API_KEY = "sk-test-secret"; process.env.LANG = "en_US.UTF-8"; @@ -89,25 +96,7 @@ describe("ssh subprocess env sanitization", () => { }); it("filters blocked secrets before spawning ssh uploads", async () => { - spawnMock - .mockImplementationOnce( - (_command: string, _args: readonly string[], _options: SpawnOptions): ChildProcess => { - const child = createMockChildProcess(); - process.nextTick(() => { - child.emit("close", 0); - }); - return child as unknown as ChildProcess; - }, - ) - .mockImplementationOnce( - (_command: string, _args: readonly string[], _options: SpawnOptions): ChildProcess => { - const child = createMockChildProcess(); - process.nextTick(() => { - child.emit("close", 0); - }); - return child as unknown as ChildProcess; - }, - ); + mockSuccessfulSpawnCalls(2); process.env.ANTHROPIC_API_KEY = "sk-test-secret"; process.env.NODE_ENV = "test"; @@ -133,25 +122,7 @@ describe("ssh subprocess env sanitization", () => { it.runIf(process.platform !== "win32")( "allows in-workspace symlinks to upload normally", async () => { - spawnMock - .mockImplementationOnce( - (_command: string, _args: readonly string[], _options: SpawnOptions): ChildProcess => { - const child = createMockChildProcess(); - process.nextTick(() => { - child.emit("close", 0); - }); - return child as unknown as ChildProcess; - }, - ) - .mockImplementationOnce( - (_command: string, _args: readonly string[], _options: SpawnOptions): ChildProcess => { - const child = createMockChildProcess(); - process.nextTick(() => { - child.emit("close", 0); - }); - return child as unknown as ChildProcess; - }, - ); + mockSuccessfulSpawnCalls(2); const localDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ssh-upload-safe-")); tempDirs.push(localDir);