| 1 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | |
| 3 | import { runTerminalCommandTool } from "src/tools/runTerminalCommand.js"; |
| 4 | import { services } from "../../services/index.js"; |
| 5 | |
| 6 | import { handleBashModeProcessing } from "./useChat.shellMode.js"; |
| 7 | |
| 8 | // Mock the runTerminalCommandTool with the correct path for the import in shellMode.ts |
| 9 | vi.mock("src/tools/runTerminalCommand.js", () => ({ |
| 10 | runTerminalCommandTool: { |
| 11 | run: vi.fn(), |
| 12 | }, |
| 13 | })); |
| 14 | |
| 15 | // Mock the services |
| 16 | vi.mock("../../services/index.js", () => ({ |
| 17 | services: { |
| 18 | chatHistory: { |
| 19 | addAssistantMessage: vi.fn(), |
| 20 | updateToolStatus: vi.fn(), |
| 21 | addToolResult: vi.fn(), |
| 22 | }, |
| 23 | }, |
| 24 | })); |
| 25 | |
| 26 | describe("handleBashModeProcessing", () => { |
| 27 | beforeEach(() => { |
| 28 | vi.clearAllMocks(); |
| 29 | }); |
| 30 | |
| 31 | it("should detect bash mode when message starts with !", async () => { |
| 32 | const mockRun = vi.mocked(runTerminalCommandTool.run); |
| 33 | mockRun.mockResolvedValue( |
| 34 | "total 5\ndrwxr-xr-x 2 user user 4096 Jan 1 12:00 .\ndrwxr-xr-x 3 user user 4096 Jan 1 12:00 ..", |
| 35 | ); |
| 36 | |
| 37 | const result = await handleBashModeProcessing("!ls -la"); |
| 38 | |
| 39 | expect(result).toBe(null); // Processed, no further handling needed |
| 40 | expect(services.chatHistory.addAssistantMessage).toHaveBeenCalled(); |
| 41 | expect(services.chatHistory.updateToolStatus).toHaveBeenCalledWith( |
| 42 | expect.any(String), |
| 43 | "calling", |
| 44 | ); |
| 45 | |
| 46 | // Use vi.waitFor to wait for async operations in the void expression |
| 47 | await vi.waitFor( |
| 48 | () => { |
| 49 | expect(mockRun).toHaveBeenCalledWith({ command: "ls -la" }); |
| 50 | }, |
| 51 | { timeout: 1000 }, |
| 52 | ); |
| 53 | |
| 54 | await vi.waitFor( |
| 55 | () => { |
| 56 | expect(services.chatHistory.addToolResult).toHaveBeenCalledWith( |
| 57 | expect.any(String), |
| 58 | "total 5\ndrwxr-xr-x 2 user user 4096 Jan 1 12:00 .\ndrwxr-xr-x 3 user user 4096 Jan 1 12:00 ..", |
| 59 | "done", |
| 60 | ); |
| 61 | }, |
| 62 | { timeout: 1000 }, |
| 63 | ); |
| 64 | }); |
| 65 | |
| 66 | it("should handle bash command errors", async () => { |
| 67 | const mockRun = vi.mocked(runTerminalCommandTool.run); |
| 68 | mockRun.mockRejectedValue(new Error("Command not found")); |
| 69 | |
| 70 | const result = await handleBashModeProcessing("!invalidcommand"); |
| 71 | |
| 72 | expect(result).toBe(null); // Processed, no further handling needed |
| 73 | expect(services.chatHistory.addAssistantMessage).toHaveBeenCalled(); |
| 74 | expect(services.chatHistory.updateToolStatus).toHaveBeenCalledWith( |
| 75 | expect.any(String), |
| 76 | "calling", |
| 77 | ); |
| 78 | |
| 79 | // Use vi.waitFor to wait for async operations in the void expression |
| 80 | await vi.waitFor( |
| 81 | () => { |
| 82 | expect(mockRun).toHaveBeenCalledWith({ command: "invalidcommand" }); |
| 83 | }, |
| 84 | { timeout: 1000 }, |
| 85 | ); |
| 86 | |
| 87 | await vi.waitFor( |
| 88 | () => { |
| 89 | expect(services.chatHistory.addToolResult).toHaveBeenCalledWith( |
| 90 | expect.any(String), |
| 91 | "Bash command failed: Command not found", |
| 92 | "errored", |
| 93 | ); |
| 94 | }, |
| 95 | { timeout: 1000 }, |
| 96 | ); |
| 97 | }); |
| 98 | |
| 99 | it("should not trigger bash mode for regular messages", async () => { |
| 100 | const mockRun = vi.mocked(runTerminalCommandTool.run); |
| 101 | |
| 102 | const result = await handleBashModeProcessing("Hello! How are you?"); |
| 103 | |
| 104 | expect(result).toBe("Hello! How are you?"); // Pass through unchanged |
| 105 | expect(mockRun).not.toHaveBeenCalled(); |
| 106 | expect(services.chatHistory.addAssistantMessage).not.toHaveBeenCalled(); |
| 107 | }); |
| 108 | |
| 109 | it("should handle empty bash command", async () => { |
| 110 | const mockRun = vi.mocked(runTerminalCommandTool.run); |
| 111 | |
| 112 | const result = await handleBashModeProcessing("!"); |
| 113 | |
| 114 | expect(result).toBe("!"); // Pass through unchanged for empty command |
| 115 | expect(mockRun).not.toHaveBeenCalled(); |
| 116 | expect(services.chatHistory.addAssistantMessage).not.toHaveBeenCalled(); |
| 117 | }); |
| 118 | |
| 119 | it("should handle bash command with leading whitespace", async () => { |
| 120 | const mockRun = vi.mocked(runTerminalCommandTool.run); |
| 121 | mockRun.mockResolvedValue("echo test output"); |
| 122 | |
| 123 | const result = await handleBashModeProcessing(" !echo hello"); |
| 124 | |
| 125 | expect(result).toBe(null); // Processed, no further handling needed |
| 126 | expect(services.chatHistory.addAssistantMessage).toHaveBeenCalled(); |
| 127 | |
| 128 | // Use vi.waitFor to wait for async operations in the void expression |
| 129 | await vi.waitFor( |
| 130 | () => { |
| 131 | expect(mockRun).toHaveBeenCalledWith({ command: "echo hello" }); |
| 132 | }, |
| 133 | { timeout: 1000 }, |
| 134 | ); |
| 135 | |
| 136 | await vi.waitFor( |
| 137 | () => { |
| 138 | expect(services.chatHistory.addToolResult).toHaveBeenCalledWith( |
| 139 | expect.any(String), |
| 140 | "echo test output", |
| 141 | "done", |
| 142 | ); |
| 143 | }, |
| 144 | { timeout: 1000 }, |
| 145 | ); |
| 146 | }); |
| 147 | }); |