cora-code 0.9.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
//! Call graph traversal for `cora callers` and `cora impact`.
//!
//! Uses the existing `engine/context/extraction.rs` symbol reference extraction
//! to build call edges at index time, then traverse them for queries.

use rusqlite::Connection;

#[allow(unused_imports)]
use super::symbols::{IndexedSymbol, SymbolKind};

/// A directed edge in the call graph: caller → callee.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct CallEdge {
    /// Symbol that makes the call.
    pub caller: String,
    /// Symbol that is called.
    pub callee: String,
    /// File where the call happens.
    pub file: String,
    /// Line number of the call site.
    pub line: u32,
}

/// Store call edges in the database, scoped to a project.
#[allow(dead_code)]
pub fn store_edges(
    conn: &Connection,
    edges: &[CallEdge],
    project_id: i64,
) -> anyhow::Result<usize> {
    let tx = conn.unchecked_transaction()?;
    let mut count = 0;
    for edge in edges {
        tx.execute(
            "INSERT INTO call_graph (caller, callee, file, line, project_id) VALUES (?1, ?2, ?3, ?4, ?5)",
            rusqlite::params![edge.caller, edge.callee, edge.file, edge.line as i64, project_id],
        )?;
        count += 1;
    }
    tx.commit()?;
    Ok(count)
}

/// A typed edge in the knowledge graph.
#[cfg(feature = "tree-sitter")]
#[derive(Debug, Clone)]
pub struct KgEdge {
    /// Source symbol.
    pub source: String,
    /// Edge kind: CALLS, IMPORTS, IMPLEMENTS, INHERITS, CHILD_OF.
    pub kind: String,
    /// Target symbol.
    pub target: String,
    /// File where the relationship is defined.
    pub file: String,
    /// Line number.
    pub line: u32,
}

/// Store knowledge graph edges in the `edges` table.
#[cfg(feature = "tree-sitter")]
#[allow(dead_code)]
pub fn store_kg_edges(
    conn: &Connection,
    edges: &[KgEdge],
    project_id: i64,
) -> anyhow::Result<usize> {
    let tx = conn.unchecked_transaction()?;
    let mut count = 0;
    for edge in edges {
        tx.execute(
            "INSERT INTO edges (source, kind, target, file, line, project_id) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
            rusqlite::params![edge.source, edge.kind, edge.target, edge.file, edge.line as i64, project_id],
        )?;
        count += 1;
    }
    tx.commit()?;
    Ok(count)
}

/// Clear knowledge graph edges for a specific file.
#[cfg(feature = "tree-sitter")]
#[allow(dead_code)]
pub fn clear_kg_edges_for_file(
    conn: &Connection,
    file: &str,
    project_id: i64,
) -> anyhow::Result<()> {
    conn.execute(
        "DELETE FROM edges WHERE file = ?1 AND project_id = ?2",
        rusqlite::params![file, project_id],
    )?;
    Ok(())
}

/// Clear call graph edges for a specific file (before re-indexing), scoped to project.
#[allow(dead_code)]
pub fn clear_edges_for_file(conn: &Connection, file: &str, project_id: i64) -> anyhow::Result<()> {
    conn.execute(
        "DELETE FROM call_graph WHERE file = ?1 AND project_id = ?2",
        rusqlite::params![file, project_id],
    )?;
    Ok(())
}

/// Find all callers of a symbol (who calls this?), scoped to a project.
///
/// Returns symbols that call the given name, grouped by file.
pub fn find_callers(
    conn: &Connection,
    project_id: i64,
    symbol_name: &str,
    limit: usize,
) -> anyhow::Result<Vec<CallerResult>> {
    let pattern = format!("%{symbol_name}%");

    let mut stmt = conn.prepare(
        "SELECT DISTINCT cg.caller, cg.file, cg.line
         FROM call_graph cg
         WHERE cg.callee LIKE ?1 AND cg.project_id = ?2
         LIMIT ?3",
    )?;

    let rows = stmt.query_map(
        rusqlite::params![pattern, project_id, limit as i64],
        |row| {
            Ok(CallerResult {
                caller: row.get(0)?,
                file: row.get(1)?,
                line: row.get::<_, i64>(2)? as u32,
            })
        },
    )?;

    Ok(rows.filter_map(|r| r.ok()).collect())
}

