Elffuss BraidHub

kikocis/continue public

fix: shell mode tests
Changeset4c7748c819b28e75a97661a7bf6e37ccf1ac5cfc29fd50441771d1ad4b0e3e5d
AuthorDallin Romney <dallinromney@gmail.com> · 2025-09-02 22:40 -07:00
CommitterDallin Romney <dallinromney@gmail.com> · 2025-09-02 22:40 -07:00
Parents88f0ce600806

Browse the files at this changeset

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";
21 import { beforeEach, describe, expect, it, vi } from "vitest";
32
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";
65
7-import { useChat } from "./useChat.js";
6+import { handleBashModeProcessing } from "./useChat.shellMode.js";
87
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", () => ({
1110 runTerminalCommandTool: {
1211 run: vi.fn(),
1312 },
1413 }));
1514
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", () => {
1727 beforeEach(() => {
1828 vi.clearAllMocks();
1929 });
@@ -24,87 +34,114 @@
2434 "total 5\ndrwxr-xr-x 2 user user 4096 Jan 1 12:00 .\ndrwxr-xr-x 3 user user 4096 Jan 1 12:00 ..",
2535 );
2636
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");
3338
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+ );
3745
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+ );
3953
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+ );
4264 });
4365
4466 it("should handle bash command errors", async () => {
4567 const mockRun = vi.mocked(runTerminalCommandTool.run);
4668 mockRun.mockRejectedValue(new Error("Command not found"));
4769
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+ );
5378
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+ );
5786
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+ );
5997 });
6098
6199 it("should not trigger bash mode for regular messages", async () => {
62100 const mockRun = vi.mocked(runTerminalCommandTool.run);
63101
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?");
73103
104+ expect(result).toBe("Hello! How are you?"); // Pass through unchanged
74105 expect(mockRun).not.toHaveBeenCalled();
106+ expect(services.chatHistory.addAssistantMessage).not.toHaveBeenCalled();
75107 });
76108
77109 it("should handle empty bash command", async () => {
78110 const mockRun = vi.mocked(runTerminalCommandTool.run);
79111
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("!");
85113
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
91115 expect(mockRun).not.toHaveBeenCalled();
116+ expect(services.chatHistory.addAssistantMessage).not.toHaveBeenCalled();
92117 });
93118
94119 it("should handle bash command with leading whitespace", async () => {
95120 const mockRun = vi.mocked(runTerminalCommandTool.run);
96121 mockRun.mockResolvedValue("echo test output");
97122
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();
103127
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+ );
107135
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+ );
109146 });
110147 });