go-brrr 0.1.0

Token-efficient code analysis for LLMs - Rust implementation
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
//! Semantic search type definitions.
//!
//! Core data structures for semantic code search and embedding.
//! Mirrors the Python implementation in brrr/semantic.py.

use serde::{Deserialize, Serialize};
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};

// =============================================================================
// Token Budget Constants
// =============================================================================

/// Maximum tokens for embedding text (conservative limit for good retrieval quality).
/// Qwen3 supports 32K, TEI configured for 16K, but 8K gives best results.
pub const MAX_EMBEDDING_TOKENS: usize = 8192;

/// Maximum tokens for code preview, leaving room for metadata in embedding text.
pub const MAX_CODE_PREVIEW_TOKENS: usize = 6000;

/// Token overlap between chunks for context continuity.
pub const CHUNK_OVERLAP_TOKENS: usize = 200;

// =============================================================================
// Semantic Pattern Definitions
// =============================================================================

/// Semantic pattern categories for automatic code tagging.
/// Each pattern is a regex that matches code containing that semantic concept.
#[derive(Debug, Clone)]
pub struct SemanticPattern {
    /// Pattern category name (e.g., "crud", "validation")
    pub name: &'static str,
    /// Regex pattern to match
    pub pattern: &'static str,
}

/// All semantic patterns for code tagging.
/// These patterns detect common code categories for semantic enrichment.
pub static SEMANTIC_PATTERNS: &[SemanticPattern] = &[
    // Data operations
    SemanticPattern {
        name: "crud",
        pattern: r"\b(create|read|update|delete|insert|select|save|load|fetch|store|persist|get|set|add|remove)\b",
    },
    SemanticPattern {
        name: "validation",
        pattern: r"\b(valid|validate|check|verify|assert|ensure|sanitize|normalize|parse|format)\b",
    },
    SemanticPattern {
        name: "transform",
        pattern: r"\b(convert|transform|map|reduce|filter|sort|merge|split|join|serialize|deserialize)\b",
    },
    // Control flow patterns
    SemanticPattern {
        name: "error_handling",
        pattern: r"\b(try|catch|except|raise|throw|error|exception|fail|panic)\b",
    },
    SemanticPattern {
        name: "async_ops",
        pattern: r"\b(async|await|promise|future|callback|then|concurrent|parallel|thread)\b",
    },
    SemanticPattern {
        name: "iteration",
        pattern: r"\b(for|while|loop|iterate|each|map|reduce|filter)\b",
    },
    // Architecture patterns
    SemanticPattern {
        name: "api_endpoint",
        pattern: r"\b(route|endpoint|handler|controller|get|post|put|delete|patch|request|response)\b",
    },
    SemanticPattern {
        name: "database",
        pattern: r"\b(query|sql|select|insert|update|delete|table|schema|migration|model|entity)\b",
    },
    SemanticPattern {
        name: "auth",
        pattern: r"\b(auth|login|logout|session|token|jwt|oauth|permission|role|access)\b",
    },
    SemanticPattern {
        name: "cache",
        pattern: r"\b(cache|memoize|memo|store|redis|memcache|ttl|expire|invalidate)\b",
    },
    // Code quality
    SemanticPattern {
        name: "test",
        pattern: r"\b(test|spec|mock|stub|assert|expect|should|describe|it)\b",
    },
    SemanticPattern {
        name: "logging",
        pattern: r"\b(log|logger|debug|info|warn|error|trace|print|console)\b",
    },
    SemanticPattern {
        name: "config",
        pattern: r"\b(config|setting|option|env|environment|parameter|argument)\b",
    },
];

// =============================================================================
// Core Types
// =============================================================================

/// Kind of code unit for embedding.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum UnitKind {
    /// Top-level function
    Function,
    /// Class method
    Method,
    /// Class or struct definition
    Class,
    /// Module-level code or file summary
    Module,
    /// Chunk of an oversized unit
    Chunk,
}

