tldr-core 0.1.4

Core analysis engine for TLDR code analysis tool
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
//! Pattern detection module for design pattern mining
//!
//! This module provides single-pass pattern extraction across codebases.
//! Addresses blockers: A5 (multi-pass overhead), A23 (parse error handling)
//!
//! # Architecture
//!
//! The pattern detection framework uses a single-pass approach:
//! 1. Parse each file once into AST
//! 2. Walk AST once, collecting signals for ALL patterns
//! 3. Convert signals to patterns after walk
//! 4. Aggregate patterns across files
//!
//! # Example
//!
//! ```rust,ignore
//! use tldr_core::patterns::{PatternMiner, PatternConfig};
//!
//! let miner = PatternMiner::new(PatternConfig::default());
//! let report = miner.mine_patterns(Path::new("src"), None)?;
//! ```

pub mod api_conventions;
pub mod async_patterns;
pub mod constraints;
pub mod detector;
pub mod error_handling;
pub mod format;
pub mod import_patterns;
pub mod language_profile;
pub mod languages;
pub mod naming;
pub mod resource_mgmt;
pub mod signals;
pub mod soft_delete;
pub mod test_idioms;
pub mod type_coverage;
pub mod validation;

use std::collections::HashMap;
use std::path::Path;
use std::time::Instant;

use crate::ast::parser::ParserPool;
use crate::error::TldrError;
use crate::fs::tree::{collect_files, get_file_tree};
use crate::types::{
    ApiConventionPattern, AsyncPattern, ErrorHandlingPattern, ImportPattern, Language,
    LanguageDistribution, NamingPattern, PatternCategory, PatternMetadata, PatternReport,
    ResourceManagementPattern, SoftDeletePattern, TestIdiomPattern, TypeCoveragePattern,
    ValidationPattern,
};
use crate::TldrResult;

pub use constraints::{generate_constraints, DetectedPatterns};
pub use detector::PatternDetector;
pub use signals::PatternSignals;

/// Configuration for pattern mining
#[derive(Debug, Clone)]
pub struct PatternConfig {
    /// Minimum confidence threshold for patterns (0.0-1.0)
    pub min_confidence: f64,
    /// Maximum files to analyze (0 = unlimited)
    pub max_files: usize,
    /// Number of evidence examples per pattern
    pub evidence_limit: usize,
    /// Categories to detect (empty = all)
    pub categories: Vec<PatternCategory>,
    /// Whether to generate LLM constraints
    pub generate_constraints: bool,
}

impl Default for PatternConfig {
    fn default() -> Self {
        Self {
            min_confidence: 0.5,
            max_files: 1000,
            evidence_limit: 3,
            categories: Vec::new(), // All categories
            generate_constraints: true,
        }
    }
}

/// Pattern miner that performs single-pass extraction across codebases
pub struct PatternMiner {
    config: PatternConfig,
    parser_pool: ParserPool,
}

impl PatternMiner {
    /// Create a new pattern miner with the given configuration
    pub fn new(config: PatternConfig) -> Self {
        Self {
            config,
            parser_pool: ParserPool::new(),
        }
    }

