@@ -1,457 +0,0 @@
11− "github.com/stretchr/testify/assert"
12− "github.com/stretchr/testify/require"
15−func TestLsTool_Info(t *testing.T) {
19− assert.Equal(t, LSToolName, info.Name)
20− assert.NotEmpty(t, info.Description)
21− assert.Contains(t, info.Parameters, "path")
22− assert.Contains(t, info.Parameters, "ignore")
23− assert.Contains(t, info.Required, "path")
26−func TestLsTool_Run(t *testing.T) {
27− // Create a temporary directory for testing
28− tempDir, err := os.MkdirTemp("", "ls_tool_test")
29− require.NoError(t, err)
30− defer os.RemoveAll(tempDir)
32− // Create a test directory structure
33− testDirs := []string{
43− testFiles := []string{
48− "dir2/subdir1/file5.txt",
49− "dir2/subdir2/file6.txt",
51− "dir3/.hidden_file.txt",
52− "__pycache__/cache.pyc",
53− ".hidden_root_file.txt",
56− // Create directories
57− for _, dir := range testDirs {
58− dirPath := filepath.Join(tempDir, dir)
59− err := os.MkdirAll(dirPath, 0o755)
60− require.NoError(t, err)
64− for _, file := range testFiles {
65− filePath := filepath.Join(tempDir, file)
66− err := os.WriteFile(filePath, []byte("test content"), 0o644)
67− require.NoError(t, err)
70− t.Run("lists directory successfully", func(t *testing.T) {
76− paramsJSON, err := json.Marshal(params)
77− require.NoError(t, err)
81− Input: string(paramsJSON),
84− response, err := tool.Run(context.Background(), call)
85− require.NoError(t, err)
87− // Check that visible directories and files are included
88− assert.Contains(t, response.Content, "dir1")
89− assert.Contains(t, response.Content, "dir2")
90− assert.Contains(t, response.Content, "dir3")
91− assert.Contains(t, response.Content, "file1.txt")
92− assert.Contains(t, response.Content, "file2.txt")
94− // Check that hidden files and directories are not included
95− assert.NotContains(t, response.Content, ".hidden_dir")
96− assert.NotContains(t, response.Content, ".hidden_file.txt")
97− assert.NotContains(t, response.Content, ".hidden_root_file.txt")
99− // Check that __pycache__ is not included
100− assert.NotContains(t, response.Content, "__pycache__")
103− t.Run("handles non-existent path", func(t *testing.T) {
106− Path: filepath.Join(tempDir, "non_existent_dir"),
109− paramsJSON, err := json.Marshal(params)
110− require.NoError(t, err)
114− Input: string(paramsJSON),
117− response, err := tool.Run(context.Background(), call)
118− require.NoError(t, err)
119− assert.Contains(t, response.Content, "path does not exist")
122− t.Run("handles empty path parameter", func(t *testing.T) {
123− // For this test, we need to mock the config.WorkingDirectory function
124− // Since we can't easily do that, we'll just check that the response doesn't contain an error message
131− paramsJSON, err := json.Marshal(params)
132− require.NoError(t, err)
136− Input: string(paramsJSON),
139− response, err := tool.Run(context.Background(), call)
140− require.NoError(t, err)
142− // The response should either contain a valid directory listing or an error
143− // We'll just check that it's not empty
144− assert.NotEmpty(t, response.Content)
147− t.Run("handles invalid parameters", func(t *testing.T) {
151− Input: "invalid json",
154− response, err := tool.Run(context.Background(), call)
155− require.NoError(t, err)
156− assert.Contains(t, response.Content, "error parsing parameters")
159− t.Run("respects ignore patterns", func(t *testing.T) {
163− Ignore: []string{"file1.txt", "dir1"},
166− paramsJSON, err := json.Marshal(params)
167− require.NoError(t, err)
171− Input: string(paramsJSON),
174− response, err := tool.Run(context.Background(), call)
175− require.NoError(t, err)
177− // The output format is a tree, so we need to check for specific patterns
178− // Check that file1.txt is not directly mentioned
179− assert.NotContains(t, response.Content, "- file1.txt")
181− // Check that dir1/ is not directly mentioned
182− assert.NotContains(t, response.Content, "- dir1/")
185− t.Run("handles relative path", func(t *testing.T) {
186− // Save original working directory
187− origWd, err := os.Getwd()
188− require.NoError(t, err)
193− // Change to a directory above the temp directory
194− parentDir := filepath.Dir(tempDir)
195− err = os.Chdir(parentDir)
196− require.NoError(t, err)
200− Path: filepath.Base(tempDir),
203− paramsJSON, err := json.Marshal(params)
204− require.NoError(t, err)
208− Input: string(paramsJSON),
211− response, err := tool.Run(context.Background(), call)
212− require.NoError(t, err)
214− // Should list the temp directory contents
215− assert.Contains(t, response.Content, "dir1")
216− assert.Contains(t, response.Content, "file1.txt")
220−func TestShouldSkip(t *testing.T) {
221− testCases := []struct {
224− ignorePatterns []string
228− name: "hidden file",
229− path: "/path/to/.hidden_file",
230− ignorePatterns: []string{},
234− name: "hidden directory",
235− path: "/path/to/.hidden_dir",
236− ignorePatterns: []string{},
240− name: "pycache directory",
241− path: "/path/to/__pycache__/file.pyc",
242− ignorePatterns: []string{},
246− name: "node_modules directory",
247− path: "/path/to/node_modules/package",
248− ignorePatterns: []string{},
249− expected: false, // The shouldSkip function doesn't directly check for node_modules in the path
252− name: "normal file",
253− path: "/path/to/normal_file.txt",
254− ignorePatterns: []string{},
258− name: "normal directory",
259− path: "/path/to/normal_dir",
260− ignorePatterns: []string{},
264− name: "ignored by pattern",
265− path: "/path/to/ignore_me.txt",
266− ignorePatterns: []string{"ignore_*.txt"},
270− name: "not ignored by pattern",
271− path: "/path/to/keep_me.txt",
272− ignorePatterns: []string{"ignore_*.txt"},
277− for _, tc := range testCases {
278− t.Run(tc.name, func(t *testing.T) {
279− result := shouldSkip(tc.path, tc.ignorePatterns)
280− assert.Equal(t, tc.expected, result)
285−func TestCreateFileTree(t *testing.T) {
287− "/path/to/file1.txt",
288− "/path/to/dir1/file2.txt",
289− "/path/to/dir1/subdir/file3.txt",
290− "/path/to/dir2/file4.txt",
293− tree := createFileTree(paths)
295− // Check the structure of the tree
296− assert.Len(t, tree, 1) // Should have one root node
298− // Check the root node
300− assert.Equal(t, "path", rootNode.Name)
301− assert.Equal(t, "directory", rootNode.Type)
302− assert.Len(t, rootNode.Children, 1)
304− // Check the "to" node
305− toNode := rootNode.Children[0]
306− assert.Equal(t, "to", toNode.Name)
307− assert.Equal(t, "directory", toNode.Type)
308− assert.Len(t, toNode.Children, 3) // file1.txt, dir1, dir2
310− // Find the dir1 node
311− var dir1Node *TreeNode
312− for _, child := range toNode.Children {
313− if child.Name == "dir1" {
319− require.NotNil(t, dir1Node)
320− assert.Equal(t, "directory", dir1Node.Type)
321− assert.Len(t, dir1Node.Children, 2) // file2.txt and subdir
324−func TestPrintTree(t *testing.T) {
325− // Create a simple tree
326− tree := []*TreeNode{
331− Children: []*TreeNode{
334− Path: "dir1/file1.txt",
339− Path: "dir1/subdir",
341− Children: []*TreeNode{
344− Path: "dir1/subdir/file2.txt",
358− result := printTree(tree, "/root")
360− // Check the output format
361− assert.Contains(t, result, "- /root/")
362− assert.Contains(t, result, " - dir1/")
363− assert.Contains(t, result, " - file1.txt")
364− assert.Contains(t, result, " - subdir/")
365− assert.Contains(t, result, " - file2.txt")
366− assert.Contains(t, result, " - file3.txt")
369−func TestListDirectory(t *testing.T) {
370− // Create a temporary directory for testing
371− tempDir, err := os.MkdirTemp("", "list_directory_test")
372− require.NoError(t, err)
373− defer os.RemoveAll(tempDir)
375− // Create a test directory structure
376− testDirs := []string{
382− testFiles := []string{
386− "dir1/subdir1/file4.txt",
390− // Create directories
391− for _, dir := range testDirs {
392− dirPath := filepath.Join(tempDir, dir)
393− err := os.MkdirAll(dirPath, 0o755)
394− require.NoError(t, err)
398− for _, file := range testFiles {
399− filePath := filepath.Join(tempDir, file)
400− err := os.WriteFile(filePath, []byte("test content"), 0o644)
401− require.NoError(t, err)
404− t.Run("lists files with no limit", func(t *testing.T) {
405− files, truncated, err := listDirectory(tempDir, []string{}, 1000)
406− require.NoError(t, err)
407− assert.False(t, truncated)
409− // Check that visible files and directories are included
410− containsPath := func(paths []string, target string) bool {
411− targetPath := filepath.Join(tempDir, target)
412− for _, path := range paths {
413− if strings.HasPrefix(path, targetPath) {
420− assert.True(t, containsPath(files, "dir1"))
421− assert.True(t, containsPath(files, "file1.txt"))
422− assert.True(t, containsPath(files, "file2.txt"))
423− assert.True(t, containsPath(files, "dir1/file3.txt"))
425− // Check that hidden files and directories are not included
426− assert.False(t, containsPath(files, ".hidden_dir"))
427− assert.False(t, containsPath(files, ".hidden_file.txt"))
430− t.Run("respects limit and returns truncated flag", func(t *testing.T) {
431− files, truncated, err := listDirectory(tempDir, []string{}, 2)
432− require.NoError(t, err)
433− assert.True(t, truncated)
434− assert.Len(t, files, 2)
437− t.Run("respects ignore patterns", func(t *testing.T) {
438− files, truncated, err := listDirectory(tempDir, []string{"*.txt"}, 1000)
439− require.NoError(t, err)
440− assert.False(t, truncated)
442− // Check that no .txt files are included
443− for _, file := range files {
444− assert.False(t, strings.HasSuffix(file, ".txt"), "Found .txt file: %s", file)
447− // But directories should still be included
448− containsDir := false
449− for _, file := range files {
450− if strings.Contains(file, "dir1") {
455− assert.True(t, containsDir)