返回 DeepSeek-Reasonix
grep_engine_test.go
根目录 / internal / tool / builtin / grep_engine_test.go
1 package builtin
2
3 import (
4 "bytes"
5 "context"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "strings"
10 "testing"
11 "time"
12
13 fileenc "reasonix/internal/fileutil/encoding"
14 "reasonix/internal/sandbox"
15 )
16
17 func TestGrepTimeoutClamp(t *testing.T) {
18 cases := []struct {
19 sec int
20 want time.Duration
21 }{
22 {0, grepDefaultTimeout},
23 {-5, grepDefaultTimeout},
24 {5, 5 * time.Second},
25 {99999, grepMaxTimeout},
26 }
27 for _, c := range cases {
28 if got := grepTimeout(c.sec); got != c.want {
29 t.Errorf("grepTimeout(%d) = %v, want %v", c.sec, got, c.want)
30 }
31 }
32 }
33
34 func TestGrepTimeoutPreservesPartialResults(t *testing.T) {
35 ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
36 defer cancel()
37 <-ctx.Done()
38
39 hit := []string{"a.go:1:match"}
40 got := formatGrep(ctx, hit, false, 30*time.Second)
41 if !strings.HasPrefix(got, "a.go:1:match") {
42 t.Errorf("timeout must keep the matches found so far, got %q", got)
43 }
44 if !strings.Contains(got, "timed out") || !strings.Contains(got, "timeout_seconds") {
45 t.Errorf("timeout result must flag the cutoff and point at timeout_seconds, got %q", got)
46 }
47
48 if got := formatGrep(ctx, nil, false, 30*time.Second); !strings.Contains(got, "timed out") {
49 t.Errorf("a zero-match timeout must report the timeout, not (no matches), got %q", got)
50 }
51
52 done := context.Background()
53 if got := formatGrep(done, nil, false, 30*time.Second); got != "(no matches)" {
54 t.Errorf("a completed zero-match search = %q, want (no matches)", got)
55 }
56 }
57
58 func TestResolveSearch(t *testing.T) {
59 rgFile := filepath.Join(t.TempDir(), "rg")
60 if err := os.WriteFile(rgFile, []byte("x"), 0o755); err != nil {
61 t.Fatal(err)
62 }
63 missing := filepath.Join(t.TempDir(), "absent")
64
65 if got := ResolveSearch("native", rgFile, nil); got.RgPath != "" {
66 t.Fatalf("native must ignore ripgrep, got %q", got.RgPath)
67 }
68 if got := ResolveSearch("rg", rgFile, nil); got.RgPath != rgFile {
69 t.Fatalf(`engine "rg" with an explicit path = %q, want %q`, got.RgPath, rgFile)
70 }
71 if got := ResolveSearch("auto", rgFile, nil); got.RgPath != rgFile {
72 t.Fatalf(`engine "auto" with an explicit path = %q, want %q`, got.RgPath, rgFile)
73 }
74
75 var warn bytes.Buffer
76 if got := ResolveSearch("rg", missing, &warn); got.RgPath != "" {
77 t.Fatalf(`engine "rg" with a missing binary must fall back to native, got %q`, got.RgPath)
78 }
79 if !strings.Contains(warn.String(), "ripgrep") {
80 t.Fatalf("expected a fall-back warning mentioning ripgrep, got %q", warn.String())
81 }
82 }
83
84 func TestConfineSearch(t *testing.T) {
85 g, ok := ConfineSearch(SearchSpec{RgPath: "/path/to/rg"}, sandbox.Spec{}, nil).(grepTool)
86 if !ok || g.rg != "/path/to/rg" {
87 t.Fatalf("ConfineSearch must bind the rg path, got %+v ok=%v", g, ok)
88 }
89 }
90
91 func TestGrepRipgrepEngine(t *testing.T) {
92 rg, err := exec.LookPath("rg")
93 if err != nil {
94 t.Skip("ripgrep not installed")
95 }
96 dir := t.TempDir()
97 if err := os.WriteFile(filepath.Join(dir, "a.txt"), []byte("alpha\nBETA needle here\ngamma\n"), 0o644); err != nil {
98 t.Fatal(err)
99 }
100 g := grepTool{rg: rg}
101
102 out := runTool(t, g, map[string]any{"pattern": "needle", "path": dir})
103 if !strings.Contains(out, "a.txt:2:BETA needle here") {
104 t.Fatalf("ripgrep output = %q, want path:line:text with the match", out)
105 }
106
107 if out := runTool(t, g, map[string]any{"pattern": "zzz_absent_token", "path": dir}); out != "(no matches)" {
108 t.Fatalf("no-match search = %q, want (no matches)", out)
109 }
110
111 if _, err := g.Execute(context.Background(), argsJSON(t, map[string]any{"pattern": "(unclosed", "path": dir})); err == nil {
112 t.Fatal("an invalid regex must surface ripgrep's error")
113 }
114 }
115
116 func TestGrepRipgrepFallsBackWhenForbidReadIsNotSandboxed(t *testing.T) {
117 rg, err := exec.LookPath("rg")
118 if err != nil {
119 t.Skip("ripgrep not installed")
120 }
121 root := t.TempDir()
122 forbidDir := filepath.Join(root, "secret")
123 if err := os.MkdirAll(forbidDir, 0o755); err != nil {
124 t.Fatal(err)
125 }
126 if err := os.WriteFile(filepath.Join(root, "allowed.txt"), []byte("needle allowed\n"), 0o644); err != nil {
127 t.Fatal(err)
128 }
129 if err := os.WriteFile(filepath.Join(forbidDir, "secret.txt"), []byte("needle secret\n"), 0o644); err != nil {
130 t.Fatal(err)
131 }
132
133 g := grepTool{workDir: root, rg: rg, forbidRoots: realRoots([]string{forbidDir}), sb: sandbox.Spec{Mode: "off"}}
134 out := runTool(t, g, map[string]any{"pattern": "needle", "path": "."})
135 if !strings.Contains(out, "allowed.txt") {
136 t.Fatalf("fallback grep should still find allowed matches, got:\n%s", out)
137 }
138 if strings.Contains(out, "secret") {
139 t.Fatalf("fallback grep leaked forbidden matches:\n%s", out)
140 }
141 }
142
143 func TestGrepNativeStreamsUTF16WithAndWithoutBOM(t *testing.T) {
144 content := strings.Repeat("ordinary line\n", 20000) + "needle UTF16-END\n"
145 cases := []struct {
146 name string
147 kind fileenc.Kind
148 }{
149 {"le-bom", fileenc.UTF16LE}, {"be-bom", fileenc.UTF16BE},
150 {"le-no-bom", fileenc.UTF16LENoBOM}, {"be-no-bom", fileenc.UTF16BENoBOM},
151 }
152 for _, tc := range cases {
153 t.Run(tc.name, func(t *testing.T) {
154 path := filepath.Join(t.TempDir(), "windows-utf16.txt")
155 if err := os.WriteFile(path, fileenc.Encode(content, tc.kind), 0o644); err != nil {
156 t.Fatal(err)
157 }
158 out := runTool(t, grepTool{}, map[string]any{"pattern": "UTF16-END", "path": path})
159 if !strings.Contains(out, "20001:needle UTF16-END") || strings.Contains(out, "\x00") {
160 t.Fatalf("native UTF-16 grep output=%q", out)
161 }
162 })
163 }
164 }
165
165 lines GO