/// Find callers of a symbol across ALL projects (cross-project fallback).
///
/// Used when project-scoped `find_callers` returns empty — the symbol may
/// exist in another indexed project. Returns results with the project root
/// path so the caller can display which project the match came from.
pub fn find_callers_cross_project(
    conn: &Connection,
    symbol_name: &str,
    limit: usize,
) -> anyhow::Result<Vec<CrossProjectCallerResult>> {
    let pattern = format!("%{symbol_name}%");

    let mut stmt = conn.prepare(
        "SELECT DISTINCT cg.caller, cg.file, cg.line, p.root_path
         FROM call_graph cg
         JOIN projects p ON cg.project_id = p.id
         WHERE cg.callee LIKE ?1
         LIMIT ?2",
    )?;

    let rows = stmt.query_map(rusqlite::params![pattern, limit as i64], |row| {
        Ok(CrossProjectCallerResult {
            caller: row.get(0)?,
            file: row.get(1)?,
            line: row.get::<_, i64>(2)? as u32,
            project_root: row.get(3)?,
        })
    })?;

    Ok(rows.filter_map(|r| r.ok()).collect())
}

/// Find all callees of a symbol (what does this call?), scoped to a project.
///
/// Returns symbols that are called by the given name.
#[allow(dead_code)]
pub fn find_callees(
    conn: &Connection,
    project_id: i64,
    symbol_name: &str,
    limit: usize,
) -> anyhow::Result<Vec<CalleeResult>> {
    let pattern = format!("%{symbol_name}%");

    let mut stmt = conn.prepare(
        "SELECT DISTINCT cg.callee, cg.file, cg.line
         FROM call_graph cg
         WHERE cg.caller LIKE ?1 AND cg.project_id = ?2
         LIMIT ?3",
    )?;

    let rows = stmt.query_map(
        rusqlite::params![pattern, project_id, limit as i64],
        |row| {
            Ok(CalleeResult {
                callee: row.get(0)?,
                file: row.get(1)?,
                line: row.get::<_, i64>(2)? as u32,
            })
        },
    )?;

    Ok(rows.filter_map(|r| r.ok()).collect())
}

/// Impact analysis: what breaks if a symbol changes?, scoped to a project.
///
/// Uses reverse traversal: find all callers recursively up to `depth`.
pub fn impact_analysis(
    conn: &Connection,
    project_id: i64,
    symbol_name: &str,
    depth: u32,
) -> anyhow::Result<Vec<ImpactNode>> {
    let mut visited: std::collections::HashSet<String> = std::collections::HashSet::new();
    let mut result = Vec::new();
    let mut current_level = vec![symbol_name.to_string()];
    let mut current_depth = 0u32;

    while current_depth < depth && !current_level.is_empty() {
        let mut next_level = Vec::new();

        for sym in &current_level {
            if !visited.insert(sym.clone()) {
                continue;
            }

            let callers = find_callers(conn, project_id, sym, 100)?;
            for caller in callers {
                let node = ImpactNode {
                    symbol: caller.caller.clone(),
                    file: caller.file.clone(),
                    line: caller.line,
                    depth: current_depth + 1,
                };

                if !visited.contains(&caller.caller) {
                    next_level.push(caller.caller.clone());
                }

                result.push(node);
            }
        }

        current_level = next_level;
        current_depth += 1;
    }

    // Sort by depth then file
    result.sort_by(|a, b| {
        a.depth
            .cmp(&b.depth)
            .then_with(|| a.file.cmp(&b.file))
            .then_with(|| a.line.cmp(&b.line))
    });

    Ok(result)
}

/// A caller result entry.
#[derive(Debug, Clone, serde::Serialize)]
pub struct CallerResult {
    pub caller: String,
    pub file: String,
    pub line: u32,
}

/// A cross-project caller result (includes which project the caller belongs to).
#[derive(Debug, Clone, serde::Serialize)]
pub struct CrossProjectCallerResult {
    pub caller: String,
    pub file: String,
    pub line: u32,
    pub project_root: String,
}

