loctree 0.8.16

Structural code intelligence for AI agents. Scan once, query everything.
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
//! Snapshot comparison engine for temporal analysis
//!
//! This module compares loctree snapshots between different commits,
//! providing semantic analysis of how the codebase structure changed.

use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::path::PathBuf;

use crate::git::{ChangeStatus, ChangedFile, CommitInfo};
use crate::snapshot::Snapshot;

/// Result of comparing two snapshots
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SnapshotDiff {
    /// Information about the source commit
    pub from_commit: Option<CommitInfo>,
    /// Information about the target commit (None = working tree)
    pub to_commit: Option<CommitInfo>,
    /// Files that changed between snapshots
    pub files: FilesDiff,
    /// Changes in the import/export graph
    pub graph: GraphDiff,
    /// Changes in exported symbols
    pub exports: ExportsDiff,
    /// Impact analysis
    pub impact: ImpactAnalysis,
}

/// Diff of files between snapshots
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct FilesDiff {
    /// Files added in the new snapshot
    pub added: Vec<PathBuf>,
    /// Files removed from the old snapshot
    pub removed: Vec<PathBuf>,
    /// Files modified between snapshots
    pub modified: Vec<PathBuf>,
    /// Files renamed (old_path -> new_path)
    pub renamed: Vec<(PathBuf, PathBuf)>,
}

impl FilesDiff {
    pub fn from_changed_files(changes: &[ChangedFile]) -> Self {
        let mut diff = FilesDiff::default();

        for change in changes {
            match change.status {
                ChangeStatus::Added => {
                    if let Some(path) = &change.new_path {
                        diff.added.push(path.clone());
                    }
                }
                ChangeStatus::Deleted => {
                    if let Some(path) = &change.old_path {
                        diff.removed.push(path.clone());
                    }
                }
                ChangeStatus::Modified => {
                    if let Some(path) = &change.new_path {
                        diff.modified.push(path.clone());
                    }
                }
                ChangeStatus::Renamed | ChangeStatus::Copied => {
                    if let (Some(old), Some(new)) = (&change.old_path, &change.new_path) {
                        diff.renamed.push((old.clone(), new.clone()));
                    }
                }
            }
        }

        diff
    }

    /// Total number of changes
    pub fn total_changes(&self) -> usize {
        self.added.len() + self.removed.len() + self.modified.len() + self.renamed.len()
    }
}

/// Edge in the import graph (for diff operations)
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct DiffEdge {
    /// Source file (importer)
    pub from: PathBuf,
    /// Target file (imported)
    pub to: PathBuf,
    /// Imported symbols (if known)
    pub symbols: Vec<String>,
}

/// Diff of the import graph
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct GraphDiff {
    /// New import edges added
    pub edges_added: Vec<DiffEdge>,
    /// Import edges removed
    pub edges_removed: Vec<DiffEdge>,
}

/// An exported symbol
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ExportedSymbol {
    /// File containing the export
    pub file: PathBuf,
    /// Symbol name
    pub name: String,
    /// Symbol kind (function, class, const, etc.)
    pub kind: String,
}

/// Diff of exports
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct ExportsDiff {
    /// New exports added
    pub added: Vec<ExportedSymbol>,
    /// Exports removed
    pub removed: Vec<ExportedSymbol>,
}

/// Impact analysis of the changes
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct ImpactAnalysis {
    /// Number of files affected by changes
    pub affected_files: usize,
    /// Files that consume changed exports
    pub affected_consumers: Vec<PathBuf>,
    /// Risk score (0.0 - 1.0)
    pub risk_score: f64,
    /// Summary of the impact
    pub summary: String,
}

