ripr 0.7.0

Find static mutation-exposure gaps before expensive mutation testing
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
pub(crate) mod agent_brief;
pub(crate) mod agent_review_summary;
pub(crate) mod agent_status;
pub(crate) mod agent_workflow;
mod check;
mod context;
mod explain;
mod selector;

pub use crate::output::format::OutputFormat;
pub use check::{check_workspace, check_workspace_repo, repo_seam_inventory_input};
pub(crate) use check::{check_workspace_repo_with_config, check_workspace_with_config};
pub(crate) use context::collect_context_with_config;
pub use context::{collect_context, collect_context_with_input};
pub(crate) use explain::explain_finding_with_config;
pub use explain::{explain_finding, explain_finding_with_input};

use crate::analysis::AnalysisMode;
use crate::config::RiprConfig;
use crate::domain::{Finding, Summary};
use crate::output;
use std::path::PathBuf;

/// Input contract for [`check_workspace`].
///
/// This structure mirrors the user-facing CLI switches but is exposed for
/// library consumers that embed `ripr` checks in their own tooling.
#[derive(Clone, Debug)]
pub struct CheckInput {
    /// Workspace root used for discovery and analysis.
    pub root: PathBuf,
    /// Git base revision used when collecting a diff automatically.
    pub base: Option<String>,
    /// Optional path to a unified diff file. When set, `base` is ignored.
    pub diff_file: Option<PathBuf>,
    /// Analysis effort profile.
    pub mode: Mode,
    /// Preferred renderer for programmatic wrappers.
    pub format: OutputFormat,
    /// Whether unchanged tests may still be used as static evidence.
    pub include_unchanged_tests: bool,
}

impl Default for CheckInput {
    fn default() -> Self {
        Self {
            root: PathBuf::from("."),
            base: Some("origin/main".to_string()),
            diff_file: None,
            mode: Mode::Draft,
            format: OutputFormat::Human,
            include_unchanged_tests: true,
        }
    }
}

/// Public analysis effort profile used by both CLI flags and library
/// integrations.
///
/// Modes tune static evidence collection cost versus depth, while keeping
/// result language in terms of exposure estimates (`exposed`,
/// `weakly_exposed`, unknown classes) rather than runtime mutation outcomes.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Mode {
    /// Minimal-latency local feedback.
    Instant,
    /// Default developer draft mode.
    Draft,
    /// Faster-than-deep with broader evidence than draft.
    Fast,
    /// Higher-effort local review mode.
    Deep,
    /// Review-ready mode used before sharing results.
    Ready,
}

impl Mode {
    /// Returns the stable CLI/programmatic label for this mode.
    pub fn as_str(&self) -> &'static str {
        match self {
            Mode::Instant => "instant",
            Mode::Draft => "draft",
            Mode::Fast => "fast",
            Mode::Deep => "deep",
            Mode::Ready => "ready",
        }
    }

    /// Maps a public mode to the internal analysis profile.
    pub fn analysis_mode(&self) -> AnalysisMode {
        match self {
            Mode::Instant => AnalysisMode::Instant,
            Mode::Draft => AnalysisMode::Draft,
            Mode::Fast => AnalysisMode::Fast,
            Mode::Deep => AnalysisMode::Deep,
            Mode::Ready => AnalysisMode::Ready,
        }
    }
}

/// Result payload produced by [`check_workspace`].
#[derive(Clone, Debug)]
pub struct CheckOutput {
    /// Output schema version for machine consumers.
    pub schema_version: String,
    /// Tool identifier.
    pub tool: String,
    /// Mode used for this analysis.
    pub mode: Mode,
    /// Analyzed workspace root.
    pub root: PathBuf,
    /// Base revision used to build the diff when applicable.
    pub base: Option<String>,
    /// Summary counts and high-level evidence status.
    pub summary: Summary,
    /// Probe-level findings.
    pub findings: Vec<Finding>,
}

/// Renders a previously computed [`CheckOutput`] in the requested format.
///
/// Returns `Err` when the requested format requires auxiliary inputs that
/// are not present — currently only the `BadgePlus*` formats, which read
/// the test-efficiency report. The other formats are infallible and
/// always return `Ok`.
pub fn render_check(output: &CheckOutput, format: &OutputFormat) -> Result<String, String> {
    render_check_with_config(output, format, &RiprConfig::default())
}

