Elffuss BraidHub

kikocis/continue public

88f9397dc3b6 · 3.1 KiBRawPermalink
1import { act } from "@testing-library/react";
2import { describe, it, expect, vi, beforeEach } from "vitest";
3
4import { createMinimalTestContext } from "../../test-helpers/ui-test-context.js";
5import { runTerminalCommandTool } from "../../tools/runTerminalCommand.js";
6
7import { useChat } from "./useChat.js";
8
9// Mock the runTerminalCommandTool
10vi.mock("../../tools/runTerminalCommand.js", () => ({
11 runTerminalCommandTool: {
12 run: vi.fn(),
13 },
14}));
15
16describe("useChat - Bash Mode", () => {
17 beforeEach(() => {
18 vi.clearAllMocks();
19 });
20
21 it("should detect bash mode when message starts with !", async () => {
22 const mockRun = vi.mocked(runTerminalCommandTool.run);
23 mockRun.mockResolvedValue(
24 "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 );
26
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);
33
34 await act(async () => {
35 await chat.handleUserMessage("!ls -la");
36 });
37
38 expect(mockRun).toHaveBeenCalledWith({ command: "ls -la" });
39
40 // Since history is managed by service, we just verify no exception and tool called
41 ctx.cleanup();
42 });
43
44 it("should handle bash command errors", async () => {
45 const mockRun = vi.mocked(runTerminalCommandTool.run);
46 mockRun.mockRejectedValue(new Error("Command not found"));
47
48 const chat = useChat({
49 assistant: { name: "test" } as any,
50 model: { name: "test-model" } as any,
51 llmApi: {} as any,
52 } as any);
53
54 await act(async () => {
55 await chat.handleUserMessage("!invalidcommand");
56 });
57
58 expect(mockRun).toHaveBeenCalledWith({ command: "invalidcommand" });
59 });
60
61 it("should not trigger bash mode for regular messages", async () => {
62 const mockRun = vi.mocked(runTerminalCommandTool.run);
63
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 });
73
74 expect(mockRun).not.toHaveBeenCalled();
75 });
76
77 it("should handle empty bash command", async () => {
78 const mockRun = vi.mocked(runTerminalCommandTool.run);
79
80 const chat = useChat({
81 assistant: { name: "test" } as any,
82 model: { name: "test-model" } as any,
83 llmApi: {} as any,
84 } as any);
85
86 await act(async () => {
87 await chat.handleUserMessage("!");
88 });
89
90 // Should not call the tool for empty command
91 expect(mockRun).not.toHaveBeenCalled();
92 });
93
94 it("should handle bash command with leading whitespace", async () => {
95 const mockRun = vi.mocked(runTerminalCommandTool.run);
96 mockRun.mockResolvedValue("echo test output");
97
98 const chat = useChat({
99 assistant: { name: "test" } as any,
100 model: { name: "test-model" } as any,
101 llmApi: {} as any,
102 } as any);
103
104 await act(async () => {
105 await chat.handleUserMessage(" !echo hello");
106 });
107
108 expect(mockRun).toHaveBeenCalledWith({ command: "echo hello" });
109 });
110});