yek 0.25.5

A tool to serialize a repository into chunks of text files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
use assert_cmd::prelude::*;
use predicates::prelude::*;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use tempfile::TempDir;
use yek::tree::{clean_path_components, generate_tree};

#[cfg(test)]
mod tree_tests {
    use super::*;

    fn create_test_structure(base_dir: &Path) -> std::io::Result<()> {
        // Create nested directory structure
        fs::create_dir_all(base_dir.join("src"))?;
        fs::create_dir_all(base_dir.join("tests"))?;
        fs::create_dir_all(base_dir.join("docs/guides"))?;

        // Create files
        fs::write(base_dir.join("config.py"), "# Config file\n")?;
        fs::write(base_dir.join("Cargo.toml"), "[package]\nname = \"test\"\n")?;
        fs::write(base_dir.join("src/main.rs"), "fn main() {}\n")?;
        fs::write(base_dir.join("src/lib.rs"), "// Library code\n")?;
        fs::write(base_dir.join("tests/test.rs"), "#[test]\nfn test() {}\n")?;
        fs::write(base_dir.join("docs/api.py"), "# API Documentation\n")?;
        fs::write(base_dir.join("docs/guides/setup.py"), "# Setup Guide\n")?;

        Ok(())
    }

    #[test]
    fn test_tree_header_basic() {
        let temp_dir = TempDir::new().unwrap();
        create_test_structure(temp_dir.path()).unwrap();

        let mut cmd = Command::cargo_bin("yek").unwrap();
        cmd.arg("--tree-header")
            .arg("--max-size")
            .arg("1KB")
            .arg(temp_dir.path());

        cmd.assert()
            .success()
            .stdout(predicate::str::contains("Directory structure:"))
            .stdout(predicate::str::contains("├── src/"))
            .stdout(predicate::str::contains("│   ├── lib.rs"))
            .stdout(predicate::str::contains("│   └── main.rs"))
            .stdout(predicate::str::contains("├── tests/"))
            .stdout(predicate::str::contains("├── Cargo.toml"))
            .stdout(predicate::str::contains("└── config.py"))
            .stdout(predicate::str::contains(">>>> "));
    }

    #[test]
    fn test_tree_only_mode() {
        let temp_dir = TempDir::new().unwrap();
        create_test_structure(temp_dir.path()).unwrap();

        let mut cmd = Command::cargo_bin("yek").unwrap();
        cmd.arg("--tree-only").arg(temp_dir.path());

        cmd.assert()
            .success()
            .stdout(predicate::str::contains("Directory structure:"))
            .stdout(predicate::str::contains("├── docs/"))
            .stdout(predicate::str::contains("│   ├── guides/"))
            .stdout(predicate::str::contains("│   │   └── setup.py"))
            .stdout(predicate::str::contains("│   └── api.py"))
            .stdout(predicate::str::contains("├── src/"))
            .stdout(predicate::str::contains(">>>> ").not())
            .stdout(predicate::str::contains("fn main()").not());
    }

    #[test]
    fn test_tree_header_short_flag() {
        let temp_dir = TempDir::new().unwrap();
        fs::write(temp_dir.path().join("test.rs"), "content").unwrap();

        let mut cmd = Command::cargo_bin("yek").unwrap();
        cmd.arg("-t").arg(temp_dir.path());

        cmd.assert()
            .success()
            .stdout(predicate::str::contains("Directory structure:"))
            .stdout(predicate::str::contains("└── test.rs"));
    }

    #[test]
    fn test_tree_mutual_exclusivity() {
        let temp_dir = TempDir::new().unwrap();
        fs::write(temp_dir.path().join("test.rs"), "content").unwrap();

        let mut cmd = Command::cargo_bin("yek").unwrap();
        cmd.arg("--tree-header")
            .arg("--tree-only")
            .arg(temp_dir.path());

        cmd.assert().failure().stderr(predicate::str::contains(
            "tree_header and tree_only cannot both be enabled",
        ));
    }

