sqlitegraph 2.2.2

Embedded graph database with full ACID transactions, HNSW vector search, dual backend support, and comprehensive graph algorithms library
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
//! Call graph analysis for program dependence and cyclic region detection.
//!
//! This module provides algorithms for analyzing call graphs and other directed
//! graphs where cycles indicate mutual recursion or circular dependencies. The key
//! operation is SCC (Strongly Connected Component) collapse, which transforms
//! cyclic graphs into Directed Acyclic Graphs (DAGs) for easier analysis.
//!
//! # SCC Collapse (Graph Condensation)
//!
//! The core operation collapses each strongly connected component into a single
//! "supernode" and builds edges between supernodes. This produces the **condensation
//! graph**, which is always a DAG by the fundamental property of SCC decomposition.
//!
//! ## Why Collapse SCCs?
//!
//! - **Mutual Recursion**: Functions that call each other directly or indirectly
//! - **Circular Dependencies**: Modules that depend on each other
//! - **Cycle Detection**: Non-trivial SCCs indicate cycles in the original graph
//! - **Topological Analysis**: Condensation graph enables topological sorting
//! - **Visualization**: Simplifies complex graphs by merging cyclic regions
//!
//! ## Condensation Graph Properties
//!
//! - **Acyclic**: No edges from a supernode to itself (by definition)
//! - **DAG**: Always suitable for topological sorting
//! - **Hierarchical**: Preserves reachability relationships between SCCs
//! - **Reversible**: Bidirectional mappings enable original node lookup
//!
//! # Complexity
//!
//! - **Time**: O(|V| + |E|) - one SCC pass + one edge enumeration
//! - **Space**: O(|V|) for node mappings and condensed edges
//!
//! Where:
//! - V = number of vertices
//! - E = number of edges
//!
//! # Example
//!
//! ```rust,ignore
//! use sqlitegraph::{SqliteGraph, algo::collapse_sccs};
//!
//! let graph = SqliteGraph::open_in_memory()?;
//! // ... build call graph with mutual recursion ...
//!
//! let collapsed = collapse_sccs(&graph)?;
//!
//! println!("Found {} SCCs", collapsed.num_sccs);
//! println!("Condensed graph has {} edges", collapsed.supernode_edges.len());
//!
//! // Find which SCC a node belongs to
//! if let Some(supernode) = collapsed.supernode_for(node_id) {
//!     println!("Node {} is in SCC {}", node_id, supernode);
//! }
//!
//! // Get all nodes in an SCC
//! if let Some(members) = collapsed.members_of(supernode) {
//!     println!("SCC {} has {} members", supernode, members.len());
//! }
//! ```
//!
//! # Use Cases
//!
//! ## Call Graph Analysis
//!
//! - **Mutual Recursion Detection**: Find functions that call each other
//! - **Dependency Clusters**: Group mutually dependent functions
//! - **Refactoring Safety**: Check if changing one function affects others
//!
//! ## Dependency Analysis
//!
//! - **Circular Dependencies**: Detect module circular dependencies
//! - **Build Order**: Topological sort on condensation DAG
//! - **Impact Analysis**: What gets affected by changing an SCC?
//!
//! ## Program Visualization
//!
//! - **Graph Simplification**: Collapse cycles for cleaner diagrams
//! - **Cluster Detection**: SCCs represent tightly-coupled regions
//! - **Hierarchical Views**: Multi-level zoom (original vs condensed)
//!
//! # References
//!
//! - CP-Algorithms: Strongly Connected Components and Condensation Graph
//!   https://cp-algorithms.com/graph/strongly-connected-components.html
//! - Tarjan, R. E. "Depth-First Search and Linear Graph Algorithms." SIAM 1972

use ahash::{AHashMap, AHashSet};

use crate::errors::SqliteGraphError;
use crate::graph::SqliteGraph;
use crate::progress::ProgressCallback;

use super::scc::strongly_connected_components;

/// Result of SCC collapse operation (condensation graph).
///
/// Contains the bidirectional mappings between original nodes and supernodes,
/// plus the condensed DAG edges between supernodes.
///
/// # Fields
///
/// - `node_to_supernode`: Maps each original node ID to its SCC supernode ID
/// - `supernode_members`: Maps each supernode ID to the set of original node IDs in that SCC
/// - `supernode_edges`: Edges between supernodes in the condensed DAG (sorted, no self-loops)
/// - `num_sccs`: Total number of SCCs found (equals number of supernodes)
///
/// # Example
///
/// ```rust,ignore
/// let collapsed = collapse_sccs(&graph)?;
///
/// // Check how many SCCs were found
/// println!("Found {} SCCs", collapsed.num_sccs);
///
/// // Find which SCC a node belongs to
/// if let Some(supernode) = collapsed.supernode_for(node_id) {
///     println!("Node {} is in SCC {}", node_id, supernode);
///     if let Some(members) = collapsed.members_of(supernode) {
///         println!("SCC members: {:?}", members);
///     }
///     // Check if this is a trivial SCC (single node, no self-loop)
///     if collapsed.is_trivial(supernode) {
///         println!("This is a trivial SCC (no cycle)");
///     } else {
///         println!("This is a non-trivial SCC (contains a cycle)");
///     }
/// }
///
/// // Iterate over condensed DAG edges
/// for &(from, to) in &collapsed.supernode_edges {
///     println!("SCC {} -> SCC {}", from, to);
/// }
/// ```
#[derive(Debug, Clone)]
pub struct SccCollapseResult {
    /// Maps each original node ID to its SCC supernode ID.
    /// Use `supernode_for()` for convenient lookup.
    pub node_to_supernode: AHashMap<i64, i64>,

