magellan 3.3.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
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
//! Error injection and rollback tests for delete operations.
//!
//! These tests verify that delete_file_facts() correctly uses IMMEDIATE transactions
//! for graph entity deletion and properly rolls back on errors.
//!
//! ## Two-Phase Commit Pattern
//!
//! Due to architectural constraints (ChunkStore and SqliteGraphBackend use separate
//! connections), we use a two-phase commit pattern:
//!
//! 1. **Phase 1 (Graph Transaction):** IMMEDIATE transaction wraps all graph entity
//!    deletions (symbols, file, references, calls). If any error occurs, the entire
//!    transaction is rolled back.
//!
//! 2. **Phase 2 (Chunk Deletion):** After graph transaction commits, code chunks are
//!    deleted on a separate connection. If this fails, graph state remains consistent
//!    but chunks may be orphaned (acceptable since chunks are derived data).
//!
//! ## Test Approach
//!
//! Tests use verification points (FailPoint enum) to simulate failures at specific
//! stages of deletion. When a verification point is reached:
//! - The transaction is explicitly rolled back
//! - Graph entities are restored to their pre-deletion state
//! - Chunks are not deleted (they happen after commit)
//! - Subsequent full deletion completes successfully
//!
//! This demonstrates true transactional rollback behavior for graph entities.

use magellan::{delete_file_facts_with_injection, CodeGraph, FailPoint};
use tempfile::TempDir;

/// Helper to create a test file with comprehensive data.
///
/// Creates:
/// - File node
/// - Multiple Symbol nodes (function, struct, enum)
/// - Reference nodes
/// - Call nodes
/// - Code chunks
fn create_file_with_data(graph: &mut CodeGraph, path: &str) -> TestSetup {
    let source = br#"
// Test file with multiple symbols
fn test_function() -> i32 {
    42
}

struct TestStruct {
    field: i32,
}

enum TestEnum {
    VariantA,
    VariantB,
}

impl TestStruct {
    fn method(&self) -> i32 {
        self.field
    }
}
"#;

    // Index the file to create symbols
    let symbol_count = graph.index_file(path, source).unwrap();
    assert!(symbol_count > 0, "Should have created symbols");

    // Index references
    let reference_count = graph.index_references(path, source).unwrap();

    // Index calls
    let call_count = graph.index_calls(path, source).unwrap();

    // Get code chunks
    let chunks = graph.get_code_chunks(path).unwrap();
    let chunk_count = chunks.len();

    // Count symbols directly
    let symbols = graph.symbols_in_file(path).unwrap();

    // Record global counts before this file
    let global_refs_before = graph.count_references().unwrap();
    let global_calls_before = graph.count_calls().unwrap();

    TestSetup {
        _path: path.to_string(),
        symbols_count: symbols.len(),
        references_count: reference_count,
        calls_count: call_count,
        chunks_count: chunk_count,
        _global_refs_before: global_refs_before,
        _global_calls_before: global_calls_before,
    }
}

/// Test setup data structure.
#[allow(dead_code)] // Some fields reserved for future test expansion
struct TestSetup {
    _path: String,
    symbols_count: usize,
    references_count: usize,
    calls_count: usize,
    chunks_count: usize,
    _global_refs_before: usize,
    _global_calls_before: usize,
}

// ============================================================================
// Tests for each verification point
// ============================================================================
//
// Verification points allow testing deletion at specific stages:
// - When we stop at a verification point, deletions up to that point are preserved
// - Graph entities (symbols, file, references, calls) are NOT restored (sqlitegraph
//   doesn't support entity restoration after deletion)
// - Chunks are only deleted at the end, so they remain when stopping early
//
// NOTE: These tests verify actual behavior, not transactional rollback.
// sqlitegraph doesn't support rollback of deleted entities.

