cuke-dedup 0.11.0

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

fn definitions(source: &str) -> Vec<StepDefinition> {
    crate::typescript::extract(
        source,
        &SourceFile {
            path: PathBuf::from("steps.ts"),
            language: SourceLanguage::TypeScript,
        },
    )
    .unwrap()
}

/// Matcher summaries as the step-matching pass computes them, for direct overlap calls.
fn summaries(definitions: &[StepDefinition], config: &Config) -> Vec<MatcherSummary> {
    definitions
        .iter()
        .map(|definition| {
            let matcher = compile_matcher(definition, &config.parameter_types).0;
            MatcherSummary {
                authoritative: matcher.authoritative,
                has_regex: matcher.regex.is_some(),
            }
        })
        .collect()
}

fn config() -> (tempfile::TempDir, Config) {
    let directory = tempfile::tempdir().unwrap();
    let config = Config::load(directory.path(), ConfigOverrides::default()).unwrap();
    (directory, config)
}

type OverlapFixture = (
    tempfile::TempDir,
    Vec<StepDefinition>,
    Config,
    Vec<MatcherSummary>,
    Vec<(usize, String)>,
);

fn overlap_fixture(source: &str) -> OverlapFixture {
    let definitions = definitions(source);
    let (directory, mut config) = config();
    config
        .parameter_types
        .insert("colour".to_owned(), "red".to_owned());
    let summaries = summaries(&definitions, &config);
    let witnesses = vec![(0, "value red".to_owned())];
    (directory, definitions, config, summaries, witnesses)
}

#[test]
fn cucumber_expression_expansion_stops_at_the_regex_pattern_limit() {
    let matcher = ".".repeat(600_000);
    assert!(matches!(
        cucumber_regex_expression(&matcher),
        CucumberRegexExpression::ResourceLimit
    ));
}

#[test]
fn regex_pattern_limit_is_inclusive() {
    let mut definition = crate::typescript::extract(
        "Given(/x/, () => work());",
        &SourceFile {
            path: PathBuf::from("steps.ts"),
            language: SourceLanguage::TypeScript,
        },
    )
    .unwrap()
    .remove(0);
    definition.matcher = format!(
        "(?x){}",
        " ".repeat(MAX_REGEX_PATTERN_BYTES.saturating_sub(4))
    );

    let (compiled, error) = compile_matcher(&definition, &BTreeMap::new());
    assert!(error.is_none());
    assert!(compiled.regex.is_some());
}

#[test]
fn broad_ambiguity_groups_use_linear_membership_storage() {
    let matched = (0..10_000).collect::<BTreeSet<_>>();
    let mut proven = ProvenAmbiguities::new(matched.len());

    proven.record(&matched);
    proven.record(&matched);

    assert_eq!(proven.groups.len(), 1);
    assert_eq!(
        proven.memberships.iter().map(Vec::len).sum::<usize>(),
        10_000
    );
    assert!(proven.contains(0, 9_999));
}

#[test]
fn matcher_overlap_respects_the_shared_candidate_budget() {
    let definitions = definitions(
        "Given('value {first}', () => first());\n\
         Given('value {second}', () => second());\n\
         Given('value {third}', () => third());",
    );
    let (_directory, mut config) = config();
    for name in ["first", "second", "third"] {
        config
            .parameter_types
            .insert(name.to_owned(), "red|green".to_owned());
    }
    let suppressions = SuppressionIndex::new(&config, &definitions);
    let mut findings = Vec::new();

    let outcome =
        analyze_feature_usage(&definitions, &[], &config, &suppressions, &mut findings, 1);

    assert_eq!(outcome.overlap_census.evaluated, 1);
    assert_eq!(outcome.overlap_census.skipped, 1);
    assert!(outcome
        .incomplete
        .iter()
        .any(|diagnostic| diagnostic.contains("matcher-overlap analysis is incomplete")));
}

