cora-code 0.13.0

CLI-first AI code review — BYOK, diff/scan/branch, pre-commit hooks
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
//! Symbol extraction from source code lines.
//!
//! Uses language-aware regex patterns to extract function calls, type
//! references, and import statements from changed diff lines. No full
//! AST parsing — regex is sufficient for the deterministic context chain
//! and avoids pulling in heavy dependencies like `syn`.

use std::collections::HashSet;

use regex::Regex;
use std::sync::LazyLock;
use tracing::debug;

use super::types::{ExtractedSymbol, SymbolKind};

// --- Regex patterns by language ---

/// Rust: `use crate::foo::bar` or `use super::baz` or `mod foo`
static RE_RUST_IMPORT: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\buse\s+(crate|super|self)::([\w:]+)").unwrap());

/// Rust: module declaration `mod foo;` or `mod foo {` (#73).
static RE_RUST_MOD: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\bmod\s+(\w+)").unwrap());

/// Rust: function call — identifier followed by `(`, possibly preceded by `::` or `.`
static RE_RUST_FN_CALL: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"(?:\w+::)?\w+(?:::\w+)*\s*\(").unwrap());

/// Rust: type-like references — `Type`, `Option<T>`, `Vec<String>`, after `: `, `<`, or `as`
static RE_RUST_TYPE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"(?:(?::\s)|(?:<)|(?:as\s)|(?:->\s))([A-Z]\w+)").unwrap());

/// Python: `import foo` or `from foo import bar`
static RE_PY_IMPORT: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\b(?:from|import)\s+([\w.]+)").unwrap());

/// Python: function call — `foo(` or `obj.method(`
static RE_PY_FN_CALL: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\b(\w+)\s*\(").unwrap());

/// Python: type annotation — `: SomeType` or `-> ReturnType`
static RE_PY_TYPE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"(?::\s*[A-Z]\w*|->\s*[A-Z]\w*)").unwrap());

/// JS/TS: `import ... from '...'`, `require('...')`, or dynamic `import('...')`
static RE_JS_IMPORT: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r#"(?:import\s+.*?\s+from\s*['"]([^'"]+)['"]|require\s*\(\s*['"]([^'"]+)['"]|import\s*\(\s*['"]([^'"]+)['"])"#).unwrap()
});

/// JS/TS: function call — `foo(` or `obj.method(`
static RE_JS_FN_CALL: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\b(\w+)\s*\(").unwrap());

/// Go: `import "foo"` or `import foo "bar"`
static RE_GO_IMPORT: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r#"\bimport\s+[\w.]*\s*["']([^"']+)["']"#).unwrap());

/// Go: function call — `foo(` or `pkg.Func(`
static RE_GO_FN_CALL: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\b([A-Z]\w*\.\w+|\w+)\s*\(").unwrap());

/// Go: type reference — `SomeType{` or `var x SomeType` or `: SomeType`
static RE_GO_TYPE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"(?:\b([A-Z]\w+)\s*\{|:\s*([A-Z]\w+))").unwrap());

/// Java: `import foo.bar.*` (also `import static foo.Bar.baz`)
static RE_JAVA_IMPORT: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\bimport\s+(?:static\s+)?([\w.*]+)").unwrap());

