sbom-tools 0.1.18

Semantic SBOM diff and analysis tool
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
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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
//! Graph-aware dependency diffing module.
//!
//! This module provides functionality to detect structural changes in the
//! dependency graph between two SBOMs, going beyond simple component-level
//! comparisons to identify:
//! - Dependencies being added or removed
//! - Dependencies being reparented (moved from one parent to another)
//! - Depth changes (transitive becoming direct or vice versa)

use std::collections::{HashMap, HashSet, VecDeque};

use crate::model::{CanonicalId, DependencyScope, DependencyType, NormalizedSbom};

use super::result::{
    DependencyChangeType, DependencyGraphChange, GraphChangeImpact, GraphChangeSummary,
};

/// Sentinel depth for nodes unreachable from any root via BFS (e.g., pure cycles).
/// Distinct from real depths (1=root, 2=direct, 3+=transitive) so impact assessment
/// can handle them separately.
const CYCLIC_SENTINEL_DEPTH: u32 = u32::MAX;

/// Configuration for graph-aware diffing
#[derive(Debug, Clone)]
pub struct GraphDiffConfig {
    /// Whether to detect reparenting (computationally more expensive)
    pub detect_reparenting: bool,
    /// Whether to track depth changes
    pub detect_depth_changes: bool,
    /// Maximum depth to analyze (0 = unlimited)
    pub max_depth: u32,
    /// Relationship type filter — only include edges matching these types (empty = all)
    pub relation_filter: Vec<String>,
}

impl Default for GraphDiffConfig {
    fn default() -> Self {
        Self {
            detect_reparenting: true,
            detect_depth_changes: true,
            max_depth: 0,
            relation_filter: Vec::new(),
        }
    }
}

/// Edge attributes (relationship type and scope) for a dependency edge.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct EdgeAttrs {
    relationship: DependencyType,
    scope: Option<DependencyScope>,
}

/// Internal representation of dependency graph for diffing
struct DependencyGraph<'a> {
    /// Reference to the SBOM
    sbom: &'a NormalizedSbom,
    /// `parent_id` -> Vec<`child_id`>
    edges: HashMap<CanonicalId, Vec<CanonicalId>>,
    /// `child_id` -> Vec<`parent_id`> (reverse index)
    reverse_edges: HashMap<CanonicalId, Vec<CanonicalId>>,
    /// `(from_id, to_id)` -> edge attributes
    edge_attrs: HashMap<(CanonicalId, CanonicalId), EdgeAttrs>,
    /// `component_id` -> minimum depth from root (1 = direct)
    /// Uses minimum depth when multiple paths exist (diamond dependencies)
    depths: HashMap<CanonicalId, u32>,
    /// `component_id` -> has vulnerabilities
    vulnerable_components: HashSet<CanonicalId>,
}

impl<'a> DependencyGraph<'a> {
    fn from_sbom(sbom: &'a NormalizedSbom, config: &GraphDiffConfig) -> Self {
        let mut edges: HashMap<CanonicalId, Vec<CanonicalId>> = HashMap::new();
        let mut reverse_edges: HashMap<CanonicalId, Vec<CanonicalId>> = HashMap::new();
        let mut edge_attrs: HashMap<(CanonicalId, CanonicalId), EdgeAttrs> = HashMap::new();
        let mut vulnerable_components = HashSet::new();

        // Build edge maps from SBOM edges, applying relation filter if set
        for edge in &sbom.edges {
            if !config.relation_filter.is_empty()
                && !config
                    .relation_filter
                    .iter()
                    .any(|f| f.eq_ignore_ascii_case(&edge.relationship.to_string()))
            {
                continue; // Skip edges not matching the filter
            }

            edges
                .entry(edge.from.clone())
                .or_default()
                .push(edge.to.clone());

            reverse_edges
                .entry(edge.to.clone())
                .or_default()
                .push(edge.from.clone());

            edge_attrs.insert(
                (edge.from.clone(), edge.to.clone()),
                EdgeAttrs {
                    relationship: edge.relationship.clone(),
                    scope: edge.scope.clone(),
                },
            );
        }

        // Identify vulnerable components
        for (id, comp) in &sbom.components {
            if !comp.vulnerabilities.is_empty() {
                vulnerable_components.insert(id.clone());
            }
        }

        // Calculate depths via BFS from roots (respecting max_depth limit)
        let all_components: HashSet<_> = sbom.components.keys().cloned().collect();
        let depths =
            Self::calculate_depths(&edges, &reverse_edges, &all_components, config.max_depth);

        Self {
            sbom,
            edges,
            reverse_edges,
            edge_attrs,
            depths,
            vulnerable_components,
        }
    }

    /// Calculate minimum depths from roots via BFS.
    ///
    /// Uses minimum depth when multiple paths exist (diamond dependencies),
    /// which gives the most accurate "direct vs transitive" classification.
    /// Respects `max_depth` limit (0 = unlimited).
    fn calculate_depths(
        edges: &HashMap<CanonicalId, Vec<CanonicalId>>,
        reverse_edges: &HashMap<CanonicalId, Vec<CanonicalId>>,
        all_components: &HashSet<CanonicalId>,
        max_depth: u32,
    ) -> HashMap<CanonicalId, u32> {
        let mut depths = HashMap::new();

        // BFS to calculate minimum depths
        // We use a queue that may revisit nodes if a shorter path is found
        let mut queue: VecDeque<(CanonicalId, u32)> = all_components
            .iter()
            .filter(|id| reverse_edges.get(*id).is_none_or(std::vec::Vec::is_empty))
            .cloned()
            .map(|id| (id, 1))
            .collect();

        while let Some((id, depth)) = queue.pop_front() {
            // Check if we've found a shorter path to this node
            if let Some(&existing_depth) = depths.get(&id)
                && depth >= existing_depth
            {
                continue; // Already have a shorter or equal path
            }

            // Record this depth (it's either new or shorter than existing)
            depths.insert(id.clone(), depth);

            // Stop traversing if we've reached max_depth (0 = unlimited)
            if max_depth > 0 && depth >= max_depth {
                continue;
            }

            if let Some(children) = edges.get(&id) {
                for child_id in children {
                    let child_depth = depth + 1;
                    // Only queue if this might be a shorter path
                    let dominated = depths.get(child_id).is_some_and(|&d| d <= child_depth);
                    if !dominated {
                        queue.push_back((child_id.clone(), child_depth));
                    }
                }
            }
        }

        // Assign sentinel depth to unreachable/cyclic-only nodes so they
        // participate in impact assessment but are NOT confused with real roots.
        // u32::MAX means "unreachable from any root via BFS".
        for id in all_components {
            depths.entry(id.clone()).or_insert(CYCLIC_SENTINEL_DEPTH);
        }

        depths
    }

    fn get_parents(&self, component_id: &CanonicalId) -> Vec<CanonicalId> {
        self.reverse_edges
            .get(component_id)
            .cloned()
            .unwrap_or_default()
    }

    fn get_children(&self, component_id: &CanonicalId) -> Vec<CanonicalId> {
        self.edges.get(component_id).cloned().unwrap_or_default()
    }

