fallow-api 3.35.0

Programmatic API contract types for fallow
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
//! Joins a mutation report with a separate verdict document.
//!
//! A verdict marks one mutant as equivalent to the original code. This
//! replaces an inline suppression comment: the report stays the evidence, and the verdict is a separate,
//! reviewable judgment. The join key is the `review_key` of the verdict, which
//! is the `mutant_id` of the report. The id hashes the enclosing function
//! text, so a verdict stays valid until that function changes.

use std::collections::BTreeSet;

use fallow_output::{
    MutantStatus, MutationReport, MutationStatusCounts, MutationSummary, MutationVerdictInput,
    MutationVerdictJoin,
};
use rustc_hash::FxHashSet;
use std::fmt::Write as _;

use sha2::{Digest, Sha256};

use crate::ProgrammaticError;

/// The largest verdict document that the join reads.
pub const MAX_VERDICT_INPUT_BYTES: usize = 16 * 1024 * 1024;

fn verdict_error(message: impl Into<String>) -> ProgrammaticError {
    ProgrammaticError::new(message, 2)
        .with_code("FALLOW_MUTATION_VERDICTS_INVALID")
        .with_context("mutation.verdicts")
}

fn sha256_hex(bytes: &[u8]) -> String {
    Sha256::digest(bytes)
        .iter()
        .fold(String::with_capacity(64), |mut hex, byte| {
            let _ = write!(hex, "{byte:02x}");
            hex
        })
}

/// The effect of one verdict on a mutant with `status`.
enum Effect {
    Applied,
    Contradicted,
    Deferred,
}

const fn effect(status: MutantStatus) -> Effect {
    match status {
        MutantStatus::Survived
        | MutantStatus::NoCoverage
        | MutantStatus::Flaky
        | MutantStatus::Equivalent => Effect::Applied,
        MutantStatus::Killed | MutantStatus::Timeout | MutantStatus::CompileError => {
            Effect::Contradicted
        }
        MutantStatus::Pending | MutantStatus::RuntimeError | MutantStatus::Pruned => {
            Effect::Deferred
        }
    }
}

/// Moves one mutant from `from` to `equivalent` in the function group that
/// holds it.
fn move_in_function(report: &mut MutationReport, index: usize, from: MutantStatus) {
    let location = &report.mutants[index].location;
    let key = (
        location.file.as_str(),
        location.function.as_deref(),
        location.function_line,
    );
    let group = report.functions.iter_mut().find(|group| {
        (group.file.as_str(), group.name.as_deref(), group.line) == key
            && count_of(&group.counts, from) > 0
    });
    let Some(group) = group else {
        return;
    };
    group.counts.remove(from);
    group.counts.add(MutantStatus::Equivalent);
    group.survived.retain(|id| *id != location.mutant_id);
    group.score = group.counts.score();
}

const fn count_of(counts: &MutationStatusCounts, status: MutantStatus) -> usize {
    match status {
        MutantStatus::Killed => counts.killed,
        MutantStatus::Timeout => counts.timeout,
        MutantStatus::Survived => counts.survived,
        MutantStatus::NoCoverage => counts.no_coverage,
        MutantStatus::RuntimeError => counts.runtime_error,
        MutantStatus::Flaky => counts.flaky,
        MutantStatus::Pruned => counts.pruned,
        MutantStatus::Pending => counts.pending,
        MutantStatus::Equivalent => counts.equivalent,
        MutantStatus::CompileError => counts.compile_error,
    }
}

/// Parses a verdict document and checks its keys.
fn parse_verdicts(verdict_json: &[u8]) -> Result<MutationVerdictInput, ProgrammaticError> {
    if verdict_json.len() > MAX_VERDICT_INPUT_BYTES {
        return Err(verdict_error(
            "the mutation verdict document is larger than 16 MiB",
        ));
    }
    let input: MutationVerdictInput = serde_json::from_slice(verdict_json)
        .map_err(|error| verdict_error(format!("invalid mutation verdict document: {error}")))?;
    let mut keys = FxHashSet::default();
    for verdict in &input.verdicts {
        if verdict.review_key.trim().is_empty() {
            return Err(verdict_error("a mutation verdict has an empty review_key"));
        }
        if !keys.insert(verdict.review_key.as_str()) {
            return Err(verdict_error(format!(
                "the review_key `{}` occurs more than one time",
                verdict.review_key
            )));
        }
    }
    Ok(input)
}

/// Checks a verdict document without a report.
///
/// A caller runs this check before a mutation run, so an invalid document
/// stops the command before any test runs.
///
/// # Errors
///
/// Returns the same errors as [`apply_mutation_verdicts`].
pub fn validate_mutation_verdicts(verdict_json: &[u8]) -> Result<(), ProgrammaticError> {
    parse_verdicts(verdict_json).map(|_| ())
}

