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