vera2-core 0.12.16-aristoddle.1

Core engine for Vera: parsing, indexing, retrieval, storage, and embedding
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
//! Integration tests for the parsing pipeline.
//!
//! Tests the full `parse_and_chunk` flow across all supported languages,
//! Tier 0 fallback, large symbol splitting, and edge cases.

use crate::config::IndexingConfig;
use crate::parsing::test_support::{default_config, find_chunk, parse};
use crate::parsing::{parse_and_chunk, parse_file_with_diagnostics};
use crate::types::{Language, SymbolType};

// =========================================================
// Rust tests
// =========================================================

#[test]
fn rust_functions_produce_chunks() {
    let source = r#"fn hello() {
    println!("hello");
}

fn world() -> i32 {
    42
}"#;
    let chunks = parse(source, "main.rs", Language::Rust);
    let funcs: Vec<_> = chunks
        .iter()
        .filter(|c| c.symbol_type == Some(SymbolType::Function))
        .collect();
    assert_eq!(funcs.len(), 2, "expected 2 function chunks");
    assert_eq!(funcs[0].symbol_name, Some("hello".to_string()));
    assert_eq!(funcs[1].symbol_name, Some("world".to_string()));
}

#[test]
fn rust_struct_and_impl() {
    let source = r#"struct Point {
    x: f64,
    y: f64,
}

impl Point {
    fn new(x: f64, y: f64) -> Self {
        Self { x, y }
    }
}"#;
    let chunks = parse(source, "point.rs", Language::Rust);
    assert!(chunks.len() >= 2, "expected struct + method(s)");

    let struc = chunks
        .iter()
        .find(|c| c.symbol_type == Some(SymbolType::Struct));
    assert!(struc.is_some(), "should have struct chunk");
    assert_eq!(struc.unwrap().symbol_name, Some("Point".to_string()));

    let method = chunks
        .iter()
        .find(|c| c.symbol_type == Some(SymbolType::Method));
    assert!(method.is_some(), "should have method chunk");
    assert_eq!(method.unwrap().symbol_name, Some("new".to_string()));

    let impl_block = chunks
        .iter()
        .find(|c| c.symbol_type == Some(SymbolType::Block) && c.content.contains("impl Point"));
    assert!(impl_block.is_some(), "should keep impl block chunk");
}

#[test]
fn rust_impl_methods_emit_no_gap_chunks() {
    let source = r#"impl CancellationToken {
    /// Create a token.
    fn new() -> Self {
        Self
    }

    /// Cancel the token.
    fn cancel(&self) {}
}
"#;
    let chunks = parse(source, "cancellation.rs", Language::Rust);

    let impl_block = find_chunk(&chunks, "impl CancellationToken");
    assert_eq!(impl_block.symbol_type, Some(SymbolType::Block));
    assert!(
        impl_block.content.contains("fn cancel"),
        "impl chunk should cover the whole body: {impl_block:?}"
    );
    for method in ["new", "cancel"] {
        let chunk = find_chunk(&chunks, method);
        assert_eq!(
            chunk.symbol_type,
            Some(SymbolType::Method),
            "`{method}` should be chunked as a method: {chunk:?}"
        );
    }

    let unnamed: Vec<_> = chunks.iter().filter(|c| c.symbol_name.is_none()).collect();
    assert!(
        unnamed.is_empty(),
        "spans inside the impl are already covered by its chunk: {unnamed:?}"
    );
    assert!(
        !chunks.iter().any(|c| c.content.trim() == "}"),
        "the impl's closing brace must not become its own chunk: {chunks:?}"
    );
}

#[test]
fn rust_inline_module_children_preserve_only_the_trailing_gap() {
    let source = r#"mod nested {
    fn first() {}

    // Covered by the module chunk, not a separate gap.
    fn second() {}
}

use std::fmt;
"#;
    let chunks = parse(source, "nested.rs", Language::Rust);
    let names: Vec<_> = chunks
        .iter()
        .map(|chunk| chunk.symbol_name.as_deref())
        .collect();

    assert_eq!(names, [Some("nested"), Some("first"), Some("second"), None]);
    assert_eq!(chunks.last().unwrap().content.trim(), "use std::fmt;");
}