    /// Maps each supernode ID to the set of original node IDs in that SCC.
    /// Use `members_of()` for convenient lookup.
    pub supernode_members: AHashMap<i64, AHashSet<i64>>,

    /// Edges between supernodes in the condensed DAG.
    /// No self-loops (from == to) exist by definition.
    /// Sorted for deterministic output.
    pub supernode_edges: Vec<(i64, i64)>,

    /// Total number of SCCs found.
    /// Equals the number of supernodes in the condensed graph.
    pub num_sccs: usize,
}

impl SccCollapseResult {
    /// Gets the supernode ID for a given original node.
    ///
    /// Returns `Some(supernode_id)` if the node exists in the graph,
    /// or `None` if the node is unknown.
    ///
    /// # Arguments
    /// * `node` - The original node ID to look up
    ///
    /// # Returns
    /// `Some(i64)` with the supernode ID, or `None` if node not found
    ///
    /// # Example
    /// ```rust,ignore
    /// let collapsed = collapse_sccs(&graph)?;
    /// if let Some(supernode) = collapsed.supernode_for(node_id) {
    ///     println!("Node {} is in SCC {}", node_id, supernode);
    /// }
    /// ```
    pub fn supernode_for(&self, node: i64) -> Option<i64> {
        self.node_to_supernode.get(&node).copied()
    }

    /// Gets the set of original node IDs belonging to a supernode.
    ///
    /// Returns `Some(&AHashSet<i64>)` with the member nodes, or `None` if the
    /// supernode ID is unknown.
    ///
    /// # Arguments
    /// * `supernode` - The supernode ID to look up
    ///
    /// # Returns
    /// `Some(&AHashSet<i64>)` with member nodes, or `None` if supernode not found
    ///
    /// # Example
    /// ```rust,ignore
    /// let collapsed = collapse_sccs(&graph)?;
    /// if let Some(members) = collapsed.members_of(supernode_id) {
    ///     println!("SCC {} has {} members:", supernode_id, members.len());
    ///     for &node in members {
    ///         println!("  - Node {}", node);
    ///     }
    /// }
    /// ```
    pub fn members_of(&self, supernode: i64) -> Option<&AHashSet<i64>> {
        self.supernode_members.get(&supernode)
    }

    /// Checks if an SCC is trivial (single node with no self-loop).
    ///
    /// A trivial SCC is a single node that doesn't participate in a cycle.
    /// Non-trivial SCCs have multiple nodes OR a single node with a self-loop,
    /// both indicating cyclic behavior.
    ///
    /// # Arguments
    /// * `supernode` - The supernode ID to check
    ///
    /// # Returns
    /// `true` if the SCC is trivial (single node), `false` if non-trivial or not found
    ///
    /// # Example
    /// ```rust,ignore
    /// let collapsed = collapse_sccs(&graph)?;
    /// for (&supernode, members) in &collapsed.supernode_members {
    ///     if collapsed.is_trivial(supernode) {
    ///         println!("SCC {} is trivial (no cycle)", supernode);
    ///     } else {
    ///         println!("SCC {} is non-trivial (contains cycle)", supernode);
    ///     }
    /// }
    /// ```
    pub fn is_trivial(&self, supernode: i64) -> bool {
        match self.members_of(supernode) {
            Some(members) => members.len() == 1,
            None => false,
        }
    }

    /// Gets the number of non-trivial SCCs (cycles in the original graph).
    ///
    /// Non-trivial SCCs indicate cyclic regions in the original graph.
    /// This is useful for detecting mutual recursion or circular dependencies.
    ///
    /// # Returns
    /// Number of SCCs with more than one member
    ///
    /// # Example
    /// ```rust,ignore
    /// let collapsed = collapse_sccs(&graph)?;
    /// let cycle_count = collapsed.non_trivial_count();
    /// if cycle_count > 0 {
    ///     println!("Graph contains {} cycles (mutual recursion)", cycle_count);
    /// }
    /// ```
    pub fn non_trivial_count(&self) -> usize {
        self.supernode_members
            .values()
            .filter(|members| members.len() > 1)
            .count()
    }

    /// Gets all nodes that are part of non-trivial SCCs.
    ///
    /// Returns the set of all node IDs that participate in cycles.
    /// Useful for identifying which functions are involved in mutual recursion.
    ///
    /// # Returns
    /// Set of node IDs in non-trivial SCCs
    ///
    /// # Example
    /// ```rust,ignore
    /// let collapsed = collapse_sccs(&graph)?;
    /// let cyclic_nodes = collapsed.non_trivial_nodes();
    /// if !cyclic_nodes.is_empty() {
    ///     println!("Nodes involved in cycles: {:?}", cyclic_nodes);
    /// }
    /// ```
    pub fn non_trivial_nodes(&self) -> AHashSet<i64> {
        self.supernode_members
            .values()
            .filter(|members| members.len() > 1)
            .flat_map(|members| members.iter().copied())
            .collect()
    }
}

