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