impl UnitKind {
    /// Convert to string representation.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Function => "function",
            Self::Method => "method",
            Self::Class => "class",
            Self::Module => "module",
            Self::Chunk => "chunk",
        }
    }
}

impl std::fmt::Display for UnitKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Code complexity metrics (heuristic analysis without full AST parsing).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CodeComplexity {
    /// Maximum nesting depth
    pub depth: usize,
    /// Number of branch statements (if, elif, else, case, switch, match)
    pub branches: usize,
    /// Number of loop statements (for, while, loop)
    pub loops: usize,
}

impl CodeComplexity {
    /// Create empty complexity metrics.
    #[must_use]
    pub fn empty() -> Self {
        Self::default()
    }

    /// Check if the code has notable complexity.
    #[must_use]
    pub fn is_complex(&self) -> bool {
        self.depth > 3 || self.branches > 5 || self.loops > 2
    }

    /// Generate a natural language description of complexity.
    #[must_use]
    pub fn describe(&self) -> Option<String> {
        let mut parts = Vec::new();
        if self.depth > 3 {
            parts.push("deep nesting");
        }
        if self.branches > 5 {
            parts.push("many branches");
        }
        if self.loops > 2 {
            parts.push("multiple loops");
        }
        if parts.is_empty() {
            None
        } else {
            Some(parts.join(", "))
        }
    }
}

/// A code unit for semantic embedding.
///
/// Contains information from all 5 brrr analysis layers plus semantic enrichment.
/// This is the primary unit of indexing and retrieval in semantic search.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmbeddingUnit {
    /// Unique identifier (typically file::qualified_name or file::name#chunkN)
    pub id: String,

    /// Source file path (relative to project root)
    pub file: String,

    /// Simple name (function/class/method name)
    pub name: String,

    /// Kind of code unit
    pub kind: UnitKind,

    /// Full code content (may be truncated for large units)
    pub code: String,

    /// Function/method signature or class declaration
    pub signature: String,

    /// Docstring or documentation comment
    pub docstring: Option<String>,

    /// Starting line number (1-indexed)
    pub start_line: usize,

    /// Ending line number (1-indexed)
    pub end_line: usize,

    /// Token count for this unit's code
    pub token_count: usize,

    /// Semantic tags detected from code patterns
    pub semantic_tags: Vec<String>,

    /// Parent unit name (for chunks and methods)
    pub parent: Option<String>,

    // ==========================================================================
    // Extended metadata from brrr layers
    // ==========================================================================
    /// L1: Programming language
    pub language: String,

    /// L2: Functions this unit calls
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub calls: Vec<String>,

    /// L2: Functions that call this unit
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub called_by: Vec<String>,

    /// L3: CFG summary (complexity, block count)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub cfg_summary: String,

    /// L4: DFG summary (variable count, def-use chains)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub dfg_summary: String,

    /// L5: Dependencies (imported modules)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub dependencies: String,

    /// Code complexity metrics
    #[serde(default)]
    pub complexity: CodeComplexity,

    /// Chunk index (0-indexed, for chunked units)
    #[serde(default)]
    pub chunk_index: usize,

    /// Total number of chunks (1 for non-chunked units)
    #[serde(default = "default_chunk_total")]
    pub chunk_total: usize,
}

fn default_chunk_total() -> usize {
    1
}

impl EmbeddingUnit {
    /// Create a new embedding unit with minimal required fields.
    #[must_use]
    pub fn new(
        file: impl Into<String>,
        name: impl Into<String>,
        kind: UnitKind,
        code: impl Into<String>,
        start_line: usize,
        language: impl Into<String>,
    ) -> Self {
        let name = name.into();
        let file = file.into();
        let code = code.into();

        Self {
            id: format!("{}::{}", file, name),
            file,
            name,
            kind,
            code,
            signature: String::new(),
            docstring: None,
            start_line,
            end_line: start_line,
            token_count: 0,
            semantic_tags: Vec::new(),
            parent: None,
            language: language.into(),
            calls: Vec::new(),
            called_by: Vec::new(),
            cfg_summary: String::new(),
            dfg_summary: String::new(),
            dependencies: String::new(),
            complexity: CodeComplexity::default(),
            chunk_index: 0,
            chunk_total: 1,
        }
    }

