|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import bash from "@/agent/tools/definitions/bash"; |
| 3 | +import { ToolError } from "@/agent/errors"; |
| 4 | +import { ChildProcess, spawn } from "child_process"; |
| 5 | +import { EventEmitter } from "events"; |
| 6 | + |
| 7 | +vi.mock("child_process", () => ({ |
| 8 | + spawn: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +describe("bash tool", () => { |
| 12 | + let spawnMock: vi.MockedFunction<typeof spawn>; |
| 13 | + let childProcessMock: ChildProcess; |
| 14 | + let stdoutMock: EventEmitter; |
| 15 | + let stderrMock: EventEmitter; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + childProcessMock = new EventEmitter() as ChildProcess; |
| 19 | + stdoutMock = new EventEmitter(); |
| 20 | + stderrMock = new EventEmitter(); |
| 21 | + // @ts-expect-error - Mocking properties for testing |
| 22 | + childProcessMock.stdout = stdoutMock; |
| 23 | + // @ts-expect-error - Mocking properties for testing |
| 24 | + childProcessMock.stderr = stderrMock; |
| 25 | + |
| 26 | + spawnMock = vi.mocked(spawn).mockReturnValue(childProcessMock); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + vi.resetAllMocks(); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should resolve with output on successful execution and use default timeout", async () => { |
| 34 | + const args = bash.schema.shape.arguments.parse({ cmd: "ls -l" }); |
| 35 | + const promise = bash.implementation(args); |
| 36 | + |
| 37 | + stdoutMock.emit("data", "file1.txt"); |
| 38 | + stdoutMock.emit("data", "file2.txt"); |
| 39 | + childProcessMock.emit("close", 0); |
| 40 | + |
| 41 | + await expect(promise).resolves.toBe("file1.txtfile2.txt"); |
| 42 | + expect(spawnMock).toHaveBeenCalledWith("ls -l", { |
| 43 | + cwd: process.cwd(), |
| 44 | + shell: "/bin/bash", |
| 45 | + stdio: ["ignore", "pipe", "pipe"], |
| 46 | + timeout: 30000, |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should use provided timeout", async () => { |
| 51 | + const args = bash.schema.shape.arguments.parse({ cmd: "sleep 5", timeout: 5000 }); |
| 52 | + const promise = bash.implementation(args); |
| 53 | + |
| 54 | + childProcessMock.emit("close", 0); |
| 55 | + |
| 56 | + await expect(promise).resolves.toBe(""); |
| 57 | + expect(spawnMock).toHaveBeenCalledWith("sleep 5", { |
| 58 | + cwd: process.cwd(), |
| 59 | + shell: "/bin/bash", |
| 60 | + stdio: ["ignore", "pipe", "pipe"], |
| 61 | + timeout: 5000, |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should reject with ToolError on non-zero exit code", async () => { |
| 66 | + const args = bash.schema.shape.arguments.parse({ cmd: "ls non_existent_dir" }); |
| 67 | + const promise = bash.implementation(args); |
| 68 | + |
| 69 | + stderrMock.emit("data", "ls: cannot access 'non_existent_dir': No such file or directory"); |
| 70 | + childProcessMock.emit("close", 2); |
| 71 | + |
| 72 | + await expect(promise).rejects.toThrow(ToolError); |
| 73 | + await expect(promise).rejects.toThrow( |
| 74 | + "Command exited with code: 2\nOutput:\nls: cannot access 'non_existent_dir': No such file or directory", |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should reject with ToolError on command start error", async () => { |
| 79 | + const error = new Error("spawn ENOENT"); |
| 80 | + const args = bash.schema.shape.arguments.parse({ cmd: "invalid_command" }); |
| 81 | + const promise = bash.implementation(args); |
| 82 | + |
| 83 | + childProcessMock.emit("error", error); |
| 84 | + |
| 85 | + await expect(promise).rejects.toThrow(ToolError); |
| 86 | + await expect(promise).rejects.toThrow(`Command failed to start: ${error.message}`); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should handle stderr output correctly", async () => { |
| 90 | + const args = bash.schema.shape.arguments.parse({ cmd: "some_command" }); |
| 91 | + const promise = bash.implementation(args); |
| 92 | + |
| 93 | + stderrMock.emit("data", "some error"); |
| 94 | + childProcessMock.emit("close", 1); |
| 95 | + |
| 96 | + await expect(promise).rejects.toThrow("Command exited with code: 1\nOutput:\nsome error"); |
| 97 | + }); |
| 98 | +}); |
0 commit comments