pub(crate) fn render_check_with_config(
    output: &CheckOutput,
    format: &OutputFormat,
    config: &RiprConfig,
) -> Result<String, String> {
    output::render::render_check_with_config(output, format, config)
}

#[cfg(test)]
mod tests {
    use super::{
        CheckOutput, Mode, OutputFormat, render_check, render_check_with_config,
        selector::selector_matches_location,
    };
    use crate::analysis::AnalysisMode;
    use crate::domain::{
        ActivationEvidence, Confidence, ExposureClass, Finding, OracleStrength, Probe, ProbeFamily,
        ProbeId, RelatedTest, RevealEvidence, RiprEvidence, SourceLocation, StageEvidence,
        StageState, StopReason, Summary,
    };
    use std::path::PathBuf;

    #[test]
    fn mode_labels_match_public_contract() {
        assert_eq!(Mode::Instant.as_str(), "instant");
        assert_eq!(Mode::Draft.as_str(), "draft");
        assert_eq!(Mode::Fast.as_str(), "fast");
        assert_eq!(Mode::Deep.as_str(), "deep");
        assert_eq!(Mode::Ready.as_str(), "ready");
    }

    #[test]
    fn mode_maps_to_internal_profiles() {
        assert_eq!(Mode::Instant.analysis_mode(), AnalysisMode::Instant);
        assert_eq!(Mode::Draft.analysis_mode(), AnalysisMode::Draft);
        assert_eq!(Mode::Fast.analysis_mode(), AnalysisMode::Fast);
        assert_eq!(Mode::Deep.analysis_mode(), AnalysisMode::Deep);
        assert_eq!(Mode::Ready.analysis_mode(), AnalysisMode::Ready);
    }

    #[test]
    fn selector_matches_exact_and_suffix_file_locations() {
        let finding = sample_finding("src/lib.rs", 42);

        assert!(selector_matches_location("src/lib.rs:42", &finding));
        assert!(selector_matches_location(
            "crates/ripr/src/lib.rs:42",
            &finding
        ));
        assert!(selector_matches_location(
            "crates\\ripr\\src/lib.rs:42",
            &finding
        ));
        assert!(!selector_matches_location("src/lib.rs:41", &finding));
        assert!(!selector_matches_location("src/main.rs:42", &finding));
    }

    #[test]
    fn selector_rejects_location_lookalikes_that_only_contain_file_text() {
        let finding = sample_finding("src/lib.rs", 42);

        assert!(!selector_matches_location("src/lib.rs.bak:42", &finding));
        assert!(!selector_matches_location(
            "generated-src/lib.rs:42",
            &finding
        ));
        assert!(!selector_matches_location("src/lib.rs", &finding));
        assert!(!selector_matches_location("src/lib.rs:042", &finding));
    }

    fn sample_finding(file: &str, line: usize) -> Finding {
        Finding {
            id: "probe:src_lib_rs:42:error_path".to_string(),
            probe: Probe {
                id: ProbeId("probe:src_lib_rs:42:error_path".to_string()),
                family: ProbeFamily::ErrorPath,
                location: SourceLocation::new(file, line, 1),
                owner: None,
                delta: crate::domain::DeltaKind::Control,
                before: None,
                after: None,
                expression: "sample_expr".to_string(),
                expected_sinks: Vec::new(),
                required_oracles: Vec::new(),
            },
            class: ExposureClass::WeaklyExposed,
            ripr: RiprEvidence {
                reach: StageEvidence::new(StageState::Yes, Confidence::Medium, "reached"),
                infect: StageEvidence::new(StageState::Weak, Confidence::Low, "infected"),
                propagate: StageEvidence::new(StageState::No, Confidence::Medium, "not propagated"),
                reveal: RevealEvidence {
                    observe: StageEvidence::new(StageState::Weak, Confidence::Low, "observed"),
                    discriminate: StageEvidence::new(
                        StageState::No,
                        Confidence::Medium,
                        "no discriminator",
                    ),
                },
            },
            confidence: 0.5,
            evidence: vec!["changed test".to_string()],
            missing: vec!["strong oracle".to_string()],
            flow_sinks: Vec::new(),
            activation: ActivationEvidence::default(),
            stop_reasons: vec![StopReason::NoChangedRustLine],
            related_tests: vec![RelatedTest {
                name: "sample_test".to_string(),
                file: "tests/sample.rs".into(),
                line: 10,
                oracle: None,
                oracle_kind: crate::domain::OracleKind::Unknown,
                oracle_strength: OracleStrength::Weak,
            }],
            recommended_next_step: Some("add stronger assertion".to_string()),
            language: None,
            language_status: None,
            owner_kind: None,
            static_limit_kind: None,
        }
    }

