fallow-output 3.48.0

Output contract types for fallow reports
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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
//! The mutation testing section of the CI surfaces.
//!
//! One pure renderer turns a [`MutationCiView`] into the markdown section of
//! the sticky comment, the review summary and the job summary, and into the
//! `mutation` row of the PR decision. No function here reads a file, the clock
//! or the environment, so the same view always gives the same bytes.
//!
//! A CI job writes the report after it ran project code, so each string in a
//! view is untrusted. Paths, function names, codes and the root go into code
//! spans with each line ending collapsed to a space. Messages go into a fenced
//! block that uses four backticks, and each run of three backticks in a message
//! becomes three single quotes, so no message can close the fence.

use crate::ci_output::markdown_code_span;
use crate::gate_outcomes::{GateOutcome, GateStatus};
use crate::mutation::{
    MutantChange, MutationCompletion, MutationCompletionStatus, MutationIncompleteReason,
    MutationScopeKind, MutationStatusCounts,
};
use crate::pr_decision::{PrDecisionAnnotationLevel, PrDecisionConclusion, PrDecisionGate};

/// The maximum number of characters of a rendered mutation section. A longer
/// section keeps only the heading, the headline and the count table.
pub const MUTATION_SECTION_MAX_CHARS: usize = 6000;

/// The maximum number of items in the list of lines that no test reaches.
pub const MUTATION_NO_COVERAGE_ITEMS: usize = 10;

/// The maximum number of characters of an error message or a help text.
pub const MUTATION_MESSAGE_MAX_CHARS: usize = 1000;

/// Why the CI job did not run mutation testing.
///
/// The CI wiring selects the reason, and this module owns the text of each
/// reason. A run that a signal stopped is [`MutationCiState::Interrupted`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MutationNotRunReason {
    /// The event can run the code of a fork with the secrets of the base
    /// repository.
    EventRefused,
    /// The merge request comes from a fork, and the pipeline runs in the
    /// target project.
    ForkRefused,
    /// The changed scope needs a diff or a base ref, and the job has neither.
    NoChangeScope,
}

/// The state of the mutation run that a CI job renders.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MutationCiState {
    /// The run wrote a report.
    Ran,
    /// `fallow mutation` exited with an error envelope.
    Failed {
        /// The exit code of `fallow mutation`.
        exit_code: i32,
        /// The error code of the envelope, for example `FALLOW_X`.
        code: String,
        /// The error message of the envelope.
        message: String,
        /// The help text of the envelope, when it has one.
        help: Option<String>,
    },
    /// The CI job did not run mutation testing.
    NotRun(MutationNotRunReason),
    /// A signal stopped the run before it wrote a report.
    Interrupted,
    /// The report has `kind: "mutation"`, but its content does not parse.
    Unreadable {
        /// Why the parse failed.
        reason: String,
    },
}

/// One source line that no test reaches.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct MutationCiLocation {
    /// The root-relative path, with forward slashes.
    pub file: String,
    /// The 1-based line.
    pub line: u32,
    /// The name of the enclosing function, when it has one.
    pub function: Option<String>,
}

/// The heading level of a rendered mutation section.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MutationHeading {
    /// `####`, under the `### Fallow <title>` heading of the sticky comment.
    /// The comment has the findings tables, so the text points at them.
    Comment,
    /// `####`, under the heading of a compact sticky comment, which has no
    /// findings tables, so the text points at no section.
    CompactComment,
    /// `####`, in the summary body of an inline review. The survived mutants
    /// are inline comments there, not table rows.
    Review,
    /// `##`, in the job summary. Only this heading prints the elapsed time
    /// and the cache hits, because a job summary belongs to one run.
    Summary,
}