#[test]
fn rust_enum_and_trait() {
    let source = r#"enum Color {
    Red,
    Green,
    Blue,
}

trait Paintable {
    fn paint(&self, color: Color);
}"#;
    let chunks = parse(source, "paint.rs", Language::Rust);
    let enm = chunks
        .iter()
        .find(|c| c.symbol_type == Some(SymbolType::Enum));
    assert!(enm.is_some(), "should have enum chunk");
    let trt = chunks
        .iter()
        .find(|c| c.symbol_type == Some(SymbolType::Trait));
    assert!(trt.is_some(), "should have trait chunk");
}

#[test]
fn rust_type_alias() {
    let source = "type Result<T> = std::result::Result<T, MyError>;\n";
    let chunks = parse(source, "lib.rs", Language::Rust);
    let ta = chunks
        .iter()
        .find(|c| c.symbol_type == Some(SymbolType::TypeAlias));
    assert!(ta.is_some(), "should have type alias chunk");
}

// =========================================================
// Python tests
// =========================================================

#[test]
fn python_function_and_class_methods() {
    let source = r#"def greet(name):
    return f"Hello, {name}!"

class UserService:
    def __init__(self, db):
        self.db = db

    def get_user(self, user_id):
        return self.db.get(user_id)
"#;
    let chunks = parse(source, "service.py", Language::Python);
    // Now: function + __init__ method + get_user method (+ possible gap chunks)
    assert!(chunks.len() >= 3, "expected function + 2 class methods");

    let func = chunks
        .iter()
        .find(|c| c.symbol_type == Some(SymbolType::Function));
    assert!(func.is_some());
    assert_eq!(func.unwrap().symbol_name, Some("greet".to_string()));

    // Class methods are now extracted individually.
    let init = chunks
        .iter()
        .find(|c| c.symbol_name == Some("__init__".to_string()));
    assert!(init.is_some(), "should find method __init__");
    assert_eq!(init.unwrap().symbol_type, Some(SymbolType::Method));

    let get_user = chunks
        .iter()
        .find(|c| c.symbol_name == Some("get_user".to_string()));
    assert!(get_user.is_some(), "should find method get_user");
    assert_eq!(get_user.unwrap().symbol_type, Some(SymbolType::Method));

    let class_chunk = chunks
        .iter()
        .find(|c| c.symbol_name == Some("UserService".to_string()));
    assert!(class_chunk.is_some(), "should retain the class chunk");
    assert_eq!(class_chunk.unwrap().symbol_type, Some(SymbolType::Class));
}

#[test]
fn python_decorated_function() {
    let source = r#"import functools

@functools.cache
def expensive_compute(n):
    return sum(range(n))
"#;
    let chunks = parse(source, "compute.py", Language::Python);
    let func = chunks
        .iter()
        .find(|c| c.symbol_type == Some(SymbolType::Function));
    assert!(func.is_some(), "should extract decorated function");
}

// =========================================================
// TypeScript tests
// =========================================================

#[test]
fn typescript_function_interface_class() {
    let source = r#"function add(a: number, b: number): number {
    return a + b;
}

interface Config {
    host: string;
    port: number;
}

class Server {
    private config: Config;

    constructor(config: Config) {
        this.config = config;
    }