/// Witness index scans are charged against the configured limit before any candidate is proposed.
#[test]
fn matcher_overlap_charges_index_scans_before_candidate_generation() {
    let definitions = definitions(
        "Given('value {first}', () => first());\n\
         Given('value {second}', () => second());\n\
         Given('value {third}', () => third());\n\
         Given('value {fourth}', () => fourth());\n\
         Given('value {fifth}', () => fifth());",
    );
    let (_directory, mut config) = config();
    config.max_candidate_comparisons = 1;
    for name in ["first", "second", "third", "fourth", "fifth"] {
        config
            .parameter_types
            .insert(name.to_owned(), format!("{name}-only"));
    }
    let summaries = summaries(&definitions, &config);
    let suppressions = SuppressionIndex::new(&config, &definitions);
    let mut findings = Vec::new();

    // One definition per window puts one automaton scan per definition into the witness scan
    // charge, so the scan budget is exceeded before any candidate pair is proposed. Previously the
    // same condition was produced by shrinking the regex limits until one index split into many
    // sets; windowing is now the mechanism that multiplies scans.
    let outcome = analyze_matcher_overlap(
        &definitions,
        &summaries,
        &ProvenAmbiguities::new(definitions.len()),
        &BTreeMap::new(),
        &config,
        &suppressions,
        &mut findings,
        1,
        1,
        // Scan work the step-matching pass reports for this corpus at one definition per window:
        // one automaton per window. With a work budget of 1 the witness scan budget is 4, so not
        // even one witness is affordable and nothing is scanned.
        definitions.len(),
    );

    assert_eq!(outcome.census.evaluated, 0);
    assert_eq!(outcome.census.skipped, 1);
    assert!(outcome.incomplete.is_some());
    assert!(findings.is_empty());
}

/// Stored overlap proposals never exceed the budget, even when one witness's match list crosses
/// it inside a single window, and the proposable match that did not fit is reported.
#[test]
fn matcher_overlap_proposals_stop_at_the_budget_inside_one_match_list() {
    // Six regexes accept the one witness `value red`: one match list of six against a budget of 4.
    let (_directory, definitions, config, summaries, witnesses) = overlap_fixture(
        "Given('value {colour}', () => colour());\n\
         Given(/^value red$/, () => one());\n\
         Given(/^value r.*$/, () => two());\n\
         Given(/^value .*$/, () => three());\n\
         Given(/^val.*$/, () => four());\n\
         Given(/^v.*$/, () => five());\n\
         Given(/^.*red$/, () => six());",
    );
    let collect = |window, budget| {
        collect_overlap_proposals(
            &definitions,
            &summaries,
            &config.parameter_types,
            window,
            &witnesses,
            budget,
            None,
        )
    };
    assert_eq!(
        collect(definitions.len(), 4),
        (
            vec![vec![1, 2, 3, 4]],
            true,
            OverlapProposalSource::CompiledWindows
        )
    );
    assert_eq!(
        collect(definitions.len(), 6),
        (
            vec![vec![1, 2, 3, 4, 5, 6]],
            false,
            OverlapProposalSource::CompiledWindows
        )
    );
}

#[test]
fn complete_feature_matches_replace_only_an_untruncated_overlap_scan() {
    let (_directory, definitions, config, summaries, witnesses) = overlap_fixture(
        "Given('value {colour}', () => colour());\n\
         Given(/^value red$/, () => regex());\n\
         Given('value {colour}', () => duplicate());",
    );
    let complete = BTreeMap::from([("value red", vec![0, 1, 2])]);

    assert_eq!(
        cached_overlap_proposals(&definitions, &summaries, &witnesses, 1, &complete),
        Some(vec![vec![1]])
    );
    let cached = collect_overlap_proposals(
        &definitions,
        &summaries,
        &config.parameter_types,
        1,
        &witnesses,
        1,
        Some(&complete),
    );
    let compiled = collect_overlap_proposals(
        &definitions,
        &summaries,
        &config.parameter_types,
        1,
        &witnesses,
        1,
        None,
    );
    assert_eq!(cached.0, compiled.0);
    assert!(!cached.1 && !compiled.1);
    assert_eq!(cached.2, OverlapProposalSource::FeatureCache);
    assert_eq!(compiled.2, OverlapProposalSource::CompiledWindows);
    assert_eq!(
        cached_overlap_proposals(&definitions, &summaries, &witnesses, 0, &complete),
        None,
        "an over-budget cache must preserve the compiled path's partial ordering"
    );
    assert_eq!(
        collect_overlap_proposals(
            &definitions,
            &summaries,
            &config.parameter_types,
            1,
            &witnesses,
            0,
            Some(&complete),
        )
        .2,
        OverlapProposalSource::CompiledWindows
    );
    assert_eq!(
        cached_overlap_proposals(&definitions, &summaries, &witnesses, 1, &BTreeMap::new()),
        None,
        "an unobserved witness must still scan every matcher window"
    );
    assert_eq!(
        collect_overlap_proposals(
            &definitions,
            &summaries,
            &config.parameter_types,
            1,
            &witnesses,
            1,
            Some(&BTreeMap::new()),
        )
        .2,
        OverlapProposalSource::CompiledWindows
    );
}