/// The facts of a mutation run that the CI surfaces render.
#[derive(Debug, Clone, PartialEq)]
pub struct MutationCiView {
    /// The state of the run.
    pub state: MutationCiState,
    /// The scope of the run. `None` when the state has no report.
    pub scope: Option<MutationScopeKind>,
    /// The number of mutants for each status.
    pub counts: MutationStatusCounts,
    /// The mutation score, a fraction from 0 to 1. `None` when no mutant was
    /// counted.
    pub score: Option<f64>,
    /// The completion of the run, when the report has one.
    pub completion: Option<MutationCompletion>,
    /// The `mutation-min-score` gate entry of the report, when a minimum
    /// applies.
    pub min_score_gate: Option<GateOutcome>,
    /// The lines that no test reaches, one item for each line. The renderer
    /// sorts the list and removes duplicates.
    pub no_coverage: Vec<MutationCiLocation>,
    /// The survived mutants that are not shown because their file changed
    /// after the run.
    pub stale_hidden: usize,
    /// The survived mutants that are not shown because the diff filter
    /// removed them: they are on lines that the change did not add.
    pub off_diff_hidden: usize,
    /// The survived mutants that are not shown because of the inline cap.
    pub inline_overflow: usize,
    /// The `--root` of the run, as the inspect command must spell it.
    pub root_flag: Option<String>,
    /// The duration of the run in milliseconds.
    pub elapsed_ms: Option<u64>,
    /// The number of mutants whose status came from the result cache.
    pub cache_hits: Option<usize>,
}

impl MutationCiView {
    /// A view in `state` with no counts, no score and no lists.
    #[must_use]
    pub fn new(state: MutationCiState) -> Self {
        Self {
            state,
            scope: None,
            counts: MutationStatusCounts::default(),
            score: None,
            completion: None,
            min_score_gate: None,
            no_coverage: Vec::new(),
            stale_hidden: 0,
            off_diff_hidden: 0,
            inline_overflow: 0,
            root_flag: None,
            elapsed_ms: None,
            cache_hits: None,
        }
    }
}

/// A workflow annotation that states the run state: an error for a failed,
/// stopped or unreadable run, and a notice for a partial run.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MutationRunNotice {
    /// `failure` for an error, `notice` for a partial run.
    pub level: PrDecisionAnnotationLevel,
    /// The annotation title.
    pub title: &'static str,
    /// The annotation message, as plain text. The caller escapes it.
    pub message: String,
}

/// Whether a complete run had no mutant in its scope and no score gate, so a
/// PR comment or review has nothing to say about it. The job summary and the
/// decision row still state the run.
#[must_use]
pub fn mutation_scope_was_empty(view: &MutationCiView) -> bool {
    matches!(view.state, MutationCiState::Ran)
        && view.counts.total == 0
        && !is_partial(view)
        && view.min_score_gate.is_none()
}

/// Render the mutation section for `heading`.
///
/// A not-run, failed, stopped or unreadable state replaces everything after
/// the heading with the text of that state. A section longer than
/// [`MUTATION_SECTION_MAX_CHARS`] keeps the heading, the partial-run line, the
/// headline and the count table, followed by a note.
#[must_use]
pub fn render_mutation_section(view: &MutationCiView, heading: MutationHeading) -> String {
    let title = match heading {
        MutationHeading::Comment | MutationHeading::CompactComment | MutationHeading::Review => {
            "#### Mutation testing"
        }
        MutationHeading::Summary => "## Mutation testing",
    };
    let body = section_body(view, heading);
    let shown = if heading == MutationHeading::Summary {
        body.clone()
    } else {
        collapse_details(view, body.clone())
    };
    // The cap is measured on the open section, so the fold wrapper does not
    // push a section that fits over it.
    let open = join_paragraphs(title, &body.core, &body.extra);
    if open.chars().count() <= MUTATION_SECTION_MAX_CHARS {
        return join_paragraphs(title, &shown.core, &shown.extra);
    }
    // The shortened section keeps the count table in view: the details it
    // would fold are the part that the cap removed.
    join_paragraphs(title, &body.core, &[SHORTENED_SECTION_NOTE.to_owned()])
}

/// The `mutation` row of the PR decision, which follows the command row.
///
/// A complete run with no survivor and no uncovered mutant is `success`. A
/// partial run, a survivor or an uncovered mutant is `neutral`. A run that
/// the CI job did not start is `skipped`, and a failed, stopped or unreadable
/// run is `failure`. The row has no threshold: the `mutation-min-score` row
/// carries the gate.
#[must_use]
pub fn mutation_decision_row(view: &MutationCiView) -> PrDecisionGate {
    let (status, observed) = decision_outcome(view);
    PrDecisionGate {
        id: DECISION_ROW_ID.to_owned(),
        label: TITLE.to_owned(),
        status,
        observed,
        threshold: None,
        scope: decision_scope(view.scope).to_owned(),
    }
}