    start(): void {
        console.log("starting");
    }
}
"#;
    let chunks = parse(source, "server.ts", Language::TypeScript);
    assert!(
        chunks.len() >= 5,
        "expected function + interface + class + methods"
    );

    let func = chunks
        .iter()
        .find(|c| c.symbol_name == Some("add".to_string()));
    assert!(func.is_some());
    assert_eq!(func.unwrap().symbol_type, Some(SymbolType::Function));

    let iface = chunks
        .iter()
        .find(|c| c.symbol_name == Some("Config".to_string()));
    assert!(iface.is_some());
    assert_eq!(iface.unwrap().symbol_type, Some(SymbolType::Interface));

    let cls = chunks
        .iter()
        .find(|c| c.symbol_name == Some("Server".to_string()));
    assert!(cls.is_some());
    assert_eq!(cls.unwrap().symbol_type, Some(SymbolType::Class));

    let constructor = chunks
        .iter()
        .find(|c| c.symbol_name == Some("constructor".to_string()));
    assert!(constructor.is_some());
    assert_eq!(constructor.unwrap().symbol_type, Some(SymbolType::Method));

    let method = chunks
        .iter()
        .find(|c| c.symbol_name == Some("start".to_string()));
    assert!(method.is_some());
    assert_eq!(method.unwrap().symbol_type, Some(SymbolType::Method));
}

#[test]
fn typescript_interface_and_abstract_methods() {
    let source = r#"interface Store {
    get(key: string): string | undefined;
    set(key: string, value: string): void;
}

abstract class Base {
    abstract render(): void;
}
"#;
    let chunks = parse(source, "store.ts", Language::TypeScript);

    for name in ["get", "set", "render"] {
        let chunk = chunks
            .iter()
            .find(|c| c.symbol_name == Some(name.to_string()));
        assert!(chunk.is_some(), "missing method chunk: {name}");
        assert_eq!(chunk.unwrap().symbol_type, Some(SymbolType::Method));
    }
}

#[test]
fn typescript_enum_and_type_alias() {
    let source = r#"enum Direction {
    Up,
    Down,
    Left,
    Right,
}

type Point = { x: number; y: number };
"#;
    let chunks = parse(source, "types.ts", Language::TypeScript);

    let enm = chunks
        .iter()
        .find(|c| c.symbol_type == Some(SymbolType::Enum));
    assert!(enm.is_some(), "should have enum chunk");

    let ta = chunks
        .iter()
        .find(|c| c.symbol_type == Some(SymbolType::TypeAlias));
    assert!(ta.is_some(), "should have type alias chunk");
}

#[test]
fn typescript_const_assigned_functions_are_named() {
    let source = r#"export function declaredFn(a: number): number { return a; }
export const arrowFn = (a: number): number => a;
export const typedArrow: (a: number) => number = (a) => a;
export const asyncArrow = async () => {};
export const exprFn = function (a: number) { return a; };
export const plainConst = 42;
"#;
    let chunks = parse(source, "shapes.ts", Language::TypeScript);

    for name in [
        "declaredFn",
        "arrowFn",
        "typedArrow",
        "asyncArrow",
        "exprFn",
    ] {
        assert_eq!(
            find_chunk(&chunks, name).symbol_type,
            Some(SymbolType::Function),
            "{name} binds a function"
        );
    }

    // Whether plain value consts should be indexed as named symbols is a
    // separate call; this fix deliberately leaves them as they were.
    assert!(
        chunks
            .iter()
            .all(|c| c.symbol_name.as_deref() != Some("plainConst")),
        "value consts should keep their existing shape"
    );

    // The span covers the declaration, initialiser included, not just the name.
    // `export` is present here because chunk content is expanded to whole lines
    // and this declaration is on one line; see the multiline case below.
    assert_eq!(
        find_chunk(&chunks, "arrowFn").content.trim(),
        "export const arrowFn = (a: number): number => a;"
    );
}

/// A declaration split across lines from its `export` keeps the same span shape
/// as every other declaration kind: the symbol covers the declaration, and the
/// `export` on the preceding line falls outside it.
///
/// Pinned deliberately. Const bindings must not become the only declaration
/// kind whose span swallows `export`; changing that is a repo-wide decision
/// about `export_statement`, not something to do for one node kind.
#[test]
fn typescript_multiline_export_spans_match_function_declarations() {
    let source = r#"export
const arrowFn = (a: number): number => a;

export
function declaredFn(a: number): number { return a; }
"#;
    let chunks = parse(source, "multiline.ts", Language::TypeScript);

    for name in ["arrowFn", "declaredFn"] {
        let content = find_chunk(&chunks, name).content.trim().to_string();
        assert!(
            !content.starts_with("export"),
            "{name} should span the declaration, not the export: {content:?}"
        );
    }
}

