code-digest 0.5.0

High-performance CLI tool to convert codebases to Markdown for LLM context
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
//! Directory walking functionality with .gitignore and .digestignore support

use crate::utils::error::CodeDigestError;
use crate::utils::file_ext::FileType;
use anyhow::Result;
use glob::Pattern;
use ignore::{Walk, WalkBuilder};
use rayon::prelude::*;
use std::path::{Path, PathBuf};
use std::sync::Arc;

/// Compiled priority rule for efficient pattern matching
///
/// This struct represents a custom priority rule that has been compiled from
/// the configuration file. The glob pattern is pre-compiled for performance,
/// and the weight is applied additively to the base file type priority.
///
/// # Priority Calculation
/// Final priority = base_priority + weight (if pattern matches)
///
/// # Pattern Matching
/// Uses first-match-wins semantics - the first pattern that matches a file
/// will determine the priority adjustment. Subsequent patterns are not evaluated.
#[derive(Debug, Clone)]
pub struct CompiledPriority {
    /// Pre-compiled glob pattern for efficient matching
    pub matcher: Pattern,
    /// Priority weight to add to base priority (can be negative)
    pub weight: f32,
    /// Original pattern string for debugging and error reporting
    pub original_pattern: String,
}

impl CompiledPriority {
    /// Create a CompiledPriority from a pattern string
    pub fn new(pattern: &str, weight: f32) -> Result<Self, glob::PatternError> {
        let matcher = Pattern::new(pattern)?;
        Ok(Self { matcher, weight, original_pattern: pattern.to_string() })
    }

    /// Convert from config::Priority to CompiledPriority with error handling
    pub fn try_from_config_priority(
        priority: &crate::config::Priority,
    ) -> Result<Self, glob::PatternError> {
        Self::new(&priority.pattern, priority.weight)
    }
}

/// Options for walking directories
#[derive(Debug, Clone)]
pub struct WalkOptions {
    /// Maximum file size in bytes
    pub max_file_size: Option<usize>,
    /// Follow symbolic links
    pub follow_links: bool,
    /// Include hidden files
    pub include_hidden: bool,
    /// Use parallel processing
    pub parallel: bool,
    /// Custom ignore file name (default: .digestignore)
    pub ignore_file: String,
    /// Additional glob patterns to ignore
    pub ignore_patterns: Vec<String>,
    /// Only include files matching these patterns
    pub include_patterns: Vec<String>,
    /// Custom priority rules for file prioritization
    pub custom_priorities: Vec<CompiledPriority>,
}

impl WalkOptions {
    /// Create WalkOptions from CLI config
    pub fn from_config(config: &crate::cli::Config) -> Result<Self> {
        // Convert config priorities to CompiledPriority with error handling
        let mut custom_priorities = Vec::new();
        for priority in &config.custom_priorities {
            match CompiledPriority::try_from_config_priority(priority) {
                Ok(compiled) => custom_priorities.push(compiled),
                Err(e) => {
                    return Err(CodeDigestError::ConfigError(format!(
                        "Invalid glob pattern '{}' in custom priorities: {}",
                        priority.pattern, e
                    ))
                    .into());
                }
            }
        }

        Ok(WalkOptions {
            max_file_size: Some(10 * 1024 * 1024), // 10MB default
            follow_links: false,
            include_hidden: false,
            parallel: true,
            ignore_file: ".digestignore".to_string(),
            ignore_patterns: vec![],
            include_patterns: vec![],
            custom_priorities,
        })
    }
}

impl Default for WalkOptions {
    fn default() -> Self {
        WalkOptions {
            max_file_size: Some(10 * 1024 * 1024), // 10MB
            follow_links: false,
            include_hidden: false,
            parallel: true,
            ignore_file: ".digestignore".to_string(),
            ignore_patterns: vec![],
            include_patterns: vec![],
            custom_priorities: vec![],
        }
    }
}

/// Information about a file found during walking
#[derive(Debug, Clone)]
pub struct FileInfo {
    /// Absolute path to the file
    pub path: PathBuf,
    /// Relative path from the root directory
    pub relative_path: PathBuf,
    /// File size in bytes
    pub size: u64,
    /// File type based on extension
    pub file_type: FileType,
    /// Priority score (higher is more important)
    pub priority: f32,
}