    fn get_edge_attrs(&self, from: &CanonicalId, to: &CanonicalId) -> Option<&EdgeAttrs> {
        self.edge_attrs.get(&(from.clone(), to.clone()))
    }

    fn get_depth(&self, component_id: &CanonicalId) -> Option<u32> {
        self.depths.get(component_id).copied()
    }

    fn is_vulnerable(&self, component_id: &CanonicalId) -> bool {
        self.vulnerable_components.contains(component_id)
    }

    fn get_component_name(&self, component_id: &CanonicalId) -> String {
        self.sbom.components.get(component_id).map_or_else(
            || component_id.to_string(),
            |c| {
                c.version
                    .as_ref()
                    .map_or_else(|| c.name.clone(), |v| format!("{}@{}", c.name, v))
            },
        )
    }
}

/// Perform graph-aware diff between two SBOMs
#[allow(clippy::implicit_hasher)]
#[must_use]
pub fn diff_dependency_graph(
    old_sbom: &NormalizedSbom,
    new_sbom: &NormalizedSbom,
    component_matches: &HashMap<CanonicalId, Option<CanonicalId>>,
    config: &GraphDiffConfig,
) -> (Vec<DependencyGraphChange>, GraphChangeSummary) {
    let old_graph = DependencyGraph::from_sbom(old_sbom, config);
    let new_graph = DependencyGraph::from_sbom(new_sbom, config);

    let mut changes = Vec::new();

    // Iterate through matched components to find dependency changes
    for (old_id, new_id_option) in component_matches {
        if let Some(new_id) = new_id_option {
            let component_name = new_graph.get_component_name(new_id);

            // Get children in both graphs, mapping old children through component_matches
            // so we compare in the new-SBOM ID space.
            // Children not in the match map or matched to None (removed) are excluded —
            // they have no new-space representation and should not participate in comparison.
            let old_children_mapped: HashSet<CanonicalId> = old_graph
                .get_children(old_id)
                .into_iter()
                .filter_map(|old_child| {
                    component_matches
                        .get(&old_child)
                        .and_then(|opt| opt.clone())
                })
                .collect();
            let new_children: HashSet<_> = new_graph.get_children(new_id).into_iter().collect();

            // Build a reverse map from new-space child to old-space child for attr lookup
            let old_child_to_new: HashMap<CanonicalId, CanonicalId> = old_graph
                .get_children(old_id)
                .into_iter()
                .filter_map(|old_child| {
                    component_matches
                        .get(&old_child)
                        .and_then(|opt| opt.clone())
                        .map(|new_child_id| (new_child_id, old_child))
                })
                .collect();

            // Detect added dependencies
            for child_id in new_children.difference(&old_children_mapped) {
                let dep_name = new_graph.get_component_name(child_id);
                let impact = assess_impact_added(&new_graph, child_id);

                changes.push(DependencyGraphChange {
                    component_id: new_id.clone(),
                    component_name: component_name.clone(),
                    change: DependencyChangeType::DependencyAdded {
                        dependency_id: child_id.clone(),
                        dependency_name: dep_name,
                    },
                    impact,
                });
            }

            // Detect removed dependencies
            for child_id in old_children_mapped.difference(&new_children) {
                let dep_name = new_graph.get_component_name(child_id);

                changes.push(DependencyGraphChange {
                    component_id: new_id.clone(),
                    component_name: component_name.clone(),
                    change: DependencyChangeType::DependencyRemoved {
                        dependency_id: child_id.clone(),
                        dependency_name: dep_name,
                    },
                    impact: GraphChangeImpact::Low,
                });
            }

            // Detect relationship/scope changes for children present in both
            for child_id in old_children_mapped.intersection(&new_children) {
                // Look up old edge attrs: old_id → old_child_id (in old-space)
                let old_attrs = old_child_to_new
                    .get(child_id)
                    .and_then(|old_child_id| old_graph.get_edge_attrs(old_id, old_child_id));
                let new_attrs = new_graph.get_edge_attrs(new_id, child_id);

                if let (Some(old_a), Some(new_a)) = (old_attrs, new_attrs)
                    && old_a != new_a
                {
                    let dep_name = new_graph.get_component_name(child_id);
                    changes.push(DependencyGraphChange {
                        component_id: new_id.clone(),
                        component_name: component_name.clone(),
                        change: DependencyChangeType::RelationshipChanged {
                            dependency_id: child_id.clone(),
                            dependency_name: dep_name,
                            old_relationship: old_a.relationship.to_string(),
                            new_relationship: new_a.relationship.to_string(),
                            old_scope: old_a.scope.as_ref().map(ToString::to_string),
                            new_scope: new_a.scope.as_ref().map(ToString::to_string),
                        },
                        impact: GraphChangeImpact::Medium,
                    });
                }
            }
        }
    }

    // Detect depth changes
    if config.detect_depth_changes {
        detect_depth_changes(&old_graph, &new_graph, component_matches, &mut changes);
    }

    // Detect reparenting (post-process to find moved dependencies)
    if config.detect_reparenting {
        detect_reparenting(&old_graph, &new_graph, component_matches, &mut changes);
    }

    // Sort changes by impact (critical first)
    changes.sort_by(|a, b| {
        let impact_order = |i: &GraphChangeImpact| match i {
            GraphChangeImpact::Critical => 0,
            GraphChangeImpact::High => 1,
            GraphChangeImpact::Medium => 2,
            GraphChangeImpact::Low => 3,
        };
        impact_order(&a.impact).cmp(&impact_order(&b.impact))
    });

    let summary = GraphChangeSummary::from_changes(&changes);
    (changes, summary)
}

/// Assess the impact of adding a dependency.
///
/// Depth numbering: 1 = root (no incoming edges), 2 = direct dep, 3+ = transitive.
/// `CYCLIC_SENTINEL_DEPTH` = unreachable from root (cyclic-only), treated as transitive.
/// A direct dependency (depth <= 2) that is vulnerable is Critical impact.
fn assess_impact_added(graph: &DependencyGraph, component_id: &CanonicalId) -> GraphChangeImpact {
    let depth = graph
        .get_depth(component_id)
        .unwrap_or(CYCLIC_SENTINEL_DEPTH);
    let is_direct = depth > 0 && depth <= 2 && depth != CYCLIC_SENTINEL_DEPTH;

    if graph.is_vulnerable(component_id) {
        if is_direct {
            GraphChangeImpact::Critical
        } else {
            GraphChangeImpact::High
        }
    } else if is_direct {
        GraphChangeImpact::Medium
    } else {
        GraphChangeImpact::Low
    }
}

