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