    #[test]
    fn test_tree_with_single_file() {
        let temp_dir = TempDir::new().unwrap();
        fs::write(temp_dir.path().join("single.rs"), "// Single file\n").unwrap();

        let mut cmd = Command::cargo_bin("yek").unwrap();
        cmd.arg("--tree-header")
            .arg(temp_dir.path().join("single.rs"));

        cmd.assert()
            .success()
            .stdout(predicate::str::contains("Directory structure:"))
            .stdout(predicate::str::contains("└── single.rs"))
            .stdout(predicate::str::contains(">>>> single.rs"))
            .stdout(predicate::str::contains("// Single file"));
    }

    #[test]
    fn test_tree_empty_directory() {
        let temp_dir = TempDir::new().unwrap();
        fs::create_dir_all(temp_dir.path().join("empty")).unwrap();

        let mut cmd = Command::cargo_bin("yek").unwrap();
        cmd.arg("--tree-only").arg(temp_dir.path());

        let output = cmd.assert().success();
        let stdout = std::str::from_utf8(&output.get_output().stdout).unwrap();

        // For empty directories, tree-only should produce empty content
        // Since this runs in streaming mode (no files to process), it should be empty or just whitespace
        assert!(
            stdout.trim().is_empty(),
            "Expected empty output for empty directory, got: '{}'",
            stdout
        );
    }

    #[test]
    fn test_tree_with_ignored_patterns() {
        let temp_dir = TempDir::new().unwrap();
        create_test_structure(temp_dir.path()).unwrap();

        // Create additional files that should be ignored
        fs::create_dir_all(temp_dir.path().join("node_modules")).unwrap();
        fs::write(temp_dir.path().join("node_modules/package.json"), "{}").unwrap();
        fs::write(temp_dir.path().join("Cargo.lock"), "lock file").unwrap();

        let mut cmd = Command::cargo_bin("yek").unwrap();
        cmd.arg("--tree-only").arg(temp_dir.path());

        cmd.assert()
            .success()
            .stdout(predicate::str::contains("Directory structure:"))
            .stdout(predicate::str::contains("node_modules").not())
            .stdout(predicate::str::contains("Cargo.lock").not());
    }

    #[test]
    fn test_tree_header_with_json_output() {
        let temp_dir = TempDir::new().unwrap();
        fs::write(temp_dir.path().join("test.rs"), "content").unwrap();

        let mut cmd = Command::cargo_bin("yek").unwrap();
        cmd.arg("--tree-header").arg("--json").arg(temp_dir.path());

        cmd.assert().failure().stderr(predicate::str::contains(
            "JSON output not supported with tree header mode",
        ));
    }

    #[test]
    fn test_tree_only_with_json_output() {
        let temp_dir = TempDir::new().unwrap();
        fs::write(temp_dir.path().join("test.rs"), "content").unwrap();

        let mut cmd = Command::cargo_bin("yek").unwrap();
        cmd.arg("--tree-only").arg("--json").arg(temp_dir.path());

        cmd.assert().failure().stderr(predicate::str::contains(
            "JSON output not supported in tree-only mode",
        ));
    }

    #[test]
    fn test_tree_header_with_token_mode() {
        let temp_dir = TempDir::new().unwrap();
        fs::write(temp_dir.path().join("small.rs"), "small content").unwrap();

        let mut cmd = Command::cargo_bin("yek").unwrap();
        cmd.arg("--tree-header")
            .arg("--tokens")
            .arg("100")
            .arg(temp_dir.path());

        cmd.assert()
            .success()
            .stdout(predicate::str::contains("Directory structure:"))
            .stdout(predicate::str::contains("└── small.rs"));
    }

    #[test]
    fn test_tree_respects_max_size() {
        let temp_dir = TempDir::new().unwrap();
        let large_content = "x".repeat(2000);
        fs::write(temp_dir.path().join("large.rs"), &large_content).unwrap();
        fs::write(temp_dir.path().join("small.rs"), "small").unwrap();

        let mut cmd = Command::cargo_bin("yek").unwrap();
        cmd.arg("--tree-header")
            .arg("--max-size")
            .arg("1KB")
            .arg(temp_dir.path());

        cmd.assert()
            .success()
            .stdout(predicate::str::contains("Directory structure:"))
            .stdout(predicate::str::contains("├── ").or(predicate::str::contains("└── ")));
    }