/// `let` and `var` function bindings are named too. The reported symptom was
/// about `const` because that is the dominant style, but the cause is the name
/// living on the declarator, which is identical for all three keywords.
#[test]
fn javascript_let_var_and_generator_bindings_are_named() {
    let source = r#"export const arrowFn = (a) => a;
let laterFn = function (a) { return a; };
var oldStyleFn = () => {};
export const genFn = function* () { yield 1; };
"#;
    let chunks = parse(source, "shapes.js", Language::JavaScript);

    for name in ["arrowFn", "laterFn", "oldStyleFn", "genFn"] {
        assert_eq!(
            find_chunk(&chunks, name).symbol_type,
            Some(SymbolType::Function),
            "{name} binds a function"
        );
    }
}

#[test]
fn typescript_multi_declarator_statements_are_unchanged() {
    let source = "const a = () => 1, b = () => 2;\n";
    let chunks = parse(source, "multi.ts", Language::TypeScript);

    // The whole shape is pinned, not just the absence of names: one unnamed
    // variable chunk spanning the statement, exactly as before this change.
    assert_eq!(chunks.len(), 1, "unexpected chunks: {chunks:?}");
    assert_eq!(chunks[0].symbol_name, None);
    assert_eq!(chunks[0].symbol_type, Some(SymbolType::Variable));
    assert_eq!(chunks[0].content.trim(), "const a = () => 1, b = () => 2;");
}

#[test]
fn tsx_files_parse_clean_and_expose_jsx_call_sites() {
    let source = r#"import { Hello } from "./hello";

export function App() {
    return <Hello name="world" />;
}
"#;
    let (chunks, refs, diagnostics) = parse_file_with_diagnostics(
        source,
        "src/app.tsx",
        Language::TypeScript,
        &default_config(),
    )
    .unwrap();

    assert!(
        !diagnostics.tree_has_error,
        "the tsx grammar should parse JSX without error nodes"
    );
    assert!(!chunks.is_empty(), "should produce chunks");
    assert!(
        refs.iter().any(|r| r.callee == "Hello"),
        "JSX usage should be recorded as a call site: {refs:?}"
    );
}

#[test]
fn jsx_dotted_components_index_under_the_rightmost_segment() {
    // Pins the naming convention for dotted JSX components.
    //
    // `<Icons.Arrow />` is recorded as `Arrow`, not `Icons.Arrow`. That is not
    // an accident of `extract_callee` — it is what makes the call site link to
    // the definition. A component re-exported as `Icons.Arrow` is *defined*
    // somewhere as `Arrow` (`export function Arrow`), and definitions are
    // indexed under that bare name, as is every other member call
    // (`obj.method()` records `method`). Recording `Icons.Arrow` would leave
    // the reference matching no definition, which is the invisibility this
    // whole path exists to fix.
    //
    // A lowercase root stays a component too: `<icons.Arrow />` is a member
    // expression, and a member expression is always a value lookup. Only a
    // bare lowercase identifier is an intrinsic host element.
    let source = r#"import * as Icons from "./icons";
import * as icons from "./icons";

export function Toolbar() {
    return (
        <div className="bar">
            <Icons.Arrow />
            <icons.Chevron />
            <span />
            <My-element />
        </div>
    );
}
"#;
    let (_, refs, diagnostics) = parse_file_with_diagnostics(
        source,
        "src/toolbar.tsx",
        Language::TypeScript,
        &default_config(),
    )
    .unwrap();

    assert!(
        !diagnostics.tree_has_error,
        "tsx should parse without errors"
    );
    let mut callees: Vec<&str> = refs.iter().map(|r| r.callee.as_str()).collect();
    callees.sort_unstable();

    // An exact set, not `contains`: a root segment leaking in as its own
    // reference (`Icons`, `icons`) is precisely the failure mode worth
    // catching, and a containment check would not see it. `div`/`span` are
    // intrinsic by the lowercase rule and `My-element` by the hyphen rule.
    assert_eq!(
        callees,
        vec!["Arrow", "Chevron"],
        "expected exactly the two dotted components, rightmost segment only"
    );
}