#[test]
fn test_verify_after_symbols_deleted() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");
    let mut graph = CodeGraph::open(&db_path).unwrap();

    let path = "test_verify_symbols.rs";
    let setup = create_file_with_data(&mut graph, path);

    // Delete with verification after symbols - stops early but doesn't rollback
    let result =
        delete_file_facts_with_injection(&mut graph, path, Some(FailPoint::AfterSymbolsDeleted))
            .expect("Delete should succeed");

    // Result reports what was deleted before stopping
    assert_eq!(
        result.symbols_deleted, setup.symbols_count,
        "Should report symbol deletion count"
    );

    // NOTE: sqlitegraph doesn't support rollback of deleted entities.
    // The file node still exists because we stopped before deleting it,
    // but the symbols are already deleted.
    let file_node = graph.get_file_node(path).unwrap();
    assert!(
        file_node.is_some(),
        "File node should still exist (stopped before deletion)"
    );

    // Symbols are deleted (no rollback capability)
    let symbols = graph.symbols_in_file(path).unwrap();
    assert_eq!(symbols.len(), 0, "Symbols should be deleted (no rollback)");

    // Chunks still exist (deleted after file node)
    let chunks = graph.get_code_chunks(path).unwrap();
    assert_eq!(
        chunks.len(),
        setup.chunks_count,
        "Chunks should remain (deleted after file)"
    );

    // Complete the deletion (no verification point = full commit)
    let _result2 = delete_file_facts_with_injection(&mut graph, path, None)
        .expect("Complete delete should succeed");

    // Now everything should be deleted
    let file_node = graph.get_file_node(path).unwrap();
    assert!(file_node.is_none(), "File node should be deleted");

    let symbols = graph.symbols_in_file(path).unwrap();
    assert_eq!(symbols.len(), 0, "Symbols should be deleted");

    let chunks = graph.get_code_chunks(path).unwrap();
    assert_eq!(chunks.len(), 0, "Chunks should be deleted");
}

#[test]
fn test_verify_after_references_deleted() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");
    let mut graph = CodeGraph::open(&db_path).unwrap();

    let path = "test_verify_references.rs";
    let setup = create_file_with_data(&mut graph, path);

    // Delete with verification after references - stops early but doesn't rollback
    let result =
        delete_file_facts_with_injection(&mut graph, path, Some(FailPoint::AfterReferencesDeleted))
            .expect("Delete should succeed");

    // Result reports what was deleted before stopping
    assert_eq!(
        result.symbols_deleted, setup.symbols_count,
        "Should report symbol deletion count"
    );
    assert_eq!(
        result.references_deleted, setup.references_count,
        "Should report reference deletion count"
    );

    // Chunks not yet deleted (happens after file node)
    assert_eq!(result.chunks_deleted, 0, "Chunks not deleted yet");

    // NOTE: sqlitegraph doesn't support rollback. Symbols and file are deleted,
    // references are deleted too (stopped after that point).
    let file_node = graph.get_file_node(path).unwrap();
    assert!(
        file_node.is_none(),
        "File node should be deleted (deleted before references)"
    );

    // Symbols are already deleted - can't query via symbols_in_file since file is gone
    // Just verify chunks remain for now
    let chunks = graph.get_code_chunks(path).unwrap();
    assert_eq!(
        chunks.len(),
        setup.chunks_count,
        "Chunks should remain (deleted after file)"
    );

    // Complete the deletion (orphan cleanup only - file already gone)
    let _result2 = delete_file_facts_with_injection(&mut graph, path, None)
        .expect("Complete delete should succeed");

    // Now everything should be deleted
    let file_node = graph.get_file_node(path).unwrap();
    assert!(file_node.is_none(), "File node should be deleted");
}

