magellan 3.3.1

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
#![cfg(feature = "geometric-backend")]
//! System-level workflow tests for Magellan
//!
//! These tests verify real behavior using actual filesystem operations
//! and real .geo databases using library APIs.

#[cfg(feature = "geometric-backend")]
mod tests {
    use magellan::graph::geo_index::{scan_directory_with_progress, IndexingMode};
    use magellan::graph::geometric_backend::GeometricBackend;
    use std::path::Path;
    use std::thread;
    use std::time::Duration;

    /// Helper: Create a test project with multiple Rust files
    fn create_test_project(dir: &Path) -> std::io::Result<()> {
        std::fs::create_dir_all(dir.join("src"))?;
        std::fs::create_dir_all(dir.join("src/utils"))?;

        // Main lib file
        std::fs::write(
            dir.join("src/lib.rs"),
            r#"pub mod utils;

pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

pub fn process_data(input: &str) -> String {
    format!("Processed: {}", input)
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_add() {
        assert_eq!(add(2, 3), 5);
    }
}
"#,
        )?;

        // Utils module
        std::fs::write(
            dir.join("src/utils/mod.rs"),
            r#"pub fn helper(x: i32) -> i32 {
    x * 2
}

pub struct DataProcessor {
    value: i32,
}

impl DataProcessor {
    pub fn new(value: i32) -> Self {
        Self { value }
    }
    
    pub fn process(&self) -> i32 {
        self.value * 3
    }
}
"#,
        )?;

