rust-doctor 0.6.0

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

use serde::Deserialize;
use serde_json::Value;

use super::*;
use crate::policy::{CATALOG, Producer};
use crate::report::{DiagnosticSource, DiagnosticSpan};

mod lambda_freeze;
mod ranking;
mod scale;

/// The block passes the rule the score it computes ranks. `oversized_unit` reports a file at a
/// thousand lines, and this module holds that bound: these tests and the source inventory have
/// files of their own for that reason, and a file that grows back past it fails here rather than
/// on a self-scan nobody reads.
#[test]
fn the_audit_holds_the_size_bound_it_scores_for() {
    for own in [
        include_str!("../audit.rs"),
        include_str!("density.rs"),
        include_str!("source_inventory.rs"),
        include_str!("tests.rs"),
        include_str!("tests/lambda_freeze.rs"),
        include_str!("tests/ranking.rs"),
        include_str!("tests/scale.rs"),
    ] {
        let lines = own.lines().count();
        assert!(
            lines < crate::structure::FILE_LINES,
            "a file of the audit block is {lines} lines long, over the {} it reports",
            crate::structure::FILE_LINES
        );
    }
}

/// The categories are published in their declaration order, and nothing restates that order.
///
/// `Ord` derives from the declaration, the tally map is keyed by it and `Audit::is_valid` checks
/// it, so this is the one place the sequence itself is written down and frozen.
#[test]
fn the_categories_are_published_in_their_declaration_order() {
    let declared = [
        AuditCategoryName::Security,
        AuditCategoryName::Bugs,
        AuditCategoryName::Performance,
        AuditCategoryName::Dependencies,
        AuditCategoryName::Maintainability,
        AuditCategoryName::Other,
    ];
    let mut sorted = declared;
    sorted.sort();
    assert_eq!(sorted, declared);

    let tallies = category_tallies(&[
        diagnostic_with_category(Some("maintainability")),
        diagnostic_with_category(None),
        diagnostic_with_category(Some("security")),
    ]);
    assert_eq!(
        tallies
            .iter()
            .map(|tally| tally.name)
            .collect::<Vec<_>>(),
        [
            AuditCategoryName::Security,
            AuditCategoryName::Maintainability,
            AuditCategoryName::Other,
        ]
    );
}

/// `ScoreDimension::ALL` names every dimension exactly once.
///
/// The match is exhaustive on purpose: a dimension declared and forgotten in `ALL` stops this
/// file compiling, which no assertion over a length could catch.
#[test]
fn every_dimension_is_listed_once() {
    for dimension in ScoreDimension::ALL {
        match dimension {
            ScoreDimension::Security
            | ScoreDimension::Reliability
            | ScoreDimension::Maintainability
            | ScoreDimension::Performance
            | ScoreDimension::Dependencies => {}
        }
    }
    let distinct: BTreeSet<_> = ScoreDimension::ALL.into_iter().collect();
    assert_eq!(distinct.len(), ScoreDimension::ALL.len());
}

/// A rule that only ever fired outside production code is counted and shown, and costs nothing.
///
/// The two used to be separated by which population each caller aggregated over, so the cost the
/// report ranked by was not the cost the score charged.
#[test]
fn a_finding_outside_production_code_is_counted_and_charged_nothing() {
    let mut in_tests = diagnostic_with_category(Some("security"));
    in_tests.code = Some("clippy::todo".to_owned());
    in_tests.severity = Severity::Error;
    in_tests.occurrences = 40;
    in_tests.context = Some(crate::report::DiagnosticContext::Tests);

    let aggregation = aggregate_rules(100, [&in_tests]);
    let rule = aggregation
        .rules
        .first()
        .expect("the rule is aggregated like any other");
    assert_eq!(rule.occurrences, 40, "it stays counted");
    assert_eq!(rule.contribution(), 0, "and costs the score nothing");
    assert!(aggregation.diagnostics_are_authoritative);

    let audit = Audit::build(1, 100, Status::Complete, &[in_tests]);
    let score = audit.score.as_ref().expect("a scored workspace");
    assert_eq!(score.value, 100);
    assert!(score.projected_rule_ids.is_empty());
}