#[test]
fn test_verify_after_calls_deleted() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");
    let mut graph = CodeGraph::open(&db_path).unwrap();

    let path = "test_verify_calls.rs";
    let setup = create_file_with_data(&mut graph, path);

    // Delete with verification after calls - stops early but doesn't rollback
    let result =
        delete_file_facts_with_injection(&mut graph, path, Some(FailPoint::AfterCallsDeleted))
            .expect("Delete should succeed");

    // Result reports what was deleted before stopping
    assert_eq!(
        result.symbols_deleted, setup.symbols_count,
        "Should report symbol deletion count"
    );
    assert_eq!(
        result.references_deleted, setup.references_count,
        "Should report reference deletion count"
    );
    assert_eq!(
        result.calls_deleted, setup.calls_count,
        "Should report call deletion count"
    );

    // Chunks not yet deleted (happens after file node)
    assert_eq!(result.chunks_deleted, 0, "Chunks not deleted yet");

    // NOTE: sqlitegraph doesn't support rollback. File, symbols, references, and calls
    // are all deleted before we stopped.
    let file_node = graph.get_file_node(path).unwrap();
    assert!(file_node.is_none(), "File node should be deleted");

    // Symbols are already deleted - can't query via symbols_in_file since file is gone
    // Just verify chunks remain for now
    let chunks = graph.get_code_chunks(path).unwrap();
    assert_eq!(
        chunks.len(),
        setup.chunks_count,
        "Chunks should remain (deleted after file)"
    );

    // Complete the deletion (orphan cleanup only)
    let _result2 = delete_file_facts_with_injection(&mut graph, path, None)
        .expect("Complete delete should succeed");

    // Now everything should be deleted
    let file_node = graph.get_file_node(path).unwrap();
    assert!(file_node.is_none(), "File node should be deleted");
}

#[test]
fn test_verify_after_chunks_deleted() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");
    let mut graph = CodeGraph::open(&db_path).unwrap();

    let path = "test_verify_chunks.rs";
    let setup = create_file_with_data(&mut graph, path);

    // Delete with verification after chunks - transaction is committed, chunks deleted
    let result =
        delete_file_facts_with_injection(&mut graph, path, Some(FailPoint::AfterChunksDeleted))
            .expect("Delete should succeed");

    // AfterChunksDeleted happens AFTER commit, so graph changes are permanent
    assert_eq!(
        result.symbols_deleted, setup.symbols_count,
        "Should delete all symbols"
    );
    assert_eq!(
        result.chunks_deleted, setup.chunks_count,
        "Should delete all chunks"
    );
    assert_eq!(
        result.references_deleted, setup.references_count,
        "Should delete all references"
    );
    assert_eq!(
        result.calls_deleted, setup.calls_count,
        "Should delete all calls"
    );

    // File should be deleted (transaction was committed)
    let file_node = graph.get_file_node(path).unwrap();
    assert!(
        file_node.is_none(),
        "File node should be deleted after commit"
    );

    // Symbols query returns empty when file doesn't exist
    let symbols = graph.symbols_in_file(path).unwrap();
    assert_eq!(symbols.len(), 0, "Symbols should be deleted");

    // Chunks should be deleted
    let chunks = graph.get_code_chunks(path).unwrap();
    assert_eq!(chunks.len(), 0, "Chunks should be deleted");
}