/// The plain-text description of a survived mutant, for the finding of the
/// rule `fallow/mutation-survived`.
///
/// For example ``Changing `>=` to `>` in `isAdult` made no test fail.
/// Mutant `9f3c2a1b`.`` The code values are code spans, as in the other
/// finding descriptions, so a Markdown surface shows an operator such as `<`
/// as code and not as escaped text. Each value becomes one line, and a long
/// value is cut.
#[must_use]
pub fn survived_description(
    operator: &str,
    change: Option<&MutantChange>,
    function: Option<&str>,
    mutant_id: &str,
) -> String {
    let token = |value: &str| single_line(value, TOKEN_MAX_CHARS);
    let code = |value: &str| markdown_code_span(&token(value));
    let action = match change.map(|change| (token(&change.from), token(&change.to))) {
        Some((from, to)) if !from.is_empty() && !to.is_empty() => {
            format!("Changing {} to {}", code(&from), code(&to))
        }
        Some((from, _)) if !from.is_empty() => format!("Removing {}", code(&from)),
        Some((_, to)) if !to.is_empty() => format!("Adding {}", code(&to)),
        _ => format!("A {} change", code(operator)),
    };
    let place = function
        .map(|name| single_line(name, INLINE_VALUE_MAX_CHARS))
        .filter(|name| !name.is_empty())
        .map_or_else(String::new, |name| {
            format!(" in {}", markdown_code_span(&name))
        });
    format!(
        "{action}{place} made no test fail. Mutant {}.",
        code(mutant_id)
    )
}

/// The workflow annotation that states the run state, when the state needs
/// one: an error for a failed, stopped or unreadable run, and a notice for a
/// partial run. The message is plain text, and the caller escapes it for the
/// workflow command.
#[must_use]
pub fn mutation_run_notice(view: &MutationCiView) -> Option<MutationRunNotice> {
    let (level, message) = match &view.state {
        MutationCiState::NotRun(_) => return None,
        MutationCiState::Ran => {
            let completion = view.completion.as_ref().filter(|_| is_partial(view))?;
            let reasons = completion
                .reasons
                .iter()
                .map(|reason| reason_text(*reason))
                .collect::<Vec<_>>()
                .join(", ");
            (
                PrDecisionAnnotationLevel::Notice,
                format!("Partial run: {reasons}. The annotations cover only the mutants that ran."),
            )
        }
        MutationCiState::Failed {
            exit_code,
            code,
            message,
            ..
        } => {
            let code = single_line(code, CODE_MAX_CHARS);
            let code = if code.is_empty() {
                String::new()
            } else {
                format!(", {code}")
            };
            let message = cap_chars(&normalize_block(message), MUTATION_MESSAGE_MAX_CHARS);
            (
                PrDecisionAnnotationLevel::Failure,
                format!("Mutation testing failed (exit code {exit_code}{code}): {message}"),
            )
        }
        MutationCiState::Interrupted => (
            PrDecisionAnnotationLevel::Failure,
            INTERRUPTED_TEXT.to_owned(),
        ),
        MutationCiState::Unreadable { .. } => (
            PrDecisionAnnotationLevel::Failure,
            format!("Fallow could not read the mutation report ({INVALID_REPORT_CODE})."),
        ),
    };
    Some(MutationRunNotice {
        level,
        title: TITLE,
        message,
    })
}

const TITLE: &str = "Mutation testing";
const DECISION_ROW_ID: &str = "mutation";
const REPORT_FILE: &str = "fallow-mutation.json";
const INVALID_REPORT_CODE: &str = "FALLOW_MUTATION_REPORT_INVALID";
const SHORTENED_SUFFIX: &str = " ... (shortened)";
const CUT_SUFFIX: &str = "...";
const SHORTENED_SECTION_NOTE: &str =
    "The mutation section is shortened. The fallow-mutation.json file of this job has all results.";