    #[test]
    fn test_tree_header_cli_flag() {
        let temp_dir = TempDir::new().unwrap();
        fs::write(temp_dir.path().join("test.py"), "content").unwrap();

        let mut cmd = Command::cargo_bin("yek").unwrap();
        cmd.arg("--tree-header")
            .arg("--max-size")
            .arg("1KB")
            .arg(temp_dir.path());

        cmd.assert()
            .success()
            .stdout(predicate::str::contains("Directory structure:"))
            .stdout(predicate::str::contains("test.py"))
            .stdout(predicate::str::contains(">>>> test.py"));
    }

    #[test]
    fn test_tree_directory_sorting() {
        let temp_dir = TempDir::new().unwrap();

        // Create files and directories in non-alphabetical order
        fs::write(temp_dir.path().join("zebra.rs"), "content").unwrap();
        fs::create_dir_all(temp_dir.path().join("alpha")).unwrap();
        fs::write(temp_dir.path().join("alpha/file.rs"), "content").unwrap();
        fs::write(temp_dir.path().join("beta.rs"), "content").unwrap();
        fs::create_dir_all(temp_dir.path().join("gamma")).unwrap();
        fs::write(temp_dir.path().join("gamma/file.rs"), "content").unwrap();

        let mut cmd = Command::cargo_bin("yek").unwrap();
        cmd.arg("--tree-only").arg(temp_dir.path());

        let output = cmd.assert().success().get_output().stdout.clone();
        let output_str = String::from_utf8(output).unwrap();

        // Directories should come before files, both sorted alphabetically
        let alpha_pos = output_str.find("alpha/").unwrap();
        let gamma_pos = output_str.find("gamma/").unwrap();
        let beta_pos = output_str.find("beta.rs").unwrap();
        let zebra_pos = output_str.find("zebra.rs").unwrap();

        // Directories first (alpha, gamma), then files (beta, zebra)
        assert!(alpha_pos < gamma_pos);
        assert!(gamma_pos < beta_pos);
        assert!(beta_pos < zebra_pos);
    }

    #[test]
    fn test_tree_with_custom_template() {
        let temp_dir = TempDir::new().unwrap();
        fs::write(temp_dir.path().join("test.rs"), "hello world").unwrap();

        let mut cmd = Command::cargo_bin("yek").unwrap();
        cmd.arg("--tree-header")
            .arg("--output-template")
            .arg("==== FILE_PATH ====\\nFILE_CONTENT\\n")
            .arg(temp_dir.path());

        cmd.assert()
            .success()
            .stdout(predicate::str::contains("Directory structure:"))
            .stdout(predicate::str::contains("└── test.rs"))
            .stdout(predicate::str::contains("==== test.rs ===="))
            .stdout(predicate::str::contains("hello world"));
    }

