/ go / test / track_a_test.go
track_a_test.go
  1  package test
  2  
  3  import (
  4  	"context"
  5  	"os"
  6  	"path/filepath"
  7  	"strings"
  8  	"testing"
  9  
 10  	"github.com/TransformerOS/kamaji-go/internal/tools"
 11  )
 12  
 13  // TestTrackA_FileSystemTools tests all file system tools comprehensively
 14  func TestTrackA_FileSystemTools(t *testing.T) {
 15  	ctx := context.Background()
 16  	tmpDir := t.TempDir()
 17  
 18  	t.Run("FileReadTool", func(t *testing.T) {
 19  		// Test successful read
 20  		testFile := filepath.Join(tmpDir, "read_test.txt")
 21  		testContent := "Hello, World!\nLine 2\nLine 3"
 22  		err := os.WriteFile(testFile, []byte(testContent), 0644)
 23  		if err != nil {
 24  			t.Fatalf("Failed to create test file: %v", err)
 25  		}
 26  
 27  		tool := tools.FileReadTool{}
 28  		result, err := tool.Call(ctx, testFile)
 29  		if err != nil {
 30  			t.Errorf("FileReadTool failed: %v", err)
 31  		}
 32  		if result != testContent {
 33  			t.Errorf("Expected %q, got %q", testContent, result)
 34  		}
 35  
 36  		// Test empty path
 37  		_, err = tool.Call(ctx, "")
 38  		if err == nil {
 39  			t.Error("Expected error for empty path")
 40  		}
 41  
 42  		// Test non-existent file
 43  		_, err = tool.Call(ctx, "nonexistent.txt")
 44  		if err == nil {
 45  			t.Error("Expected error for non-existent file")
 46  		}
 47  	})
 48  
 49  	t.Run("FileWriteTool", func(t *testing.T) {
 50  		tool := tools.FileWriteTool{}
 51  		
 52  		// Test successful write
 53  		testFile := filepath.Join(tmpDir, "write_test.txt")
 54  		testContent := "Test content\nWith multiple lines"
 55  		input := testFile + "|" + testContent
 56  		
 57  		result, err := tool.Call(ctx, input)
 58  		if err != nil {
 59  			t.Errorf("FileWriteTool failed: %v", err)
 60  		}
 61  		if !strings.Contains(result, "Successfully wrote") {
 62  			t.Errorf("Unexpected result: %s", result)
 63  		}
 64  
 65  		// Verify file was written
 66  		content, err := os.ReadFile(testFile)
 67  		if err != nil {
 68  			t.Errorf("Failed to read written file: %v", err)
 69  		}
 70  		if string(content) != testContent {
 71  			t.Errorf("Expected %q, got %q", testContent, string(content))
 72  		}
 73  
 74  		// Test directory creation
 75  		nestedFile := filepath.Join(tmpDir, "nested", "dir", "file.txt")
 76  		input = nestedFile + "|nested content"
 77  		_, err = tool.Call(ctx, input)
 78  		if err != nil {
 79  			t.Errorf("FileWriteTool failed with nested directories: %v", err)
 80  		}
 81  
 82  		// Test invalid input format
 83  		_, err = tool.Call(ctx, "invalid_format")
 84  		if err == nil {
 85  			t.Error("Expected error for invalid input format")
 86  		}
 87  
 88  		// Test empty path
 89  		_, err = tool.Call(ctx, "|content")
 90  		if err == nil {
 91  			t.Error("Expected error for empty path")
 92  		}
 93  	})
 94  
 95  	t.Run("ListDirectoryTool", func(t *testing.T) {
 96  		tool := tools.ListDirectoryTool{}
 97  		
 98  		// Create test files and directories
 99  		testFiles := []string{"file1.txt", "file2.md", "file3.go"}
100  		testDirs := []string{"dir1", "dir2"}
101  		
102  		for _, file := range testFiles {
103  			err := os.WriteFile(filepath.Join(tmpDir, file), []byte("test"), 0644)
104  			if err != nil {
105  				t.Fatalf("Failed to create test file %s: %v", file, err)
106  			}
107  		}
108  		
109  		for _, dir := range testDirs {
110  			err := os.Mkdir(filepath.Join(tmpDir, dir), 0755)
111  			if err != nil {
112  				t.Fatalf("Failed to create test directory %s: %v", dir, err)
113  			}
114  		}
115  
116  		// Test listing
117  		result, err := tool.Call(ctx, tmpDir)
118  		if err != nil {
119  			t.Errorf("ListDirectoryTool failed: %v", err)
120  		}
121  
122  		// Verify all files and directories are listed
123  		for _, file := range testFiles {
124  			if !strings.Contains(result, file) {
125  				t.Errorf("Expected result to contain %s, got: %s", file, result)
126  			}
127  		}
128  		for _, dir := range testDirs {
129  			if !strings.Contains(result, dir) {
130  				t.Errorf("Expected result to contain %s, got: %s", dir, result)
131  			}
132  		}
133  
134  		// Test current directory (empty input)
135  		_, err = tool.Call(ctx, "")
136  		if err != nil {
137  			t.Errorf("ListDirectoryTool failed with empty input: %v", err)
138  		}
139  
140  		// Test non-existent directory
141  		_, err = tool.Call(ctx, "nonexistent_dir")
142  		if err == nil {
143  			t.Error("Expected error for non-existent directory")
144  		}
145  	})
146  
147  	t.Run("FileAppendTool", func(t *testing.T) {
148  		tool := tools.FileAppendTool{}
149  		
150  		// Create initial file
151  		testFile := filepath.Join(tmpDir, "append_test.txt")
152  		initialContent := "Initial content\n"
153  		err := os.WriteFile(testFile, []byte(initialContent), 0644)
154  		if err != nil {
155  			t.Fatalf("Failed to create initial file: %v", err)
156  		}
157  
158  		// Test append
159  		appendContent := "Appended content\nMore content"
160  		input := testFile + "|" + appendContent
161  		
162  		result, err := tool.Call(ctx, input)
163  		if err != nil {
164  			t.Errorf("FileAppendTool failed: %v", err)
165  		}
166  		if !strings.Contains(result, "Successfully appended") {
167  			t.Errorf("Unexpected result: %s", result)
168  		}
169  
170  		// Verify content was appended
171  		content, err := os.ReadFile(testFile)
172  		if err != nil {
173  			t.Errorf("Failed to read file: %v", err)
174  		}
175  		expected := initialContent + appendContent
176  		if string(content) != expected {
177  			t.Errorf("Expected %q, got %q", expected, string(content))
178  		}
179  
180  		// Test append to non-existent file (should create)
181  		newFile := filepath.Join(tmpDir, "new_append.txt")
182  		input = newFile + "|new content"
183  		_, err = tool.Call(ctx, input)
184  		if err != nil {
185  			t.Errorf("FileAppendTool failed to create new file: %v", err)
186  		}
187  
188  		// Test invalid input format
189  		_, err = tool.Call(ctx, "invalid_format")
190  		if err == nil {
191  			t.Error("Expected error for invalid input format")
192  		}
193  	})
194  
195  	t.Run("ToolRegistration", func(t *testing.T) {
196  		// Test that all tools are registered
197  		allTools := tools.GetAll()
198  		
199  		expectedTools := []string{
200  			"file_read",
201  			"file_write", 
202  			"list_directory",
203  			"file_append",
204  		}
205  		
206  		toolNames := make(map[string]bool)
207  		for _, tool := range allTools {
208  			toolNames[tool.Name()] = true
209  		}
210  		
211  		for _, expected := range expectedTools {
212  			if !toolNames[expected] {
213  				t.Errorf("Expected tool %s to be registered", expected)
214  			}
215  		}
216  	})
217  
218  	t.Run("ToolInterface", func(t *testing.T) {
219  		// Test that all tools implement the interface correctly
220  		testTools := []tools.Tool{
221  			tools.FileReadTool{},
222  			tools.FileWriteTool{},
223  			tools.ListDirectoryTool{},
224  			tools.FileAppendTool{},
225  		}
226  		
227  		for _, tool := range testTools {
228  			// Test Name() returns non-empty string
229  			name := tool.Name()
230  			if name == "" {
231  				t.Errorf("Tool %T returned empty name", tool)
232  			}
233  			
234  			// Test Description() returns non-empty string
235  			desc := tool.Description()
236  			if desc == "" {
237  				t.Errorf("Tool %s returned empty description", name)
238  			}
239  		}
240  	})
241  }