const INTERRUPTED_TEXT: &str =
    "Mutation testing stopped. A signal stopped the run, and the run wrote no report.";
const FENCE: &str = "````";
const THREE_BACKTICKS: &str = "```";
const THREE_QUOTES: &str = "'''";
/// A path or a function name longer than this is cut. The section cap still
/// applies to the sum.
const INLINE_VALUE_MAX_CHARS: usize = 512;
/// An operator, a change token or a mutant id longer than this is cut.
const TOKEN_MAX_CHARS: usize = 128;
const CODE_MAX_CHARS: usize = 80;
/// Added before the floor, so that a percent such as `28.999999999999996`
/// from `0.29 * 100` shows as `29.0` and not as `28.9`.
const FLOOR_EPSILON: f64 = 1e-9;
const PERCENT: f64 = 100.0;
const TENTHS: f64 = 10.0;
const MILLIS_PER_SECOND: u64 = 1000;
const SECONDS_PER_MINUTE: u64 = 60;
const SECONDS_PER_HOUR: u64 = 3600;

/// The paragraphs of a section. `core` survives the section cap, and `extra`
/// does not.
#[derive(Clone)]
struct SectionBody {
    core: Vec<String>,
    extra: Vec<String>,
}

/// The PR comment and the review show the partial-run line and the headline,
/// and fold the count table and the other paragraphs into one `<details>`
/// block, so the section is two lines until a reader opens it. The job
/// summary keeps every paragraph open.
fn collapse_details(view: &MutationCiView, mut body: SectionBody) -> SectionBody {
    if !matches!(view.state, MutationCiState::Ran) {
        return body;
    }
    let mut hidden = Vec::new();
    if view.counts.total > 0 && body.core.len() > 1 {
        hidden.extend(body.core.pop());
    }
    hidden.append(&mut body.extra);
    if hidden.is_empty() {
        return body;
    }
    body.extra = vec![format!(
        "<details>\n<summary>Counts and details</summary>\n\n{}\n\n</details>",
        hidden.join("\n\n")
    )];
    body
}

fn join_paragraphs(title: &str, core: &[String], extra: &[String]) -> String {
    std::iter::once(title)
        .chain(core.iter().map(String::as_str))
        .chain(extra.iter().map(String::as_str))
        .collect::<Vec<_>>()
        .join("\n\n")
}

fn section_body(view: &MutationCiView, heading: MutationHeading) -> SectionBody {
    let only = |text: String| SectionBody {
        core: vec![text],
        extra: Vec::new(),
    };
    match &view.state {
        MutationCiState::Ran => ran_body(view, heading),
        MutationCiState::NotRun(reason) => only(not_run_text(*reason).to_owned()),
        MutationCiState::Interrupted => only(
            "**Mutation testing stopped.** A signal stopped the run, and the run wrote no report."
                .to_owned(),
        ),
        MutationCiState::Unreadable { reason } => SectionBody {
            core: vec![format!(
                "**Fallow could not read the mutation report** ({}).",
                markdown_code_span(INVALID_REPORT_CODE)
            )],
            extra: text_block(reason).into_iter().collect(),
        },
        MutationCiState::Failed {
            exit_code,
            code,
            message,
            help,
        } => {
            let code = single_line(code, CODE_MAX_CHARS);
            let code = if code.is_empty() {
                String::new()
            } else {
                format!(", {}", markdown_code_span(&code))
            };
            SectionBody {
                core: vec![format!(
                    "**Mutation testing failed** (exit code {exit_code}{code})."
                )],
                extra: text_block(message)
                    .into_iter()
                    .chain(help.as_deref().and_then(text_block))
                    .collect(),
            }
        }
    }
}

const fn not_run_text(reason: MutationNotRunReason) -> &'static str {
    match reason {
        MutationNotRunReason::EventRefused => {
            "**Mutation testing did not run.** This event can run the code of a fork with the secrets of the base repository. Mutation testing runs only for the pull_request, push, schedule, workflow_dispatch and merge_group events."
        }
        MutationNotRunReason::ForkRefused => {
            "**Mutation testing did not run.** The merge request comes from a fork, and the pipeline runs in the target project."
        }
        MutationNotRunReason::NoChangeScope => {
            "**Mutation testing did not run.** The changed scope needs a pull request diff or a base ref, and this job has neither. Run the job in a pull request, set a base ref, or use the all scope."
        }
    }
}