/// Collapses SCCs to form a condensation DAG.
///
/// Transforms a directed graph into a DAG by collapsing each strongly connected
/// component into a single "supernode". The resulting condensation graph is
/// always acyclic, enabling topological analysis on graphs with cycles.
///
/// This is particularly useful for:
/// - **Call graph analysis**: Collapse mutually recursive functions
/// - **Dependency analysis**: Detect and handle circular dependencies
/// - **Graph visualization**: Simplify cyclic regions for cleaner diagrams
///
/// # Arguments
/// * `graph` - The graph to analyze
///
/// # Returns
/// `SccCollapseResult` containing:
/// - Bidirectional mappings between original nodes and supernodes
/// - Condensed DAG edges (between supernodes)
/// - Number of SCCs found
///
/// # Complexity
/// - **Time**: O(|V| + |E|) - one SCC pass + one edge enumeration
/// - **Space**: O(|V|) for node mappings and condensed edges
///
/// # Algorithm Steps
///
/// 1. **Compute SCCs**: Call `strongly_connected_components()` to decompose graph
/// 2. **Handle empty graph**: Return empty `SccCollapseResult` if no nodes exist
/// 3. **Build node_to_supernode mapping**: Map each original node to its SCC supernode ID
/// 4. **Build supernode_members mapping**: Map each supernode to its member nodes
/// 5. **Build supernode_edges**: Enumerate edges, deduplicate, filter self-loops
/// 6. **Sort edges**: Ensure deterministic output
/// 7. **Return result**: Complete with count and mappings
///
/// # Condensation Graph Properties
///
/// - **No self-loops**: Edges only exist between different supernodes
/// - **Acyclic**: Always a DAG by fundamental property of SCC decomposition
/// - **Deterministic**: Edges are sorted for consistent output
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::{SqliteGraph, algo::collapse_sccs};
///
/// let graph = SqliteGraph::open_in_memory()?;
/// // ... build call graph ...
///
/// let collapsed = collapse_sccs(&graph)?;
///
/// println!("Found {} SCCs", collapsed.num_sccs);
/// println!("Cycles detected: {}", collapsed.non_trivial_count());
/// println!("Condensed edges: {}", collapsed.supernode_edges.len());
/// ```
///
/// # References
///
/// - CP-Algorithms: Strongly Connected Components and Condensation Graph
///   https://cp-algorithms.com/graph/strongly-connected-components.html
pub fn collapse_sccs(graph: &SqliteGraph) -> Result<SccCollapseResult, SqliteGraphError> {
    // Step 1: Compute SCCs using Tarjan's algorithm
    let scc_result = strongly_connected_components(graph)?;

    // Step 2: Handle empty graph
    if scc_result.components.is_empty() {
        return Ok(SccCollapseResult {
            node_to_supernode: AHashMap::new(),
            supernode_members: AHashMap::new(),
            supernode_edges: Vec::new(),
            num_sccs: 0,
        });
    }

    // Step 3: Build node_to_supernode and supernode_members mappings
    // Use the component index as the supernode ID (deterministic)
    let mut node_to_supernode: AHashMap<i64, i64> = AHashMap::new();
    let mut supernode_members: AHashMap<i64, AHashSet<i64>> = AHashMap::new();

    for (&node, &component_idx) in &scc_result.node_to_component {
        // Use component index as supernode ID (ensures deterministic output)
        let supernode_id = component_idx as i64;

        node_to_supernode.insert(node, supernode_id);

        supernode_members
            .entry(supernode_id)
            .or_default()
            .insert(node);
    }

    // Step 4: Build supernode edges (condensed graph)
    // Use AHashSet to deduplicate edges
    let mut edge_set: AHashSet<(i64, i64)> = AHashSet::new();

    for &from_node in &graph.all_entity_ids()? {
        if let Some(&from_supernode) = node_to_supernode.get(&from_node) {
            for &to_node in &graph.fetch_outgoing(from_node)? {
                if let Some(&to_supernode) = node_to_supernode.get(&to_node) {
                    // Only add edges between different supernodes (no self-loops)
                    if from_supernode != to_supernode {
                        edge_set.insert((from_supernode, to_supernode));
                    }
                }
            }
        }
    }

    // Step 5: Convert to sorted Vec for deterministic output
    let mut supernode_edges: Vec<(i64, i64)> = edge_set.into_iter().collect();
    supernode_edges.sort();
    supernode_edges.dedup();

    // Step 6: Return result
    Ok(SccCollapseResult {
        node_to_supernode,
        supernode_members,
        supernode_edges,
        num_sccs: scc_result.components.len(),
    })
}

