crawk 0.7.0

Dependency crawler for Rust. It crawls so you don't have to untangle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
//! Rule evaluation: a single pass over the dependency graph's edges.

use std::collections::{BTreeSet, HashMap};

use tracing::warn;

use crate::graph::{Cycle, DependencyGraph};
use crate::module_path::is_in_subtree;

use super::{
    AllowedCycle, CheckReport, LayerPos, RuleSet, Violation, ViolationKind, is_parent_child_cycle,
};

impl RuleSet {
    /// Build `module -> [LayerPos]` via per-group longest-prefix match.
    ///
    /// A module may belong to several overlapping groups, so each maps to a
    /// list of positions (one per covering group). Uncovered modules are
    /// omitted.
    pub(crate) fn build_layer_index(
        &self,
        modules: &BTreeSet<String>,
    ) -> HashMap<String, Vec<LayerPos>> {
        let mut index = HashMap::new();
        for module in modules {
            let positions = self.memberships(module);
            if !positions.is_empty() {
                index.insert(module.clone(), positions);
            }
        }
        index
    }

    /// Check a single edge against the deny rules, pushing any violation.
    ///
    /// Each matching rule contributes its own violation, so an edge banned by
    /// several rules is reported once per rule (consistent with overlapping
    /// layer groups).
    fn check_deny(
        &self,
        source: &str,
        target: &str,
        apis: &BTreeSet<String>,
        out: &mut Vec<Violation>,
    ) {
        for rule in &self.deny {
            if rule.from.matches(source) && rule.to.matches(target) {
                out.push(Violation {
                    kind: ViolationKind::Deny,
                    source: source.to_owned(),
                    target: target.to_owned(),
                    rule: rule.display(),
                    apis: apis.clone(),
                });
            }
        }
    }

    /// Check a single edge against the restrict rules, pushing any violation.
    ///
    /// Every matching rule is evaluated independently, so overlapping rules
    /// intersect their allowances rather than the narrower one overriding.
    fn check_restrict(
        &self,
        source: &str,
        target: &str,
        apis: &BTreeSet<String>,
        out: &mut Vec<Violation>,
    ) {
        // A module and its own descendant are one unit, not a cross-boundary
        // dependency — exempt before any allowance is consulted. Needed for
        // exact `from` patterns (`cli`), which cannot match their own children.
        if is_in_subtree(target, source) || is_in_subtree(source, target) {
            return;
        }
        for rule in &self.restrict {
            // An edge whose target still matches `from` stays inside the
            // rule's own scope (sibling modules of one subsystem). Restrict
            // guards the boundary, not the internal structure — that is
            // layers/deny work.
            if rule.from.matches(source)
                && !rule.from.matches(target)
                && !rule.to.iter().any(|pattern| pattern.matches(target))
            {
                out.push(Violation {
                    kind: ViolationKind::Restrict,
                    source: source.to_owned(),
                    target: target.to_owned(),
                    rule: rule.display(),
                    apis: apis.clone(),
                });
            }
        }
    }

    /// Report the detected cycles, one violation per edge of every banned loop.
    ///
    /// Callers gate on `deny_cycles` — detecting the cycles is not free — so
    /// this assumes the rule is on. Allowlist matching runs **before** the
    /// parent-child filter, so an entry aimed at a containment loop still counts
    /// as used and does not warn as stale.
    fn check_cycles(&self, cycles: &[Cycle], out: &mut Vec<Violation>) {
        let mut used = vec![false; self.allow_cycles.len()];
        for cycle in cycles {
            let mut allowed = false;
            for (entry, used) in self.allow_cycles.iter().zip(used.iter_mut()) {
                if entry.covers(&cycle.modules) {
                    *used = true;
                    allowed = true;
                }
            }
            let structural =
                !self.deny_parent_child_cycles && is_parent_child_cycle(&cycle.modules);
            if allowed || structural {
                continue;
            }

            let names = cycle
                .modules
                .iter()
                .map(String::as_str)
                .collect::<Vec<_>>()
                .join(", ");
            // A comma list, not an arrow path: `modules` is alphabetical, not in
            // traversal order, so an arrow would imply a direction that is not
            // there. The actual edges are the rows themselves.
            let rule = format!("cycle: {names}");
            for ((source, target), apis) in &cycle.edges {
                out.push(Violation {
                    kind: ViolationKind::Cycle,
                    source: source.clone(),
                    target: target.clone(),
                    rule: rule.clone(),
                    apis: apis.clone(),
                });
            }
        }
        for (entry, used) in self.allow_cycles.iter().zip(used) {
            if !used {
                warn_stale(entry);
            }
        }
    }