impl SnapshotDiff {
    /// Compare two snapshots and produce a diff
    pub fn compare(
        from_snapshot: &Snapshot,
        to_snapshot: &Snapshot,
        from_commit: Option<CommitInfo>,
        to_commit: Option<CommitInfo>,
        changed_files: &[ChangedFile],
    ) -> Self {
        let files = FilesDiff::from_changed_files(changed_files);
        let graph = Self::compare_graphs(from_snapshot, to_snapshot);
        let exports = Self::compare_exports(from_snapshot, to_snapshot);
        let impact = Self::analyze_impact(&files, &graph, &exports, to_snapshot);

        Self {
            from_commit,
            to_commit,
            files,
            graph,
            exports,
            impact,
        }
    }

    /// Compare import graphs between snapshots
    fn compare_graphs(from: &Snapshot, to: &Snapshot) -> GraphDiff {
        let mut diff = GraphDiff::default();

        // Build edge sets for comparison
        let from_edges = Self::extract_edges(from);
        let to_edges = Self::extract_edges(to);

        // Find added edges
        for edge in &to_edges {
            if !from_edges.contains(edge) {
                diff.edges_added.push(edge.clone());
            }
        }

        // Find removed edges
        for edge in &from_edges {
            if !to_edges.contains(edge) {
                diff.edges_removed.push(edge.clone());
            }
        }

        diff
    }

    /// Extract edges from snapshot
    fn extract_edges(snapshot: &Snapshot) -> HashSet<DiffEdge> {
        let mut edges = HashSet::new();

        // Use snapshot.edges which contains GraphEdge structs
        for edge in &snapshot.edges {
            // Parse symbols from label (label format: "symbol1, symbol2" or empty)
            let symbols: Vec<String> = if edge.label.is_empty() {
                Vec::new()
            } else {
                edge.label.split(", ").map(|s| s.to_string()).collect()
            };

            edges.insert(DiffEdge {
                from: PathBuf::from(&edge.from),
                to: PathBuf::from(&edge.to),
                symbols,
            });
        }

        edges
    }

    /// Compare exports between snapshots
    fn compare_exports(from: &Snapshot, to: &Snapshot) -> ExportsDiff {
        let mut diff = ExportsDiff::default();

        let from_exports = Self::extract_exports(from);
        let to_exports = Self::extract_exports(to);

        for export in &to_exports {
            if !from_exports.contains(export) {
                diff.added.push(export.clone());
            }
        }

        for export in &from_exports {
            if !to_exports.contains(export) {
                diff.removed.push(export.clone());
            }
        }

        diff
    }

    /// Extract exports from snapshot
    fn extract_exports(snapshot: &Snapshot) -> HashSet<ExportedSymbol> {
        let mut exports = HashSet::new();

        // Use snapshot.files which is Vec<FileAnalysis>
        for file_info in &snapshot.files {
            let file_path = PathBuf::from(&file_info.path);

            for export in &file_info.exports {
                exports.insert(ExportedSymbol {
                    file: file_path.clone(),
                    name: export.name.clone(),
                    kind: export.kind.clone(),
                });
            }
        }

        exports
    }

    /// Analyze the impact of changes
    fn analyze_impact(
        files: &FilesDiff,
        graph: &GraphDiff,
        exports: &ExportsDiff,
        to_snapshot: &Snapshot,
    ) -> ImpactAnalysis {
        let affected_files = files.total_changes();

        // Find consumers of changed files
        let changed_paths: HashSet<String> = files
            .modified
            .iter()
            .chain(files.removed.iter())
            .map(|p| p.to_string_lossy().to_string())
            .collect();

        let mut affected_consumers = Vec::new();
        for file_info in &to_snapshot.files {
            for import in &file_info.imports {
                if let Some(resolved) = &import.resolved_path
                    && changed_paths.contains(resolved)
                {
                    affected_consumers.push(PathBuf::from(&file_info.path));
                    break;
                }
            }
        }

        // Calculate risk score
        let risk_score = Self::calculate_risk_score(files, graph, exports);

        // Generate summary
        let summary = Self::generate_summary(files, graph, exports, &affected_consumers);

        ImpactAnalysis {
            affected_files,
            affected_consumers,
            risk_score,
            summary,
        }
    }