// =========================================================
// Go tests
// =========================================================

#[test]
fn go_function_struct_method() {
    let source = r#"package main

import "fmt"

func Hello() string {
    return "hello"
}

type Point struct {
    X float64
    Y float64
}

func (p *Point) Distance() float64 {
    return p.X*p.X + p.Y*p.Y
}
"#;
    let chunks = parse(source, "main.go", Language::Go);
    assert!(
        chunks.len() >= 3,
        "expected func + struct + method, got {}",
        chunks.len()
    );

    let func = chunks
        .iter()
        .find(|c| c.symbol_name == Some("Hello".to_string()));
    assert!(func.is_some());
    assert_eq!(func.unwrap().symbol_type, Some(SymbolType::Function));

    let struc = chunks
        .iter()
        .find(|c| c.symbol_name == Some("Point".to_string()));
    assert!(struc.is_some());
    assert_eq!(struc.unwrap().symbol_type, Some(SymbolType::Struct));

    let method = chunks
        .iter()
        .find(|c| c.symbol_name == Some("Distance".to_string()));
    assert!(method.is_some());
    assert_eq!(method.unwrap().symbol_type, Some(SymbolType::Method));
}

#[test]
fn go_interface() {
    let source = r#"package shapes

type Shape interface {
    Area() float64
    Perimeter() float64
}
"#;
    let chunks = parse(source, "shapes.go", Language::Go);
    let iface = chunks
        .iter()
        .find(|c| c.symbol_type == Some(SymbolType::Interface));
    assert!(iface.is_some(), "should have interface chunk");
    assert_eq!(iface.unwrap().symbol_name, Some("Shape".to_string()));
}

// =========================================================
// Java tests
// =========================================================

#[test]
fn java_class_with_methods() {
    let source = r#"class Calculator {
    public int add(int a, int b) {
        return a + b;
    }

    public int multiply(int a, int b) {
        return a * b;
    }
}
"#;
    let chunks = parse(source, "Calculator.java", Language::Java);
    assert!(!chunks.is_empty());

    let cls = chunks
        .iter()
        .find(|c| c.symbol_type == Some(SymbolType::Class));
    assert!(cls.is_some(), "should have class chunk");
}

#[test]
fn java_interface_and_enum() {
    let source = r#"interface Drawable {
    void draw();
    double area();
}

enum Color {
    RED, GREEN, BLUE;
}
"#;
    let chunks = parse(source, "Drawable.java", Language::Java);

    let iface = chunks
        .iter()
        .find(|c| c.symbol_type == Some(SymbolType::Interface));
    assert!(iface.is_some(), "should have interface chunk");

    let enm = chunks
        .iter()
        .find(|c| c.symbol_type == Some(SymbolType::Enum));
    assert!(enm.is_some(), "should have enum chunk");
}

// =========================================================
// C tests
// =========================================================

#[test]
fn c_function_and_struct() {
    let source = r#"#include <stdio.h>

struct Point {
    double x;
    double y;
};

double distance(struct Point* p) {
    return p->x * p->x + p->y * p->y;
}

int main() {
    struct Point p = {3.0, 4.0};
    printf("%f\n", distance(&p));
    return 0;
}
"#;
    let chunks = parse(source, "main.c", Language::C);
    assert!(chunks.len() >= 2, "expected struct + functions");

    let func = chunks
        .iter()
        .find(|c| c.symbol_name == Some("distance".to_string()));
    assert!(func.is_some(), "should find function 'distance'");
    assert_eq!(func.unwrap().symbol_type, Some(SymbolType::Function));
}

// =========================================================
// C++ tests
// =========================================================

#[test]
fn cpp_class_and_namespace() {
    let source = r#"class Shape {
public:
    virtual double area() = 0;
    virtual ~Shape() = default;
};