    /// Check a single edge against the layer rules, pushing any violation.
    ///
    /// Each group containing **both** endpoints is checked independently, so an
    /// edge forbidden by several overlapping groups produces one violation per
    /// group (the rule message names the offending group).
    fn check_layers(
        &self,
        source: &str,
        target: &str,
        layer_index: &HashMap<String, Vec<LayerPos>>,
        apis: &BTreeSet<String>,
        out: &mut Vec<Violation>,
    ) {
        let (Some(src_positions), Some(tgt_positions)) =
            (layer_index.get(source), layer_index.get(target))
        else {
            return; // at least one endpoint is unassigned
        };

        for src_pos in src_positions {
            // The edge is only constrained where both endpoints share a group.
            let Some(tgt_pos) = tgt_positions.iter().find(|t| t.group == src_pos.group) else {
                continue; // independent stacks: no cross-group constraint
            };

            // Fetch the covering group once; its `deny_same_layer` is per-group.
            let layer = self.layers.get(src_pos.group);
            let deny_same = layer.is_some_and(|l| l.deny_same_layer);
            let upward = src_pos.index > tgt_pos.index;
            // A module and its own submodule share the implicit subtree match, so
            // they land on the same index. Such an edge (e.g. a `mod.rs`
            // re-exporting its own child) is a natural containment relation, not a
            // same-layer coupling — exempt it from the deny-same-layer check.
            let related = is_in_subtree(target, source) || is_in_subtree(source, target);
            let same_layer = src_pos.index == tgt_pos.index && !related;
            let violates = upward || (same_layer && deny_same);
            if !violates {
                continue;
            }

            let group_name = layer.map_or("?", |layer| layer.name.as_str());
            let rule = if upward {
                format!("layer '{group_name}' forbids upward dependency ({source} -> {target})")
            } else {
                format!("layer '{group_name}' forbids same-layer dependency ({source} -> {target})")
            };

            out.push(Violation {
                kind: ViolationKind::Layer,
                source: source.to_owned(),
                target: target.to_owned(),
                rule,
                apis: apis.clone(),
            });
        }
    }
}

/// Warn that an allowlist entry matched nothing, so it can be dropped.
///
/// A stale entry is config rot, not a failure: whoever untangled the loop must
/// not have their build broken by the leftover.
fn warn_stale(entry: &AllowedCycle) {
    let because = entry
        .reason
        .as_deref()
        .map_or_else(String::new, |reason| format!(" ({reason})"));
    warn!(
        "{}{because} covers no detected cycle; remove or update it",
        entry.display()
    );
}

/// Evaluate `rules` against `graph`, returning all violations (sorted).
pub(crate) fn evaluate(rules: &RuleSet, graph: &DependencyGraph) -> CheckReport {
    let mut violations = Vec::new();
    let layer_index = rules.build_layer_index(graph.modules());

    for ((source, target), apis) in graph.edges() {
        rules.check_deny(source, target, apis, &mut violations);
        rules.check_restrict(source, target, apis, &mut violations);
        rules.check_layers(source, target, &layer_index, apis, &mut violations);
    }

    // Gated here: `cycles()` runs Tarjan over the graph on every call.
    if rules.deny_cycles {
        rules.check_cycles(&graph.cycles(), &mut violations);
    }

    violations.sort();
    CheckReport { violations }
}

#[cfg(test)]
mod tests {
    use super::super::{
        AllowedCycle, DenyRule, LayerRule, ModulePattern, RestrictRule, RuleSet, ViolationKind,
    };
    use crate::graph::{AnnotatedEdges, Cycle};
    use std::collections::BTreeSet;

    fn layer(name: &str, order: &[&str]) -> LayerRule {
        layer_with_deny(name, order, false)
    }