fn ran_body(view: &MutationCiView, heading: MutationHeading) -> SectionBody {
    let mut core = Vec::new();
    if let Some(line) = partial_line(view) {
        core.push(line);
    }
    core.push(headline(view));
    if view.counts.total > 0 {
        core.push(count_table(view));
    }
    let facts = match heading {
        MutationHeading::Comment | MutationHeading::CompactComment | MutationHeading::Review => {
            None
        }
        MutationHeading::Summary => run_facts_line(view),
    };
    let extra = [
        scope_line(view.scope).map(str::to_owned),
        facts,
        survivor_explanation(view, heading),
        hidden_line(
            view.stale_hidden,
            "survived mutant is not shown because its file changed after the run.",
            "survived mutants are not shown because their file changed after the run.",
        ),
        hidden_line(
            view.off_diff_hidden,
            "survived mutant is on a line that this change did not add, so it has no finding.",
            "survived mutants are on lines that this change did not add, so they have no finding.",
        ),
        hidden_line(
            view.inline_overflow,
            "more survived mutant is not shown inline. The fallow-mutation.json file of this job lists it.",
            "more survived mutants are not shown inline. The fallow-mutation.json file of this job lists them.",
        ),
        no_coverage_details(&view.no_coverage),
        inspect_line(view),
    ]
    .into_iter()
    .flatten()
    .collect();
    SectionBody { core, extra }
}

fn is_partial(view: &MutationCiView) -> bool {
    view.completion
        .as_ref()
        .is_some_and(|completion| completion.status == MutationCompletionStatus::Partial)
}

fn partial_line(view: &MutationCiView) -> Option<String> {
    let completion = view.completion.as_ref().filter(|_| is_partial(view))?;
    let reasons = completion
        .reasons
        .iter()
        .map(|reason| reason_text(*reason))
        .collect::<Vec<_>>()
        .join(", ");
    let mut line = if reasons.is_empty() {
        "**Partial run.** The counts cover only the mutants that ran.".to_owned()
    } else {
        format!("**Partial run:** {reasons}. The counts cover only the mutants that ran.")
    };
    if completion
        .reasons
        .contains(&MutationIncompleteReason::TimeBudget)
    {
        line.push_str(" The next run with the same budget continues from the result cache.");
    }
    Some(line)
}

/// Why a run is partial, in words. The match is exhaustive, so a new reason
/// does not compile until it has a text here.
const fn reason_text(reason: MutationIncompleteReason) -> &'static str {
    match reason {
        MutationIncompleteReason::RuntimeError => "some mutants stopped with a runtime error",
        MutationIncompleteReason::MutantLimit => "the run reached the mutant limit",
        MutationIncompleteReason::SampledOut => "the run tested a sample of the mutants",
        MutationIncompleteReason::TimeBudget => "the run reached its time budget",
        MutationIncompleteReason::SkippedFiles => "the run skipped some files",
        MutationIncompleteReason::UnloadedFiles => "the test runner did not load some files",
        MutationIncompleteReason::TestFilesNotDiscovered => "the run found no test files",
        MutationIncompleteReason::BaselineFailure => "the tests failed before any change",
        MutationIncompleteReason::WorktreeChanged => "files changed during the run",
        MutationIncompleteReason::TypecheckFailed => "the type check failed",
        MutationIncompleteReason::WorktreeUnchecked => "the run could not check the worktree",
    }
}

