perfgate 0.17.0

Core library for perfgate performance budgets and baseline diffs
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
//! DiffUseCase — git-aware zero-argument comparison.
//!
//! This module implements the `diff` workflow which:
//! 1. Auto-discovers `perfgate.toml` by walking up from cwd
//! 2. Determines which benchmarks to run (all, or filtered by name)
//! 3. Finds the baseline for each benchmark
//! 4. Runs each benchmark
//! 5. Compares against baseline
//! 6. Returns structured diff outcomes for terminal rendering

use crate::app::runtime::{HostProbe, ProcessRunner};
use crate::app::{Clock, CompareRequest, CompareUseCase, RunBenchRequest, RunBenchUseCase};
use anyhow::Context;
use perfgate_types::{
    BenchConfigFile, CompareReceipt, CompareRef, ConfigFile, HostMismatchPolicy, Metric,
    MetricStatistic, RunReceipt, ToolInfo, VerdictStatus,
};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

/// Request for the diff use case.
#[derive(Debug, Clone)]
pub struct DiffRequest {
    /// Path to config file. If `None`, auto-discover by walking up from cwd.
    pub config_path: Option<PathBuf>,

    /// Filter to a single benchmark by name.
    pub bench_filter: Option<String>,

    /// Git ref to compare against (reserved for future use).
    pub against: Option<String>,

    /// If true, reduce repeat count for faster feedback.
    pub quick: bool,

    /// If true, produce JSON output instead of terminal rendering.
    pub json: bool,

    /// Tool info for receipts.
    pub tool: ToolInfo,
}

/// Outcome for a single benchmark diff.
#[derive(Debug, Clone)]
pub struct BenchDiffOutcome {
    /// Name of the benchmark.
    pub bench_name: String,

    /// The run receipt produced.
    pub run_receipt: RunReceipt,

    /// The compare receipt (None if no baseline was found).
    pub compare_receipt: Option<CompareReceipt>,

    /// Path to the baseline that was used (if any).
    pub baseline_path: Option<PathBuf>,

    /// True if no baseline was found.
    pub no_baseline: bool,
}

/// Overall outcome of the diff command.
#[derive(Debug, Clone)]
pub struct DiffOutcome {
    /// Path to the config file that was used.
    pub config_path: PathBuf,

    /// Per-benchmark outcomes.
    pub bench_outcomes: Vec<BenchDiffOutcome>,

    /// Overall exit code (0=pass, 2=fail).
    pub exit_code: i32,
}

impl DiffOutcome {
    /// Returns the worst verdict status across all benchmarks.
    pub fn worst_verdict(&self) -> VerdictStatus {
        let mut worst = VerdictStatus::Pass;
        for outcome in &self.bench_outcomes {
            if let Some(compare) = &outcome.compare_receipt {
                match compare.verdict.status {
                    VerdictStatus::Fail => return VerdictStatus::Fail,
                    VerdictStatus::Warn => worst = VerdictStatus::Warn,
                    VerdictStatus::Skip if worst == VerdictStatus::Pass => {
                        worst = VerdictStatus::Skip;
                    }
                    _ => {}
                }
            }
        }
        worst
    }
}

/// Walk up from `start` looking for `perfgate.toml` or `perfgate.json`.
pub fn discover_config(start: &Path) -> Option<PathBuf> {
    let mut dir = start.to_path_buf();
    loop {
        let toml_path = dir.join("perfgate.toml");
        if toml_path.is_file() {
            return Some(toml_path);
        }
        let json_path = dir.join("perfgate.json");
        if json_path.is_file() {
            return Some(json_path);
        }
        if !dir.pop() {
            return None;
        }
    }
}

/// Use case for running a diff workflow.
pub struct DiffUseCase<R: ProcessRunner + Clone, H: HostProbe + Clone, C: Clock + Clone> {
    runner: R,
    host_probe: H,
    clock: C,
}

impl<R: ProcessRunner + Clone, H: HostProbe + Clone, C: Clock + Clone> DiffUseCase<R, H, C> {
    pub fn new(runner: R, host_probe: H, clock: C) -> Self {
        Self {
            runner,
            host_probe,
            clock,
        }
    }

