| 1 | package builtin |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/binary" |
| 7 | "encoding/json" |
| 8 | "fmt" |
| 9 | "net/http" |
| 10 | "net/http/httptest" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | "unicode/utf16" |
| 16 | |
| 17 | "go.uber.org/goleak" |
| 18 | "golang.org/x/text/encoding/simplifiedchinese" |
| 19 | |
| 20 | "reasonix/internal/tool" |
| 21 | ) |
| 22 | |
| 23 | // argsJSON marshals m into the JSON form a tool expects. Tests must not build |
| 24 | // the JSON by concatenating Go strings: on Windows, t.TempDir() returns a path |
| 25 | // like C:\Users\… and the embedded backslashes are interpreted as JSON string |
| 26 | // escapes (\U triggers a parse error). json.Marshal handles the escaping. |
| 27 | func argsJSON(t *testing.T, m map[string]any) json.RawMessage { |
| 28 | t.Helper() |
| 29 | b, err := json.Marshal(m) |
| 30 | if err != nil { |
| 31 | t.Fatalf("marshal args: %v", err) |
| 32 | } |
| 33 | return json.RawMessage(b) |
| 34 | } |
| 35 | |
| 36 | func runTool(t *testing.T, tl tool.Tool, m map[string]any) string { |
| 37 | t.Helper() |
| 38 | out, err := tl.Execute(context.Background(), argsJSON(t, m)) |
| 39 | if err != nil { |
| 40 | t.Fatalf("%s: %v", tl.Name(), err) |
| 41 | } |
| 42 | return out |
| 43 | } |
| 44 | |
| 45 | func TestBuiltinsRegistered(t *testing.T) { |
| 46 | want := []string{"bash", "code_index", "compress", "edit_file", "glob", "grep", "ls", "move_file", "multi_edit", "read_file", "web_fetch", "write_file"} |
| 47 | for _, name := range want { |
| 48 | if _, ok := tool.LookupBuiltin(name); !ok { |
| 49 | t.Errorf("built-in %q not registered", name) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // TestBuiltinReadOnlyClassification locks in which built-ins the agent may |
| 55 | // parallelise. Flipping a writer (write_file, edit_file, bash) to ReadOnly |
| 56 | // would re-order writes against reads in the same turn; this test fails fast |
| 57 | // if that ever happens. bash specifically must stay non-ReadOnly even though |
| 58 | // many invocations are pure reads — args aren't introspected. |
| 59 | func TestBuiltinReadOnlyClassification(t *testing.T) { |
| 60 | readOnly := map[string]bool{ |
| 61 | "read_file": true, "ls": true, "glob": true, "grep": true, "code_index": true, "compress": true, "web_fetch": true, |
| 62 | "write_file": false, "edit_file": false, "multi_edit": false, "move_file": false, "bash": false, |
| 63 | } |
| 64 | for name, want := range readOnly { |
| 65 | tl, ok := tool.LookupBuiltin(name) |
| 66 | if !ok { |
| 67 | t.Fatalf("built-in %q not registered", name) |
| 68 | } |
| 69 | if got := tl.ReadOnly(); got != want { |
| 70 | t.Errorf("%s.ReadOnly() = %v, want %v", name, got, want) |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | type compressStub struct { |
| 76 | request tool.CompressRequest |
| 77 | result tool.CompressResult |
| 78 | } |
| 79 | |
| 80 | func (s *compressStub) CompressContext(_ context.Context, request tool.CompressRequest) (tool.CompressResult, error) { |
| 81 | s.request = request |
| 82 | return s.result, nil |
| 83 | } |
| 84 | |
| 85 | func TestCompressToolProtocol(t *testing.T) { |
| 86 | stub := &compressStub{result: tool.CompressResult{ |
| 87 | Status: "ok", Direction: "before", Anchor: "unique", Messages: 4, |
| 88 | SourceTokens: 900, ProjectionTokens: 200, Mode: "summarized", |
| 89 | }} |
| 90 | ctx := tool.WithContextCompressor(context.Background(), stub) |
| 91 | out, err := (compressContext{}).Execute(ctx, argsJSON(t, map[string]any{ |
| 92 | "direction": "before", "anchor": " unique excerpt ", "focus": " keep decisions ", |
| 93 | })) |
| 94 | if err != nil { |
| 95 | t.Fatalf("compress Execute: %v", err) |
| 96 | } |
| 97 | if stub.request.Direction != "before" || stub.request.Anchor != "unique excerpt" || stub.request.Focus != "keep decisions" { |
| 98 | t.Fatalf("forwarded request = %+v", stub.request) |
| 99 | } |
| 100 | var got tool.CompressResult |
| 101 | if err := json.Unmarshal([]byte(out), &got); err != nil { |
| 102 | t.Fatalf("decode result: %v", err) |
| 103 | } |
| 104 | if got != stub.result { |
| 105 | t.Fatalf("result = %+v, want %+v", got, stub.result) |
| 106 | } |
| 107 | if !(compressContext{}).ReadOnly() || !(compressContext{}).PlanModeSafe() { |
| 108 | t.Fatal("compress must be workspace-read-only and Plan Mode safe") |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func TestCompressToolRejectsInvalidArgs(t *testing.T) { |
| 113 | tests := []struct { |
| 114 | name string |
| 115 | args map[string]any |
| 116 | want string |
| 117 | }{ |
| 118 | {name: "direction", args: map[string]any{"direction": "around", "anchor": "x"}, want: "direction"}, |
| 119 | {name: "case-sensitive direction", args: map[string]any{"direction": "Before", "anchor": "x"}, want: "direction"}, |
| 120 | {name: "empty anchor", args: map[string]any{"direction": "before", "anchor": " "}, want: "empty"}, |
| 121 | {name: "long anchor", args: map[string]any{"direction": "before", "anchor": strings.Repeat("a", 513)}, want: "512 bytes"}, |
| 122 | {name: "long focus", args: map[string]any{"direction": "before", "anchor": "x", "focus": strings.Repeat("a", 2001)}, want: "2000 bytes"}, |
| 123 | } |
| 124 | for _, tt := range tests { |
| 125 | t.Run(tt.name, func(t *testing.T) { |
| 126 | _, err := (compressContext{}).Execute(context.Background(), argsJSON(t, tt.args)) |
| 127 | if err == nil || !strings.Contains(err.Error(), tt.want) { |
| 128 | t.Fatalf("error = %v, want containing %q", err, tt.want) |
| 129 | } |
| 130 | }) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func TestCompressToolRequiresActiveAgent(t *testing.T) { |
| 135 | _, err := (compressContext{}).Execute(context.Background(), argsJSON(t, map[string]any{ |
| 136 | "direction": "after", "anchor": "unique", |
| 137 | })) |
| 138 | if err == nil || !strings.Contains(err.Error(), "active agent session") { |
| 139 | t.Fatalf("error = %v, want unavailable context compressor", err) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | func TestReadFile(t *testing.T) { |
| 144 | dir := t.TempDir() |
| 145 | f := filepath.Join(dir, "src.go") |
| 146 | body := "package main\n\nfunc main() {}\n" |
| 147 | os.WriteFile(f, []byte(body), 0o644) |
| 148 | |
| 149 | out := runTool(t, readFile{}, map[string]any{"path": f}) |
| 150 | // Line numbers must be present, right-aligned, with the arrow separator. |
| 151 | for _, want := range []string{"1→package main", "2→", "3→func main"} { |
| 152 | if !strings.Contains(out, want) { |
| 153 | t.Errorf("missing %q in:\n%s", want, out) |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func TestReadFileDirectory(t *testing.T) { |
| 159 | dir := t.TempDir() |
| 160 | _, err := readFile{}.Execute(context.Background(), argsJSON(t, map[string]any{"path": dir})) |
| 161 | if err == nil { |
| 162 | t.Fatal("read_file on a directory should error, not return contents") |
| 163 | } |
| 164 | // The message must be actionable (point at ls) and not the doubled |
| 165 | // "read X: read X:" the raw scanner error produced. |
| 166 | if !strings.Contains(err.Error(), "directory") || !strings.Contains(err.Error(), "ls") { |
| 167 | t.Errorf("error should tell the model to use ls, got: %v", err) |
| 168 | } |
| 169 | if strings.Count(err.Error(), "read "+dir) > 1 { |
| 170 | t.Errorf("error is doubled: %v", err) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func TestReadFileOffsetLimit(t *testing.T) { |
| 175 | dir := t.TempDir() |
| 176 | f := filepath.Join(dir, "many.txt") |
| 177 | var b strings.Builder |
| 178 | for i := 1; i <= 50; i++ { |
| 179 | fmt.Fprintf(&b, "line %d\n", i) |
| 180 | } |
| 181 | os.WriteFile(f, []byte(b.String()), 0o644) |
| 182 | |
| 183 | out := runTool(t, readFile{}, map[string]any{"path": f, "offset": 10, "limit": 5}) |
| 184 | // Should see lines 11-15 only. |
| 185 | for _, want := range []string{"11→line 11", "15→line 15"} { |
| 186 | if !strings.Contains(out, want) { |
| 187 | t.Errorf("missing %q in:\n%s", want, out) |
| 188 | } |
| 189 | } |
| 190 | for _, leak := range []string{"line 5\n", "line 16\n", "line 20\n"} { |
| 191 | if strings.Contains(out, leak) { |
| 192 | t.Errorf("leaked %q (outside the slice)\n%s", leak, out) |
| 193 | } |
| 194 | } |
| 195 | // Trailer announces what's left so the model can paginate. |
| 196 | if !strings.Contains(out, "PARTIAL view") || !strings.Contains(out, "offset=15") { |
| 197 | t.Errorf("pagination hint missing:\n%s", out) |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | func TestReadFileBinary(t *testing.T) { |
| 202 | f := filepath.Join(t.TempDir(), "blob") |
| 203 | os.WriteFile(f, []byte{0x7f, 'E', 'L', 'F', 0, 0, 0}, 0o644) |
| 204 | |
| 205 | _, err := readFile{}.Execute(context.Background(), argsJSON(t, map[string]any{"path": f})) |
| 206 | if err == nil || !strings.Contains(err.Error(), "binary") { |
| 207 | t.Errorf("expected binary-file error, got %v", err) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | func TestReadFileBOM(t *testing.T) { |
| 212 | enc := func(order binary.ByteOrder, s string) []byte { |
| 213 | var b bytes.Buffer |
| 214 | if order == binary.LittleEndian { |
| 215 | b.Write([]byte{0xFF, 0xFE}) |
| 216 | } else { |
| 217 | b.Write([]byte{0xFE, 0xFF}) |
| 218 | } |
| 219 | for _, r := range utf16.Encode([]rune(s)) { |
| 220 | _ = binary.Write(&b, order, r) |
| 221 | } |
| 222 | return b.Bytes() |
| 223 | } |
| 224 | cases := map[string][]byte{ |
| 225 | "utf16le.txt": enc(binary.LittleEndian, "hello world\nsecond line"), |
| 226 | "utf16be.txt": enc(binary.BigEndian, "hello world\nsecond line"), |
| 227 | "utf8bom.txt": append([]byte{0xEF, 0xBB, 0xBF}, []byte("hello world\nsecond line")...), |
| 228 | } |
| 229 | for name, content := range cases { |
| 230 | f := filepath.Join(t.TempDir(), name) |
| 231 | os.WriteFile(f, content, 0o644) |
| 232 | out := runTool(t, readFile{}, map[string]any{"path": f}) |
| 233 | if !strings.Contains(out, "hello world") || !strings.Contains(out, "second line") { |
| 234 | t.Errorf("%s: expected decoded text, got %q", name, out) |
| 235 | } |
| 236 | if strings.Contains(out, "\ufeff") || strings.IndexByte(out, 0) >= 0 { |
| 237 | t.Errorf("%s: BOM/NUL leaked into output: %q", name, out) |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | func TestReadFileEmpty(t *testing.T) { |
| 243 | f := filepath.Join(t.TempDir(), "empty.txt") |
| 244 | os.WriteFile(f, nil, 0o644) |
| 245 | if out := runTool(t, readFile{}, map[string]any{"path": f}); !strings.Contains(out, "empty") { |
| 246 | t.Errorf("empty file should report empty, got %q", out) |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | func TestEditFile(t *testing.T) { |
| 251 | f := filepath.Join(t.TempDir(), "a.txt") |
| 252 | os.WriteFile(f, []byte("hello world\n"), 0o644) |
| 253 | |
| 254 | out := runTool(t, editFile{}, map[string]any{"path": f, "old_string": "world", "new_string": "reasonix"}) |
| 255 | for _, want := range []string{"Actual replacement receipt after write:", "-world", "+reasonix"} { |
| 256 | if !strings.Contains(out, want) { |
| 257 | t.Fatalf("edit result should contain %q in actual post-write receipt:\n%s", want, out) |
| 258 | } |
| 259 | } |
| 260 | if strings.Contains(out, "hello") { |
| 261 | t.Fatalf("edit receipt should not include unchanged same-line content:\n%s", out) |
| 262 | } |
| 263 | if b, _ := os.ReadFile(f); string(b) != "hello reasonix\n" { |
| 264 | t.Fatalf("after edit = %q", b) |
| 265 | } |
| 266 | |
| 267 | // Non-unique old_string must error and not modify the file. |
| 268 | os.WriteFile(f, []byte("x x x"), 0o644) |
| 269 | args := argsJSON(t, map[string]any{"path": f, "old_string": "x", "new_string": "y"}) |
| 270 | if _, err := (editFile{}).Execute(context.Background(), args); err == nil { |
| 271 | t.Fatal("expected not-unique error") |
| 272 | } else if !strings.Contains(err.Error(), "repeated separator lines") { |
| 273 | t.Fatalf("not-unique error should steer away from weak anchors, got: %v", err) |
| 274 | } |
| 275 | if b, _ := os.ReadFile(f); string(b) != "x x x" { |
| 276 | t.Fatalf("file modified despite error: %q", b) |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | func TestMultiEdit(t *testing.T) { |
| 281 | f := filepath.Join(t.TempDir(), "src.go") |
| 282 | body := "package old\n\nfunc old() {\n\told()\n}\n" |
| 283 | os.WriteFile(f, []byte(body), 0o644) |
| 284 | |
| 285 | // Two edits: rename the package (unique) then sweep every old → new. |
| 286 | out := runTool(t, multiEdit{}, map[string]any{ |
| 287 | "path": f, |
| 288 | "edits": []map[string]any{ |
| 289 | {"old_string": "package old", "new_string": "package new"}, |
| 290 | {"old_string": "old", "new_string": "reasonix", "replace_all": true}, |
| 291 | }, |
| 292 | }) |
| 293 | if !strings.Contains(out, "multi_edit") || !strings.Contains(out, "2 edits applied") { |
| 294 | t.Errorf("summary unexpected: %q", out) |
| 295 | } |
| 296 | for _, want := range []string{"Actual replacement receipt after write:", "-package old", "+package new", "-old", "+reasonix"} { |
| 297 | if !strings.Contains(out, want) { |
| 298 | t.Fatalf("multi_edit result should contain %q in actual post-write receipt:\n%s", want, out) |
| 299 | } |
| 300 | } |
| 301 | if strings.Contains(out, "func reasonix") { |
| 302 | t.Fatalf("multi_edit receipt should not include unchanged same-line content:\n%s", out) |
| 303 | } |
| 304 | got, _ := os.ReadFile(f) |
| 305 | want := "package new\n\nfunc reasonix() {\n\treasonix()\n}\n" |
| 306 | if string(got) != want { |
| 307 | t.Errorf("after multi_edit = %q\n want = %q", got, want) |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // TestMultiEditAtomicity is the safety guarantee: if any edit fails, the file |
| 312 | // stays exactly as it was. A chained sequence of single edit_file calls would |
| 313 | // have left a half-written intermediate state. |
| 314 | func TestMultiEditAtomicity(t *testing.T) { |
| 315 | f := filepath.Join(t.TempDir(), "a.txt") |
| 316 | original := "alpha\nbeta\ngamma\n" |
| 317 | os.WriteFile(f, []byte(original), 0o644) |
| 318 | |
| 319 | args := argsJSON(t, map[string]any{ |
| 320 | "path": f, |
| 321 | "edits": []map[string]any{ |
| 322 | {"old_string": "alpha", "new_string": "ALPHA"}, |
| 323 | {"old_string": "no-such-text", "new_string": "x"}, |
| 324 | {"old_string": "gamma", "new_string": "GAMMA"}, |
| 325 | }, |
| 326 | }) |
| 327 | if _, err := (multiEdit{}).Execute(context.Background(), args); err == nil { |
| 328 | t.Fatal("expected failure on the missing edit") |
| 329 | } |
| 330 | got, _ := os.ReadFile(f) |
| 331 | if string(got) != original { |
| 332 | t.Errorf("file was modified despite failure:\n got %q\nwant %q", got, original) |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | func TestGrep(t *testing.T) { |
| 337 | dir := t.TempDir() |
| 338 | os.WriteFile(filepath.Join(dir, "a.go"), []byte("package main\nfunc Foo() {}\n"), 0o644) |
| 339 | os.WriteFile(filepath.Join(dir, "b.go"), []byte("var x = 1\n"), 0o644) |
| 340 | |
| 341 | out := runTool(t, grepTool{}, map[string]any{"pattern": "func ", "path": dir}) |
| 342 | if !strings.Contains(out, "Foo") || strings.Contains(out, "var x") { |
| 343 | t.Fatalf("grep result = %q", out) |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // TestWebFetchHTML serves a tiny HTML page and checks the reducer keeps the |
| 348 | // readable text while removing scripts, styles, and tags. |
| 349 | func TestWebFetchHTML(t *testing.T) { |
| 350 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 351 | w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 352 | _, _ = w.Write([]byte(`<!doctype html> |
| 353 | <html><head><title>T</title><style>body{color:red}</style></head> |
| 354 | <body> |
| 355 | <h1>Hello & world</h1> |
| 356 | <script>alert("bad")</script> |
| 357 | <p>Visible text.</p> |
| 358 | </body></html>`)) |
| 359 | })) |
| 360 | defer srv.Close() |
| 361 | |
| 362 | out := runTool(t, webFetch{}, map[string]any{"url": srv.URL}) |
| 363 | for _, want := range []string{"Hello & world", "Visible text", "text/html"} { |
| 364 | if !strings.Contains(out, want) { |
| 365 | t.Errorf("missing %q in:\n%s", want, out) |
| 366 | } |
| 367 | } |
| 368 | for _, leak := range []string{"<script", "alert(", "<style", "<h1>", "&"} { |
| 369 | if strings.Contains(out, leak) { |
| 370 | t.Errorf("leaked raw HTML/script %q", leak) |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | func TestWebFetchHTMLTokenizerHandlesAttributesAndEntities(t *testing.T) { |
| 376 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 377 | w.Header().Set("Content-Type", "text/html") |
| 378 | _, _ = w.Write([]byte(`<html><body><p title="1 > 0">Tom's docs</p><script>visible = false</script><p>Next</p></body></html>`)) |
| 379 | })) |
| 380 | defer srv.Close() |
| 381 | |
| 382 | out := runTool(t, webFetch{}, map[string]any{"url": srv.URL}) |
| 383 | for _, want := range []string{"Tom's docs", "Next"} { |
| 384 | if !strings.Contains(out, want) { |
| 385 | t.Fatalf("missing %q in:\n%s", want, out) |
| 386 | } |
| 387 | } |
| 388 | if strings.Contains(out, "visible = false") || strings.Contains(out, "title=") || strings.Contains(out, "'") { |
| 389 | t.Fatalf("HTML tokenizer leaked markup/script/entity:\n%s", out) |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | func TestWebFetchHTMLStructuredText(t *testing.T) { |
| 394 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 395 | w.Header().Set("Content-Type", "text/html") |
| 396 | _, _ = w.Write([]byte(`<html><head><title>Doc title</title></head><body> |
| 397 | <h1>Main</h1> |
| 398 | <p>Read the <a href="/guide?a=1&b=2">guide</a>.</p> |
| 399 | <ul><li>First</li><li>Second</li></ul> |
| 400 | <pre>go test ./... |
| 401 | line two</pre> |
| 402 | <table><tr><th>Name</th><th>Value</th></tr><tr><td>A</td><td>42</td></tr></table> |
| 403 | </body></html>`)) |
| 404 | })) |
| 405 | defer srv.Close() |
| 406 | |
| 407 | out := runTool(t, webFetch{}, map[string]any{"url": srv.URL}) |
| 408 | for _, want := range []string{ |
| 409 | "# Doc title", |
| 410 | "# Main", |
| 411 | "guide (/guide?a=1&b=2)", |
| 412 | "- First", |
| 413 | "- Second", |
| 414 | "```\ngo test ./...\nline two\n```", |
| 415 | "Name | Value", |
| 416 | "A | 42", |
| 417 | } { |
| 418 | if !strings.Contains(out, want) { |
| 419 | t.Fatalf("structured HTML output missing %q:\n%s", want, out) |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | // TestWebFetchPlain confirms non-HTML bodies pass through untouched (apart |
| 425 | // from the prepended status header). |
| 426 | func TestWebFetchPlain(t *testing.T) { |
| 427 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 428 | w.Header().Set("Content-Type", "text/plain") |
| 429 | _, _ = w.Write([]byte("line1\nline2\n")) |
| 430 | })) |
| 431 | defer srv.Close() |
| 432 | |
| 433 | out := runTool(t, webFetch{}, map[string]any{"url": srv.URL}) |
| 434 | if !strings.Contains(out, "line1") || !strings.Contains(out, "line2") { |
| 435 | t.Errorf("plain body content missing:\n%s", out) |
| 436 | } |
| 437 | if strings.Contains(out, "<") { |
| 438 | t.Errorf("html reducer ran on text/plain: %s", out) |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | // TestWebFetchSchemeRejected blocks anything that's not http(s) so the tool |
| 443 | // can't be tricked into reading file:// or arbitrary URI schemes. |
| 444 | func TestWebFetchSchemeRejected(t *testing.T) { |
| 445 | _, err := webFetch{}.Execute(context.Background(), argsJSON(t, map[string]any{"url": "file:///etc/passwd"})) |
| 446 | if err == nil || !strings.Contains(err.Error(), "http(s)") { |
| 447 | t.Errorf("expected scheme rejection, got %v", err) |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | func TestLsAndGlob(t *testing.T) { |
| 452 | dir := t.TempDir() |
| 453 | os.WriteFile(filepath.Join(dir, "x.txt"), []byte("hi"), 0o644) |
| 454 | os.Mkdir(filepath.Join(dir, "sub"), 0o755) |
| 455 | |
| 456 | ls := runTool(t, listDir{}, map[string]any{"path": dir}) |
| 457 | if !strings.Contains(ls, "x.txt") || !strings.Contains(ls, "sub/") { |
| 458 | t.Fatalf("ls result = %q", ls) |
| 459 | } |
| 460 | |
| 461 | g := runTool(t, globTool{}, map[string]any{"pattern": filepath.Join(dir, "*.txt")}) |
| 462 | if !strings.Contains(g, "x.txt") { |
| 463 | t.Fatalf("glob result = %q", g) |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | func TestGlobRecursive(t *testing.T) { |
| 468 | dir := t.TempDir() |
| 469 | // Create a nested structure: |
| 470 | // dir/a.go |
| 471 | // dir/sub/b.go |
| 472 | // dir/sub/deep/c.go |
| 473 | // dir/sub/deep/c.txt |
| 474 | // dir/other.txt |
| 475 | os.WriteFile(filepath.Join(dir, "a.go"), []byte("package a"), 0o644) |
| 476 | os.MkdirAll(filepath.Join(dir, "sub", "deep"), 0o755) |
| 477 | os.WriteFile(filepath.Join(dir, "sub", "b.go"), []byte("package b"), 0o644) |
| 478 | os.WriteFile(filepath.Join(dir, "sub", "deep", "c.go"), []byte("package c"), 0o644) |
| 479 | os.WriteFile(filepath.Join(dir, "sub", "deep", "c.txt"), []byte("text"), 0o644) |
| 480 | os.WriteFile(filepath.Join(dir, "other.txt"), []byte("other"), 0o644) |
| 481 | |
| 482 | // ** *.go should find all .go files recursively. |
| 483 | out := runTool(t, globTool{}, map[string]any{"pattern": filepath.Join(dir, "**", "*.go")}) |
| 484 | if !strings.Contains(out, "a.go") { |
| 485 | t.Errorf("missing a.go in:\n%s", out) |
| 486 | } |
| 487 | if !strings.Contains(out, "b.go") { |
| 488 | t.Errorf("missing b.go in:\n%s", out) |
| 489 | } |
| 490 | if !strings.Contains(out, "c.go") { |
| 491 | t.Errorf("missing c.go in:\n%s", out) |
| 492 | } |
| 493 | // Should not include .txt files. |
| 494 | if strings.Contains(out, "other.txt") || strings.Contains(out, "c.txt") { |
| 495 | t.Errorf("should not include .txt files:\n%s", out) |
| 496 | } |
| 497 | |
| 498 | // ** *.txt should find all .txt files recursively. |
| 499 | out2 := runTool(t, globTool{}, map[string]any{"pattern": filepath.Join(dir, "**", "*.txt")}) |
| 500 | if !strings.Contains(out2, "other.txt") { |
| 501 | t.Errorf("missing other.txt in:\n%s", out2) |
| 502 | } |
| 503 | if !strings.Contains(out2, "c.txt") { |
| 504 | t.Errorf("missing c.txt in:\n%s", out2) |
| 505 | } |
| 506 | |
| 507 | // ** with no suffix should find all files. |
| 508 | out3 := runTool(t, globTool{}, map[string]any{"pattern": filepath.Join(dir, "**")}) |
| 509 | if !strings.Contains(out3, "a.go") || !strings.Contains(out3, "c.txt") { |
| 510 | t.Errorf("bare ** should find all files:\n%s", out3) |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | func TestGlobForwardSlashPattern(t *testing.T) { |
| 515 | dir := t.TempDir() |
| 516 | os.MkdirAll(filepath.Join(dir, "sub", "deep"), 0o755) |
| 517 | os.WriteFile(filepath.Join(dir, "top.txt"), []byte("x"), 0o644) |
| 518 | os.WriteFile(filepath.Join(dir, "sub", "deep", "nested.txt"), []byte("y"), 0o644) |
| 519 | t.Chdir(dir) |
| 520 | |
| 521 | out := runTool(t, globTool{}, map[string]any{"pattern": "**/*.txt"}) |
| 522 | if !strings.Contains(out, "top.txt") || !strings.Contains(out, "nested.txt") { |
| 523 | t.Errorf("forward-slash recursive pattern should match every .txt:\n%s", out) |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | func TestGlobRecursiveDoublestarBracePattern(t *testing.T) { |
| 528 | dir := t.TempDir() |
| 529 | os.WriteFile(filepath.Join(dir, "a.go"), []byte("go"), 0o644) |
| 530 | os.WriteFile(filepath.Join(dir, "b.txt"), []byte("txt"), 0o644) |
| 531 | os.WriteFile(filepath.Join(dir, "c.md"), []byte("md"), 0o644) |
| 532 | |
| 533 | out := runTool(t, globTool{}, map[string]any{"pattern": filepath.Join(dir, "**", "*.{go,txt}")}) |
| 534 | if !strings.Contains(out, "a.go") || !strings.Contains(out, "b.txt") { |
| 535 | t.Fatalf("brace pattern should match go and txt:\n%s", out) |
| 536 | } |
| 537 | if strings.Contains(out, "c.md") { |
| 538 | t.Fatalf("brace pattern should not match markdown:\n%s", out) |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | func TestGlobRecursiveNoMatches(t *testing.T) { |
| 543 | dir := t.TempDir() |
| 544 | os.MkdirAll(filepath.Join(dir, "sub"), 0o755) |
| 545 | os.WriteFile(filepath.Join(dir, "sub", "a.go"), []byte("package a"), 0o644) |
| 546 | |
| 547 | out := runTool(t, globTool{}, map[string]any{"pattern": filepath.Join(dir, "**", "*.py")}) |
| 548 | if !strings.Contains(out, "(no matches)") { |
| 549 | t.Errorf("expected (no matches), got:\n%s", out) |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | func TestGlobNoMatches(t *testing.T) { |
| 554 | dir := t.TempDir() |
| 555 | out := runTool(t, globTool{}, map[string]any{"pattern": filepath.Join(dir, "*.xyz")}) |
| 556 | if !strings.Contains(out, "(no matches)") { |
| 557 | t.Errorf("expected (no matches), got:\n%s", out) |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | // GB18030 encoding integration tests (issue #2637) |
| 562 | |
| 563 | func TestReadFileGB18030(t *testing.T) { |
| 564 | f := filepath.Join(t.TempDir(), "gbk.txt") |
| 565 | gb, err := simplifiedchinese.GB18030.NewEncoder().String("你好世界\n第二行") |
| 566 | if err != nil { |
| 567 | t.Fatalf("encode: %v", err) |
| 568 | } |
| 569 | os.WriteFile(f, []byte(gb), 0o644) |
| 570 | |
| 571 | out := runTool(t, readFile{}, map[string]any{"path": f}) |
| 572 | if !strings.Contains(out, "你好世界") || !strings.Contains(out, "第二行") { |
| 573 | t.Errorf("expected decoded Chinese text, got:\n%s", out) |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | func TestEditFileGB18030RoundTrip(t *testing.T) { |
| 578 | f := filepath.Join(t.TempDir(), "gbk.txt") |
| 579 | original, _ := simplifiedchinese.GB18030.NewEncoder().String("你好世界\n第二行\n") |
| 580 | os.WriteFile(f, []byte(original), 0o644) |
| 581 | |
| 582 | runTool(t, editFile{}, map[string]any{ |
| 583 | "path": f, |
| 584 | "old_string": "第二行", |
| 585 | "new_string": "新的行", |
| 586 | }) |
| 587 | |
| 588 | got, _ := os.ReadFile(f) |
| 589 | // The file should still be GB18030-encoded (not silently converted to UTF-8). |
| 590 | dec, _ := simplifiedchinese.GB18030.NewDecoder().Bytes(got) |
| 591 | if string(dec) != "你好世界\n新的行\n" { |
| 592 | t.Errorf("after edit = %q (decoded)", dec) |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | func TestMultiEditGB18030RoundTrip(t *testing.T) { |
| 597 | f := filepath.Join(t.TempDir(), "gbk.txt") |
| 598 | original, _ := simplifiedchinese.GB18030.NewEncoder().String("package old\n\nfunc old() {\n\told()\n}\n") |
| 599 | os.WriteFile(f, []byte(original), 0o644) |
| 600 | |
| 601 | runTool(t, multiEdit{}, map[string]any{ |
| 602 | "path": f, |
| 603 | "edits": []map[string]any{ |
| 604 | {"old_string": "package old", "new_string": "package new"}, |
| 605 | {"old_string": "old", "new_string": "reasonix", "replace_all": true}, |
| 606 | }, |
| 607 | }) |
| 608 | |
| 609 | got, _ := os.ReadFile(f) |
| 610 | dec, _ := simplifiedchinese.GB18030.NewDecoder().Bytes(got) |
| 611 | want := "package new\n\nfunc reasonix() {\n\treasonix()\n}\n" |
| 612 | if string(dec) != want { |
| 613 | t.Errorf("after multi_edit = %q (decoded), want %q", dec, want) |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | func TestGrepGB18030(t *testing.T) { |
| 618 | dir := t.TempDir() |
| 619 | gb, _ := simplifiedchinese.GB18030.NewEncoder().String("你好世界\n包含函数的行\n") |
| 620 | os.WriteFile(filepath.Join(dir, "gbk.txt"), []byte(gb), 0o644) |
| 621 | |
| 622 | out := runTool(t, grepTool{}, map[string]any{"pattern": "函数", "path": dir}) |
| 623 | if !strings.Contains(out, "函数") { |
| 624 | t.Errorf("expected match in decoded GB18030 text, got:\n%s", out) |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | func TestGrepGB18030TruncationDoesNotLeakGoroutine(t *testing.T) { |
| 629 | defer goleak.VerifyNone(t, goleak.IgnoreCurrent()) |
| 630 | |
| 631 | var content strings.Builder |
| 632 | for range grepMaxMatches { |
| 633 | content.WriteString("命中\n") |
| 634 | } |
| 635 | content.WriteString(strings.Repeat("padding\n", 2000)) |
| 636 | gb, err := simplifiedchinese.GB18030.NewEncoder().String(content.String()) |
| 637 | if err != nil { |
| 638 | t.Fatalf("encode GB18030: %v", err) |
| 639 | } |
| 640 | |
| 641 | path := filepath.Join(t.TempDir(), "many-matches.gbk") |
| 642 | if err := os.WriteFile(path, []byte(gb), 0o644); err != nil { |
| 643 | t.Fatalf("write fixture: %v", err) |
| 644 | } |
| 645 | |
| 646 | out := runTool(t, grepTool{}, map[string]any{"pattern": "命中", "path": path}) |
| 647 | if got := strings.Count(out, ":命中"); got != grepMaxMatches { |
| 648 | t.Fatalf("matches = %d, want %d:\n%s", got, grepMaxMatches, out) |
| 649 | } |
| 650 | if !strings.Contains(out, "truncated at 200 matches") { |
| 651 | t.Fatalf("missing truncation marker:\n%s", out) |
| 652 | } |
| 653 | } |
| 654 |