/// Java: method call — `foo(` or `obj.method(`
static RE_JAVA_FN_CALL: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\b(\w+)\s*\(").unwrap());

/// Java: type reference — `SomeType ` or `<SomeType>`
static RE_JAVA_TYPE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\b([A-Z]\w+)\s*[<{( ]").unwrap());

/// Maximum number of symbols to extract per file to prevent runaway extraction.
const MAX_SYMBOLS_PER_FILE: usize = 50;

// ─── Definition regexes (functions/types *declared* in changed lines) ───
// Used for inbound caller (blast-radius) resolution.

static RE_DEF_RUST_FN: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\b(?:pub\s+)?(?:async\s+)?(?:unsafe\s+)?fn\s+(\w+)").unwrap());
static RE_DEF_RUST_TYPE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\b(?:pub\s+)?(?:struct|enum|trait|type)\s+(\w+)").unwrap());
static RE_DEF_PY_FN: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\bdef\s+(\w+)").unwrap());
static RE_DEF_PY_TYPE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\bclass\s+(\w+)").unwrap());
static RE_DEF_JS_FN: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\bfunction\s+\*?\s*(\w+)").unwrap());
static RE_DEF_JS_TYPE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\bclass\s+(\w+)").unwrap());
static RE_DEF_GO_FN: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\bfunc\s+(?:\([^)]*\)\s*)?(\w+)\s*\(").unwrap());
static RE_DEF_GO_TYPE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\btype\s+(\w+)\s+(?:struct|interface)").unwrap());
static RE_DEF_JAVA_TYPE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\b(?:class|interface|enum)\s+(\w+)").unwrap());

/// Extract symbols from a single line of code for a given language.
pub fn extract_symbols_from_line(line: &str, language: &str) -> Vec<SymbolKind> {
    let mut symbols = Vec::new();
    let mut seen = HashSet::new();

    let add_unique = |syms: &mut Vec<SymbolKind>, seen: &mut HashSet<String>, s: SymbolKind| {
        let key = format!("{s}");
        if seen.insert(key) {
            syms.push(s);
        }
    };

    match language {
        "rs" => {
            // Imports (highest value for resolution)
            for cap in RE_RUST_IMPORT.captures_iter(line) {
                add_unique(
                    &mut symbols,
                    &mut seen,
                    SymbolKind::Import(cap.get(2).unwrap().as_str().to_string()),
                );
            }
            // Module declarations (#73)
            for cap in RE_RUST_MOD.captures_iter(line) {
                add_unique(
                    &mut symbols,
                    &mut seen,
                    SymbolKind::Import(cap.get(1).unwrap().as_str().to_string()),
                );
            }
            // Function calls
            for cap in RE_RUST_FN_CALL.captures_iter(line) {
                let raw = cap.get(0).unwrap().as_str().trim_end_matches('(').trim();
                // Skip macros like `println!`, `vec!`, `format!`, `debug!`, etc.
                if raw.ends_with('!') {
                    continue;
                }
                // Skip common keywords that look like function calls
                if matches!(
                    raw,
                    "if" | "while" | "for" | "loop" | "match" | "return" | "break"
                ) {
                    continue;
                }
                add_unique(
                    &mut symbols,
                    &mut seen,
                    SymbolKind::FunctionCall(raw.to_string()),
                );
            }
            // Type references
            for cap in RE_RUST_TYPE.captures_iter(line) {
                if let Some(type_name) = cap.get(1) {
                    let name = type_name.as_str().to_string();
                    add_unique(&mut symbols, &mut seen, SymbolKind::TypeRef(name));
                }
            }
        }
        "py" | "pyi" => {
            for cap in RE_PY_IMPORT.captures_iter(line) {
                add_unique(
                    &mut symbols,
                    &mut seen,
                    SymbolKind::Import(cap.get(1).unwrap().as_str().to_string()),
                );
            }
            for cap in RE_PY_FN_CALL.captures_iter(line) {
                let name = cap.get(1).unwrap().as_str().to_string();
                if matches!(
                    name.as_str(),
                    "if" | "while" | "for" | "print" | "range" | "len"
                ) {
                    continue;
                }
                add_unique(&mut symbols, &mut seen, SymbolKind::FunctionCall(name));
            }
            for cap in RE_PY_TYPE.captures_iter(line) {
                let full = cap.get(0).unwrap().as_str();
                // Extract the type name after `:` or `->`
                let name = full.trim_start_matches(':').trim_start_matches('-').trim();
                if !name.is_empty() && name.starts_with(char::is_uppercase) {
                    add_unique(
                        &mut symbols,
                        &mut seen,
                        SymbolKind::TypeRef(name.to_string()),
                    );
                }
            }
        }
        "ts" | "tsx" | "js" | "jsx" | "mjs" | "cjs" => {
            for cap in RE_JS_IMPORT.captures_iter(line) {
                // The regex has 3 capture groups (one per alternative). Take the first non-None.
                let path = cap
                    .get(1)
                    .or_else(|| cap.get(2))
                    .or_else(|| cap.get(3))
                    .unwrap()
                    .as_str()
                    .to_string();
                add_unique(&mut symbols, &mut seen, SymbolKind::Import(path));
            }
            for cap in RE_JS_FN_CALL.captures_iter(line) {
                let name = cap.get(1).unwrap().as_str().to_string();
                if matches!(name.as_str(), "if" | "while" | "for" | "require" | "import") {
                    continue;
                }
                add_unique(&mut symbols, &mut seen, SymbolKind::FunctionCall(name));
            }
        }
        "go" => {
            for cap in RE_GO_IMPORT.captures_iter(line) {
                add_unique(
                    &mut symbols,
                    &mut seen,
                    SymbolKind::Import(cap.get(1).unwrap().as_str().to_string()),
                );
            }
            for cap in RE_GO_FN_CALL.captures_iter(line) {
                let name = cap.get(1).unwrap().as_str().to_string();
                if matches!(name.as_str(), "if" | "for" | "range" | "defer" | "go") {
                    continue;
                }
                add_unique(&mut symbols, &mut seen, SymbolKind::FunctionCall(name));
            }
            for cap in RE_GO_TYPE.captures_iter(line) {
                if let Some(type_name) = cap.get(1).or_else(|| cap.get(2)) {
                    add_unique(
                        &mut symbols,
                        &mut seen,
                        SymbolKind::TypeRef(type_name.as_str().to_string()),
                    );
                }
            }
        }
        "java" | "kt" | "kts" | "dart" | "svelte" => {
            for cap in RE_JAVA_IMPORT.captures_iter(line) {
                add_unique(
                    &mut symbols,
                    &mut seen,
                    SymbolKind::Import(cap.get(1).unwrap().as_str().to_string()),
                );
            }
            for cap in RE_JAVA_FN_CALL.captures_iter(line) {
                let name = cap.get(1).unwrap().as_str().to_string();
                if matches!(
                    name.as_str(),
                    "if" | "while" | "for" | "switch" | "return" | "new" | "instanceof"
                ) {
                    continue;
                }
                add_unique(&mut symbols, &mut seen, SymbolKind::FunctionCall(name));
            }
            for cap in RE_JAVA_TYPE.captures_iter(line) {
                if let Some(type_name) = cap.get(1) {
                    let name = type_name.as_str().to_string();
                    // Skip common non-user types
                    if matches!(
                        name.as_str(),
                        "String" | "Integer" | "Boolean" | "List" | "Map"
                    ) {
                        continue;
                    }
                    add_unique(&mut symbols, &mut seen, SymbolKind::TypeRef(name));
                }
            }
        }
        _ => {
            // Fallback: basic function call detection for any language
            for cap in RE_PY_FN_CALL.captures_iter(line) {
                let name = cap.get(1).unwrap().as_str().to_string();
                if name.len() > 2 {
                    add_unique(&mut symbols, &mut seen, SymbolKind::FunctionCall(name));
                }
            }
        }
    }

    symbols
}

/// Extract all symbols from the added lines of parsed diff chunks.
///
/// Returns a list of `ExtractedSymbol` entries, one per symbol found.
/// Duplicates across lines are deduplicated (same kind + same name + same file).
pub fn extract_symbols_from_diff(
    chunks: &[crate::engine::diff_parser::FileChunk],
) -> Vec<ExtractedSymbol> {
    let mut all_symbols = Vec::new();
    let mut seen: HashSet<(String, String)> = HashSet::new(); // (symbol key, file)

    for file in chunks {
        if file.is_binary || file.is_deleted {
            continue;
        }

        let file_path = file
            .new_path
            .as_deref()
            .or(file.old_path.as_deref())
            .unwrap_or("unknown");

        let mut file_symbol_count = 0;

        for hunk in &file.chunks {
            for line in &hunk.lines {
                // Only extract from added lines
                if line.line_type != crate::engine::diff_parser::DiffLineType::Add {
                    continue;
                }

                // Early cutoff: stop processing lines for this file once cap is reached
                if file_symbol_count >= MAX_SYMBOLS_PER_FILE {
                    break;
                }

                let language = &file.language;
                let symbols = extract_symbols_from_line(&line.content, language);

                for sym in symbols {
                    let key = format!("{sym}");
                    if seen.insert((key, file_path.to_string())) {
                        all_symbols.push(ExtractedSymbol {
                            file: file_path.to_string(),
                            line: line.new_line_no.unwrap_or(0),
                            kind: sym,
                            raw: line.content.clone(),
                        });
                        file_symbol_count += 1;
                        if file_symbol_count >= MAX_SYMBOLS_PER_FILE {
                            break;
                        }
                    }
                }
                if file_symbol_count >= MAX_SYMBOLS_PER_FILE {
                    break;
                }
            }
            if file_symbol_count >= MAX_SYMBOLS_PER_FILE {
                break;
            }
        }

        debug!(
            file = file_path,
            symbols = file_symbol_count,
            "extracted symbols from file"
        );
    }

    debug!(
        total = all_symbols.len(),
        "total symbols extracted from diff"
    );
    all_symbols
}

/// Extract function/type *definitions* declared in the added lines of a diff.
/// These are the symbols whose **callers** (blast radius) we want to resolve.
///
/// Only added lines are scanned (a definition only matters if this PR
/// introduces or modifies it). Returns deduplicated `(name, kind, file)`.
pub fn extract_definitions_from_diff(
    chunks: &[crate::engine::diff_parser::FileChunk],
) -> Vec<crate::engine::context::types::DefinedSymbol> {
    use crate::engine::context::types::{DefinedSymbol, DefinitionKind};
    use crate::engine::diff_parser::DiffLineType;
    let mut out = Vec::new();
    let mut seen: HashSet<(String, String)> = HashSet::new(); // (name, file)

    for file in chunks {
        if file.is_binary || file.is_deleted {
            continue;
        }
        let path = file
            .new_path
            .as_deref()
            .or(file.old_path.as_deref())
            .unwrap_or("unknown");
        let lang = &file.language;

        for hunk in &file.chunks {
            for line in &hunk.lines {
                if line.line_type != DiffLineType::Add {
                    continue;
                }
                let content = &line.content;
                let (fn_re, type_re): (Option<&Regex>, Option<&Regex>) = match lang.as_str() {
                    "rs" => (Some(&RE_DEF_RUST_FN), Some(&RE_DEF_RUST_TYPE)),
                    "py" | "pyi" => (Some(&RE_DEF_PY_FN), Some(&RE_DEF_PY_TYPE)),
                    "ts" | "tsx" | "js" | "jsx" | "mjs" | "cjs" => {
                        (Some(&RE_DEF_JS_FN), Some(&RE_DEF_JS_TYPE))
                    }
                    "go" => (Some(&RE_DEF_GO_FN), Some(&RE_DEF_GO_TYPE)),
                    "java" | "kt" | "kts" | "dart" | "svelte" => (None, Some(&RE_DEF_JAVA_TYPE)),
                    _ => (None, None),
                };

                let mut push = |name: &str, kind: DefinitionKind| {
                    if name.is_empty() {
                        return;
                    }
                    // Skip obvious noise / keywords that slip through.
                    if matches!(
                        name,
                        "if" | "for" | "while" | "match" | "new" | "return" | "test" | "main"
                    ) {
                        return;
                    }
                    if seen.insert((name.to_string(), path.to_string())) {
                        out.push(DefinedSymbol {
                            name: name.to_string(),
                            kind,
                            file: path.to_string(),
                        });
                    }
                };

                if let Some(re) = fn_re {
                    for cap in re.captures_iter(content) {
                        push(&cap[1], DefinitionKind::Function);
                    }
                }
                if let Some(re) = type_re {
                    for cap in re.captures_iter(content) {
                        push(&cap[1], DefinitionKind::Type);
                    }
                }
            }
        }
    }

    debug!(definitions = out.len(), "extracted definitions from diff");
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::engine::diff_parser::{DiffHunk, DiffLine, DiffLineType, FileChunk, parse_diff};

    fn make_file_chunk(path: &str, lang: &str, added_lines: &[(&str, u32)]) -> FileChunk {
        FileChunk {
            old_path: Some(path.to_string()),
            new_path: Some(path.to_string()),
            language: lang.to_string(),
            chunks: vec![DiffHunk {
                old_start: 1,
                old_count: 0,
                new_start: 1,
                new_count: added_lines.len() as u32,
                header: String::new(),
                lines: added_lines
                    .iter()
                    .map(|(text, line_no)| DiffLine {
                        line_type: DiffLineType::Add,
                        content: text.to_string(),
                        old_line_no: None,
                        new_line_no: Some(*line_no),
                    })
                    .collect(),
            }],
            is_binary: false,
            is_deleted: false,
            is_new: false,
        }
    }

    // --- Rust extraction ---

    #[test]
    fn extract_definitions_rust_fn_and_type() {
        let chunks = vec![make_file_chunk(
            "src/lib.rs",
            "rs",
            &[
                ("pub fn validate_token(token: &str) -> bool {", 1),
                ("pub struct CryptoConfig { seed: u64 }", 2),
            ],
        )];
        let defs = extract_definitions_from_diff(&chunks);
        let names: Vec<&str> = defs.iter().map(|d| d.name.as_str()).collect();
        assert!(
            names.contains(&"validate_token"),
            "should detect fn definition"
        );
        assert!(
            names.contains(&"CryptoConfig"),
            "should detect struct definition"
        );
    }

    #[test]
    fn extract_definitions_python() {
        let chunks = vec![make_file_chunk(
            "app/auth.py",
            "py",
            &[("def login(user):", 1), ("class User:", 2)],
        )];
        let defs = extract_definitions_from_diff(&chunks);
        let names: Vec<&str> = defs.iter().map(|d| d.name.as_str()).collect();
        assert!(names.contains(&"login"));
        assert!(names.contains(&"User"));
    }

    #[test]
    fn extract_definitions_go() {
        let chunks = vec![make_file_chunk(
            "main.go",
            "go",
            &[("func Parse(input string) error {", 1)],
        )];
        let defs = extract_definitions_from_diff(&chunks);
        assert!(defs.iter().any(|d| d.name == "Parse"));
    }

    #[test]
    fn extract_definitions_dedups_within_file() {
        let chunks = vec![make_file_chunk(
            "src/lib.rs",
            "rs",
            &[("fn helper() {}", 1), ("fn helper() {}", 2)],
        )];
        let defs = extract_definitions_from_diff(&chunks);
        assert_eq!(defs.iter().filter(|d| d.name == "helper").count(), 1);
    }

    #[test]
    fn extract_rust_function_calls() {
        let chunks = vec![make_file_chunk(
            "src/main.rs",
            "rs",
            &[("let result = validate_token(tok);", 5)],
        )];
        let symbols = extract_symbols_from_diff(&chunks);
        let fn_calls: Vec<_> = symbols
            .iter()
            .filter(
                |s| matches!(&s.kind, SymbolKind::FunctionCall(name) if name == "validate_token"),
            )
            .collect();
        assert!(
            !fn_calls.is_empty(),
            "should extract validate_token function call from Rust"
        );
    }

    #[test]
    fn extract_rust_imports() {
        let chunks = vec![make_file_chunk(
            "src/engine/mod.rs",
            "rs",
            &[("use crate::engine::scanner;", 1)],
        )];
        let symbols = extract_symbols_from_diff(&chunks);
        let imports: Vec<_> = symbols
            .iter()
            .filter(|s| matches!(&s.kind, SymbolKind::Import(p) if p == "engine::scanner"))
            .collect();
        assert!(
            !imports.is_empty(),
            "should extract use crate::engine::scanner"
        );
    }

    #[test]
    fn extract_rust_module_declarations() {
        // #73: `mod foo;` should be extracted as an import-like dependency.
        let chunks = vec![make_file_chunk(
            "src/main.rs",
            "rs",
            &[("mod engine;", 1), ("pub mod config;", 2)],
        )];
        let symbols = extract_symbols_from_diff(&chunks);
        let mods: Vec<_> = symbols
            .iter()
            .filter(|s| matches!(&s.kind, SymbolKind::Import(p) if p == "engine" || p == "config"))
            .collect();
        assert_eq!(mods.len(), 2, "should extract both mod declarations");
    }

    #[test]
    fn extract_java_wildcard_import() {
        // #72: `import foo.bar.*` should keep the wildcard, not truncate to `foo.bar`.
        let chunks = vec![make_file_chunk(
            "src/App.java",
            "java",
            &[("import com.example.*;", 1)],
        )];
        let symbols = extract_symbols_from_diff(&chunks);
        let imports: Vec<_> = symbols
            .iter()
            .filter(|s| matches!(&s.kind, SymbolKind::Import(p) if p.contains("*")))
            .collect();
        assert!(
            !imports.is_empty(),
            "java wildcard import should keep the '*'"
        );
    }

    #[test]
    fn extract_rust_type_refs() {
        let chunks = vec![make_file_chunk(
            "src/config.rs",
            "rs",
            &[("let config: CryptoConfig = load();", 10)],
        )];
        let symbols = extract_symbols_from_diff(&chunks);
        let types: Vec<_> = symbols
            .iter()
            .filter(|s| matches!(&s.kind, SymbolKind::TypeRef(t) if t == "CryptoConfig"))
            .collect();
        assert!(
            !types.is_empty(),
            "should extract CryptoConfig type reference"
        );
    }

    #[test]
    fn rust_skips_macros() {
        let chunks = vec![make_file_chunk(
            "src/main.rs",
            "rs",
            &[
                ("println!(\"hello\");", 1),
                ("debug!(\"value: {}\", x);", 2),
            ],
        )];
        let symbols = extract_symbols_from_diff(&chunks);
        let fn_calls: Vec<_> = symbols
            .iter()
            .filter(|s| matches!(&s.kind, SymbolKind::FunctionCall(_)))
            .collect();
        assert!(
            fn_calls.is_empty(),
            "should skip Rust macros (println!, debug!, etc.)"
        );
    }

    #[test]
    fn rust_skips_keywords() {
        let chunks = vec![make_file_chunk(
            "src/main.rs",
            "rs",
            &[("if condition { return; }", 1)],
        )];
        let symbols = extract_symbols_from_diff(&chunks);
        let fn_calls: Vec<_> = symbols
            .iter()
            .filter(|s| matches!(&s.kind, SymbolKind::FunctionCall(name) if name == "if" || name == "return"))
            .collect();
        assert!(fn_calls.is_empty(), "should skip Rust keyword-like calls");
    }

    // --- Python extraction ---

    #[test]
    fn extract_python_imports() {
        let chunks = vec![make_file_chunk(
            "app/auth.py",
            "py",
            &[("from auth import validate_token", 1)],
        )];
        let symbols = extract_symbols_from_diff(&chunks);
        let imports: Vec<_> = symbols
            .iter()
            .filter(|s| matches!(&s.kind, SymbolKind::Import(p) if p == "auth"))
            .collect();
        assert!(!imports.is_empty(), "should extract Python from/import");
    }

    #[test]
    fn extract_python_function_calls() {
        let chunks = vec![make_file_chunk(
            "app/auth.py",
            "py",
            &[("result = validate_token(tok)", 5)],
        )];
        let symbols = extract_symbols_from_diff(&chunks);
        let fn_calls: Vec<_> = symbols
            .iter()
            .filter(
                |s| matches!(&s.kind, SymbolKind::FunctionCall(name) if name == "validate_token"),
            )
            .collect();
        assert!(
            !fn_calls.is_empty(),
            "should extract validate_token from Python"
        );
    }

    // --- JavaScript/TypeScript extraction ---

    #[test]
    fn extract_js_imports() {
        let chunks = vec![make_file_chunk(
            "src/api.ts",
            "ts",
            &[("import { validate } from './auth';", 1)],
        )];
        let symbols = extract_symbols_from_diff(&chunks);
        let imports: Vec<_> = symbols
            .iter()
            .filter(|s| matches!(&s.kind, SymbolKind::Import(p) if p == "./auth"))
            .collect();
        assert!(!imports.is_empty(), "should extract JS/TS import path");
    }

    // --- Go extraction ---

    #[test]
    fn extract_go_imports() {
        let chunks = vec![make_file_chunk(
            "main.go",
            "go",
            &[("import \"net/http\"", 3)],
        )];
        let symbols = extract_symbols_from_diff(&chunks);
        let imports: Vec<_> = symbols
            .iter()
            .filter(|s| matches!(&s.kind, SymbolKind::Import(p) if p == "net/http"))
            .collect();
        assert!(!imports.is_empty(), "should extract Go import");
    }

    // --- Edge cases ---

    #[test]
    fn skips_binary_files() {
        let chunks = vec![FileChunk {
            old_path: Some("image.png".to_string()),
            new_path: Some("image.png".to_string()),
            language: "image".to_string(),
            chunks: vec![],
            is_binary: true,
            is_deleted: false,
            is_new: false,
        }];
        let symbols = extract_symbols_from_diff(&chunks);
        assert!(symbols.is_empty(), "should skip binary files");
    }

    #[test]
    fn skips_deleted_files() {
        let chunks = vec![FileChunk {
            old_path: Some("old.rs".to_string()),
            new_path: None,
            language: "rs".to_string(),
            chunks: vec![],
            is_binary: false,
            is_deleted: true,
            is_new: false,
        }];
        let symbols = extract_symbols_from_diff(&chunks);
        assert!(symbols.is_empty(), "should skip deleted files");
    }

    #[test]
    fn skips_context_and_removed_lines() {
        let chunks = vec![FileChunk {
            old_path: Some("src/lib.rs".to_string()),
            new_path: Some("src/lib.rs".to_string()),
            language: "rs".to_string(),
            chunks: vec![DiffHunk {
                old_start: 1,
                old_count: 2,
                new_start: 1,
                new_count: 1,
                header: String::new(),
                lines: vec![
                    DiffLine {
                        line_type: DiffLineType::Context,
                        content: "fn old() {}".to_string(),
                        old_line_no: Some(1),
                        new_line_no: Some(1),
                    },
                    DiffLine {
                        line_type: DiffLineType::Remove,
                        content: "fn removed() {}".to_string(),
                        old_line_no: Some(2),
                        new_line_no: None,
                    },
                ],
            }],
            is_binary: false,
            is_deleted: false,
            is_new: false,
        }];
        let symbols = extract_symbols_from_diff(&chunks);
        assert!(symbols.is_empty(), "should only extract from added lines");
    }

    #[test]
    fn deduplicates_across_lines() {
        let chunks = vec![make_file_chunk(
            "src/main.rs",
            "rs",
            &[
                ("let a = foo();", 1),
                ("let b = foo();", 2),
                ("let c = bar();", 3),
            ],
        )];
        let symbols = extract_symbols_from_diff(&chunks);
        let foo_count = symbols
            .iter()
            .filter(|s| matches!(&s.kind, SymbolKind::FunctionCall(n) if n == "foo"))
            .count();
        assert_eq!(
            foo_count, 1,
            "should deduplicate same symbol across lines in same file"
        );
    }

    #[test]
    fn respects_max_symbols_per_file() {
        let lines: Vec<(&str, u32)> = (0..60u32)
            .map(|i| {
                let s = format!("let x{i} = func{i}();");
                // Leak to get &'static str
                let leaked: &'static str = Box::leak(s.into_boxed_str());
                (leaked, i + 1)
            })
            .collect();
        let chunks = vec![make_file_chunk("src/big.rs", "rs", &lines)];
        let symbols = extract_symbols_from_diff(&chunks);
        assert!(
            symbols.len() <= MAX_SYMBOLS_PER_FILE,
            "should cap at MAX_SYMBOLS_PER_FILE"
        );
    }

    #[test]
    fn unknown_language_fallback() {
        let chunks = vec![make_file_chunk(
            "README.md",
            "md",
            &[("see foo() for details", 1)],
        )];
        let symbols = extract_symbols_from_diff(&chunks);
        // Fallback should at least extract function calls
        let fn_calls: Vec<_> = symbols
            .iter()
            .filter(|s| matches!(&s.kind, SymbolKind::FunctionCall(n) if n == "foo"))
            .collect();
        assert!(
            !fn_calls.is_empty(),
            "fallback should extract function calls for unknown languages"
        );
    }

    #[test]
    fn full_diff_extraction() {
        let diff = r#"diff --git a/src/auth.rs b/src/auth.rs
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -10,6 +10,8 @@
 use crate::config::CryptoConfig;
+use crate::engine::scanner;
 
 pub fn authenticate(req: &Request) -> Result<bool> {
+    let config = CryptoConfig::load();
+    let valid = validate_token(&req.token, &config);
+    scanner::scan_file(&req.path)
 }
"#;
        let chunks = parse_diff(diff);
        let symbols = extract_symbols_from_diff(&chunks);
        // Should find imports, function calls, and type refs
        assert!(!symbols.is_empty(), "should extract symbols from full diff");
        let has_import = symbols
            .iter()
            .any(|s| matches!(&s.kind, SymbolKind::Import(_)));
        let has_fn_call = symbols
            .iter()
            .any(|s| matches!(&s.kind, SymbolKind::FunctionCall(_)));
        assert!(has_import, "should find import symbols");
        assert!(has_fn_call, "should find function call symbols");
    }
}