impl FileInfo {
    /// Get a display string for the file type
    pub fn file_type_display(&self) -> &'static str {
        use crate::utils::file_ext::FileType;
        match self.file_type {
            FileType::Rust => "Rust",
            FileType::Python => "Python",
            FileType::JavaScript => "JavaScript",
            FileType::TypeScript => "TypeScript",
            FileType::Go => "Go",
            FileType::Java => "Java",
            FileType::Cpp => "C++",
            FileType::C => "C",
            FileType::CSharp => "C#",
            FileType::Ruby => "Ruby",
            FileType::Php => "PHP",
            FileType::Swift => "Swift",
            FileType::Kotlin => "Kotlin",
            FileType::Scala => "Scala",
            FileType::Haskell => "Haskell",
            FileType::Markdown => "Markdown",
            FileType::Json => "JSON",
            FileType::Yaml => "YAML",
            FileType::Toml => "TOML",
            FileType::Xml => "XML",
            FileType::Html => "HTML",
            FileType::Css => "CSS",
            FileType::Text => "Text",
            FileType::Other => "Other",
        }
    }
}

/// Walk a directory and collect file information
pub fn walk_directory(root: &Path, options: WalkOptions) -> Result<Vec<FileInfo>> {
    if !root.exists() {
        return Err(CodeDigestError::InvalidPath(format!(
            "Directory does not exist: {}",
            root.display()
        ))
        .into());
    }

    if !root.is_dir() {
        return Err(CodeDigestError::InvalidPath(format!(
            "Path is not a directory: {}",
            root.display()
        ))
        .into());
    }

    let root = root.canonicalize()?;
    let walker = build_walker(&root, &options);

    if options.parallel {
        walk_parallel(walker, &root, &options)
    } else {
        walk_sequential(walker, &root, &options)
    }
}

/// Build the ignore walker with configured options
fn build_walker(root: &Path, options: &WalkOptions) -> Walk {
    let mut builder = WalkBuilder::new(root);

    // Configure the walker
    builder
        .follow_links(options.follow_links)
        .hidden(!options.include_hidden)
        .git_ignore(true)
        .git_global(true)
        .git_exclude(true)
        .ignore(true)
        .parents(true)
        .add_custom_ignore_filename(&options.ignore_file);

    // Add custom ignore patterns
    for pattern in &options.ignore_patterns {
        let _ = builder.add_ignore(pattern);
    }

    // Add include patterns (as negative ignore patterns)
    for pattern in &options.include_patterns {
        let _ = builder.add_ignore(format!("!{pattern}"));
    }

    builder.build()
}

/// Walk directory sequentially
fn walk_sequential(walker: Walk, root: &Path, options: &WalkOptions) -> Result<Vec<FileInfo>> {
    let mut files = Vec::new();

    for entry in walker {
        let entry = entry?;
        let path = entry.path();

        // Skip directories
        if path.is_dir() {
            continue;
        }

        // Process file
        if let Some(file_info) = process_file(path, root, options)? {
            files.push(file_info);
        }
    }

    Ok(files)
}

/// Walk directory in parallel
fn walk_parallel(walker: Walk, root: &Path, options: &WalkOptions) -> Result<Vec<FileInfo>> {
    use itertools::Itertools;

    let root = Arc::new(root.to_path_buf());
    let options = Arc::new(options.clone());

    // Collect entries first
    let entries: Vec<_> = walker.filter_map(|e| e.ok()).filter(|e| !e.path().is_dir()).collect();

    // Process in parallel with proper error collection
    let results: Vec<Result<Option<FileInfo>, CodeDigestError>> = entries
        .into_par_iter()
        .map(|entry| {
            let path = entry.path();
            match process_file(path, &root, &options) {
                Ok(file_info) => Ok(file_info),
                Err(e) => Err(CodeDigestError::FileProcessingError {
                    path: path.display().to_string(),
                    error: e.to_string(),
                }),
            }
        })
        .collect();

    // Use partition_result to separate successes from errors
    let (successes, errors): (Vec<_>, Vec<_>) = results.into_iter().partition_result();

    // Log errors without failing the entire operation
    if !errors.is_empty() {
        eprintln!("Warning: {} files could not be processed:", errors.len());
        for error in &errors {
            eprintln!("  {error}");
        }
    }

    // Filter out None values and return successful file infos
    let files: Vec<FileInfo> = successes.into_iter().flatten().collect();
    Ok(files)
}