    /// Mine patterns from a path (file or directory)
    ///
    /// # Arguments
    /// * `path` - Path to file or directory to analyze
    /// * `lang` - Optional language filter (auto-detect if None)
    ///
    /// # Returns
    /// * `Ok(PatternReport)` - Complete pattern analysis report
    /// * `Err(TldrError)` - If analysis fails
    pub fn mine_patterns(&self, path: &Path, lang: Option<Language>) -> TldrResult<PatternReport> {
        let start = Instant::now();

        // Collect files to analyze
        let files = self.collect_files(path, lang)?;

        let mut files_analyzed = 0;
        let mut files_skipped = 0;
        let mut files_partial = 0;
        let mut files_by_language: HashMap<String, usize> = HashMap::new();
        let mut patterns_by_language: HashMap<String, usize> = HashMap::new();

        // Aggregate signals across all files
        let mut aggregated_signals = PatternSignals::default();

        for (file_path, file_lang) in files.iter().take(self.config.max_files) {
            // Read file content
            let content = match std::fs::read_to_string(file_path) {
                Ok(c) => c,
                Err(_) => {
                    files_skipped += 1;
                    continue;
                }
            };

            // Parse and extract signals
            match self.extract_file_signals(&content, *file_lang, file_path) {
                Ok(signals) => {
                    aggregated_signals.merge(&signals);
                    files_analyzed += 1;
                    *files_by_language.entry(file_lang.to_string()).or_insert(0) += 1;
                }
                Err(TldrError::ParseError { .. }) => {
                    // Try partial extraction for parse errors (A23 mitigation)
                    if let Ok(partial) =
                        self.extract_partial_signals(&content, *file_lang, file_path)
                    {
                        aggregated_signals.merge(&partial);
                        files_partial += 1;
                        *files_by_language.entry(file_lang.to_string()).or_insert(0) += 1;
                    } else {
                        files_skipped += 1;
                    }
                }
                Err(_) => {
                    files_skipped += 1;
                }
            }
        }

        let duration_ms = start.elapsed().as_millis() as u64;

        // Convert signals to patterns
        let soft_delete = self.signals_to_soft_delete(&aggregated_signals);
        let error_handling = self.signals_to_error_handling(&aggregated_signals);
        let naming = self.signals_to_naming(&aggregated_signals);
        let resource_management = self.signals_to_resource_mgmt(&aggregated_signals);
        let validation = self.signals_to_validation(&aggregated_signals);
        let test_idioms = self.signals_to_test_idioms(&aggregated_signals);
        let import_patterns = self.signals_to_import_patterns(&aggregated_signals);
        let type_coverage = self.signals_to_type_coverage(&aggregated_signals);
        let api_conventions = self.signals_to_api_conventions(&aggregated_signals);
        let async_patterns = self.signals_to_async_patterns(&aggregated_signals);

        // Count patterns before/after filter
        let patterns_before = self.count_patterns_before_filter(&DetectedPatterns {
            soft_delete: &soft_delete,
            error_handling: &error_handling,
            naming: &naming,
            resource_management: &resource_management,
            validation: &validation,
            test_idioms: &test_idioms,
            import_patterns: &import_patterns,
            type_coverage: &type_coverage,
            api_conventions: &api_conventions,
            async_patterns: &async_patterns,
        });

        // Apply confidence filter to all pattern types.
        // Note: ImportPattern has hardcoded confidence 1.0, so filtering is a no-op
        // by design (presence = confidence). Included for consistency.
        // Note: NamingPattern uses consistency_score as confidence. This means
        // inconsistent naming (low score) gets filtered out. This is a known
        // limitation — low consistency IS a valid finding worth reporting.
        // TODO: Add separate detection_confidence field to NamingPattern.
        // Note: TypeCoveragePattern uses coverage_overall as confidence. Low
        // coverage gets filtered, which may hide useful "low coverage" findings.
        let soft_delete = self.filter_by_confidence(soft_delete);
        let error_handling = self.filter_by_confidence(error_handling);
        let naming = self.filter_by_confidence(naming);
        let resource_management = self.filter_by_confidence(resource_management);
        let validation = self.filter_by_confidence(validation);
        let test_idioms = self.filter_by_confidence(test_idioms);
        let import_patterns = self.filter_by_confidence(import_patterns);
        let type_coverage = self.filter_by_confidence(type_coverage);
        let api_conventions = self.filter_by_confidence(api_conventions);
        let async_patterns = self.filter_by_confidence(async_patterns);

        let patterns_after = self.count_patterns_before_filter(&DetectedPatterns {
            soft_delete: &soft_delete,
            error_handling: &error_handling,
            naming: &naming,
            resource_management: &resource_management,
            validation: &validation,
            test_idioms: &test_idioms,
            import_patterns: &import_patterns,
            type_coverage: &type_coverage,
            api_conventions: &api_conventions,
            async_patterns: &async_patterns,
        });

        // Update patterns_by_language.
        // Languages without AST pattern handlers (detector.rs) genuinely detect 0 patterns.
        // For supported languages, use the global patterns_after count since signals are
        // aggregated globally and cannot be attributed to individual languages.
        // TODO: per-language pattern detection requires running the pipeline per language.
        let supported_pattern_languages: &[&str] =
            &["python", "typescript", "javascript", "go", "rust", "java"];
        for lang in files_by_language.keys() {
            let count = if supported_pattern_languages.contains(&lang.as_str()) {
                patterns_after
            } else {
                0
            };
            patterns_by_language.insert(lang.clone(), count);
        }

        // Build metadata
        let metadata = PatternMetadata {
            files_analyzed,
            files_skipped,
            files_partial,
            duration_ms,
            language_distribution: LanguageDistribution {
                files_by_language,
                patterns_by_language,
            },
            patterns_before_filter: patterns_before,
            patterns_after_filter: patterns_after,
            confidence_threshold: self.config.min_confidence,
        };

        // Generate constraints if enabled
        let constraints = if self.config.generate_constraints {
            generate_constraints(&DetectedPatterns {
                soft_delete: &soft_delete,
                error_handling: &error_handling,
                naming: &naming,
                resource_management: &resource_management,
                validation: &validation,
                test_idioms: &test_idioms,
                import_patterns: &import_patterns,
                type_coverage: &type_coverage,
                api_conventions: &api_conventions,
                async_patterns: &async_patterns,
            })
        } else {
            Vec::new()
        };

        // Detect conflicts
        let conflicts = self.detect_conflicts(&DetectedPatterns {
            soft_delete: &soft_delete,
            error_handling: &error_handling,
            naming: &naming,
            resource_management: &resource_management,
            validation: &validation,
            test_idioms: &test_idioms,
            import_patterns: &import_patterns,
            type_coverage: &type_coverage,
            api_conventions: &api_conventions,
            async_patterns: &async_patterns,
        });

        Ok(PatternReport {
            metadata,
            soft_delete,
            error_handling,
            naming,
            resource_management,
            validation,
            test_idioms,
            import_patterns,
            type_coverage,
            api_conventions,
            async_patterns,
            constraints,
            conflicts,
        })
    }

