(t *testing.T)
| 171 | } |
| 172 | |
| 173 | func TestReadDirSortBeforeTruncate(t *testing.T) { |
| 174 | // Create a temporary test directory |
| 175 | tmpDir, err := os.MkdirTemp("", "readdir_test_sort") |
| 176 | if err != nil { |
| 177 | t.Fatalf("Failed to create temp dir: %v", err) |
| 178 | } |
| 179 | defer os.RemoveAll(tmpDir) |
| 180 | |
| 181 | // Create files with names that would sort alphabetically before directories |
| 182 | // but we want directories to appear first |
| 183 | for i := 0; i < 5; i++ { |
| 184 | testFile := filepath.Join(tmpDir, fmt.Sprintf("a_file_%d.txt", i)) |
| 185 | if err := os.WriteFile(testFile, []byte("test"), 0644); err != nil { |
| 186 | t.Fatalf("Failed to create test file: %v", err) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // Create directories with names that sort alphabetically after the files |
| 191 | for i := 0; i < 3; i++ { |
| 192 | testDir := filepath.Join(tmpDir, fmt.Sprintf("z_dir_%d", i)) |
| 193 | if err := os.Mkdir(testDir, 0755); err != nil { |
| 194 | t.Fatalf("Failed to create test dir: %v", err) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Test with max_entries=5 (less than total of 8) |
| 199 | // All 3 directories should still appear because they're sorted first |
| 200 | maxEntries := 5 |
| 201 | input := map[string]any{ |
| 202 | "path": tmpDir, |
| 203 | "max_entries": maxEntries, |
| 204 | } |
| 205 | |
| 206 | result, err := readDirCallback(input, &uctypes.UIMessageDataToolUse{}) |
| 207 | if err != nil { |
| 208 | t.Fatalf("readDirCallback failed: %v", err) |
| 209 | } |
| 210 | |
| 211 | resultMap := result.(map[string]any) |
| 212 | entries, ok := resultMap["entries"].([]fileutil.DirEntryOut) |
| 213 | if !ok { |
| 214 | t.Fatalf("entries is not a slice of DirEntryOut") |
| 215 | } |
| 216 | |
| 217 | // Count directories in the result |
| 218 | dirCount := 0 |
| 219 | for _, entry := range entries { |
| 220 | if entry.Dir { |
| 221 | dirCount++ |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // All 3 directories should be present because sorting happens before truncation |
| 226 | if dirCount != 3 { |
| 227 | t.Errorf("Expected 3 directories in truncated results, got %d", dirCount) |
| 228 | } |
| 229 | |
| 230 | // First 3 entries should be directories |
nothing calls this directly
no test coverage detected