fn diagnostic_with_category(category: Option<&str>) -> Diagnostic {
    Diagnostic {
        context: None,
        id: format!("id-{}", category.unwrap_or("none")),
        source: DiagnosticSource::Clippy,
        code: Some(format!("clippy::{}", category.unwrap_or("bare"))),
        base_severity: Severity::Warning,
        severity: Severity::Warning,
        category: category.map(str::to_owned),
        message: "message".to_owned(),
        help: None,
        package: None,
        target: None,
        path: None,
        span: None,
        related: Vec::new(),
        similarity_basis_points: None,
        complexity: None,
        occurrences: 1,
    }
}

/// The producer names the oracle spells, paired with the enum they name.
const PRODUCERS: [(&str, Producer); 5] = [
    ("cargo-health", Producer::CargoHealth),
    ("clippy", Producer::Clippy),
    ("repo", Producer::Repo),
    ("source-kernel", Producer::SourceKernel),
    ("structure", Producer::Structure),
];

/// The dimension names the oracle spells, paired with the enum they name.
const DIMENSIONS: [(&str, ScoreDimension); 5] = [
    ("dependencies", ScoreDimension::Dependencies),
    ("maintainability", ScoreDimension::Maintainability),
    ("performance", ScoreDimension::Performance),
    ("reliability", ScoreDimension::Reliability),
    ("security", ScoreDimension::Security),
];

#[derive(Debug, Deserialize)]
struct Oracle {
    schema_version: u8,
    model: String,
    uncategorized_bucket: String,
    category_mappings: BTreeMap<String, CategoryExpectation>,
    rule_tiers: BTreeMap<String, String>,
    tier_ceilings: Vec<TierCeilingExpectation>,
    denominators: Vec<DenominatorExpectation>,
    score_boundaries: Vec<ScoreBoundaryExpectation>,
    density_cases: Vec<DensityExpectation>,
    score_cases: Vec<ScoreCase>,
    share_cases: Vec<ShareCase>,
}

#[derive(Debug, Deserialize)]
struct TierCeilingExpectation {
    tier: String,
    dimension: Option<u8>,
    overall: Option<u8>,
}

#[derive(Debug, Deserialize)]
struct DenominatorExpectation {
    producer: String,
    per_kiloline: bool,
}

#[derive(Debug, Deserialize)]
struct CategoryExpectation {
    display: String,
    dimension: String,
}

#[derive(Debug, Deserialize)]
struct ScoreBoundaryExpectation {
    name: String,
    dimensions: ScoreDimensions,
    expected_value: u8,
    expected_label: String,
    expected_counted_rule_ids: Vec<String>,
}

#[derive(Debug, Deserialize)]
struct DensityExpectation {
    dimension: String,
    density: f64,
    score: u8,
}

#[derive(Debug, Deserialize)]
struct ScoreCase {
    name: String,
    /// Denominator the case is scored against, recorded rather than derived
    /// from the file count: core-v3 reads a density, so a case whose line count
    /// is inferred by the test is a case the record cannot be replayed from.
    production_lines: usize,
    source_files: usize,
    complete: bool,
    diagnostics: Vec<OracleDiagnostic>,
    expected_audit: Value,
    expected_counted_rule_ids: Vec<String>,
}

#[derive(Debug, Deserialize)]
struct OracleDiagnostic {
    rule_id: Option<String>,
    category: Option<String>,
    severity: String,
    occurrences: usize,
}

#[derive(Debug, Deserialize)]
struct ShareCase {
    score: u8,
    errors: usize,
    warnings: usize,
    info: usize,
    files: usize,
    /// Production lines, absent from the two frozen oracles: they were taken
    /// before the count existed, and a count of zero is omitted from the
    /// query, so their expected URLs still hold. What the new key does is
    /// asserted by `the_share_url_carries_the_production_line_count`.
    #[serde(default)]
    lines: usize,
    expected: Option<String>,
}