    fn layer_deny(name: &str, order: &[&str]) -> LayerRule {
        layer_with_deny(name, order, true)
    }

    fn layer_with_deny(name: &str, order: &[&str], deny_same_layer: bool) -> LayerRule {
        LayerRule {
            name: name.to_owned(),
            order: order
                .iter()
                .map(|s| ModulePattern::parse_subtree(s))
                .collect(),
            deny_same_layer,
        }
    }

    fn module_set(names: &[&str]) -> BTreeSet<String> {
        names.iter().map(ToString::to_string).collect()
    }

    fn app_rules() -> RuleSet {
        RuleSet {
            layers: vec![layer("app", &["cli", "analyzer", "parser", "discover"])],
            ..RuleSet::default()
        }
    }

    #[test]
    fn downward_dependency_is_allowed() {
        let rules = app_rules();
        let modules = module_set(&["cli", "analyzer"]);
        let index = rules.build_layer_index(&modules);
        let mut out = Vec::new();
        rules.check_layers("cli", "analyzer", &index, &BTreeSet::new(), &mut out);
        assert!(out.is_empty());
    }

    #[test]
    fn upward_dependency_is_a_violation() {
        let rules = app_rules();
        let modules = module_set(&["cli", "analyzer"]);
        let index = rules.build_layer_index(&modules);
        let mut out = Vec::new();
        rules.check_layers("analyzer", "cli", &index, &BTreeSet::new(), &mut out);
        assert_eq!(out.len(), 1);
    }

    #[test]
    fn subtree_inherits_layer() {
        let rules = app_rules();
        // parser::visitor is in the parser subtree (idx 2); cli is idx 0.
        let modules = module_set(&["cli", "parser::visitor"]);
        let index = rules.build_layer_index(&modules);
        let mut out = Vec::new();
        rules.check_layers("parser::visitor", "cli", &index, &BTreeSet::new(), &mut out);
        assert_eq!(out.len(), 1, "deep module depending upward must violate");
    }

    #[test]
    fn different_groups_have_no_constraint() {
        let rules = RuleSet {
            layers: vec![
                layer("app", &["cli", "core"]),
                layer("web", &["web::api", "web::repo"]),
            ],
            ..RuleSet::default()
        };
        let modules = module_set(&["cli", "web::repo"]);
        let index = rules.build_layer_index(&modules);
        let mut out = Vec::new();
        // cli (app) -> web::repo (web): cross-group, even though indices differ.
        rules.check_layers("cli", "web::repo", &index, &BTreeSet::new(), &mut out);
        assert!(out.is_empty());
    }

    #[test]
    fn same_layer_allowed_by_default() {
        let rules = RuleSet {
            layers: vec![layer("app", &["mid"])],
            ..RuleSet::default()
        };
        let modules = module_set(&["mid::a", "mid::b"]);
        let index = rules.build_layer_index(&modules);
        let mut out = Vec::new();
        rules.check_layers("mid::a", "mid::b", &index, &BTreeSet::new(), &mut out);
        assert!(out.is_empty());
    }

    #[test]
    fn same_layer_denied_when_flag_set() {
        let rules = RuleSet {
            layers: vec![layer_deny("app", &["mid"])],
            ..RuleSet::default()
        };
        let modules = module_set(&["mid::a", "mid::b"]);
        let index = rules.build_layer_index(&modules);
        let mut out = Vec::new();
        rules.check_layers("mid::a", "mid::b", &index, &BTreeSet::new(), &mut out);
        assert_eq!(out.len(), 1);
    }

    #[test]
    fn same_layer_ancestor_edge_allowed_under_deny() {
        // A parent depending on its own submodule (e.g. `parser` re-exporting
        // `parser::visitor`) is same-layer under the implicit subtree match, but
        // is a natural edge — deny-same-layer must not flag it.
        let rules = RuleSet {
            layers: vec![layer_deny("app", &["parser"])],
            ..RuleSet::default()
        };
        let modules = module_set(&["parser", "parser::visitor"]);
        let index = rules.build_layer_index(&modules);
        let mut out = Vec::new();
        rules.check_layers(
            "parser",
            "parser::visitor",
            &index,
            &BTreeSet::new(),
            &mut out,
        );
        assert!(out.is_empty(), "parent -> own submodule must not violate");
    }