    /// Check if this unit is a chunk of a larger unit.
    #[must_use]
    pub fn is_chunk(&self) -> bool {
        self.chunk_total > 1
    }

    /// Check if this unit needs to be split into chunks based on token count.
    #[must_use]
    pub fn needs_chunking(&self) -> bool {
        self.token_count > MAX_EMBEDDING_TOKENS
    }

    /// Get the qualified name (file::name or file::parent.name for methods).
    #[must_use]
    pub fn qualified_name(&self) -> String {
        match &self.parent {
            Some(parent) if self.kind == UnitKind::Method => {
                format!("{}::{}.{}", self.file, parent, self.name)
            }
            _ => format!("{}::{}", self.file, self.name),
        }
    }

    /// Convert to a HashMap for JSON serialization.
    #[must_use]
    pub fn to_map(&self) -> HashMap<String, serde_json::Value> {
        serde_json::to_value(self)
            .ok()
            .and_then(|v| v.as_object().cloned())
            .map(|m| m.into_iter().collect())
            .unwrap_or_default()
    }
}

/// Result of a semantic search query.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
    /// The matching code unit
    pub unit: EmbeddingUnit,

    /// Similarity score (0.0 to 1.0, higher is better)
    pub score: f32,

    /// Highlighted portions of code that matched (optional)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub highlights: Vec<String>,
}

impl SearchResult {
    /// Create a new search result.
    #[must_use]
    pub fn new(unit: EmbeddingUnit, score: f32) -> Self {
        Self {
            unit,
            score,
            highlights: Vec::new(),
        }
    }

    /// Create a search result with highlights.
    #[must_use]
    pub fn with_highlights(unit: EmbeddingUnit, score: f32, highlights: Vec<String>) -> Self {
        Self {
            unit,
            score,
            highlights,
        }
    }
}

/// Information about a chunk split from an oversized unit.
#[derive(Debug, Clone)]
pub struct ChunkInfo {
    /// Chunk text content
    pub text: String,
    /// Start character offset in original code
    pub start_char: usize,
    /// End character offset in original code
    pub end_char: usize,
}

impl ChunkInfo {
    /// Create a new chunk info.
    #[must_use]
    pub fn new(text: String, start_char: usize, end_char: usize) -> Self {
        Self {
            text,
            start_char,
            end_char,
        }
    }
}

// =============================================================================
// Content-Hashed Index for Deduplication
// =============================================================================

/// Location information for a code unit (file, function name, line).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CodeLocation {
    /// Source file path
    pub file: String,
    /// Function or code unit name
    pub name: String,
    /// Line number (1-indexed)
    pub line: usize,
}

impl CodeLocation {
    /// Create a new code location.
    #[must_use]
    pub fn new(file: impl Into<String>, name: impl Into<String>, line: usize) -> Self {
        Self {
            file: file.into(),
            name: name.into(),
            line,
        }
    }
}

/// Content-hashed index for deduplication of code units.
///
/// Used to avoid indexing identical code multiple times. The index normalizes
/// whitespace before hashing, so code with different formatting but identical
/// content will be detected as duplicates.
///
/// # Examples
///
/// ```
/// use go_brrr::semantic::ContentHashedIndex;
///
/// let mut index = ContentHashedIndex::new();
///
/// // First occurrence is added
/// assert!(index.add("def foo(): pass", "src/a.py", "foo", 10));
///
/// // Identical content is detected as duplicate
/// assert!(!index.add("def foo(): pass", "src/b.py", "foo", 20));
///
/// // Check stats
/// let (unique, duplicates) = index.stats();
/// assert_eq!(unique, 1);
/// assert_eq!(duplicates, 1);
/// ```
#[derive(Debug, Clone, Default)]
pub struct ContentHashedIndex {
    /// Hash -> original location (file, function_name, line)
    seen: HashMap<u64, CodeLocation>,
    /// Number of duplicate items detected
    pub duplicates_found: usize,
    /// Number of unique items indexed
    pub unique_items: usize,
}

