pmat 3.30.1

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP)
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
#![cfg_attr(coverage_nightly, coverage(off))]
/// Calculator unit tests for perfection_score module
#[cfg(test)]
mod calculator_tests {
    use super::super::calculator::{normalize_rps_percentage, PerfectionScoreCalculator};
    use super::super::types::{CategoryScore, PerfectionScoreResult};
    use std::fs;
    use std::path::Path;
    use tempfile::TempDir;

    // ============================================================================
    // PerfectionScoreCalculator Tests
    // ============================================================================

    #[test]
    fn test_calculator_new() {
        let calc = PerfectionScoreCalculator::new();
        assert!(!calc.fast_mode);
        assert_eq!(calc.weights.tdg, 40);
    }

    #[test]
    fn test_calculator_default() {
        let calc = PerfectionScoreCalculator::default();
        assert!(!calc.fast_mode);
    }

    #[test]
    fn test_calculator_fast_mode_setter() {
        let calc = PerfectionScoreCalculator::new().fast_mode(true);
        assert!(calc.fast_mode);

        let calc = PerfectionScoreCalculator::new().fast_mode(false);
        assert!(!calc.fast_mode);
    }

    /// A title line is not a document: each of these is under a paragraph, so
    /// each earns the thin-file fraction of its weight (40 %), and the empty
    /// `docs/` directory earns nothing at all. This asserted 100.0 (A+).
    #[tokio::test]
    async fn test_get_documentation_score_all_docs() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        fs::write(root.join("README.md"), "# Test Project").unwrap();
        fs::write(root.join("CHANGELOG.md"), "# Changelog").unwrap();
        fs::create_dir(root.join("docs")).unwrap();
        fs::write(root.join("CONTRIBUTING.md"), "# Contributing").unwrap();
        let calc = PerfectionScoreCalculator::new();
        let (score, details) = calc.get_documentation_score(temp_dir.path()).await;
        assert_eq!(score, 16.0 + 8.0 + 0.0 + 6.0);
        assert!(details.contains("docs/ 0/25"), "{details}");
    }

    #[tokio::test]
    async fn test_get_documentation_score_readme_only() {
        let temp_dir = TempDir::new().unwrap();
        fs::write(temp_dir.path().join("README.md"), "# Test Project").unwrap();
        let calc = PerfectionScoreCalculator::new();
        let (score, _) = calc.get_documentation_score(temp_dir.path()).await;
        assert_eq!(score, 16.0);
    }

    #[tokio::test]
    async fn test_get_documentation_score_no_docs() {
        let temp_dir = TempDir::new().unwrap();
        let calc = PerfectionScoreCalculator::new();
        let (score, details) = calc.get_documentation_score(temp_dir.path()).await;
        assert_eq!(score, 0.0);
        assert!(details.contains("README 0/40 (missing)"), "{details}");
    }

    #[tokio::test]
    async fn test_get_documentation_score_lowercase_readme() {
        let temp_dir = TempDir::new().unwrap();
        fs::write(temp_dir.path().join("readme.md"), "# Test").unwrap();
        let calc = PerfectionScoreCalculator::new();
        let (score, _) = calc.get_documentation_score(temp_dir.path()).await;
        assert_eq!(score, 16.0);
    }

    // ========================================================================
    // #938: three categories were a 50.0 constant plus file-existence bonuses
    //
    // Every assertion below used to encode the arithmetic of that constant
    // ("50 base + 30 for benches"). A benchmark that was never run, a mutant
    // that was never generated and a line of code that was never executed are
    // all *not measured*, and now say so.
    // ========================================================================

    #[tokio::test]
    async fn test_performance_is_not_measured_from_a_benches_directory() {
        let temp_dir = TempDir::new().unwrap();
        fs::create_dir(temp_dir.path().join("benches")).unwrap();
        fs::write(
            temp_dir.path().join("Cargo.toml"),
            "[dev-dependencies]\ncriterion = \"0.5\"\n",
        )
        .unwrap();

        let calc = PerfectionScoreCalculator::new();
        let err = calc
            .get_performance_score(temp_dir.path())
            .await
            .expect_err("an empty benches/ dir is not a benchmark run");
        assert!(err.contains("cargo bench"), "unhelpful reason: {err}");
    }

    #[tokio::test]
    async fn test_performance_scores_criterion_comparisons() {
        let temp_dir = TempDir::new().unwrap();
        let criterion = temp_dir.path().join("target/criterion");
        for (name, mean) in [("fast", -0.02), ("slow", 0.40), ("steady", 0.001)] {
            let dir = criterion.join(name).join("change");
            fs::create_dir_all(&dir).unwrap();
            fs::write(
                dir.join("estimates.json"),
                format!("{{\"mean\":{{\"point_estimate\":{mean}}}}}"),
            )
            .unwrap();
        }

        let calc = PerfectionScoreCalculator::new();
        let score = calc.get_performance_score(temp_dir.path()).await.unwrap();
        // 1 of 3 benchmarks regressed past the 5% noise threshold.
        assert!(
            (score - (2.0 / 3.0 * 100.0)).abs() < 0.001,
            "score was {score}"
        );
    }

    #[tokio::test]
    async fn test_mutation_is_not_measured_from_config_files() {
        let temp_dir = TempDir::new().unwrap();
        fs::write(temp_dir.path().join("mutants.toml"), "[mutants]").unwrap();
        fs::create_dir(temp_dir.path().join(".mutants")).unwrap();
        fs::write(
            temp_dir.path().join("Cargo.toml"),
            "[dev-dependencies]\ncargo-mutants = \"1.0\"\n",
        )
        .unwrap();

        let calc = PerfectionScoreCalculator::new();
        let err = calc
            .get_mutation_score(temp_dir.path())
            .await
            .expect_err("configuration is not a mutation run");
        assert!(err.contains("cargo mutants"), "unhelpful reason: {err}");
    }

    #[tokio::test]
    async fn test_mutation_scores_cargo_mutants_outcomes() {
        let temp_dir = TempDir::new().unwrap();
        let out = temp_dir.path().join("mutants.out");
        fs::create_dir_all(&out).unwrap();
        fs::write(
            out.join("outcomes.json"),
            r#"{"outcomes":[
                {"scenario":"Baseline","summary":"Success"},
                {"scenario":{"Mutant":{}},"summary":"CaughtMutant"},
                {"scenario":{"Mutant":{}},"summary":"CaughtMutant"},
                {"scenario":{"Mutant":{}},"summary":"CaughtMutant"},
                {"scenario":{"Mutant":{}},"summary":"MissedMutant"},
                {"scenario":{"Mutant":{}},"summary":"Unviable"}
            ]}"#,
        )
        .unwrap();

        let calc = PerfectionScoreCalculator::new();
        let score = calc.get_mutation_score(temp_dir.path()).await.unwrap();
        // 3 caught of 4 viable; the unviable mutant and the baseline are excluded.
        assert_eq!(score, 75.0);
    }

    #[tokio::test]
    async fn test_get_coverage_score_from_cache() {
        let temp_dir = TempDir::new().unwrap();
        let metrics_dir = temp_dir.path().join(".pmat-metrics");
        fs::create_dir_all(&metrics_dir).unwrap();
        fs::write(metrics_dir.join("coverage.json"), r#"{"coverage": 85.5}"#).unwrap();
        let calc = PerfectionScoreCalculator::new();
        let score = calc.get_coverage_score(temp_dir.path()).await.unwrap();
        assert_eq!(score, 85.5);
    }

    /// Counting `#[test]` attributes measures how many tests were written, not
    /// how much code they execute. It used to produce
    /// `50 + test_count * 0.1 + density * 5` and call it coverage.
    #[tokio::test]
    async fn test_coverage_is_not_estimated_from_test_attribute_counts() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        fs::create_dir(root.join("src")).unwrap();
        for i in 0..5_usize {
            fs::write(
                root.join("src").join(format!("mod_{i}.rs")),
                format!("// Source file {i}\n\n#[test]\nfn test_{i}_0 () {{}}\n"),
            )
            .unwrap();
        }
        fs::write(root.join("Cargo.toml"), "[package]\nname = \"t\"\n").unwrap();

        let calc = PerfectionScoreCalculator::new();
        let err = calc
            .get_coverage_score(root)
            .await
            .expect_err("test attributes are not coverage");
        assert!(err.contains("llvm-cov"), "unhelpful reason: {err}");
    }

    #[tokio::test]
    async fn test_get_coverage_score_empty_project() {
        let temp_dir = TempDir::new().unwrap();
        let calc = PerfectionScoreCalculator::new();
        assert!(
            calc.get_coverage_score(temp_dir.path()).await.is_err(),
            "an empty project has no coverage, not 70%"
        );
    }

    /// #938's reproduction, as a test: four empty files must not move the score.
    #[tokio::test]
    async fn test_empty_files_do_not_move_the_total() {
        fn bare_crate() -> TempDir {
            let dir = TempDir::new().unwrap();
            fs::create_dir(dir.path().join("src")).unwrap();
            fs::write(dir.path().join("Cargo.toml"), "[package]\nname = \"t\"\n").unwrap();
            fs::write(dir.path().join("src/lib.rs"), "//! x\n").unwrap();
            dir
        }

        let bare = bare_crate();
        let dressed = bare_crate();
        fs::write(dressed.path().join("mutants.toml"), "").unwrap();
        fs::create_dir(dressed.path().join(".mutants")).unwrap();
        fs::create_dir(dressed.path().join("benches")).unwrap();
        fs::write(
            dressed.path().join("Cargo.toml"),
            "[package]\nname = \"t\"\n\n[dev-dependencies]\ncriterion = \"0.5\"\n",
        )
        .unwrap();

        let calc = PerfectionScoreCalculator::new().fast_mode(true);
        let bare_result = calc.calculate(bare.path()).await.unwrap();
        let dressed_result = calc.calculate(dressed.path()).await.unwrap();

        let category = |r: &super::super::types::PerfectionScoreResult, name: &str| {
            r.categories
                .iter()
                .find(|c| c.name == name)
                .unwrap_or_else(|| panic!("missing category {name}"))
                .earned_points
        };
        for name in ["Mutation Testing", "Performance", "Test Coverage"] {
            assert_eq!(
                category(&bare_result, name),
                category(&dressed_result, name),
                "{name} moved on four empty files"
            );
            assert_eq!(
                category(&bare_result, name),
                0.0,
                "{name} was scored without evidence"
            );
        }
    }

    // ============================================================================
    // Calculator Fast Mode Integration Test
    // ============================================================================

    /// Fast mode cannot run mutation testing, so it must not award points for it.
    /// This test previously asserted the opposite — that the category came back
    /// with a flat raw_score of 50.0 ("default credit") — which is exactly how ten
    /// unearned points ended up inside a total presented as a grade, identically
    /// for a real repo and for a path that does not exist.
    #[tokio::test]
    async fn test_calculator_fast_mode_mutation_earns_nothing() {
        let temp_dir = TempDir::new().unwrap();
        let calc = PerfectionScoreCalculator::new().fast_mode(true);

        let result = calc.calculate(temp_dir.path()).await.unwrap();

        let mutation_cat = result
            .categories
            .iter()
            .find(|c| c.name == "Mutation Testing")
            .unwrap();
        assert_eq!(
            mutation_cat.earned_points, 0.0,
            "an unmeasured category must not contribute points"
        );
        assert_eq!(
            mutation_cat.max_points, 0,
            "an unmeasured category must not sit in the denominator"
        );
        assert!(mutation_cat
            .details
            .as_ref()
            .is_some_and(|d| d.contains("Not measured")));

        // The reported denominator must equal what was actually measured.
        // (This asserted a fixed 180 when Mutation Testing was the only
        // category that could be N/A; Test Coverage and Performance now drop
        // out too when no coverage or benchmark run left evidence — #938.)
        let summed: u16 = result.categories.iter().map(|c| c.max_points).sum();
        assert_eq!(summed, result.max_score);
        assert!(
            result.max_score <= 180,
            "mutation must have left the denominator, got {}",
            result.max_score
        );
    }

    // ========================================================================
    // #941: Technical Debt Grade contradicted `pmat tdg` on the same path
    // ========================================================================

    /// A file `pmat tdg` grades F must not be graded C+ by the same binary's
    /// perfection-score. The category used to run a separate `TDGCalculator` on
    /// a 0-5 debt scale converted as `100 - average_tdg * 20`, which reported
    /// 77.2 (C+, 30.9 of 40 points) for the fixture below while `pmat tdg` and
    /// `pmat analyze tdg` both reported 0.0/100 (F).
    #[tokio::test]
    async fn test_tdg_category_agrees_with_the_tdg_command() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        fs::create_dir(root.join("src")).unwrap();
        fs::write(root.join("Cargo.toml"), "[package]\nname = \"awful\"\n").unwrap();

        let mut src = String::from(
            "// TODO: this whole file is a mess\n// FIXME: rewrite\n// HACK: do not ship\n",
        );
        src.push_str(
            "pub fn monster(a: i32, b: i32, c: i32, d: i32) -> i32 {\n    let mut r = 0;\n",
        );
        for i in 0..60 {
            src.push_str(&format!(
                "    if a > {i} && b < {i} || c == {i} {{ r += {i}; }} else if d != {i} {{ r -= {i}; }}\n"
            ));
        }
        src.push_str("    r\n}\n");
        for i in 0..40 {
            src.push_str(&format!(
                "pub fn bad{i}(s: &str) -> i32 {{ // TODO fix bad{i}\n    let v: i32 = s.parse().unwrap();\n    if v < 0 {{ panic!(\"negative\"); }}\n    v\n}}\n"
            ));
        }
        fs::write(root.join("src/lib.rs"), src).unwrap();

        // What `pmat tdg` / `pmat analyze tdg` report for this tree.
        let expected = crate::tdg::TdgAnalyzer::new()
            .unwrap()
            .analyze_project(root)
            .await
            .unwrap()
            .average_score
            .expect("the fixture has a gradable file");

        let calc = PerfectionScoreCalculator::new();
        let measured = calc.get_tdg_score(root).await.expect("a gradable tree");

        assert!(
            (measured - f64::from(expected)).abs() < 0.001,
            "perfection-score says {measured}, `pmat tdg` says {expected}"
        );
    }

    /// A tree with nothing to grade is not a tree that scored 100 — or 0.
    #[tokio::test]
    async fn test_tdg_category_is_not_measured_without_source() {
        let temp_dir = TempDir::new().unwrap();
        let calc = PerfectionScoreCalculator::new();
        let err = calc
            .get_tdg_score(temp_dir.path())
            .await
            .expect_err("nothing to grade");
        assert!(err.contains("gradable"), "unhelpful reason: {err}");
    }

    #[test]
    fn test_category_score_in_calculator_context() {
        // Test that CategoryScore created via calculator uses correct weights
        let score = CategoryScore::new("Technical Debt Grade", 75.0, 40);
        assert_eq!(score.earned_points, 30.0);
        assert_eq!(score.grade, "C");
    }

    // ============================================================================
    // RPS Normalization Tests (raw points → percentage, not raw → category)
    // ============================================================================

    #[test]
    fn test_rps_raw_points_normalize_to_category_fraction() {
        // RPS raw 246.6/289 = 85.3% → 0.853 * 30 ≈ 25.6/30, NOT 246.6 treated
        // as a percentage (which earned 55.2/30 and clamped total at 200 A+)
        let pct = normalize_rps_percentage(246.6, 289.0);
        assert!((pct - 85.328).abs() < 0.01, "pct was {}", pct);

        let score = CategoryScore::new("Rust Project Quality", pct, 30);
        assert!(
            (score.earned_points - 25.6).abs() < 0.01,
            "earned was {}",
            score.earned_points
        );
    }

    #[test]
    fn test_rps_perfect_input_earns_exactly_max() {
        let pct = normalize_rps_percentage(289.0, 289.0);
        assert_eq!(pct, 100.0);

        let score = CategoryScore::new("Rust Project Quality", pct, 30);
        assert_eq!(score.earned_points, 30.0);
    }

    #[test]
    fn test_rps_normalize_degenerate_inputs() {
        // Zero/negative max must not divide by zero or go negative
        assert_eq!(normalize_rps_percentage(100.0, 0.0), 0.0);
        assert_eq!(normalize_rps_percentage(-5.0, 289.0), 0.0);
        // Earned above max (should not happen) clamps to 100%
        assert_eq!(normalize_rps_percentage(300.0, 289.0), 100.0);
    }

    #[test]
    fn test_category_never_exceeds_max_points() {
        // Regression: raw 184.03 fed as a percentage must clamp at the
        // category max instead of earning 55.2/30
        let score = CategoryScore::new("Rust Project Quality", 184.03, 30);
        assert_eq!(score.earned_points, 30.0);
        let perfect = CategoryScore::new("Rust Project Quality", 100.0, 30);
        assert_eq!(
            score.grade, perfect.grade,
            "over-max raw must grade as the clamped 100%, not via overflow"
        );

        let negative = CategoryScore::new("Rust Project Quality", -10.0, 30);
        assert_eq!(negative.earned_points, 0.0);
        let zero = CategoryScore::new("Rust Project Quality", 0.0, 30);
        assert_eq!(
            negative.grade, zero.grade,
            "negative raw must grade as the clamped 0%"
        );
    }

    // ── The 120 s backstop reported eight measured zeros (#938 family) ──

    /// The whole-run backstop used to return a scorecard: every category `0.0`
    /// out of its full weight, details "Timed out", total 0/200, grade F —
    /// absence rendered as the worst possible measurement. Nothing is
    /// measurable once the inner future is dropped, so it must refuse.
    #[tokio::test(start_paused = true)]
    async fn a_run_that_exceeds_its_budget_refuses_instead_of_grading_zero() {
        let budget = std::time::Duration::from_secs(120);
        let never = async {
            tokio::time::sleep(budget * 10).await;
            unreachable!("the backstop must fire first")
        };

        let err = super::super::calculator::guard_total(Path::new("/some/project"), budget, never)
            .await
            .expect_err("a run that measured nothing must not produce a score");

        let text = err.to_string();
        assert!(text.contains("measured nothing"), "{text}");
        assert!(text.contains("120s"), "{text}");
        assert!(!text.contains("Timed out"), "{text}");
    }

    /// The backstop is transparent when the run finishes inside it.
    #[tokio::test(start_paused = true)]
    async fn the_backstop_passes_a_finished_run_through_untouched() {
        let inner = async {
            Ok(PerfectionScoreResult::new(vec![CategoryScore::new(
                "Documentation",
                80.0,
                15,
            )]))
        };
        let result = super::super::calculator::guard_total(
            Path::new("/some/project"),
            std::time::Duration::from_secs(120),
            inner,
        )
        .await
        .expect("an in-budget run must be returned as-is");
        assert_eq!(result.categories.len(), 1);
        assert_eq!(result.categories[0].earned_points, 12.0);
    }

    /// A single slow category is excluded and disclosed — it does not take the
    /// run down, and it does not score zero either.
    #[tokio::test(start_paused = true)]
    async fn a_category_that_overruns_its_budget_is_not_measured() {
        let slow = async {
            tokio::time::sleep(std::time::Duration::from_secs(10_000)).await;
            Ok(100.0)
        };
        let why = super::super::calculator::within_budget(slow)
            .await
            .expect_err("an overrunning category must not report a number");
        assert!(why.contains("did not finish within"), "{why}");

        // …and the category it produces carries no weight, so it cannot drag
        // the total down.
        let mut categories = Vec::new();
        super::super::calculator::push_category(
            &mut categories,
            "Technical Debt Grade",
            40,
            Err(why),
        );
        assert_eq!(categories[0].max_points, 0);
        assert_eq!(categories[0].grade, "N/A");
        assert!(
            categories[0]
                .details
                .as_deref()
                .unwrap_or_default()
                .contains("Not measured"),
            "{:?}",
            categories[0].details
        );
    }

    // ── Documentation scored file existence: four empty files were 100/100 ──

    /// The exact fixture from the report: `touch README.md CHANGELOG.md
    /// CONTRIBUTING.md && mkdir docs` scored Documentation 100.0 (A+, 15/15
    /// points) because every component was `Path::exists()`. Empty files
    /// document nothing.
    #[tokio::test]
    async fn four_empty_files_buy_no_documentation_points() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        for name in ["README.md", "CHANGELOG.md", "CONTRIBUTING.md"] {
            fs::write(root.join(name), "").unwrap();
        }
        fs::create_dir(root.join("docs")).unwrap();

        let (score, details) = PerfectionScoreCalculator::new()
            .get_documentation_score(root)
            .await;

        assert_eq!(score, 0.0, "{details}");
        assert!(details.contains("README 0/40 (empty)"), "{details}");
        assert!(details.contains("docs/ 0/25 (0 non-empty"), "{details}");
    }

    /// …and real documentation still scores. The ordering is the property that
    /// matters: written documentation must outrank touched filenames.
    #[tokio::test]
    async fn written_documentation_outranks_empty_files() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        let readme = format!(
            "# Project\n\n{}\n\n## Usage\n\n```sh\npmat perfection-score\n```\n",
            "It does the thing, and here is a paragraph about how. ".repeat(6)
        );
        fs::write(root.join("README.md"), readme).unwrap();
        fs::write(
            root.join("CHANGELOG.md"),
            format!(
                "# Changelog\n\n## [1.2.3] - 2026-01-01\n\n{}\n",
                "- fixed a thing that was broken in a way worth writing down. ".repeat(5)
            ),
        )
        .unwrap();
        fs::create_dir(root.join("docs")).unwrap();
        for i in 0..5 {
            fs::write(
                root.join("docs").join(format!("g{i}.md")),
                "# Guide\nbody\n",
            )
            .unwrap();
        }
        fs::write(
            root.join("CONTRIBUTING.md"),
            format!(
                "# Contributing\n\n{}\n",
                "Run cargo test, then open a pull request. ".repeat(8)
            ),
        )
        .unwrap();

        let (score, details) = PerfectionScoreCalculator::new()
            .get_documentation_score(root)
            .await;

        assert_eq!(score, 100.0, "{details}");
    }

    /// A README without an example or sections is prose, not a manual: it earns
    /// most of its weight, never all of it, and never zero.
    #[tokio::test]
    async fn substantive_but_unstructured_readme_earns_partial_credit() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        fs::write(
            root.join("README.md"),
            "a wall of prose with no heading and no example. ".repeat(10),
        )
        .unwrap();

        let (score, details) = PerfectionScoreCalculator::new()
            .get_documentation_score(root)
            .await;

        assert_eq!(score, 28.0, "{details}");
        assert!(details.contains("no structure"), "{details}");
    }
}