    /// Calculate risk score (0.0 - 1.0)
    fn calculate_risk_score(files: &FilesDiff, graph: &GraphDiff, exports: &ExportsDiff) -> f64 {
        let mut score = 0.0;

        // File changes
        score += files.removed.len() as f64 * 0.1;
        score += files.modified.len() as f64 * 0.05;

        // Graph changes
        score += graph.edges_removed.len() as f64 * 0.05;

        // Export changes
        score += exports.removed.len() as f64 * 0.15;

        // Clamp to 0.0 - 1.0
        score.min(1.0)
    }

    /// Generate human-readable summary
    fn generate_summary(
        files: &FilesDiff,
        graph: &GraphDiff,
        exports: &ExportsDiff,
        affected_consumers: &[PathBuf],
    ) -> String {
        let mut parts = Vec::new();

        if !files.added.is_empty() {
            parts.push(format!("{} files added", files.added.len()));
        }
        if !files.removed.is_empty() {
            parts.push(format!("{} files removed", files.removed.len()));
        }
        if !files.modified.is_empty() {
            parts.push(format!("{} files modified", files.modified.len()));
        }
        if !graph.edges_added.is_empty() {
            parts.push(format!("{} imports added", graph.edges_added.len()));
        }
        if !graph.edges_removed.is_empty() {
            parts.push(format!("{} imports removed", graph.edges_removed.len()));
        }
        if !exports.removed.is_empty() {
            parts.push(format!("{} exports removed", exports.removed.len()));
        }
        if !affected_consumers.is_empty() {
            parts.push(format!("{} consumers affected", affected_consumers.len()));
        }

        if parts.is_empty() {
            "No significant changes".to_string()
        } else {
            parts.join(", ")
        }
    }