impl ContentHashedIndex {
    /// Create a new empty content-hashed index.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Compute content hash for code with whitespace normalization.
    ///
    /// Normalizes code by:
    /// - Trimming each line
    /// - Removing empty lines
    /// - Joining with single newlines
    ///
    /// This ensures code with different indentation or blank lines
    /// but identical content produces the same hash.
    fn hash_content(content: &str) -> u64 {
        let mut hasher = DefaultHasher::new();
        let normalized: String = content
            .lines()
            .map(|l| l.trim())
            .filter(|l| !l.is_empty())
            .collect::<Vec<_>>()
            .join("\n");
        normalized.hash(&mut hasher);
        hasher.finish()
    }

    /// Check if content is a duplicate, returning the original location if so.
    ///
    /// # Arguments
    ///
    /// * `content` - Code content to check
    ///
    /// # Returns
    ///
    /// `Some(&CodeLocation)` if this content was already seen, `None` otherwise.
    #[must_use]
    pub fn check_duplicate(&self, content: &str) -> Option<&CodeLocation> {
        let hash = Self::hash_content(content);
        self.seen.get(&hash)
    }

    /// Add content to the index.
    ///
    /// # Arguments
    ///
    /// * `content` - Code content to add
    /// * `file` - Source file path
    /// * `function_name` - Name of the function or code unit
    /// * `line` - Line number (1-indexed)
    ///
    /// # Returns
    ///
    /// `true` if this is new content (was added), `false` if duplicate (was not added).
    pub fn add(
        &mut self,
        content: &str,
        file: &str,
        function_name: &str,
        line: usize,
    ) -> bool {
        let hash = Self::hash_content(content);

        if self.seen.contains_key(&hash) {
            self.duplicates_found += 1;
            false
        } else {
            self.seen.insert(
                hash,
                CodeLocation::new(file, function_name, line),
            );
            self.unique_items += 1;
            true
        }
    }

    /// Get deduplication statistics.
    ///
    /// # Returns
    ///
    /// Tuple of (unique_items, duplicates_found).
    #[must_use]
    pub fn stats(&self) -> (usize, usize) {
        (self.unique_items, self.duplicates_found)
    }

    /// Get the number of unique items in the index.
    #[must_use]
    pub fn len(&self) -> usize {
        self.seen.len()
    }