fn oracle() -> Oracle {
    serde_json::from_str(include_str!(
        "../../tests/fixtures/local-cli-experience/audit-core-v3.json"
    ))
    .expect("audit oracle should be valid")
}

fn diagnostic(input: &OracleDiagnostic, index: usize) -> Diagnostic {
    Diagnostic {
        context: None,
        id: format!("finding-{index}"),
        source: DiagnosticSource::Clippy,
        code: input.rule_id.clone(),
        base_severity: severity(&input.severity).expect("oracle severity should be known"),
        severity: severity(&input.severity).expect("oracle severity should be known"),
        category: input.category.clone(),
        message: format!("oracle finding {index}"),
        help: None,
        package: None,
        target: None,
        path: Some(format!("src/{index}.rs")),
        span: Some(DiagnosticSpan {
            line_start: 1,
            column_start: 1,
            line_end: 1,
            column_end: 2,
        }),
        related: Vec::new(),
        similarity_basis_points: None,
        complexity: None,
        occurrences: input.occurrences,
    }
}

fn severity(value: &str) -> Option<Severity> {
    match value {
        "error" => Some(Severity::Error),
        "warning" => Some(Severity::Warning),
        "info" => Some(Severity::Info),
        "unknown" => Some(Severity::Unknown),
        _ => None,
    }
}

#[test]
fn versioned_oracle_covers_categories_labels_scores_and_rule_identity() {
    let oracle = oracle();
    assert_eq!(oracle.schema_version, 3);
    assert_eq!(oracle.model, SCORE_MODEL);
    for (category, expected) in oracle.category_mappings {
        let (display, dimension) = category_mapping(&category).expect("mapped category");
        assert_eq!(display.to_string(), expected.display, "{category}");
        assert_eq!(format!("{dimension:?}"), expected.dimension, "{category}");
        assert_eq!(category_bucket(Some(&category)), display, "{category}");
    }
    for unmapped in [None, Some("future"), Some(""), Some("style")] {
        assert_eq!(
            category_bucket(unmapped).to_string(),
            oracle.uncategorized_bucket
        );
    }

    let tiers: BTreeMap<_, _> = CATALOG
        .iter()
        .map(|definition| {
            (
                definition.id.to_owned(),
                definition.tier.as_str().to_owned(),
            )
        })
        .collect();
    assert_eq!(tiers, oracle.rule_tiers);

    let mut previous: Option<(u8, u8)> = None;
    for expected in oracle.tier_ceilings {
        let tier = RuleTier::parse(&expected.tier).expect("published tier should be known");
        assert_eq!(tier_dimension_ceiling(tier), expected.dimension, "{tier:?}");
        assert_eq!(tier_overall_ceiling(tier), expected.overall, "{tier:?}");
        let current = (
            expected.dimension.unwrap_or(100),
            expected.overall.unwrap_or(100),
        );
        if let Some(previous) = previous {
            assert!(
                previous.0 < current.0 && previous.1 <= current.1,
                "caps must decrease strictly with gravity: {previous:?} then {current:?}",
            );
        }
        previous = Some(current);
    }

    for expected in oracle.denominators {
        let producer = PRODUCERS
            .iter()
            .find(|(name, _)| *name == expected.producer)
            .map(|(_, producer)| *producer)
            .unwrap_or_else(|| {
                unreachable!(
                    "the oracle names a producer the catalog does not: {}",
                    expected.producer
                )
            });
        assert_eq!(
            DensityScope::of(producer) == DensityScope::PerKiloline,
            expected.per_kiloline,
            "{}",
            expected.producer
        );
    }
    for expected in oracle.score_boundaries {
        assert_eq!(
            weighted_score(expected.dimensions),
            expected.expected_value,
            "{}",
            expected.name
        );
        assert_eq!(
            score_label(expected.expected_value).to_string(),
            expected.expected_label,
            "{}",
            expected.name
        );
        assert!(
            expected.expected_counted_rule_ids.is_empty(),
            "dimension-only boundary cases must not invent Rule IDs: {}",
            expected.name
        );
    }
    for expected in oracle.density_cases {
        let dimension = DIMENSIONS
            .iter()
            .find(|(name, _)| *name == expected.dimension)
            .map(|(_, dimension)| *dimension)
            .unwrap_or_else(|| {
                unreachable!(
                    "the oracle names a dimension the score does not: {}",
                    expected.dimension
                )
            });
        assert_eq!(
            density::dimension_score(expected.density, dimension),
            expected.score,
            "{} at density {}",
            expected.dimension,
            expected.density
        );
        let dimensions = ScoreDimensions {
            security: expected.score,
            reliability: expected.score,
            maintainability: expected.score,
            performance: expected.score,
            dependencies: expected.score,
        };
        assert_eq!(weighted_score(dimensions), expected.score);
    }
    for case in oracle.score_cases {
        let diagnostics: Vec<_> = case
            .diagnostics
            .iter()
            .enumerate()
            .map(|(index, input)| diagnostic(input, index))
            .collect();
        let status = if case.complete {
            Status::Complete
        } else {
            Status::Incomplete
        };
        let audit = Audit::build(
            case.source_files,
            case.production_lines,
            status,
            &diagnostics,
        );
        assert_eq!(
            serde_json::to_value(&audit).unwrap_or(Value::Null),
            case.expected_audit,
            "{}",
            case.name
        );
        let input = aggregate_rules(case.production_lines, &diagnostics);
        let counted: Vec<_> = input
            .rules
            .into_iter()
            .filter(RuleAggregate::is_scorable)
            .map(|rule| rule.id)
            .collect();
        assert_eq!(counted, case.expected_counted_rule_ids, "{}", case.name);
        assert!(audit.is_valid(), "{}", case.name);
    }
}