namespace geometry {
    double pi() {
        return 3.14159;
    }
}
"#;
    let chunks = parse(source, "shape.cpp", Language::Cpp);
    assert!(
        !chunks.is_empty(),
        "expected at least class or namespace chunks"
    );

    let cls = chunks
        .iter()
        .find(|c| c.symbol_type == Some(SymbolType::Class));
    assert!(cls.is_some(), "should have class chunk");

    let ns = chunks
        .iter()
        .find(|c| c.symbol_type == Some(SymbolType::Module));
    assert!(ns.is_some(), "should have namespace chunk");
}

// =========================================================
// Tier 0 fallback tests
// =========================================================

#[test]
fn unknown_language_uses_tier0() {
    let source = "some content\nmore content\nthird line\n";
    let chunks = parse(source, "data.xyz", Language::Unknown);
    assert!(!chunks.is_empty(), "tier0 should produce chunks");
    assert_eq!(chunks[0].language, Language::Unknown);
    assert_eq!(chunks[0].symbol_type, Some(SymbolType::Block));
}

#[test]
fn haskell_indexing_uses_tier0_fallback() {
    let source = "data Point = Point Float Float\nadd x y = x + y\n";
    let (chunks, refs, diagnostics) = parse_file_with_diagnostics(
        source,
        "Data/Point.hs",
        Language::Haskell,
        &default_config(),
    )
    .unwrap();

    assert!(!chunks.is_empty(), "tier0 should produce Haskell chunks");
    assert!(
        refs.is_empty(),
        "tier0 fallback should not extract references"
    );
    assert!(diagnostics.used_tier0_fallback);
}

#[test]
fn toml_uses_whole_file_chunking() {
    let source = "[package]\nname = \"vera\"\nversion = \"0.1.0\"\n";
    let chunks = parse(source, "Cargo.toml", Language::Toml);
    assert_eq!(chunks.len(), 1);
    assert_eq!(chunks[0].language, Language::Toml);
    assert_eq!(chunks[0].symbol_name.as_deref(), Some("Cargo.toml"));
    assert_eq!(chunks[0].line_start, 1);
    assert_eq!(chunks[0].line_end, 3);
}

#[test]
fn whole_file_chunking_respects_max_chunk_bytes() {
    let source = (0..250)
        .map(|i| format!("key_{i} = \"{}\"", "x".repeat(24)))
        .collect::<Vec<_>>()
        .join("\n");
    let config = IndexingConfig {
        max_chunk_bytes: 600,
        ..Default::default()
    };

    let chunks = parse_and_chunk(&source, "large.toml", Language::Toml, &config).unwrap();

    assert!(
        chunks.len() > 1,
        "oversized whole-file chunks should be split"
    );
    assert!(
        chunks.iter().all(|chunk| chunk.content.len() <= 600),
        "all split chunks should stay within max_chunk_bytes"
    );
}

#[test]
fn rst_respects_max_chunk_bytes() {
    let source = (0..250)
        .map(|i| format!("Paragraph {i}: {}", "x".repeat(28)))
        .collect::<Vec<_>>()
        .join("\n");
    let config = IndexingConfig {
        max_chunk_bytes: 600,
        ..Default::default()
    };

    let chunks = parse_and_chunk(&source, "guide.rst", Language::Rst, &config).unwrap();

    assert!(chunks.len() > 1, "oversized RST chunks should be split");
    assert!(
        chunks.iter().all(|chunk| chunk.content.len() <= 600),
        "all split chunks should stay within max_chunk_bytes"
    );
}

#[test]
fn rst_chunks_are_section_aware() {
    let source = r#"Messenger
=========

Intro paragraph.

Installation
------------

Install the package.

Usage
-----

Dispatch messages.
"#;

    let chunks = parse(source, "guide.rst", Language::Rst);

    let names: Vec<String> = chunks
        .iter()
        .filter_map(|c| c.symbol_name.clone())
        .collect();
    assert!(names.iter().any(|n| n == "Messenger"));
    assert!(names.iter().any(|n| n == "Installation"));
    assert!(names.iter().any(|n| n == "Usage"));

    let usage = chunks
        .iter()
        .find(|chunk| chunk.symbol_name.as_deref() == Some("Usage"))
        .expect("expected a Usage section chunk");
    assert!(usage.content.contains("Dispatch messages."));
}