    #[test]
    fn test_tree_critical_fixes_comprehensive() {
        let temp_dir = TempDir::new().unwrap();

        // Create a complex structure that tests all critical fixes:
        // 1. Path normalization and component filtering
        // 2. Duplicate file handling
        // 3. File vs directory conflicts
        // 4. Proper sorting and tree structure

        // Create nested directories
        fs::create_dir_all(temp_dir.path().join("src").join("utils")).unwrap();
        fs::create_dir_all(temp_dir.path().join("config")).unwrap();
        fs::create_dir_all(temp_dir.path().join("tests")).unwrap();

        // Create files that test duplicate handling
        fs::write(temp_dir.path().join("src").join("main.rs"), "fn main() {}").unwrap();
        fs::write(temp_dir.path().join("src").join("lib.rs"), "// Library").unwrap();
        fs::write(
            temp_dir.path().join("src").join("utils").join("helper.rs"),
            "// Helper",
        )
        .unwrap();

        // Create file vs directory conflict scenario
        fs::write(temp_dir.path().join("config").join("app.toml"), "[app]").unwrap();
        fs::write(temp_dir.path().join("config.json"), "{}").unwrap(); // config as both file and dir

        // Create files with various extensions for sorting test
        fs::write(temp_dir.path().join("README.md"), "# Project").unwrap();
        fs::write(temp_dir.path().join("Cargo.toml"), "[package]").unwrap();
        fs::write(
            temp_dir.path().join("tests").join("integration.rs"),
            "#[test]",
        )
        .unwrap();

        let mut cmd = Command::cargo_bin("yek").unwrap();
        cmd.arg("--tree-only").arg(temp_dir.path());

        let output = cmd.assert().success().get_output().stdout.clone();
        let output_str = String::from_utf8(output).unwrap();

        // Test 1: Proper directory structure with correct sorting (directories first)
        assert!(output_str.contains("├── config/"));
        assert!(output_str.contains("├── src/"));
        assert!(output_str.contains("├── tests/"));

        // Test 2: Files come after directories, sorted alphabetically
        assert!(output_str.contains("├── Cargo.toml"));
        assert!(output_str.contains("└── config.json"));

        // Test 3: Nested structure is properly rendered
        assert!(output_str.contains("│   ├── utils/"));
        assert!(output_str.contains("│   │   └── helper.rs"));
        assert!(output_str.contains("│   ├── lib.rs"));
        assert!(output_str.contains("│   └── main.rs"));

        // Test 4: File vs directory conflict resolved (config/ directory and config.json file coexist)
        let config_dir_count = output_str.matches("config/").count();
        let config_file_count = output_str.matches("config.json").count();
        assert_eq!(
            config_dir_count, 1,
            "Should have exactly one config/ directory"
        );
        assert_eq!(
            config_file_count, 1,
            "Should have exactly one config.json file"
        );

        // Test 5: No problematic path components (like Windows drive prefixes) appear
        assert!(!output_str.contains("C:"));
        assert!(!output_str.contains("D:"));
        assert!(!output_str.contains("./"));
        assert!(!output_str.contains("../"));

        // Test 6: Proper Unicode tree characters are used
        assert!(output_str.contains("├──"));
        assert!(output_str.contains("└──"));
        assert!(output_str.contains(""));

        // Test 7: Directory structure header is present in tree-only mode
        assert!(output_str.contains("Directory structure:"));

        // Test 8: All expected files are present and accounted for
        assert!(output_str.contains("main.rs"));
        assert!(output_str.contains("lib.rs"));
        assert!(output_str.contains("helper.rs"));
        assert!(output_str.contains("app.toml"));
        assert!(output_str.contains("integration.rs"));
        assert!(output_str.contains("Cargo.toml"));
    }

    #[test]
    fn test_tree_windows_path_handling() {
        let temp_dir = TempDir::new().unwrap();

        // Create a nested structure that would trigger Windows path issues
        fs::create_dir_all(temp_dir.path().join("repo").join("src")).unwrap();
        fs::write(
            temp_dir.path().join("repo").join("src").join("lib.rs"),
            "// lib content",
        )
        .unwrap();
        fs::write(temp_dir.path().join("repo").join("Cargo.toml"), "[package]").unwrap();

        let mut cmd = Command::cargo_bin("yek").unwrap();
        cmd.arg("--tree-only").arg(temp_dir.path());

        let output = cmd.assert().success().get_output().stdout.clone();
        let output_str = String::from_utf8(output).unwrap();

        // Should not contain drive prefixes (C:, D:, etc.) that could appear on Windows
        assert!(!output_str.contains("C:"));
        assert!(!output_str.contains("D:"));
        assert!(!output_str.contains("E:"));

        // Should contain proper nested structure
        assert!(output_str.contains("repo/"));
        assert!(output_str.contains("├── src/") || output_str.contains("└── src/"));
        assert!(output_str.contains("lib.rs"));
        assert!(output_str.contains("Cargo.toml"));
    }

    #[test]
    fn test_generate_tree_empty() {
        let paths = vec![];
        let result = generate_tree(&paths);
        assert_eq!(result, "");
    }

    #[test]
    fn test_generate_tree_single_file() {
        let paths = vec![PathBuf::from("README.md")];
        let result = generate_tree(&paths);
        assert!(result.contains("Directory structure:"));
        assert!(result.contains("└── README.md"));
    }