/// A callee result entry.
#[allow(dead_code)]
#[derive(Debug, Clone, serde::Serialize)]
pub struct CalleeResult {
    pub callee: String,
    pub file: String,
    pub line: u32,
}

/// An impact analysis node.
#[derive(Debug, Clone, serde::Serialize)]
pub struct ImpactNode {
    pub symbol: String,
    pub file: String,
    pub line: u32,
    pub depth: u32,
}

/// Trace the execution path from a symbol — BFS over call_graph + edges.
///
/// Returns a tree-like structure showing outgoing call chains.
pub fn trace_path(
    conn: &Connection,
    project_id: i64,
    symbol_name: &str,
    depth: u32,
    direction: TraceDirection,
) -> anyhow::Result<Vec<TraceNode>> {
    let mut visited: std::collections::HashSet<String> = std::collections::HashSet::new();
    let mut result = Vec::new();
    let mut current_level = vec![(symbol_name.to_string(), 0u32)];

    visited.insert(symbol_name.to_string());

    while !current_level.is_empty() {
        let mut next_level = Vec::new();

        for (sym, d) in &current_level {
            if *d >= depth {
                continue;
            }

            let neighbors = match direction {
                TraceDirection::Outgoing => find_callees_edges(conn, project_id, sym, 100)?,
                TraceDirection::Incoming => find_callers_edges(conn, project_id, sym, 100)?,
            };

            for neighbor in neighbors {
                let name = match direction {
                    TraceDirection::Outgoing => neighbor.target,
                    TraceDirection::Incoming => neighbor.source,
                };

                let is_new = visited.insert(name.clone());
                let node = TraceNode {
                    symbol: name.clone(),
                    file: neighbor.file,
                    line: neighbor.line,
                    kind: neighbor.kind,
                    depth: d + 1,
                };

                if is_new {
                    next_level.push((name.clone(), d + 1));
                }
                result.push(node);
            }
        }

        current_level = next_level;
    }

    result.sort_by(|a, b| a.depth.cmp(&b.depth).then_with(|| a.file.cmp(&b.file)));
    Ok(result)
}

/// Trace direction: follow outgoing calls or incoming callers.
#[derive(Debug, Clone, Copy)]
pub enum TraceDirection {
    Outgoing,
    Incoming,
}

/// A trace node with edge kind info.
#[derive(Debug, Clone, serde::Serialize)]
pub struct TraceNode {
    pub symbol: String,
    pub file: String,
    pub line: u32,
    pub kind: String,
    pub depth: u32,
}

/// Find outgoing edges from a symbol (uses edges table, falls back to call_graph).
fn find_callees_edges(
    conn: &Connection,
    project_id: i64,
    symbol_name: &str,
    limit: usize,
) -> anyhow::Result<Vec<EdgeRow>> {
    let pattern = format!("%{symbol_name}%");

    // Try edges table first (has typed relationships)
    let mut stmt = conn.prepare(
        "SELECT source, kind, target, file, line
         FROM edges
         WHERE source LIKE ?1 AND project_id = ?2
         LIMIT ?3",
    )?;

    let rows: Vec<EdgeRow> = stmt
        .query_map(
            rusqlite::params![pattern, project_id, limit as i64],
            |row| {
                Ok(EdgeRow {
                    source: row.get(0)?,
                    kind: row.get(1)?,
                    target: row.get(2)?,
                    file: row.get(3)?,
                    line: row.get::<_, i64>(4)? as u32,
                })
            },
        )?
        .filter_map(|r| r.ok())
        .collect();

    if !rows.is_empty() {
        return Ok(rows);
    }

    // Fallback to call_graph
    let mut stmt = conn.prepare(
        "SELECT caller, 'CALLS', callee, file, line
         FROM call_graph
         WHERE caller LIKE ?1 AND project_id = ?2
         LIMIT ?3",
    )?;

    let rows: Vec<EdgeRow> = stmt
        .query_map(
            rusqlite::params![pattern, project_id, limit as i64],
            |row| {
                Ok(EdgeRow {
                    source: row.get(0)?,
                    kind: row.get(1)?,
                    target: row.get(2)?,
                    file: row.get(3)?,
                    line: row.get::<_, i64>(4)? as u32,
                })
            },
        )?
        .filter_map(|r| r.ok())
        .collect();

    Ok(rows)
}