    /// Convert to JSON value
    pub fn to_json(&self) -> serde_json::Value {
        serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::snapshot::GraphEdge;
    use crate::types::{ExportSymbol, FileAnalysis};

    #[test]
    fn test_files_diff_from_changed_files() {
        let changes = vec![
            ChangedFile {
                old_path: None,
                new_path: Some(PathBuf::from("new.ts")),
                status: ChangeStatus::Added,
            },
            ChangedFile {
                old_path: Some(PathBuf::from("old.ts")),
                new_path: None,
                status: ChangeStatus::Deleted,
            },
            ChangedFile {
                old_path: Some(PathBuf::from("mod.ts")),
                new_path: Some(PathBuf::from("mod.ts")),
                status: ChangeStatus::Modified,
            },
        ];

        let diff = FilesDiff::from_changed_files(&changes);

        assert_eq!(diff.added, vec![PathBuf::from("new.ts")]);
        assert_eq!(diff.removed, vec![PathBuf::from("old.ts")]);
        assert_eq!(diff.modified, vec![PathBuf::from("mod.ts")]);
        assert_eq!(diff.total_changes(), 3);
    }

    #[test]
    fn test_files_diff_renamed() {
        let changes = vec![ChangedFile {
            old_path: Some(PathBuf::from("old.ts")),
            new_path: Some(PathBuf::from("new.ts")),
            status: ChangeStatus::Renamed,
        }];

        let diff = FilesDiff::from_changed_files(&changes);
        assert_eq!(diff.renamed.len(), 1);
        assert_eq!(diff.renamed[0].0, PathBuf::from("old.ts"));
        assert_eq!(diff.renamed[0].1, PathBuf::from("new.ts"));
    }

    #[test]
    fn test_files_diff_copied() {
        let changes = vec![ChangedFile {
            old_path: Some(PathBuf::from("src.ts")),
            new_path: Some(PathBuf::from("copy.ts")),
            status: ChangeStatus::Copied,
        }];

        let diff = FilesDiff::from_changed_files(&changes);
        assert_eq!(diff.renamed.len(), 1); // Copied uses same handling as renamed
    }

    #[test]
    fn test_files_diff_empty() {
        let changes: Vec<ChangedFile> = vec![];
        let diff = FilesDiff::from_changed_files(&changes);
        assert_eq!(diff.total_changes(), 0);
    }

    #[test]
    fn test_risk_score_clamped() {
        let files = FilesDiff {
            removed: (0..100)
                .map(|i| PathBuf::from(format!("file{}.ts", i)))
                .collect(),
            ..Default::default()
        };
        let graph = GraphDiff::default();
        let exports = ExportsDiff::default();

        let score = SnapshotDiff::calculate_risk_score(&files, &graph, &exports);
        assert!(score <= 1.0);
    }

    #[test]
    fn test_risk_score_components() {
        // Test modified files contribution
        let files = FilesDiff {
            modified: vec![PathBuf::from("a.ts")],
            ..Default::default()
        };
        let score1 = SnapshotDiff::calculate_risk_score(
            &files,
            &GraphDiff::default(),
            &ExportsDiff::default(),
        );
        assert!(score1 > 0.0);

        // Test removed exports contribution
        let exports = ExportsDiff {
            removed: vec![ExportedSymbol {
                file: PathBuf::from("a.ts"),
                name: "foo".to_string(),
                kind: "function".to_string(),
            }],
            ..Default::default()
        };
        let score2 = SnapshotDiff::calculate_risk_score(
            &FilesDiff::default(),
            &GraphDiff::default(),
            &exports,
        );
        assert!(score2 > 0.0);
    }

    #[test]
    fn test_generate_summary_empty() {
        let summary = SnapshotDiff::generate_summary(
            &FilesDiff::default(),
            &GraphDiff::default(),
            &ExportsDiff::default(),
            &[],
        );
        assert_eq!(summary, "No significant changes");
    }

    #[test]
    fn test_generate_summary_with_changes() {
        let files = FilesDiff {
            added: vec![PathBuf::from("new.ts")],
            removed: vec![PathBuf::from("old.ts")],
            modified: vec![PathBuf::from("mod.ts")],
            ..Default::default()
        };
        let summary = SnapshotDiff::generate_summary(
            &files,
            &GraphDiff::default(),
            &ExportsDiff::default(),
            &[],
        );
        assert!(summary.contains("1 files added"));
        assert!(summary.contains("1 files removed"));
        assert!(summary.contains("1 files modified"));
    }

    #[test]
    fn test_generate_summary_with_graph_changes() {
        let graph = GraphDiff {
            edges_added: vec![DiffEdge {
                from: PathBuf::from("a.ts"),
                to: PathBuf::from("b.ts"),
                symbols: vec![],
            }],
            edges_removed: vec![DiffEdge {
                from: PathBuf::from("c.ts"),
                to: PathBuf::from("d.ts"),
                symbols: vec![],
            }],
        };
        let summary = SnapshotDiff::generate_summary(
            &FilesDiff::default(),
            &graph,
            &ExportsDiff::default(),
            &[],
        );
        assert!(summary.contains("1 imports added"));
        assert!(summary.contains("1 imports removed"));
    }

    #[test]
    fn test_generate_summary_with_consumers() {
        let summary = SnapshotDiff::generate_summary(
            &FilesDiff::default(),
            &GraphDiff::default(),
            &ExportsDiff::default(),
            &[PathBuf::from("consumer.ts")],
        );
        assert!(summary.contains("1 consumers affected"));
    }

    fn mock_metadata() -> crate::snapshot::SnapshotMetadata {
        crate::snapshot::SnapshotMetadata {
            schema_version: crate::snapshot::SNAPSHOT_SCHEMA_VERSION.to_string(),
            generated_at: "2025-01-01T00:00:00Z".to_string(),
            roots: vec![".".to_string()],
            languages: std::collections::HashSet::new(),
            file_count: 0,
            total_loc: 0,
            scan_duration_ms: 0,
            resolver_config: None,
            manifest_summary: Vec::new(),
            entrypoints: Vec::new(),
            entrypoint_drift: crate::snapshot::EntrypointDriftSummary::default(),
            git_repo: None,
            git_branch: None,
            git_commit: None,
            git_scan_id: None,
        }
    }

    fn mock_snapshot_with_edges(edges: Vec<GraphEdge>) -> Snapshot {
        Snapshot {
            metadata: mock_metadata(),
            files: vec![],
            edges,
            export_index: std::collections::HashMap::new(),
            command_bridges: vec![],
            event_bridges: vec![],
            barrels: vec![],
        }
    }

    fn mock_snapshot_with_files(files: Vec<FileAnalysis>) -> Snapshot {
        Snapshot {
            metadata: mock_metadata(),
            files,
            edges: vec![],
            export_index: std::collections::HashMap::new(),
            command_bridges: vec![],
            event_bridges: vec![],
            barrels: vec![],
        }
    }

    #[test]
    fn test_extract_edges_empty() {
        let snapshot = mock_snapshot_with_edges(vec![]);
        let edges = SnapshotDiff::extract_edges(&snapshot);
        assert!(edges.is_empty());
    }

    #[test]
    fn test_extract_edges() {
        let snapshot = mock_snapshot_with_edges(vec![GraphEdge {
            from: "a.ts".to_string(),
            to: "b.ts".to_string(),
            label: "foo, bar".to_string(),
        }]);
        let edges = SnapshotDiff::extract_edges(&snapshot);
        assert_eq!(edges.len(), 1);
        let edge = edges.iter().next().unwrap();
        assert_eq!(edge.from, PathBuf::from("a.ts"));
        assert_eq!(edge.to, PathBuf::from("b.ts"));
        assert_eq!(edge.symbols, vec!["foo", "bar"]);
    }

    #[test]
    fn test_extract_exports() {
        let file = FileAnalysis {
            path: "utils.ts".to_string(),
            exports: vec![ExportSymbol::new(
                "helper".to_string(),
                "function",
                "named",
                Some(10),
            )],
            ..Default::default()
        };

        let snapshot = mock_snapshot_with_files(vec![file]);
        let exports = SnapshotDiff::extract_exports(&snapshot);
        assert_eq!(exports.len(), 1);
    }

    #[test]
    fn test_diff_edge_equality() {
        let edge1 = DiffEdge {
            from: PathBuf::from("a.ts"),
            to: PathBuf::from("b.ts"),
            symbols: vec!["foo".to_string()],
        };
        let edge2 = DiffEdge {
            from: PathBuf::from("a.ts"),
            to: PathBuf::from("b.ts"),
            symbols: vec!["foo".to_string()],
        };
        assert_eq!(edge1, edge2);

        let mut set = HashSet::new();
        set.insert(edge1.clone());
        assert!(set.contains(&edge2));
    }

    #[test]
    fn test_snapshot_diff_to_json() {
        let diff = SnapshotDiff {
            from_commit: None,
            to_commit: None,
            files: FilesDiff::default(),
            graph: GraphDiff::default(),
            exports: ExportsDiff::default(),
            impact: ImpactAnalysis {
                affected_files: 0,
                affected_consumers: vec![],
                risk_score: 0.0,
                summary: "No changes".to_string(),
            },
        };
        let json = diff.to_json();
        assert!(!json.is_null());
    }

    #[test]
    fn test_compare_graphs_added_edge() {
        let from = mock_snapshot_with_edges(vec![]);
        let to = mock_snapshot_with_edges(vec![GraphEdge {
            from: "a.ts".to_string(),
            to: "b.ts".to_string(),
            label: "".to_string(),
        }]);
        let diff = SnapshotDiff::compare_graphs(&from, &to);
        assert_eq!(diff.edges_added.len(), 1);
        assert!(diff.edges_removed.is_empty());
    }

    #[test]
    fn test_compare_graphs_removed_edge() {
        let from = mock_snapshot_with_edges(vec![GraphEdge {
            from: "a.ts".to_string(),
            to: "b.ts".to_string(),
            label: "".to_string(),
        }]);
        let to = mock_snapshot_with_edges(vec![]);
        let diff = SnapshotDiff::compare_graphs(&from, &to);
        assert!(diff.edges_added.is_empty());
        assert_eq!(diff.edges_removed.len(), 1);
    }

    #[test]
    fn test_compare_exports_added() {
        let from = mock_snapshot_with_files(vec![]);

        let file = FileAnalysis {
            path: "utils.ts".to_string(),
            exports: vec![ExportSymbol::new(
                "helper".to_string(),
                "function",
                "named",
                Some(10),
            )],
            ..Default::default()
        };
        let to = mock_snapshot_with_files(vec![file]);

        let diff = SnapshotDiff::compare_exports(&from, &to);
        assert_eq!(diff.added.len(), 1);
        assert!(diff.removed.is_empty());
    }

    #[test]
    fn test_compare_exports_removed() {
        let file = FileAnalysis {
            path: "utils.ts".to_string(),
            exports: vec![ExportSymbol::new(
                "helper".to_string(),
                "function",
                "named",
                Some(10),
            )],
            ..Default::default()
        };
        let from = mock_snapshot_with_files(vec![file]);

        let to = mock_snapshot_with_files(vec![]);
        let diff = SnapshotDiff::compare_exports(&from, &to);
        assert!(diff.added.is_empty());
        assert_eq!(diff.removed.len(), 1);
    }

    #[test]
    fn test_generate_summary_with_exports_removed() {
        let exports = ExportsDiff {
            removed: vec![ExportedSymbol {
                file: PathBuf::from("a.ts"),
                name: "foo".to_string(),
                kind: "function".to_string(),
            }],
            ..Default::default()
        };
        let summary = SnapshotDiff::generate_summary(
            &FilesDiff::default(),
            &GraphDiff::default(),
            &exports,
            &[],
        );
        assert!(summary.contains("1 exports removed"));
    }

    #[test]
    fn test_full_compare() {
        let from = mock_snapshot_with_edges(vec![]);
        let to = mock_snapshot_with_edges(vec![GraphEdge {
            from: "a.ts".to_string(),
            to: "b.ts".to_string(),
            label: "foo".to_string(),
        }]);
        let changed_files = vec![ChangedFile {
            old_path: None,
            new_path: Some(PathBuf::from("a.ts")),
            status: ChangeStatus::Added,
        }];

        let diff = SnapshotDiff::compare(&from, &to, None, None, &changed_files);

        assert_eq!(diff.files.added.len(), 1);
        assert_eq!(diff.graph.edges_added.len(), 1);
        assert!(!diff.impact.summary.is_empty());
    }

    #[test]
    fn test_extract_edges_empty_label() {
        let snapshot = mock_snapshot_with_edges(vec![GraphEdge {
            from: "a.ts".to_string(),
            to: "b.ts".to_string(),
            label: "".to_string(),
        }]);
        let edges = SnapshotDiff::extract_edges(&snapshot);
        let edge = edges.iter().next().unwrap();
        assert!(edge.symbols.is_empty());
    }

    #[test]
    fn test_exported_symbol_equality() {
        let sym1 = ExportedSymbol {
            file: PathBuf::from("a.ts"),
            name: "foo".to_string(),
            kind: "function".to_string(),
        };
        let sym2 = ExportedSymbol {
            file: PathBuf::from("a.ts"),
            name: "foo".to_string(),
            kind: "function".to_string(),
        };
        assert_eq!(sym1, sym2);

        let mut set = HashSet::new();
        set.insert(sym1.clone());
        assert!(set.contains(&sym2));
    }
}