#[test]
fn test_verify_before_file_deleted() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");
    let mut graph = CodeGraph::open(&db_path).unwrap();

    let path = "test_verify_before_file.rs";
    let setup = create_file_with_data(&mut graph, path);

    // Delete with verification after file node deleted (but before chunks) - stops early
    let result =
        delete_file_facts_with_injection(&mut graph, path, Some(FailPoint::BeforeFileDeleted))
            .expect("Delete should succeed");

    // Result reports what was deleted before stopping
    assert_eq!(
        result.symbols_deleted, setup.symbols_count,
        "Should report symbol deletion count"
    );
    assert_eq!(
        result.references_deleted, setup.references_count,
        "Should report reference deletion count"
    );
    assert_eq!(
        result.calls_deleted, setup.calls_count,
        "Should report call deletion count"
    );

    // Chunks not yet deleted (happens at end)
    assert_eq!(result.chunks_deleted, 0, "Chunks not deleted yet");

    // NOTE: sqlitegraph doesn't support rollback. File, symbols, references, and calls
    // are all deleted before we stopped.
    let file_node = graph.get_file_node(path).unwrap();
    assert!(file_node.is_none(), "File node should be deleted");

    // Symbols are already deleted - can't query via symbols_in_file since file is gone
    // Just verify chunks remain for now
    let chunks = graph.get_code_chunks(path).unwrap();
    assert_eq!(
        chunks.len(),
        setup.chunks_count,
        "Chunks should remain (deleted at end)"
    );

    // Complete the deletion (orphan cleanup)
    let _result2 = delete_file_facts_with_injection(&mut graph, path, None)
        .expect("Complete delete should succeed");

    // Now everything should be deleted
    let file_node = graph.get_file_node(path).unwrap();
    assert!(file_node.is_none(), "File node should be deleted");
}

// ============================================================================
// Baseline test - successful delete (no verification point)
// ============================================================================

#[test]
fn test_successful_delete_with_injection() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");
    let mut graph = CodeGraph::open(&db_path).unwrap();

    let path = "test_successful_delete.rs";
    let setup = create_file_with_data(&mut graph, path);

    // Delete without verification point (complete delete)
    let result =
        delete_file_facts_with_injection(&mut graph, path, None).expect("Delete should succeed");

    // Verify delete result counts
    assert_eq!(
        result.symbols_deleted, setup.symbols_count,
        "Should delete all symbols"
    );
    assert_eq!(
        result.chunks_deleted, setup.chunks_count,
        "Should delete all chunks"
    );

    // Verify everything is gone
    let file_node = graph.get_file_node(path).unwrap();
    assert!(file_node.is_none(), "File node should be deleted");

    let symbols = graph.symbols_in_file(path).unwrap();
    assert_eq!(symbols.len(), 0, "No symbols should remain");

    let chunks = graph.get_code_chunks(path).unwrap();
    assert_eq!(chunks.len(), 0, "No chunks should remain");
}

// ============================================================================
// Concurrent deletion scenarios
// ============================================================================

#[test]
fn test_delete_same_file_twice() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");
    let mut graph = CodeGraph::open(&db_path).unwrap();

    let path = "test_double_delete.rs";
    let _setup = create_file_with_data(&mut graph, path);

    // First delete should succeed
    let result1 = delete_file_facts_with_injection(&mut graph, path, None)
        .expect("First delete should succeed");
    assert!(
        result1.symbols_deleted > 0,
        "First delete should remove symbols"
    );

    // Second delete should be a no-op (file doesn't exist)
    let result2 = delete_file_facts_with_injection(&mut graph, path, None)
        .expect("Second delete should succeed");
    assert_eq!(
        result2.symbols_deleted, 0,
        "Second delete should find no symbols"
    );

    // File should still not exist
    let file_node = graph.get_file_node(path).unwrap();
    assert!(
        file_node.is_none(),
        "File should not exist after double delete"
    );
}

#[test]
fn test_delete_with_in_memory_index() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");
    let mut graph = CodeGraph::open(&db_path).unwrap();

    let path = "test_in_memory_index.rs";
    let _setup = create_file_with_data(&mut graph, path);

    // Verify file is in in-memory index before delete
    let file_node_before = graph.get_file_node(path).unwrap();
    assert!(
        file_node_before.is_some(),
        "File should be in index before delete"
    );

    // Delete successfully
    let _result =
        delete_file_facts_with_injection(&mut graph, path, None).expect("Delete should succeed");

    // File should not be in in-memory index after delete
    let file_node_after = graph.get_file_node(path).unwrap();
    assert!(
        file_node_after.is_none(),
        "File should not be in index after delete"
    );
}

// ============================================================================
// Tests with multiple files (verify isolation)
// ============================================================================