fn headline(view: &MutationCiView) -> String {
    let gate = view.min_score_gate.as_ref();
    let minimum = gate.and_then(|gate| gate.threshold).map(minimum_percent);
    if let Some(gate) = gate.filter(|gate| gate.status == GateStatus::Fail) {
        let observed = gate
            .observed
            .or_else(|| view.score.map(|score| score * PERCENT));
        return match (observed, minimum) {
            (Some(observed), Some(minimum)) => format!(
                "**Score below the minimum.** The score is {}, and the minimum is {minimum}.",
                percent_floor(observed)
            ),
            (None, Some(minimum)) => {
                format!("**Score below the minimum.** The minimum is {minimum}.")
            }
            (_, None) => "**Score below the minimum.**".to_owned(),
        };
    }
    if view.counts.total == 0 {
        return "The scope has no mutant.".to_owned();
    }
    let detected = view.counts.killed.saturating_add(view.counts.timeout);
    if view.score.is_none() && (detected > 0 || view.counts.not_killed_by_target > 0) {
        // Only the target-first mode of `--kill-matrix-for` reports tested
        // mutants with no score.
        let missed = view.counts.not_killed_by_target;
        return format!(
            "**The target-first kill matrix has no score.** {} {} not killed by a target; pass `--kill-matrix-full` to score each mutant.",
            count_noun(missed, "mutant", "mutants"),
            if missed == 1 { "was" } else { "were" }
        );
    }
    let Some(score) = view.score else {
        return if gate.is_some() {
            "**No mutant was counted.** The score gate was skipped.".to_owned()
        } else {
            "**No mutant was counted.**".to_owned()
        };
    };
    if view.counts.survived > 0 {
        let minimum = minimum.map_or_else(String::new, |minimum| format!(" (minimum {minimum})"));
        return format!(
            "**{} survived.** Score {}{minimum}.",
            count_noun(view.counts.survived, "mutant", "mutants"),
            score_percent(score)
        );
    }
    "The tests detected each mutant that ran.".to_owned()
}

fn count_table(view: &MutationCiView) -> String {
    let counts = &view.counts;
    // The counts come from a file that test code can write, so the sums saturate.
    let not_measured = counts
        .pending
        .saturating_add(counts.runtime_error)
        .saturating_add(counts.flaky)
        .saturating_add(counts.compile_error)
        .saturating_add(counts.not_killed_by_target);
    let score = view.score.map_or_else(|| "-".to_owned(), score_percent);
    let killed = counts.killed.saturating_add(counts.timeout);
    // The column shows only when it has a count, so a normal run reads as
    // four columns.
    if not_measured == 0 {
        return format!(
            "| Killed | Survived | No coverage | Score |\n\
             | --: | --: | --: | --: |\n\
             | {killed} | {} | {} | {score} |",
            counts.survived, counts.no_coverage,
        );
    }
    format!(
        "| Killed | Survived | No coverage | Not measured | Score |\n\
         | --: | --: | --: | --: | --: |\n\
         | {killed} | {} | {} | {not_measured} | {score} |",
        counts.survived, counts.no_coverage,
    )
}

const fn scope_line(scope: Option<MutationScopeKind>) -> Option<&'static str> {
    match scope {
        // The changed lines are the default scope of a pull request, so the
        // section states only a scope that a reader would not expect.
        Some(MutationScopeKind::Changed) | None => None,
        Some(MutationScopeKind::All) => Some("Scope: all files."),
        Some(MutationScopeKind::Paths) => Some("Scope: the selected paths."),
    }
}

fn run_facts_line(view: &MutationCiView) -> Option<String> {
    let elapsed = view
        .elapsed_ms
        .map(|ms| format!("The run took {}.", duration_text(ms)));
    let cache = view.cache_hits.filter(|hits| *hits > 0).map(|hits| {
        if hits == 1 {
            "1 result came from the cache.".to_owned()
        } else {
            format!("{hits} results came from the cache.")
        }
    });
    let parts: Vec<String> = elapsed.into_iter().chain(cache).collect();
    (!parts.is_empty()).then(|| parts.join(" "))
}

fn duration_text(ms: u64) -> String {
    let seconds = ms / MILLIS_PER_SECOND;
    if seconds == 0 {
        return "less than 1s".to_owned();
    }
    let hours = seconds / SECONDS_PER_HOUR;
    let minutes = seconds % SECONDS_PER_HOUR / SECONDS_PER_MINUTE;
    let rest = seconds % SECONDS_PER_MINUTE;
    if hours > 0 {
        format!("{hours}h {minutes}m {rest}s")
    } else if minutes > 0 {
        format!("{minutes}m {rest}s")
    } else {
        format!("{rest}s")
    }
}