/// Applies a verdict document to `report`.
///
/// Each verdict for a `survived`, `no_coverage` or `flaky` mutant sets the
/// status `equivalent`, which does not count in the score. A verdict for a
/// killed or timed-out mutant is contradicted by a test, so the status stays.
/// A verdict for a mutant with no tested result (`pending`, `runtime_error`,
/// `pruned`) is deferred. A verdict that matches no mutant is unmatched; it
/// is not an error, because one verdict document serves many scoped runs.
/// The summary and the function results are recomputed.
///
/// # Errors
///
/// Returns a fail-closed error for a document over 16 MiB, a malformed
/// document, an empty `review_key`, or a `review_key` that occurs twice.
pub fn apply_mutation_verdicts(
    report: &mut MutationReport,
    verdict_json: &[u8],
) -> Result<(), ProgrammaticError> {
    let input = parse_verdicts(verdict_json)?;
    let keys: FxHashSet<&str> = input
        .verdicts
        .iter()
        .map(|verdict| verdict.review_key.as_str())
        .collect();

    let mut applied = BTreeSet::new();
    let mut contradicted = BTreeSet::new();
    let mut deferred = BTreeSet::new();
    let mut matched = FxHashSet::default();
    for index in 0..report.mutants.len() {
        let id = report.mutants[index].location.mutant_id.clone();
        if !keys.contains(id.as_str()) {
            continue;
        }
        matched.insert(id.clone());
        let status = report.mutants[index].status;
        match effect(status) {
            Effect::Applied => {
                if status != MutantStatus::Equivalent {
                    report.mutants[index].status = MutantStatus::Equivalent;
                    move_in_function(report, index, status);
                }
                applied.insert(id);
            }
            Effect::Contradicted => {
                contradicted.insert(id);
            }
            Effect::Deferred => {
                deferred.insert(id);
            }
        }
    }
    let unmatched: BTreeSet<String> = input
        .verdicts
        .iter()
        .filter(|verdict| !matched.contains(&verdict.review_key))
        .map(|verdict| verdict.review_key.clone())
        .collect();

    report.summary = MutationSummary::from_results(&report.mutants);
    report.verdicts = Some(MutationVerdictJoin {
        verdicts_sha256: sha256_hex(verdict_json),
        applied: applied.into_iter().collect(),
        contradicted: contradicted.into_iter().collect(),
        deferred: deferred.into_iter().collect(),
        unmatched: unmatched.into_iter().collect(),
    });
    Ok(())
}

#[cfg(test)]
#[expect(clippy::unwrap_used, reason = "tests unwrap to keep assertions short")]
mod tests {
    use fallow_output::{
        MutantLocation, MutantResult, MutationCompletion, MutationFunctionResult,
        MutationSchemaVersion, MutationScopeKind, MutationScopeOutput, MutationSummary,
        MutationTimings, MutationWorktreeCheck,
    };
    use fallow_types::envelope::{ElapsedMs, ToolVersion};

    use super::*;

    fn mutant(id: &str, status: MutantStatus) -> MutantResult {
        MutantResult {
            location: MutantLocation {
                mutant_id: id.to_owned(),
                file: "src/a.ts".to_owned(),
                line: 2,
                column: 3,
                span: (10, 15),
                operator: "arithmetic-operator".to_owned(),
                function: Some("add".to_owned()),
                function_line: Some(1),
                rank: Some(1),
                change: None,
            },
            status,
            prune_reason: None,
            killed_by: None,
            covering_tests: match status {
                MutantStatus::NoCoverage | MutantStatus::Pending | MutantStatus::Pruned => {
                    Vec::new()
                }
                _ => vec!["test/a.test.ts::t1".to_owned()],
            },
            static_mutant: false,
            duration_ms: None,
            test_reach: None,
            retried: false,
            attempts: Vec::new(),
            cached: false,
            type_error_codes: Vec::new(),
        }
    }

    fn report(mutants: Vec<MutantResult>) -> MutationReport {
        let mut counts = MutationStatusCounts::default();
        let mut survived = Vec::new();
        for result in &mutants {
            counts.add(result.status);
            if result.status == MutantStatus::Survived {
                survived.push(result.location.mutant_id.clone());
            }
        }
        MutationReport {
            schema_version: MutationSchemaVersion::V1,
            version: ToolVersion("0.0.0".to_owned()),
            elapsed_ms: ElapsedMs(0),
            completion: MutationCompletion::from_reasons(Vec::new()),
            scope: MutationScopeOutput {
                kind: MutationScopeKind::All,
                base_ref: None,
            },
            runner: None,
            cache: None,
            summary: MutationSummary::from_results(&mutants),
            functions: vec![MutationFunctionResult {
                file: "src/a.ts".to_owned(),
                name: Some("add".to_owned()),
                line: Some(1),
                end_line: Some(4),
                in_diff: true,
                cyclomatic: Some(1),
                counts,
                score: counts.score(),
                survived,
            }],
            mutants,
            no_test_files: false,
            baseline_failures: Vec::new(),
            skipped_files: Vec::new(),
            unmatched_paths: Vec::new(),
            unloaded_paths: Vec::new(),
            worktree: MutationWorktreeCheck {
                checked: true,
                allow_dirty: false,
                dirty_at_start: Vec::new(),
                changed_paths: Vec::new(),
            },
            errors: Vec::new(),
            timings: MutationTimings::default(),
            files: Vec::new(),
            verdicts: None,
            typecheck: None,
        }
    }

