Skip to main content

fallow_graph/graph/
cycles.rs

1//! Circular dependency detection via Tarjan's SCC algorithm + elementary cycle enumeration.
2
3use std::ops::Range;
4
5use fixedbitset::FixedBitSet;
6use rustc_hash::FxHashSet;
7
8use fallow_types::discover::FileId;
9
10use super::ModuleGraph;
11use super::types::ModuleNode;
12
13impl ModuleGraph {
14    /// Find all circular dependency cycles in the module graph.
15    ///
16    /// Uses an iterative implementation of Tarjan's strongly connected components
17    /// algorithm (O(V + E)) to find all SCCs with 2 or more nodes. Each such SCC
18    /// represents a set of files involved in a circular dependency.
19    ///
20    /// Returns cycles sorted by length (shortest first), with files within each
21    /// cycle sorted by path for deterministic output.
22    ///
23    /// # Panics
24    ///
25    /// Panics if the internal file-to-path lookup is inconsistent with the module list.
26    #[must_use]
27    #[expect(
28        clippy::excessive_nesting,
29        reason = "Tarjan's SCC requires deep nesting"
30    )]
31    #[expect(
32        clippy::cast_possible_truncation,
33        reason = "file count is bounded by project size, well under u32::MAX"
34    )]
35    pub fn find_cycles(&self) -> Vec<Vec<FileId>> {
36        let n = self.modules.len();
37        if n == 0 {
38            return Vec::new();
39        }
40
41        // Tarjan's SCC state
42        let mut index_counter: u32 = 0;
43        let mut indices: Vec<u32> = vec![u32::MAX; n]; // u32::MAX = undefined
44        let mut lowlinks: Vec<u32> = vec![0; n];
45        let mut on_stack = FixedBitSet::with_capacity(n);
46        let mut stack: Vec<usize> = Vec::new();
47        let mut sccs: Vec<Vec<FileId>> = Vec::new();
48
49        // Iterative DFS stack frame
50        struct Frame {
51            node: usize,
52            succ_pos: usize,
53            succ_end: usize,
54        }
55
56        // Pre-collect all successors (deduplicated) into a flat vec for cache-friendly access.
57        let mut all_succs: Vec<usize> = Vec::with_capacity(self.edges.len());
58        let mut succ_ranges: Vec<Range<usize>> = Vec::with_capacity(n);
59        let mut seen_set = FxHashSet::default();
60        for module in &self.modules {
61            let start = all_succs.len();
62            seen_set.clear();
63            for edge in &self.edges[module.edge_range.clone()] {
64                // Skip edges where all imports are type-only (`import type`).
65                // Type-only imports are erased at compile time and cannot cause
66                // runtime circular dependency issues.
67                if edge.symbols.iter().all(|s| s.is_type_only) {
68                    continue;
69                }
70                let target = edge.target.0 as usize;
71                if target < n && seen_set.insert(target) {
72                    all_succs.push(target);
73                }
74            }
75            let end = all_succs.len();
76            succ_ranges.push(start..end);
77        }
78
79        let mut dfs_stack: Vec<Frame> = Vec::new();
80
81        for start_node in 0..n {
82            if indices[start_node] != u32::MAX {
83                continue;
84            }
85
86            // Push the starting node
87            indices[start_node] = index_counter;
88            lowlinks[start_node] = index_counter;
89            index_counter += 1;
90            on_stack.insert(start_node);
91            stack.push(start_node);
92
93            let range = &succ_ranges[start_node];
94            dfs_stack.push(Frame {
95                node: start_node,
96                succ_pos: range.start,
97                succ_end: range.end,
98            });
99
100            while let Some(frame) = dfs_stack.last_mut() {
101                if frame.succ_pos < frame.succ_end {
102                    let w = all_succs[frame.succ_pos];
103                    frame.succ_pos += 1;
104
105                    if indices[w] == u32::MAX {
106                        // Tree edge: push w onto the DFS stack
107                        indices[w] = index_counter;
108                        lowlinks[w] = index_counter;
109                        index_counter += 1;
110                        on_stack.insert(w);
111                        stack.push(w);
112
113                        let range = &succ_ranges[w];
114                        dfs_stack.push(Frame {
115                            node: w,
116                            succ_pos: range.start,
117                            succ_end: range.end,
118                        });
119                    } else if on_stack.contains(w) {
120                        // Back edge: update lowlink
121                        let v = frame.node;
122                        lowlinks[v] = lowlinks[v].min(indices[w]);
123                    }
124                } else {
125                    // All successors processed — pop this frame
126                    let v = frame.node;
127                    let v_lowlink = lowlinks[v];
128                    let v_index = indices[v];
129                    dfs_stack.pop();
130
131                    // Update parent's lowlink
132                    if let Some(parent) = dfs_stack.last_mut() {
133                        lowlinks[parent.node] = lowlinks[parent.node].min(v_lowlink);
134                    }
135
136                    // If v is a root node, pop the SCC
137                    if v_lowlink == v_index {
138                        let mut scc = Vec::new();
139                        loop {
140                            let w = stack.pop().expect("SCC stack should not be empty");
141                            on_stack.set(w, false);
142                            scc.push(FileId(w as u32));
143                            if w == v {
144                                break;
145                            }
146                        }
147                        // Only report cycles of length >= 2
148                        if scc.len() >= 2 {
149                            sccs.push(scc);
150                        }
151                    }
152                }
153            }
154        }
155
156        self.enumerate_cycles_from_sccs(&sccs, &all_succs, &succ_ranges)
157    }
158
159    /// Enumerate individual elementary cycles from SCCs and return sorted results.
160    #[expect(
161        clippy::cast_possible_truncation,
162        reason = "file count is bounded by project size, well under u32::MAX"
163    )]
164    fn enumerate_cycles_from_sccs(
165        &self,
166        sccs: &[Vec<FileId>],
167        all_succs: &[usize],
168        succ_ranges: &[Range<usize>],
169    ) -> Vec<Vec<FileId>> {
170        const MAX_CYCLES_PER_SCC: usize = 20;
171
172        let succs = SuccessorMap {
173            all_succs,
174            succ_ranges,
175            modules: &self.modules,
176        };
177
178        let mut result: Vec<Vec<FileId>> = Vec::new();
179        let mut seen_cycles: FxHashSet<Vec<u32>> = FxHashSet::default();
180
181        for scc in sccs {
182            if scc.len() == 2 {
183                let mut cycle = vec![scc[0].0 as usize, scc[1].0 as usize];
184                // Canonical: smallest path first
185                if self.modules[cycle[1]].path < self.modules[cycle[0]].path {
186                    cycle.swap(0, 1);
187                }
188                let key: Vec<u32> = cycle.iter().map(|&n| n as u32).collect();
189                if seen_cycles.insert(key) {
190                    result.push(cycle.into_iter().map(|n| FileId(n as u32)).collect());
191                }
192                continue;
193            }
194
195            let scc_nodes: Vec<usize> = scc.iter().map(|id| id.0 as usize).collect();
196            let elementary = enumerate_elementary_cycles(&scc_nodes, &succs, MAX_CYCLES_PER_SCC);
197
198            for cycle in elementary {
199                let key: Vec<u32> = cycle.iter().map(|&n| n as u32).collect();
200                if seen_cycles.insert(key) {
201                    result.push(cycle.into_iter().map(|n| FileId(n as u32)).collect());
202                }
203            }
204        }
205
206        // Sort: shortest first, then by first file path
207        result.sort_by(|a, b| {
208            a.len().cmp(&b.len()).then_with(|| {
209                self.modules[a[0].0 as usize]
210                    .path
211                    .cmp(&self.modules[b[0].0 as usize].path)
212            })
213        });
214
215        result
216    }
217}
218
219/// Rotate a cycle so the node with the smallest path is first (canonical form for dedup).
220fn canonical_cycle(cycle: &[usize], modules: &[ModuleNode]) -> Vec<usize> {
221    if cycle.is_empty() {
222        return Vec::new();
223    }
224    let min_pos = cycle
225        .iter()
226        .enumerate()
227        .min_by(|(_, a), (_, b)| modules[**a].path.cmp(&modules[**b].path))
228        .map_or(0, |(i, _)| i);
229    let mut result = cycle[min_pos..].to_vec();
230    result.extend_from_slice(&cycle[..min_pos]);
231    result
232}
233
234/// DFS frame for iterative cycle finding.
235struct CycleFrame {
236    succ_pos: usize,
237    succ_end: usize,
238}
239
240/// Pre-collected, deduplicated successor data for cache-friendly graph traversal.
241struct SuccessorMap<'a> {
242    all_succs: &'a [usize],
243    succ_ranges: &'a [Range<usize>],
244    modules: &'a [ModuleNode],
245}
246
247/// Record a cycle in canonical form if not already seen.
248#[expect(
249    clippy::cast_possible_truncation,
250    reason = "file count is bounded by project size, well under u32::MAX"
251)]
252fn try_record_cycle(
253    path: &[usize],
254    modules: &[ModuleNode],
255    seen: &mut FxHashSet<Vec<u32>>,
256    cycles: &mut Vec<Vec<usize>>,
257) {
258    let canonical = canonical_cycle(path, modules);
259    let key: Vec<u32> = canonical.iter().map(|&n| n as u32).collect();
260    if seen.insert(key) {
261        cycles.push(canonical);
262    }
263}
264
265/// Run a bounded DFS from `start`, looking for elementary cycles of exactly `depth_limit` nodes.
266///
267/// Appends any newly found cycles to `cycles` (deduped via `seen`).
268/// Stops early once `cycles.len() >= max_cycles`.
269fn dfs_find_cycles_from(
270    start: usize,
271    depth_limit: usize,
272    scc_set: &FxHashSet<usize>,
273    succs: &SuccessorMap<'_>,
274    max_cycles: usize,
275    seen: &mut FxHashSet<Vec<u32>>,
276    cycles: &mut Vec<Vec<usize>>,
277) {
278    let mut path: Vec<usize> = vec![start];
279    let mut path_set = FixedBitSet::with_capacity(succs.modules.len());
280    path_set.insert(start);
281
282    let range = &succs.succ_ranges[start];
283    let mut dfs: Vec<CycleFrame> = vec![CycleFrame {
284        succ_pos: range.start,
285        succ_end: range.end,
286    }];
287
288    while let Some(frame) = dfs.last_mut() {
289        if cycles.len() >= max_cycles {
290            return;
291        }
292
293        if frame.succ_pos >= frame.succ_end {
294            // Backtrack: all successors exhausted for this frame
295            dfs.pop();
296            if path.len() > 1 {
297                let removed = path.pop().unwrap();
298                path_set.set(removed, false);
299            }
300            continue;
301        }
302
303        let w = succs.all_succs[frame.succ_pos];
304        frame.succ_pos += 1;
305
306        // Only follow edges within this SCC
307        if !scc_set.contains(&w) {
308            continue;
309        }
310
311        // Found an elementary cycle at exactly this depth
312        if w == start && path.len() >= 2 && path.len() == depth_limit {
313            try_record_cycle(&path, succs.modules, seen, cycles);
314            continue;
315        }
316
317        // Skip if already on current path or beyond depth limit
318        if path_set.contains(w) || path.len() >= depth_limit {
319            continue;
320        }
321
322        // Extend path
323        path.push(w);
324        path_set.insert(w);
325
326        let range = &succs.succ_ranges[w];
327        dfs.push(CycleFrame {
328            succ_pos: range.start,
329            succ_end: range.end,
330        });
331    }
332}
333
334/// Enumerate individual elementary cycles within an SCC using depth-limited DFS.
335///
336/// Uses iterative deepening: first finds all 2-node cycles, then 3-node, etc.
337/// This ensures the shortest, most actionable cycles are always found first.
338/// Stops after `max_cycles` total cycles to bound work on dense SCCs.
339fn enumerate_elementary_cycles(
340    scc_nodes: &[usize],
341    succs: &SuccessorMap<'_>,
342    max_cycles: usize,
343) -> Vec<Vec<usize>> {
344    let scc_set: FxHashSet<usize> = scc_nodes.iter().copied().collect();
345    let mut cycles: Vec<Vec<usize>> = Vec::new();
346    let mut seen: FxHashSet<Vec<u32>> = FxHashSet::default();
347
348    // Sort start nodes by path for deterministic enumeration order
349    let mut sorted_nodes: Vec<usize> = scc_nodes.to_vec();
350    sorted_nodes.sort_by(|a, b| succs.modules[*a].path.cmp(&succs.modules[*b].path));
351
352    // Iterative deepening: increase max depth from 2 up to SCC size
353    let max_depth = scc_nodes.len().min(12); // Cap depth to avoid very long cycles
354    for depth_limit in 2..=max_depth {
355        if cycles.len() >= max_cycles {
356            break;
357        }
358
359        for &start in &sorted_nodes {
360            if cycles.len() >= max_cycles {
361                break;
362            }
363
364            dfs_find_cycles_from(
365                start,
366                depth_limit,
367                &scc_set,
368                succs,
369                max_cycles,
370                &mut seen,
371                &mut cycles,
372            );
373        }
374    }
375
376    cycles
377}
378
379#[cfg(test)]
380mod tests {
381    use std::ops::Range;
382    use std::path::PathBuf;
383
384    use rustc_hash::FxHashSet;
385
386    use crate::graph::types::ModuleNode;
387    use crate::resolve::{ResolveResult, ResolvedImport, ResolvedModule};
388    use fallow_types::discover::{DiscoveredFile, EntryPoint, EntryPointSource, FileId};
389    use fallow_types::extract::{ExportName, ImportInfo, ImportedName};
390
391    use super::{
392        ModuleGraph, SuccessorMap, canonical_cycle, dfs_find_cycles_from,
393        enumerate_elementary_cycles, try_record_cycle,
394    };
395
396    /// Helper: build a graph from files+edges, no entry points needed for cycle detection.
397    #[expect(
398        clippy::cast_possible_truncation,
399        reason = "test file counts are trivially small"
400    )]
401    fn build_cycle_graph(file_count: usize, edges_spec: &[(u32, u32)]) -> ModuleGraph {
402        let files: Vec<DiscoveredFile> = (0..file_count)
403            .map(|i| DiscoveredFile {
404                id: FileId(i as u32),
405                path: PathBuf::from(format!("/project/file{i}.ts")),
406                size_bytes: 100,
407            })
408            .collect();
409
410        let resolved_modules: Vec<ResolvedModule> = (0..file_count)
411            .map(|i| {
412                let imports: Vec<ResolvedImport> = edges_spec
413                    .iter()
414                    .filter(|(src, _)| *src == i as u32)
415                    .map(|(_, tgt)| ResolvedImport {
416                        info: ImportInfo {
417                            source: format!("./file{tgt}"),
418                            imported_name: ImportedName::Named("x".to_string()),
419                            local_name: "x".to_string(),
420                            is_type_only: false,
421                            span: oxc_span::Span::new(0, 10),
422                            source_span: oxc_span::Span::default(),
423                        },
424                        target: ResolveResult::InternalModule(FileId(*tgt)),
425                    })
426                    .collect();
427
428                ResolvedModule {
429                    file_id: FileId(i as u32),
430                    path: PathBuf::from(format!("/project/file{i}.ts")),
431                    exports: vec![fallow_types::extract::ExportInfo {
432                        name: ExportName::Named("x".to_string()),
433                        local_name: Some("x".to_string()),
434                        is_type_only: false,
435                        is_public: false,
436                        span: oxc_span::Span::new(0, 20),
437                        members: vec![],
438                        super_class: None,
439                    }],
440                    re_exports: vec![],
441                    resolved_imports: imports,
442                    resolved_dynamic_imports: vec![],
443                    resolved_dynamic_patterns: vec![],
444                    member_accesses: vec![],
445                    whole_object_uses: vec![],
446                    has_cjs_exports: false,
447                    unused_import_bindings: FxHashSet::default(),
448                }
449            })
450            .collect();
451
452        let entry_points = vec![EntryPoint {
453            path: PathBuf::from("/project/file0.ts"),
454            source: EntryPointSource::PackageJsonMain,
455        }];
456
457        ModuleGraph::build(&resolved_modules, &entry_points, &files)
458    }
459
460    #[test]
461    fn find_cycles_empty_graph() {
462        let graph = ModuleGraph::build(&[], &[], &[]);
463        assert!(graph.find_cycles().is_empty());
464    }
465
466    #[test]
467    fn find_cycles_no_cycles() {
468        // A -> B -> C (no back edges)
469        let graph = build_cycle_graph(3, &[(0, 1), (1, 2)]);
470        assert!(graph.find_cycles().is_empty());
471    }
472
473    #[test]
474    fn find_cycles_simple_two_node_cycle() {
475        // A -> B -> A
476        let graph = build_cycle_graph(2, &[(0, 1), (1, 0)]);
477        let cycles = graph.find_cycles();
478        assert_eq!(cycles.len(), 1);
479        assert_eq!(cycles[0].len(), 2);
480    }
481
482    #[test]
483    fn find_cycles_three_node_cycle() {
484        // A -> B -> C -> A
485        let graph = build_cycle_graph(3, &[(0, 1), (1, 2), (2, 0)]);
486        let cycles = graph.find_cycles();
487        assert_eq!(cycles.len(), 1);
488        assert_eq!(cycles[0].len(), 3);
489    }
490
491    #[test]
492    fn find_cycles_self_import_ignored() {
493        // A -> A (self-import, should NOT be reported as a cycle).
494        // Reason: Tarjan's SCC only reports components with >= 2 nodes,
495        // so a single-node self-edge never forms a reportable cycle.
496        let graph = build_cycle_graph(1, &[(0, 0)]);
497        let cycles = graph.find_cycles();
498        assert!(
499            cycles.is_empty(),
500            "self-imports should not be reported as cycles"
501        );
502    }
503
504    #[test]
505    fn find_cycles_multiple_independent_cycles() {
506        // Cycle 1: A -> B -> A
507        // Cycle 2: C -> D -> C
508        // No connection between cycles
509        let graph = build_cycle_graph(4, &[(0, 1), (1, 0), (2, 3), (3, 2)]);
510        let cycles = graph.find_cycles();
511        assert_eq!(cycles.len(), 2);
512        // Both cycles should have length 2
513        assert!(cycles.iter().all(|c| c.len() == 2));
514    }
515
516    #[test]
517    fn find_cycles_linear_chain_with_back_edge() {
518        // A -> B -> C -> D -> B (cycle is B-C-D)
519        let graph = build_cycle_graph(4, &[(0, 1), (1, 2), (2, 3), (3, 1)]);
520        let cycles = graph.find_cycles();
521        assert_eq!(cycles.len(), 1);
522        assert_eq!(cycles[0].len(), 3);
523        // The cycle should contain files 1, 2, 3
524        let ids: Vec<u32> = cycles[0].iter().map(|f| f.0).collect();
525        assert!(ids.contains(&1));
526        assert!(ids.contains(&2));
527        assert!(ids.contains(&3));
528        assert!(!ids.contains(&0));
529    }
530
531    #[test]
532    fn find_cycles_overlapping_cycles_enumerated() {
533        // A -> B -> A, B -> C -> B => SCC is {A, B, C} but should report 2 elementary cycles
534        let graph = build_cycle_graph(3, &[(0, 1), (1, 0), (1, 2), (2, 1)]);
535        let cycles = graph.find_cycles();
536        assert_eq!(
537            cycles.len(),
538            2,
539            "should find 2 elementary cycles, not 1 SCC"
540        );
541        assert!(
542            cycles.iter().all(|c| c.len() == 2),
543            "both cycles should have length 2"
544        );
545    }
546
547    #[test]
548    fn find_cycles_deterministic_ordering() {
549        // Run twice with the same graph — results should be identical
550        let graph1 = build_cycle_graph(3, &[(0, 1), (1, 2), (2, 0)]);
551        let graph2 = build_cycle_graph(3, &[(0, 1), (1, 2), (2, 0)]);
552        let cycles1 = graph1.find_cycles();
553        let cycles2 = graph2.find_cycles();
554        assert_eq!(cycles1.len(), cycles2.len());
555        for (c1, c2) in cycles1.iter().zip(cycles2.iter()) {
556            let paths1: Vec<&PathBuf> = c1
557                .iter()
558                .map(|f| &graph1.modules[f.0 as usize].path)
559                .collect();
560            let paths2: Vec<&PathBuf> = c2
561                .iter()
562                .map(|f| &graph2.modules[f.0 as usize].path)
563                .collect();
564            assert_eq!(paths1, paths2);
565        }
566    }
567
568    #[test]
569    fn find_cycles_sorted_by_length() {
570        // Two cycles: A-B (len 2) and C-D-E (len 3)
571        let graph = build_cycle_graph(5, &[(0, 1), (1, 0), (2, 3), (3, 4), (4, 2)]);
572        let cycles = graph.find_cycles();
573        assert_eq!(cycles.len(), 2);
574        assert!(
575            cycles[0].len() <= cycles[1].len(),
576            "cycles should be sorted by length"
577        );
578    }
579
580    #[test]
581    fn find_cycles_large_cycle() {
582        // Chain of 10 nodes forming a single cycle: 0->1->2->...->9->0
583        let edges: Vec<(u32, u32)> = (0..10).map(|i| (i, (i + 1) % 10)).collect();
584        let graph = build_cycle_graph(10, &edges);
585        let cycles = graph.find_cycles();
586        assert_eq!(cycles.len(), 1);
587        assert_eq!(cycles[0].len(), 10);
588    }
589
590    #[test]
591    fn find_cycles_complex_scc_multiple_elementary() {
592        // A square: A->B, B->C, C->D, D->A, plus diagonal A->C
593        // Elementary cycles: A->B->C->D->A, A->C->D->A, and A->B->C->...
594        let graph = build_cycle_graph(4, &[(0, 1), (1, 2), (2, 3), (3, 0), (0, 2)]);
595        let cycles = graph.find_cycles();
596        // Should find multiple elementary cycles, not just one SCC of 4
597        assert!(
598            cycles.len() >= 2,
599            "should find at least 2 elementary cycles, got {}",
600            cycles.len()
601        );
602        // All cycles should be shorter than the full SCC
603        assert!(cycles.iter().all(|c| c.len() <= 4));
604    }
605
606    #[test]
607    fn find_cycles_no_duplicate_cycles() {
608        // Triangle: A->B->C->A — should find exactly 1 cycle, not duplicates
609        // from different DFS start points
610        let graph = build_cycle_graph(3, &[(0, 1), (1, 2), (2, 0)]);
611        let cycles = graph.find_cycles();
612        assert_eq!(cycles.len(), 1, "triangle should produce exactly 1 cycle");
613        assert_eq!(cycles[0].len(), 3);
614    }
615
616    // -----------------------------------------------------------------------
617    // Unit-level helpers for testing extracted functions directly
618    // -----------------------------------------------------------------------
619
620    /// Build lightweight `ModuleNode` stubs and successor data for unit tests.
621    ///
622    /// `edges_spec` is a list of (source, target) pairs (0-indexed).
623    /// Returns (modules, all_succs, succ_ranges) suitable for constructing a `SuccessorMap`.
624    #[expect(
625        clippy::cast_possible_truncation,
626        reason = "test file counts are trivially small"
627    )]
628    fn build_test_succs(
629        file_count: usize,
630        edges_spec: &[(usize, usize)],
631    ) -> (Vec<ModuleNode>, Vec<usize>, Vec<Range<usize>>) {
632        let modules: Vec<ModuleNode> = (0..file_count)
633            .map(|i| {
634                let mut node = ModuleNode {
635                    file_id: FileId(i as u32),
636                    path: PathBuf::from(format!("/project/file{i}.ts")),
637                    edge_range: 0..0,
638                    exports: vec![],
639                    re_exports: vec![],
640                    flags: ModuleNode::flags_from(i == 0, true, false),
641                };
642                node.set_reachable(true);
643                node
644            })
645            .collect();
646
647        let mut all_succs: Vec<usize> = Vec::new();
648        let mut succ_ranges: Vec<Range<usize>> = Vec::with_capacity(file_count);
649        for src in 0..file_count {
650            let start = all_succs.len();
651            let mut seen = FxHashSet::default();
652            for &(s, t) in edges_spec {
653                if s == src && t < file_count && seen.insert(t) {
654                    all_succs.push(t);
655                }
656            }
657            let end = all_succs.len();
658            succ_ranges.push(start..end);
659        }
660
661        (modules, all_succs, succ_ranges)
662    }
663
664    // -----------------------------------------------------------------------
665    // canonical_cycle tests
666    // -----------------------------------------------------------------------
667
668    #[test]
669    fn canonical_cycle_empty() {
670        let modules: Vec<ModuleNode> = vec![];
671        assert!(canonical_cycle(&[], &modules).is_empty());
672    }
673
674    #[test]
675    fn canonical_cycle_rotates_to_smallest_path() {
676        let (modules, _, _) = build_test_succs(3, &[]);
677        // Cycle [2, 0, 1] — file0 has the smallest path, so canonical is [0, 1, 2]
678        let result = canonical_cycle(&[2, 0, 1], &modules);
679        assert_eq!(result, vec![0, 1, 2]);
680    }
681
682    #[test]
683    fn canonical_cycle_already_canonical() {
684        let (modules, _, _) = build_test_succs(3, &[]);
685        let result = canonical_cycle(&[0, 1, 2], &modules);
686        assert_eq!(result, vec![0, 1, 2]);
687    }
688
689    #[test]
690    fn canonical_cycle_single_node() {
691        let (modules, _, _) = build_test_succs(1, &[]);
692        let result = canonical_cycle(&[0], &modules);
693        assert_eq!(result, vec![0]);
694    }
695
696    // -----------------------------------------------------------------------
697    // try_record_cycle tests
698    // -----------------------------------------------------------------------
699
700    #[test]
701    fn try_record_cycle_inserts_new_cycle() {
702        let (modules, _, _) = build_test_succs(3, &[]);
703        let mut seen = FxHashSet::default();
704        let mut cycles = Vec::new();
705
706        try_record_cycle(&[0, 1, 2], &modules, &mut seen, &mut cycles);
707        assert_eq!(cycles.len(), 1);
708        assert_eq!(cycles[0], vec![0, 1, 2]);
709    }
710
711    #[test]
712    fn try_record_cycle_deduplicates_rotated_cycle() {
713        // Same cycle in two rotations: [0,1,2] and [1,2,0]
714        // Both should canonicalize to the same key, so only one is recorded.
715        let (modules, _, _) = build_test_succs(3, &[]);
716        let mut seen = FxHashSet::default();
717        let mut cycles = Vec::new();
718
719        try_record_cycle(&[0, 1, 2], &modules, &mut seen, &mut cycles);
720        try_record_cycle(&[1, 2, 0], &modules, &mut seen, &mut cycles);
721        try_record_cycle(&[2, 0, 1], &modules, &mut seen, &mut cycles);
722
723        assert_eq!(
724            cycles.len(),
725            1,
726            "rotations of the same cycle should be deduped"
727        );
728    }
729
730    #[test]
731    fn try_record_cycle_single_node_self_loop() {
732        // A single-node "cycle" (self-loop) — should be recorded if passed in
733        let (modules, _, _) = build_test_succs(1, &[]);
734        let mut seen = FxHashSet::default();
735        let mut cycles = Vec::new();
736
737        try_record_cycle(&[0], &modules, &mut seen, &mut cycles);
738        assert_eq!(cycles.len(), 1);
739        assert_eq!(cycles[0], vec![0]);
740    }
741
742    #[test]
743    fn try_record_cycle_distinct_cycles_both_recorded() {
744        // Two genuinely different cycles
745        let (modules, _, _) = build_test_succs(4, &[]);
746        let mut seen = FxHashSet::default();
747        let mut cycles = Vec::new();
748
749        try_record_cycle(&[0, 1], &modules, &mut seen, &mut cycles);
750        try_record_cycle(&[2, 3], &modules, &mut seen, &mut cycles);
751
752        assert_eq!(cycles.len(), 2);
753    }
754
755    // -----------------------------------------------------------------------
756    // SuccessorMap construction tests
757    // -----------------------------------------------------------------------
758
759    #[test]
760    fn successor_map_empty_graph() {
761        let (modules, all_succs, succ_ranges) = build_test_succs(0, &[]);
762        let succs = SuccessorMap {
763            all_succs: &all_succs,
764            succ_ranges: &succ_ranges,
765            modules: &modules,
766        };
767        assert!(succs.all_succs.is_empty());
768        assert!(succs.succ_ranges.is_empty());
769    }
770
771    #[test]
772    fn successor_map_single_node_self_edge() {
773        let (modules, all_succs, succ_ranges) = build_test_succs(1, &[(0, 0)]);
774        let succs = SuccessorMap {
775            all_succs: &all_succs,
776            succ_ranges: &succ_ranges,
777            modules: &modules,
778        };
779        assert_eq!(succs.all_succs.len(), 1);
780        assert_eq!(succs.all_succs[0], 0);
781        assert_eq!(succs.succ_ranges[0], 0..1);
782    }
783
784    #[test]
785    fn successor_map_deduplicates_edges() {
786        // Two edges from 0 to 1 — should be deduped
787        let (modules, all_succs, succ_ranges) = build_test_succs(2, &[(0, 1), (0, 1)]);
788        let succs = SuccessorMap {
789            all_succs: &all_succs,
790            succ_ranges: &succ_ranges,
791            modules: &modules,
792        };
793        let range = &succs.succ_ranges[0];
794        assert_eq!(
795            range.end - range.start,
796            1,
797            "duplicate edges should be deduped"
798        );
799    }
800
801    #[test]
802    fn successor_map_multiple_successors() {
803        let (modules, all_succs, succ_ranges) = build_test_succs(4, &[(0, 1), (0, 2), (0, 3)]);
804        let succs = SuccessorMap {
805            all_succs: &all_succs,
806            succ_ranges: &succ_ranges,
807            modules: &modules,
808        };
809        let range = &succs.succ_ranges[0];
810        assert_eq!(range.end - range.start, 3);
811        // Node 1, 2, 3 have no successors
812        for i in 1..4 {
813            let r = &succs.succ_ranges[i];
814            assert_eq!(r.end - r.start, 0);
815        }
816    }
817
818    // -----------------------------------------------------------------------
819    // dfs_find_cycles_from tests
820    // -----------------------------------------------------------------------
821
822    #[test]
823    fn dfs_find_cycles_from_isolated_node() {
824        // Node 0 with no successors — should find no cycles
825        let (modules, all_succs, succ_ranges) = build_test_succs(1, &[]);
826        let succs = SuccessorMap {
827            all_succs: &all_succs,
828            succ_ranges: &succ_ranges,
829            modules: &modules,
830        };
831        let scc_set: FxHashSet<usize> = std::iter::once(0).collect();
832        let mut seen = FxHashSet::default();
833        let mut cycles = Vec::new();
834
835        dfs_find_cycles_from(0, 2, &scc_set, &succs, 10, &mut seen, &mut cycles);
836        assert!(cycles.is_empty(), "isolated node should have no cycles");
837    }
838
839    #[test]
840    fn dfs_find_cycles_from_simple_two_cycle() {
841        // 0 -> 1, 1 -> 0, both in SCC
842        let (modules, all_succs, succ_ranges) = build_test_succs(2, &[(0, 1), (1, 0)]);
843        let succs = SuccessorMap {
844            all_succs: &all_succs,
845            succ_ranges: &succ_ranges,
846            modules: &modules,
847        };
848        let scc_set: FxHashSet<usize> = [0, 1].into_iter().collect();
849        let mut seen = FxHashSet::default();
850        let mut cycles = Vec::new();
851
852        dfs_find_cycles_from(0, 2, &scc_set, &succs, 10, &mut seen, &mut cycles);
853        assert_eq!(cycles.len(), 1);
854        assert_eq!(cycles[0].len(), 2);
855    }
856
857    #[test]
858    fn dfs_find_cycles_from_diamond_graph() {
859        // Diamond: 0->1, 0->2, 1->3, 2->3, 3->0 (all in SCC)
860        // At depth 3: 0->1->3->0 and 0->2->3->0
861        // At depth 4: 0->1->3->?->0 — but 3 only goes to 0, so no 4-cycle
862        let (modules, all_succs, succ_ranges) =
863            build_test_succs(4, &[(0, 1), (0, 2), (1, 3), (2, 3), (3, 0)]);
864        let succs = SuccessorMap {
865            all_succs: &all_succs,
866            succ_ranges: &succ_ranges,
867            modules: &modules,
868        };
869        let scc_set: FxHashSet<usize> = [0, 1, 2, 3].into_iter().collect();
870        let mut seen = FxHashSet::default();
871        let mut cycles = Vec::new();
872
873        // Depth 3: should find two 3-node cycles
874        dfs_find_cycles_from(0, 3, &scc_set, &succs, 10, &mut seen, &mut cycles);
875        assert_eq!(cycles.len(), 2, "diamond should have two 3-node cycles");
876        assert!(cycles.iter().all(|c| c.len() == 3));
877    }
878
879    #[test]
880    fn dfs_find_cycles_from_depth_limit_prevents_longer_cycles() {
881        // 0->1->2->3->0 forms a 4-cycle
882        // With depth_limit=3, the DFS should NOT find this 4-cycle
883        let (modules, all_succs, succ_ranges) =
884            build_test_succs(4, &[(0, 1), (1, 2), (2, 3), (3, 0)]);
885        let succs = SuccessorMap {
886            all_succs: &all_succs,
887            succ_ranges: &succ_ranges,
888            modules: &modules,
889        };
890        let scc_set: FxHashSet<usize> = [0, 1, 2, 3].into_iter().collect();
891        let mut seen = FxHashSet::default();
892        let mut cycles = Vec::new();
893
894        dfs_find_cycles_from(0, 3, &scc_set, &succs, 10, &mut seen, &mut cycles);
895        assert!(
896            cycles.is_empty(),
897            "depth_limit=3 should prevent finding a 4-node cycle"
898        );
899    }
900
901    #[test]
902    fn dfs_find_cycles_from_depth_limit_exact_match() {
903        // 0->1->2->3->0 forms a 4-cycle
904        // With depth_limit=4, the DFS should find it
905        let (modules, all_succs, succ_ranges) =
906            build_test_succs(4, &[(0, 1), (1, 2), (2, 3), (3, 0)]);
907        let succs = SuccessorMap {
908            all_succs: &all_succs,
909            succ_ranges: &succ_ranges,
910            modules: &modules,
911        };
912        let scc_set: FxHashSet<usize> = [0, 1, 2, 3].into_iter().collect();
913        let mut seen = FxHashSet::default();
914        let mut cycles = Vec::new();
915
916        dfs_find_cycles_from(0, 4, &scc_set, &succs, 10, &mut seen, &mut cycles);
917        assert_eq!(
918            cycles.len(),
919            1,
920            "depth_limit=4 should find the 4-node cycle"
921        );
922        assert_eq!(cycles[0].len(), 4);
923    }
924
925    #[test]
926    fn dfs_find_cycles_from_respects_max_cycles() {
927        // Dense graph: complete graph of 4 nodes — many cycles
928        let edges: Vec<(usize, usize)> = (0..4)
929            .flat_map(|i| (0..4).filter(move |&j| i != j).map(move |j| (i, j)))
930            .collect();
931        let (modules, all_succs, succ_ranges) = build_test_succs(4, &edges);
932        let succs = SuccessorMap {
933            all_succs: &all_succs,
934            succ_ranges: &succ_ranges,
935            modules: &modules,
936        };
937        let scc_set: FxHashSet<usize> = (0..4).collect();
938        let mut seen = FxHashSet::default();
939        let mut cycles = Vec::new();
940
941        // max_cycles = 2: should stop after finding 2
942        dfs_find_cycles_from(0, 2, &scc_set, &succs, 2, &mut seen, &mut cycles);
943        assert!(
944            cycles.len() <= 2,
945            "should respect max_cycles limit, got {}",
946            cycles.len()
947        );
948    }
949
950    #[test]
951    fn dfs_find_cycles_from_ignores_nodes_outside_scc() {
952        // 0->1->2->0 but only {0, 1} in SCC set — node 2 should be ignored
953        let (modules, all_succs, succ_ranges) = build_test_succs(3, &[(0, 1), (1, 2), (2, 0)]);
954        let succs = SuccessorMap {
955            all_succs: &all_succs,
956            succ_ranges: &succ_ranges,
957            modules: &modules,
958        };
959        let scc_set: FxHashSet<usize> = [0, 1].into_iter().collect();
960        let mut seen = FxHashSet::default();
961        let mut cycles = Vec::new();
962
963        for depth in 2..=3 {
964            dfs_find_cycles_from(0, depth, &scc_set, &succs, 10, &mut seen, &mut cycles);
965        }
966        assert!(
967            cycles.is_empty(),
968            "should not find cycles through nodes outside the SCC set"
969        );
970    }
971
972    // -----------------------------------------------------------------------
973    // enumerate_elementary_cycles tests
974    // -----------------------------------------------------------------------
975
976    #[test]
977    fn enumerate_elementary_cycles_empty_scc() {
978        let (modules, all_succs, succ_ranges) = build_test_succs(0, &[]);
979        let succs = SuccessorMap {
980            all_succs: &all_succs,
981            succ_ranges: &succ_ranges,
982            modules: &modules,
983        };
984        let cycles = enumerate_elementary_cycles(&[], &succs, 10);
985        assert!(cycles.is_empty());
986    }
987
988    #[test]
989    fn enumerate_elementary_cycles_max_cycles_limit() {
990        // Complete graph of 4 nodes — many elementary cycles
991        let edges: Vec<(usize, usize)> = (0..4)
992            .flat_map(|i| (0..4).filter(move |&j| i != j).map(move |j| (i, j)))
993            .collect();
994        let (modules, all_succs, succ_ranges) = build_test_succs(4, &edges);
995        let succs = SuccessorMap {
996            all_succs: &all_succs,
997            succ_ranges: &succ_ranges,
998            modules: &modules,
999        };
1000        let scc_nodes: Vec<usize> = (0..4).collect();
1001
1002        let cycles = enumerate_elementary_cycles(&scc_nodes, &succs, 3);
1003        assert!(
1004            cycles.len() <= 3,
1005            "should respect max_cycles=3 limit, got {}",
1006            cycles.len()
1007        );
1008    }
1009
1010    #[test]
1011    fn enumerate_elementary_cycles_finds_all_in_triangle() {
1012        // 0->1->2->0 — single elementary cycle
1013        let (modules, all_succs, succ_ranges) = build_test_succs(3, &[(0, 1), (1, 2), (2, 0)]);
1014        let succs = SuccessorMap {
1015            all_succs: &all_succs,
1016            succ_ranges: &succ_ranges,
1017            modules: &modules,
1018        };
1019        let scc_nodes: Vec<usize> = vec![0, 1, 2];
1020
1021        let cycles = enumerate_elementary_cycles(&scc_nodes, &succs, 20);
1022        assert_eq!(cycles.len(), 1);
1023        assert_eq!(cycles[0].len(), 3);
1024    }
1025
1026    #[test]
1027    fn enumerate_elementary_cycles_iterative_deepening_order() {
1028        // SCC with both 2-node and 3-node cycles
1029        // 0->1->0 (2-cycle) and 0->1->2->0 (3-cycle)
1030        let (modules, all_succs, succ_ranges) =
1031            build_test_succs(3, &[(0, 1), (1, 0), (1, 2), (2, 0)]);
1032        let succs = SuccessorMap {
1033            all_succs: &all_succs,
1034            succ_ranges: &succ_ranges,
1035            modules: &modules,
1036        };
1037        let scc_nodes: Vec<usize> = vec![0, 1, 2];
1038
1039        let cycles = enumerate_elementary_cycles(&scc_nodes, &succs, 20);
1040        assert!(cycles.len() >= 2, "should find at least 2 cycles");
1041        // Iterative deepening: shorter cycles should come first
1042        assert!(
1043            cycles[0].len() <= cycles[cycles.len() - 1].len(),
1044            "shorter cycles should be found before longer ones"
1045        );
1046    }
1047
1048    // -----------------------------------------------------------------------
1049    // Integration-level edge cases
1050    // -----------------------------------------------------------------------
1051
1052    #[test]
1053    fn find_cycles_max_cycles_per_scc_respected() {
1054        // Dense SCC (complete graph of 5 nodes) — should cap at MAX_CYCLES_PER_SCC (20)
1055        let edges: Vec<(u32, u32)> = (0..5)
1056            .flat_map(|i| (0..5).filter(move |&j| i != j).map(move |j| (i, j)))
1057            .collect();
1058        let graph = build_cycle_graph(5, &edges);
1059        let cycles = graph.find_cycles();
1060        // K5 has many elementary cycles, but we cap at 20 per SCC
1061        assert!(
1062            cycles.len() <= 20,
1063            "should cap at MAX_CYCLES_PER_SCC, got {}",
1064            cycles.len()
1065        );
1066        assert!(
1067            !cycles.is_empty(),
1068            "dense graph should still find some cycles"
1069        );
1070    }
1071
1072    #[test]
1073    fn find_cycles_graph_with_no_cycles_returns_empty() {
1074        // Star topology: center -> all leaves, no cycles possible
1075        let graph = build_cycle_graph(5, &[(0, 1), (0, 2), (0, 3), (0, 4)]);
1076        assert!(graph.find_cycles().is_empty());
1077    }
1078
1079    #[test]
1080    fn find_cycles_diamond_no_cycle() {
1081        // Diamond without back-edge: A->B, A->C, B->D, C->D — no cycle
1082        let graph = build_cycle_graph(4, &[(0, 1), (0, 2), (1, 3), (2, 3)]);
1083        assert!(graph.find_cycles().is_empty());
1084    }
1085
1086    #[test]
1087    fn find_cycles_diamond_with_back_edge() {
1088        // Diamond with back-edge: A->B, A->C, B->D, C->D, D->A
1089        let graph = build_cycle_graph(4, &[(0, 1), (0, 2), (1, 3), (2, 3), (3, 0)]);
1090        let cycles = graph.find_cycles();
1091        assert!(
1092            cycles.len() >= 2,
1093            "diamond with back-edge should have at least 2 elementary cycles, got {}",
1094            cycles.len()
1095        );
1096        // Shortest cycles should be length 3 (A->B->D->A and A->C->D->A)
1097        assert_eq!(cycles[0].len(), 3);
1098    }
1099
1100    // -----------------------------------------------------------------------
1101    // Additional canonical_cycle tests
1102    // -----------------------------------------------------------------------
1103
1104    #[test]
1105    fn canonical_cycle_non_sequential_indices() {
1106        // Cycle with non-sequential node indices [3, 1, 4] — file1 has smallest path
1107        let (modules, _, _) = build_test_succs(5, &[]);
1108        let result = canonical_cycle(&[3, 1, 4], &modules);
1109        // file1 has path "/project/file1.ts" which is smallest, so rotation starts there
1110        assert_eq!(result, vec![1, 4, 3]);
1111    }
1112
1113    #[test]
1114    fn canonical_cycle_different_starting_points_same_result() {
1115        // The same logical cycle [0, 1, 2, 3] presented from four different starting points
1116        // should always canonicalize to [0, 1, 2, 3] since file0 has the smallest path.
1117        let (modules, _, _) = build_test_succs(4, &[]);
1118        let r1 = canonical_cycle(&[0, 1, 2, 3], &modules);
1119        let r2 = canonical_cycle(&[1, 2, 3, 0], &modules);
1120        let r3 = canonical_cycle(&[2, 3, 0, 1], &modules);
1121        let r4 = canonical_cycle(&[3, 0, 1, 2], &modules);
1122        assert_eq!(r1, r2);
1123        assert_eq!(r2, r3);
1124        assert_eq!(r3, r4);
1125        assert_eq!(r1, vec![0, 1, 2, 3]);
1126    }
1127
1128    #[test]
1129    fn canonical_cycle_two_node_both_rotations() {
1130        // Two-node cycle: [0, 1] and [1, 0] should both canonicalize to [0, 1]
1131        let (modules, _, _) = build_test_succs(2, &[]);
1132        assert_eq!(canonical_cycle(&[0, 1], &modules), vec![0, 1]);
1133        assert_eq!(canonical_cycle(&[1, 0], &modules), vec![0, 1]);
1134    }
1135
1136    // -----------------------------------------------------------------------
1137    // Self-loop unit-level tests
1138    // -----------------------------------------------------------------------
1139
1140    #[test]
1141    fn dfs_find_cycles_from_self_loop_not_found() {
1142        // Node 0 has a self-edge (0->0). The DFS requires path.len() >= 2 for a cycle,
1143        // so a self-loop should not be detected as a cycle.
1144        let (modules, all_succs, succ_ranges) = build_test_succs(1, &[(0, 0)]);
1145        let succs = SuccessorMap {
1146            all_succs: &all_succs,
1147            succ_ranges: &succ_ranges,
1148            modules: &modules,
1149        };
1150        let scc_set: FxHashSet<usize> = std::iter::once(0).collect();
1151        let mut seen = FxHashSet::default();
1152        let mut cycles = Vec::new();
1153
1154        for depth in 1..=3 {
1155            dfs_find_cycles_from(0, depth, &scc_set, &succs, 10, &mut seen, &mut cycles);
1156        }
1157        assert!(
1158            cycles.is_empty(),
1159            "self-loop should not be detected as a cycle by dfs_find_cycles_from"
1160        );
1161    }
1162
1163    #[test]
1164    fn enumerate_elementary_cycles_self_loop_not_found() {
1165        // Single node with self-edge — enumerate should find no elementary cycles
1166        let (modules, all_succs, succ_ranges) = build_test_succs(1, &[(0, 0)]);
1167        let succs = SuccessorMap {
1168            all_succs: &all_succs,
1169            succ_ranges: &succ_ranges,
1170            modules: &modules,
1171        };
1172        let cycles = enumerate_elementary_cycles(&[0], &succs, 20);
1173        assert!(
1174            cycles.is_empty(),
1175            "self-loop should not produce elementary cycles"
1176        );
1177    }
1178
1179    // -----------------------------------------------------------------------
1180    // Two overlapping cycles sharing an edge
1181    // -----------------------------------------------------------------------
1182
1183    #[test]
1184    fn find_cycles_two_cycles_sharing_edge() {
1185        // A->B->C->A and A->B->D->A share edge A->B
1186        // Should find exactly 2 elementary cycles, both of length 3
1187        let graph = build_cycle_graph(4, &[(0, 1), (1, 2), (2, 0), (1, 3), (3, 0)]);
1188        let cycles = graph.find_cycles();
1189        assert_eq!(
1190            cycles.len(),
1191            2,
1192            "two cycles sharing edge A->B should both be found, got {}",
1193            cycles.len()
1194        );
1195        assert!(
1196            cycles.iter().all(|c| c.len() == 3),
1197            "both cycles should have length 3"
1198        );
1199    }
1200
1201    #[test]
1202    fn enumerate_elementary_cycles_shared_edge() {
1203        // Same topology at the unit level: 0->1->2->0 and 0->1->3->0 share edge 0->1
1204        let (modules, all_succs, succ_ranges) =
1205            build_test_succs(4, &[(0, 1), (1, 2), (2, 0), (1, 3), (3, 0)]);
1206        let succs = SuccessorMap {
1207            all_succs: &all_succs,
1208            succ_ranges: &succ_ranges,
1209            modules: &modules,
1210        };
1211        let scc_nodes: Vec<usize> = vec![0, 1, 2, 3];
1212        let cycles = enumerate_elementary_cycles(&scc_nodes, &succs, 20);
1213        assert_eq!(
1214            cycles.len(),
1215            2,
1216            "should find exactly 2 elementary cycles sharing edge 0->1, got {}",
1217            cycles.len()
1218        );
1219    }
1220
1221    // -----------------------------------------------------------------------
1222    // Large SCC with multiple elementary cycles — verify all found
1223    // -----------------------------------------------------------------------
1224
1225    #[test]
1226    fn enumerate_elementary_cycles_pentagon_with_chords() {
1227        // Pentagon 0->1->2->3->4->0 plus chords 0->2 and 0->3
1228        // Elementary cycles include:
1229        //   len 3: 0->2->3->4->... no, let's enumerate:
1230        //   0->1->2->3->4->0 (len 5)
1231        //   0->2->3->4->0 (len 4, via chord 0->2)
1232        //   0->3->4->0 (len 3, via chord 0->3)
1233        //   0->1->2->... subsets through chords
1234        let (modules, all_succs, succ_ranges) =
1235            build_test_succs(5, &[(0, 1), (1, 2), (2, 3), (3, 4), (4, 0), (0, 2), (0, 3)]);
1236        let succs = SuccessorMap {
1237            all_succs: &all_succs,
1238            succ_ranges: &succ_ranges,
1239            modules: &modules,
1240        };
1241        let scc_nodes: Vec<usize> = vec![0, 1, 2, 3, 4];
1242        let cycles = enumerate_elementary_cycles(&scc_nodes, &succs, 20);
1243
1244        // Should find at least 3 distinct elementary cycles (the pentagon + two chord-shortened)
1245        assert!(
1246            cycles.len() >= 3,
1247            "pentagon with chords should have at least 3 elementary cycles, got {}",
1248            cycles.len()
1249        );
1250        // All cycles should be unique (no duplicates)
1251        let unique: FxHashSet<Vec<usize>> = cycles.iter().cloned().collect();
1252        assert_eq!(
1253            unique.len(),
1254            cycles.len(),
1255            "all enumerated cycles should be unique"
1256        );
1257        // Shortest cycle should be length 3 (0->3->4->0)
1258        assert_eq!(
1259            cycles[0].len(),
1260            3,
1261            "shortest cycle in pentagon with chords should be length 3"
1262        );
1263    }
1264
1265    #[test]
1266    fn find_cycles_large_scc_complete_graph_k6() {
1267        // Complete graph K6: every node connects to every other node
1268        // This creates a dense SCC with many elementary cycles
1269        let edges: Vec<(u32, u32)> = (0..6)
1270            .flat_map(|i| (0..6).filter(move |&j| i != j).map(move |j| (i, j)))
1271            .collect();
1272        let graph = build_cycle_graph(6, &edges);
1273        let cycles = graph.find_cycles();
1274
1275        // K6 has a huge number of elementary cycles; we should find many but cap at 20
1276        assert!(
1277            cycles.len() <= 20,
1278            "should cap at MAX_CYCLES_PER_SCC (20), got {}",
1279            cycles.len()
1280        );
1281        assert_eq!(
1282            cycles.len(),
1283            20,
1284            "K6 has far more than 20 elementary cycles, so we should hit the cap"
1285        );
1286        // Shortest cycles should be 2-node cycles (since every pair has bidirectional edges)
1287        assert_eq!(cycles[0].len(), 2, "shortest cycles in K6 should be 2-node");
1288    }
1289
1290    // -----------------------------------------------------------------------
1291    // Depth limit enforcement in enumerate_elementary_cycles
1292    // -----------------------------------------------------------------------
1293
1294    #[test]
1295    fn enumerate_elementary_cycles_respects_depth_cap_of_12() {
1296        // Build a single long cycle of 15 nodes: 0->1->2->...->14->0
1297        // enumerate_elementary_cycles caps depth at min(scc.len(), 12) = 12
1298        // So the 15-node cycle should NOT be found.
1299        let edges: Vec<(usize, usize)> = (0..15).map(|i| (i, (i + 1) % 15)).collect();
1300        let (modules, all_succs, succ_ranges) = build_test_succs(15, &edges);
1301        let succs = SuccessorMap {
1302            all_succs: &all_succs,
1303            succ_ranges: &succ_ranges,
1304            modules: &modules,
1305        };
1306        let scc_nodes: Vec<usize> = (0..15).collect();
1307        let cycles = enumerate_elementary_cycles(&scc_nodes, &succs, 20);
1308
1309        assert!(
1310            cycles.is_empty(),
1311            "a pure 15-node cycle should not be found with depth cap of 12, got {} cycles",
1312            cycles.len()
1313        );
1314    }
1315
1316    #[test]
1317    fn enumerate_elementary_cycles_finds_cycle_at_depth_cap_boundary() {
1318        // Build a single cycle of exactly 12 nodes: 0->1->...->11->0
1319        // depth cap = min(12, 12) = 12, so this cycle should be found.
1320        let edges: Vec<(usize, usize)> = (0..12).map(|i| (i, (i + 1) % 12)).collect();
1321        let (modules, all_succs, succ_ranges) = build_test_succs(12, &edges);
1322        let succs = SuccessorMap {
1323            all_succs: &all_succs,
1324            succ_ranges: &succ_ranges,
1325            modules: &modules,
1326        };
1327        let scc_nodes: Vec<usize> = (0..12).collect();
1328        let cycles = enumerate_elementary_cycles(&scc_nodes, &succs, 20);
1329
1330        assert_eq!(
1331            cycles.len(),
1332            1,
1333            "a pure 12-node cycle should be found at the depth cap boundary"
1334        );
1335        assert_eq!(cycles[0].len(), 12);
1336    }
1337
1338    #[test]
1339    fn enumerate_elementary_cycles_13_node_pure_cycle_not_found() {
1340        // 13-node pure cycle: depth cap = min(13, 12) = 12, so the 13-node cycle is skipped
1341        let edges: Vec<(usize, usize)> = (0..13).map(|i| (i, (i + 1) % 13)).collect();
1342        let (modules, all_succs, succ_ranges) = build_test_succs(13, &edges);
1343        let succs = SuccessorMap {
1344            all_succs: &all_succs,
1345            succ_ranges: &succ_ranges,
1346            modules: &modules,
1347        };
1348        let scc_nodes: Vec<usize> = (0..13).collect();
1349        let cycles = enumerate_elementary_cycles(&scc_nodes, &succs, 20);
1350
1351        assert!(
1352            cycles.is_empty(),
1353            "13-node pure cycle exceeds depth cap of 12"
1354        );
1355    }
1356
1357    // -----------------------------------------------------------------------
1358    // MAX_CYCLES_PER_SCC enforcement at integration level
1359    // -----------------------------------------------------------------------
1360
1361    #[test]
1362    fn find_cycles_max_cycles_per_scc_enforced_on_k7() {
1363        // K7 complete graph: enormous number of elementary cycles
1364        // Should still be capped at 20 per SCC
1365        let edges: Vec<(u32, u32)> = (0..7)
1366            .flat_map(|i| (0..7).filter(move |&j| i != j).map(move |j| (i, j)))
1367            .collect();
1368        let graph = build_cycle_graph(7, &edges);
1369        let cycles = graph.find_cycles();
1370
1371        assert!(
1372            cycles.len() <= 20,
1373            "K7 should cap at MAX_CYCLES_PER_SCC (20), got {}",
1374            cycles.len()
1375        );
1376        assert_eq!(
1377            cycles.len(),
1378            20,
1379            "K7 has far more than 20 elementary cycles, should hit the cap exactly"
1380        );
1381    }
1382
1383    #[test]
1384    fn find_cycles_two_dense_sccs_each_capped() {
1385        // Two separate complete subgraphs K4 (nodes 0-3) and K4 (nodes 4-7)
1386        // Each has many elementary cycles; total should be capped at 20 per SCC
1387        let mut edges: Vec<(u32, u32)> = Vec::new();
1388        // First K4: nodes 0-3
1389        for i in 0..4 {
1390            for j in 0..4 {
1391                if i != j {
1392                    edges.push((i, j));
1393                }
1394            }
1395        }
1396        // Second K4: nodes 4-7
1397        for i in 4..8 {
1398            for j in 4..8 {
1399                if i != j {
1400                    edges.push((i, j));
1401                }
1402            }
1403        }
1404        let graph = build_cycle_graph(8, &edges);
1405        let cycles = graph.find_cycles();
1406
1407        // Each K4 has 2-cycles: C(4,2)=6, plus 3-cycles and 4-cycles
1408        // Both SCCs contribute cycles, but each is independently capped at 20
1409        assert!(!cycles.is_empty(), "two dense SCCs should produce cycles");
1410        // Total can be up to 40 (20 per SCC), but K4 has fewer than 20 elementary cycles
1411        // K4 elementary cycles: 6 two-cycles + 8 three-cycles + 3 four-cycles = 17
1412        // So we should get all from both SCCs
1413        assert!(
1414            cycles.len() > 2,
1415            "should find multiple cycles across both SCCs, got {}",
1416            cycles.len()
1417        );
1418    }
1419
1420    mod proptests {
1421        use super::*;
1422        use proptest::prelude::*;
1423
1424        proptest! {
1425            /// A DAG (directed acyclic graph) should always have zero cycles.
1426            /// We construct a DAG by only allowing edges from lower to higher node indices.
1427            #[test]
1428            fn dag_has_no_cycles(
1429                file_count in 2..20usize,
1430                edge_pairs in prop::collection::vec((0..19u32, 0..19u32), 0..30),
1431            ) {
1432                // Filter to only forward edges (i < j) to guarantee a DAG
1433                let dag_edges: Vec<(u32, u32)> = edge_pairs
1434                    .into_iter()
1435                    .filter(|(a, b)| (*a as usize) < file_count && (*b as usize) < file_count && a < b)
1436                    .collect();
1437
1438                let graph = build_cycle_graph(file_count, &dag_edges);
1439                let cycles = graph.find_cycles();
1440                prop_assert!(
1441                    cycles.is_empty(),
1442                    "DAG should have no cycles, but found {}",
1443                    cycles.len()
1444                );
1445            }
1446
1447            /// Adding mutual edges A->B->A should always detect a cycle.
1448            #[test]
1449            fn mutual_edges_always_detect_cycle(extra_nodes in 0..10usize) {
1450                let file_count = 2 + extra_nodes;
1451                let graph = build_cycle_graph(file_count, &[(0, 1), (1, 0)]);
1452                let cycles = graph.find_cycles();
1453                prop_assert!(
1454                    !cycles.is_empty(),
1455                    "A->B->A should always produce at least one cycle"
1456                );
1457                // The cycle should contain both nodes 0 and 1
1458                let has_pair_cycle = cycles.iter().any(|c| {
1459                    c.contains(&FileId(0)) && c.contains(&FileId(1))
1460                });
1461                prop_assert!(has_pair_cycle, "Should find a cycle containing nodes 0 and 1");
1462            }
1463
1464            /// All cycle members should be valid FileId indices.
1465            #[test]
1466            fn cycle_members_are_valid_indices(
1467                file_count in 2..15usize,
1468                edge_pairs in prop::collection::vec((0..14u32, 0..14u32), 1..20),
1469            ) {
1470                let edges: Vec<(u32, u32)> = edge_pairs
1471                    .into_iter()
1472                    .filter(|(a, b)| (*a as usize) < file_count && (*b as usize) < file_count && a != b)
1473                    .collect();
1474
1475                let graph = build_cycle_graph(file_count, &edges);
1476                let cycles = graph.find_cycles();
1477                for cycle in &cycles {
1478                    prop_assert!(cycle.len() >= 2, "Cycles must have at least 2 nodes");
1479                    for file_id in cycle {
1480                        prop_assert!(
1481                            (file_id.0 as usize) < file_count,
1482                            "FileId {} exceeds file count {}",
1483                            file_id.0, file_count
1484                        );
1485                    }
1486                }
1487            }
1488
1489            /// Cycles should be sorted by length (shortest first).
1490            #[test]
1491            fn cycles_sorted_by_length(
1492                file_count in 3..12usize,
1493                edge_pairs in prop::collection::vec((0..11u32, 0..11u32), 2..25),
1494            ) {
1495                let edges: Vec<(u32, u32)> = edge_pairs
1496                    .into_iter()
1497                    .filter(|(a, b)| (*a as usize) < file_count && (*b as usize) < file_count && a != b)
1498                    .collect();
1499
1500                let graph = build_cycle_graph(file_count, &edges);
1501                let cycles = graph.find_cycles();
1502                for window in cycles.windows(2) {
1503                    prop_assert!(
1504                        window[0].len() <= window[1].len(),
1505                        "Cycles should be sorted by length: {} > {}",
1506                        window[0].len(), window[1].len()
1507                    );
1508                }
1509            }
1510        }
1511    }
1512
1513    // ── Type-only cycle tests ────────────────────────────────────
1514
1515    /// Build a cycle graph where specific edges are type-only.
1516    fn build_cycle_graph_with_type_only(
1517        file_count: usize,
1518        edges_spec: &[(u32, u32, bool)], // (source, target, is_type_only)
1519    ) -> ModuleGraph {
1520        let files: Vec<DiscoveredFile> = (0..file_count)
1521            .map(|i| DiscoveredFile {
1522                id: FileId(i as u32),
1523                path: PathBuf::from(format!("/project/file{i}.ts")),
1524                size_bytes: 100,
1525            })
1526            .collect();
1527
1528        let resolved_modules: Vec<ResolvedModule> = (0..file_count)
1529            .map(|i| {
1530                let imports: Vec<ResolvedImport> = edges_spec
1531                    .iter()
1532                    .filter(|(src, _, _)| *src == i as u32)
1533                    .map(|(_, tgt, type_only)| ResolvedImport {
1534                        info: ImportInfo {
1535                            source: format!("./file{tgt}"),
1536                            imported_name: ImportedName::Named("x".to_string()),
1537                            local_name: "x".to_string(),
1538                            is_type_only: *type_only,
1539                            span: oxc_span::Span::new(0, 10),
1540                            source_span: oxc_span::Span::default(),
1541                        },
1542                        target: ResolveResult::InternalModule(FileId(*tgt)),
1543                    })
1544                    .collect();
1545
1546                ResolvedModule {
1547                    file_id: FileId(i as u32),
1548                    path: PathBuf::from(format!("/project/file{i}.ts")),
1549                    exports: vec![fallow_types::extract::ExportInfo {
1550                        name: ExportName::Named("x".to_string()),
1551                        local_name: Some("x".to_string()),
1552                        is_type_only: false,
1553                        is_public: false,
1554                        span: oxc_span::Span::new(0, 20),
1555                        members: vec![],
1556                        super_class: None,
1557                    }],
1558                    re_exports: vec![],
1559                    resolved_imports: imports,
1560                    resolved_dynamic_imports: vec![],
1561                    resolved_dynamic_patterns: vec![],
1562                    member_accesses: vec![],
1563                    whole_object_uses: vec![],
1564                    has_cjs_exports: false,
1565                    unused_import_bindings: FxHashSet::default(),
1566                }
1567            })
1568            .collect();
1569
1570        let entry_points = vec![EntryPoint {
1571            path: PathBuf::from("/project/file0.ts"),
1572            source: EntryPointSource::PackageJsonMain,
1573        }];
1574
1575        ModuleGraph::build(&resolved_modules, &entry_points, &files)
1576    }
1577
1578    #[test]
1579    fn type_only_bidirectional_import_not_a_cycle() {
1580        // A imports type from B, B imports type from A — not a runtime cycle
1581        let graph = build_cycle_graph_with_type_only(2, &[(0, 1, true), (1, 0, true)]);
1582        let cycles = graph.find_cycles();
1583        assert!(
1584            cycles.is_empty(),
1585            "type-only bidirectional imports should not be reported as cycles"
1586        );
1587    }
1588
1589    #[test]
1590    fn mixed_type_and_value_import_not_a_cycle() {
1591        // A value-imports B, B type-imports A — NOT a runtime cycle.
1592        // B's import of A is type-only (erased at compile time), so the runtime
1593        // dependency is one-directional: A→B only.
1594        let graph = build_cycle_graph_with_type_only(2, &[(0, 1, false), (1, 0, true)]);
1595        let cycles = graph.find_cycles();
1596        assert!(
1597            cycles.is_empty(),
1598            "A->B (value) + B->A (type-only) is not a runtime cycle"
1599        );
1600    }
1601
1602    #[test]
1603    fn both_value_imports_with_one_type_still_a_cycle() {
1604        // A value-imports B AND type-imports B. B value-imports A.
1605        // A->B has a non-type-only symbol, B->A has a non-type-only symbol = real cycle.
1606        let graph = build_cycle_graph_with_type_only(2, &[(0, 1, false), (1, 0, false)]);
1607        let cycles = graph.find_cycles();
1608        assert!(
1609            !cycles.is_empty(),
1610            "bidirectional value imports should be reported as a cycle"
1611        );
1612    }
1613
1614    #[test]
1615    fn all_value_imports_still_a_cycle() {
1616        // A value-imports B, B value-imports A — still a cycle
1617        let graph = build_cycle_graph_with_type_only(2, &[(0, 1, false), (1, 0, false)]);
1618        let cycles = graph.find_cycles();
1619        assert_eq!(cycles.len(), 1);
1620    }
1621
1622    #[test]
1623    fn three_node_type_only_cycle_not_reported() {
1624        // A -> B -> C -> A, all type-only
1625        let graph =
1626            build_cycle_graph_with_type_only(3, &[(0, 1, true), (1, 2, true), (2, 0, true)]);
1627        let cycles = graph.find_cycles();
1628        assert!(
1629            cycles.is_empty(),
1630            "three-node type-only cycle should not be reported"
1631        );
1632    }
1633
1634    #[test]
1635    fn three_node_cycle_one_value_edge_still_reported() {
1636        // A -value-> B -type-> C -type-> A
1637        // B->C and C->A are type-only, but A->B is a value edge.
1638        // This still forms a cycle because Tarjan's considers all non-type-only successors.
1639        // However, since B only has type-only successors (B->C is type-only),
1640        // B has no runtime successors, so no SCC with B will form.
1641        let graph =
1642            build_cycle_graph_with_type_only(3, &[(0, 1, false), (1, 2, true), (2, 0, true)]);
1643        let cycles = graph.find_cycles();
1644        // B has no runtime successors (B->C is type-only), so the cycle is broken
1645        assert!(
1646            cycles.is_empty(),
1647            "cycle broken by type-only edge in the middle should not be reported"
1648        );
1649    }
1650}