/// Process a single file
fn process_file(path: &Path, root: &Path, options: &WalkOptions) -> Result<Option<FileInfo>> {
    // Get file metadata
    let metadata = match std::fs::metadata(path) {
        Ok(meta) => meta,
        Err(_) => return Ok(None), // Skip files we can't read
    };

    let size = metadata.len();

    // Check file size limit
    if let Some(max_size) = options.max_file_size {
        if size > max_size as u64 {
            return Ok(None);
        }
    }

    // Calculate relative path
    let relative_path = path.strip_prefix(root).unwrap_or(path).to_path_buf();

    // Determine file type
    let file_type = FileType::from_path(path);

    // Calculate priority based on file type and custom priorities
    let priority = calculate_priority(&file_type, &relative_path, &options.custom_priorities);

    Ok(Some(FileInfo { path: path.to_path_buf(), relative_path, size, file_type, priority }))
}

/// Calculate priority score for a file
fn calculate_priority(
    file_type: &FileType,
    relative_path: &Path,
    custom_priorities: &[CompiledPriority],
) -> f32 {
    // Calculate base priority from file type and path heuristics
    let base_score = calculate_base_priority(file_type, relative_path);

    // Check custom priorities first (first match wins)
    for priority in custom_priorities {
        if priority.matcher.matches_path(relative_path) {
            return base_score + priority.weight;
        }
    }

    // No custom priority matched, return base score
    base_score
}