    fn document(keys: &[&str]) -> Vec<u8> {
        let verdicts: Vec<serde_json::Value> = keys
            .iter()
            .map(|key| serde_json::json!({ "review_key": key, "verdict": "equivalent" }))
            .collect();
        serde_json::to_vec(&serde_json::json!({ "schema_version": "1", "verdicts": verdicts }))
            .unwrap()
    }

    #[test]
    fn equivalent_verdicts_leave_the_score_and_the_survivor_list() {
        let mut report = report(vec![
            mutant("m1", MutantStatus::Killed),
            mutant("m2", MutantStatus::Survived),
            mutant("m3", MutantStatus::Survived),
            mutant("m4", MutantStatus::NoCoverage),
        ]);
        assert_eq!(report.summary.score, Some(1.0 / 3.0));
        assert_eq!(report.summary.coverage, Some(0.75));

        apply_mutation_verdicts(&mut report, &document(&["m2", "m4"])).unwrap();

        let statuses: Vec<MutantStatus> = report.mutants.iter().map(|m| m.status).collect();
        assert_eq!(
            statuses,
            vec![
                MutantStatus::Killed,
                MutantStatus::Equivalent,
                MutantStatus::Survived,
                MutantStatus::Equivalent
            ]
        );
        assert_eq!(report.summary.counts.equivalent, 2);
        assert_eq!(report.summary.counts.survived, 1);
        assert_eq!(report.summary.counts.no_coverage, 0);
        assert_eq!(report.summary.counts.total, 4);
        assert_eq!(report.summary.score, Some(0.5));
        assert_eq!(report.summary.coverage, Some(1.0));
        let function = &report.functions[0];
        assert_eq!(function.counts.equivalent, 2);
        assert_eq!(function.counts.no_coverage, 0);
        assert_eq!(function.survived, vec!["m3".to_owned()]);
        assert_eq!(function.score, Some(0.5));
        let join = report.verdicts.unwrap();
        assert_eq!(join.applied, vec!["m2".to_owned(), "m4".to_owned()]);
        assert!(join.unmatched.is_empty());
        assert_eq!(join.verdicts_sha256.len(), 64);
    }

    #[test]
    fn a_kill_contradicts_a_verdict_and_unknown_keys_stay_unmatched() {
        let mut report = report(vec![
            mutant("m1", MutantStatus::Killed),
            mutant("m2", MutantStatus::Pending),
            mutant("m3", MutantStatus::Survived),
        ]);

        apply_mutation_verdicts(&mut report, &document(&["gone", "m1", "m2"])).unwrap();

        assert_eq!(report.mutants[0].status, MutantStatus::Killed);
        assert_eq!(report.mutants[1].status, MutantStatus::Pending);
        assert_eq!(report.mutants[2].status, MutantStatus::Survived);
        assert_eq!(report.summary.score, Some(0.5));
        let join = report.verdicts.unwrap();
        assert!(join.applied.is_empty());
        assert_eq!(join.contradicted, vec!["m1".to_owned()]);
        assert_eq!(join.deferred, vec!["m2".to_owned()]);
        assert_eq!(join.unmatched, vec!["gone".to_owned()]);
    }

    #[test]
    fn malformed_and_duplicate_verdicts_fail_closed() {
        let cases: [(&[u8], &str); 4] = [
            (b"{", "invalid mutation verdict document"),
            (
                br#"{"schema_version":"1","verdicts":[{"review_key":"m1","verdict":"survived"}]}"#,
                "invalid mutation verdict document",
            ),
            (
                br#"{"schema_version":"1","verdicts":[{"review_key":" ","verdict":"equivalent"}]}"#,
                "empty review_key",
            ),
            (&document(&["m1", "m1"]), "more than one time"),
        ];
        for (json, needle) in cases {
            let checked = validate_mutation_verdicts(json).unwrap_err();
            assert_eq!(checked.exit_code, 2);
            assert!(checked.message.contains(needle), "{}", checked.message);
            let mut report = report(vec![mutant("m1", MutantStatus::Survived)]);
            let error = apply_mutation_verdicts(&mut report, json).unwrap_err();
            assert_eq!(
                error.code.as_deref(),
                Some("FALLOW_MUTATION_VERDICTS_INVALID")
            );
            assert_eq!(error.exit_code, 2);
            assert!(error.message.contains(needle), "{}", error.message);
            assert_eq!(report.mutants[0].status, MutantStatus::Survived);
            assert!(report.verdicts.is_none());
        }
        assert!(validate_mutation_verdicts(&document(&["m1", "m2"])).is_ok());
    }
}