Changesets · Branches · Tags · Compare
| Changeset | 4c7748c819b28e75a97661a7bf6e37ccf1ac5cfc29fd50441771d1ad4b0e3e5d |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-09-02 22:40 -07:00 |
| Committer | Dallin Romney <dallinromney@gmail.com> · 2025-09-02 22:40 -07:00 |
| Parents | 88f0ce600806 |
1 file changed
modified extensions/cli/src/ui/hooks/useChat.shellMode.test.ts +93 −56
| @@ -1,19 +1,29 @@ | ||
| 1 | -import { act } from "@testing-library/react"; | |
| 2 | 1 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | 2 | |
| 4 | -import { createMinimalTestContext } from "../../test-helpers/ui-test-context.js"; | |
| 5 | -import { runTerminalCommandTool } from "../../tools/runTerminalCommand.js"; | |
| 3 | +import { runTerminalCommandTool } from "src/tools/runTerminalCommand.js"; | |
| 4 | +import { services } from "../../services/index.js"; | |
| 6 | 5 | |
| 7 | -import { useChat } from "./useChat.js"; | |
| 6 | +import { handleBashModeProcessing } from "./useChat.shellMode.js"; | |
| 8 | 7 | |
| 9 | -// Mock the runTerminalCommandTool | |
| 10 | -vi.mock("../../tools/runTerminalCommand.js", () => ({ | |
| 8 | +// Mock the runTerminalCommandTool with the correct path for the import in shellMode.ts | |
| 9 | +vi.mock("src/tools/runTerminalCommand.js", () => ({ | |
| 11 | 10 | runTerminalCommandTool: { |
| 12 | 11 | run: vi.fn(), |
| 13 | 12 | }, |
| 14 | 13 | })); |
| 15 | 14 | |
| 16 | -describe("useChat - Bash Mode", () => { | |
| 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", () => { | |
| 17 | 27 | beforeEach(() => { |
| 18 | 28 | vi.clearAllMocks(); |
| 19 | 29 | }); |
| @@ -24,87 +34,114 @@ | ||
| 24 | 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 ..", |
| 25 | 35 | ); |
| 26 | 36 | |
| 27 | - const ctx = createMinimalTestContext(); | |
| 28 | - const chat = useChat({ | |
| 29 | - assistant: { name: "test" } as any, | |
| 30 | - model: { name: "test-model" } as any, | |
| 31 | - llmApi: {} as any, | |
| 32 | - } as any); | |
| 37 | + const result = await handleBashModeProcessing("!ls -la"); | |
| 33 | 38 | |
| 34 | - await act(async () => { | |
| 35 | - await chat.handleUserMessage("!ls -la"); | |
| 36 | - }); | |
| 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 | + ); | |
| 37 | 45 | |
| 38 | - expect(mockRun).toHaveBeenCalledWith({ command: "ls -la" }); | |
| 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 | + ); | |
| 39 | 53 | |
| 40 | - // Since history is managed by service, we just verify no exception and tool called | |
| 41 | - ctx.cleanup(); | |
| 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 | + ); | |
| 42 | 64 | }); |
| 43 | 65 | |
| 44 | 66 | it("should handle bash command errors", async () => { |
| 45 | 67 | const mockRun = vi.mocked(runTerminalCommandTool.run); |
| 46 | 68 | mockRun.mockRejectedValue(new Error("Command not found")); |
| 47 | 69 | |
| 48 | - const chat = useChat({ | |
| 49 | - assistant: { name: "test" } as any, | |
| 50 | - model: { name: "test-model" } as any, | |
| 51 | - llmApi: {} as any, | |
| 52 | - } as any); | |
| 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 | + ); | |
| 53 | 78 | |
| 54 | - await act(async () => { | |
| 55 | - await chat.handleUserMessage("!invalidcommand"); | |
| 56 | - }); | |
| 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 | + ); | |
| 57 | 86 | |
| 58 | - expect(mockRun).toHaveBeenCalledWith({ command: "invalidcommand" }); | |
| 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 | + ); | |
| 59 | 97 | }); |
| 60 | 98 | |
| 61 | 99 | it("should not trigger bash mode for regular messages", async () => { |
| 62 | 100 | const mockRun = vi.mocked(runTerminalCommandTool.run); |
| 63 | 101 | |
| 64 | - const chat = useChat({ | |
| 65 | - assistant: { name: "test" } as any, | |
| 66 | - model: { name: "test-model" } as any, | |
| 67 | - llmApi: {} as any, | |
| 68 | - } as any); | |
| 69 | - | |
| 70 | - await act(async () => { | |
| 71 | - await chat.handleUserMessage("Hello! How are you?"); | |
| 72 | - }); | |
| 102 | + const result = await handleBashModeProcessing("Hello! How are you?"); | |
| 73 | 103 | |
| 104 | + expect(result).toBe("Hello! How are you?"); // Pass through unchanged | |
| 74 | 105 | expect(mockRun).not.toHaveBeenCalled(); |
| 106 | + expect(services.chatHistory.addAssistantMessage).not.toHaveBeenCalled(); | |
| 75 | 107 | }); |
| 76 | 108 | |
| 77 | 109 | it("should handle empty bash command", async () => { |
| 78 | 110 | const mockRun = vi.mocked(runTerminalCommandTool.run); |
| 79 | 111 | |
| 80 | - const chat = useChat({ | |
| 81 | - assistant: { name: "test" } as any, | |
| 82 | - model: { name: "test-model" } as any, | |
| 83 | - llmApi: {} as any, | |
| 84 | - } as any); | |
| 112 | + const result = await handleBashModeProcessing("!"); | |
| 85 | 113 | |
| 86 | - await act(async () => { | |
| 87 | - await chat.handleUserMessage("!"); | |
| 88 | - }); | |
| 89 | - | |
| 90 | - // Should not call the tool for empty command | |
| 114 | + expect(result).toBe("!"); // Pass through unchanged for empty command | |
| 91 | 115 | expect(mockRun).not.toHaveBeenCalled(); |
| 116 | + expect(services.chatHistory.addAssistantMessage).not.toHaveBeenCalled(); | |
| 92 | 117 | }); |
| 93 | 118 | |
| 94 | 119 | it("should handle bash command with leading whitespace", async () => { |
| 95 | 120 | const mockRun = vi.mocked(runTerminalCommandTool.run); |
| 96 | 121 | mockRun.mockResolvedValue("echo test output"); |
| 97 | 122 | |
| 98 | - const chat = useChat({ | |
| 99 | - assistant: { name: "test" } as any, | |
| 100 | - model: { name: "test-model" } as any, | |
| 101 | - llmApi: {} as any, | |
| 102 | - } as any); | |
| 123 | + const result = await handleBashModeProcessing(" !echo hello"); | |
| 124 | + | |
| 125 | + expect(result).toBe(null); // Processed, no further handling needed | |
| 126 | + expect(services.chatHistory.addAssistantMessage).toHaveBeenCalled(); | |
| 103 | 127 | |
| 104 | - await act(async () => { | |
| 105 | - await chat.handleUserMessage(" !echo hello"); | |
| 106 | - }); | |
| 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 | + ); | |
| 107 | 135 | |
| 108 | - expect(mockRun).toHaveBeenCalledWith({ command: "echo hello" }); | |
| 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 | + ); | |
| 109 | 146 | }); |
| 110 | 147 | }); |