#[test]
fn versioned_share_oracle_matches_public_query_bounds() {
    for case in oracle().share_cases {
        let actual = build_share_url(
            case.score,
            case.errors,
            case.warnings,
            case.info,
            case.files,
            case.lines,
        )
        .ok();
        assert_eq!(actual, case.expected);
    }
    assert_eq!(score_label(52), ScoreLabel::NeedsWork);
}

#[test]
fn invalid_score_state_is_rejected_before_sharing() {
    let audit = Audit {
        source_files: 1,
        production_lines: 100,
        categories: Vec::new(),
        inventory_is_complete: true,
        score: Some(AuditScore {
            model: SCORE_MODEL.to_owned(),
            value: 101,
            label: ScoreLabel::Great,
            authoritative: true,
            dimensions: ScoreDimensions {
                security: 100,
                reliability: 100,
                maintainability: 100,
                performance: 100,
                dependencies: 100,
            },
            worst_tier: None,
            applied_ceiling: None,
            projected_after_top_three: None,
            projected_rule_ids: Vec::new(),
            withheld_rule_ids: Vec::new(),
        }),
    };

    assert!(!audit.is_valid());
    assert_eq!(audit.share_url(), Err(ShareError::InvalidPayload));
    assert!(serde_json::to_vec(&audit).is_err());
    assert_eq!(
        build_share_url(100, 1_000_001, 0, 0, 1, 100),
        Err(ShareError::InvalidPayload)
    );
    // The line count is bounded like every other published count.
    assert_eq!(
        build_share_url(100, 0, 0, 0, 1, 1_000_001),
        Err(ShareError::InvalidPayload)
    );
}

/// The denominator the score is computed against travels with the score.
#[test]
fn the_share_url_carries_the_production_line_count() {
    let audit = Audit::build(3, 420, Status::Complete, &[]);

    assert_eq!(
        audit.share_url(),
        Ok("https://rust-doctor.com/share?s=100&m=core-v3&f=3&l=420".to_owned())
    );
}