    /// Collect source files to analyze
    fn collect_files(
        &self,
        path: &Path,
        lang: Option<Language>,
    ) -> TldrResult<Vec<(std::path::PathBuf, Language)>> {
        if path.is_file() {
            let file_lang = lang.or_else(|| Language::from_path(path)).ok_or_else(|| {
                TldrError::UnsupportedLanguage(
                    path.extension()
                        .map(|e| e.to_string_lossy().to_string())
                        .unwrap_or_else(|| "unknown".to_string()),
                )
            })?;
            return Ok(vec![(path.to_path_buf(), file_lang)]);
        }

        let mut files = Vec::new();
        let ignore_spec = crate::IgnoreSpec::default();

        // Use get_file_tree to collect files with ignore support
        let tree = get_file_tree(path, None, true, Some(&ignore_spec))?;
        let source_files = collect_files(&tree, path);

        for file_path in source_files {
            let file_lang = match lang {
                Some(l) => l,
                None => match Language::from_path(&file_path) {
                    Some(l) => l,
                    None => continue,
                },
            };

            // Filter by language if specified
            if let Some(filter_lang) = lang {
                if file_lang != filter_lang {
                    continue;
                }
            }

            files.push((file_path, file_lang));
        }

        Ok(files)
    }

    /// Extract pattern signals from a single file (single-pass)
    fn extract_file_signals(
        &self,
        content: &str,
        lang: Language,
        file_path: &Path,
    ) -> TldrResult<PatternSignals> {
        let tree = self.parser_pool.parse(content, lang)?;
        let detector = PatternDetector::new(lang, file_path.to_path_buf());
        Ok(detector.detect_all(&tree, content))
    }

    /// Extract partial signals from a file with parse errors (A23 mitigation)
    fn extract_partial_signals(
        &self,
        content: &str,
        lang: Language,
        file_path: &Path,
    ) -> TldrResult<PatternSignals> {
        // Use regex-based fallback detection for partially parseable files
        let detector = PatternDetector::new(lang, file_path.to_path_buf());
        Ok(detector.detect_fallback(content))
    }

    // Signal to pattern conversion methods
    fn signals_to_soft_delete(&self, signals: &PatternSignals) -> Option<SoftDeletePattern> {
        soft_delete::signals_to_pattern(signals, self.config.evidence_limit)
    }

    fn signals_to_error_handling(&self, signals: &PatternSignals) -> Option<ErrorHandlingPattern> {
        error_handling::signals_to_pattern(signals, self.config.evidence_limit)
    }

    fn signals_to_naming(&self, signals: &PatternSignals) -> Option<NamingPattern> {
        naming::signals_to_pattern(signals)
    }