/// A `Break` from the visitor ends the window walk: no later window is compiled or visited.
#[test]
fn scan_windows_stop_at_a_break() {
    let definitions = definitions(
        "Given('one', () => one());\n\
         Given('two', () => two());\n\
         Given('three', () => three());",
    );
    let (_directory, config) = config();
    let mut visited = Vec::new();
    let work = scan_windows(
        &definitions,
        &config.parameter_types,
        1,
        |start, _, _, _| {
            visited.push(start);
            if start == 1 {
                ControlFlow::Break(())
            } else {
                ControlFlow::Continue(())
            }
        },
    );
    assert_eq!(visited, vec![0, 1]);
    // Only the two visited windows are compiled and charged.
    let whole = scan_windows(&definitions, &config.parameter_types, 1, |_, _, _, _| {
        ControlFlow::Continue(())
    });
    assert_eq!((work, whole), (2, 3));
}

#[test]
fn reverse_witnesses_do_not_consume_the_unique_pair_budget_twice() {
    let definitions = definitions(
        "Given('value {first}', () => first());\n\
         Given('value {second}', () => second());",
    );
    let (_directory, mut config) = config();
    for name in ["first", "second"] {
        config
            .parameter_types
            .insert(name.to_owned(), "red|green".to_owned());
    }
    let suppressions = SuppressionIndex::new(&config, &definitions);
    let mut findings = Vec::new();

    let outcome =
        analyze_feature_usage(&definitions, &[], &config, &suppressions, &mut findings, 1);

    assert_eq!(outcome.overlap_census.evaluated, 1);
    assert_eq!(outcome.overlap_census.skipped, 0);
    assert!(outcome.incomplete.is_empty());
    assert_eq!(findings.len(), 1);
}

#[test]
fn matcher_overlap_caps_finding_storms_and_marks_the_result_incomplete() {
    let mut source = String::new();
    let mut parameter_types = BTreeMap::new();
    for index in 0..150 {
        source.push_str(&format!(
            "Given('value {{type_{index}}}', () => action_{index}());\n"
        ));
        parameter_types.insert(format!("type_{index}"), "red|green".to_owned());
    }
    let definitions = definitions(&source);
    let (_directory, mut config) = config();
    config.parameter_types = parameter_types;
    let suppressions = SuppressionIndex::new(&config, &definitions);
    let mut findings = Vec::new();

    let outcome = analyze_feature_usage(
        &definitions,
        &[],
        &config,
        &suppressions,
        &mut findings,
        30_000,
    );

    assert_eq!(findings.len(), MAX_STATIC_OVERLAP_FINDINGS);
    assert_eq!(outcome.overlap_census.skipped, 1);
    assert!(outcome
        .incomplete
        .iter()
        .any(|diagnostic| diagnostic.contains("matcher-overlap analysis is incomplete")));
    assert!(findings
        .iter()
        .all(|finding| finding.rule == Rule::OverlappingMatcher));
}

#[test]
fn oversized_combined_matcher_programs_split_without_changing_matches() {
    let compiled = (0..200)
        .map(|index| {
            let expression = format!("^distinct matcher value {index:04}$");
            CompiledMatcher {
                regex: Some(Regex::new(&expression).unwrap()),
                expression: Some(expression),
                authoritative: true,
            }
        })
        .collect::<Vec<_>>();

    let index = MatcherIndex::build_with_limits(&compiled, 4 * 1024, 64 * 1024);
    assert!(index.sets.len() > 1);
    assert!(index.fallback_definitions.is_empty());

    let mut matches = Vec::new();
    index.matches(&compiled, "distinct matcher value 0123", &mut matches);
    assert_eq!(matches, vec![123]);
}