    #[test]
    fn test_generate_tree_nested_structure() {
        let paths = vec![
            PathBuf::from("src/lib.rs"),
            PathBuf::from("src/main.rs"),
            PathBuf::from("Cargo.toml"),
            PathBuf::from("README.md"),
        ];
        let result = generate_tree(&paths);

        assert!(result.contains("Directory structure:"));
        assert!(result.contains("├── src/"));
        assert!(result.contains("│   ├── lib.rs"));
        assert!(result.contains("│   └── main.rs"));
        assert!(result.contains("├── Cargo.toml"));
        assert!(result.contains("└── README.md"));
    }

    #[test]
    fn test_generate_tree_directories_before_files() {
        let paths = vec![PathBuf::from("file.txt"), PathBuf::from("dir/nested.rs")];
        let result = generate_tree(&paths);

        // Directories should come before files
        let dir_pos = result.find("├── dir/").unwrap_or(0);
        let file_pos = result.find("└── file.txt").unwrap_or(0);
        assert!(dir_pos < file_pos);
    }

    #[test]
    fn test_final_component_always_treated_as_file() {
        // Test that final components are always treated as files, regardless of extension
        let paths = vec![
            PathBuf::from("Makefile"),      // No extension
            PathBuf::from("Dockerfile"),    // No extension
            PathBuf::from("src/mod"),       // No extension in subdirectory
            PathBuf::from("config.toml"),   // With extension
            PathBuf::from("scripts/build"), // No extension, could look like directory
        ];
        let result = generate_tree(&paths);

        // All final components should be files (no trailing slash)
        // Directories come first, then files alphabetically
        assert!(result.contains("├── scripts/"));
        assert!(result.contains("│   └── build")); // build should be a file, not build/
        assert!(result.contains("├── src/"));
        assert!(result.contains("│   └── mod")); // mod should be a file, not mod/
        assert!(result.contains("├── Dockerfile"));
        assert!(result.contains("├── Makefile"));
        assert!(result.contains("└── config.toml")); // Last file uses └──

        // Verify no final components have trailing slashes (which would indicate directories)
        assert!(!result.contains("Dockerfile/"));
        assert!(!result.contains("Makefile/"));
        assert!(!result.contains("config.toml/"));
        assert!(!result.contains("build/"));
        assert!(!result.contains("mod/"));
    }

    #[test]
    fn test_windows_path_component_filtering() {
        // Test the clean_path_components function directly
        // On Unix systems, we can't easily create Windows-style paths,
        // so we test the filtering logic with relative paths that have
        // problematic components like ".." and "."

        let path = Path::new("./src/../src/lib.rs");
        let components = clean_path_components(path);

        // Should filter out "." and keep ".." and normal components
        assert_eq!(components, vec!["src", "..", "src", "lib.rs"]);

        // Test with a simple path
        let path = Path::new("repo/src/lib.rs");
        let components = clean_path_components(path);
        assert_eq!(components, vec!["repo", "src", "lib.rs"]);
    }

    #[test]
    fn test_path_normalization_in_tree() {
        // Test that paths with current directory components are handled correctly
        let paths = vec![PathBuf::from("./src/lib.rs"), PathBuf::from("src/main.rs")];
        let result = generate_tree(&paths);

        // Should contain proper structure without "./"
        assert!(result.contains("└── src/"));
        assert!(result.contains("    ├── lib.rs"));
        assert!(result.contains("    └── main.rs"));
        // Should not contain "./" in the output
        assert!(!result.contains("./"));
    }

    #[test]
    fn test_duplicate_file_paths() {
        // Test that duplicate file paths are handled correctly
        // The same file path added twice should still result in a single entry
        let paths = vec![
            PathBuf::from("src/lib.rs"),
            PathBuf::from("src/lib.rs"), // Duplicate
            PathBuf::from("src/main.rs"),
        ];
        let result = generate_tree(&paths);

        // Should only show lib.rs once
        let lib_rs_count = result.matches("lib.rs").count();
        assert_eq!(
            lib_rs_count, 1,
            "lib.rs should appear only once, got: {}",
            result
        );

        // Should still show both files
        assert!(result.contains("├── lib.rs"));
        assert!(result.contains("└── main.rs"));
    }