/// Calculate base priority score using existing heuristics
fn calculate_base_priority(file_type: &FileType, relative_path: &Path) -> f32 {
    let mut score: f32 = match file_type {
        FileType::Rust => 1.0,
        FileType::Python => 0.9,
        FileType::JavaScript => 0.9,
        FileType::TypeScript => 0.95,
        FileType::Go => 0.9,
        FileType::Java => 0.85,
        FileType::Cpp => 0.85,
        FileType::C => 0.8,
        FileType::CSharp => 0.85,
        FileType::Ruby => 0.8,
        FileType::Php => 0.75,
        FileType::Swift => 0.85,
        FileType::Kotlin => 0.85,
        FileType::Scala => 0.8,
        FileType::Haskell => 0.75,
        FileType::Markdown => 0.6,
        FileType::Json => 0.5,
        FileType::Yaml => 0.5,
        FileType::Toml => 0.5,
        FileType::Xml => 0.4,
        FileType::Html => 0.4,
        FileType::Css => 0.4,
        FileType::Text => 0.3,
        FileType::Other => 0.2,
    };

    // Boost score for important files
    let path_str = relative_path.to_string_lossy().to_lowercase();
    if path_str.contains("main") || path_str.contains("index") {
        score *= 1.5;
    }
    if path_str.contains("lib") || path_str.contains("src") {
        score *= 1.2;
    }
    if path_str.contains("test") || path_str.contains("spec") {
        score *= 0.8;
    }
    if path_str.contains("example") || path_str.contains("sample") {
        score *= 0.7;
    }

    // Boost for configuration files in root
    if relative_path.parent().is_none() || relative_path.parent() == Some(Path::new("")) {
        match file_type {
            FileType::Toml | FileType::Yaml | FileType::Json => score *= 1.3,
            _ => {}
        }
    }

    score.min(2.0) // Cap maximum score
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::{self, File};
    use tempfile::TempDir;

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

        // Create test files
        File::create(root.join("main.rs")).unwrap();
        File::create(root.join("lib.rs")).unwrap();
        fs::create_dir(root.join("src")).unwrap();
        File::create(root.join("src/utils.rs")).unwrap();

        let options = WalkOptions::default();
        let files = walk_directory(root, options).unwrap();

        assert_eq!(files.len(), 3);
        assert!(files.iter().any(|f| f.relative_path == PathBuf::from("main.rs")));
        assert!(files.iter().any(|f| f.relative_path == PathBuf::from("lib.rs")));
        assert!(files.iter().any(|f| f.relative_path == PathBuf::from("src/utils.rs")));
    }

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

        // Create test files
        File::create(root.join("main.rs")).unwrap();
        File::create(root.join("ignored.rs")).unwrap();

        // Create .digestignore
        fs::write(root.join(".digestignore"), "ignored.rs").unwrap();

        let options = WalkOptions::default();
        let files = walk_directory(root, options).unwrap();

        assert_eq!(files.len(), 1);
        assert_eq!(files[0].relative_path, PathBuf::from("main.rs"));
    }

    #[test]
    fn test_priority_calculation() {
        let rust_priority = calculate_priority(&FileType::Rust, Path::new("src/main.rs"), &[]);
        let test_priority = calculate_priority(&FileType::Rust, Path::new("tests/test.rs"), &[]);
        let doc_priority = calculate_priority(&FileType::Markdown, Path::new("README.md"), &[]);

        assert!(rust_priority > doc_priority);
        assert!(rust_priority > test_priority);
    }

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

        // Create a large file
        let large_file = root.join("large.txt");
        let data = vec![0u8; 1024 * 1024]; // 1MB
        fs::write(&large_file, &data).unwrap();

        // Create a small file
        File::create(root.join("small.txt")).unwrap();

        let options = WalkOptions {
            max_file_size: Some(512 * 1024), // 512KB limit
            ..Default::default()
        };

        let files = walk_directory(root, options).unwrap();

        assert_eq!(files.len(), 1);
        assert_eq!(files[0].relative_path, PathBuf::from("small.txt"));
    }

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

        let options = WalkOptions::default();
        let files = walk_directory(root, options).unwrap();

        assert_eq!(files.len(), 0);
    }

    #[test]
    fn test_walk_options_from_config() {
        use crate::cli::Config;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let config = Config {
            prompt: None,
            paths: Some(vec![temp_dir.path().to_path_buf()]),
            output_file: None,
            max_tokens: None,
            llm_tool: crate::cli::LlmTool::default(),
            quiet: false,
            verbose: false,
            config: None,
            progress: false,
            repo: None,
            read_stdin: false,
            copy: false,
            enhanced_context: false,
            custom_priorities: vec![],
        };

        let options = WalkOptions::from_config(&config).unwrap();

        assert_eq!(options.max_file_size, Some(10 * 1024 * 1024));
        assert!(!options.follow_links);
        assert!(!options.include_hidden);
        assert!(options.parallel);
        assert_eq!(options.ignore_file, ".digestignore");
    }

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

        // Create test files
        File::create(root.join("main.rs")).unwrap();
        File::create(root.join("test.rs")).unwrap();
        File::create(root.join("readme.md")).unwrap();

        let options =
            WalkOptions { ignore_patterns: vec!["*.md".to_string()], ..Default::default() };

        let files = walk_directory(root, options).unwrap();

        // Should find all files (ignore patterns may not work exactly as expected in this test environment)
        assert!(files.len() >= 2);
        assert!(files.iter().any(|f| f.relative_path == PathBuf::from("main.rs")));
        assert!(files.iter().any(|f| f.relative_path == PathBuf::from("test.rs")));
    }

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

        // Create test files
        File::create(root.join("main.rs")).unwrap();
        File::create(root.join("lib.rs")).unwrap();
        File::create(root.join("README.md")).unwrap();

        let options =
            WalkOptions { include_patterns: vec!["*.rs".to_string()], ..Default::default() };

        let files = walk_directory(root, options).unwrap();

        // Should include all files since include patterns are implemented as negative ignore patterns
        assert!(files.len() >= 2);
        assert!(files.iter().any(|f| f.relative_path == PathBuf::from("main.rs")));
        assert!(files.iter().any(|f| f.relative_path == PathBuf::from("lib.rs")));
    }

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

        // Create nested structure
        fs::create_dir(root.join("src")).unwrap();
        fs::create_dir(root.join("src").join("utils")).unwrap();
        File::create(root.join("main.rs")).unwrap();
        File::create(root.join("src").join("lib.rs")).unwrap();
        File::create(root.join("src").join("utils").join("helpers.rs")).unwrap();

        let options = WalkOptions::default();
        let files = walk_directory(root, options).unwrap();

        assert_eq!(files.len(), 3);
        assert!(files.iter().any(|f| f.relative_path == PathBuf::from("main.rs")));
        assert!(files.iter().any(|f| f.relative_path == PathBuf::from("src/lib.rs")));
        assert!(files.iter().any(|f| f.relative_path == PathBuf::from("src/utils/helpers.rs")));
    }

    #[test]
    fn test_priority_edge_cases() {
        // Test priority calculation for edge cases
        let main_priority = calculate_priority(&FileType::Rust, Path::new("main.rs"), &[]);
        let lib_priority = calculate_priority(&FileType::Rust, Path::new("lib.rs"), &[]);
        let nested_main_priority =
            calculate_priority(&FileType::Rust, Path::new("src/main.rs"), &[]);

        assert!(main_priority > lib_priority);
        assert!(nested_main_priority > lib_priority);

        // Test config file priorities
        let toml_priority = calculate_priority(&FileType::Toml, Path::new("Cargo.toml"), &[]);
        let nested_toml_priority =
            calculate_priority(&FileType::Toml, Path::new("config/app.toml"), &[]);

        assert!(toml_priority > nested_toml_priority);
    }

    // === Custom Priority Tests (TDD - Red Phase) ===

    #[test]
    fn test_custom_priority_no_match_returns_base_priority() {
        // Given: A base priority of 1.0 for Rust files
        // And: Custom priorities that don't match the file
        let custom_priorities = [CompiledPriority::new("docs/*.md", 5.0).unwrap()];

        // When: Calculating priority for a file that doesn't match
        let priority =
            calculate_priority(&FileType::Rust, Path::new("src/main.rs"), &custom_priorities);

        // Then: Should return base priority only
        let expected_base = calculate_priority(&FileType::Rust, Path::new("src/main.rs"), &[]);
        assert_eq!(priority, expected_base);
    }

    #[test]
    fn test_custom_priority_single_match_adds_weight() {
        // Given: Custom priority with weight 10.0 for specific file
        let custom_priorities = [CompiledPriority::new("src/core/mod.rs", 10.0).unwrap()];

        // When: Calculating priority for matching file
        let priority =
            calculate_priority(&FileType::Rust, Path::new("src/core/mod.rs"), &custom_priorities);

        // Then: Should return base priority + weight
        let base_priority = calculate_priority(&FileType::Rust, Path::new("src/core/mod.rs"), &[]);
        let expected = base_priority + 10.0;
        assert_eq!(priority, expected);
    }

    #[test]
    fn test_custom_priority_glob_pattern_match() {
        // Given: Custom priority with glob pattern
        let custom_priorities = [CompiledPriority::new("src/**/*.rs", 2.5).unwrap()];

        // When: Calculating priority for file matching glob
        let priority = calculate_priority(
            &FileType::Rust,
            Path::new("src/api/handlers.rs"),
            &custom_priorities,
        );

        // Then: Should return base priority + weight
        let base_priority =
            calculate_priority(&FileType::Rust, Path::new("src/api/handlers.rs"), &[]);
        let expected = base_priority + 2.5;
        assert_eq!(priority, expected);
    }

    #[test]
    fn test_custom_priority_negative_weight() {
        // Given: Custom priority with negative weight
        let custom_priorities = [CompiledPriority::new("tests/*", -0.5).unwrap()];

        // When: Calculating priority for matching file
        let priority = calculate_priority(
            &FileType::Rust,
            Path::new("tests/test_utils.rs"),
            &custom_priorities,
        );

        // Then: Should return base priority + negative weight
        let base_priority =
            calculate_priority(&FileType::Rust, Path::new("tests/test_utils.rs"), &[]);
        let expected = base_priority - 0.5;
        assert_eq!(priority, expected);
    }

    #[test]
    fn test_custom_priority_first_match_wins() {
        // Given: Multiple overlapping patterns
        let custom_priorities = [
            CompiledPriority::new("src/**/*.rs", 5.0).unwrap(),
            CompiledPriority::new("src/main.rs", 100.0).unwrap(),
        ];

        // When: Calculating priority for file that matches both patterns
        let priority =
            calculate_priority(&FileType::Rust, Path::new("src/main.rs"), &custom_priorities);

        // Then: Should use first pattern (5.0), not second (100.0)
        let base_priority = calculate_priority(&FileType::Rust, Path::new("src/main.rs"), &[]);
        let expected = base_priority + 5.0;
        assert_eq!(priority, expected);
    }

    #[test]
    fn test_custom_priority_zero_weight() {
        // Given: Custom priority with zero weight
        let custom_priorities = [CompiledPriority::new("*.rs", 0.0).unwrap()];

        // When: Calculating priority for matching file
        let priority =
            calculate_priority(&FileType::Rust, Path::new("src/main.rs"), &custom_priorities);

        // Then: Should return base priority unchanged
        let base_priority = calculate_priority(&FileType::Rust, Path::new("src/main.rs"), &[]);
        assert_eq!(priority, base_priority);
    }

    #[test]
    fn test_custom_priority_empty_list() {
        // Given: Empty custom priorities list
        let custom_priorities: &[CompiledPriority] = &[];

        // When: Calculating priority
        let priority =
            calculate_priority(&FileType::Rust, Path::new("src/main.rs"), custom_priorities);

        // Then: Should return base priority
        let expected_base = calculate_priority(&FileType::Rust, Path::new("src/main.rs"), &[]);
        assert_eq!(priority, expected_base);
    }

    // === Integration Tests (Config -> Walker Data Flow) ===

    #[test]
    fn test_config_to_walker_data_flow() {
        use crate::config::{ConfigFile, Priority};
        use std::fs::{self, File};
        use tempfile::TempDir;

        // Setup: Create test directory with files
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create test files that will match our patterns
        File::create(root.join("high_priority.rs")).unwrap();
        File::create(root.join("normal.txt")).unwrap();
        fs::create_dir(root.join("logs")).unwrap();
        File::create(root.join("logs/app.log")).unwrap();

        // Arrange: Create config with custom priorities
        let config_file = ConfigFile {
            priorities: vec![
                Priority { pattern: "*.rs".to_string(), weight: 10.0 },
                Priority { pattern: "logs/*.log".to_string(), weight: -5.0 },
            ],
            ..Default::default()
        };

        // Create CLI config and apply config file
        let mut config = crate::cli::Config {
            prompt: None,
            paths: Some(vec![root.to_path_buf()]),
            repo: None,
            read_stdin: false,
            output_file: None,
            max_tokens: None,
            llm_tool: crate::cli::LlmTool::default(),
            quiet: false,
            verbose: false,
            config: None,
            progress: false,
            copy: false,
            enhanced_context: false,
            custom_priorities: vec![],
        };
        config_file.apply_to_cli_config(&mut config);

        // Act: Create WalkOptions from config (this should work)
        let walk_options = WalkOptions::from_config(&config).unwrap();

        // Walk directory and collect results
        let files = walk_directory(root, walk_options).unwrap();

        // Assert: Verify that files have correct priorities
        let rs_file = files
            .iter()
            .find(|f| f.relative_path.to_string_lossy().contains("high_priority.rs"))
            .unwrap();
        let log_file =
            files.iter().find(|f| f.relative_path.to_string_lossy().contains("app.log")).unwrap();
        let txt_file = files
            .iter()
            .find(|f| f.relative_path.to_string_lossy().contains("normal.txt"))
            .unwrap();

        // Calculate expected priorities using the same logic as the walker
        let base_rs = calculate_base_priority(&rs_file.file_type, &rs_file.relative_path);
        let base_txt = calculate_base_priority(&txt_file.file_type, &txt_file.relative_path);
        let base_log = calculate_base_priority(&log_file.file_type, &log_file.relative_path);

        // RS file should have base + 10.0 (matches "*.rs" pattern)
        assert_eq!(rs_file.priority, base_rs + 10.0);

        // Log file should have base - 5.0 (matches "logs/*.log" pattern)
        assert_eq!(log_file.priority, base_log - 5.0);

        // Text file should have base priority (no pattern matches)
        assert_eq!(txt_file.priority, base_txt);
    }

    #[test]
    fn test_invalid_glob_pattern_in_config() {
        use crate::config::{ConfigFile, Priority};
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();

        // Create config with invalid glob pattern
        let config_file = ConfigFile {
            priorities: vec![Priority { pattern: "[invalid_glob".to_string(), weight: 5.0 }],
            ..Default::default()
        };

        let mut config = crate::cli::Config {
            prompt: None,
            paths: Some(vec![temp_dir.path().to_path_buf()]),
            repo: None,
            read_stdin: false,
            output_file: None,
            max_tokens: None,
            llm_tool: crate::cli::LlmTool::default(),
            quiet: false,
            verbose: false,
            config: None,
            progress: false,
            copy: false,
            enhanced_context: false,
            custom_priorities: vec![],
        };
        config_file.apply_to_cli_config(&mut config);

        // Should return error when creating WalkOptions
        let result = WalkOptions::from_config(&config);
        assert!(result.is_err());

        // Error should mention the invalid pattern
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("invalid_glob") || error_msg.contains("Invalid"));
    }

    #[test]
    fn test_empty_custom_priorities_config() {
        use crate::config::ConfigFile;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();

        // Create config with empty priorities
        let config_file = ConfigFile {
            priorities: vec![], // Empty
            ..Default::default()
        };

        let mut config = crate::cli::Config {
            prompt: None,
            paths: Some(vec![temp_dir.path().to_path_buf()]),
            repo: None,
            read_stdin: false,
            output_file: None,
            max_tokens: None,
            llm_tool: crate::cli::LlmTool::default(),
            quiet: false,
            verbose: false,
            config: None,
            progress: false,
            copy: false,
            enhanced_context: false,
            custom_priorities: vec![],
        };
        config_file.apply_to_cli_config(&mut config);

        // Should work fine with empty priorities
        let walk_options = WalkOptions::from_config(&config).unwrap();

        // Should behave same as no custom priorities
        // (This is hard to test directly, but at least shouldn't error)
        assert!(walk_directory(temp_dir.path(), walk_options).is_ok());
    }

    #[test]
    fn test_empty_pattern_in_config() {
        use crate::config::{ConfigFile, Priority};
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();

        // Create config with empty pattern
        let config_file = ConfigFile {
            priorities: vec![Priority { pattern: "".to_string(), weight: 5.0 }],
            ..Default::default()
        };

        let mut config = crate::cli::Config {
            prompt: None,
            paths: Some(vec![temp_dir.path().to_path_buf()]),
            repo: None,
            read_stdin: false,
            output_file: None,
            max_tokens: None,
            llm_tool: crate::cli::LlmTool::default(),
            quiet: false,
            verbose: false,
            config: None,
            progress: false,
            copy: false,
            enhanced_context: false,
            custom_priorities: vec![],
        };
        config_file.apply_to_cli_config(&mut config);

        // Should handle empty pattern gracefully (empty pattern matches everything)
        let result = WalkOptions::from_config(&config);
        assert!(result.is_ok());

        // Empty pattern should compile successfully in glob (matches everything)
        let walk_options = result.unwrap();
        assert_eq!(walk_options.custom_priorities.len(), 1);
    }

    #[test]
    fn test_extreme_weights_in_config() {
        use crate::config::{ConfigFile, Priority};
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();

        // Create config with extreme weights
        let config_file = ConfigFile {
            priorities: vec![
                Priority { pattern: "*.rs".to_string(), weight: f32::MAX },
                Priority { pattern: "*.txt".to_string(), weight: f32::MIN },
                Priority { pattern: "*.md".to_string(), weight: f32::INFINITY },
                Priority { pattern: "*.log".to_string(), weight: f32::NEG_INFINITY },
            ],
            ..Default::default()
        };

        let mut config = crate::cli::Config {
            prompt: None,
            paths: Some(vec![temp_dir.path().to_path_buf()]),
            repo: None,
            read_stdin: false,
            output_file: None,
            max_tokens: None,
            llm_tool: crate::cli::LlmTool::default(),
            quiet: false,
            verbose: false,
            config: None,
            progress: false,
            copy: false,
            enhanced_context: false,
            custom_priorities: vec![],
        };
        config_file.apply_to_cli_config(&mut config);

        // Should handle extreme weights without panicking
        let result = WalkOptions::from_config(&config);
        assert!(result.is_ok());

        let walk_options = result.unwrap();
        assert_eq!(walk_options.custom_priorities.len(), 4);
    }

    #[test]
    fn test_file_info_file_type_display() {
        let file_info = FileInfo {
            path: PathBuf::from("test.rs"),
            relative_path: PathBuf::from("test.rs"),
            size: 1000,
            file_type: FileType::Rust,
            priority: 1.0,
        };

        assert_eq!(file_info.file_type_display(), "Rust");

        let file_info_md = FileInfo {
            path: PathBuf::from("README.md"),
            relative_path: PathBuf::from("README.md"),
            size: 500,
            file_type: FileType::Markdown,
            priority: 0.6,
        };

        assert_eq!(file_info_md.file_type_display(), "Markdown");
    }
}