Skip to content

Commit a55c0b0

Browse files
committed
Add tests
Signed-off-by: David Gageot <david.gageot@docker.com>
1 parent 51ac5a5 commit a55c0b0

1 file changed

Lines changed: 206 additions & 0 deletions

File tree

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
package tool
2+
3+
import (
4+
"testing"
5+
6+
"github.com/docker/cagent/pkg/tools"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestRender_search_files(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
toolCall tools.ToolCall
14+
expected string
15+
}{
16+
{
17+
name: "pattern only",
18+
toolCall: tools.ToolCall{
19+
Function: tools.FunctionCall{
20+
Arguments: `{"pattern": "*.go"}`,
21+
},
22+
},
23+
expected: "*.go",
24+
},
25+
{
26+
name: "empty pattern",
27+
toolCall: tools.ToolCall{
28+
Function: tools.FunctionCall{
29+
Arguments: `{"pattern": ""}`,
30+
},
31+
},
32+
expected: "",
33+
},
34+
{
35+
name: "pattern with valid path",
36+
toolCall: tools.ToolCall{
37+
Function: tools.FunctionCall{
38+
Arguments: `{"pattern": "*.go", "path": "/src/app"}`,
39+
},
40+
},
41+
expected: "*.go in /src/app",
42+
},
43+
{
44+
name: "pattern with current directory path",
45+
toolCall: tools.ToolCall{
46+
Function: tools.FunctionCall{
47+
Arguments: `{"pattern": "*.go", "path": "."}`,
48+
},
49+
},
50+
expected: "*.go",
51+
},
52+
{
53+
name: "pattern with empty path",
54+
toolCall: tools.ToolCall{
55+
Function: tools.FunctionCall{
56+
Arguments: `{"pattern": "*.go", "path": ""}`,
57+
},
58+
},
59+
expected: "*.go",
60+
},
61+
{
62+
name: "pattern with relative path",
63+
toolCall: tools.ToolCall{
64+
Function: tools.FunctionCall{
65+
Arguments: `{"pattern": "test_*.py", "path": "tests/unit"}`,
66+
},
67+
},
68+
expected: "test_*.py in tests/unit",
69+
},
70+
{
71+
name: "pattern with single exclude",
72+
toolCall: tools.ToolCall{
73+
Function: tools.FunctionCall{
74+
Arguments: `{"pattern": "*.go", "excludePatterns": ["*_test.go"]}`,
75+
},
76+
},
77+
expected: "*.go excluding [*_test.go]",
78+
},
79+
{
80+
name: "pattern with multiple excludes",
81+
toolCall: tools.ToolCall{
82+
Function: tools.FunctionCall{
83+
Arguments: `{"pattern": "*.js", "excludePatterns": ["node_modules", "*.min.js", "dist"]}`,
84+
},
85+
},
86+
expected: "*.js excluding [node_modules, *.min.js, dist]",
87+
},
88+
{
89+
name: "pattern with empty exclude patterns array",
90+
toolCall: tools.ToolCall{
91+
Function: tools.FunctionCall{
92+
Arguments: `{"pattern": "*.py", "excludePatterns": []}`,
93+
},
94+
},
95+
expected: "*.py",
96+
},
97+
{
98+
name: "all fields present",
99+
toolCall: tools.ToolCall{
100+
Function: tools.FunctionCall{
101+
Arguments: `{"pattern": "*.java", "path": "src/main/java", "excludePatterns": ["*Test.java", "*Mock.java"]}`,
102+
},
103+
},
104+
expected: "*.java in src/main/java excluding [*Test.java, *Mock.java]",
105+
},
106+
{
107+
name: "all fields with current directory",
108+
toolCall: tools.ToolCall{
109+
Function: tools.FunctionCall{
110+
Arguments: `{"pattern": "README*", "path": ".", "excludePatterns": ["*.backup"]}`,
111+
},
112+
},
113+
expected: "README* excluding [*.backup]",
114+
},
115+
{
116+
name: "all fields with empty path",
117+
toolCall: tools.ToolCall{
118+
Function: tools.FunctionCall{
119+
Arguments: `{"pattern": "config.*", "path": "", "excludePatterns": ["*.bak", "*.tmp"]}`,
120+
},
121+
},
122+
expected: "config.* excluding [*.bak, *.tmp]",
123+
},
124+
{
125+
name: "pattern with spaces",
126+
toolCall: tools.ToolCall{
127+
Function: tools.FunctionCall{
128+
Arguments: `{"pattern": "test file.txt"}`,
129+
},
130+
},
131+
expected: "test file.txt",
132+
},
133+
{
134+
name: "pattern with special regex characters",
135+
toolCall: tools.ToolCall{
136+
Function: tools.FunctionCall{
137+
Arguments: `{"pattern": "file[0-9]+\\.txt"}`,
138+
},
139+
},
140+
expected: "file[0-9]+\\.txt",
141+
},
142+
{
143+
name: "path with spaces and special characters",
144+
toolCall: tools.ToolCall{
145+
Function: tools.FunctionCall{
146+
Arguments: `{"pattern": "*.log", "path": "/var/log/my app/data"}`,
147+
},
148+
},
149+
expected: "*.log in /var/log/my app/data",
150+
},
151+
{
152+
name: "exclude patterns with special characters",
153+
toolCall: tools.ToolCall{
154+
Function: tools.FunctionCall{
155+
Arguments: `{"pattern": "*", "excludePatterns": ["*.~", "#*#", ".#*"]}`,
156+
},
157+
},
158+
expected: "* excluding [*.~, #*#, .#*]",
159+
},
160+
{
161+
name: "invalid JSON",
162+
toolCall: tools.ToolCall{
163+
Function: tools.FunctionCall{
164+
Arguments: `{invalid json}`,
165+
},
166+
},
167+
expected: "",
168+
},
169+
{
170+
name: "empty arguments",
171+
toolCall: tools.ToolCall{
172+
Function: tools.FunctionCall{
173+
Arguments: ``,
174+
},
175+
},
176+
expected: "",
177+
},
178+
{
179+
name: "search for documentation files",
180+
toolCall: tools.ToolCall{
181+
Function: tools.FunctionCall{
182+
Arguments: `{"pattern": "*.md", "path": "docs", "excludePatterns": ["node_modules", ".git"]}`,
183+
},
184+
},
185+
expected: "*.md in docs excluding [node_modules, .git]",
186+
},
187+
{
188+
name: "search in deep directory structure",
189+
toolCall: tools.ToolCall{
190+
Function: tools.FunctionCall{
191+
Arguments: `{"pattern": "component_*.tsx", "path": "src/components/ui/buttons/primary", "excludePatterns": ["*.stories.tsx", "*.test.tsx", "*.spec.tsx"]}`,
192+
},
193+
},
194+
expected: "component_*.tsx in src/components/ui/buttons/primary excluding [*.stories.tsx, *.test.tsx, *.spec.tsx]",
195+
},
196+
}
197+
for _, tt := range tests {
198+
t.Run(tt.name, func(t *testing.T) {
199+
t.Parallel()
200+
201+
result := render_search_files(tt.toolCall)
202+
203+
assert.Equal(t, tt.expected, result)
204+
})
205+
}
206+
}

0 commit comments

Comments
 (0)