    /// Execute the diff workflow.
    pub fn execute(&self, req: DiffRequest) -> anyhow::Result<DiffOutcome> {
        // 1. Discover config
        let config_path = match &req.config_path {
            Some(p) => p.clone(),
            None => {
                let cwd = std::env::current_dir().context("failed to get current directory")?;
                discover_config(&cwd).ok_or_else(|| {
                    anyhow::anyhow!(
                        "no perfgate.toml found (searched upward from {})",
                        cwd.display()
                    )
                })?
            }
        };

        // 2. Load config
        let config = perfgate_types::config::load_config_file(&config_path)
            .with_context(|| format!("failed to load config from {}", config_path.display()))?;

        config
            .validate()
            .map_err(|e| anyhow::anyhow!("config validation failed: {}", e))?;

        // 3. Determine which benchmarks to run
        let bench_names: Vec<String> = if let Some(filter) = &req.bench_filter {
            // Verify the bench exists
            if !config.benches.iter().any(|b| &b.name == filter) {
                let available: Vec<&str> = config.benches.iter().map(|b| b.name.as_str()).collect();
                anyhow::bail!(
                    "bench '{}' not found in config; available: {}",
                    filter,
                    available.join(", ")
                );
            }
            vec![filter.clone()]
        } else {
            if config.benches.is_empty() {
                anyhow::bail!("no benchmarks defined in {}", config_path.display());
            }
            config.benches.iter().map(|b| b.name.clone()).collect()
        };

        // 4. Run each benchmark and compare
        let mut bench_outcomes = Vec::new();
        let mut max_exit_code: i32 = 0;

        for bench_name in &bench_names {
            let bench_config = config
                .benches
                .iter()
                .find(|b| &b.name == bench_name)
                .ok_or_else(|| anyhow::anyhow!("bench '{}' not found in config", bench_name))?;

            let outcome = self.run_single_bench(bench_config, &config, &req)?;

            // Update exit code
            if let Some(compare) = &outcome.compare_receipt {
                match compare.verdict.status {
                    VerdictStatus::Fail => {
                        if max_exit_code < 2 {
                            max_exit_code = 2;
                        }
                    }
                    VerdictStatus::Warn | VerdictStatus::Pass | VerdictStatus::Skip => {}
                }
            }

            bench_outcomes.push(outcome);
        }

        Ok(DiffOutcome {
            config_path,
            bench_outcomes,
            exit_code: max_exit_code,
        })
    }