    #[test]
    fn summary_default_is_empty() {
        let summary = Summary::default();
        assert_eq!(summary.findings, 0);
        assert_eq!(summary.exposed, 0);
        assert_eq!(summary.weakly_exposed, 0);
    }

    fn check_output_with(findings: Vec<Finding>) -> CheckOutput {
        CheckOutput {
            schema_version: "0.1".to_string(),
            tool: "ripr".to_string(),
            mode: Mode::Draft,
            root: PathBuf::from("."),
            base: Some("origin/main".to_string()),
            summary: Summary::default(),
            findings,
        }
    }

    fn check_output_with_temp_seam_workspace(
        findings: Vec<Finding>,
    ) -> Result<CheckOutput, String> {
        let stamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_nanos())
            .unwrap_or(0);
        let root = std::env::temp_dir().join(format!("ripr-app-repo-badge-{stamp}"));
        std::fs::create_dir_all(root.join("src"))
            .map_err(|err| format!("create temp src dir: {err}"))?;
        std::fs::write(
            root.join("Cargo.toml"),
            "[package]\nname=\"ripr-app-repo-badge\"\nversion=\"0.1.0\"\nedition=\"2024\"\n",
        )
        .map_err(|err| format!("write temp Cargo.toml: {err}"))?;
        std::fs::write(
            root.join("src/lib.rs"),
            "pub fn over_threshold(amount: i32, threshold: i32) -> bool {\n    amount >= threshold\n}\n",
        )
        .map_err(|err| format!("write temp src/lib.rs: {err}"))?;

