Elffuss BraidHub

kikocis/continue public

cd9781d18fda · 8.8 KiBRawPermalink
1import { runTerminalCommandTool } from "./runTerminalCommand.js";
2
3describe("runTerminalCommandTool", () => {
4 const TEST_TIMEOUT_MS = 1000; // 1 second for tests
5
6 beforeAll(() => {
7 process.env.NODE_ENV = "test";
8 process.env.TEST_TERMINAL_TIMEOUT = String(TEST_TIMEOUT_MS);
9 });
10
11 afterAll(() => {
12 delete process.env.TEST_TERMINAL_TIMEOUT;
13 });
14 describe("basic command execution", () => {
15 it("should execute simple echo command and return stdout", async () => {
16 const result = await runTerminalCommandTool.run({
17 command: "echo 'hello world'",
18 });
19 expect(result.trim()).toBe("hello world");
20 });
21
22 it("should execute pwd command and return current directory", async () => {
23 const result = await runTerminalCommandTool.run({ command: "pwd" });
24 expect(result.trim()).toMatch(/^\/.*$/); // Should be an absolute path
25 });
26
27 it("should execute directory listing command and return file listings", async () => {
28 // Use a cross-platform command that works on both Unix and Windows
29 const command = process.platform === "win32" ? "dir" : "ls -la .";
30 const result = await runTerminalCommandTool.run({ command });
31
32 // On Windows, look for <DIR> entries, on Unix look for . and ..
33 if (process.platform === "win32") {
34 expect(result).toContain(".");
35 } else {
36 expect(result).toContain(".");
37 expect(result).toContain("..");
38 }
39 });
40
41 it("should handle commands with multiple arguments", async () => {
42 const result = await runTerminalCommandTool.run({
43 command: "echo 'arg1' 'arg2' 'arg3'",
44 });
45 expect(result.trim()).toBe("arg1 arg2 arg3");
46 });
47 });
48
49 describe("error handling", () => {
50 it("should reject with error message for non-existent commands", async () => {
51 await expect(
52 runTerminalCommandTool.run({ command: "nonexistentcommand12345" }),
53 ).rejects.toMatch(/Error \(exit code 127\):/);
54 });
55
56 it("should handle commands with non-zero exit codes and stderr", async () => {
57 await expect(
58 runTerminalCommandTool.run({
59 command: "sh -c 'echo error >&2; exit 1'",
60 }),
61 ).rejects.toMatch(/Error \(exit code 1\): error/);
62 });
63
64 it("should resolve when commands have non-zero exit codes but no stderr", async () => {
65 const result = await runTerminalCommandTool.run({
66 command: "sh -c 'exit 1'",
67 });
68 expect(result).toBe("");
69 });
70
71 it("should handle commands that write to stderr", async () => {
72 const result = await runTerminalCommandTool.run({
73 command: 'sh -c \'echo "error message" >&2; echo "success"\'',
74 });
75 expect(result).toContain("success");
76 expect(result).toContain("Stderr: error message");
77 });
78 });
79
80 describe("timeout functionality", () => {
81 it(
82 "should timeout commands that produce no output for configured duration",
83 async () => {
84 // Create a command that sleeps longer than the timeout
85 const sleepDuration = TEST_TIMEOUT_MS / 1000 + 0.5; // Sleep 0.5s longer than timeout
86 const startTime = Date.now();
87 const result = await runTerminalCommandTool.run({
88 command: `sleep ${sleepDuration}`,
89 });
90 const endTime = Date.now();
91 const elapsed = endTime - startTime;
92
93 // Should timeout within a reasonable margin of the configured timeout
94 expect(elapsed).toBeLessThan(TEST_TIMEOUT_MS + 200); // Allow 200ms margin
95 expect(elapsed).toBeGreaterThan(TEST_TIMEOUT_MS - 100); // Allow 100ms margin
96 expect(result).toContain(
97 `[Command timed out after ${TEST_TIMEOUT_MS / 1000} seconds of no output]`,
98 );
99 },
100 TEST_TIMEOUT_MS + 2000,
101 ); // Set test timeout with buffer
102
103 it(
104 "should not timeout commands that continuously produce output",
105 async () => {
106 // Create a command that produces output at intervals shorter than timeout
107 const outputInterval = TEST_TIMEOUT_MS / 1000 / 3; // Output 3 times within timeout period
108 const iterations = 3;
109 const startTime = Date.now();
110 const result = await runTerminalCommandTool.run({
111 command: `for i in $(seq 1 ${iterations}); do echo "output $i"; sleep ${outputInterval}; done`,
112 });
113 const endTime = Date.now();
114
115 // Should complete normally
116 expect(endTime - startTime).toBeLessThan(TEST_TIMEOUT_MS * 2);
117 expect(result).toContain("output 1");
118 expect(result).toContain(`output ${iterations}`);
119 expect(result).not.toContain("[Command timed out");
120 },
121 TEST_TIMEOUT_MS * 3,
122 ); // Set test timeout with buffer
123
124 it(
125 "should reset timeout when command produces stderr output",
126 async () => {
127 // Create a command that produces stderr output periodically
128 const outputInterval = TEST_TIMEOUT_MS / 1000 / 3; // Output 3 times within timeout period
129 const iterations = 3;
130 const startTime = Date.now();
131 const result = await runTerminalCommandTool.run({
132 command: `for i in $(seq 1 ${iterations}); do echo "error $i" >&2; sleep ${outputInterval}; done; echo 'done'`,
133 });
134 const endTime = Date.now();
135
136 // Should complete normally
137 expect(endTime - startTime).toBeLessThan(TEST_TIMEOUT_MS * 2);
138 expect(result).toContain("done");
139 expect(result).toContain("Stderr: error 1");
140 expect(result).not.toContain("[Command timed out");
141 },
142 TEST_TIMEOUT_MS * 3,
143 ); // Set test timeout with buffer
144
145 it(
146 "should include partial output when timing out",
147 async () => {
148 // Create a command that produces some output then stops
149 const sleepDuration = TEST_TIMEOUT_MS / 1000 + 0.5; // Sleep longer than timeout
150 const result = await runTerminalCommandTool.run({
151 command: `echo 'initial output'; sleep ${sleepDuration}`,
152 });
153
154 expect(result).toContain("initial output");
155 expect(result).toContain(
156 `[Command timed out after ${TEST_TIMEOUT_MS / 1000} seconds of no output]`,
157 );
158 },
159 TEST_TIMEOUT_MS + 2000,
160 ); // Set test timeout with buffer
161 });
162
163 describe("output handling", () => {
164 it("should preserve line breaks in output", async () => {
165 const result = await runTerminalCommandTool.run({
166 command: "printf 'line1\\nline2\\nline3'",
167 });
168 expect(result).toContain("line1\nline2\nline3");
169 });
170
171 it("should handle empty output", async () => {
172 const result = await runTerminalCommandTool.run({ command: "true" });
173 expect(result).toBe("");
174 });
175
176 it("should handle large output", async () => {
177 // Generate a command that produces substantial output
178 const result = await runTerminalCommandTool.run({
179 command: "seq 1 1000",
180 });
181 expect(result).toContain("1\n");
182 expect(result).toContain("1000");
183 // Count lines to ensure all output is captured
184 const lines = result.trim().split("\n");
185 expect(lines).toHaveLength(1000);
186 });
187
188 it("should truncate output when it exceeds 5000 lines", async () => {
189 // Generate a command that produces more than 5000 lines
190 const result = await runTerminalCommandTool.run({
191 command: "seq 1 6000",
192 });
193
194 // Should contain the truncation message
195 expect(result).toContain(
196 "[Output truncated to first 5000 lines of 6001 total]",
197 );
198
199 // Should contain first line
200 expect(result).toContain("1\n");
201
202 // Should contain line 5000 but not line 6000
203 expect(result).toContain("5000");
204 expect(result).not.toContain("6000");
205
206 // Count lines in the result - should be 5000 content lines + 2 truncation message lines
207 const lines = result.split("\n");
208 expect(lines).toHaveLength(5002); // 5000 content lines + empty line + truncation message
209 });
210 });
211
212 describe("command types", () => {
213 it("should handle shell built-ins", async () => {
214 const result = await runTerminalCommandTool.run({ command: "echo $PWD" });
215 expect(result.trim()).toMatch(/^\/.*$/);
216 });
217
218 it("should handle commands with pipes", async () => {
219 const result = await runTerminalCommandTool.run({
220 command: "echo 'hello world' | wc -w",
221 });
222 expect(result.trim()).toBe("2");
223 });
224
225 it("should handle commands with redirections", async () => {
226 const result = await runTerminalCommandTool.run({
227 command:
228 "echo 'test' > /tmp/test-file && cat /tmp/test-file && rm /tmp/test-file",
229 });
230 expect(result.trim()).toBe("test");
231 });
232
233 it("should handle commands with environment variables", async () => {
234 const result = await runTerminalCommandTool.run({
235 command: "TEST_VAR='hello' sh -c 'echo $TEST_VAR'",
236 });
237 expect(result.trim()).toBe("hello");
238 });
239 });
240});