    fn run_single_bench(
        &self,
        bench: &BenchConfigFile,
        config: &ConfigFile,
        req: &DiffRequest,
    ) -> anyhow::Result<BenchDiffOutcome> {
        let defaults = &config.defaults;

        // Build run request
        let mut repeat = bench.repeat.or(defaults.repeat).unwrap_or(5);
        let warmup = bench.warmup.or(defaults.warmup).unwrap_or(0);

        // In quick mode, reduce repeat count
        if req.quick {
            repeat = repeat.clamp(1, 2);
        }

        let timeout = bench
            .timeout
            .as_deref()
            .map(|s| {
                humantime::parse_duration(s)
                    .with_context(|| format!("invalid timeout '{}' for bench '{}'", s, bench.name))
            })
            .transpose()?;

        let cwd = bench.cwd.as_ref().map(PathBuf::from);

        let run_request = RunBenchRequest {
            name: bench.name.clone(),
            cwd,
            command: bench.command.clone(),
            repeat,
            warmup,
            work_units: bench.work,
            timeout,
            env: Vec::new(),
            output_cap_bytes: 8192,
            allow_nonzero: false,
            include_hostname_hash: false,
        };

        // Run the benchmark
        let run_usecase = RunBenchUseCase::new(
            self.runner.clone(),
            self.host_probe.clone(),
            self.clock.clone(),
            req.tool.clone(),
        );
        let run_outcome = run_usecase.execute(run_request)?;
        let run_receipt = run_outcome.receipt;

        // Resolve baseline
        let baseline_path =
            perfgate_app_baseline_resolve::resolve_baseline_path(&None, &bench.name, config);

        let baseline_receipt = if baseline_path.is_file() {
            Some(perfgate_types::read_json_file::<RunReceipt>(
                &baseline_path,
            )?)
        } else {
            None
        };

        // Compare if baseline exists
        let compare_receipt = if let Some(baseline) = &baseline_receipt {
            let (budgets, metric_statistics) =
                build_diff_budgets(bench, config, baseline, &run_receipt)?;

            let compare_req = CompareRequest {
                baseline: baseline.clone(),
                current: run_receipt.clone(),
                budgets,
                metric_statistics,
                significance: None,
                tradeoffs: config.tradeoffs.clone(),
                baseline_ref: CompareRef {
                    path: Some(baseline_path.display().to_string()),
                    run_id: Some(baseline.run.id.clone()),
                },
                current_ref: CompareRef {
                    path: None,
                    run_id: Some(run_receipt.run.id.clone()),
                },
                tool: req.tool.clone(),
                host_mismatch_policy: HostMismatchPolicy::Warn,
            };

            Some(CompareUseCase::execute(compare_req)?.receipt)
        } else {
            None
        };

        Ok(BenchDiffOutcome {
            bench_name: bench.name.clone(),
            run_receipt,
            compare_receipt,
            baseline_path: Some(baseline_path),
            no_baseline: baseline_receipt.is_none(),
        })
    }
}

/// Build budgets for the diff comparison (simplified from CheckUseCase).
fn build_diff_budgets(
    bench: &BenchConfigFile,
    config: &ConfigFile,
    baseline: &RunReceipt,
    current: &RunReceipt,
) -> anyhow::Result<(
    BTreeMap<Metric, perfgate_types::Budget>,
    BTreeMap<Metric, MetricStatistic>,
)> {
    let defaults = &config.defaults;
    let global_threshold = defaults.threshold.unwrap_or(0.20);
    let global_warn_factor = defaults.warn_factor.unwrap_or(0.90);

    let mut candidates = Vec::new();
    candidates.push(Metric::WallMs);
    if baseline.stats.cpu_ms.is_some() && current.stats.cpu_ms.is_some() {
        candidates.push(Metric::CpuMs);
    }
    if baseline.stats.page_faults.is_some() && current.stats.page_faults.is_some() {
        candidates.push(Metric::PageFaults);
    }
    if baseline.stats.ctx_switches.is_some() && current.stats.ctx_switches.is_some() {
        candidates.push(Metric::CtxSwitches);
    }
    if baseline.stats.max_rss_kb.is_some() && current.stats.max_rss_kb.is_some() {
        candidates.push(Metric::MaxRssKb);
    }
    if baseline.stats.binary_bytes.is_some() && current.stats.binary_bytes.is_some() {
        candidates.push(Metric::BinaryBytes);
    }
    if baseline.stats.throughput_per_s.is_some() && current.stats.throughput_per_s.is_some() {
        candidates.push(Metric::ThroughputPerS);
    }

    let mut budgets = BTreeMap::new();
    let mut metric_statistics = BTreeMap::new();

    for metric in candidates {
        let override_opt = bench.budgets.as_ref().and_then(|b| b.get(&metric).cloned());

        let threshold = override_opt
            .as_ref()
            .and_then(|o| o.threshold)
            .unwrap_or(global_threshold);

        let warn_factor = override_opt
            .as_ref()
            .and_then(|o| o.warn_factor)
            .unwrap_or(global_warn_factor);

        let warn_threshold = threshold * warn_factor;

        let noise_threshold = override_opt
            .as_ref()
            .and_then(|o| o.noise_threshold)
            .or(defaults.noise_threshold);

        let noise_policy = override_opt
            .as_ref()
            .and_then(|o| o.noise_policy)
            .or(defaults.noise_policy)
            .unwrap_or(perfgate_types::NoisePolicy::Warn);

        let direction = override_opt
            .as_ref()
            .and_then(|o| o.direction)
            .unwrap_or_else(|| metric.default_direction());

        let statistic = override_opt
            .as_ref()
            .and_then(|o| o.statistic)
            .unwrap_or(MetricStatistic::Median);

        budgets.insert(
            metric,
            perfgate_types::Budget {
                threshold,
                warn_threshold,
                noise_threshold,
                noise_policy,
                direction,
            },
        );

        metric_statistics.insert(metric, statistic);
    }

    Ok((budgets, metric_statistics))
}