#[test]
fn matcher_index_falls_back_when_even_one_combined_pattern_is_rejected() {
    let compiled = vec![CompiledMatcher {
        regex: Some(Regex::new("^fallback$").unwrap()),
        expression: Some("(".to_owned()),
        authoritative: true,
    }];
    let index = MatcherIndex::build_with_limits(&compiled, 32, 32);
    assert!(index.sets.is_empty());
    assert_eq!(index.fallback_definitions, [0]);
    let mut matches = Vec::new();
    index.matches(&compiled, "fallback", &mut matches);
    assert_eq!(matches, [0]);
}

#[test]
fn ambiguity_membership_intersection_handles_ordered_disjoint_groups() {
    let mut proven = ProvenAmbiguities::new(5);
    proven.record(&[0, 2].into_iter().collect());
    proven.record(&[1, 3].into_iter().collect());
    proven.record(&[2, 4].into_iter().collect());
    assert!(!proven.contains(0, 1));
    assert!(proven.contains(0, 2));
    assert!(!proven.contains(1, 4));
}

#[test]
fn feature_usage_caches_repeated_text_and_supports_suppressed_ambiguity() {
    let definitions =
        definitions("Given('same step', () => first());\nGiven(/^same step$/, () => second());");
    let steps = crate::gherkin::extract(
        "Feature: F\n  Scenario Outline: S\n    Given same step\n    And same step\n    Examples:\n      | value |\n      | one |\n",
        Path::new("example.feature"),
    )
    .unwrap();
    let (_directory, mut config) = config();
    config.suppressions.push(SuppressionConfig {
        rule: Rule::AmbiguousStep,
        reason: "known overlap".to_owned(),
        path: Some("steps.ts".to_owned()),
        matcher: None,
    });
    let suppressions = SuppressionIndex::new(&config, &definitions);
    let mut findings = Vec::new();
    let outcome = analyze_feature_usage(
        &definitions,
        &steps,
        &config,
        &suppressions,
        &mut findings,
        100,
    );
    assert_eq!(outcome.used, [0, 1].into_iter().collect());
    assert_eq!(findings.len(), 2);
    assert!(findings
        .iter()
        .all(|finding| finding.suppression.as_ref().unwrap().reason == "known overlap"));
}

#[test]
fn overlap_and_unused_rules_honor_disabled_severity_and_usage() {
    let definitions = definitions("Given('unused', () => work());");
    let (_directory, mut config) = config();
    config.rules.insert(Rule::OverlappingMatcher, Severity::Off);
    config.rules.insert(Rule::UnusedDefinition, Severity::Off);
    let suppressions = SuppressionIndex::new(&config, &definitions);
    let mut findings = Vec::new();
    let outcome = analyze_feature_usage(
        &definitions,
        &[],
        &config,
        &suppressions,
        &mut findings,
        100,
    );
    analyze_unused(
        &definitions,
        &BTreeSet::new(),
        &config,
        &suppressions,
        &mut findings,
    );
    assert_eq!(outcome.overlap_census, CandidateSourceCensus::default());
    assert!(findings.is_empty());

    config
        .rules
        .insert(Rule::UnusedDefinition, Severity::Warning);
    let suppressions = SuppressionIndex::new(&config, &definitions);
    analyze_unused(
        &definitions,
        &[0].into_iter().collect(),
        &config,
        &suppressions,
        &mut findings,
    );
    assert!(findings.is_empty());
}

#[test]
fn non_authoritative_matchers_cannot_prove_a_definition_unused() {
    let mut extracted = definitions("Given('a {projectType}', () => work());");
    extracted.push(definitions("Given(/^value$/v, () => work());").remove(0));
    let (_directory, mut config) = config();
    config
        .rules
        .insert(Rule::UnusedDefinition, Severity::Warning);
    let suppressions = SuppressionIndex::new(&config, &extracted);
    let mut findings = Vec::new();

    let outcome =
        analyze_feature_usage(&extracted, &[], &config, &suppressions, &mut findings, 100);
    analyze_unused(
        &extracted,
        &outcome.used,
        &config,
        &suppressions,
        &mut findings,
    );

    assert_eq!(outcome.used, [0, 1].into_iter().collect());
    assert!(findings.is_empty());
}