/// Find incoming edges to a symbol (uses edges table, falls back to call_graph).
fn find_callers_edges(
    conn: &Connection,
    project_id: i64,
    symbol_name: &str,
    limit: usize,
) -> anyhow::Result<Vec<EdgeRow>> {
    let pattern = format!("%{symbol_name}%");

    let mut stmt = conn.prepare(
        "SELECT source, kind, target, file, line
         FROM edges
         WHERE target LIKE ?1 AND project_id = ?2
         LIMIT ?3",
    )?;

    let rows: Vec<EdgeRow> = stmt
        .query_map(
            rusqlite::params![pattern, project_id, limit as i64],
            |row| {
                Ok(EdgeRow {
                    source: row.get(0)?,
                    kind: row.get(1)?,
                    target: row.get(2)?,
                    file: row.get(3)?,
                    line: row.get::<_, i64>(4)? as u32,
                })
            },
        )?
        .filter_map(|r| r.ok())
        .collect();

    if !rows.is_empty() {
        return Ok(rows);
    }

    let mut stmt = conn.prepare(
        "SELECT caller, 'CALLS', callee, file, line
         FROM call_graph
         WHERE callee LIKE ?1 AND project_id = ?2
         LIMIT ?3",
    )?;

    let rows: Vec<EdgeRow> = stmt
        .query_map(
            rusqlite::params![pattern, project_id, limit as i64],
            |row| {
                Ok(EdgeRow {
                    source: row.get(0)?,
                    kind: row.get(1)?,
                    target: row.get(2)?,
                    file: row.get(3)?,
                    line: row.get::<_, i64>(4)? as u32,
                })
            },
        )?
        .filter_map(|r| r.ok())
        .collect();

    Ok(rows)
}

/// Raw edge row from the database.
struct EdgeRow {
    source: String,
    kind: String,
    target: String,
    file: String,
    line: u32,
}

/// Architecture overview: module statistics and edge density.
pub fn arch_overview(conn: &Connection, project_id: i64) -> anyhow::Result<ArchOverview> {
    // Module = directory component of file path (e.g., "src/index" from "src/index/mod.rs")
    let mut stmt = conn.prepare(
        "SELECT
            CASE
                WHEN instr(file, '/') > 0 THEN substr(file, 1, instr(file, '/') - 1)
                ELSE file
            END AS module,
            COUNT(*) AS symbol_count,
            COUNT(DISTINCT kind) AS edge_types
         FROM symbols
         WHERE project_id = ?1
         GROUP BY module
         ORDER BY symbol_count DESC",
    )?;

    let modules: Vec<ModuleInfo> = stmt
        .query_map(rusqlite::params![project_id], |row| {
            Ok(ModuleInfo {
                name: row.get(0)?,
                symbol_count: row.get::<_, i64>(1)? as usize,
                edge_types: row.get::<_, i64>(2)? as usize,
            })
        })?
        .filter_map(|r| r.ok())
        .collect();

    // Edge type distribution
    let edge_counts = get_edge_kind_counts(conn, project_id);

    Ok(ArchOverview {
        modules,
        edge_counts,
    })
}

/// Count edges by kind.
fn get_edge_kind_counts(conn: &Connection, project_id: i64) -> Vec<(String, i64)> {
    let mut counts = Vec::new();

    if let Ok(mut s) = conn.prepare(
        "SELECT kind, COUNT(*) FROM edges WHERE project_id = ?1 GROUP BY kind ORDER BY COUNT(*) DESC",
    ) {
        if let Ok(rows) = s.query_map(rusqlite::params![project_id], |row| {
            Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?))
        }) {
            counts.extend(rows.flatten());
        }
    }

    if counts.is_empty() {
        if let Ok(mut s) =
            conn.prepare("SELECT 'CALLS', COUNT(*) FROM call_graph WHERE project_id = ?1")
        {
            if let Ok(rows) = s.query_map(rusqlite::params![project_id], |row| {
                Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?))
            }) {
                counts.extend(rows.flatten());
            }
        }
    }

    counts
}