    #[test]
    fn test_file_vs_directory_conflict() {
        // Test when the same path is used as both intermediate directory and final file
        // This tests the fix for issue where a file could be marked as directory
        // In this case, the directory usage takes precedence to maintain tree consistency
        let paths = vec![
            PathBuf::from("config/settings.json"), // config as directory
            PathBuf::from("config"), // config as file - should be absorbed into directory
            PathBuf::from("readme.txt"), // another file for comparison
        ];
        let result = generate_tree(&paths);

        // config should be treated as a directory containing settings.json
        // The standalone "config" file is absorbed into the directory structure
        // because directory usage takes precedence when there are children
        assert!(result.contains("├── config/"));
        assert!(result.contains("│   └── settings.json"));
        assert!(result.contains("└── readme.txt"));

        // Should not show config as both file and directory
        let config_lines: Vec<&str> = result
            .lines()
            .filter(|line| line.contains("config"))
            .collect();
        assert_eq!(
            config_lines.len(),
            1,
            "Config should appear only once as directory"
        );
    }

    #[test]
    fn test_empty_directory_becomes_file() {
        // Test that an empty directory entry can be converted to a file
        let paths = vec![
            PathBuf::from("item"), // item as file
        ];
        let result = generate_tree(&paths);

        // item should be treated as a file (no trailing slash)
        assert!(result.contains("└── item"));
        assert!(!result.contains("item/"));
    }

    #[test]
    fn test_processing_order_independence() {
        // Test that the result is the same regardless of processing order
        let paths1 = vec![
            PathBuf::from("src/lib.rs"),
            PathBuf::from("src/main.rs"),
            PathBuf::from("src"),
        ];
        let paths2 = vec![
            PathBuf::from("src"),
            PathBuf::from("src/lib.rs"),
            PathBuf::from("src/main.rs"),
        ];

        let result1 = generate_tree(&paths1);
        let result2 = generate_tree(&paths2);

        // Both should produce the same tree structure
        // src should be a directory containing lib.rs and main.rs
        assert!(result1.contains("src/"));
        assert!(result1.contains("lib.rs"));
        assert!(result1.contains("main.rs"));

        assert!(result2.contains("src/"));
        assert!(result2.contains("lib.rs"));
        assert!(result2.contains("main.rs"));

        // The essential structure should be the same (ignoring exact formatting)
        let result1_lines: Vec<&str> = result1.lines().filter(|l| !l.trim().is_empty()).collect();
        let result2_lines: Vec<&str> = result2.lines().filter(|l| !l.trim().is_empty()).collect();
        assert_eq!(result1_lines.len(), result2_lines.len());
    }
    #[test]
    fn test_clean_path_components_windows_prefix() {
        // Test filtering Windows drive prefixes
        // Since we can't create actual Windows paths on Unix, we test the logic indirectly
        let path = Path::new("C:").join("Users").join("file.txt");
        let components = clean_path_components(&path);

        // On Unix, "C:" becomes a normal component, but the function should handle it
        // The key is that it filters out RootDir and Prefix components
        assert!(!components.is_empty());
    }

    #[test]
    fn test_generate_tree_with_empty_components() {
        // Test paths that result in empty components after cleaning
        let paths = vec![PathBuf::from("")];
        let result = generate_tree(&paths);
        // Should handle gracefully
        assert!(result.contains("Directory structure:") || result.is_empty());
    }

    #[test]
    fn test_add_path_to_tree_conflicts() {
        // Test file/directory conflicts by creating paths that would conflict
        let paths = vec![
            PathBuf::from("conflict"),          // as file
            PathBuf::from("conflict/item.txt"), // makes conflict a directory
        ];
        let result = generate_tree(&paths);

        // conflict should be a directory containing item.txt
        assert!(result.contains("conflict/"));
        assert!(result.contains("item.txt"));
    }

    #[test]
    fn test_render_tree_sorting_edge_cases() {
        // Test sorting with mixed files and directories with same names
        let paths = vec![
            PathBuf::from("a/file"), // directory a with file
            PathBuf::from("a"),      // file a - should be absorbed into directory
            PathBuf::from("b"),      // file b
        ];
        let result = generate_tree(&paths);

        // Should handle the conflict properly
        assert!(result.contains("Directory structure:"));
    }
}