// =========================================================
// Large symbol splitting
// =========================================================

#[test]
fn large_function_splits_no_content_gaps() {
    // Generate a 500+ line function
    let mut lines = vec!["fn huge_function() {".to_string()];
    for i in 0..550 {
        lines.push(format!("    let x{i} = {i};"));
    }
    lines.push("}".to_string());
    let source = lines.join("\n");

    let config = IndexingConfig {
        max_chunk_lines: 200,
        ..Default::default()
    };
    let chunks = parse_and_chunk(&source, "big.rs", Language::Rust, &config).unwrap();

    // Should be split into multiple sub-chunks
    assert!(chunks.len() >= 3, "552 lines / 200 = 3 sub-chunks expected");

    // Verify no content gaps: concatenating all chunks = original source
    let func_chunks: Vec<_> = chunks
        .iter()
        .filter(|c| c.symbol_type == Some(SymbolType::Function))
        .collect();
    let mut reconstructed = String::new();
    for (i, chunk) in func_chunks.iter().enumerate() {
        if i > 0 {
            reconstructed.push('\n');
        }
        reconstructed.push_str(&chunk.content);
    }
    assert_eq!(
        reconstructed, source,
        "reconstructed content should match original"
    );

    // Verify line ranges are contiguous
    for i in 1..func_chunks.len() {
        assert_eq!(
            func_chunks[i].line_start,
            func_chunks[i - 1].line_end + 1,
            "sub-chunks should be contiguous"
        );
    }
}

// =========================================================
// Metadata correctness
// =========================================================

#[test]
fn chunk_metadata_matches_source() {
    let source = r#"fn greet(name: &str) -> String {
    format!("Hello, {}!", name)
}"#;
    let chunks = parse(source, "lib.rs", Language::Rust);
    assert_eq!(chunks.len(), 1);

    let chunk = &chunks[0];
    assert_eq!(chunk.file_path, "lib.rs");
    assert_eq!(chunk.language, Language::Rust);
    assert_eq!(chunk.symbol_type, Some(SymbolType::Function));
    assert_eq!(chunk.symbol_name, Some("greet".to_string()));
    assert_eq!(chunk.line_start, 1);
    assert_eq!(chunk.line_end, 3);

    // Verify content matches source at declared lines
    let source_lines: Vec<&str> = source.lines().collect();
    let expected = source_lines[0..3].join("\n");
    assert_eq!(chunk.content, expected);
}

#[test]
fn javascript_extracts_like_typescript() {
    let source = r#"function sum(a, b) {
    return a + b;
}

class Calculator {
    add(a, b) {
        return a + b;
    }
}
"#;
    let chunks = parse(source, "calc.js", Language::JavaScript);
    assert!(chunks.len() >= 2, "expected function + class");

    let func = chunks
        .iter()
        .find(|c| c.symbol_name == Some("sum".to_string()));
    assert!(func.is_some());

    let cls = chunks
        .iter()
        .find(|c| c.symbol_name == Some("Calculator".to_string()));
    assert!(cls.is_some());
}

// =========================================================
// Edge cases
// =========================================================

#[test]
fn empty_source_produces_no_chunks() {
    let chunks = parse_and_chunk("", "empty.rs", Language::Rust, &default_config()).unwrap();
    assert!(chunks.is_empty());
}

#[test]
fn whitespace_only_source_no_chunks() {
    let chunks =
        parse_and_chunk("   \n\n  \n", "ws.rs", Language::Rust, &default_config()).unwrap();
    assert!(chunks.is_empty());
}

#[test]
fn comment_only_file_falls_back() {
    let source = "// This is a comment\n// Another comment\n";
    let chunks = parse(source, "comments.rs", Language::Rust);
    // Should produce Tier 0 fallback chunks since no symbols found
    assert!(
        !chunks.is_empty(),
        "comment-only files should still produce chunks"
    );
}