fn survivor_explanation(view: &MutationCiView, heading: MutationHeading) -> Option<String> {
    const FIRST: &str = "A survived mutant is a code change that no test detects.";
    const LAST: &str = "Add a test that fails on the change.";
    if view.counts.survived == 0 {
        return None;
    }
    let lines = if view.scope == Some(MutationScopeKind::Changed) {
        " on the changed lines"
    } else {
        ""
    };
    // Point at the findings only when this render shows at least one. A
    // survivor in a changed file or off the diff has no finding.
    let shown = view
        .counts
        .survived
        .saturating_sub(view.stale_hidden.saturating_add(view.off_diff_hidden));
    if shown == 0 {
        return Some(format!("{FIRST} {LAST}"));
    }
    // The job summary has no findings, so only the comment and the review
    // point at the place where the survived mutants are.
    Some(match heading {
        MutationHeading::Summary | MutationHeading::CompactComment => format!("{FIRST} {LAST}"),
        MutationHeading::Comment => {
            format!("{FIRST} The **Tests** section above lists the survived mutants{lines}. {LAST}")
        }
        MutationHeading::Review => {
            format!("{FIRST} The inline comments show the survived mutants{lines}. {LAST}")
        }
    })
}

fn hidden_line(count: usize, singular: &str, plural: &str) -> Option<String> {
    match count {
        0 => None,
        1 => Some(format!("1 {singular}")),
        _ => Some(format!("{count} {plural}")),
    }
}

fn no_coverage_details(locations: &[MutationCiLocation]) -> Option<String> {
    if locations.is_empty() {
        return None;
    }
    let mut sorted = locations.to_vec();
    sorted.sort();
    sorted.dedup();
    let total = sorted.len();
    let count = if total > MUTATION_NO_COVERAGE_ITEMS {
        format!("{total} lines, the first {MUTATION_NO_COVERAGE_ITEMS} shown")
    } else {
        count_noun(total, "line", "lines")
    };
    let items = sorted
        .iter()
        .take(MUTATION_NO_COVERAGE_ITEMS)
        .map(location_item)
        .collect::<Vec<_>>()
        .join("\n");
    Some(format!(
        "<details>\n<summary>No test reaches these lines ({count})</summary>\n\n{items}\n\n</details>"
    ))
}

fn location_item(location: &MutationCiLocation) -> String {
    let place = inline_code(&format!("{}:{}", location.file, location.line));
    let function = location
        .function
        .as_deref()
        .map(|name| single_line(name, INLINE_VALUE_MAX_CHARS))
        .filter(|name| !name.is_empty());
    match function {
        Some(name) => format!("- {place}, function {}", markdown_code_span(&name)),
        None => format!("- {place}"),
    }
}

fn inspect_line(view: &MutationCiView) -> Option<String> {
    if view.counts.survived.saturating_add(view.counts.no_coverage) == 0 {
        return None;
    }
    let mut command = format!("fallow mutation inspect <mutant-id> --report {REPORT_FILE}");
    let root = view
        .root_flag
        .as_deref()
        .map(|root| single_line(root, INLINE_VALUE_MAX_CHARS))
        .filter(|root| !root.is_empty() && root != ".");
    if let Some(root) = root {
        command.push_str(" --root ");
        command.push_str(&shell_quote(&root));
    }
    Some(format!(
        "To inspect a mutant, run {} with the report that this job writes.",
        markdown_code_span(&command)
    ))
}

fn shell_quote(value: &str) -> String {
    let plain = value
        .chars()
        .all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '_' | '.' | '/' | '-' | '@' | '+'));
    if plain {
        value.to_owned()
    } else {
        format!("'{}'", value.replace('\'', r"'\''"))
    }
}