fn catalog_diagnostics(occurrences: usize) -> Vec<Diagnostic> {
    CATALOG
        .iter()
        .enumerate()
        .map(|(index, definition)| Diagnostic {
            context: None,
            id: format!("finding-{index}"),
            source: DiagnosticSource::Clippy,
            code: Some(definition.id.to_owned()),
            base_severity: Severity::Error,
            severity: Severity::Error,
            category: Some(definition.category.to_owned()),
            message: "worst case".to_owned(),
            help: None,
            package: None,
            target: None,
            path: None,
            span: None,
            related: Vec::new(),
            similarity_basis_points: None,
            complexity: None,
            occurrences,
        })
        .collect()
}

/// What the real catalog produces, as opposed to the injected dimensions
/// of the oracle.
///
/// Under `core-v1`, saturating the twelve rules left the score at 96,
/// label `Great`: the additive scale was structurally unable to go down.
/// Under `core-v2` the worst observed tier caps the score, so the same
/// catalog reaches the `Critical` band. Under `core-v3` the workspace has to be named for the
/// question to mean anything: sixty-two rules firing once is a catastrophe in a hundred lines
/// and an ordinary Tuesday in a hundred thousand, so this fires them across fifty kilolines,
/// which is a workspace the corpus would recognize.
#[test]
fn the_catalog_drives_the_score_out_of_its_top_label() {
    let diagnostics = catalog_diagnostics(1);
    let audit = Audit::build(500, 50_000, Status::Complete, &diagnostics);
    let score = audit.score.expect("a scored audit should exist");

    assert_eq!(score.value, 26);
    assert_eq!(score.label, ScoreLabel::Critical);
    assert_eq!(score.worst_tier, Some(RuleTier::P0));
    assert_eq!(score.applied_ceiling, Some(40));
    assert!(score.authoritative);

    // Two of the five are the density and three are not, which is the whole model in one score.
    // `security` and `dependencies` fall on their own: security reads a lambda of one, and the
    // eleven manifest rules are charged per workspace, so neither is diluted by fifty kilolines.
    // `performance` is its `P2` ceiling and `security` would be capped at twenty if the curve had
    // not already taken it below.
    assert_eq!(score.dimensions.security, 0);
    assert_eq!(score.dimensions.reliability, 34);
    assert_eq!(score.dimensions.maintainability, 42);
    // EP-024 opens `performance` and `dependencies`: no dimension stays
    // frozen at 100, so no weight of the scale is inert any more.
    assert_eq!(score.dimensions.performance, 75);
    assert_eq!(score.dimensions.dependencies, 4);
    assert!(
        score
            .dimensions
            .values()
            .into_iter()
            .all(|value| value < 100),
        "{:?}",
        score.dimensions
    );
}

/// One diagnostic per distinct site, which is what a core-v3 numerator counts.
///
/// The last member of a tuple is a number of sites and not a number of occurrences: the score
/// charges a rule for the places a reader has to go and look at, so a rule firing sixty times
/// is sixty diagnostics here, each in a file of its own.
fn diagnostics_for(rules: &[(&str, &str, Severity, usize)]) -> Vec<Diagnostic> {
    let mut diagnostics = Vec::new();
    for (rule, (code, category, severity, sites)) in rules.iter().enumerate() {
        for site in 0..*sites {
            let index = diagnostics.len();
            diagnostics.push(Diagnostic {
                context: None,
                id: format!("finding-{rule}-{site}"),
                source: DiagnosticSource::Clippy,
                code: Some((*code).to_owned()),
                base_severity: *severity,
                severity: *severity,
                category: Some((*category).to_owned()),
                message: format!("finding {index}"),
                help: None,
                package: None,
                target: None,
                path: Some(format!("src/{index}.rs")),
                span: None,
                related: Vec::new(),
                similarity_basis_points: None,
                complexity: None,
                occurrences: 1,
            });
        }
    }
    diagnostics
}