        // Main binary
        std::fs::write(
            dir.join("src/main.rs"),
            r#"fn main() {
    println!("Hello from main");
}
"#,
        )?;

        Ok(())
    }

    /// Test 1: Indexing completes and creates valid .geo database
    #[test]
    fn indexing_completes_and_creates_valid_geo() {
        let temp_dir = tempfile::tempdir().unwrap();
        let db_path = temp_dir.path().join("test.geo");
        let src_path = temp_dir.path().join("src");

        create_test_project(temp_dir.path()).unwrap();

        // Create backend and index
        let mut backend = GeometricBackend::create(&db_path).expect("Failed to create backend");

        let result = scan_directory_with_progress(
            &mut backend,
            &src_path,
            None, // No progress callback for tests
            IndexingMode::CfgFirst,
        );

        assert!(
            result.is_ok(),
            "Indexing should succeed: {:?}",
            result.err()
        );

        // Save to disk
        let save_result = backend.save_to_disk();
        assert!(save_result.is_ok(), "Save should succeed");

        // Verify database exists
        assert!(db_path.exists(), "Database file should be created");

        // Verify we can reopen it
        let reopened = GeometricBackend::open(&db_path);
        assert!(reopened.is_ok(), "Should be able to reopen database");

        // Verify stats are available
        let backend = reopened.unwrap();
        let stats = backend.get_stats();
        assert!(stats.is_ok(), "Should be able to get stats");
    }

    /// Test 2: Repeated index of same src tree is idempotent
    #[test]
    fn repeated_index_same_src_tree_is_idempotent() {
        let temp_dir = tempfile::tempdir().unwrap();
        let db_path = temp_dir.path().join("test.geo");
        let src_path = temp_dir.path().join("src");

        create_test_project(temp_dir.path()).unwrap();

        // First index
        {
            let mut backend = GeometricBackend::create(&db_path).expect("Failed to create backend");
            scan_directory_with_progress(&mut backend, &src_path, None, IndexingMode::CfgFirst)
                .expect("First index failed");
            backend.save_to_disk().expect("First save failed");
        }

        // Get first stats
        let stats1 = {
            let backend = GeometricBackend::open(&db_path).expect("Failed to reopen");
            backend.get_geometric_stats()
        };

        // Second index (same database - should clear and reindex due to clear_file_data)
        {
            let mut backend =
                GeometricBackend::open(&db_path).expect("Failed to reopen for reindex");
            scan_directory_with_progress(&mut backend, &src_path, None, IndexingMode::CfgFirst)
                .expect("Second index failed");
            backend.save_to_disk().expect("Second save failed");
        }

        // Get second stats
        let stats2 = {
            let backend = GeometricBackend::open(&db_path).expect("Failed to reopen");
            backend.get_geometric_stats()
        };

        // Stats should be stable
        assert_eq!(
            stats1.symbol_count, stats2.symbol_count,
            "Symbol count should be stable after reindex. First: {}, Second: {}",
            stats1.symbol_count, stats2.symbol_count
        );
        assert_eq!(
            stats1.file_count, stats2.file_count,
            "File count should be stable after reindex. First: {}, Second: {}",
            stats1.file_count, stats2.file_count
        );
    }

    /// Test 3: Reopen counts match after save
    #[test]
    fn reopen_counts_match_after_save() {
        let temp_dir = tempfile::tempdir().unwrap();
        let db_path = temp_dir.path().join("test.geo");
        let src_path = temp_dir.path().join("src");

        create_test_project(temp_dir.path()).unwrap();

        // Index and save
        {
            let mut backend = GeometricBackend::create(&db_path).expect("Failed to create backend");
            scan_directory_with_progress(&mut backend, &src_path, None, IndexingMode::CfgFirst)
                .expect("Index failed");
            backend.save_to_disk().expect("Save failed");
        }

        // First status check
        let stats1 = {
            let backend = GeometricBackend::open(&db_path).expect("Failed to reopen");
            backend.get_geometric_stats()
        };

        // Small delay
        thread::sleep(Duration::from_millis(50));

        // Second status check (reopen)
        let stats2 = {
            let backend = GeometricBackend::open(&db_path).expect("Failed to reopen");
            backend.get_geometric_stats()
        };

        // Stats should match
        assert_eq!(
            stats1.symbol_count, stats2.symbol_count,
            "Symbol count should match after reopen"
        );
        assert_eq!(
            stats1.file_count, stats2.file_count,
            "File count should match after reopen"
        );
        assert_eq!(
            stats1.node_count, stats2.node_count,
            "Node count should match after reopen"
        );
        assert_eq!(
            stats1.cfg_block_count, stats2.cfg_block_count,
            "CFG block count should match after reopen"
        );
    }

    /// Test 4: Symbol resolution finds functions
    #[test]
    fn symbol_resolution_finds_functions() {
        let temp_dir = tempfile::tempdir().unwrap();
        let db_path = temp_dir.path().join("test.geo");
        let src_path = temp_dir.path().join("src");

        create_test_project(temp_dir.path()).unwrap();

        // Index
        {
            let mut backend = GeometricBackend::create(&db_path).expect("Failed to create backend");
            scan_directory_with_progress(&mut backend, &src_path, None, IndexingMode::CfgFirst)
                .expect("Index failed");
            backend.save_to_disk().expect("Save failed");
        }

        // Reopen and search for symbols
        let backend = GeometricBackend::open(&db_path).expect("Failed to reopen");

        // Should find 'add' function
        let add_results = backend.find_symbols_by_name_info("add");
        assert!(!add_results.is_empty(), "Should find 'add' function");

        // Should find 'helper' function
        let helper_results = backend.find_symbols_by_name_info("helper");
        assert!(!helper_results.is_empty(), "Should find 'helper' function");

        // Should find 'DataProcessor' struct
        let struct_results = backend.find_symbols_by_name_info("DataProcessor");
        assert!(
            !struct_results.is_empty(),
            "Should find 'DataProcessor' struct"
        );

        // Should find 'process' method
        let method_results = backend.find_symbols_by_name_info("process");
        assert!(!method_results.is_empty(), "Should find 'process' method");
    }

    /// Test 5: Dead code detection works
    #[test]
    fn dead_code_analysis_runs_successfully() {
        let temp_dir = tempfile::tempdir().unwrap();
        let db_path = temp_dir.path().join("test.geo");
        let src_path = temp_dir.path().join("src");

        create_test_project(temp_dir.path()).unwrap();

        // Index
        {
            let mut backend = GeometricBackend::create(&db_path).expect("Failed to create backend");
            scan_directory_with_progress(&mut backend, &src_path, None, IndexingMode::CfgFirst)
                .expect("Index failed");
            backend.save_to_disk().expect("Save failed");
        }

        // Reopen and run dead code analysis
        let backend = GeometricBackend::open(&db_path).expect("Failed to reopen");

        // Get entry points (main function)
        let entry_symbols = backend.find_symbols_by_name_info("main");
        let entry_ids: Vec<u64> = entry_symbols.iter().map(|s| s.id).collect();

        // Should be able to analyze without panic
        if !entry_ids.is_empty() {
            let dead_code = backend.dead_code_from_entries(&entry_ids);
            // Just verify it returns something (could be empty if everything is reachable)
            // The important thing is it doesn't panic
        }
    }

    /// Test 6: CFG first indexing mode produces expected data
    #[test]
    fn cfg_first_mode_creates_cfg_data() {
        let temp_dir = tempfile::tempdir().unwrap();
        let db_path = temp_dir.path().join("test.geo");
        let src_path = temp_dir.path().join("src");

        create_test_project(temp_dir.path()).unwrap();

        // Index with CfgFirst mode
        {
            let mut backend = GeometricBackend::create(&db_path).expect("Failed to create backend");
            scan_directory_with_progress(&mut backend, &src_path, None, IndexingMode::CfgFirst)
                .expect("Index failed");
            backend.save_to_disk().expect("Save failed");
        }

        // Reopen and verify CFG data exists
        let backend = GeometricBackend::open(&db_path).expect("Failed to reopen");
        let stats = backend.get_geometric_stats();

        // CFG data should exist in CfgFirst mode
        // Note: CFG count may vary based on parsing, but should generally have data
        // for functions like 'add', 'helper', etc.
    }

    /// Test 7: Both indexing modes work
    #[test]
    fn different_indexing_modes_work() {
        let temp_dir = tempfile::tempdir().unwrap();
        let src_path = temp_dir.path().join("src");

        create_test_project(temp_dir.path()).unwrap();

        // Test CfgFirst mode
        let db_path_cfg = temp_dir.path().join("test_cfg.geo");
        {
            let mut backend =
                GeometricBackend::create(&db_path_cfg).expect("Failed to create backend");

            let result =
                scan_directory_with_progress(&mut backend, &src_path, None, IndexingMode::CfgFirst);
            assert!(result.is_ok(), "Indexing with CfgFirst should succeed");

            backend.save_to_disk().expect("Save failed");
            assert!(db_path_cfg.exists(), "Database should exist for CfgFirst");
        }

        // Test FullAst mode
        let db_path_ast = temp_dir.path().join("test_ast.geo");
        {
            let mut backend =
                GeometricBackend::create(&db_path_ast).expect("Failed to create backend");

            let result =
                scan_directory_with_progress(&mut backend, &src_path, None, IndexingMode::FullAst);
            assert!(result.is_ok(), "Indexing with FullAst should succeed");

            backend.save_to_disk().expect("Save failed");
            assert!(db_path_ast.exists(), "Database should exist for FullAst");
        }
    }

    /// Test 8: End-to-end smoke test
    #[test]
    fn end_to_end_smoke_test() {
        let temp_dir = tempfile::tempdir().unwrap();
        let db_path = temp_dir.path().join("test.geo");
        let src_path = temp_dir.path().join("src");

        create_test_project(temp_dir.path()).unwrap();

        // Step 1: Create and index
        {
            let mut backend =
                GeometricBackend::create(&db_path).expect("Step 1: Failed to create backend");

            let result =
                scan_directory_with_progress(&mut backend, &src_path, None, IndexingMode::CfgFirst);
            assert!(result.is_ok(), "Step 1: Indexing failed");

            let save_result = backend.save_to_disk();
            assert!(save_result.is_ok(), "Step 1: Save failed");
        }

        // Step 2: Reopen and verify data
        {
            let backend = GeometricBackend::open(&db_path).expect("Step 2: Failed to reopen");

            let stats = backend.get_geometric_stats();

            assert!(stats.symbol_count > 0, "Step 2: Should have symbols");
            assert!(
                stats.file_count >= 3,
                "Step 2: Should have at least 3 files"
            );

            // Query for specific symbols
            let results = backend.find_symbols_by_name_info("process_data");
            assert!(!results.is_empty(), "Step 2: Should find 'process_data'");
        }

        // Step 3: Reindex and verify stability
        {
            let mut backend =
                GeometricBackend::open(&db_path).expect("Step 3: Failed to reopen for reindex");

            let reindex_result =
                scan_directory_with_progress(&mut backend, &src_path, None, IndexingMode::CfgFirst);
            assert!(reindex_result.is_ok(), "Step 3: Reindex failed");

            backend
                .save_to_disk()
                .expect("Step 3: Save after reindex failed");
        }

        // Step 4: Final verification
        {
            let backend = GeometricBackend::open(&db_path).expect("Step 4: Failed final reopen");

            let stats = backend.get_geometric_stats();
            assert!(stats.symbol_count > 0, "Step 4: Should still have symbols");

            // Test file path query
            let files = backend.get_all_file_paths();
            assert!(!files.is_empty(), "Step 4: Should have files");
        }
    }
}