    #[test]
    fn same_layer_sibling_edge_still_denied() {
        // Guarding ancestor edges must not weaken the sibling case: two distinct
        // submodules of the same layer are still same-layer under the flag.
        let rules = RuleSet {
            layers: vec![layer_deny("app", &["parser"])],
            ..RuleSet::default()
        };
        let modules = module_set(&["parser::a", "parser::b"]);
        let index = rules.build_layer_index(&modules);
        let mut out = Vec::new();
        rules.check_layers("parser::a", "parser::b", &index, &BTreeSet::new(), &mut out);
        assert_eq!(out.len(), 1, "sibling same-layer edge still violates");
    }

    #[test]
    fn deny_same_layer_is_per_group() {
        // `strict` denies same-layer deps; `lax` (same modules) does not. The
        // edge a -> b is same-layer in both, so only `strict` yields a violation.
        let rules = RuleSet {
            layers: vec![layer_deny("strict", &["mid"]), layer("lax", &["mid"])],
            ..RuleSet::default()
        };
        let modules = module_set(&["mid::a", "mid::b"]);
        let index = rules.build_layer_index(&modules);
        let mut out = Vec::new();
        rules.check_layers("mid::a", "mid::b", &index, &BTreeSet::new(), &mut out);
        assert_eq!(out.len(), 1, "only the deny group contributes a violation");
        assert!(out[0].rule.contains("'strict'"), "{}", out[0].rule);
    }

    #[test]
    fn overlapping_groups_each_yield_a_violation() {
        // `mid` and `top` belong to both groups, each reversing their order, so
        // the edge mid -> top is upward in both → one violation per group.
        let rules = RuleSet {
            layers: vec![
                layer("left", &["top", "mid"]),
                layer("right", &["top", "mid"]),
            ],
            ..RuleSet::default()
        };
        let modules = module_set(&["top", "mid"]);
        let index = rules.build_layer_index(&modules);
        let mut out = Vec::new();
        rules.check_layers("mid", "top", &index, &BTreeSet::new(), &mut out);
        assert_eq!(out.len(), 2, "one violation per overlapping group");
    }

    #[test]
    fn overlapping_groups_clean_when_each_satisfied() {
        // `shared` is index 0 in both groups; each edge is downward in its own
        // group, so overlap produces no violation.
        let rules = RuleSet {
            layers: vec![
                layer("a", &["shared", "low_a"]),
                layer("b", &["shared", "low_b"]),
            ],
            ..RuleSet::default()
        };
        let modules = module_set(&["shared", "low_a", "low_b"]);
        let index = rules.build_layer_index(&modules);
        let mut out = Vec::new();
        rules.check_layers("shared", "low_a", &index, &BTreeSet::new(), &mut out);
        rules.check_layers("shared", "low_b", &index, &BTreeSet::new(), &mut out);
        assert!(out.is_empty());
    }

    fn deny(from: &str, to: &str) -> DenyRule {
        DenyRule {
            from: ModulePattern::parse(from),
            to: ModulePattern::parse(to),
        }
    }

    fn deny_rules(rules: Vec<DenyRule>) -> RuleSet {
        RuleSet {
            deny: rules,
            ..RuleSet::default()
        }
    }