fn decision_outcome(view: &MutationCiView) -> (PrDecisionConclusion, String) {
    let counts = &view.counts;
    match &view.state {
        MutationCiState::NotRun(_) => (PrDecisionConclusion::Skipped, "not run".to_owned()),
        MutationCiState::Failed { .. }
        | MutationCiState::Interrupted
        | MutationCiState::Unreadable { .. } => {
            (PrDecisionConclusion::Failure, "failed".to_owned())
        }
        MutationCiState::Ran if is_partial(view) => (
            PrDecisionConclusion::Neutral,
            format!(
                "partial: {} of {} mutants ran",
                counts.executed(),
                counts.total
            ),
        ),
        MutationCiState::Ran if counts.total == 0 => {
            (PrDecisionConclusion::Success, "no mutant".to_owned())
        }
        MutationCiState::Ran => {
            let score = view.score.map_or_else(
                || "no score".to_owned(),
                |score| format!("{} score", score_percent(score)),
            );
            if counts.survived > 0 {
                (
                    PrDecisionConclusion::Neutral,
                    format!("{score}, {} survived", counts.survived),
                )
            } else if counts.no_coverage > 0 {
                (
                    PrDecisionConclusion::Neutral,
                    format!("{score}, {} not covered", counts.no_coverage),
                )
            } else {
                (PrDecisionConclusion::Success, score)
            }
        }
    }
}

const fn decision_scope(scope: Option<MutationScopeKind>) -> &'static str {
    match scope {
        Some(MutationScopeKind::Changed) => "changed lines",
        Some(MutationScopeKind::All) => "all files",
        Some(MutationScopeKind::Paths) => "selected paths",
        None => "-",
    }
}

fn count_noun(count: usize, singular: &str, plural: &str) -> String {
    if count == 1 {
        format!("1 {singular}")
    } else {
        format!("{count} {plural}")
    }
}

/// A score fraction as a percent with one decimal.
fn score_percent(score: f64) -> String {
    percent_floor(score * PERCENT)
}

/// A percent with one decimal, rounded down, so that a score just below the
/// minimum never shows as equal to it.
fn percent_floor(percent: f64) -> String {
    let floored = percent.mul_add(TENTHS, FLOOR_EPSILON).floor() / TENTHS;
    format!("{floored:.1}%")
}

/// A configured minimum with at most two decimals and no trailing zero.
fn minimum_percent(percent: f64) -> String {
    let text = format!("{percent:.2}");
    let trimmed = text.trim_end_matches('0').trim_end_matches('.');
    format!("{trimmed}%")
}

/// A code span around an untrusted value on one line.
fn inline_code(value: &str) -> String {
    markdown_code_span(&single_line(value, INLINE_VALUE_MAX_CHARS))
}

/// An untrusted value on one line: each line ending and each other control
/// character becomes one space, and a value longer than `max` is cut.
fn single_line(value: &str, max: usize) -> String {
    let flat: String = value
        .replace("\r\n", " ")
        .chars()
        .map(|ch| if ch.is_control() { ' ' } else { ch })
        .collect();
    let flat = flat.trim();
    if flat.chars().count() <= max {
        return flat.to_owned();
    }
    let mut cut: String = flat.chars().take(max).collect();
    cut.push_str(CUT_SUFFIX);
    cut
}

/// An untrusted text with `\n` line endings, and no other control character
/// than a tab.
fn normalize_block(value: &str) -> String {
    value
        .replace("\r\n", "\n")
        .chars()
        .map(|ch| match ch {
            '\r' => '\n',
            '\n' | '\t' => ch,
            _ if ch.is_control() => ' ',
            _ => ch,
        })
        .collect::<String>()
        .trim()
        .to_owned()
}

/// Cut `value` to `max` characters and mark the cut.
fn cap_chars(value: &str, max: usize) -> String {
    if value.chars().count() <= max {
        return value.to_owned();
    }
    let mut cut: String = value.chars().take(max).collect();
    cut.push_str(SHORTENED_SUFFIX);
    cut
}

/// A fenced text block around an untrusted text, or `None` for an empty text.
/// Each run of three backticks becomes three single quotes, so the longest
/// backtick run left is two, and the four-backtick fence cannot close early.
fn text_block(value: &str) -> Option<String> {
    let text = normalize_block(value).replace(THREE_BACKTICKS, THREE_QUOTES);
    if text.is_empty() {
        return None;
    }
    let text = cap_chars(&text, MUTATION_MESSAGE_MAX_CHARS);
    Some(format!("{FENCE}text\n{text}\n{FENCE}"))
}

#[cfg(test)]
#[path = "mutation_ci_tests.rs"]
mod tests;