/// Detect depth changes between matched components.
///
/// Ignores sentinel↔sentinel transitions (both unreachable).
/// Reports sentinel→real or real→sentinel transitions appropriately.
fn detect_depth_changes(
    old_graph: &DependencyGraph,
    new_graph: &DependencyGraph,
    matches: &HashMap<CanonicalId, Option<CanonicalId>>,
    changes: &mut Vec<DependencyGraphChange>,
) {
    for (old_id, new_id_opt) in matches {
        if let Some(new_id) = new_id_opt {
            let old_depth = old_graph.get_depth(old_id);
            let new_depth = new_graph.get_depth(new_id);

            if let (Some(od), Some(nd)) = (old_depth, new_depth)
                && od != nd
            {
                // Skip sentinel↔sentinel (both unreachable, no meaningful change)
                if od == CYCLIC_SENTINEL_DEPTH && nd == CYCLIC_SENTINEL_DEPTH {
                    continue;
                }

                let component_name = new_graph.get_component_name(new_id);

                let impact =
                    if nd < od && nd != CYCLIC_SENTINEL_DEPTH && new_graph.is_vulnerable(new_id) {
                        // Vulnerable component moved closer to root
                        GraphChangeImpact::High
                    } else if nd <= 2 && (od > 2 || od == CYCLIC_SENTINEL_DEPTH) {
                        // Became direct dependency (from transitive or unreachable)
                        GraphChangeImpact::Medium
                    } else {
                        GraphChangeImpact::Low
                    };

                changes.push(DependencyGraphChange {
                    component_id: new_id.clone(),
                    component_name,
                    change: DependencyChangeType::DepthChanged {
                        old_depth: od,
                        new_depth: nd,
                    },
                    impact,
                });
            }
        }
    }
}