// Module alias to avoid name collision with the crate itself
mod perfgate_app_baseline_resolve {
    pub use crate::app::baseline_resolve::resolve_baseline_path;
}

/// Render a terminal-friendly colored diff output from a DiffOutcome.
pub fn render_terminal_diff(outcome: &DiffOutcome) -> String {
    use crate::app::{format_metric_with_statistic, format_pct, format_value};

    let mut out = String::new();

    for bench_outcome in &outcome.bench_outcomes {
        out.push_str(&format!("bench: {}\n", bench_outcome.bench_name));

        if bench_outcome.no_baseline {
            out.push_str("  (no baseline found, skipping comparison)\n\n");
            continue;
        }

        if let Some(compare) = &bench_outcome.compare_receipt {
            let verdict_label = match compare.verdict.status {
                VerdictStatus::Pass => "PASS",
                VerdictStatus::Warn => "WARN",
                VerdictStatus::Fail => "FAIL",
                VerdictStatus::Skip => "SKIP",
            };
            out.push_str(&format!("  verdict: {}\n", verdict_label));

            for (metric, delta) in &compare.deltas {
                let name = format_metric_with_statistic(*metric, delta.statistic);
                let baseline_str = format_value(*metric, delta.baseline);
                let current_str = format_value(*metric, delta.current);
                let pct_str = format_pct(delta.pct);
                let unit = metric.display_unit();

                let status_indicator = match delta.status {
                    perfgate_types::MetricStatus::Pass => " ",
                    perfgate_types::MetricStatus::Warn => "~",
                    perfgate_types::MetricStatus::Fail => "!",
                    perfgate_types::MetricStatus::Skip => "-",
                };

                out.push_str(&format!(
                    "  {status_indicator} {name}: {baseline_str} {unit} -> {current_str} {unit} ({pct_str})\n"
                ));
            }
        }

        out.push('\n');
    }

    out
}