    #[test]
    fn deny_flags_matching_edge() {
        let rules = deny_rules(vec![deny("cli", "web::*")]);
        let mut out = Vec::new();
        rules.check_deny("cli", "web::repo", &BTreeSet::new(), &mut out);
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].kind, ViolationKind::Deny);
        assert_eq!(out[0].rule, "deny cli -> web::*");
    }

    #[test]
    fn deny_ignores_non_matching_edge() {
        let rules = deny_rules(vec![deny("cli", "web::*")]);
        let mut out = Vec::new();
        // cli::args is not `cli` (no explicit ::*); webs is not under web::*.
        rules.check_deny("cli::args", "web::repo", &BTreeSet::new(), &mut out);
        rules.check_deny("cli", "webs", &BTreeSet::new(), &mut out);
        rules.check_deny("analyzer", "web::repo", &BTreeSet::new(), &mut out);
        assert!(out.is_empty());
    }

    #[test]
    fn deny_subtree_matches_base_and_descendants() {
        let rules = deny_rules(vec![deny("format::*", "discover")]);
        let mut out = Vec::new();
        rules.check_deny("format", "discover", &BTreeSet::new(), &mut out);
        rules.check_deny("format::deps_cmd", "discover", &BTreeSet::new(), &mut out);
        assert_eq!(out.len(), 2);
    }

    #[test]
    fn edge_banned_by_several_rules_reports_each() {
        let rules = deny_rules(vec![deny("cli", "web::*"), deny("*", "web::repo")]);
        let mut out = Vec::new();
        rules.check_deny("cli", "web::repo", &BTreeSet::new(), &mut out);
        assert_eq!(out.len(), 2, "one violation per matching rule");
    }

    #[test]
    fn deny_sorts_before_layer_violations() {
        // The same edge breaks a deny rule and a layer order; the derived Ord on
        // Violation compares `kind` first, so DENY rows lead the report.
        let rules = RuleSet {
            layers: vec![layer("app", &["low", "high"])],
            deny: vec![deny("high", "low")],
            ..RuleSet::default()
        };
        let modules = module_set(&["high", "low"]);
        let index = rules.build_layer_index(&modules);
        let mut out = Vec::new();
        rules.check_layers("high", "low", &index, &BTreeSet::new(), &mut out);
        rules.check_deny("high", "low", &BTreeSet::new(), &mut out);
        out.sort();
        assert_eq!(out.len(), 2);
        assert_eq!(out[0].kind, ViolationKind::Deny);
        assert_eq!(out[1].kind, ViolationKind::Layer);
    }

    // --- restrict ----------------------------------------------------------

    fn restrict(from: &str, to: &[&str]) -> RestrictRule {
        RestrictRule {
            from: ModulePattern::parse(from),
            to: to
                .iter()
                .map(|target| ModulePattern::parse(target))
                .collect(),
        }
    }

    fn restrict_rules(rules: Vec<RestrictRule>) -> RuleSet {
        RuleSet {
            restrict: rules,
            ..RuleSet::default()
        }
    }

    #[test]
    fn restrict_allows_listed_targets() {
        let rules = restrict_rules(vec![restrict("cli", &["analyzer", "web::*"])]);
        let mut out = Vec::new();
        rules.check_restrict("cli", "analyzer", &BTreeSet::new(), &mut out);
        rules.check_restrict("cli", "web::repo", &BTreeSet::new(), &mut out);
        assert!(out.is_empty());
    }

    #[test]
    fn restrict_flags_a_target_outside_the_allowance() {
        let rules = restrict_rules(vec![restrict("cli", &["analyzer"])]);
        let mut out = Vec::new();
        rules.check_restrict("cli", "web::repo", &BTreeSet::new(), &mut out);
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].kind, ViolationKind::Restrict);
        assert_eq!(out[0].rule, "restrict cli -> [analyzer]");
    }

    #[test]
    fn restrict_empty_allowance_blocks_every_outbound_edge() {
        let rules = restrict_rules(vec![restrict("web::*", &[])]);
        let mut out = Vec::new();
        rules.check_restrict("web::api", "analyzer", &BTreeSet::new(), &mut out);
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].rule, "restrict web::* -> []");
    }

    #[test]
    fn restrict_exempts_parent_child_edges_both_ways() {
        // An exact `from` cannot match its own children, so the global
        // containment filter must exempt these edges before any rule fires.
        let rules = restrict_rules(vec![restrict("cli", &[])]);
        let mut out = Vec::new();
        rules.check_restrict("cli", "cli::validation", &BTreeSet::new(), &mut out);
        rules.check_restrict("cli::validation", "cli", &BTreeSet::new(), &mut out);
        assert!(out.is_empty(), "parent <-> own child is one unit");
    }

    #[test]
    fn restrict_exempts_siblings_inside_the_scope() {
        // Neither endpoint is the other's ancestor, but both fall under the
        // rule's `from` — the edge never leaves the restricted subsystem.
        let rules = restrict_rules(vec![restrict("web::*", &[])]);
        let mut out = Vec::new();
        rules.check_restrict("web::api", "web::service", &BTreeSet::new(), &mut out);
        assert!(out.is_empty(), "sibling edge inside the scope is internal");
    }

    #[test]
    fn restrict_overlapping_rules_intersect_allowances() {
        // The edge satisfies the broad rule but not the narrow one — the
        // narrow rule still fires (intersection, not override).
        let rules = restrict_rules(vec![
            restrict("cli", &["analyzer", "web::*"]),
            restrict("cli", &["analyzer"]),
        ]);
        let mut out = Vec::new();
        rules.check_restrict("cli", "web::repo", &BTreeSet::new(), &mut out);
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].rule, "restrict cli -> [analyzer]");
    }

    #[test]
    fn restrict_edge_breaking_several_rules_reports_each() {
        let rules = restrict_rules(vec![
            restrict("cli", &["analyzer"]),
            restrict("cli", &["parser"]),
        ]);
        let mut out = Vec::new();
        rules.check_restrict("cli", "web::repo", &BTreeSet::new(), &mut out);
        assert_eq!(out.len(), 2, "one violation per matching rule");
    }

    #[test]
    fn restrict_ignores_an_uncovered_module() {
        let rules = restrict_rules(vec![restrict("cli", &["analyzer"])]);
        let mut out = Vec::new();
        rules.check_restrict("analyzer", "parser", &BTreeSet::new(), &mut out);
        assert!(out.is_empty(), "module outside every rule is unrestricted");
    }

    #[test]
    fn restrict_sorts_after_deny_and_before_layer() {
        let rules = RuleSet {
            layers: vec![layer("app", &["low", "high"])],
            deny: vec![deny("high", "low")],
            restrict: vec![restrict("high", &[])],
            ..RuleSet::default()
        };
        let modules = module_set(&["high", "low"]);
        let index = rules.build_layer_index(&modules);
        let mut out = Vec::new();
        rules.check_layers("high", "low", &index, &BTreeSet::new(), &mut out);
        rules.check_deny("high", "low", &BTreeSet::new(), &mut out);
        rules.check_restrict("high", "low", &BTreeSet::new(), &mut out);
        out.sort();
        assert_eq!(out.len(), 3);
        assert_eq!(out[0].kind, ViolationKind::Deny);
        assert_eq!(out[1].kind, ViolationKind::Restrict);
        assert_eq!(out[2].kind, ViolationKind::Layer);
    }

    // --- deny-cycles -------------------------------------------------------

    fn cycle(modules: &[&str], edges: &[(&str, &str)]) -> Cycle {
        let annotated: AnnotatedEdges = edges
            .iter()
            .map(|(source, target)| {
                (
                    ((*source).to_owned(), (*target).to_owned()),
                    BTreeSet::new(),
                )
            })
            .collect();
        Cycle::new(module_set(modules), annotated)
    }

    fn allow(modules: &[&str]) -> AllowedCycle {
        AllowedCycle {
            modules: module_set(modules),
            reason: None,
        }
    }

    fn cycle_rules(allow_cycles: Vec<AllowedCycle>, deny_parent_child_cycles: bool) -> RuleSet {
        RuleSet {
            allow_cycles,
            deny_cycles: true,
            deny_parent_child_cycles,
            ..RuleSet::default()
        }
    }

    /// The `rules <-> rules::eval <-> rules::load` shape crawk itself has.
    fn parent_child_cycle() -> Cycle {
        cycle(
            &["rules", "rules::eval", "rules::load"],
            &[
                ("rules", "rules::eval"),
                ("rules", "rules::load"),
                ("rules::eval", "rules"),
                ("rules::load", "rules"),
            ],
        )
    }

    fn abc_cycle() -> Cycle {
        cycle(
            &["alpha", "beta", "gamma"],
            &[("alpha", "beta"), ("beta", "gamma"), ("gamma", "alpha")],
        )
    }

    #[test]
    fn cycle_yields_one_violation_per_edge() {
        let rules = cycle_rules(Vec::new(), false);
        let mut out = Vec::new();
        rules.check_cycles(&[abc_cycle()], &mut out);
        assert_eq!(out.len(), 3);
        assert!(out.iter().all(|v| v.kind == ViolationKind::Cycle));
        assert!(
            out.iter().all(|v| v.rule == "cycle: alpha, beta, gamma"),
            "every row cites the whole loop"
        );
    }

    #[test]
    fn parent_child_cycle_is_skipped_by_default() {
        let rules = cycle_rules(Vec::new(), false);
        let mut out = Vec::new();
        rules.check_cycles(&[parent_child_cycle()], &mut out);
        assert!(
            out.is_empty(),
            "containment loop is structural, not a tangle"
        );
    }

    #[test]
    fn parent_child_cycle_is_reported_when_knob_set() {
        let rules = cycle_rules(Vec::new(), true);
        let mut out = Vec::new();
        rules.check_cycles(&[parent_child_cycle()], &mut out);
        assert_eq!(out.len(), 4, "the knob turns the filter off");
    }

    #[test]
    fn cycle_across_unrelated_subtrees_is_reported() {
        // Two of the three modules share a subtree, but no module is an ancestor
        // of them all — a submodule tangled with a foreign subtree is real.
        let rules = cycle_rules(Vec::new(), false);
        let tangle = cycle(
            &["graph", "rules", "rules::eval"],
            &[
                ("graph", "rules"),
                ("rules", "rules::eval"),
                ("rules::eval", "graph"),
            ],
        );
        let mut out = Vec::new();
        rules.check_cycles(&[tangle], &mut out);
        assert_eq!(out.len(), 3);
    }

    #[test]
    fn allowlist_covers_cycle_by_subset() {
        // The entry names the loop as it was found; after a partial fix the loop
        // is smaller, and a subset still passes.
        let rules = cycle_rules(vec![allow(&["alpha", "beta", "gamma"])], false);
        let shrunk = cycle(&["alpha", "beta"], &[("alpha", "beta"), ("beta", "alpha")]);
        let mut out = Vec::new();
        rules.check_cycles(&[shrunk], &mut out);
        assert!(out.is_empty());
    }

    #[test]
    fn allowlist_missing_a_module_still_reports() {
        // A module joining the loop breaks the subset — that is the ratchet.
        let rules = cycle_rules(vec![allow(&["alpha", "beta"])], false);
        let mut out = Vec::new();
        rules.check_cycles(&[abc_cycle()], &mut out);
        assert_eq!(out.len(), 3);
    }

    #[test]
    fn allowlist_entry_covers_a_parent_child_loop_too() {
        // Matching runs before the structural filter, so an entry aimed at a
        // containment loop is still marked used (it must not warn as stale).
        let rules = cycle_rules(vec![allow(&["rules", "rules::eval", "rules::load"])], false);
        let mut out = Vec::new();
        rules.check_cycles(&[parent_child_cycle()], &mut out);
        assert!(out.is_empty());
    }

    #[test]
    fn cycles_sort_after_deny_and_layer_violations() {
        let rules = RuleSet {
            layers: vec![layer("app", &["low", "high"])],
            deny: vec![deny("high", "low")],
            deny_cycles: true,
            ..RuleSet::default()
        };
        let modules = module_set(&["high", "low"]);
        let index = rules.build_layer_index(&modules);
        let mut out = Vec::new();
        rules.check_layers("high", "low", &index, &BTreeSet::new(), &mut out);
        rules.check_deny("high", "low", &BTreeSet::new(), &mut out);
        rules.check_cycles(
            &[cycle(&["high", "low"], &[("high", "low"), ("low", "high")])],
            &mut out,
        );
        out.sort();
        assert_eq!(out.len(), 4);
        assert_eq!(out[0].kind, ViolationKind::Deny);
        assert_eq!(out[1].kind, ViolationKind::Layer);
        assert_eq!(out[2].kind, ViolationKind::Cycle);
    }

    #[test]
    fn unassigned_module_is_skipped() {
        let rules = app_rules();
        let modules = module_set(&["cli"]);
        let index = rules.build_layer_index(&modules);
        let mut out = Vec::new();
        // "stray" is not in any layer → no constraint.
        rules.check_layers("stray", "cli", &index, &BTreeSet::new(), &mut out);
        rules.check_layers("cli", "stray", &index, &BTreeSet::new(), &mut out);
        assert!(out.is_empty());
    }
}