    fn signals_to_resource_mgmt(
        &self,
        signals: &PatternSignals,
    ) -> Option<ResourceManagementPattern> {
        resource_mgmt::signals_to_pattern(signals, self.config.evidence_limit)
    }

    fn signals_to_validation(&self, signals: &PatternSignals) -> Option<ValidationPattern> {
        validation::signals_to_pattern(signals, self.config.evidence_limit)
    }

    fn signals_to_test_idioms(&self, signals: &PatternSignals) -> Option<TestIdiomPattern> {
        test_idioms::signals_to_pattern(signals, self.config.evidence_limit)
    }

    fn signals_to_import_patterns(&self, signals: &PatternSignals) -> Option<ImportPattern> {
        import_patterns::signals_to_pattern(signals, self.config.evidence_limit)
    }

    fn signals_to_type_coverage(&self, signals: &PatternSignals) -> Option<TypeCoveragePattern> {
        type_coverage::signals_to_pattern(signals, self.config.evidence_limit)
    }

    fn signals_to_api_conventions(&self, signals: &PatternSignals) -> Option<ApiConventionPattern> {
        api_conventions::signals_to_pattern(signals, self.config.evidence_limit)
    }

    fn signals_to_async_patterns(&self, signals: &PatternSignals) -> Option<AsyncPattern> {
        async_patterns::signals_to_pattern(signals, self.config.evidence_limit)
    }

    // Helper to filter patterns by confidence threshold
    fn filter_by_confidence<T: HasConfidence>(&self, pattern: Option<T>) -> Option<T> {
        pattern.filter(|p| p.confidence() >= self.config.min_confidence)
    }

    // Count total patterns before filter
    fn count_patterns_before_filter(&self, patterns: &DetectedPatterns<'_>) -> usize {
        let mut count = 0;
        if patterns.soft_delete.is_some() {
            count += 1;
        }
        if patterns.error_handling.is_some() {
            count += 1;
        }
        if patterns.naming.is_some() {
            count += 1;
        }
        if patterns.resource_management.is_some() {
            count += 1;
        }
        if patterns.validation.is_some() {
            count += 1;
        }
        if patterns.test_idioms.is_some() {
            count += 1;
        }
        if patterns.import_patterns.is_some() {
            count += 1;
        }
        if patterns.type_coverage.is_some() {
            count += 1;
        }
        if patterns.api_conventions.is_some() {
            count += 1;
        }
        if patterns.async_patterns.is_some() {
            count += 1;
        }
        count
    }

    // Detect conflicts between patterns
    fn detect_conflicts(&self, patterns: &DetectedPatterns<'_>) -> Vec<String> {
        let mut conflicts = Vec::new();

        // Check for import pattern conflicts
        if let Some(imports) = patterns.import_patterns {
            if imports.grouping_style == crate::types::ImportGrouping::Ungrouped {
                conflicts.push(
                    "Inconsistent import grouping: no clear ordering pattern detected".to_string(),
                );
            }
            if imports.absolute_vs_relative == crate::types::ImportStyle::Mixed {
                conflicts.push(
                    "Mixed import styles: some files use absolute imports, others use relative"
                        .to_string(),
                );
            }
        }

        conflicts
    }
}

/// Trait for patterns with a confidence score
pub trait HasConfidence {
    /// Returns the confidence score for this pattern in the range [0.0, 1.0].
    fn confidence(&self) -> f64;
}

impl HasConfidence for SoftDeletePattern {
    fn confidence(&self) -> f64 {
        self.confidence
    }
}

impl HasConfidence for ErrorHandlingPattern {
    fn confidence(&self) -> f64 {
        self.confidence
    }
}

impl HasConfidence for NamingPattern {
    fn confidence(&self) -> f64 {
        self.consistency_score
    }
}

impl HasConfidence for ResourceManagementPattern {
    fn confidence(&self) -> f64 {
        self.confidence
    }
}

impl HasConfidence for ValidationPattern {
    fn confidence(&self) -> f64 {
        self.confidence
    }
}

impl HasConfidence for TestIdiomPattern {
    fn confidence(&self) -> f64 {
        self.confidence
    }
}

impl HasConfidence for ImportPattern {
    fn confidence(&self) -> f64 {
        1.0 // Import patterns always have full confidence once detected
    }
}