#[test]
fn witness_generation_covers_builtins_declared_literals_and_unknowns() {
    let mut definitions = definitions(
        "Given('I have {int} item(s) in red/blue', () => work());\nGiven(/^regex$/, () => work());",
    );
    assert_eq!(
        witness_for(&definitions[0], &BTreeMap::new()).as_deref(),
        Some("I have 1 items in red")
    );
    assert!(witness_for(&definitions[1], &BTreeMap::new()).is_none());
    definitions[0].matcher = "a {colour}".to_owned();
    assert!(witness_for(&definitions[0], &BTreeMap::new()).is_none());
    for (pattern, expected) in [
        ("red|green", Some("red")),
        ("(red|green)", Some("red")),
        ("(?:red|green)", Some("red")),
        ("(?m:red|green)", Some("red")),
        ("(?ms:red|green)", Some("red")),
        ("((red|green))", Some("red")),
        ("(red)|(green)", None),
        ("", None),
        ("red.*", None),
        ("(red", None),
    ] {
        for framework in [
            crate::model::Framework::CucumberJs,
            crate::model::Framework::CucumberRuby,
        ] {
            definitions[0].framework = framework;
            assert_eq!(
                definitions[0]
                    .analysis_profile()
                    .dialect
                    .parameter_sample(pattern)
                    .as_deref(),
                expected,
                "{framework:?}/{pattern}"
            );
        }
    }
    assert_eq!(witness_literal(r"a\(b) c/d"), "a(b) c");
    assert_eq!(witness_literal("open(unclosed"), "open(unclosed");
}

#[test]
fn fallback_expression_handles_optional_alternative_and_declared_parameters() {
    let parameters = BTreeMap::from([("colour".to_owned(), "red|green".to_owned())]);
    let exact = fallback_cucumber_expression_regex(
        "I eat/eats {int} cucumber(s) with {colour}",
        &parameters,
    );
    assert!(exact.exact);
    let regex = Regex::new(&exact.expression).unwrap();
    assert!(regex.is_match("I eat 2 cucumbers with red"));
    assert!(regex.is_match("I eats -2 cucumber with green"));

    let approximate = fallback_cucumber_expression_regex("a {custom}", &BTreeMap::new());
    assert!(!approximate.exact);
    assert!(Regex::new(&approximate.expression)
        .unwrap()
        .is_match("a anything"));
    assert_eq!(
        fallback_cucumber_token_regex("open(unclosed"),
        r"open\(unclosed"
    );
    assert!(fallback_cucumber_literal_regex("two  spaces").contains("two"));
}

#[test]
fn matcher_compilation_reports_limits_but_not_unsupported_syntax() {
    let mut definition = definitions("Given(/^valid$/, () => work());").remove(0);
    definition.matcher = "x".repeat(MAX_REGEX_PATTERN_BYTES + 1);
    let (compiled, diagnostic) = compile_matcher(&definition, &BTreeMap::new());
    assert!(compiled.regex.is_none());
    assert!(diagnostic.unwrap().contains("regex resource limit"));

    definition.matcher = "(".to_owned();
    let (compiled, diagnostic) = compile_matcher(&definition, &BTreeMap::new());
    assert!(compiled.regex.is_none());
    assert!(diagnostic.is_none());

    definition.matcher_kind = MatcherKind::CucumberExpression;
    definition.matcher = "x".repeat(MAX_REGEX_PATTERN_BYTES + 1);
    let (compiled, diagnostic) = compile_matcher(&definition, &BTreeMap::new());
    assert!(compiled.regex.is_none());
    assert!(diagnostic.unwrap().contains("regex resource limit"));
}