/// An incomplete scan still scores and still caps, and it names neither what to fix next nor
/// what the ranking withheld, because it ranked nothing.
///
/// The two inputs are the two ways of asking: a rule the corpus adjudicated wrong is the one
/// that would be withheld, and a `P0` rule is the one that would cap.
#[test]
fn an_incomplete_scan_caps_and_names_neither_a_projection_nor_a_withholding() {
    let noisy = scored_at(
        Status::Incomplete,
        &[(
            "clippy::indexing_slicing",
            "reliability",
            Severity::Warning,
            60,
        )],
    );
    let capped = scored_at(
        Status::Incomplete,
        &[(
            "rust_doctor::source::dynamic_shell_command",
            "security",
            Severity::Warning,
            1,
        )],
    );
    assert_eq!(capped.value, 40);
    assert_eq!(capped.applied_ceiling, Some(40));

    for score in [noisy, capped] {
        assert!(!score.authoritative);
        assert_eq!(score.projected_after_top_three, None);
        assert!(score.projected_rule_ids.is_empty());
        assert!(score.withheld_rule_ids.is_empty());
    }
}

/// The promise the report makes is the score with those three rules gone, not an estimate of it.
///
/// The projection is rescored through the same path the published value takes, ceilings
/// included, which is what this replays: score once, drop every diagnostic the three named rules
/// raised, score again, and the two numbers have to be the same. It is the one claim a reader
/// acts on, and the contribution the ranking orders by is deliberately computed without the
/// ceilings, so the two are separate facts and only this one is published.
#[test]
fn the_projection_is_the_score_the_named_repairs_actually_reach() {
    let profile = [
        ("clippy::dbg_macro", "maintainability", Severity::Warning, 9),
        ("clippy::todo", "correctness", Severity::Warning, 6),
        (
            "rust_doctor::cargo::duplicate_major_versions",
            "dependencies",
            Severity::Warning,
            3,
        ),
        ("clippy::string_slice", "reliability", Severity::Warning, 2),
    ];
    let diagnostics = diagnostics_for(&profile);
    let score = Audit::build(20, 20_000, Status::Complete, &diagnostics)
        .score
        .expect("twenty kilolines is a scorable workspace");

    assert!(score.authoritative);
    assert_eq!(score.projected_rule_ids.len(), 3);
    let named: BTreeSet<&str> = score
        .projected_rule_ids
        .iter()
        .map(String::as_str)
        .collect();

    let repaired: Vec<_> = diagnostics
        .into_iter()
        .filter(|diagnostic| {
            diagnostic
                .code
                .as_deref()
                .is_none_or(|code| !named.contains(code))
        })
        .collect();
    let after = Audit::build(20, 20_000, Status::Complete, &repaired)
        .score
        .expect("repairing findings never makes a workspace unscorable");

    assert_eq!(
        score.projected_after_top_three,
        Some(after.value),
        "the projection promised {:?} and the repair reaches {}",
        score.projected_after_top_three,
        after.value
    );
    assert!(after.value > score.value, "the three repairs are worth points");
}

fn scored(rules: &[(&str, &str, Severity, usize)]) -> AuditScore {
    scored_at(Status::Complete, rules)
}

fn scored_at(status: Status, rules: &[(&str, &str, Severity, usize)]) -> AuditScore {
    Audit::build(1, 100, status, &diagnostics_for(rules))
        .score
        .expect("a scored audit should exist")
}

/// A clean codebase takes no cap, and a cap is not invented out of a rule
/// outside the catalog.
#[test]
fn a_clean_codebase_scores_one_hundred_without_any_ceiling() {
    let clean = Audit::build(1, 100, Status::Complete, &[])
        .score
        .expect("a scored audit should exist");
    assert_eq!(clean.value, 100);
    assert_eq!(clean.worst_tier, None);
    assert_eq!(clean.applied_ceiling, None);

    let uncatalogued = scored(&[("clippy::unknown_rule", "security", Severity::Error, 1)]);
    assert_eq!(uncatalogued.worst_tier, None);
    assert_eq!(uncatalogued.applied_ceiling, None);
}

/// A tier only caps when it acts: a rule switched off by the policy carries
/// an unknown severity, hence neither penalty nor cap.
#[test]
fn a_disabled_rule_neither_penalizes_nor_caps() {
    let disabled = scored(&[(
        "rust_doctor::source::dynamic_shell_command",
        "security",
        Severity::Unknown,
        1,
    )]);
    assert_eq!(disabled.value, 100);
    assert_eq!(disabled.worst_tier, None);
    assert_eq!(disabled.applied_ceiling, None);
    assert_eq!(disabled.dimensions.security, 100);
}