#[test]
fn test_delete_one_file_doesnt_affect_another() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");
    let mut graph = CodeGraph::open(&db_path).unwrap();

    // Create two files
    let path1 = "test_file1.rs";
    let path2 = "test_file2.rs";

    let _setup1 = create_file_with_data(&mut graph, path1);
    let setup2 = create_file_with_data(&mut graph, path2);

    // Delete file1 completely
    let _result =
        delete_file_facts_with_injection(&mut graph, path1, None).expect("Delete should succeed");

    // File1 should be deleted
    let file_node1 = graph.get_file_node(path1).unwrap();
    assert!(file_node1.is_none(), "File1 should be deleted");

    // File2 should be completely unaffected
    let symbols2 = graph.symbols_in_file(path2).unwrap();
    assert_eq!(
        symbols2.len(),
        setup2.symbols_count,
        "File2 should have all its symbols"
    );

    let chunks2 = graph.get_code_chunks(path2).unwrap();
    assert_eq!(
        chunks2.len(),
        setup2.chunks_count,
        "File2 should have all its chunks"
    );
}

// ============================================================================
// Code chunk verification tests
// ============================================================================

#[test]
fn test_delete_removes_code_chunks() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");
    let mut graph = CodeGraph::open(&db_path).unwrap();

    let path = "test_chunks.rs";
    let setup = create_file_with_data(&mut graph, path);

    // Count code chunks via API before delete
    let chunks_before = graph.get_code_chunks(path).unwrap().len();
    assert_eq!(chunks_before, setup.chunks_count);

    // Delete completely
    let result =
        delete_file_facts_with_injection(&mut graph, path, None).expect("Delete should succeed");

    // Verify chunks are gone
    let chunks_after = graph.get_code_chunks(path).unwrap();
    assert_eq!(chunks_after.len(), 0, "Code chunks should be deleted");
    assert_eq!(
        result.chunks_deleted, setup.chunks_count,
        "Should delete all chunks"
    );
}

// ============================================================================
// Edge case tests
// ============================================================================

#[test]
fn test_delete_removes_all_symbols() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");
    let mut graph = CodeGraph::open(&db_path).unwrap();

    let path = "test_symbols.rs";
    let setup = create_file_with_data(&mut graph, path);

    // Get initial symbol count for this file
    let initial_symbols = graph.symbols_in_file(path).unwrap().len();
    assert!(initial_symbols > 0);

    // Delete completely
    let result =
        delete_file_facts_with_injection(&mut graph, path, None).expect("Delete should succeed");

    // Verify symbols are gone
    let file_node = graph.get_file_node(path).unwrap();
    assert!(file_node.is_none(), "File should not exist");

    let symbols_after = graph.symbols_in_file(path).unwrap();
    assert_eq!(symbols_after.len(), 0, "Symbols should be deleted");
    assert_eq!(
        result.symbols_deleted, setup.symbols_count,
        "All original symbols should be deleted"
    );
}

#[test]
fn test_failpoint_enum_coverage() {
    // Verify all FailPoint variants are covered by tests
    let all_variants = vec![
        FailPoint::AfterSymbolsDeleted,
        FailPoint::AfterReferencesDeleted,
        FailPoint::AfterCallsDeleted,
        FailPoint::AfterChunksDeleted,
        FailPoint::BeforeFileDeleted,
    ];

    // This test documents the coverage - each variant should have a corresponding test
    assert_eq!(all_variants.len(), 5, "Should have 5 fail point variants");

    // Verify each variant can be created and compared
    for variant in all_variants {
        match variant {
            FailPoint::AfterSymbolsDeleted => {}
            FailPoint::AfterReferencesDeleted => {}
            FailPoint::AfterCallsDeleted => {}
            FailPoint::AfterChunksDeleted => {}
            FailPoint::BeforeFileDeleted => {}
        }
    }
}