| 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", "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, "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 | func TestReadFile(t *testing.T) { |
| 76 | dir := t.TempDir() |
| 77 | f := filepath.Join(dir, "src.go") |
| 78 | body := "package main\n\nfunc main() {}\n" |
| 79 | os.WriteFile(f, []byte(body), 0o644) |
| 80 | |
| 81 | out := runTool(t, readFile{}, map[string]any{"path": f}) |
| 82 | // Line numbers must be present, right-aligned, with the arrow separator. |
| 83 | for _, want := range []string{"1→package main", "2→", "3→func main"} { |
| 84 | if !strings.Contains(out, want) { |
| 85 | t.Errorf("missing %q in:\n%s", want, out) |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestReadFileDirectory(t *testing.T) { |
| 91 | dir := t.TempDir() |
| 92 | _, err := readFile{}.Execute(context.Background(), argsJSON(t, map[string]any{"path": dir})) |
| 93 | if err == nil { |
| 94 | t.Fatal("read_file on a directory should error, not return contents") |
| 95 | } |
| 96 | // The message must be actionable (point at ls) and not the doubled |
| 97 | // "read X: read X:" the raw scanner error produced. |
| 98 | if !strings.Contains(err.Error(), "directory") || !strings.Contains(err.Error(), "ls") { |
| 99 | t.Errorf("error should tell the model to use ls, got: %v", err) |
| 100 | } |
| 101 | if strings.Count(err.Error(), "read "+dir) > 1 { |
| 102 | t.Errorf("error is doubled: %v", err) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func TestReadFileOffsetLimit(t *testing.T) { |
| 107 | dir := t.TempDir() |
| 108 | f := filepath.Join(dir, "many.txt") |
| 109 | var b strings.Builder |
| 110 | for i := 1; i <= 50; i++ { |
| 111 | fmt.Fprintf(&b, "line %d\n", i) |
| 112 | } |
| 113 | os.WriteFile(f, []byte(b.String()), 0o644) |
| 114 | |
| 115 | out := runTool(t, readFile{}, map[string]any{"path": f, "offset": 10, "limit": 5}) |
| 116 | // Should see lines 11-15 only. |
| 117 | for _, want := range []string{"11→line 11", "15→line 15"} { |
| 118 | if !strings.Contains(out, want) { |
| 119 | t.Errorf("missing %q in:\n%s", want, out) |
| 120 | } |
| 121 | } |
| 122 | for _, leak := range []string{"line 5\n", "line 16\n", "line 20\n"} { |
| 123 | if strings.Contains(out, leak) { |
| 124 | t.Errorf("leaked %q (outside the slice)\n%s", leak, out) |
| 125 | } |
| 126 | } |
| 127 | // Trailer announces what's left so the model can paginate. |
| 128 | if !strings.Contains(out, "more line") || !strings.Contains(out, "offset=15") { |
| 129 | t.Errorf("pagination hint missing:\n%s", out) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | func TestReadFileBinary(t *testing.T) { |
| 134 | f := filepath.Join(t.TempDir(), "blob") |
| 135 | os.WriteFile(f, []byte{0x7f, 'E', 'L', 'F', 0, 0, 0}, 0o644) |
| 136 | |
| 137 | _, err := readFile{}.Execute(context.Background(), argsJSON(t, map[string]any{"path": f})) |
| 138 | if err == nil || !strings.Contains(err.Error(), "binary") { |
| 139 | t.Errorf("expected binary-file error, got %v", err) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | func TestReadFileBOM(t *testing.T) { |
| 144 | enc := func(order binary.ByteOrder, s string) []byte { |
| 145 | var b bytes.Buffer |
| 146 | if order == binary.LittleEndian { |
| 147 | b.Write([]byte{0xFF, 0xFE}) |
| 148 | } else { |
| 149 | b.Write([]byte{0xFE, 0xFF}) |
| 150 | } |
| 151 | for _, r := range utf16.Encode([]rune(s)) { |
| 152 | _ = binary.Write(&b, order, r) |
| 153 | } |
| 154 | return b.Bytes() |
| 155 | } |
| 156 | cases := map[string][]byte{ |
| 157 | "utf16le.txt": enc(binary.LittleEndian, "hello world\nsecond line"), |
| 158 | "utf16be.txt": enc(binary.BigEndian, "hello world\nsecond line"), |
| 159 | "utf8bom.txt": append([]byte{0xEF, 0xBB, 0xBF}, []byte("hello world\nsecond line")...), |
| 160 | } |
| 161 | for name, content := range cases { |
| 162 | f := filepath.Join(t.TempDir(), name) |
| 163 | os.WriteFile(f, content, 0o644) |
| 164 | out := runTool(t, readFile{}, map[string]any{"path": f}) |
| 165 | if !strings.Contains(out, "hello world") || !strings.Contains(out, "second line") { |
| 166 | t.Errorf("%s: expected decoded text, got %q", name, out) |
| 167 | } |
| 168 | if strings.Contains(out, "\ufeff") || strings.IndexByte(out, 0) >= 0 { |
| 169 | t.Errorf("%s: BOM/NUL leaked into output: %q", name, out) |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func TestReadFileEmpty(t *testing.T) { |
| 175 | f := filepath.Join(t.TempDir(), "empty.txt") |
| 176 | os.WriteFile(f, nil, 0o644) |
| 177 | if out := runTool(t, readFile{}, map[string]any{"path": f}); !strings.Contains(out, "empty") { |
| 178 | t.Errorf("empty file should report empty, got %q", out) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | func TestEditFile(t *testing.T) { |
| 183 | f := filepath.Join(t.TempDir(), "a.txt") |
| 184 | os.WriteFile(f, []byte("hello world\n"), 0o644) |
| 185 | |
| 186 | out := runTool(t, editFile{}, map[string]any{"path": f, "old_string": "world", "new_string": "reasonix"}) |
| 187 | for _, want := range []string{"Actual replacement receipt after write:", "-world", "+reasonix"} { |
| 188 | if !strings.Contains(out, want) { |
| 189 | t.Fatalf("edit result should contain %q in actual post-write receipt:\n%s", want, out) |
| 190 | } |
| 191 | } |
| 192 | if strings.Contains(out, "hello") { |
| 193 | t.Fatalf("edit receipt should not include unchanged same-line content:\n%s", out) |
| 194 | } |
| 195 | if b, _ := os.ReadFile(f); string(b) != "hello reasonix\n" { |
| 196 | t.Fatalf("after edit = %q", b) |
| 197 | } |
| 198 | |
| 199 | // Non-unique old_string must error and not modify the file. |
| 200 | os.WriteFile(f, []byte("x x x"), 0o644) |
| 201 | args := argsJSON(t, map[string]any{"path": f, "old_string": "x", "new_string": "y"}) |
| 202 | if _, err := (editFile{}).Execute(context.Background(), args); err == nil { |
| 203 | t.Fatal("expected not-unique error") |
| 204 | } else if !strings.Contains(err.Error(), "repeated separator lines") { |
| 205 | t.Fatalf("not-unique error should steer away from weak anchors, got: %v", err) |
| 206 | } |
| 207 | if b, _ := os.ReadFile(f); string(b) != "x x x" { |
| 208 | t.Fatalf("file modified despite error: %q", b) |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | func TestMultiEdit(t *testing.T) { |
| 213 | f := filepath.Join(t.TempDir(), "src.go") |
| 214 | body := "package old\n\nfunc old() {\n\told()\n}\n" |
| 215 | os.WriteFile(f, []byte(body), 0o644) |
| 216 | |
| 217 | // Two edits: rename the package (unique) then sweep every old → new. |
| 218 | out := runTool(t, multiEdit{}, map[string]any{ |
| 219 | "path": f, |
| 220 | "edits": []map[string]any{ |
| 221 | {"old_string": "package old", "new_string": "package new"}, |
| 222 | {"old_string": "old", "new_string": "reasonix", "replace_all": true}, |
| 223 | }, |
| 224 | }) |
| 225 | if !strings.Contains(out, "multi_edit") || !strings.Contains(out, "2 edits applied") { |
| 226 | t.Errorf("summary unexpected: %q", out) |
| 227 | } |
| 228 | for _, want := range []string{"Actual replacement receipt after write:", "-package old", "+package new", "-old", "+reasonix"} { |
| 229 | if !strings.Contains(out, want) { |
| 230 | t.Fatalf("multi_edit result should contain %q in actual post-write receipt:\n%s", want, out) |
| 231 | } |
| 232 | } |
| 233 | if strings.Contains(out, "func reasonix") { |
| 234 | t.Fatalf("multi_edit receipt should not include unchanged same-line content:\n%s", out) |
| 235 | } |
| 236 | got, _ := os.ReadFile(f) |
| 237 | want := "package new\n\nfunc reasonix() {\n\treasonix()\n}\n" |
| 238 | if string(got) != want { |
| 239 | t.Errorf("after multi_edit = %q\n want = %q", got, want) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // TestMultiEditAtomicity is the safety guarantee: if any edit fails, the file |
| 244 | // stays exactly as it was. A chained sequence of single edit_file calls would |
| 245 | // have left a half-written intermediate state. |
| 246 | func TestMultiEditAtomicity(t *testing.T) { |
| 247 | f := filepath.Join(t.TempDir(), "a.txt") |
| 248 | original := "alpha\nbeta\ngamma\n" |
| 249 | os.WriteFile(f, []byte(original), 0o644) |
| 250 | |
| 251 | args := argsJSON(t, map[string]any{ |
| 252 | "path": f, |
| 253 | "edits": []map[string]any{ |
| 254 | {"old_string": "alpha", "new_string": "ALPHA"}, |
| 255 | {"old_string": "no-such-text", "new_string": "x"}, |
| 256 | {"old_string": "gamma", "new_string": "GAMMA"}, |
| 257 | }, |
| 258 | }) |
| 259 | if _, err := (multiEdit{}).Execute(context.Background(), args); err == nil { |
| 260 | t.Fatal("expected failure on the missing edit") |
| 261 | } |
| 262 | got, _ := os.ReadFile(f) |
| 263 | if string(got) != original { |
| 264 | t.Errorf("file was modified despite failure:\n got %q\nwant %q", got, original) |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | func TestGrep(t *testing.T) { |
| 269 | dir := t.TempDir() |
| 270 | os.WriteFile(filepath.Join(dir, "a.go"), []byte("package main\nfunc Foo() {}\n"), 0o644) |
| 271 | os.WriteFile(filepath.Join(dir, "b.go"), []byte("var x = 1\n"), 0o644) |
| 272 | |
| 273 | out := runTool(t, grepTool{}, map[string]any{"pattern": "func ", "path": dir}) |
| 274 | if !strings.Contains(out, "Foo") || strings.Contains(out, "var x") { |
| 275 | t.Fatalf("grep result = %q", out) |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // TestWebFetchHTML serves a tiny HTML page and checks the reducer keeps the |
| 280 | // readable text while removing scripts, styles, and tags. |
| 281 | func TestWebFetchHTML(t *testing.T) { |
| 282 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 283 | w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 284 | _, _ = w.Write([]byte(`<!doctype html> |
| 285 | <html><head><title>T</title><style>body{color:red}</style></head> |
| 286 | <body> |
| 287 | <h1>Hello & world</h1> |
| 288 | <script>alert("bad")</script> |
| 289 | <p>Visible text.</p> |
| 290 | </body></html>`)) |
| 291 | })) |
| 292 | defer srv.Close() |
| 293 | |
| 294 | out := runTool(t, webFetch{}, map[string]any{"url": srv.URL}) |
| 295 | for _, want := range []string{"Hello & world", "Visible text", "text/html"} { |
| 296 | if !strings.Contains(out, want) { |
| 297 | t.Errorf("missing %q in:\n%s", want, out) |
| 298 | } |
| 299 | } |
| 300 | for _, leak := range []string{"<script", "alert(", "<style", "<h1>", "&"} { |
| 301 | if strings.Contains(out, leak) { |
| 302 | t.Errorf("leaked raw HTML/script %q", leak) |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | func TestWebFetchHTMLTokenizerHandlesAttributesAndEntities(t *testing.T) { |
| 308 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 309 | w.Header().Set("Content-Type", "text/html") |
| 310 | _, _ = w.Write([]byte(`<html><body><p title="1 > 0">Tom's docs</p><script>visible = false</script><p>Next</p></body></html>`)) |
| 311 | })) |
| 312 | defer srv.Close() |
| 313 | |
| 314 | out := runTool(t, webFetch{}, map[string]any{"url": srv.URL}) |
| 315 | for _, want := range []string{"Tom's docs", "Next"} { |
| 316 | if !strings.Contains(out, want) { |
| 317 | t.Fatalf("missing %q in:\n%s", want, out) |
| 318 | } |
| 319 | } |
| 320 | if strings.Contains(out, "visible = false") || strings.Contains(out, "title=") || strings.Contains(out, "'") { |
| 321 | t.Fatalf("HTML tokenizer leaked markup/script/entity:\n%s", out) |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | func TestWebFetchHTMLStructuredText(t *testing.T) { |
| 326 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 327 | w.Header().Set("Content-Type", "text/html") |
| 328 | _, _ = w.Write([]byte(`<html><head><title>Doc title</title></head><body> |
| 329 | <h1>Main</h1> |
| 330 | <p>Read the <a href="/guide?a=1&b=2">guide</a>.</p> |
| 331 | <ul><li>First</li><li>Second</li></ul> |
| 332 | <pre>go test ./... |
| 333 | line two</pre> |
| 334 | <table><tr><th>Name</th><th>Value</th></tr><tr><td>A</td><td>42</td></tr></table> |
| 335 | </body></html>`)) |
| 336 | })) |
| 337 | defer srv.Close() |
| 338 | |
| 339 | out := runTool(t, webFetch{}, map[string]any{"url": srv.URL}) |
| 340 | for _, want := range []string{ |
| 341 | "# Doc title", |
| 342 | "# Main", |
| 343 | "guide (/guide?a=1&b=2)", |
| 344 | "- First", |
| 345 | "- Second", |
| 346 | "```\ngo test ./...\nline two\n```", |
| 347 | "Name | Value", |
| 348 | "A | 42", |
| 349 | } { |
| 350 | if !strings.Contains(out, want) { |
| 351 | t.Fatalf("structured HTML output missing %q:\n%s", want, out) |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // TestWebFetchPlain confirms non-HTML bodies pass through untouched (apart |
| 357 | // from the prepended status header). |
| 358 | func TestWebFetchPlain(t *testing.T) { |
| 359 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 360 | w.Header().Set("Content-Type", "text/plain") |
| 361 | _, _ = w.Write([]byte("line1\nline2\n")) |
| 362 | })) |
| 363 | defer srv.Close() |
| 364 | |
| 365 | out := runTool(t, webFetch{}, map[string]any{"url": srv.URL}) |
| 366 | if !strings.Contains(out, "line1") || !strings.Contains(out, "line2") { |
| 367 | t.Errorf("plain body content missing:\n%s", out) |
| 368 | } |
| 369 | if strings.Contains(out, "<") { |
| 370 | t.Errorf("html reducer ran on text/plain: %s", out) |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | // TestWebFetchSchemeRejected blocks anything that's not http(s) so the tool |
| 375 | // can't be tricked into reading file:// or arbitrary URI schemes. |
| 376 | func TestWebFetchSchemeRejected(t *testing.T) { |
| 377 | _, err := webFetch{}.Execute(context.Background(), argsJSON(t, map[string]any{"url": "file:///etc/passwd"})) |
| 378 | if err == nil || !strings.Contains(err.Error(), "http(s)") { |
| 379 | t.Errorf("expected scheme rejection, got %v", err) |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | func TestLsAndGlob(t *testing.T) { |
| 384 | dir := t.TempDir() |
| 385 | os.WriteFile(filepath.Join(dir, "x.txt"), []byte("hi"), 0o644) |
| 386 | os.Mkdir(filepath.Join(dir, "sub"), 0o755) |
| 387 | |
| 388 | ls := runTool(t, listDir{}, map[string]any{"path": dir}) |
| 389 | if !strings.Contains(ls, "x.txt") || !strings.Contains(ls, "sub/") { |
| 390 | t.Fatalf("ls result = %q", ls) |
| 391 | } |
| 392 | |
| 393 | g := runTool(t, globTool{}, map[string]any{"pattern": filepath.Join(dir, "*.txt")}) |
| 394 | if !strings.Contains(g, "x.txt") { |
| 395 | t.Fatalf("glob result = %q", g) |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | func TestGlobRecursive(t *testing.T) { |
| 400 | dir := t.TempDir() |
| 401 | // Create a nested structure: |
| 402 | // dir/a.go |
| 403 | // dir/sub/b.go |
| 404 | // dir/sub/deep/c.go |
| 405 | // dir/sub/deep/c.txt |
| 406 | // dir/other.txt |
| 407 | os.WriteFile(filepath.Join(dir, "a.go"), []byte("package a"), 0o644) |
| 408 | os.MkdirAll(filepath.Join(dir, "sub", "deep"), 0o755) |
| 409 | os.WriteFile(filepath.Join(dir, "sub", "b.go"), []byte("package b"), 0o644) |
| 410 | os.WriteFile(filepath.Join(dir, "sub", "deep", "c.go"), []byte("package c"), 0o644) |
| 411 | os.WriteFile(filepath.Join(dir, "sub", "deep", "c.txt"), []byte("text"), 0o644) |
| 412 | os.WriteFile(filepath.Join(dir, "other.txt"), []byte("other"), 0o644) |
| 413 | |
| 414 | // ** *.go should find all .go files recursively. |
| 415 | out := runTool(t, globTool{}, map[string]any{"pattern": filepath.Join(dir, "**", "*.go")}) |
| 416 | if !strings.Contains(out, "a.go") { |
| 417 | t.Errorf("missing a.go in:\n%s", out) |
| 418 | } |
| 419 | if !strings.Contains(out, "b.go") { |
| 420 | t.Errorf("missing b.go in:\n%s", out) |
| 421 | } |
| 422 | if !strings.Contains(out, "c.go") { |
| 423 | t.Errorf("missing c.go in:\n%s", out) |
| 424 | } |
| 425 | // Should not include .txt files. |
| 426 | if strings.Contains(out, "other.txt") || strings.Contains(out, "c.txt") { |
| 427 | t.Errorf("should not include .txt files:\n%s", out) |
| 428 | } |
| 429 | |
| 430 | // ** *.txt should find all .txt files recursively. |
| 431 | out2 := runTool(t, globTool{}, map[string]any{"pattern": filepath.Join(dir, "**", "*.txt")}) |
| 432 | if !strings.Contains(out2, "other.txt") { |
| 433 | t.Errorf("missing other.txt in:\n%s", out2) |
| 434 | } |
| 435 | if !strings.Contains(out2, "c.txt") { |
| 436 | t.Errorf("missing c.txt in:\n%s", out2) |
| 437 | } |
| 438 | |
| 439 | // ** with no suffix should find all files. |
| 440 | out3 := runTool(t, globTool{}, map[string]any{"pattern": filepath.Join(dir, "**")}) |
| 441 | if !strings.Contains(out3, "a.go") || !strings.Contains(out3, "c.txt") { |
| 442 | t.Errorf("bare ** should find all files:\n%s", out3) |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | func TestGlobForwardSlashPattern(t *testing.T) { |
| 447 | dir := t.TempDir() |
| 448 | os.MkdirAll(filepath.Join(dir, "sub", "deep"), 0o755) |
| 449 | os.WriteFile(filepath.Join(dir, "top.txt"), []byte("x"), 0o644) |
| 450 | os.WriteFile(filepath.Join(dir, "sub", "deep", "nested.txt"), []byte("y"), 0o644) |
| 451 | t.Chdir(dir) |
| 452 | |
| 453 | out := runTool(t, globTool{}, map[string]any{"pattern": "**/*.txt"}) |
| 454 | if !strings.Contains(out, "top.txt") || !strings.Contains(out, "nested.txt") { |
| 455 | t.Errorf("forward-slash recursive pattern should match every .txt:\n%s", out) |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | func TestGlobRecursiveDoublestarBracePattern(t *testing.T) { |
| 460 | dir := t.TempDir() |
| 461 | os.WriteFile(filepath.Join(dir, "a.go"), []byte("go"), 0o644) |
| 462 | os.WriteFile(filepath.Join(dir, "b.txt"), []byte("txt"), 0o644) |
| 463 | os.WriteFile(filepath.Join(dir, "c.md"), []byte("md"), 0o644) |
| 464 | |
| 465 | out := runTool(t, globTool{}, map[string]any{"pattern": filepath.Join(dir, "**", "*.{go,txt}")}) |
| 466 | if !strings.Contains(out, "a.go") || !strings.Contains(out, "b.txt") { |
| 467 | t.Fatalf("brace pattern should match go and txt:\n%s", out) |
| 468 | } |
| 469 | if strings.Contains(out, "c.md") { |
| 470 | t.Fatalf("brace pattern should not match markdown:\n%s", out) |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | func TestGlobRecursiveNoMatches(t *testing.T) { |
| 475 | dir := t.TempDir() |
| 476 | os.MkdirAll(filepath.Join(dir, "sub"), 0o755) |
| 477 | os.WriteFile(filepath.Join(dir, "sub", "a.go"), []byte("package a"), 0o644) |
| 478 | |
| 479 | out := runTool(t, globTool{}, map[string]any{"pattern": filepath.Join(dir, "**", "*.py")}) |
| 480 | if !strings.Contains(out, "(no matches)") { |
| 481 | t.Errorf("expected (no matches), got:\n%s", out) |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | func TestGlobNoMatches(t *testing.T) { |
| 486 | dir := t.TempDir() |
| 487 | out := runTool(t, globTool{}, map[string]any{"pattern": filepath.Join(dir, "*.xyz")}) |
| 488 | if !strings.Contains(out, "(no matches)") { |
| 489 | t.Errorf("expected (no matches), got:\n%s", out) |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | // --- GB18030 encoding integration tests (issue #2637) --- |
| 494 | |
| 495 | func TestReadFileGB18030(t *testing.T) { |
| 496 | f := filepath.Join(t.TempDir(), "gbk.txt") |
| 497 | gb, err := simplifiedchinese.GB18030.NewEncoder().String("你好世界\n第二行") |
| 498 | if err != nil { |
| 499 | t.Fatalf("encode: %v", err) |
| 500 | } |
| 501 | os.WriteFile(f, []byte(gb), 0o644) |
| 502 | |
| 503 | out := runTool(t, readFile{}, map[string]any{"path": f}) |
| 504 | if !strings.Contains(out, "你好世界") || !strings.Contains(out, "第二行") { |
| 505 | t.Errorf("expected decoded Chinese text, got:\n%s", out) |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | func TestEditFileGB18030RoundTrip(t *testing.T) { |
| 510 | f := filepath.Join(t.TempDir(), "gbk.txt") |
| 511 | original, _ := simplifiedchinese.GB18030.NewEncoder().String("你好世界\n第二行\n") |
| 512 | os.WriteFile(f, []byte(original), 0o644) |
| 513 | |
| 514 | runTool(t, editFile{}, map[string]any{ |
| 515 | "path": f, |
| 516 | "old_string": "第二行", |
| 517 | "new_string": "新的行", |
| 518 | }) |
| 519 | |
| 520 | got, _ := os.ReadFile(f) |
| 521 | // The file should still be GB18030-encoded (not silently converted to UTF-8). |
| 522 | dec, _ := simplifiedchinese.GB18030.NewDecoder().Bytes(got) |
| 523 | if string(dec) != "你好世界\n新的行\n" { |
| 524 | t.Errorf("after edit = %q (decoded)", dec) |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | func TestMultiEditGB18030RoundTrip(t *testing.T) { |
| 529 | f := filepath.Join(t.TempDir(), "gbk.txt") |
| 530 | original, _ := simplifiedchinese.GB18030.NewEncoder().String("package old\n\nfunc old() {\n\told()\n}\n") |
| 531 | os.WriteFile(f, []byte(original), 0o644) |
| 532 | |
| 533 | runTool(t, multiEdit{}, map[string]any{ |
| 534 | "path": f, |
| 535 | "edits": []map[string]any{ |
| 536 | {"old_string": "package old", "new_string": "package new"}, |
| 537 | {"old_string": "old", "new_string": "reasonix", "replace_all": true}, |
| 538 | }, |
| 539 | }) |
| 540 | |
| 541 | got, _ := os.ReadFile(f) |
| 542 | dec, _ := simplifiedchinese.GB18030.NewDecoder().Bytes(got) |
| 543 | want := "package new\n\nfunc reasonix() {\n\treasonix()\n}\n" |
| 544 | if string(dec) != want { |
| 545 | t.Errorf("after multi_edit = %q (decoded), want %q", dec, want) |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | func TestGrepGB18030(t *testing.T) { |
| 550 | dir := t.TempDir() |
| 551 | gb, _ := simplifiedchinese.GB18030.NewEncoder().String("你好世界\n包含函数的行\n") |
| 552 | os.WriteFile(filepath.Join(dir, "gbk.txt"), []byte(gb), 0o644) |
| 553 | |
| 554 | out := runTool(t, grepTool{}, map[string]any{"pattern": "函数", "path": dir}) |
| 555 | if !strings.Contains(out, "函数") { |
| 556 | t.Errorf("expected match in decoded GB18030 text, got:\n%s", out) |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | func TestGrepGB18030TruncationDoesNotLeakGoroutine(t *testing.T) { |
| 561 | defer goleak.VerifyNone(t, goleak.IgnoreCurrent()) |
| 562 | |
| 563 | var content strings.Builder |
| 564 | for range grepMaxMatches { |
| 565 | content.WriteString("命中\n") |
| 566 | } |
| 567 | content.WriteString(strings.Repeat("padding\n", 2000)) |
| 568 | gb, err := simplifiedchinese.GB18030.NewEncoder().String(content.String()) |
| 569 | if err != nil { |
| 570 | t.Fatalf("encode GB18030: %v", err) |
| 571 | } |
| 572 | |
| 573 | path := filepath.Join(t.TempDir(), "many-matches.gbk") |
| 574 | if err := os.WriteFile(path, []byte(gb), 0o644); err != nil { |
| 575 | t.Fatalf("write fixture: %v", err) |
| 576 | } |
| 577 | |
| 578 | out := runTool(t, grepTool{}, map[string]any{"pattern": "命中", "path": path}) |
| 579 | if got := strings.Count(out, ":命中"); got != grepMaxMatches { |
| 580 | t.Fatalf("matches = %d, want %d:\n%s", got, grepMaxMatches, out) |
| 581 | } |
| 582 | if !strings.Contains(out, "truncated at 200 matches") { |
| 583 | t.Fatalf("missing truncation marker:\n%s", out) |
| 584 | } |
| 585 | } |
| 586 |