/// Windowing must not change what is reported, only how much is alive at once.
///
/// Each row is an axis from the windowing matrix. Every row runs at one definition per window, at
/// two, and at a window wider than the corpus, and all three must produce identical findings. At
/// window 1 every pair is cross-window, so the merge paths are exercised by construction — a fixture
///-sized corpus never crosses a boundary at the default window of 250.
#[test]
fn windowing_does_not_change_findings_on_any_axis() {
    struct Case {
        axis: &'static str,
        source: &'static str,
        feature: &'static str,
    }
    let cases = [
        Case {
            axis: "step matching definitions in more than one window",
            source: "Given('the gate opens {word}', () => a());\nGiven(/^the gate opens wide$/, () => b());\nGiven('the gate opens wide', () => c());",
            feature: "Feature: F\n  Scenario: S\n    Given the gate opens wide\n",
        },
        Case {
            // The hazard: a proven ambiguity split across windows must still withhold the overlap
            // finding, because `ambiguous-step` owns that pair.
            axis: "proven ambiguity pair split across windows withholds the overlap",
            source: "Given('the dial reads {int}', () => a());\nGiven(/^the dial reads \\d+$/, () => b());",
            feature: "Feature: F\n  Scenario: S\n    Given the dial reads 7\n",
        },
        Case {
            axis: "overlap with no feature corpus at all",
            source: "Given('the pump runs {word}', () => a());\nGiven(/^the pump runs .+$/, () => b());",
            feature: "Feature: F\n  Scenario: S\n    Given something unrelated\n",
        },
        Case {
            axis: "equivalent-matcher group spanning windows keeps one representative witness",
            source: "Given('the lamp glows {word}', () => a());\nGiven('the lamp glows {word}', () => b());\nGiven(/^the lamp glows .+$/, () => c());",
            feature: "Feature: F\n  Scenario: S\n    Given nothing here\n",
        },
        Case {
            axis: "unused definitions are indeterminate or proven across windows",
            source: "Given('the seal closes', () => a());\nGiven('the rotor spins', () => b());\nGiven('the brake holds', () => c());",
            feature: "Feature: F\n  Scenario: S\n    Given the rotor spins\n",
        },
    ];

    for case in cases {
        let definitions = definitions(case.source);
        let steps = crate::gherkin::extract(case.feature, Path::new("example.feature")).unwrap();
        let (_directory, config) = config();
        let suppressions = SuppressionIndex::new(&config, &definitions);

        // Findings and `used` are window-invariant **for runs that do not truncate**, which is what
        // these cases are. Once a budget is exhausted that no longer holds: the witness-scan charge
        // grows as windows shrink, and the proposal cap cuts a window-major stream where a single
        // pass cut a witness-major one, so both which pairs survive and whether the run truncates
        // become window-dependent. These cases run well inside their budgets, where every output
        // must agree, and asserting all four pins that — it is not a claim of invariance under
        // truncation.
        /// Everything one window size observed, so two sizes can be compared field by field.
        struct Observed {
            findings: Vec<String>,
            used: Vec<usize>,
            incomplete: Vec<String>,
            evaluated: u64,
        }
        let mut reference: Option<Observed> = None;
        for window in [1, 2, definitions.len() + 5] {
            let mut findings = Vec::new();
            let outcome = analyze_feature_usage_in_windows(
                &definitions,
                &steps,
                &config,
                &suppressions,
                &mut findings,
                1_000,
                window,
            );
            let mut rendered: Vec<String> = findings
                .iter()
                .map(|finding| {
                    format!(
                        "{:?}|{}|{}|{:?}",
                        finding.rule,
                        finding.primary.line,
                        finding.message,
                        finding
                            .related
                            .iter()
                            .map(|location| location.line)
                            .collect::<Vec<_>>()
                    )
                })
                .collect();
            rendered.sort();
            let used = outcome.used.iter().copied().collect::<Vec<_>>();
            let incomplete = outcome.incomplete.clone();
            let evaluated = u64::try_from(outcome.overlap_census.evaluated).unwrap();
            match &reference {
                None => {
                    reference = Some(Observed {
                        findings: rendered,
                        used,
                        incomplete,
                        evaluated,
                    });
                }
                Some(expected) => {
                    assert_eq!(
                        &rendered, &expected.findings,
                        "{}: findings changed at window {window}",
                        case.axis
                    );
                    assert_eq!(
                        &used, &expected.used,
                        "{}: used set changed at window {window}",
                        case.axis
                    );
                    assert_eq!(
                        &incomplete, &expected.incomplete,
                        "{}: incomplete diagnostics changed at window {window}",
                        case.axis
                    );
                    assert_eq!(
                        evaluated, expected.evaluated,
                        "{}: overlap census changed at window {window}",
                        case.axis
                    );
                }
            }
        }
        // A case must discriminate, or comparing it across window sizes proves nothing. Findings
        // are the observable for the ambiguity and overlap axes; for the usage axis it is a `used`
        // set that is neither empty nor everything, meaning the pass actually classified.
        let observed = reference.expect("at least one window was evaluated");
        assert!(
            !observed.findings.is_empty()
                || (!observed.used.is_empty() && observed.used.len() < definitions.len()),
            "{}: no finding and no partial used set, so the comparison proves nothing",
            case.axis
        );
    }
}