/// The worst tier of a dimension overrides the others, and a graver tier in
/// another dimension still brings the overall score down.
#[test]
fn only_the_worst_tier_applies_per_dimension_and_overall() {
    let mixed = scored(&[
        ("clippy::todo", "correctness", Severity::Warning, 1),
        ("clippy::unimplemented", "correctness", Severity::Warning, 1),
        ("clippy::dbg_macro", "maintainability", Severity::Warning, 1),
        (
            "rust_doctor::source::disabled_tls_verification",
            "security",
            Severity::Warning,
            1,
        ),
    ]);

    assert_eq!(mixed.dimensions.reliability, 50, "P1 overrides P2");
    assert_eq!(mixed.dimensions.security, 20, "P0 caps its dimension");
    assert!(mixed.dimensions.maintainability > 75, "P3 does not cap");
    assert_eq!(mixed.worst_tier, Some(RuleTier::P0));
    assert_eq!(mixed.applied_ceiling, Some(40));
    assert_eq!(mixed.value, 40);
}

/// A diagnostic with no catalog category stays counted: both quantities of
/// the categories reconstitute the report population exactly.
#[test]
fn every_diagnostic_lands_in_exactly_one_bucket() {
    let mut diagnostics = diagnostics_for(&[
        ("clippy::todo", "correctness", Severity::Warning, 3),
        ("clippy::dbg_macro", "maintainability", Severity::Info, 1),
    ]);
    diagnostics.push(Diagnostic {
        context: None,
        id: "compiler".to_owned(),
        source: DiagnosticSource::Rustc,
        code: Some("E0433".to_owned()),
        base_severity: Severity::Error,
        severity: Severity::Error,
        category: None,
        message: "unresolved import".to_owned(),
        help: None,
        package: None,
        target: None,
        path: None,
        span: None,
        related: Vec::new(),
        similarity_basis_points: None,
        complexity: None,
        occurrences: 2,
    });

    let audit = Audit::build(1, 100, Status::Incomplete, &diagnostics);
    let (distinct, occurrences) = audit.totals();

    assert_eq!(distinct.total, 5);
    assert_eq!(occurrences.total, 6);
    assert_eq!(distinct.errors, 1);
    assert_eq!(occurrences.errors, 2);
    assert_eq!(
        audit
            .categories
            .iter()
            .map(|category| category.name)
            .collect::<Vec<_>>(),
        [
            AuditCategoryName::Bugs,
            AuditCategoryName::Maintainability,
            AuditCategoryName::Other,
        ]
    );
    assert!(audit.is_valid());
}

#[test]
fn incomplete_source_inventory_never_emits_an_authoritative_score() {
    let audit = Audit::build_from_inventory(
        SourceFileInventory {
            files: 1,
            production_lines: 40,
            complete: false,
        },
        Status::Complete,
        &[],
    );

    assert_eq!(audit.score.as_ref().map(|score| score.value), Some(100));
    assert_eq!(
        audit.score.as_ref().map(|score| score.authoritative),
        Some(false)
    );
}



/// One band table for the score and for both reports. `score_label` is what
/// the audit publishes and `score_block::label_for` is what the two reports
/// label a value the audit did not label itself, the projected gain among
/// them: a second set of thresholds is how a projection could read `Great`
/// under a score the block called `Needs work`.
#[test]
fn the_bands_the_score_publishes_are_the_bands_the_reports_label_by() {
    for value in 0..=100u8 {
        assert_eq!(
            score_label(value),
            crate::score_block::label_for(value),
            "the band of {value}"
        );
    }
    assert_eq!(score_label(75), ScoreLabel::Great);
    assert_eq!(score_label(74), ScoreLabel::NeedsWork);
    assert_eq!(score_label(50), ScoreLabel::NeedsWork);
    assert_eq!(score_label(49), ScoreLabel::Critical);
}