    /// Check if the index is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.seen.is_empty()
    }

    /// Clear the index and reset statistics.
    pub fn clear(&mut self) {
        self.seen.clear();
        self.duplicates_found = 0;
        self.unique_items = 0;
    }

    /// Get the deduplication ratio (duplicates / total).
    ///
    /// Returns 0.0 if no items have been processed.
    #[must_use]
    pub fn dedup_ratio(&self) -> f64 {
        let total = self.unique_items + self.duplicates_found;
        if total == 0 {
            0.0
        } else {
            self.duplicates_found as f64 / total as f64
        }
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_unit_kind_as_str() {
        assert_eq!(UnitKind::Function.as_str(), "function");
        assert_eq!(UnitKind::Method.as_str(), "method");
        assert_eq!(UnitKind::Class.as_str(), "class");
        assert_eq!(UnitKind::Module.as_str(), "module");
        assert_eq!(UnitKind::Chunk.as_str(), "chunk");
    }

    #[test]
    fn test_unit_kind_display() {
        assert_eq!(format!("{}", UnitKind::Function), "function");
    }

    #[test]
    fn test_code_complexity_describe() {
        let simple = CodeComplexity {
            depth: 2,
            branches: 3,
            loops: 1,
        };
        assert!(simple.describe().is_none());

        let complex = CodeComplexity {
            depth: 5,
            branches: 10,
            loops: 4,
        };
        let desc = complex.describe().unwrap();
        assert!(desc.contains("deep nesting"));
        assert!(desc.contains("many branches"));
        assert!(desc.contains("multiple loops"));
    }

    #[test]
    fn test_embedding_unit_new() {
        let unit = EmbeddingUnit::new(
            "src/main.py",
            "process_data",
            UnitKind::Function,
            "def process_data(): pass",
            10,
            "python",
        );

        assert_eq!(unit.id, "src/main.py::process_data");
        assert_eq!(unit.file, "src/main.py");
        assert_eq!(unit.name, "process_data");
        assert_eq!(unit.kind, UnitKind::Function);
        assert_eq!(unit.start_line, 10);
        assert_eq!(unit.language, "python");
        assert!(!unit.is_chunk());
    }

    #[test]
    fn test_embedding_unit_qualified_name() {
        let mut unit = EmbeddingUnit::new(
            "src/model.py",
            "save",
            UnitKind::Method,
            "def save(self): pass",
            20,
            "python",
        );
        unit.parent = Some("User".to_string());

        assert_eq!(unit.qualified_name(), "src/model.py::User.save");
    }

    #[test]
    fn test_embedding_unit_is_chunk() {
        let mut unit = EmbeddingUnit::new(
            "src/large.py",
            "big_function[1/3]",
            UnitKind::Chunk,
            "# chunk 1",
            1,
            "python",
        );
        unit.chunk_index = 0;
        unit.chunk_total = 3;

        assert!(unit.is_chunk());
    }

    #[test]
    fn test_search_result() {
        let unit = EmbeddingUnit::new(
            "test.py",
            "test_fn",
            UnitKind::Function,
            "def test_fn(): pass",
            1,
            "python",
        );
        let result = SearchResult::new(unit.clone(), 0.95);

        assert_eq!(result.score, 0.95);
        assert!(result.highlights.is_empty());

        let result_with_highlights =
            SearchResult::with_highlights(unit, 0.95, vec!["highlighted text".to_string()]);
        assert_eq!(result_with_highlights.highlights.len(), 1);
    }

    #[test]
    fn test_semantic_patterns_defined() {
        assert!(!SEMANTIC_PATTERNS.is_empty());

        // Check some key patterns exist
        let pattern_names: Vec<_> = SEMANTIC_PATTERNS.iter().map(|p| p.name).collect();
        assert!(pattern_names.contains(&"crud"));
        assert!(pattern_names.contains(&"validation"));
        assert!(pattern_names.contains(&"error_handling"));
        assert!(pattern_names.contains(&"async_ops"));
    }

    #[test]
    fn test_constants() {
        assert!(MAX_EMBEDDING_TOKENS > 0);
        assert!(MAX_CODE_PREVIEW_TOKENS < MAX_EMBEDDING_TOKENS);
        assert!(CHUNK_OVERLAP_TOKENS < MAX_CODE_PREVIEW_TOKENS);
    }

    #[test]
    fn test_code_location_new() {
        let loc = CodeLocation::new("src/main.py", "process", 42);
        assert_eq!(loc.file, "src/main.py");
        assert_eq!(loc.name, "process");
        assert_eq!(loc.line, 42);
    }

    #[test]
    fn test_content_hashed_index_new() {
        let index = ContentHashedIndex::new();
        assert!(index.is_empty());
        assert_eq!(index.len(), 0);
        assert_eq!(index.unique_items, 0);
        assert_eq!(index.duplicates_found, 0);
    }

    #[test]
    fn test_content_hashed_index_add_unique() {
        let mut index = ContentHashedIndex::new();

        // First item should be added
        assert!(index.add("def foo(): pass", "src/a.py", "foo", 10));
        assert_eq!(index.unique_items, 1);
        assert_eq!(index.duplicates_found, 0);
        assert_eq!(index.len(), 1);

        // Different content should also be added
        assert!(index.add("def bar(): return 1", "src/b.py", "bar", 20));
        assert_eq!(index.unique_items, 2);
        assert_eq!(index.duplicates_found, 0);
        assert_eq!(index.len(), 2);
    }

    #[test]
    fn test_content_hashed_index_detect_duplicate() {
        let mut index = ContentHashedIndex::new();

        // Add first occurrence
        assert!(index.add("def foo(): pass", "src/a.py", "foo", 10));

        // Same content in different file is duplicate
        assert!(!index.add("def foo(): pass", "src/b.py", "foo", 20));
        assert_eq!(index.unique_items, 1);
        assert_eq!(index.duplicates_found, 1);
    }

    #[test]
    fn test_content_hashed_index_whitespace_normalization() {
        let mut index = ContentHashedIndex::new();

        // Add with specific indentation
        let code1 = "def foo():\n    return 1";
        assert!(index.add(code1, "src/a.py", "foo", 10));

        // Same code with different indentation is duplicate
        let code2 = "  def foo():\n        return 1  ";
        assert!(!index.add(code2, "src/b.py", "foo", 20));

        // Same code with extra blank lines is duplicate
        let code3 = "def foo():\n\n    return 1\n\n";
        assert!(!index.add(code3, "src/c.py", "foo", 30));

        assert_eq!(index.unique_items, 1);
        assert_eq!(index.duplicates_found, 2);
    }

    #[test]
    fn test_content_hashed_index_check_duplicate() {
        let mut index = ContentHashedIndex::new();

        // Initially nothing is duplicate
        assert!(index.check_duplicate("def foo(): pass").is_none());

        // Add content
        index.add("def foo(): pass", "src/a.py", "foo", 10);

        // Now it should be detected
        let loc = index.check_duplicate("def foo(): pass").unwrap();
        assert_eq!(loc.file, "src/a.py");
        assert_eq!(loc.name, "foo");
        assert_eq!(loc.line, 10);
    }

    #[test]
    fn test_content_hashed_index_stats() {
        let mut index = ContentHashedIndex::new();

        index.add("code1", "f1.py", "fn1", 1);
        index.add("code2", "f2.py", "fn2", 2);
        index.add("code1", "f3.py", "fn1", 3); // duplicate
        index.add("code3", "f4.py", "fn3", 4);
        index.add("code2", "f5.py", "fn2", 5); // duplicate

        let (unique, dups) = index.stats();
        assert_eq!(unique, 3);
        assert_eq!(dups, 2);
    }

    #[test]
    fn test_content_hashed_index_dedup_ratio() {
        let mut index = ContentHashedIndex::new();

        // Empty index has 0 ratio
        assert_eq!(index.dedup_ratio(), 0.0);

        // 3 unique, 2 duplicates = 2/5 = 0.4
        index.add("code1", "f1.py", "fn1", 1);
        index.add("code2", "f2.py", "fn2", 2);
        index.add("code1", "f3.py", "fn1", 3);
        index.add("code3", "f4.py", "fn3", 4);
        index.add("code2", "f5.py", "fn2", 5);

        assert!((index.dedup_ratio() - 0.4).abs() < 0.001);
    }

    #[test]
    fn test_content_hashed_index_clear() {
        let mut index = ContentHashedIndex::new();

        index.add("code1", "f1.py", "fn1", 1);
        index.add("code1", "f2.py", "fn1", 2);

        assert!(!index.is_empty());
        assert_eq!(index.unique_items, 1);
        assert_eq!(index.duplicates_found, 1);

        index.clear();

        assert!(index.is_empty());
        assert_eq!(index.unique_items, 0);
        assert_eq!(index.duplicates_found, 0);

        // After clear, same content is unique again
        assert!(index.add("code1", "f1.py", "fn1", 1));
    }
}