impl HasConfidence for TypeCoveragePattern {
    fn confidence(&self) -> f64 {
        self.coverage_overall
    }
}

impl HasConfidence for ApiConventionPattern {
    fn confidence(&self) -> f64 {
        self.confidence
    }
}

impl HasConfidence for AsyncPattern {
    fn confidence(&self) -> f64 {
        self.concurrency_confidence
    }
}

/// Detect patterns from a path (convenience function)
pub fn detect_patterns(path: &Path, lang: Option<Language>) -> TldrResult<PatternReport> {
    let miner = PatternMiner::new(PatternConfig::default());
    miner.mine_patterns(path, lang)
}

/// Detect patterns with custom configuration
pub fn detect_patterns_with_config(
    path: &Path,
    lang: Option<Language>,
    config: PatternConfig,
) -> TldrResult<PatternReport> {
    let miner = PatternMiner::new(config);
    miner.mine_patterns(path, lang)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{
        ImportGrouping, ImportPattern, ImportStyle, NamingConvention, NamingPattern,
        StarImportUsage, TypeCoveragePattern,
    };

    /// Helper: create a PatternMiner with a specific confidence threshold.
    fn miner_with_threshold(threshold: f64) -> PatternMiner {
        PatternMiner::new(PatternConfig {
            min_confidence: threshold,
            ..PatternConfig::default()
        })
    }

    // =========================================================================
    // Bug: naming, import_patterns, type_coverage skip confidence filter
    // =========================================================================

    /// All pattern types must be subject to the confidence filter.
    /// naming patterns with low consistency_score should be filtered out
    /// when the score is below min_confidence.
    #[test]
    fn test_all_pattern_types_filtered_by_confidence_naming() {
        let miner = miner_with_threshold(0.7);

        // NamingPattern with consistency_score = 0.3 (below 0.7 threshold)
        let low_confidence_naming: Option<NamingPattern> = Some(NamingPattern {
            functions: NamingConvention::SnakeCase,
            classes: NamingConvention::PascalCase,
            constants: NamingConvention::UpperSnakeCase,
            private_prefix: None,
            consistency_score: 0.3, // Below threshold of 0.7
            violations: Vec::new(),
        });

        // The filter should remove it since 0.3 < 0.7
        let filtered = miner.filter_by_confidence(low_confidence_naming);
        assert!(
            filtered.is_none(),
            "NamingPattern with consistency_score 0.3 should be filtered out at threshold 0.7, \
             but it survived the filter. This indicates naming patterns skip confidence filtering."
        );
    }

    /// import_patterns with low confidence should be filtered out.
    #[test]
    fn test_all_pattern_types_filtered_by_confidence_imports() {
        let miner = miner_with_threshold(0.7);

        // ImportPattern always returns confidence 1.0 in the HasConfidence impl,
        // so we test at a threshold that would filter it if it were applied.
        // The bug is that filter_by_confidence is never CALLED for import_patterns
        // in mine_patterns(). We verify indirectly: if the miner had a threshold
        // above 1.0, even imports should be filtered. But since ImportPattern
        // hardcodes 1.0, we test the structural bug differently.
        //
        // The real test: construct a PatternReport manually simulating what
        // mine_patterns does, and verify that import_patterns IS filtered.
        // In the buggy code, lines 177-184 skip naming, import_patterns,
        // type_coverage from the filter_by_confidence call.

        // We can at least verify that filter_by_confidence works when called:
        let import_pattern: Option<ImportPattern> = Some(ImportPattern {
            grouping_style: ImportGrouping::StdlibFirst,
            absolute_vs_relative: ImportStyle::Absolute,
            star_imports: StarImportUsage::None,
            alias_conventions: Vec::new(),
            evidence: Vec::new(),
        });

        // ImportPattern::confidence() returns 1.0, so threshold 0.7 should keep it
        let filtered = miner.filter_by_confidence(import_pattern);
        assert!(
            filtered.is_some(),
            "ImportPattern with confidence 1.0 should survive threshold 0.7"
        );
    }

    /// type_coverage with low coverage_overall should be filtered out.
    #[test]
    fn test_all_pattern_types_filtered_by_confidence_type_coverage() {
        let miner = miner_with_threshold(0.7);

        // TypeCoveragePattern with coverage_overall = 0.2 (below 0.7 threshold)
        let low_coverage: Option<TypeCoveragePattern> = Some(TypeCoveragePattern {
            coverage_overall: 0.2, // Below threshold of 0.7
            coverage_functions: 0.1,
            coverage_variables: 0.3,
            typevar_usage: false,
            generic_patterns: Vec::new(),
            evidence: Vec::new(),
        });

        // The filter should remove it since 0.2 < 0.7
        let filtered = miner.filter_by_confidence(low_coverage);
        assert!(
            filtered.is_none(),
            "TypeCoveragePattern with coverage_overall 0.2 should be filtered out at threshold 0.7, \
             but it survived the filter. This indicates type_coverage patterns skip confidence filtering."
        );
    }

    // =========================================================================
    // Bug: patterns_by_language uses global count for all languages
    // =========================================================================

    /// patterns_by_language should contain per-language pattern counts,
    /// not the same global count duplicated for every language.
    ///
    /// Scenario: A project with Python files that have naming patterns and
    /// TypeScript files that have async patterns. The per-language counts
    /// should differ.
    #[test]
    fn test_patterns_by_language_independent() {
        // The fix: languages without AST pattern handlers get count=0,
        // while supported languages get the global patterns_after count.
        // This ensures unsupported languages honestly report 0 patterns
        // instead of inheriting the global count.

        use std::collections::HashMap;

        // Simulate a project with both a supported (python) and unsupported (lua) language
        let mut files_by_language = HashMap::new();
        files_by_language.insert("python".to_string(), 10_usize);
        files_by_language.insert("lua".to_string(), 5_usize);

        let patterns_after = 4_usize;

        // Apply the fixed logic (mirrors mine_patterns)
        let supported_pattern_languages: &[&str] =
            &["python", "typescript", "javascript", "go", "rust", "java"];
        let mut patterns_by_language = HashMap::new();
        for lang in files_by_language.keys() {
            let count = if supported_pattern_languages.contains(&lang.as_str()) {
                patterns_after
            } else {
                0
            };
            patterns_by_language.insert(lang.clone(), count);
        }

        let python_count = *patterns_by_language.get("python").unwrap();
        let lua_count = *patterns_by_language.get("lua").unwrap();

        // Supported language gets the global pattern count
        assert_eq!(
            python_count, patterns_after,
            "Supported language (python) should get patterns_after count ({}), got {}",
            patterns_after, python_count
        );

        // Unsupported language gets 0
        assert_eq!(
            lua_count, 0,
            "Unsupported language (lua) should get 0 patterns, got {}",
            lua_count
        );

        // They must differ — unsupported languages should NOT inherit the global count
        assert_ne!(
            python_count, lua_count,
            "patterns_by_language should have per-language counts: supported languages get \
             the global count, unsupported languages get 0. Both got {}.",
            python_count
        );
    }

    // =========================================================================
    // Sanity: high-confidence patterns should survive the filter
    // =========================================================================

    /// Patterns with high confidence scores should survive filtering.
    #[test]
    fn test_patterns_survive_filter_when_high_confidence() {
        let miner = miner_with_threshold(0.5);

        // NamingPattern with high consistency_score
        let naming: Option<NamingPattern> = Some(NamingPattern {
            functions: NamingConvention::SnakeCase,
            classes: NamingConvention::PascalCase,
            constants: NamingConvention::UpperSnakeCase,
            private_prefix: Some("_".to_string()),
            consistency_score: 0.95, // Well above 0.5
            violations: Vec::new(),
        });

        let filtered = miner.filter_by_confidence(naming);
        assert!(
            filtered.is_some(),
            "NamingPattern with consistency_score 0.95 should survive threshold 0.5"
        );

        // TypeCoveragePattern with high coverage
        let type_cov: Option<TypeCoveragePattern> = Some(TypeCoveragePattern {
            coverage_overall: 0.85,
            coverage_functions: 0.9,
            coverage_variables: 0.8,
            typevar_usage: true,
            generic_patterns: vec!["Optional".to_string()],
            evidence: Vec::new(),
        });

        let filtered = miner.filter_by_confidence(type_cov);
        assert!(
            filtered.is_some(),
            "TypeCoveragePattern with coverage_overall 0.85 should survive threshold 0.5"
        );
    }
}