/// Collapses SCCs with progress tracking.
///
/// Same algorithm as [`collapse_sccs`] but reports progress during execution.
/// Useful for long-running operations on large graphs.
///
/// # Arguments
/// * `graph` - The graph to analyze
/// * `progress` - Progress callback for reporting execution status
///
/// # Returns
/// `SccCollapseResult` containing the condensed graph and mappings.
///
/// # Progress Reporting
///
/// The callback receives:
/// - `current`: Current number of edges processed
/// - `total`: Total number of edges (if known)
/// - `message`: Progress status message
///
/// Progress milestones:
/// - After SCC computation: "SCC collapse: computed {count} SCCs"
/// - During edge enumeration: Every ~100 edges processed
/// - On completion: Calls `on_complete()`
///
/// # Example
///
/// ```rust,ignore
/// use sqlitegraph::{algo::collapse_sccs_with_progress, progress::ConsoleProgress};
///
/// let progress = ConsoleProgress::new();
/// let collapsed = collapse_sccs_with_progress(&graph, &progress)?;
/// // Output:
/// // SCC collapse: computed 5 SCCs [0 edges processed]
/// // SCC collapse: building condensed graph... [100 edges processed]
/// // Complete
/// ```
pub fn collapse_sccs_with_progress<F>(
    graph: &SqliteGraph,
    progress: &F,
) -> Result<SccCollapseResult, SqliteGraphError>
where
    F: ProgressCallback,
{
    // Step 1: Compute SCCs using Tarjan's algorithm
    let scc_result = strongly_connected_components(graph)?;

    progress.on_progress(
        0,
        None,
        &format!(
            "SCC collapse: computed {} SCCs",
            scc_result.components.len()
        ),
    );

    // Step 2: Handle empty graph
    if scc_result.components.is_empty() {
        progress.on_complete();
        return Ok(SccCollapseResult {
            node_to_supernode: AHashMap::new(),
            supernode_members: AHashMap::new(),
            supernode_edges: Vec::new(),
            num_sccs: 0,
        });
    }

    // Step 3: Build node_to_supernode and supernode_members mappings
    let mut node_to_supernode: AHashMap<i64, i64> = AHashMap::new();
    let mut supernode_members: AHashMap<i64, AHashSet<i64>> = AHashMap::new();

    for (&node, &component_idx) in &scc_result.node_to_component {
        let supernode_id = component_idx as i64;

        node_to_supernode.insert(node, supernode_id);

        supernode_members
            .entry(supernode_id)
            .or_default()
            .insert(node);
    }

    progress.on_progress(0, None, "SCC collapse: building condensed graph...");

    // Step 4: Build supernode edges with progress tracking
    let mut edge_set: AHashSet<(i64, i64)> = AHashSet::new();
    let all_nodes = graph.all_entity_ids()?;
    let total_edges_hint = all_nodes.len().saturating_mul(2); // Rough estimate

    let mut edges_processed = 0;

    for &from_node in &all_nodes {
        if let Some(&from_supernode) = node_to_supernode.get(&from_node) {
            for &to_node in &graph.fetch_outgoing(from_node)? {
                edges_processed += 1;

                if let Some(&to_supernode) = node_to_supernode.get(&to_node) {
                    if from_supernode != to_supernode {
                        edge_set.insert((from_supernode, to_supernode));
                    }
                }
            }
        }

        // Report progress every ~100 edges
        if edges_processed % 100 == 0 {
            progress.on_progress(
                edges_processed,
                Some(total_edges_hint),
                &format!(
                    "SCC collapse: processed {} edges, {} unique supernode edges",
                    edges_processed,
                    edge_set.len()
                ),
            );
        }
    }

    // Step 5: Convert to sorted Vec
    let mut supernode_edges: Vec<(i64, i64)> = edge_set.into_iter().collect();
    supernode_edges.sort();
    supernode_edges.dedup();

    // Report completion
    progress.on_complete();

    // Step 6: Return result
    Ok(SccCollapseResult {
        node_to_supernode,
        supernode_members,
        supernode_edges,
        num_sccs: scc_result.components.len(),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{GraphEdge, GraphEntity};

    /// Helper: Create empty graph
    fn create_empty_graph() -> SqliteGraph {
        SqliteGraph::open_in_memory().expect("Failed to create graph")
    }

    /// Helper: Create graph with single node
    fn create_single_node_graph() -> SqliteGraph {
        let graph = SqliteGraph::open_in_memory().expect("Failed to create graph");

        let entity = GraphEntity {
            id: 0,
            kind: "node".to_string(),
            name: "single".to_string(),
            file_path: Some("single.rs".to_string()),
            data: serde_json::json!({}),
        };
        graph
            .insert_entity(&entity)
            .expect("Failed to insert entity");

        graph
    }

    /// Helper: Create DAG: 0 -> 1 -> 2 -> 3 (all trivial SCCs)
    fn create_dag() -> SqliteGraph {
        let graph = SqliteGraph::open_in_memory().expect("Failed to create graph");

        // Create 4 nodes
        for i in 0..4 {
            let entity = GraphEntity {
                id: 0,
                kind: "node".to_string(),
                name: format!("node_{}", i),
                file_path: Some(format!("node_{}.rs", i)),
                data: serde_json::json!({"index": i}),
            };
            graph
                .insert_entity(&entity)
                .expect("Failed to insert entity");
        }

        let entity_ids: Vec<i64> = graph.all_entity_ids().expect("Failed to get IDs");

        // Create chain: 0 -> 1 -> 2 -> 3
        for i in 0..entity_ids.len().saturating_sub(1) {
            let edge = GraphEdge {
                id: 0,
                from_id: entity_ids[i],
                to_id: entity_ids[i + 1],
                edge_type: "next".to_string(),
                data: serde_json::json!({}),
            };
            graph.insert_edge(&edge).expect("Failed to insert edge");
        }

        graph
    }

    /// Helper: Create mutual recursion: 0 <-> 1, and 2 -> 3 -> 4 (linear)
    fn create_mutual_recursion_graph() -> SqliteGraph {
        let graph = SqliteGraph::open_in_memory().expect("Failed to create graph");

        // Create 5 nodes
        for i in 0..5 {
            let entity = GraphEntity {
                id: 0,
                kind: "node".to_string(),
                name: format!("node_{}", i),
                file_path: Some(format!("node_{}.rs", i)),
                data: serde_json::json!({"index": i}),
            };
            graph
                .insert_entity(&entity)
                .expect("Failed to insert entity");
        }

        let entity_ids: Vec<i64> = graph.all_entity_ids().expect("Failed to get IDs");

        // Create mutual recursion: 0 <-> 1, and 2 -> 3 -> 4 (linear)
        let edges = vec![(0, 1), (1, 0), (2, 3), (3, 4)];
        for (from_idx, to_idx) in edges {
            let edge = GraphEdge {
                id: 0,
                from_id: entity_ids[from_idx],
                to_id: entity_ids[to_idx],
                edge_type: "edge".to_string(),
                data: serde_json::json!({}),
            };
            graph.insert_edge(&edge).expect("Failed to insert edge");
        }

        graph
    }

    /// Helper: Create triangle SCC: 0 -> 1 -> 2 -> 0
    fn create_triangle_scc() -> SqliteGraph {
        let graph = SqliteGraph::open_in_memory().expect("Failed to create graph");

        // Create 3 nodes
        for i in 0..3 {
            let entity = GraphEntity {
                id: 0,
                kind: "node".to_string(),
                name: format!("node_{}", i),
                file_path: Some(format!("node_{}.rs", i)),
                data: serde_json::json!({"index": i}),
            };
            graph
                .insert_entity(&entity)
                .expect("Failed to insert entity");
        }

        let entity_ids: Vec<i64> = graph.all_entity_ids().expect("Failed to get IDs");

        // Create triangle: 0 -> 1 -> 2 -> 0
        let cycle = vec![(0, 1), (1, 2), (2, 0)];
        for (from_idx, to_idx) in cycle {
            let edge = GraphEdge {
                id: 0,
                from_id: entity_ids[from_idx],
                to_id: entity_ids[to_idx],
                edge_type: "cycle".to_string(),
                data: serde_json::json!({}),
            };
            graph.insert_edge(&edge).expect("Failed to insert edge");
        }

        graph
    }

    // Tests for collapse_sccs

    #[test]
    fn test_collapse_sccs_empty_graph() {
        // Scenario: Empty graph
        // Expected: Returns empty SccCollapseResult
        let graph = create_empty_graph();
        let result = collapse_sccs(&graph);

        assert!(result.is_ok());
        let collapsed = result.unwrap();

        assert_eq!(collapsed.num_sccs, 0);
        assert_eq!(collapsed.node_to_supernode.len(), 0);
        assert_eq!(collapsed.supernode_members.len(), 0);
        assert_eq!(collapsed.supernode_edges.len(), 0);
        assert_eq!(collapsed.non_trivial_count(), 0);
    }

    #[test]
    fn test_collapse_sccs_single_node() {
        // Scenario: Single node, no edges
        // Expected: One trivial SCC
        let graph = create_single_node_graph();
        let result = collapse_sccs(&graph);

        assert!(result.is_ok());
        let collapsed = result.unwrap();

        assert_eq!(collapsed.num_sccs, 1);
        assert_eq!(collapsed.node_to_supernode.len(), 1);
        assert_eq!(collapsed.supernode_members.len(), 1);
        assert_eq!(collapsed.supernode_edges.len(), 0);

        // Verify it's a trivial SCC
        assert_eq!(collapsed.non_trivial_count(), 0);

        let entity_ids: Vec<i64> = graph.all_entity_ids().expect("Failed to get IDs");
        let node_id = entity_ids[0];

        // Check node_to_supernode mapping
        let supernode = collapsed.supernode_for(node_id);
        assert!(supernode.is_some(), "Node should have a supernode");

        // Check supernode_members mapping
        let members = collapsed.members_of(supernode.unwrap());
        assert!(members.is_some(), "Supernode should have members");
        assert_eq!(members.unwrap().len(), 1, "SCC should have 1 member");

        // Check is_trivial
        assert!(
            collapsed.is_trivial(supernode.unwrap()),
            "Single node should be trivial"
        );
    }

    #[test]
    fn test_collapse_sccs_dag() {
        // Scenario: DAG 0 -> 1 -> 2 -> 3
        // Expected: All nodes are separate supernodes (4 trivial SCCs)
        let graph = create_dag();
        let result = collapse_sccs(&graph);

        assert!(result.is_ok());
        let collapsed = result.unwrap();

        // All 4 nodes are separate SCCs
        assert_eq!(collapsed.num_sccs, 4);
        assert_eq!(collapsed.node_to_supernode.len(), 4);
        assert_eq!(collapsed.non_trivial_count(), 0);

        // 3 edges in condensed graph (same as original)
        assert_eq!(collapsed.supernode_edges.len(), 3);

        // Verify no self-loops
        for &(from, to) in &collapsed.supernode_edges {
            assert_ne!(from, to, "Condensed graph should have no self-loops");
        }

        // Verify all SCCs are trivial
        for (&supernode, members) in &collapsed.supernode_members {
            assert!(
                collapsed.is_trivial(supernode),
                "DAG nodes should be trivial SCCs"
            );
            assert_eq!(members.len(), 1, "Each SCC should have 1 member");
        }
    }

    #[test]
    fn test_collapse_sccs_mutual_recursion() {
        // Scenario: Mutual recursion 0 <-> 1, plus chain 2 -> 3 -> 4
        // Expected: {0, 1} becomes one supernode, {2}, {3}, {4} are separate
        let graph = create_mutual_recursion_graph();
        let result = collapse_sccs(&graph);

        assert!(result.is_ok());
        let collapsed = result.unwrap();

        // 4 SCCs: {0,1}, {2}, {3}, {4}
        assert_eq!(collapsed.num_sccs, 4);
        assert_eq!(collapsed.node_to_supernode.len(), 5);

        // 1 non-trivial SCC (the mutual recursion)
        assert_eq!(collapsed.non_trivial_count(), 1);

        let entity_ids: Vec<i64> = graph.all_entity_ids().expect("Failed to get IDs");

        // Nodes 0 and 1 should be in the same SCC
        let scc0 = collapsed.supernode_for(entity_ids[0]);
        let scc1 = collapsed.supernode_for(entity_ids[1]);
        assert_eq!(scc0, scc1, "Nodes 0 and 1 should be in same SCC");

        // Nodes 2, 3, 4 should each be in their own SCC
        let scc2 = collapsed.supernode_for(entity_ids[2]);
        let scc3 = collapsed.supernode_for(entity_ids[3]);
        let scc4 = collapsed.supernode_for(entity_ids[4]);

        assert_ne!(scc2, scc0, "Node 2 should not be in SCC 0/1");
        assert_ne!(scc3, scc2, "Node 3 should not be in SCC 2");
        assert_ne!(scc4, scc3, "Node 4 should not be in SCC 3");

        // Verify the mutual recursion SCC is non-trivial
        if let Some(scc_id) = scc0 {
            assert!(
                !collapsed.is_trivial(scc_id),
                "Mutual recursion SCC should be non-trivial"
            );
            if let Some(members) = collapsed.members_of(scc_id) {
                assert_eq!(
                    members.len(),
                    2,
                    "Mutual recursion SCC should have 2 members"
                );
            }
        }

        // Verify condensed edges (should have no self-loops)
        for &(from, to) in &collapsed.supernode_edges {
            assert_ne!(from, to, "Condensed graph should have no self-loops");
        }
    }

    #[test]
    fn test_collapse_sccs_triangle() {
        // Scenario: Triangle 0 -> 1 -> 2 -> 0
        // Expected: All three nodes become one supernode
        let graph = create_triangle_scc();
        let result = collapse_sccs(&graph);

        assert!(result.is_ok());
        let collapsed = result.unwrap();

        // 1 SCC containing all 3 nodes
        assert_eq!(collapsed.num_sccs, 1);
        assert_eq!(collapsed.node_to_supernode.len(), 3);

        // 1 non-trivial SCC
        assert_eq!(collapsed.non_trivial_count(), 1);

        let entity_ids: Vec<i64> = graph.all_entity_ids().expect("Failed to get IDs");

        // All nodes should be in the same SCC
        let scc0 = collapsed.supernode_for(entity_ids[0]);
        let scc1 = collapsed.supernode_for(entity_ids[1]);
        let scc2 = collapsed.supernode_for(entity_ids[2]);

        assert_eq!(scc0, scc1, "Nodes 0 and 1 should be in same SCC");
        assert_eq!(scc1, scc2, "Nodes 1 and 2 should be in same SCC");

        // No edges in condensed graph (single supernode)
        assert_eq!(collapsed.supernode_edges.len(), 0);

        // Verify SCC is non-trivial
        if let Some(scc_id) = scc0 {
            assert!(
                !collapsed.is_trivial(scc_id),
                "Triangle SCC should be non-trivial"
            );
            if let Some(members) = collapsed.members_of(scc_id) {
                assert_eq!(members.len(), 3, "Triangle SCC should have 3 members");
            }
        }
    }

    #[test]
    fn test_collapse_sccs_no_self_loops() {
        // Scenario: Graph with mutual recursion
        // Expected: Condensed graph has no self-loops
        let graph = create_mutual_recursion_graph();
        let result = collapse_sccs(&graph);

        assert!(result.is_ok());
        let collapsed = result.unwrap();

        // Verify no self-loops in condensed graph
        for &(from, to) in &collapsed.supernode_edges {
            assert_ne!(from, to, "Condensed DAG should not have self-loops");
        }
    }

    #[test]
    fn test_collapse_sccs_bidirectional_mapping() {
        // Scenario: Mutual recursion graph
        // Expected: node_to_supernode and supernode_members are consistent
        let graph = create_mutual_recursion_graph();
        let result = collapse_sccs(&graph);

        assert!(result.is_ok());
        let collapsed = result.unwrap();

        let entity_ids: Vec<i64> = graph.all_entity_ids().expect("Failed to get IDs");

        // For each node, verify bidirectional mapping consistency
        for &node in &entity_ids {
            if let Some(supernode) = collapsed.supernode_for(node) {
                if let Some(members) = collapsed.members_of(supernode) {
                    assert!(
                        members.contains(&node),
                        "Supernode {} should contain node {}",
                        supernode,
                        node
                    );
                }
            }
        }

        // Verify each supernode's members map back to it
        for (&supernode, members) in &collapsed.supernode_members {
            for &member in members {
                let mapped = collapsed.supernode_for(member);
                assert_eq!(
                    Some(supernode),
                    mapped,
                    "Node {} should map back to supernode {}",
                    member,
                    supernode
                );
            }
        }
    }

    #[test]
    fn test_collapse_sccs_deterministic_edges() {
        // Scenario: Graph with multiple edges
        // Expected: Edges are sorted deterministically
        let graph = create_dag();
        let result = collapse_sccs(&graph);

        assert!(result.is_ok());
        let collapsed = result.unwrap();

        // Check that edges are sorted
        let mut edges_copy = collapsed.supernode_edges.clone();
        edges_copy.sort();

        assert_eq!(
            collapsed.supernode_edges, edges_copy,
            "Edges should be sorted"
        );

        // Check that there are no duplicates
        let mut unique_edges = collapsed.supernode_edges.clone();
        unique_edges.dedup();

        assert_eq!(
            collapsed.supernode_edges.len(),
            unique_edges.len(),
            "Edges should be deduplicated"
        );
    }

    // Tests for SccCollapseResult methods

    #[test]
    fn test_supernode_for() {
        // Scenario: Query supernode for existing and non-existent nodes
        // Expected: Returns Some(supernode) for existing, None for non-existent
        let graph = create_mutual_recursion_graph();
        let collapsed = collapse_sccs(&graph).expect("Failed");

        let entity_ids: Vec<i64> = graph.all_entity_ids().expect("Failed to get IDs");

        // Existing node should return Some
        let supernode = collapsed.supernode_for(entity_ids[0]);
        assert!(supernode.is_some(), "Existing node should have supernode");

        // Non-existent node should return None
        let non_existent = collapsed.supernode_for(99999);
        assert!(
            non_existent.is_none(),
            "Non-existent node should return None"
        );
    }

    #[test]
    fn test_members_of() {
        // Scenario: Query members for existing and non-existent supernodes
        // Expected: Returns Some(members) for existing, None for non-existent
        let graph = create_mutual_recursion_graph();
        let collapsed = collapse_sccs(&graph).expect("Failed");

        let entity_ids: Vec<i64> = graph.all_entity_ids().expect("Failed to get IDs");

        // Get the supernode for node 0
        if let Some(supernode) = collapsed.supernode_for(entity_ids[0]) {
            // Existing supernode should return Some
            let members = collapsed.members_of(supernode);
            assert!(members.is_some(), "Existing supernode should have members");
            assert_eq!(members.unwrap().len(), 2, "Should have 2 members");
        }

        // Non-existent supernode should return None
        let non_existent = collapsed.members_of(99999);
        assert!(
            non_existent.is_none(),
            "Non-existent supernode should return None"
        );
    }

    #[test]
    fn test_is_trivial() {
        // Scenario: Check trivial vs non-trivial SCCs
        // Expected: Single node SCCs are trivial, multi-node are not
        let graph = create_mutual_recursion_graph();
        let collapsed = collapse_sccs(&graph).expect("Failed");

        let entity_ids: Vec<i64> = graph.all_entity_ids().expect("Failed to get IDs");

        // Find the mutual recursion SCC (nodes 0 and 1)
        let scc0 = collapsed.supernode_for(entity_ids[0]);
        let scc2 = collapsed.supernode_for(entity_ids[2]);

        if let Some(scc_id) = scc0 {
            // Multi-node SCC should be non-trivial
            assert!(
                !collapsed.is_trivial(scc_id),
                "Multi-node SCC should not be trivial"
            );
        }

        if let Some(scc_id) = scc2 {
            // Single node SCC should be trivial
            assert!(
                collapsed.is_trivial(scc_id),
                "Single node SCC should be trivial"
            );
        }

        // Non-existent SCC should return false (not found)
        assert!(
            !collapsed.is_trivial(99999),
            "Non-existent SCC should return false"
        );
    }

    #[test]
    fn test_non_trivial_count() {
        // Scenario: Count non-trivial SCCs
        // Expected: Returns correct count of multi-node SCCs
        let graph = create_mutual_recursion_graph();
        let collapsed = collapse_sccs(&graph).expect("Failed");

        // Should have 1 non-trivial SCC (the mutual recursion)
        assert_eq!(collapsed.non_trivial_count(), 1);

        // DAG should have 0 non-trivial SCCs
        let dag = create_dag();
        let dag_collapsed = collapse_sccs(&dag).expect("Failed");
        assert_eq!(dag_collapsed.non_trivial_count(), 0);

        // Triangle should have 1 non-trivial SCC
        let triangle = create_triangle_scc();
        let triangle_collapsed = collapse_sccs(&triangle).expect("Failed");
        assert_eq!(triangle_collapsed.non_trivial_count(), 1);
    }

    #[test]
    fn test_non_trivial_nodes() {
        // Scenario: Get all nodes in non-trivial SCCs
        // Expected: Returns set of nodes participating in cycles
        let graph = create_mutual_recursion_graph();
        let collapsed = collapse_sccs(&graph).expect("Failed");

        let cyclic_nodes = collapsed.non_trivial_nodes();

        // Should have 2 cyclic nodes (0 and 1 in mutual recursion)
        assert_eq!(cyclic_nodes.len(), 2);

        let entity_ids: Vec<i64> = graph.all_entity_ids().expect("Failed to get IDs");

        // Nodes 0 and 1 should be in the set (they form the mutual recursion)
        assert!(cyclic_nodes.contains(&entity_ids[0]));
        assert!(cyclic_nodes.contains(&entity_ids[1]));

        // Nodes 2, 3, 4 should not be in the set (they're in a chain)
        assert!(!cyclic_nodes.contains(&entity_ids[2]));
        assert!(!cyclic_nodes.contains(&entity_ids[3]));
        assert!(!cyclic_nodes.contains(&entity_ids[4]));
    }

    // Tests for progress variant

    #[test]
    fn test_collapse_sccs_with_progress() {
        // Scenario: Progress variant matches non-progress variant
        // Expected: Same results
        use crate::progress::NoProgress;

        let graph = create_mutual_recursion_graph();

        let progress = NoProgress;
        let result_with = collapse_sccs_with_progress(&graph, &progress).expect("Failed");
        let result_without = collapse_sccs(&graph).expect("Failed");

        assert_eq!(
            result_with.num_sccs, result_without.num_sccs,
            "Progress and non-progress results should match"
        );

        assert_eq!(
            result_with.supernode_edges.len(),
            result_without.supernode_edges.len(),
            "Edges should match"
        );

        assert_eq!(
            result_with.non_trivial_count(),
            result_without.non_trivial_count(),
            "Non-trivial count should match"
        );
    }

    #[test]
    fn test_collapse_sccs_empty_with_progress() {
        // Scenario: Empty graph with progress
        // Expected: Returns empty result without error
        use crate::progress::NoProgress;

        let graph = create_empty_graph();

        let progress = NoProgress;
        let result = collapse_sccs_with_progress(&graph, &progress);

        assert!(result.is_ok());
        let collapsed = result.unwrap();

        assert_eq!(collapsed.num_sccs, 0);
        assert_eq!(collapsed.supernode_edges.len(), 0);
    }

    #[test]
    fn test_condensation_is_dag() {
        // Scenario: Verify condensation graph is acyclic
        // Expected: No cycles in supernode_edges
        let graph = create_mutual_recursion_graph();
        let collapsed = collapse_sccs(&graph).expect("Failed");

        // Build adjacency map for cycle detection
        let mut adj: AHashMap<i64, Vec<i64>> = AHashMap::new();
        for &(from, to) in &collapsed.supernode_edges {
            adj.entry(from).or_insert_with(Vec::new).push(to);
        }

        // Simple DFS cycle detection
        let mut visited = AHashSet::new();
        let mut rec_stack = AHashSet::new();

        fn has_cycle(
            node: i64,
            adj: &AHashMap<i64, Vec<i64>>,
            visited: &mut AHashSet<i64>,
            rec_stack: &mut AHashSet<i64>,
        ) -> bool {
            visited.insert(node);
            rec_stack.insert(node);

            if let Some(neighbors) = adj.get(&node) {
                for &neighbor in neighbors {
                    if !visited.contains(&neighbor) {
                        if has_cycle(neighbor, adj, visited, rec_stack) {
                            return true;
                        }
                    } else if rec_stack.contains(&neighbor) {
                        return true;
                    }
                }
            }

            rec_stack.remove(&node);
            false
        }

        // Check all supernodes
        for (&supernode, _) in &collapsed.supernode_members {
            if !visited.contains(&supernode) {
                assert!(
                    !has_cycle(supernode, &adj, &mut visited, &mut rec_stack),
                    "Condensation graph should be acyclic"
                );
            }
        }
    }
}