        let mut output = check_output_with(findings);
        output.root = root;
        Ok(output)
    }

    #[test]
    fn configured_finding_severity_applies_to_human_json_and_github() -> Result<(), String> {
        let output = check_output_with(vec![sample_finding("src/lib.rs", 1)]);
        let config =
            crate::config::tests_only_parse("[severity.findings]\nweakly_exposed = \"info\"\n")?;

        let human = render_check_with_config(&output, &OutputFormat::Human, &config)?;
        let json = render_check_with_config(&output, &OutputFormat::Json, &config)?;
        let github = render_check_with_config(&output, &OutputFormat::Github, &config)?;

        if !human.contains("INFO src/lib.rs:1") {
            return Err(format!("human severity was not configured: {human}"));
        }
        if !json.contains("\"severity\": \"info\"") {
            return Err(format!("json severity was not configured: {json}"));
        }
        if !github.starts_with("::notice ") {
            return Err(format!("github severity was not configured: {github}"));
        }
        Ok(())
    }

    #[test]
    fn render_check_dispatches_badge_json_format() -> Result<(), String> {
        let output = check_output_with(vec![sample_finding("src/lib.rs", 1)]);
        let rendered = render_check(&output, &OutputFormat::BadgeJson)?;

        // Native snake_case wire shape with all required top-level keys.
        assert!(rendered.contains("\"schema_version\": \"0.5\""));
        assert!(rendered.contains("\"kind\": \"ripr\""));
        assert!(rendered.contains("\"scope\": \"diff\""));
        assert!(rendered.contains("\"basis\": \"finding_exposure\""));
        assert!(rendered.contains("\"counts\":"));
        assert!(rendered.contains("\"reason_counts\":"));
        assert!(rendered.contains("\"policy\":"));
        // Specifically includes the new vocabulary from #187/#188 with zero default.
        assert!(rendered.contains("\"duplicate_activation_and_oracle_shape\": 0"));
        Ok(())
    }

    #[test]
    fn render_check_dispatches_badge_shields_format() -> Result<(), String> {
        let output = check_output_with(vec![sample_finding("src/lib.rs", 1)]);
        let rendered = render_check(&output, &OutputFormat::BadgeShields)?;

        assert!(rendered.contains("\"schemaVersion\": 1"));
        assert!(rendered.contains("\"label\":"));
        assert!(rendered.contains("\"message\":"));
        assert!(rendered.contains("\"color\":"));
        // Native-only fields must not leak into the Shields shape.
        for forbidden in [
            "\"counts\"",
            "\"reason_counts\"",
            "\"policy\"",
            "\"kind\"",
            "\"status\"",
            "\"scope\"",
            "\"basis\"",
        ] {
            assert!(
                !rendered.contains(forbidden),
                "Shields projection must not contain `{forbidden}`"
            );
        }
        Ok(())
    }

    #[test]
    fn badge_render_message_has_no_denominator_or_coverage_framing() -> Result<(), String> {
        let output = check_output_with(vec![
            sample_finding("src/a.rs", 1),
            sample_finding("src/b.rs", 2),
        ]);
        for format in [OutputFormat::BadgeJson, OutputFormat::BadgeShields] {
            let rendered = render_check(&output, &format)?;
            let lower = rendered.to_ascii_lowercase();
            // Confirm no "X/Y" denominator pattern in the message field; the
            // message itself is just a count string.
            assert!(
                !rendered.contains("\"message\": \"") || {
                    let after = rendered.split("\"message\": \"").nth(1).unwrap_or("");
                    let value_end = after.find('"').unwrap_or(after.len());
                    let value = &after[..value_end];
                    !value.contains('/')
                },
                "badge message must not contain a denominator: {rendered}"
            );
            assert!(!lower.contains("coverage"));
            assert!(!lower.contains("uncovered"));
        }
        Ok(())
    }

    #[test]
    fn render_check_repo_badge_json_paints_scope_repo() -> Result<(), String> {
        let output = check_output_with_temp_seam_workspace(vec![sample_finding("src/lib.rs", 1)])?;
        let rendered = render_check(&output, &OutputFormat::RepoBadgeJson)?;

        assert!(rendered.contains("\"schema_version\": \"0.5\""));
        assert!(rendered.contains("\"scope\": \"repo\""));
        assert!(rendered.contains("\"basis\": \"canonical_actionable_gap\""));
        assert!(!rendered.contains("\"scope\": \"diff\""));
        assert!(rendered.contains("\"kind\": \"ripr\""));
        assert!(rendered.contains("\"analyzed_findings\": 0"));
        assert!(!rendered.contains("\"analyzed_seams\": 0"));
        let _ = std::fs::remove_dir_all(&output.root);
        Ok(())
    }

    #[test]
    fn render_check_repo_badge_shields_stays_four_fields_without_scope_leak() -> Result<(), String>
    {
        let output = check_output_with_temp_seam_workspace(vec![sample_finding("src/lib.rs", 1)])?;
        let rendered = render_check(&output, &OutputFormat::RepoBadgeShields)?;

        // Scope is native-only metadata; Shields stays a four-field projection.
        assert!(!rendered.contains("\"scope\""));
        assert!(!rendered.contains("\"basis\""));
        let top_level_keys = rendered
            .lines()
            .filter(|line| line.starts_with("  \""))
            .count();
        assert_eq!(top_level_keys, 4);
        for forbidden in [
            "\"counts\"",
            "\"reason_counts\"",
            "\"policy\"",
            "\"kind\"",
            "\"status\"",
            "\"schema_version\"",
        ] {
            assert!(
                !rendered.contains(forbidden),
                "Shields projection must not contain `{forbidden}`"
            );
        }
        let _ = std::fs::remove_dir_all(&output.root);
        Ok(())
    }

    #[test]
    fn render_check_badge_plus_fails_when_test_efficiency_report_missing() -> Result<(), String> {
        // CheckOutput.root points at a temporary directory that does NOT
        // contain target/ripr/reports/test-efficiency.json.
        let stamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_nanos())
            .unwrap_or(0);
        let tmp = std::env::temp_dir().join(format!("ripr-badge-plus-missing-{stamp}"));
        std::fs::create_dir_all(&tmp).map_err(|e| format!("create temp dir: {e}"))?;

        let mut output = check_output_with(vec![sample_finding("src/lib.rs", 1)]);
        output.root = tmp.clone();

        let result = render_check(&output, &OutputFormat::BadgePlusJson);
        assert!(result.is_err(), "badge-plus must fail when report missing");
        let err = result.err().unwrap_or_default();
        assert!(
            err.contains("test-efficiency.json"),
            "error must name the missing report: {err}"
        );
        assert!(
            err.contains("cargo xtask test-efficiency-report"),
            "error must direct the user to the regenerator command: {err}"
        );

        let _ = std::fs::remove_dir_all(&tmp);
        Ok(())
    }
}