| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 1 | package claudetool |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 6 | "os" |
| 7 | "path/filepath" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 8 | "strings" |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 9 | "syscall" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 14 | func TestBashSlowOk(t *testing.T) { |
| 15 | // Test that slow_ok flag is properly handled |
| 16 | t.Run("SlowOk Flag", func(t *testing.T) { |
| 17 | input := json.RawMessage(`{"command":"echo 'slow test'","slow_ok":true}`) |
| 18 | |
| 19 | bashTool := (&BashTool{}).Tool() |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 20 | toolOut := bashTool.Run(context.Background(), input) |
| 21 | if toolOut.Error != nil { |
| 22 | t.Fatalf("Unexpected error: %v", toolOut.Error) |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 23 | } |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 24 | result := toolOut.LLMContent |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 25 | |
| 26 | expected := "slow test\n" |
| 27 | if len(result) == 0 || result[0].Text != expected { |
| 28 | t.Errorf("Expected %q, got %q", expected, result[0].Text) |
| 29 | } |
| 30 | }) |
| 31 | |
| 32 | // Test that slow_ok with background works |
| 33 | t.Run("SlowOk with Background", func(t *testing.T) { |
| 34 | input := json.RawMessage(`{"command":"echo 'slow background test'","slow_ok":true,"background":true}`) |
| 35 | |
| 36 | bashTool := (&BashTool{}).Tool() |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 37 | toolOut := bashTool.Run(context.Background(), input) |
| 38 | if toolOut.Error != nil { |
| 39 | t.Fatalf("Unexpected error: %v", toolOut.Error) |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 40 | } |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 41 | result := toolOut.LLMContent |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 42 | |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 43 | // Should return background result XML-ish format |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 44 | resultStr := result[0].Text |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 45 | if !strings.Contains(resultStr, "<pid>") || !strings.Contains(resultStr, "<output_file>") { |
| 46 | t.Errorf("Expected XML-ish background result format, got: %s", resultStr) |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 49 | // Extract PID and output file from XML-ish format for cleanup |
| 50 | // This is a simple extraction for test cleanup - in real usage the agent would parse this |
| 51 | lines := strings.Split(resultStr, "\n") |
| 52 | var outFile string |
| 53 | for _, line := range lines { |
| 54 | if strings.Contains(line, "<output_file>") { |
| 55 | start := strings.Index(line, "<output_file>") + len("<output_file>") |
| 56 | end := strings.Index(line, "</output_file>") |
| 57 | if end > start { |
| 58 | outFile = line[start:end] |
| 59 | } |
| 60 | break |
| 61 | } |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 64 | if outFile != "" { |
| 65 | // Clean up |
| 66 | os.Remove(outFile) |
| 67 | os.Remove(filepath.Dir(outFile)) |
| 68 | } |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 69 | }) |
| 70 | } |
| 71 | |
| Philip Zeyliger | cfd0fe6 | 2025-06-21 02:17:41 +0000 | [diff] [blame] | 72 | func TestBashTool(t *testing.T) { |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 73 | var bashTool BashTool |
| 74 | tool := bashTool.Tool() |
| 75 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 76 | // Test basic functionality |
| 77 | t.Run("Basic Command", func(t *testing.T) { |
| 78 | input := json.RawMessage(`{"command":"echo 'Hello, world!'"}`) |
| 79 | |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 80 | toolOut := tool.Run(context.Background(), input) |
| 81 | if toolOut.Error != nil { |
| 82 | t.Fatalf("Unexpected error: %v", toolOut.Error) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 83 | } |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 84 | result := toolOut.LLMContent |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 85 | |
| 86 | expected := "Hello, world!\n" |
| Philip Zeyliger | cfd0fe6 | 2025-06-21 02:17:41 +0000 | [diff] [blame] | 87 | if len(result) == 0 || result[0].Text != expected { |
| 88 | t.Errorf("Expected %q, got %q", expected, result[0].Text) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 89 | } |
| 90 | }) |
| 91 | |
| 92 | // Test with arguments |
| 93 | t.Run("Command With Arguments", func(t *testing.T) { |
| 94 | input := json.RawMessage(`{"command":"echo -n foo && echo -n bar"}`) |
| 95 | |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 96 | toolOut := tool.Run(context.Background(), input) |
| 97 | if toolOut.Error != nil { |
| 98 | t.Fatalf("Unexpected error: %v", toolOut.Error) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 99 | } |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 100 | result := toolOut.LLMContent |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 101 | |
| 102 | expected := "foobar" |
| Philip Zeyliger | cfd0fe6 | 2025-06-21 02:17:41 +0000 | [diff] [blame] | 103 | if len(result) == 0 || result[0].Text != expected { |
| 104 | t.Errorf("Expected %q, got %q", expected, result[0].Text) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 105 | } |
| 106 | }) |
| 107 | |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 108 | // Test with slow_ok parameter |
| 109 | t.Run("With SlowOK", func(t *testing.T) { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 110 | inputObj := struct { |
| 111 | Command string `json:"command"` |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 112 | SlowOK bool `json:"slow_ok"` |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 113 | }{ |
| 114 | Command: "sleep 0.1 && echo 'Completed'", |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 115 | SlowOK: true, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 116 | } |
| 117 | inputJSON, err := json.Marshal(inputObj) |
| 118 | if err != nil { |
| 119 | t.Fatalf("Failed to marshal input: %v", err) |
| 120 | } |
| 121 | |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 122 | toolOut := tool.Run(context.Background(), inputJSON) |
| 123 | if toolOut.Error != nil { |
| 124 | t.Fatalf("Unexpected error: %v", toolOut.Error) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 125 | } |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 126 | result := toolOut.LLMContent |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 127 | |
| 128 | expected := "Completed\n" |
| Philip Zeyliger | cfd0fe6 | 2025-06-21 02:17:41 +0000 | [diff] [blame] | 129 | if len(result) == 0 || result[0].Text != expected { |
| 130 | t.Errorf("Expected %q, got %q", expected, result[0].Text) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 131 | } |
| 132 | }) |
| 133 | |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 134 | // Test command timeout with custom timeout config |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 135 | t.Run("Command Timeout", func(t *testing.T) { |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 136 | // Use a custom BashTool with very short timeout |
| 137 | customTimeouts := &Timeouts{ |
| 138 | Fast: 100 * time.Millisecond, |
| 139 | Slow: 100 * time.Millisecond, |
| 140 | Background: 100 * time.Millisecond, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 141 | } |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 142 | customBash := &BashTool{ |
| 143 | Timeouts: customTimeouts, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 144 | } |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 145 | tool := customBash.Tool() |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 146 | |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 147 | input := json.RawMessage(`{"command":"sleep 0.5 && echo 'Should not see this'"}`) |
| 148 | |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 149 | toolOut := tool.Run(context.Background(), input) |
| 150 | if toolOut.Error == nil { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 151 | t.Errorf("Expected timeout error, got none") |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 152 | } else if !strings.Contains(toolOut.Error.Error(), "timed out") { |
| 153 | t.Errorf("Expected timeout error, got: %v", toolOut.Error) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 154 | } |
| 155 | }) |
| 156 | |
| 157 | // Test command that fails |
| 158 | t.Run("Failed Command", func(t *testing.T) { |
| 159 | input := json.RawMessage(`{"command":"exit 1"}`) |
| 160 | |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 161 | toolOut := tool.Run(context.Background(), input) |
| 162 | if toolOut.Error == nil { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 163 | t.Errorf("Expected error for failed command, got none") |
| 164 | } |
| 165 | }) |
| 166 | |
| 167 | // Test invalid input |
| 168 | t.Run("Invalid JSON Input", func(t *testing.T) { |
| 169 | input := json.RawMessage(`{"command":123}`) // Invalid JSON (command must be string) |
| 170 | |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 171 | toolOut := tool.Run(context.Background(), input) |
| 172 | if toolOut.Error == nil { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 173 | t.Errorf("Expected error for invalid input, got none") |
| 174 | } |
| 175 | }) |
| 176 | } |
| 177 | |
| 178 | func TestExecuteBash(t *testing.T) { |
| 179 | ctx := context.Background() |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 180 | bashTool := &BashTool{} |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 181 | |
| 182 | // Test successful command |
| 183 | t.Run("Successful Command", func(t *testing.T) { |
| 184 | req := bashInput{ |
| 185 | Command: "echo 'Success'", |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 188 | output, err := bashTool.executeBash(ctx, req, 5*time.Second) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 189 | if err != nil { |
| 190 | t.Fatalf("Unexpected error: %v", err) |
| 191 | } |
| 192 | |
| 193 | want := "Success\n" |
| 194 | if output != want { |
| 195 | t.Errorf("Expected %q, got %q", want, output) |
| 196 | } |
| 197 | }) |
| 198 | |
| Pokey Rule | b6d6d38 | 2025-05-07 10:29:03 +0100 | [diff] [blame] | 199 | // Test SKETCH=1 environment variable is set |
| 200 | t.Run("SKETCH Environment Variable", func(t *testing.T) { |
| 201 | req := bashInput{ |
| 202 | Command: "echo $SKETCH", |
| Pokey Rule | b6d6d38 | 2025-05-07 10:29:03 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 205 | output, err := bashTool.executeBash(ctx, req, 5*time.Second) |
| Pokey Rule | b6d6d38 | 2025-05-07 10:29:03 +0100 | [diff] [blame] | 206 | if err != nil { |
| 207 | t.Fatalf("Unexpected error: %v", err) |
| 208 | } |
| 209 | |
| 210 | want := "1\n" |
| 211 | if output != want { |
| 212 | t.Errorf("Expected SKETCH=1, got %q", output) |
| 213 | } |
| 214 | }) |
| 215 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 216 | // Test command with output to stderr |
| 217 | t.Run("Command with stderr", func(t *testing.T) { |
| 218 | req := bashInput{ |
| 219 | Command: "echo 'Error message' >&2 && echo 'Success'", |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 222 | output, err := bashTool.executeBash(ctx, req, 5*time.Second) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 223 | if err != nil { |
| 224 | t.Fatalf("Unexpected error: %v", err) |
| 225 | } |
| 226 | |
| 227 | want := "Error message\nSuccess\n" |
| 228 | if output != want { |
| 229 | t.Errorf("Expected %q, got %q", want, output) |
| 230 | } |
| 231 | }) |
| 232 | |
| 233 | // Test command that fails with stderr |
| 234 | t.Run("Failed Command with stderr", func(t *testing.T) { |
| 235 | req := bashInput{ |
| 236 | Command: "echo 'Error message' >&2 && exit 1", |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 239 | _, err := bashTool.executeBash(ctx, req, 5*time.Second) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 240 | if err == nil { |
| 241 | t.Errorf("Expected error for failed command, got none") |
| 242 | } else if !strings.Contains(err.Error(), "Error message") { |
| 243 | t.Errorf("Expected stderr in error message, got: %v", err) |
| 244 | } |
| 245 | }) |
| 246 | |
| 247 | // Test timeout |
| 248 | t.Run("Command Timeout", func(t *testing.T) { |
| 249 | req := bashInput{ |
| 250 | Command: "sleep 1 && echo 'Should not see this'", |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | start := time.Now() |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 254 | _, err := bashTool.executeBash(ctx, req, 100*time.Millisecond) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 255 | elapsed := time.Since(start) |
| 256 | |
| 257 | // Command should time out after ~100ms, not wait for full 1 second |
| 258 | if elapsed >= 1*time.Second { |
| 259 | t.Errorf("Command did not respect timeout, took %v", elapsed) |
| 260 | } |
| 261 | |
| 262 | if err == nil { |
| 263 | t.Errorf("Expected timeout error, got none") |
| 264 | } else if !strings.Contains(err.Error(), "timed out") { |
| 265 | t.Errorf("Expected timeout error, got: %v", err) |
| 266 | } |
| 267 | }) |
| 268 | } |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 269 | |
| 270 | func TestBackgroundBash(t *testing.T) { |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 271 | var bashTool BashTool |
| 272 | tool := bashTool.Tool() |
| 273 | |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 274 | // Test basic background execution |
| 275 | t.Run("Basic Background Command", func(t *testing.T) { |
| 276 | inputObj := struct { |
| 277 | Command string `json:"command"` |
| 278 | Background bool `json:"background"` |
| 279 | }{ |
| Pokey Rule | b6d6d38 | 2025-05-07 10:29:03 +0100 | [diff] [blame] | 280 | Command: "echo 'Hello from background' $SKETCH", |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 281 | Background: true, |
| 282 | } |
| 283 | inputJSON, err := json.Marshal(inputObj) |
| 284 | if err != nil { |
| 285 | t.Fatalf("Failed to marshal input: %v", err) |
| 286 | } |
| 287 | |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 288 | toolOut := tool.Run(context.Background(), inputJSON) |
| 289 | if toolOut.Error != nil { |
| 290 | t.Fatalf("Unexpected error: %v", toolOut.Error) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 291 | } |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 292 | result := toolOut.LLMContent |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 293 | |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 294 | // Parse the returned XML-ish format |
| Philip Zeyliger | cfd0fe6 | 2025-06-21 02:17:41 +0000 | [diff] [blame] | 295 | resultStr := result[0].Text |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 296 | if !strings.Contains(resultStr, "<pid>") || !strings.Contains(resultStr, "<output_file>") { |
| 297 | t.Fatalf("Expected XML-ish background result format, got: %s", resultStr) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 300 | // Extract PID and output file from XML-ish format |
| 301 | lines := strings.Split(resultStr, "\n") |
| 302 | var pidStr, outFile string |
| 303 | for _, line := range lines { |
| 304 | if strings.Contains(line, "<pid>") { |
| 305 | start := strings.Index(line, "<pid>") + len("<pid>") |
| 306 | end := strings.Index(line, "</pid>") |
| 307 | if end > start { |
| 308 | pidStr = line[start:end] |
| 309 | } |
| 310 | } else if strings.Contains(line, "<output_file>") { |
| 311 | start := strings.Index(line, "<output_file>") + len("<output_file>") |
| 312 | end := strings.Index(line, "</output_file>") |
| 313 | if end > start { |
| 314 | outFile = line[start:end] |
| 315 | } |
| 316 | } |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 319 | // Verify we got valid values |
| 320 | if pidStr == "" || outFile == "" { |
| 321 | t.Errorf("Failed to extract PID or output file from result: %s", resultStr) |
| 322 | return |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 323 | } |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 324 | |
| 325 | // Verify output file exists |
| 326 | if _, err := os.Stat(outFile); os.IsNotExist(err) { |
| 327 | t.Errorf("Output file doesn't exist: %s", outFile) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | // Wait for the command output to be written to file |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 331 | waitForFile(t, outFile) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 332 | |
| 333 | // Check file contents |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 334 | outputContent, err := os.ReadFile(outFile) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 335 | if err != nil { |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 336 | t.Fatalf("Failed to read output file: %v", err) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 337 | } |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 338 | // The implementation appends a completion message to the output |
| 339 | outputStr := string(outputContent) |
| 340 | if !strings.Contains(outputStr, "Hello from background 1") { |
| 341 | t.Errorf("Expected output to contain 'Hello from background 1', got %q", outputStr) |
| 342 | } |
| 343 | if !strings.Contains(outputStr, "[background process completed]") { |
| 344 | t.Errorf("Expected output to contain completion message, got %q", outputStr) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | // Clean up |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 348 | os.Remove(outFile) |
| 349 | os.Remove(filepath.Dir(outFile)) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 350 | }) |
| 351 | |
| 352 | // Test background command with stderr output |
| 353 | t.Run("Background Command with stderr", func(t *testing.T) { |
| 354 | inputObj := struct { |
| 355 | Command string `json:"command"` |
| 356 | Background bool `json:"background"` |
| 357 | }{ |
| 358 | Command: "echo 'Output to stdout' && echo 'Output to stderr' >&2", |
| 359 | Background: true, |
| 360 | } |
| 361 | inputJSON, err := json.Marshal(inputObj) |
| 362 | if err != nil { |
| 363 | t.Fatalf("Failed to marshal input: %v", err) |
| 364 | } |
| 365 | |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 366 | toolOut := tool.Run(context.Background(), inputJSON) |
| 367 | if toolOut.Error != nil { |
| 368 | t.Fatalf("Unexpected error: %v", toolOut.Error) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 369 | } |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 370 | result := toolOut.LLMContent |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 371 | |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 372 | // Parse the returned XML-ish format |
| Philip Zeyliger | cfd0fe6 | 2025-06-21 02:17:41 +0000 | [diff] [blame] | 373 | resultStr := result[0].Text |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 374 | lines := strings.Split(resultStr, "\n") |
| 375 | var outFile string |
| 376 | for _, line := range lines { |
| 377 | if strings.Contains(line, "<output_file>") { |
| 378 | start := strings.Index(line, "<output_file>") + len("<output_file>") |
| 379 | end := strings.Index(line, "</output_file>") |
| 380 | if end > start { |
| 381 | outFile = line[start:end] |
| 382 | } |
| 383 | break |
| 384 | } |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 387 | // Wait for the command output to be written to file |
| 388 | waitForFile(t, outFile) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 389 | |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 390 | // Check output content (stdout and stderr are combined in implementation) |
| 391 | outputContent, err := os.ReadFile(outFile) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 392 | if err != nil { |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 393 | t.Fatalf("Failed to read output file: %v", err) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 394 | } |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 395 | // Implementation combines stdout and stderr into one file |
| 396 | outputStr := string(outputContent) |
| 397 | if !strings.Contains(outputStr, "Output to stdout") || !strings.Contains(outputStr, "Output to stderr") { |
| 398 | t.Errorf("Expected both stdout and stderr content, got %q", outputStr) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | // Clean up |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 402 | os.Remove(outFile) |
| 403 | os.Remove(filepath.Dir(outFile)) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 404 | }) |
| 405 | |
| 406 | // Test background command running without waiting |
| 407 | t.Run("Background Command Running", func(t *testing.T) { |
| 408 | // Create a script that will continue running after we check |
| 409 | inputObj := struct { |
| 410 | Command string `json:"command"` |
| 411 | Background bool `json:"background"` |
| 412 | }{ |
| 413 | Command: "echo 'Running in background' && sleep 5", |
| 414 | Background: true, |
| 415 | } |
| 416 | inputJSON, err := json.Marshal(inputObj) |
| 417 | if err != nil { |
| 418 | t.Fatalf("Failed to marshal input: %v", err) |
| 419 | } |
| 420 | |
| 421 | // Start the command in the background |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 422 | toolOut := tool.Run(context.Background(), inputJSON) |
| 423 | if toolOut.Error != nil { |
| 424 | t.Fatalf("Unexpected error: %v", toolOut.Error) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 425 | } |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 426 | result := toolOut.LLMContent |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 427 | |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 428 | // Parse the returned XML-ish format |
| Philip Zeyliger | cfd0fe6 | 2025-06-21 02:17:41 +0000 | [diff] [blame] | 429 | resultStr := result[0].Text |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 430 | lines := strings.Split(resultStr, "\n") |
| 431 | var pidStr, outFile string |
| 432 | for _, line := range lines { |
| 433 | if strings.Contains(line, "<pid>") { |
| 434 | start := strings.Index(line, "<pid>") + len("<pid>") |
| 435 | end := strings.Index(line, "</pid>") |
| 436 | if end > start { |
| 437 | pidStr = line[start:end] |
| 438 | } |
| 439 | } else if strings.Contains(line, "<output_file>") { |
| 440 | start := strings.Index(line, "<output_file>") + len("<output_file>") |
| 441 | end := strings.Index(line, "</output_file>") |
| 442 | if end > start { |
| 443 | outFile = line[start:end] |
| 444 | } |
| 445 | } |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | // Wait for the command output to be written to file |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 449 | waitForFile(t, outFile) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 450 | |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 451 | // Check output content |
| 452 | outputContent, err := os.ReadFile(outFile) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 453 | if err != nil { |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 454 | t.Fatalf("Failed to read output file: %v", err) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 455 | } |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 456 | expectedOutput := "Running in background\n" |
| 457 | if string(outputContent) != expectedOutput { |
| 458 | t.Errorf("Expected output content %q, got %q", expectedOutput, string(outputContent)) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 461 | // Verify the process is still running by parsing PID |
| 462 | if pidStr != "" { |
| 463 | // We can't easily test if the process is still running without importing strconv |
| 464 | // and the process might have finished by now anyway due to timing |
| 465 | t.Log("Process started in background with PID:", pidStr) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | // Clean up |
| Josh Bleecher Snyder | 6fe809c | 2025-07-24 16:22:51 -0700 | [diff] [blame] | 469 | os.Remove(outFile) |
| 470 | os.Remove(filepath.Dir(outFile)) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 471 | }) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | func TestBashTimeout(t *testing.T) { |
| 475 | // Test default timeout values |
| 476 | t.Run("Default Timeout Values", func(t *testing.T) { |
| 477 | // Test foreground default timeout |
| 478 | foreground := bashInput{ |
| 479 | Command: "echo 'test'", |
| 480 | Background: false, |
| 481 | } |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 482 | fgTimeout := foreground.timeout(nil) |
| 483 | expectedFg := 30 * time.Second |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 484 | if fgTimeout != expectedFg { |
| 485 | t.Errorf("Expected foreground default timeout to be %v, got %v", expectedFg, fgTimeout) |
| 486 | } |
| 487 | |
| 488 | // Test background default timeout |
| 489 | background := bashInput{ |
| 490 | Command: "echo 'test'", |
| 491 | Background: true, |
| 492 | } |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 493 | bgTimeout := background.timeout(nil) |
| 494 | expectedBg := 24 * time.Hour |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 495 | if bgTimeout != expectedBg { |
| 496 | t.Errorf("Expected background default timeout to be %v, got %v", expectedBg, bgTimeout) |
| 497 | } |
| 498 | |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 499 | // Test slow_ok timeout |
| 500 | slowOk := bashInput{ |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 501 | Command: "echo 'test'", |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 502 | Background: false, |
| 503 | SlowOK: true, |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 504 | } |
| Josh Bleecher Snyder | 17b2fd9 | 2025-07-09 22:47:13 +0000 | [diff] [blame] | 505 | slowTimeout := slowOk.timeout(nil) |
| 506 | expectedSlow := 15 * time.Minute |
| 507 | if slowTimeout != expectedSlow { |
| 508 | t.Errorf("Expected slow_ok timeout to be %v, got %v", expectedSlow, slowTimeout) |
| 509 | } |
| 510 | |
| 511 | // Test custom timeout config |
| 512 | customTimeouts := &Timeouts{ |
| 513 | Fast: 5 * time.Second, |
| 514 | Slow: 2 * time.Minute, |
| 515 | Background: 1 * time.Hour, |
| 516 | } |
| 517 | customFast := bashInput{ |
| 518 | Command: "echo 'test'", |
| 519 | Background: false, |
| 520 | } |
| 521 | customTimeout := customFast.timeout(customTimeouts) |
| 522 | expectedCustom := 5 * time.Second |
| 523 | if customTimeout != expectedCustom { |
| 524 | t.Errorf("Expected custom timeout to be %v, got %v", expectedCustom, customTimeout) |
| Philip Zeyliger | b60f0f2 | 2025-04-23 18:19:32 +0000 | [diff] [blame] | 525 | } |
| 526 | }) |
| 527 | } |
| 528 | |
| 529 | // waitForFile waits for a file to exist and be non-empty or times out |
| 530 | func waitForFile(t *testing.T, filepath string) { |
| 531 | timeout := time.After(5 * time.Second) |
| 532 | tick := time.NewTicker(10 * time.Millisecond) |
| 533 | defer tick.Stop() |
| 534 | |
| 535 | for { |
| 536 | select { |
| 537 | case <-timeout: |
| 538 | t.Fatalf("Timed out waiting for file to exist and have contents: %s", filepath) |
| 539 | return |
| 540 | case <-tick.C: |
| 541 | info, err := os.Stat(filepath) |
| 542 | if err == nil && info.Size() > 0 { |
| 543 | return // File exists and has content |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | // waitForProcessDeath waits for a process to no longer exist or times out |
| 550 | func waitForProcessDeath(t *testing.T, pid int) { |
| 551 | timeout := time.After(5 * time.Second) |
| 552 | tick := time.NewTicker(50 * time.Millisecond) |
| 553 | defer tick.Stop() |
| 554 | |
| 555 | for { |
| 556 | select { |
| 557 | case <-timeout: |
| 558 | t.Fatalf("Timed out waiting for process %d to exit", pid) |
| 559 | return |
| 560 | case <-tick.C: |
| 561 | process, _ := os.FindProcess(pid) |
| 562 | err := process.Signal(syscall.Signal(0)) |
| 563 | if err != nil { |
| 564 | // Process doesn't exist |
| 565 | return |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | } |