magellan 3.2.0

Deterministic codebase mapping tool for local development
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
//! Comprehensive tests for AST nodes functionality

use tempfile::tempdir;

use crate::graph::CodeGraph;

/// Test end-to-end: indexing creates AST nodes
#[test]
fn test_indexing_creates_ast_nodes() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");

    let mut graph = CodeGraph::open(&db_path).unwrap();

    let source = r#"
            fn main() {
                if true {
                    println!("hello");
                } else {
                    println!("goodbye");
                }
                for i in 0..10 {
                    println!("{}", i);
                }
                while x > 0 {
                    x -= 1;
                }
                loop {
                    break;
                }
                return 42;
            }
        "#;

    graph.index_file("test.rs", source.as_bytes()).unwrap();

    // Verify AST nodes were created
    let count = graph.count_ast_nodes().unwrap();
    assert!(count > 0, "AST nodes should be created during indexing");

    // Verify specific node types exist
    let fn_count = graph.get_ast_nodes_by_kind("function_item").unwrap();
    assert!(!fn_count.is_empty(), "Should have function_item node");

    let if_count = graph.get_ast_nodes_by_kind("if_expression").unwrap();
    assert!(!if_count.is_empty(), "Should have if_expression node");

    let for_count = graph.get_ast_nodes_by_kind("for_expression").unwrap();
    assert!(!for_count.is_empty(), "Should have for_expression node");

    let while_count = graph.get_ast_nodes_by_kind("while_expression").unwrap();
    assert!(!while_count.is_empty(), "Should have while_expression node");

    let loop_count = graph.get_ast_nodes_by_kind("loop_expression").unwrap();
    assert!(!loop_count.is_empty(), "Should have loop_expression node");

    let return_count = graph.get_ast_nodes_by_kind("return_expression").unwrap();
    assert!(
        !return_count.is_empty(),
        "Should have return_expression node"
    );
}

/// Test parent-child relationship queries
#[test]
fn test_parent_child_relationships() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");
    let graph = CodeGraph::open(&db_path).unwrap();

    // Insert test hierarchy
    let conn = graph.chunks.connect().unwrap();
    conn.execute(
        "INSERT INTO ast_nodes (id, parent_id, kind, byte_start, byte_end)
         VALUES
            (1, NULL, 'function_item', 0, 200),
            (2, 1, 'block', 10, 190),
            (3, 2, 'if_expression', 20, 100),
            (4, 2, 'return_expression', 110, 130),
            (5, 3, 'block', 30, 50)",
        [],
    )
    .unwrap();

    // Get children of function (should be block)
    let children = graph.get_ast_children(1).unwrap();
    assert_eq!(children.len(), 1);
    assert_eq!(children[0].kind, "block");

    // Get children of if_expression (should be then block)
    let children = graph.get_ast_children(3).unwrap();
    assert_eq!(children.len(), 1);
    assert_eq!(children[0].kind, "block");

    // Get children of leaf node (should be empty)
    let children = graph.get_ast_children(5).unwrap();
    assert_eq!(children.len(), 0);
}

/// Test position-based queries
#[test]
fn test_position_based_queries() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");
    let graph = CodeGraph::open(&db_path).unwrap();

    // Insert nodes with overlapping spans
    let conn = graph.chunks.connect().unwrap();
    conn.execute(
        "INSERT INTO ast_nodes (id, parent_id, kind, byte_start, byte_end)
         VALUES
            (1, NULL, 'block', 0, 200),
            (2, 1, 'if_expression', 50, 150),
            (3, 2, 'block', 60, 100)",
        [],
    )
    .unwrap();

    // Position 25 should match outer block
    let node = graph.get_ast_node_at_position("test.rs", 25).unwrap();
    assert!(node.is_some());
    assert_eq!(node.unwrap().kind, "block");

    // Position 75 should match innermost block (smallest containing node)
    let node = graph.get_ast_node_at_position("test.rs", 75).unwrap();
    assert!(node.is_some());
    assert_eq!(node.unwrap().kind, "block");

    // Position 120 should match if_expression (between blocks)
    let node = graph.get_ast_node_at_position("test.rs", 120).unwrap();
    assert!(node.is_some());
    assert_eq!(node.unwrap().kind, "if_expression");
}

/// Test re-indexing updates AST nodes
///
/// NOTE: This test verifies that re-indexing adds new AST nodes.
/// Due to current schema limitation (no file_id column), old nodes
/// from previous indexing are NOT deleted. This is a known limitation
/// documented in ops.rs around line 433-437.
#[test]
fn test_reindexing_updates_ast_nodes() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");

    let mut graph = CodeGraph::open(&db_path).unwrap();

    // Index initial version
    let source1 = b"fn main() { if true { } }";
    graph.index_file("test.rs", source1).unwrap();
    let count1 = graph.count_ast_nodes().unwrap();
    assert!(count1 > 0);

    let _initial_if_count = graph.get_ast_nodes_by_kind("if_expression").unwrap().len();

    // Re-index with different content
    let source2 = b"fn main() { loop { break; } }";
    graph.index_file("test.rs", source2).unwrap();
    let count2 = graph.count_ast_nodes().unwrap();
    assert!(count2 > 0);

    // Verify new nodes are added (count should increase)
    assert!(count2 >= count1, "Re-indexing should add or maintain nodes");

    // Verify we have new loop_expression nodes
    let loop_nodes = graph.get_ast_nodes_by_kind("loop_expression").unwrap();
    assert!(!loop_nodes.is_empty(), "New loop_expression should exist");

    // NOTE: Due to missing file_id column, old if_expression nodes
    // from first indexing are NOT deleted. This is expected behavior
    // with current schema. Once file_id is added to ast_nodes table,
    // this test can be updated to verify proper cleanup.
}

/// Test empty source file
#[test]
fn test_empty_source_file() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");

    let mut graph = CodeGraph::open(&db_path).unwrap();

    graph.index_file("empty.rs", b"").unwrap();

    // Empty file should have no AST nodes
    let count = graph.count_ast_nodes().unwrap();
    assert_eq!(count, 0);
}

/// Test file with only comments
#[test]
fn test_file_with_only_comments() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");

    let mut graph = CodeGraph::open(&db_path).unwrap();

    let source = b"// This is a comment\n// Another comment\n";
    graph.index_file("comments.rs", source).unwrap();

    // Comment-only file should have no structural AST nodes
    let count = graph.count_ast_nodes().unwrap();
    assert_eq!(count, 0);
}

/// Test deeply nested control flow
#[test]
fn test_deeply_nested_control_flow() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");

    let mut graph = CodeGraph::open(&db_path).unwrap();

    let source = r#"
            fn test() {
                if a {
                    if b {
                        if c {
                            if d {
                                println!("deep");
                            }
                        }
                    }
                }
            }
        "#;

    graph.index_file("nested.rs", source.as_bytes()).unwrap();

    // Should find multiple nested if expressions
    let if_nodes = graph.get_ast_nodes_by_kind("if_expression").unwrap();
    assert_eq!(if_nodes.len(), 4, "Should have 4 nested if expressions");
}

/// Test match expression (has multiple arms)
#[test]
fn test_match_expression() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");

    let mut graph = CodeGraph::open(&db_path).unwrap();

    let source = r#"
            fn test(x: i32) {
                match x {
                    1 => println!("one"),
                    2 => println!("two"),
                    _ => println!("other"),
                }
            }
        "#;

    graph.index_file("match.rs", source.as_bytes()).unwrap();

    // Should find match expression
    let match_nodes = graph.get_ast_nodes_by_kind("match_expression").unwrap();
    assert!(!match_nodes.is_empty(), "Should have match_expression node");
}

/// Test get_ast_roots returns top-level nodes
#[test]
fn test_get_ast_roots() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");
    let graph = CodeGraph::open(&db_path).unwrap();

    // Insert nodes with mixed parentage
    let conn = graph.chunks.connect().unwrap();
    conn.execute(
        "INSERT INTO ast_nodes (id, parent_id, kind, byte_start, byte_end)
         VALUES
            (1, NULL, 'function_item', 0, 100),
            (2, 1, 'block', 10, 90),
            (3, NULL, 'struct_item', 100, 200),
            (4, 3, 'block', 110, 190)",
        [],
    )
    .unwrap();

    let roots = graph.get_ast_roots().unwrap();
    assert_eq!(roots.len(), 2);

    let kinds: Vec<_> = roots.iter().map(|n| n.kind.as_str()).collect();
    assert!(kinds.contains(&"function_item"));
    assert!(kinds.contains(&"struct_item"));
}

/// Performance test for large file
#[test]
#[ignore] // Run explicitly with cargo test -- --ignored
fn test_performance_large_file() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");

    let mut graph = CodeGraph::open(&db_path).unwrap();

    // Generate a large source file (100 functions)
    let mut source = String::new();
    for i in 0..100 {
        source.push_str(&format!("fn func_{}() {{ if x {{ return {}; }} }}\n", i, i));
    }

    let start = std::time::Instant::now();
    graph.index_file("large.rs", source.as_bytes()).unwrap();
    let index_time = start.elapsed();

    // Should complete within reasonable time
    assert!(
        index_time.as_secs() < 5,
        "Indexing too slow: {:?}",
        index_time
    );

    let count = graph.count_ast_nodes().unwrap();
    assert!(
        count > 500,
        "Should have extracted many nodes (got {})",
        count
    );
}

/// Test get_ast_nodes_by_file returns nodes in order
#[test]
fn test_get_ast_nodes_by_file() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");

    let mut graph = CodeGraph::open(&db_path).unwrap();

    let source = r#"
        fn first() { }
        fn second() { }
        fn third() { }
    "#;

    graph.index_file("ordered.rs", source.as_bytes()).unwrap();

    let nodes = graph.get_ast_nodes_by_file("ordered.rs").unwrap();
    assert!(!nodes.is_empty(), "Should have AST nodes");

    // Verify nodes are ordered by byte_start
    for i in 1..nodes.len() {
        assert!(
            nodes[i].node.byte_start >= nodes[i - 1].node.byte_start,
            "Nodes should be ordered by byte_start"
        );
    }
}

/// Test multiple files maintain separate AST nodes
#[test]
fn test_multiple_files_separate_asts() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");

    let mut graph = CodeGraph::open(&db_path).unwrap();

    // Index first file
    let source1 = b"fn foo() { if true { } }";
    graph.index_file("file1.rs", source1).unwrap();

    // Index second file
    let source2 = b"fn bar() { loop { break; } }";
    graph.index_file("file2.rs", source2).unwrap();

    // Both files should contribute to total count
    let total_count = graph.count_ast_nodes().unwrap();
    assert!(total_count > 0, "Should have AST nodes from both files");

    // Should have both if_expression and loop_expression
    let if_nodes = graph.get_ast_nodes_by_kind("if_expression").unwrap();
    let loop_nodes = graph.get_ast_nodes_by_kind("loop_expression").unwrap();

    assert!(!if_nodes.is_empty(), "Should have if_expression from file1");
    assert!(
        !loop_nodes.is_empty(),
        "Should have loop_expression from file2"
    );
}

/// Test let_declaration is captured
#[test]
fn test_let_declaration_captured() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");

    let mut graph = CodeGraph::open(&db_path).unwrap();

    let source = b"fn main() { let x = 42; }";
    graph.index_file("let_test.rs", source).unwrap();

    let let_nodes = graph.get_ast_nodes_by_kind("let_declaration").unwrap();
    assert!(!let_nodes.is_empty(), "Should capture let_declaration");
}

/// Test block expressions are captured
#[test]
fn test_block_expressions_captured() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");

    let mut graph = CodeGraph::open(&db_path).unwrap();

    let source = b"fn main() { let x = { 1 + 2 }; }";
    graph.index_file("block_test.rs", source).unwrap();

    let block_nodes = graph.get_ast_nodes_by_kind("block").unwrap();
    assert!(!block_nodes.is_empty(), "Should capture block expressions");
}

/// Test call_expression is captured
#[test]
fn test_call_expression_captured() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");

    let mut graph = CodeGraph::open(&db_path).unwrap();

    let source = b"fn main() { foo(); bar(); }";
    graph.index_file("call_test.rs", source).unwrap();

    let call_nodes = graph.get_ast_nodes_by_kind("call_expression").unwrap();
    assert!(!call_nodes.is_empty(), "Should capture call_expression");
}

/// Test assignment_expression is captured
#[test]
fn test_assignment_expression_captured() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");

    let mut graph = CodeGraph::open(&db_path).unwrap();

    let source = b"fn main() { let mut x = 1; x = 2; }";
    graph.index_file("assign_test.rs", source).unwrap();

    let assign_nodes = graph
        .get_ast_nodes_by_kind("assignment_expression")
        .unwrap();
    assert!(
        !assign_nodes.is_empty(),
        "Should capture assignment_expression"
    );
}

/// Test struct and enum definitions are captured
#[test]
fn test_struct_enum_captured() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");

    let mut graph = CodeGraph::open(&db_path).unwrap();

    let source = r#"
        struct Foo;
        enum Bar { A, B }
    "#;

    graph
        .index_file("definitions.rs", source.as_bytes())
        .unwrap();

    let struct_nodes = graph.get_ast_nodes_by_kind("struct_item").unwrap();
    assert!(!struct_nodes.is_empty(), "Should capture struct_item");

    let enum_nodes = graph.get_ast_nodes_by_kind("enum_item").unwrap();
    assert!(!enum_nodes.is_empty(), "Should capture enum_item");
}

/// Test impl and trait definitions are captured
#[test]
fn test_impl_trait_captured() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");

    let mut graph = CodeGraph::open(&db_path).unwrap();

    let source = r#"
        trait MyTrait { }
        impl MyTrait for Foo { }
    "#;

    graph
        .index_file("trait_test.rs", source.as_bytes())
        .unwrap();

    let trait_nodes = graph.get_ast_nodes_by_kind("trait_item").unwrap();
    assert!(!trait_nodes.is_empty(), "Should capture trait_item");

    let impl_nodes = graph.get_ast_nodes_by_kind("impl_item").unwrap();
    assert!(!impl_nodes.is_empty(), "Should capture impl_item");
}

/// Test mod_item is captured
#[test]
fn test_mod_item_captured() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");

    let mut graph = CodeGraph::open(&db_path).unwrap();

    let source = b"mod my_mod;";
    graph.index_file("mod_test.rs", source).unwrap();

    let mod_nodes = graph.get_ast_nodes_by_kind("mod_item").unwrap();
    assert!(!mod_nodes.is_empty(), "Should capture mod_item");
}

/// Test const and static items are captured
#[test]
fn test_const_static_captured() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");

    let mut graph = CodeGraph::open(&db_path).unwrap();

    let source = r#"
        const MAX: i32 = 100;
        static COUNTER: AtomicUsize = AtomicUsize::new(0);
    "#;

    graph
        .index_file("const_test.rs", source.as_bytes())
        .unwrap();

    let const_nodes = graph.get_ast_nodes_by_kind("const_item").unwrap();
    assert!(!const_nodes.is_empty(), "Should capture const_item");

    let static_nodes = graph.get_ast_nodes_by_kind("static_item").unwrap();
    assert!(!static_nodes.is_empty(), "Should capture static_item");
}

/// Test break and continue expressions are captured
#[test]
fn test_break_continue_captured() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join("test.db");

    let mut graph = CodeGraph::open(&db_path).unwrap();

    let source = b"fn main() { loop { if x { break; } else { continue; } } }";
    graph.index_file("break_continue_test.rs", source).unwrap();

    let break_nodes = graph.get_ast_nodes_by_kind("break_expression").unwrap();
    assert!(!break_nodes.is_empty(), "Should capture break_expression");

    let continue_nodes = graph.get_ast_nodes_by_kind("continue_expression").unwrap();
    assert!(
        !continue_nodes.is_empty(),
        "Should capture continue_expression"
    );
}