/// Architecture overview result.
#[derive(Debug, Clone, serde::Serialize)]
pub struct ArchOverview {
    pub modules: Vec<ModuleInfo>,
    pub edge_counts: Vec<(String, i64)>,
}

/// Module-level statistics.
#[derive(Debug, Clone, serde::Serialize)]
pub struct ModuleInfo {
    pub name: String,
    pub symbol_count: usize,
    pub edge_types: usize,
}
#[cfg(test)]
mod tests {
    use super::*;

    fn mem_conn() -> Connection {
        let conn = Connection::open_in_memory().unwrap();
        conn.execute_batch("PRAGMA foreign_keys=ON;").unwrap();
        super::super::schema::run_migrations(&conn).unwrap();
        conn
    }

    /// Create a test project and return its project_id.
    fn test_project(conn: &Connection) -> i64 {
        super::super::schema::get_or_create_project(conn, "/tmp/test-project").unwrap()
    }

    #[test]
    fn test_store_and_find_callers() {
        let conn = mem_conn();
        let project_id = test_project(&conn);

        let edges = vec![
            CallEdge {
                caller: "main".to_string(),
                callee: "authenticate".to_string(),
                file: "main.rs".to_string(),
                line: 10,
            },
            CallEdge {
                caller: "handler".to_string(),
                callee: "authenticate".to_string(),
                file: "handler.rs".to_string(),
                line: 25,
            },
        ];
        store_edges(&conn, &edges, project_id).unwrap();

        let callers = find_callers(&conn, project_id, "authenticate", 10).unwrap();
        assert_eq!(callers.len(), 2);
        let names: Vec<&str> = callers.iter().map(|c| c.caller.as_str()).collect();
        assert!(names.contains(&"main"));
        assert!(names.contains(&"handler"));
    }

    #[test]
    fn test_find_callers_cross_project() {
        let conn = mem_conn();
        let project_a =
            super::super::schema::get_or_create_project(&conn, "/tmp/project-a").unwrap();
        let project_b =
            super::super::schema::get_or_create_project(&conn, "/tmp/project-b").unwrap();

        // Store edges in project A
        store_edges(
            &conn,
            &[CallEdge {
                caller: "handler_a".to_string(),
                callee: "shared_util".to_string(),
                file: "handler.rs".to_string(),
                line: 5,
            }],
            project_a,
        )
        .unwrap();

        // Store edges in project B
        store_edges(
            &conn,
            &[CallEdge {
                caller: "handler_b".to_string(),
                callee: "shared_util".to_string(),
                file: "lib.rs".to_string(),
                line: 10,
            }],
            project_b,
        )
        .unwrap();

        // Scoped to project A: only finds handler_a
        let scoped = find_callers(&conn, project_a, "shared_util", 10).unwrap();
        assert_eq!(scoped.len(), 1);
        assert_eq!(scoped[0].caller, "handler_a");

        // Scoped to project B: only finds handler_b
        let scoped_b = find_callers(&conn, project_b, "shared_util", 10).unwrap();
        assert_eq!(scoped_b.len(), 1);
        assert_eq!(scoped_b[0].caller, "handler_b");

        // Cross-project: finds BOTH
        let cross = find_callers_cross_project(&conn, "shared_util", 10).unwrap();
        assert_eq!(cross.len(), 2);
        let roots: Vec<&str> = cross.iter().map(|c| c.project_root.as_str()).collect();
        assert!(roots.contains(&"/tmp/project-a"));
        assert!(roots.contains(&"/tmp/project-b"));
    }

    #[test]
    fn test_find_callees() {
        let conn = mem_conn();
        let project_id = test_project(&conn);

        let edges = vec![
            CallEdge {
                caller: "main".to_string(),
                callee: "init".to_string(),
                file: "main.rs".to_string(),
                line: 5,
            },
            CallEdge {
                caller: "main".to_string(),
                callee: "run".to_string(),
                file: "main.rs".to_string(),
                line: 10,
            },
        ];
        store_edges(&conn, &edges, project_id).unwrap();

        let callees = find_callees(&conn, project_id, "main", 10).unwrap();
        assert_eq!(callees.len(), 2);
    }