/// Render the diff outcome as JSON.
pub fn render_json_diff(outcome: &DiffOutcome) -> anyhow::Result<String> {
    let mut entries = Vec::new();

    for bench_outcome in &outcome.bench_outcomes {
        let entry = serde_json::json!({
            "bench": bench_outcome.bench_name,
            "no_baseline": bench_outcome.no_baseline,
            "compare": bench_outcome.compare_receipt,
        });
        entries.push(entry);
    }

    let output = serde_json::json!({
        "config": outcome.config_path.display().to_string(),
        "exit_code": outcome.exit_code,
        "benchmarks": entries,
    });

    serde_json::to_string_pretty(&output).context("serialize diff output")
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    #[test]
    fn discover_config_finds_toml_in_current_dir() {
        let tmp = tempfile::tempdir().unwrap();
        let config_path = tmp.path().join("perfgate.toml");
        std::fs::write(&config_path, "[defaults]\n").unwrap();

        let found = discover_config(tmp.path());
        assert_eq!(found, Some(config_path));
    }

    #[test]
    fn discover_config_finds_toml_in_parent() {
        let tmp = tempfile::tempdir().unwrap();
        let config_path = tmp.path().join("perfgate.toml");
        std::fs::write(&config_path, "[defaults]\n").unwrap();

        let child = tmp.path().join("subdir");
        std::fs::create_dir_all(&child).unwrap();

        let found = discover_config(&child);
        assert_eq!(found, Some(config_path));
    }

    #[test]
    fn discover_config_prefers_toml_over_json() {
        let tmp = tempfile::tempdir().unwrap();
        let toml_path = tmp.path().join("perfgate.toml");
        let json_path = tmp.path().join("perfgate.json");
        std::fs::write(&toml_path, "[defaults]\n").unwrap();
        std::fs::write(&json_path, "{}").unwrap();

        let found = discover_config(tmp.path());
        assert_eq!(found, Some(toml_path));
    }

    #[test]
    fn discover_config_falls_back_to_json() {
        let tmp = tempfile::tempdir().unwrap();
        let json_path = tmp.path().join("perfgate.json");
        std::fs::write(&json_path, "{}").unwrap();

        let found = discover_config(tmp.path());
        assert_eq!(found, Some(json_path));
    }

    #[test]
    fn discover_config_returns_none_when_not_found() {
        let tmp = tempfile::tempdir().unwrap();
        let found = discover_config(tmp.path());
        assert!(found.is_none());
    }

    #[test]
    fn render_terminal_diff_no_baseline() {
        let outcome = DiffOutcome {
            config_path: PathBuf::from("perfgate.toml"),
            bench_outcomes: vec![BenchDiffOutcome {
                bench_name: "my-bench".to_string(),
                run_receipt: make_dummy_run_receipt(),
                compare_receipt: None,
                baseline_path: None,
                no_baseline: true,
            }],
            exit_code: 0,
        };

        let rendered = render_terminal_diff(&outcome);
        assert!(rendered.contains("my-bench"));
        assert!(rendered.contains("no baseline found"));
    }

    #[test]
    fn render_terminal_diff_with_comparison() {
        use perfgate_types::*;

        let mut deltas = BTreeMap::new();
        deltas.insert(
            Metric::WallMs,
            Delta {
                baseline: 100.0,
                current: 110.0,
                ratio: 1.10,
                pct: 0.10,
                regression: 0.10,
                statistic: MetricStatistic::Median,
                significance: None,
                cv: None,
                noise_threshold: None,
                status: MetricStatus::Pass,
            },
        );

        let mut budgets = BTreeMap::new();
        budgets.insert(Metric::WallMs, Budget::new(0.2, 0.18, Direction::Lower));

        let compare = CompareReceipt {
            schema: COMPARE_SCHEMA_V1.to_string(),
            tool: ToolInfo {
                name: "perfgate".into(),
                version: "0.1.0".into(),
            },
            bench: BenchMeta {
                name: "my-bench".into(),
                cwd: None,
                command: vec!["echo".into()],
                repeat: 2,
                warmup: 0,
                work_units: None,
                timeout_ms: None,
            },
            baseline_ref: CompareRef {
                path: None,
                run_id: None,
            },
            current_ref: CompareRef {
                path: None,
                run_id: None,
            },
            budgets,
            deltas,
            verdict: Verdict {
                status: VerdictStatus::Pass,
                counts: VerdictCounts {
                    pass: 1,
                    warn: 0,
                    fail: 0,
                    skip: 0,
                },
                reasons: vec![],
            },
        };

        let outcome = DiffOutcome {
            config_path: PathBuf::from("perfgate.toml"),
            bench_outcomes: vec![BenchDiffOutcome {
                bench_name: "my-bench".to_string(),
                run_receipt: make_dummy_run_receipt(),
                compare_receipt: Some(compare),
                baseline_path: Some(PathBuf::from("baselines/my-bench.json")),
                no_baseline: false,
            }],
            exit_code: 0,
        };

        let rendered = render_terminal_diff(&outcome);
        assert!(rendered.contains("my-bench"));
        assert!(rendered.contains("PASS"));
        assert!(rendered.contains("wall_ms"));
        assert!(rendered.contains("+10.00%"));
    }

    #[test]
    fn render_json_diff_produces_valid_json() {
        let outcome = DiffOutcome {
            config_path: PathBuf::from("perfgate.toml"),
            bench_outcomes: vec![BenchDiffOutcome {
                bench_name: "test".to_string(),
                run_receipt: make_dummy_run_receipt(),
                compare_receipt: None,
                baseline_path: None,
                no_baseline: true,
            }],
            exit_code: 0,
        };

        let json = render_json_diff(&outcome).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed["exit_code"], 0);
        assert_eq!(parsed["benchmarks"][0]["bench"], "test");
        assert_eq!(parsed["benchmarks"][0]["no_baseline"], true);
    }

    #[test]
    fn worst_verdict_returns_fail_when_any_fail() {
        use perfgate_types::*;

        let pass_compare = make_compare_with_verdict(VerdictStatus::Pass);
        let fail_compare = make_compare_with_verdict(VerdictStatus::Fail);

        let outcome = DiffOutcome {
            config_path: PathBuf::from("perfgate.toml"),
            bench_outcomes: vec![
                BenchDiffOutcome {
                    bench_name: "a".to_string(),
                    run_receipt: make_dummy_run_receipt(),
                    compare_receipt: Some(pass_compare),
                    baseline_path: None,
                    no_baseline: false,
                },
                BenchDiffOutcome {
                    bench_name: "b".to_string(),
                    run_receipt: make_dummy_run_receipt(),
                    compare_receipt: Some(fail_compare),
                    baseline_path: None,
                    no_baseline: false,
                },
            ],
            exit_code: 2,
        };

        assert_eq!(outcome.worst_verdict(), VerdictStatus::Fail);
    }

    #[test]
    fn worst_verdict_returns_pass_when_no_comparisons() {
        let outcome = DiffOutcome {
            config_path: PathBuf::from("perfgate.toml"),
            bench_outcomes: vec![BenchDiffOutcome {
                bench_name: "a".to_string(),
                run_receipt: make_dummy_run_receipt(),
                compare_receipt: None,
                baseline_path: None,
                no_baseline: true,
            }],
            exit_code: 0,
        };

        assert_eq!(outcome.worst_verdict(), VerdictStatus::Pass);
    }

    fn make_dummy_run_receipt() -> RunReceipt {
        use perfgate_types::*;

        RunReceipt {
            schema: RUN_SCHEMA_V1.to_string(),
            tool: ToolInfo {
                name: "perfgate".into(),
                version: "0.1.0".into(),
            },
            bench: BenchMeta {
                name: "test".into(),
                cwd: None,
                command: vec!["echo".into()],
                repeat: 2,
                warmup: 0,
                work_units: None,
                timeout_ms: None,
            },
            run: RunMeta {
                id: "test-id".into(),
                started_at: "2024-01-01T00:00:00Z".into(),
                ended_at: "2024-01-01T00:00:01Z".into(),
                host: HostInfo {
                    os: "linux".into(),
                    arch: "x86_64".into(),
                    cpu_count: None,
                    memory_bytes: None,
                    hostname_hash: None,
                },
            },
            samples: vec![],
            stats: Stats {
                wall_ms: U64Summary::new(100, 90, 110),
                cpu_ms: None,
                page_faults: None,
                ctx_switches: None,
                max_rss_kb: None,
                binary_bytes: None,
                throughput_per_s: None,
                io_read_bytes: None,
                io_write_bytes: None,
                energy_uj: None,
                network_packets: None,
            },
        }
    }

    fn make_compare_with_verdict(status: VerdictStatus) -> CompareReceipt {
        use perfgate_types::*;

        CompareReceipt {
            schema: COMPARE_SCHEMA_V1.to_string(),
            tool: ToolInfo {
                name: "perfgate".into(),
                version: "0.1.0".into(),
            },
            bench: BenchMeta {
                name: "test".into(),
                cwd: None,
                command: vec!["echo".into()],
                repeat: 2,
                warmup: 0,
                work_units: None,
                timeout_ms: None,
            },
            baseline_ref: CompareRef {
                path: None,
                run_id: None,
            },
            current_ref: CompareRef {
                path: None,
                run_id: None,
            },
            budgets: BTreeMap::new(),
            deltas: BTreeMap::new(),
            verdict: Verdict {
                status,
                counts: VerdictCounts {
                    pass: 0,
                    warn: 0,
                    fail: 0,
                    skip: 0,
                },
                reasons: vec![],
            },
        }
    }
}