/// Detect reparented components (moved from one parent to another).
///
/// Handles single-parent, multi-parent, root promotion, and root demotion cases.
/// For multi-parent scenarios, compares the mapped old parent set against the new
/// parent set. A "reparenting" requires at least one removed parent AND at least
/// one added parent. Only the specific add/remove entries involved in the
/// reparenting are suppressed — unrelated add/remove entries for the same child
/// are preserved.
fn detect_reparenting(
    old_graph: &DependencyGraph,
    new_graph: &DependencyGraph,
    matches: &HashMap<CanonicalId, Option<CanonicalId>>,
    changes: &mut Vec<DependencyGraphChange>,
) {
    for (old_id, new_id_opt) in matches {
        if let Some(new_id) = new_id_opt {
            let old_parents = old_graph.get_parents(old_id);
            let new_parents = new_graph.get_parents(new_id);

            // Skip if both have no parents (both are roots — no change)
            if old_parents.is_empty() && new_parents.is_empty() {
                continue;
            }

            // Map old parents through component_matches to new-SBOM ID space.
            // Parents not in the match map or matched to None (removed) are excluded.
            let old_parents_mapped: HashSet<CanonicalId> = old_parents
                .iter()
                .filter_map(|old_parent| matches.get(old_parent).and_then(|opt| opt.clone()))
                .collect();
            let new_parents_set: HashSet<CanonicalId> = new_parents.into_iter().collect();

            // Check if parents differ
            if old_parents_mapped == new_parents_set {
                continue;
            }

            // Determine which parents were removed and added
            let removed_parents: Vec<_> = old_parents_mapped.difference(&new_parents_set).collect();
            let added_parents: Vec<_> = new_parents_set.difference(&old_parents_mapped).collect();

            // Need at least one removed AND one added parent for a proper reparenting.
            // Pure parent-add or parent-remove without the other side is just a
            // dependency add/remove, which is already captured in the main diff loop.
            if removed_parents.is_empty() || added_parents.is_empty() {
                continue;
            }

            let old_parent = removed_parents[0];
            let new_parent = added_parents[0];

            let component_name = new_graph.get_component_name(new_id);
            let old_parent_name = new_graph.get_component_name(old_parent);
            let new_parent_name = new_graph.get_component_name(new_parent);

            // Only suppress the specific add/remove entries for the primary
            // reparenting pair (old_parent→child removed, new_parent→child added).
            // Other add/remove entries for the same child but different parents
            // are preserved.
            changes.retain(|c| match &c.change {
                DependencyChangeType::DependencyAdded { dependency_id, .. } => {
                    !(dependency_id == new_id && c.component_id == *new_parent)
                }
                DependencyChangeType::DependencyRemoved { dependency_id, .. } => {
                    !(dependency_id == new_id && c.component_id == *old_parent)
                }
                _ => true,
            });

            changes.push(DependencyGraphChange {
                component_id: new_id.clone(),
                component_name,
                change: DependencyChangeType::Reparented {
                    dependency_id: new_id.clone(),
                    dependency_name: new_graph.get_component_name(new_id),
                    old_parent_id: old_parent.clone(),
                    old_parent_name,
                    new_parent_id: new_parent.clone(),
                    new_parent_name,
                },
                impact: GraphChangeImpact::Medium,
            });
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{
        Component, DependencyEdge, DependencyType, NormalizedSbom, VulnerabilityRef,
        VulnerabilitySource,
    };

    /// Helper to create a test component with a given name
    fn make_component(name: &str) -> Component {
        Component::new(name.to_string(), name.to_string())
    }

    /// Helper to create a component with version
    fn make_component_v(name: &str, version: &str) -> Component {
        Component::new(name.to_string(), format!("{name}@{version}"))
            .with_version(version.to_string())
    }

    /// Helper to build a simple SBOM with given components and edges
    fn make_sbom(
        components: Vec<Component>,
        edges: Vec<(CanonicalId, CanonicalId)>,
    ) -> NormalizedSbom {
        let mut sbom = NormalizedSbom::default();
        for comp in components {
            sbom.add_component(comp);
        }
        for (from, to) in edges {
            sbom.add_edge(DependencyEdge::new(from, to, DependencyType::DependsOn));
        }
        sbom
    }

    /// Helper to build an SBOM with explicit relationship types on edges
    fn make_sbom_with_rel(
        components: Vec<Component>,
        edges: Vec<(CanonicalId, CanonicalId, DependencyType)>,
    ) -> NormalizedSbom {
        let mut sbom = NormalizedSbom::default();
        for comp in components {
            sbom.add_component(comp);
        }
        for (from, to, rel) in edges {
            sbom.add_edge(DependencyEdge::new(from, to, rel));
        }
        sbom
    }

    #[test]
    fn test_graph_diff_config_default() {
        let config = GraphDiffConfig::default();
        assert!(config.detect_reparenting);
        assert!(config.detect_depth_changes);
        assert_eq!(config.max_depth, 0);
    }

    #[test]
    fn test_graph_change_impact_display() {
        assert_eq!(GraphChangeImpact::Critical.as_str(), "critical");
        assert_eq!(GraphChangeImpact::High.as_str(), "high");
        assert_eq!(GraphChangeImpact::Medium.as_str(), "medium");
        assert_eq!(GraphChangeImpact::Low.as_str(), "low");
    }

    #[test]
    fn test_children_mapped_through_component_matches() {
        // Old SBOM: A -> B (old IDs)
        let a_old = make_component("a-old");
        let b_old = make_component("b-old");
        let a_old_id = a_old.canonical_id.clone();
        let b_old_id = b_old.canonical_id.clone();

        let old_sbom = make_sbom(
            vec![a_old, b_old],
            vec![(a_old_id.clone(), b_old_id.clone())],
        );

        // New SBOM: A -> B (new IDs, same logical components)
        let a_new = make_component("a-new");
        let b_new = make_component("b-new");
        let a_new_id = a_new.canonical_id.clone();
        let b_new_id = b_new.canonical_id.clone();

        let new_sbom = make_sbom(
            vec![a_new, b_new],
            vec![(a_new_id.clone(), b_new_id.clone())],
        );

        // Map: a-old -> a-new, b-old -> b-new
        let mut matches = HashMap::new();
        matches.insert(a_old_id, Some(a_new_id));
        matches.insert(b_old_id, Some(b_new_id));

        let config = GraphDiffConfig::default();
        let (changes, summary) = diff_dependency_graph(&old_sbom, &new_sbom, &matches, &config);

        // No changes expected: same logical graph structure
        assert_eq!(summary.dependencies_added, 0, "No false add: {changes:?}");
        assert_eq!(
            summary.dependencies_removed, 0,
            "No false remove: {changes:?}"
        );
    }

    #[test]
    fn test_depth_linear_chain() {
        // A -> B -> C -> D
        let a = make_component("a");
        let b = make_component("b");
        let c = make_component("c");
        let d = make_component("d");

        let ids: Vec<_> = [&a, &b, &c, &d]
            .iter()
            .map(|c| c.canonical_id.clone())
            .collect();
        let sbom = make_sbom(
            vec![a, b, c, d],
            vec![
                (ids[0].clone(), ids[1].clone()),
                (ids[1].clone(), ids[2].clone()),
                (ids[2].clone(), ids[3].clone()),
            ],
        );

        let config = GraphDiffConfig::default();
        let graph = DependencyGraph::from_sbom(&sbom, &config);

        assert_eq!(graph.get_depth(&ids[0]), Some(1)); // root
        assert_eq!(graph.get_depth(&ids[1]), Some(2));
        assert_eq!(graph.get_depth(&ids[2]), Some(3));
        assert_eq!(graph.get_depth(&ids[3]), Some(4));
    }

    #[test]
    fn test_depth_diamond_dependency() {
        // A -> B, A -> C, B -> D, C -> D
        // D should have min depth 3 (via A->B->D or A->C->D)
        let a = make_component("a");
        let b = make_component("b");
        let c = make_component("c");
        let d = make_component("d");

        let ids: Vec<_> = [&a, &b, &c, &d]
            .iter()
            .map(|c| c.canonical_id.clone())
            .collect();
        let sbom = make_sbom(
            vec![a, b, c, d],
            vec![
                (ids[0].clone(), ids[1].clone()),
                (ids[0].clone(), ids[2].clone()),
                (ids[1].clone(), ids[3].clone()),
                (ids[2].clone(), ids[3].clone()),
            ],
        );

        let config = GraphDiffConfig::default();
        let graph = DependencyGraph::from_sbom(&sbom, &config);

        assert_eq!(graph.get_depth(&ids[0]), Some(1));
        assert_eq!(graph.get_depth(&ids[1]), Some(2));
        assert_eq!(graph.get_depth(&ids[2]), Some(2));
        assert_eq!(graph.get_depth(&ids[3]), Some(3)); // min of both paths
    }

    #[test]
    fn test_depth_rootless_cycle() {
        // A -> B -> C -> A (pure cycle, no roots)
        let a = make_component("a");
        let b = make_component("b");
        let c = make_component("c");

        let ids: Vec<_> = [&a, &b, &c]
            .iter()
            .map(|c| c.canonical_id.clone())
            .collect();
        let sbom = make_sbom(
            vec![a, b, c],
            vec![
                (ids[0].clone(), ids[1].clone()),
                (ids[1].clone(), ids[2].clone()),
                (ids[2].clone(), ids[0].clone()),
            ],
        );

        let config = GraphDiffConfig::default();
        let graph = DependencyGraph::from_sbom(&sbom, &config);

        // All nodes should get sentinel depth (unreachable from root)
        for (i, id) in ids.iter().enumerate() {
            let depth = graph.get_depth(id);
            assert!(depth.is_some(), "Node {i} should have depth");
            assert_eq!(
                depth.unwrap(),
                CYCLIC_SENTINEL_DEPTH,
                "Cyclic node {i} should get sentinel depth, not 0"
            );
        }
    }

    #[test]
    fn test_depth_cycle_reachable_from_root() {
        // Root -> A -> B -> C -> B (cycle B→C→B reachable from root)
        let root = make_component("root");
        let a = make_component("a");
        let b = make_component("b");
        let c = make_component("c");

        let ids: Vec<_> = [&root, &a, &b, &c]
            .iter()
            .map(|comp| comp.canonical_id.clone())
            .collect();
        let sbom = make_sbom(
            vec![root, a, b, c],
            vec![
                (ids[0].clone(), ids[1].clone()), // root → A
                (ids[1].clone(), ids[2].clone()), // A → B
                (ids[2].clone(), ids[3].clone()), // B → C
                (ids[3].clone(), ids[2].clone()), // C → B (cycle)
            ],
        );

        let config = GraphDiffConfig::default();
        let graph = DependencyGraph::from_sbom(&sbom, &config);

        assert_eq!(graph.get_depth(&ids[0]), Some(1)); // root
        assert_eq!(graph.get_depth(&ids[1]), Some(2)); // A (direct)
        assert_eq!(graph.get_depth(&ids[2]), Some(3)); // B (transitive, reachable)
        assert_eq!(graph.get_depth(&ids[3]), Some(4)); // C (transitive, reachable)
        // Despite being in a cycle, B and C have real depths because
        // they are reachable from root via BFS
    }

    #[test]
    fn test_depth_disconnected_subgraphs() {
        // Subgraph 1: R1 -> A
        // Subgraph 2: R2 -> B -> C
        // Independent depth computation for each
        let r1 = make_component("r1");
        let a = make_component("a");
        let r2 = make_component("r2");
        let b = make_component("b");
        let c = make_component("c");

        let ids: Vec<_> = [&r1, &a, &r2, &b, &c]
            .iter()
            .map(|comp| comp.canonical_id.clone())
            .collect();
        let sbom = make_sbom(
            vec![r1, a, r2, b, c],
            vec![
                (ids[0].clone(), ids[1].clone()), // R1 → A
                (ids[2].clone(), ids[3].clone()), // R2 → B
                (ids[3].clone(), ids[4].clone()), // B → C
            ],
        );

        let config = GraphDiffConfig::default();
        let graph = DependencyGraph::from_sbom(&sbom, &config);

        assert_eq!(graph.get_depth(&ids[0]), Some(1)); // R1
        assert_eq!(graph.get_depth(&ids[1]), Some(2)); // A
        assert_eq!(graph.get_depth(&ids[2]), Some(1)); // R2
        assert_eq!(graph.get_depth(&ids[3]), Some(2)); // B
        assert_eq!(graph.get_depth(&ids[4]), Some(3)); // C
    }

    #[test]
    fn test_self_referencing_edge_no_infinite_loop() {
        // A -> A (self-loop)
        let a = make_component("a");
        let a_id = a.canonical_id.clone();

        let sbom = make_sbom(vec![a], vec![(a_id.clone(), a_id.clone())]);

        let config = GraphDiffConfig::default();
        let graph = DependencyGraph::from_sbom(&sbom, &config);

        // A is its own parent, but it's also a root (no OTHER incoming edges
        // that would remove it from the root set... actually it HAS an incoming
        // edge from itself). With the self-loop, A has an incoming edge so it's
        // NOT a root → gets sentinel depth.
        let depth = graph.get_depth(&a_id);
        assert!(depth.is_some(), "A should have a depth");
        // Self-loop means A has incoming edges, so not a root → sentinel
        assert_eq!(
            depth.unwrap(),
            CYCLIC_SENTINEL_DEPTH,
            "Self-referencing node should get sentinel depth"
        );
    }

    #[test]
    fn test_depth_max_depth_limit() {
        // A -> B -> C -> D with max_depth 2
        let a = make_component("a");
        let b = make_component("b");
        let c = make_component("c");
        let d = make_component("d");

        let ids: Vec<_> = [&a, &b, &c, &d]
            .iter()
            .map(|c| c.canonical_id.clone())
            .collect();
        let sbom = make_sbom(
            vec![a, b, c, d],
            vec![
                (ids[0].clone(), ids[1].clone()),
                (ids[1].clone(), ids[2].clone()),
                (ids[2].clone(), ids[3].clone()),
            ],
        );

        let config = GraphDiffConfig {
            max_depth: 2,
            ..Default::default()
        };
        let graph = DependencyGraph::from_sbom(&sbom, &config);

        assert_eq!(graph.get_depth(&ids[0]), Some(1));
        assert_eq!(graph.get_depth(&ids[1]), Some(2));
        // C and D get sentinel depth since BFS stops at depth 2
        // and they're unreachable from root BFS at that limit
        assert_eq!(graph.get_depth(&ids[2]), Some(CYCLIC_SENTINEL_DEPTH));
        assert_eq!(graph.get_depth(&ids[3]), Some(CYCLIC_SENTINEL_DEPTH));
    }

    #[test]
    fn test_reparenting_single_parent() {
        // Old: P1 -> C (P2 exists but not parent of C)
        // New: P2 -> C (P1 exists but not parent of C)
        // P1 and P2 are distinct components present in both SBOMs.
        let p1 = make_component("p1");
        let p2 = make_component("p2");
        let child = make_component("child");

        let p1_id = p1.canonical_id.clone();
        let p2_id = p2.canonical_id.clone();
        let child_id = child.canonical_id.clone();

        let old_sbom = make_sbom(
            vec![p1.clone(), p2.clone(), child.clone()],
            vec![(p1_id.clone(), child_id.clone())],
        );
        let new_sbom = make_sbom(
            vec![p1.clone(), p2.clone(), child.clone()],
            vec![(p2_id.clone(), child_id.clone())],
        );

        // Both parents map to themselves — they are distinct logical components
        let mut matches = HashMap::new();
        matches.insert(p1_id.clone(), Some(p1_id));
        matches.insert(p2_id.clone(), Some(p2_id));
        matches.insert(child_id.clone(), Some(child_id));

        let config = GraphDiffConfig::default();
        let (changes, summary) = diff_dependency_graph(&old_sbom, &new_sbom, &matches, &config);

        assert!(
            summary.reparented > 0,
            "Should detect reparenting: {changes:?}"
        );
    }

    #[test]
    fn test_renamed_parent_is_not_reparenting() {
        // Old: P1 -> C. New: P2 -> C. P1 matched to P2 (same logical component).
        // This is a rename, not reparenting — no structural change.
        let p1 = make_component("p1");
        let p2 = make_component("p2");
        let child = make_component("child");

        let p1_id = p1.canonical_id.clone();
        let p2_id = p2.canonical_id.clone();
        let child_id = child.canonical_id.clone();

        let old_sbom = make_sbom(
            vec![p1, child.clone()],
            vec![(p1_id.clone(), child_id.clone())],
        );
        let new_sbom = make_sbom(
            vec![p2, child.clone()],
            vec![(p2_id.clone(), child_id.clone())],
        );

        let mut matches = HashMap::new();
        matches.insert(p1_id, Some(p2_id));
        matches.insert(child_id.clone(), Some(child_id));

        let config = GraphDiffConfig::default();
        let (changes, summary) = diff_dependency_graph(&old_sbom, &new_sbom, &matches, &config);

        assert_eq!(
            summary.reparented, 0,
            "Renamed parent should not be reparenting: {changes:?}"
        );
    }

    #[test]
    fn test_reparenting_multi_parent() {
        // Old: P1 -> C, P2 -> C
        // New: P1 -> C, P3 -> C
        // P2 and P3 are distinct components (P2 removed, P3 added).
        // All components exist in both SBOMs to enable proper matching.
        let p1 = make_component("p1");
        let p2 = make_component("p2");
        let p3 = make_component("p3");
        let child = make_component("child");

        let p1_id = p1.canonical_id.clone();
        let p2_id = p2.canonical_id.clone();
        let p3_id = p3.canonical_id.clone();
        let child_id = child.canonical_id.clone();

        let old_sbom = make_sbom(
            vec![p1.clone(), p2.clone(), p3.clone(), child.clone()],
            vec![
                (p1_id.clone(), child_id.clone()),
                (p2_id.clone(), child_id.clone()),
            ],
        );
        let new_sbom = make_sbom(
            vec![p1.clone(), p2.clone(), p3.clone(), child.clone()],
            vec![
                (p1_id.clone(), child_id.clone()),
                (p3_id.clone(), child_id.clone()),
            ],
        );

        // All map to themselves — they are distinct logical components
        let mut matches = HashMap::new();
        matches.insert(p1_id.clone(), Some(p1_id));
        matches.insert(p2_id.clone(), Some(p2_id));
        matches.insert(p3_id.clone(), Some(p3_id));
        matches.insert(child_id.clone(), Some(child_id));

        let config = GraphDiffConfig::default();
        let (changes, summary) = diff_dependency_graph(&old_sbom, &new_sbom, &matches, &config);

        assert!(
            summary.reparented > 0,
            "Should detect multi-parent reparenting: {changes:?}"
        );
    }

    #[test]
    fn test_vulnerable_direct_dep_is_critical() {
        // A -> V where V has vulnerabilities and depth=1 (direct)
        let a = make_component("root");
        let mut vuln_comp = make_component("vuln-lib");
        vuln_comp.vulnerabilities.push(VulnerabilityRef {
            id: "CVE-2024-0001".to_string(),
            source: VulnerabilitySource::Osv,
            severity: None,
            cvss: vec![],
            affected_versions: vec![],
            remediation: None,
            description: None,
            cwes: vec![],
            published: None,
            modified: None,
            is_kev: false,
            kev_info: None,
            vex_status: None,
        });

        let a_id = a.canonical_id.clone();
        let v_id = vuln_comp.canonical_id.clone();

        let old_sbom = make_sbom(vec![a.clone()], vec![]);
        let new_sbom = make_sbom(
            vec![a.clone(), vuln_comp],
            vec![(a_id.clone(), v_id.clone())],
        );

        let mut matches = HashMap::new();
        matches.insert(a_id.clone(), Some(a_id));

        let config = GraphDiffConfig::default();
        let (changes, _) = diff_dependency_graph(&old_sbom, &new_sbom, &matches, &config);

        let critical = changes
            .iter()
            .any(|c| c.impact == GraphChangeImpact::Critical);
        assert!(
            critical,
            "Vulnerable direct dep should be critical impact: {changes:?}"
        );
    }

    #[test]
    fn test_empty_sboms_no_changes() {
        let old_sbom = NormalizedSbom::default();
        let new_sbom = NormalizedSbom::default();
        let matches = HashMap::new();
        let config = GraphDiffConfig::default();

        let (changes, summary) = diff_dependency_graph(&old_sbom, &new_sbom, &matches, &config);
        assert!(changes.is_empty());
        assert_eq!(summary.total_changes, 0);
    }

    #[test]
    fn test_identical_graphs_no_changes() {
        let a = make_component("a");
        let b = make_component("b");
        let a_id = a.canonical_id.clone();
        let b_id = b.canonical_id.clone();

        let sbom = make_sbom(vec![a, b], vec![(a_id.clone(), b_id.clone())]);

        let mut matches = HashMap::new();
        matches.insert(a_id.clone(), Some(a_id));
        matches.insert(b_id.clone(), Some(b_id));

        let config = GraphDiffConfig::default();
        let (changes, summary) = diff_dependency_graph(&sbom, &sbom, &matches, &config);

        // No added/removed (depth might diff trivially due to same-SBOM comparison)
        assert_eq!(summary.dependencies_added, 0, "No false adds: {changes:?}");
        assert_eq!(
            summary.dependencies_removed, 0,
            "No false removes: {changes:?}"
        );
    }

    #[test]
    fn test_removed_child_not_false_positive() {
        // Old: A -> B_v1, New: A -> B_v2 (different canonical IDs for B)
        // B_v1 is matched to B_v2 in the mapping.
        // Should detect no structural change (same logical edge, just version bump).
        let a = make_component("a");
        let b_v1 = make_component_v("b", "1.0");
        let b_v2 = make_component_v("b", "2.0");

        let a_id = a.canonical_id.clone();
        let b_v1_id = b_v1.canonical_id.clone();
        let b_v2_id = b_v2.canonical_id.clone();

        let old_sbom = make_sbom(vec![a.clone(), b_v1], vec![(a_id.clone(), b_v1_id.clone())]);
        let new_sbom = make_sbom(vec![a.clone(), b_v2], vec![(a_id.clone(), b_v2_id.clone())]);

        let mut matches = HashMap::new();
        matches.insert(a_id.clone(), Some(a_id));
        matches.insert(b_v1_id, Some(b_v2_id));

        let config = GraphDiffConfig::default();
        let (changes, summary) = diff_dependency_graph(&old_sbom, &new_sbom, &matches, &config);

        assert_eq!(
            summary.dependencies_added, 0,
            "Version bump should not be false add: {changes:?}"
        );
        assert_eq!(
            summary.dependencies_removed, 0,
            "Version bump should not be false remove: {changes:?}"
        );
    }

    #[test]
    fn test_unmatched_old_child_excluded_from_comparison() {
        // Old: A -> B, A -> C. New: A -> B. C is removed (matched to None).
        // Should detect: C removed as dependency of A. Not: false add of B.
        let a = make_component("a");
        let b = make_component("b");
        let c = make_component("c");

        let a_id = a.canonical_id.clone();
        let b_id = b.canonical_id.clone();
        let c_id = c.canonical_id.clone();

        let old_sbom = make_sbom(
            vec![a.clone(), b.clone(), c],
            vec![(a_id.clone(), b_id.clone()), (a_id.clone(), c_id.clone())],
        );
        let new_sbom = make_sbom(
            vec![a.clone(), b.clone()],
            vec![(a_id.clone(), b_id.clone())],
        );

        let mut matches = HashMap::new();
        matches.insert(a_id.clone(), Some(a_id));
        matches.insert(b_id.clone(), Some(b_id));
        matches.insert(c_id, None); // C is removed

        let config = GraphDiffConfig::default();
        let (changes, summary) = diff_dependency_graph(&old_sbom, &new_sbom, &matches, &config);

        // C was removed, so it's excluded from old_children_mapped.
        // B is in both → no change for B.
        // The graph diff tracks children per matched parent. Since C is excluded from
        // the mapped set, it won't appear in the difference. This is correct because
        // the component-level diff already reports C as removed.
        assert_eq!(summary.dependencies_added, 0, "No false adds: {changes:?}");
    }

    #[test]
    fn test_reparenting_with_removed_parent() {
        // Old: P1 -> C, P2 -> C. New: P1 -> C. P2 removed (matched to None).
        // Should NOT report reparenting — parent set simply lost a removed node.
        let p1 = make_component("p1");
        let p2 = make_component("p2");
        let child = make_component("child");

        let p1_id = p1.canonical_id.clone();
        let p2_id = p2.canonical_id.clone();
        let child_id = child.canonical_id.clone();

        let old_sbom = make_sbom(
            vec![p1.clone(), p2, child.clone()],
            vec![
                (p1_id.clone(), child_id.clone()),
                (p2_id.clone(), child_id.clone()),
            ],
        );
        let new_sbom = make_sbom(
            vec![p1.clone(), child.clone()],
            vec![(p1_id.clone(), child_id.clone())],
        );

        let mut matches = HashMap::new();
        matches.insert(p1_id.clone(), Some(p1_id));
        matches.insert(p2_id, None); // P2 removed
        matches.insert(child_id.clone(), Some(child_id));

        let config = GraphDiffConfig::default();
        let (changes, summary) = diff_dependency_graph(&old_sbom, &new_sbom, &matches, &config);

        assert_eq!(
            summary.reparented, 0,
            "Removed parent should not trigger reparenting: {changes:?}"
        );
    }

    #[test]
    fn test_relationship_change_detected() {
        // Old: A -[DependsOn]-> B. New: A -[DevDependsOn]-> B.
        // Same endpoints, different relationship → RelationshipChanged.
        let a = make_component("a");
        let b = make_component("b");
        let a_id = a.canonical_id.clone();
        let b_id = b.canonical_id.clone();

        let old_sbom = make_sbom_with_rel(
            vec![a.clone(), b.clone()],
            vec![(a_id.clone(), b_id.clone(), DependencyType::DependsOn)],
        );
        let new_sbom = make_sbom_with_rel(
            vec![a, b],
            vec![(a_id.clone(), b_id.clone(), DependencyType::DevDependsOn)],
        );

        let mut matches = HashMap::new();
        matches.insert(a_id.clone(), Some(a_id));
        matches.insert(b_id.clone(), Some(b_id));

        let config = GraphDiffConfig::default();
        let (changes, summary) = diff_dependency_graph(&old_sbom, &new_sbom, &matches, &config);

        assert!(
            summary.relationship_changed > 0,
            "Should detect relationship change: {changes:?}"
        );
        // Should NOT report add+remove for same endpoints
        assert_eq!(
            summary.dependencies_added, 0,
            "Relationship change is not an add: {changes:?}"
        );
        assert_eq!(
            summary.dependencies_removed, 0,
            "Relationship change is not a remove: {changes:?}"
        );
    }

    #[test]
    fn test_scope_change_detected() {
        // Old: A -[DependsOn, Required]-> B. New: A -[DependsOn, Optional]-> B.
        // Same endpoints and relationship, different scope → RelationshipChanged.
        use crate::model::DependencyScope;

        let a = make_component("a");
        let b = make_component("b");
        let a_id = a.canonical_id.clone();
        let b_id = b.canonical_id.clone();

        let mut old_sbom = NormalizedSbom::default();
        old_sbom.add_component(a.clone());
        old_sbom.add_component(b.clone());
        old_sbom.add_edge(
            DependencyEdge::new(a_id.clone(), b_id.clone(), DependencyType::DependsOn)
                .with_scope(DependencyScope::Required),
        );

        let mut new_sbom = NormalizedSbom::default();
        new_sbom.add_component(a);
        new_sbom.add_component(b);
        new_sbom.add_edge(
            DependencyEdge::new(a_id.clone(), b_id.clone(), DependencyType::DependsOn)
                .with_scope(DependencyScope::Optional),
        );

        let mut matches = HashMap::new();
        matches.insert(a_id.clone(), Some(a_id));
        matches.insert(b_id.clone(), Some(b_id));

        let config = GraphDiffConfig::default();
        let (changes, summary) = diff_dependency_graph(&old_sbom, &new_sbom, &matches, &config);

        assert!(
            summary.relationship_changed > 0,
            "Should detect scope change: {changes:?}"
        );
    }

    #[test]
    fn test_reparenting_does_not_suppress_unrelated_add() {
        // Reparenting C from P1→P2 should NOT suppress "C added to P3".
        // Old: P1 -> C, P2 exists, P3 exists
        // New: P2 -> C, P3 -> C
        // P1→C removed, P2→C added (reparenting), P3→C added (unrelated, must survive)
        let p1 = make_component("p1");
        let p2 = make_component("p2");
        let p3 = make_component("p3");
        let child = make_component("child");

        let p1_id = p1.canonical_id.clone();
        let p2_id = p2.canonical_id.clone();
        let p3_id = p3.canonical_id.clone();
        let child_id = child.canonical_id.clone();

        let old_sbom = make_sbom(
            vec![p1.clone(), p2.clone(), p3.clone(), child.clone()],
            vec![(p1_id.clone(), child_id.clone())],
        );
        let new_sbom = make_sbom(
            vec![p1.clone(), p2.clone(), p3.clone(), child.clone()],
            vec![
                (p2_id.clone(), child_id.clone()),
                (p3_id.clone(), child_id.clone()),
            ],
        );

        let mut matches = HashMap::new();
        matches.insert(p1_id.clone(), Some(p1_id));
        matches.insert(p2_id.clone(), Some(p2_id.clone()));
        matches.insert(p3_id.clone(), Some(p3_id.clone()));
        matches.insert(child_id.clone(), Some(child_id.clone()));

        let config = GraphDiffConfig::default();
        let (changes, summary) = diff_dependency_graph(&old_sbom, &new_sbom, &matches, &config);

        // Should have reparenting (P1→P2)
        assert!(
            summary.reparented > 0,
            "Should detect reparenting: {changes:?}"
        );

        // The reparenting picks one of {P2, P3} as the new parent. The OTHER one's
        // DependencyAdded entry must survive — it's unrelated to the reparenting.
        let reparent = changes
            .iter()
            .find(|c| matches!(&c.change, DependencyChangeType::Reparented { .. }))
            .expect("Should have a reparent entry");
        let reparent_new_parent = match &reparent.change {
            DependencyChangeType::Reparented { new_parent_id, .. } => new_parent_id.clone(),
            _ => unreachable!(),
        };
        let other_parent = if reparent_new_parent == p2_id {
            &p3_id
        } else {
            &p2_id
        };

        let other_added = changes.iter().any(|c| {
            c.component_id == *other_parent
                && matches!(
                    &c.change,
                    DependencyChangeType::DependencyAdded { dependency_id, .. }
                    if *dependency_id == child_id
                )
        });
        assert!(
            other_added,
            "The non-reparented parent's add should not be suppressed: {changes:?}"
        );
    }

    #[test]
    fn test_root_promotion_not_skipped() {
        // Old: P1 -> C (C has a parent)
        // New: C is a root (no parents)
        // This is NOT reparenting (no added parent), but the code should
        // not skip it entirely — it should still detect the parent removal.
        let p1 = make_component("p1");
        let child = make_component("child");

        let p1_id = p1.canonical_id.clone();
        let child_id = child.canonical_id.clone();

        let old_sbom = make_sbom(
            vec![p1.clone(), child.clone()],
            vec![(p1_id.clone(), child_id.clone())],
        );
        let new_sbom = make_sbom(vec![p1.clone(), child.clone()], vec![]);

        let mut matches = HashMap::new();
        matches.insert(p1_id.clone(), Some(p1_id.clone()));
        matches.insert(child_id.clone(), Some(child_id));

        let config = GraphDiffConfig::default();
        let (changes, summary) = diff_dependency_graph(&old_sbom, &new_sbom, &matches, &config);

        // Should detect the removed dependency (P1→C removed)
        assert!(
            summary.dependencies_removed > 0,
            "Root promotion: dependency removal should be detected: {changes:?}"
        );
        // Should NOT report reparenting (no new parent added)
        assert_eq!(
            summary.reparented, 0,
            "Root promotion is not reparenting: {changes:?}"
        );
    }

    #[test]
    fn test_root_demotion_not_skipped() {
        // Old: C is a root (no parents)
        // New: P1 -> C (C now has a parent)
        // This is NOT reparenting, just a dependency addition.
        let p1 = make_component("p1");
        let child = make_component("child");

        let p1_id = p1.canonical_id.clone();
        let child_id = child.canonical_id.clone();

        let old_sbom = make_sbom(vec![p1.clone(), child.clone()], vec![]);
        let new_sbom = make_sbom(
            vec![p1.clone(), child.clone()],
            vec![(p1_id.clone(), child_id.clone())],
        );

        let mut matches = HashMap::new();
        matches.insert(p1_id.clone(), Some(p1_id.clone()));
        matches.insert(child_id.clone(), Some(child_id));

        let config = GraphDiffConfig::default();
        let (changes, summary) = diff_dependency_graph(&old_sbom, &new_sbom, &matches, &config);

        // Should detect the added dependency
        assert!(
            summary.dependencies_added > 0,
            "Root demotion: dependency addition should be detected: {changes:?}"
        );
        // Should NOT report reparenting (no old parent removed)
        assert_eq!(
            summary.reparented, 0,
            "Root demotion is not reparenting: {changes:?}"
        );
    }

    #[test]
    fn test_parent_added_multi_parent_not_reparenting() {
        // Old: P1 -> C. New: P1 -> C, P2 -> C.
        // C gains a parent but keeps the old one — this is NOT reparenting.
        let p1 = make_component("p1");
        let p2 = make_component("p2");
        let child = make_component("child");

        let p1_id = p1.canonical_id.clone();
        let p2_id = p2.canonical_id.clone();
        let child_id = child.canonical_id.clone();

        let old_sbom = make_sbom(
            vec![p1.clone(), p2.clone(), child.clone()],
            vec![(p1_id.clone(), child_id.clone())],
        );
        let new_sbom = make_sbom(
            vec![p1.clone(), p2.clone(), child.clone()],
            vec![
                (p1_id.clone(), child_id.clone()),
                (p2_id.clone(), child_id.clone()),
            ],
        );

        let mut matches = HashMap::new();
        matches.insert(p1_id.clone(), Some(p1_id));
        matches.insert(p2_id.clone(), Some(p2_id));
        matches.insert(child_id.clone(), Some(child_id));

        let config = GraphDiffConfig::default();
        let (changes, summary) = diff_dependency_graph(&old_sbom, &new_sbom, &matches, &config);

        assert_eq!(
            summary.reparented, 0,
            "Adding a new parent while keeping old is not reparenting: {changes:?}"
        );
        // But the P2→C addition should still be detected
        assert!(
            summary.dependencies_added > 0,
            "P2→C should be detected as added: {changes:?}"
        );
    }

    #[test]
    fn test_same_relationship_no_change() {
        // Old: A -[DependsOn]-> B. New: A -[DependsOn]-> B.
        // Same everything → no change.
        let a = make_component("a");
        let b = make_component("b");
        let a_id = a.canonical_id.clone();
        let b_id = b.canonical_id.clone();

        let old_sbom = make_sbom_with_rel(
            vec![a.clone(), b.clone()],
            vec![(a_id.clone(), b_id.clone(), DependencyType::DependsOn)],
        );
        let new_sbom = make_sbom_with_rel(
            vec![a, b],
            vec![(a_id.clone(), b_id.clone(), DependencyType::DependsOn)],
        );

        let mut matches = HashMap::new();
        matches.insert(a_id.clone(), Some(a_id));
        matches.insert(b_id.clone(), Some(b_id));

        let config = GraphDiffConfig::default();
        let (changes, summary) = diff_dependency_graph(&old_sbom, &new_sbom, &matches, &config);

        assert_eq!(
            summary.relationship_changed, 0,
            "Same relationship should not be a change: {changes:?}"
        );
    }

    #[test]
    fn test_duplicate_edges_different_types() {
        // A -[DependsOn]-> B and A -[DevDependsOn]-> B in same SBOM.
        // The last edge wins in the edge_attrs map (HashMap insert semantics).
        let a = make_component("a");
        let b = make_component("b");
        let a_id = a.canonical_id.clone();
        let b_id = b.canonical_id.clone();

        let mut sbom = NormalizedSbom::default();
        sbom.add_component(a);
        sbom.add_component(b);
        sbom.add_edge(DependencyEdge::new(
            a_id.clone(),
            b_id.clone(),
            DependencyType::DependsOn,
        ));
        sbom.add_edge(DependencyEdge::new(
            a_id.clone(),
            b_id.clone(),
            DependencyType::DevDependsOn,
        ));

        let config = GraphDiffConfig::default();
        let graph = DependencyGraph::from_sbom(&sbom, &config);

        // B should appear as child of A (possibly duplicated in children list)
        let children = graph.get_children(&a_id);
        assert!(children.contains(&b_id), "B should be a child of A");

        // Edge attrs should have one entry (last-write-wins for same pair)
        let attrs = graph.get_edge_attrs(&a_id, &b_id);
        assert!(attrs.is_some(), "Should have edge attrs for A→B");
    }

    #[test]
    fn test_large_graph_completes() {
        // 500 nodes in a chain: root → n1 → n2 → ... → n499
        let mut components = Vec::new();
        let mut edges = Vec::new();
        let mut ids = Vec::new();

        for i in 0..500 {
            let comp = make_component(&format!("node-{i}"));
            ids.push(comp.canonical_id.clone());
            components.push(comp);
        }
        for i in 0..499 {
            edges.push((ids[i].clone(), ids[i + 1].clone()));
        }

        let sbom = make_sbom(components, edges);
        let config = GraphDiffConfig::default();
        let graph = DependencyGraph::from_sbom(&sbom, &config);

        // Root should have depth 1, last node should have depth 500
        assert_eq!(graph.get_depth(&ids[0]), Some(1));
        assert_eq!(graph.get_depth(&ids[499]), Some(500));
    }

    #[test]
    fn test_empty_vs_nonempty_graph() {
        // Old: no edges. New: A → B. All deps should be "added".
        let a = make_component("a");
        let b = make_component("b");
        let a_id = a.canonical_id.clone();
        let b_id = b.canonical_id.clone();

        let old_sbom = make_sbom(vec![a.clone(), b.clone()], vec![]);
        let new_sbom = make_sbom(vec![a, b], vec![(a_id.clone(), b_id.clone())]);

        let mut matches = HashMap::new();
        matches.insert(a_id.clone(), Some(a_id));
        matches.insert(b_id.clone(), Some(b_id));

        let config = GraphDiffConfig::default();
        let (changes, summary) = diff_dependency_graph(&old_sbom, &new_sbom, &matches, &config);

        assert!(
            summary.dependencies_added > 0,
            "Should detect added dependency: {changes:?}"
        );
        assert_eq!(
            summary.dependencies_removed, 0,
            "No false removes: {changes:?}"
        );
    }

    #[test]
    fn test_nonempty_vs_empty_graph() {
        // Old: A → B. New: no edges. All deps should be "removed".
        let a = make_component("a");
        let b = make_component("b");
        let a_id = a.canonical_id.clone();
        let b_id = b.canonical_id.clone();

        let old_sbom = make_sbom(
            vec![a.clone(), b.clone()],
            vec![(a_id.clone(), b_id.clone())],
        );
        let new_sbom = make_sbom(vec![a, b], vec![]);

        let mut matches = HashMap::new();
        matches.insert(a_id.clone(), Some(a_id));
        matches.insert(b_id.clone(), Some(b_id));

        let config = GraphDiffConfig::default();
        let (changes, summary) = diff_dependency_graph(&old_sbom, &new_sbom, &matches, &config);

        assert!(
            summary.dependencies_removed > 0,
            "Should detect removed dependency: {changes:?}"
        );
        assert_eq!(summary.dependencies_added, 0, "No false adds: {changes:?}");
    }

    #[test]
    fn test_relation_filter() {
        // Graph with DependsOn and DevDependsOn edges.
        // Filtering to only DependsOn should exclude DevDependsOn edges.
        let a = make_component("a");
        let b = make_component("b");
        let c = make_component("c");
        let a_id = a.canonical_id.clone();
        let b_id = b.canonical_id.clone();
        let c_id = c.canonical_id.clone();

        let mut sbom = NormalizedSbom::default();
        sbom.add_component(a);
        sbom.add_component(b);
        sbom.add_component(c);
        sbom.add_edge(DependencyEdge::new(
            a_id.clone(),
            b_id.clone(),
            DependencyType::DependsOn,
        ));
        sbom.add_edge(DependencyEdge::new(
            a_id.clone(),
            c_id.clone(),
            DependencyType::DevDependsOn,
        ));

        let config = GraphDiffConfig {
            relation_filter: vec!["depends-on".to_string()],
            ..Default::default()
        };
        let graph = DependencyGraph::from_sbom(&sbom, &config);

        let children = graph.get_children(&a_id);
        assert!(
            children.contains(&b_id),
            "DependsOn edge should be included"
        );
        assert!(
            !children.contains(&c_id),
            "DevDependsOn edge should be excluded by filter"
        );
    }
}