    #[test]
    fn test_clear_edges_for_file() {
        let conn = mem_conn();
        let project_id = test_project(&conn);
        store_edges(
            &conn,
            &[CallEdge {
                caller: "a".to_string(),
                callee: "b".to_string(),
                file: "test.rs".to_string(),
                line: 1,
            }],
            project_id,
        )
        .unwrap();

        clear_edges_for_file(&conn, "test.rs", project_id).unwrap();
        let callers = find_callers(&conn, project_id, "b", 10).unwrap();
        assert!(callers.is_empty());
    }

    #[test]
    fn test_impact_analysis_depth() {
        let conn = mem_conn();
        let project_id = test_project(&conn);
        // a → b → c
        // If c changes, impact should find b (depth 1) and a (depth 2)
        let edges = vec![
            CallEdge {
                caller: "b".to_string(),
                callee: "c".to_string(),
                file: "b.rs".to_string(),
                line: 1,
            },
            CallEdge {
                caller: "a".to_string(),
                callee: "b".to_string(),
                file: "a.rs".to_string(),
                line: 1,
            },
        ];
        store_edges(&conn, &edges, project_id).unwrap();

        let impact = impact_analysis(&conn, project_id, "c", 3).unwrap();
        // Should find b at depth 1, a at depth 2
        assert!(impact.iter().any(|n| n.symbol == "b" && n.depth == 1));
        assert!(impact.iter().any(|n| n.symbol == "a" && n.depth == 2));
    }

    #[test]
    fn test_trace_path_outgoing() {
        let conn = mem_conn();
        let pid = test_project(&conn);

        // a→b→c, a→d
        for (caller, callee) in [("a", "b"), ("b", "c"), ("a", "d")] {
            store_edges(
                &conn,
                &[CallEdge {
                    caller: caller.into(),
                    callee: callee.into(),
                    file: "x.rs".into(),
                    line: 1,
                }],
                pid,
            )
            .unwrap();
        }

        let nodes = trace_path(&conn, pid, "a", 2, TraceDirection::Outgoing).unwrap();
        let syms: Vec<&str> = nodes.iter().map(|n| n.symbol.as_str()).collect();
        assert!(syms.contains(&"b"));
        assert!(syms.contains(&"c"));
        assert!(syms.contains(&"d"));
    }

    #[test]
    fn test_trace_path_incoming() {
        let conn = mem_conn();
        let pid = test_project(&conn);

        for (caller, callee) in [("a", "c"), ("b", "c")] {
            store_edges(
                &conn,
                &[CallEdge {
                    caller: caller.into(),
                    callee: callee.into(),
                    file: "y.rs".into(),
                    line: 1,
                }],
                pid,
            )
            .unwrap();
        }

        let nodes = trace_path(&conn, pid, "c", 1, TraceDirection::Incoming).unwrap();
        let syms: Vec<&str> = nodes.iter().map(|n| n.symbol.as_str()).collect();
        assert!(syms.contains(&"a"));
        assert!(syms.contains(&"b"));
    }

    #[test]
    fn test_arch_overview() {
        let conn = mem_conn();
        let pid = test_project(&conn);

        store_edges(
            &conn,
            &[CallEdge {
                caller: "main".into(),
                callee: "run".into(),
                file: "src/main.rs".into(),
                line: 1,
            }],
            pid,
        )
        .unwrap();

        // Insert a symbol so arch has data
        conn.execute(
            "INSERT INTO symbols (name, kind, file, line, project_id) VALUES (?1, ?2, ?3, ?4, ?5)",
            rusqlite::params!["main", "function", "src/main.rs", 1i64, pid],
        )
        .unwrap();

        let overview = arch_overview(&conn, pid).unwrap();
        assert_eq!(overview.modules.len(), 1);
        assert_eq!(overview.modules[0].name, "src");
    }
}