perfgate-cli 0.17.0

CLI for perfgate performance budgets and baseline diffs
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
//! Integration tests for host mismatch policy (--host-mismatch flag)
//!
//! **Validates: Host mismatch detection and policy enforcement**
//!
//! Host mismatches are detected when:
//! - Different `os` or `arch`
//! - Significant difference in `cpu_count` (> 2x)
//! - Significant difference in `memory_bytes` (> 2x)
//! - Different `hostname_hash` (if both present)

mod common;
use common::perfgate_cmd;
use predicates::prelude::*;

use std::fs;

use tempfile::tempdir;

use common::fixtures_dir;

// ======================================================================
// Compare command tests with --host-mismatch policy
// ======================================================================

/// Test compare with --host-mismatch=warn (default) produces warning in output
/// when hosts have different hostnames.
///
/// Exit code should be 0 (pass) but warning should be printed to stderr.
#[test]
fn test_compare_host_mismatch_warn_policy_different_hostname() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("compare.json");

    let baseline = fixtures_dir().join("baseline_host_linux_x86.json");
    let current = fixtures_dir().join("current_host_different_hostname.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("compare")
        .arg("--baseline")
        .arg(&baseline)
        .arg("--current")
        .arg(&current)
        .arg("--host-mismatch")
        .arg("warn")
        .arg("--out")
        .arg(&output_path);

    // Should succeed (warn policy does not fail)
    cmd.assert()
        .success()
        .stderr(predicate::str::contains("warning: host mismatch"))
        .stderr(predicate::str::contains("hostname mismatch"));

    // Verify output file exists and is valid
    assert!(output_path.exists(), "output file should exist");
    let content = fs::read_to_string(&output_path).expect("failed to read output file");
    let receipt: serde_json::Value =
        serde_json::from_str(&content).expect("output should be valid JSON");
    assert_eq!(
        receipt["schema"].as_str(),
        Some("perfgate.compare.v1"),
        "schema should be 'perfgate.compare.v1'"
    );
}

/// Test compare with --host-mismatch=warn produces warning for OS mismatch.
#[test]
fn test_compare_host_mismatch_warn_policy_different_os() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("compare.json");

    let baseline = fixtures_dir().join("baseline_host_linux_x86.json");
    let current = fixtures_dir().join("current_host_different_os.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("compare")
        .arg("--baseline")
        .arg(&baseline)
        .arg("--current")
        .arg(&current)
        .arg("--host-mismatch")
        .arg("warn")
        .arg("--out")
        .arg(&output_path);

    cmd.assert()
        .success()
        .stderr(predicate::str::contains("warning: host mismatch"))
        .stderr(predicate::str::contains("OS mismatch"));
}

/// Test compare with --host-mismatch=warn produces warning for architecture mismatch.
#[test]
fn test_compare_host_mismatch_warn_policy_different_arch() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("compare.json");

    let baseline = fixtures_dir().join("baseline_host_linux_x86.json");
    let current = fixtures_dir().join("current_host_different_arch.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("compare")
        .arg("--baseline")
        .arg(&baseline)
        .arg("--current")
        .arg(&current)
        .arg("--host-mismatch")
        .arg("warn")
        .arg("--out")
        .arg(&output_path);

    cmd.assert()
        .success()
        .stderr(predicate::str::contains("warning: host mismatch"))
        .stderr(predicate::str::contains("architecture mismatch"));
}

/// Test compare with --host-mismatch=warn produces warning for CPU count mismatch (> 2x).
#[test]
fn test_compare_host_mismatch_warn_policy_different_cpu_count() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("compare.json");

    let baseline = fixtures_dir().join("baseline_host_linux_x86.json");
    let current = fixtures_dir().join("current_host_different_cpu_count.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("compare")
        .arg("--baseline")
        .arg(&baseline)
        .arg("--current")
        .arg(&current)
        .arg("--host-mismatch")
        .arg("warn")
        .arg("--out")
        .arg(&output_path);

    cmd.assert()
        .success()
        .stderr(predicate::str::contains("warning: host mismatch"))
        .stderr(predicate::str::contains("CPU count differs"));
}

/// Test compare with --host-mismatch=error fails when hosts differ.
///
/// Exit code should be 1 (tool error) when mismatch is detected.
#[test]
fn test_compare_host_mismatch_error_policy_fails_on_mismatch() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("compare.json");

    let baseline = fixtures_dir().join("baseline_host_linux_x86.json");
    let current = fixtures_dir().join("current_host_different_hostname.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("compare")
        .arg("--baseline")
        .arg(&baseline)
        .arg("--current")
        .arg(&current)
        .arg("--host-mismatch")
        .arg("error")
        .arg("--out")
        .arg(&output_path);

    // Should fail with exit code 1 (tool error)
    cmd.assert()
        .code(1)
        .stderr(predicate::str::contains("host mismatch detected"))
        .stderr(predicate::str::contains("hostname mismatch"));
}

/// Test compare with --host-mismatch=error fails with OS mismatch.
#[test]
fn test_compare_host_mismatch_error_policy_os_mismatch() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("compare.json");

    let baseline = fixtures_dir().join("baseline_host_linux_x86.json");
    let current = fixtures_dir().join("current_host_different_os.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("compare")
        .arg("--baseline")
        .arg(&baseline)
        .arg("--current")
        .arg(&current)
        .arg("--host-mismatch")
        .arg("error")
        .arg("--out")
        .arg(&output_path);

    cmd.assert()
        .code(1)
        .stderr(predicate::str::contains("host mismatch detected"))
        .stderr(predicate::str::contains("OS mismatch"));
}

/// Test compare with --host-mismatch=error fails with architecture mismatch.
#[test]
fn test_compare_host_mismatch_error_policy_arch_mismatch() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("compare.json");

    let baseline = fixtures_dir().join("baseline_host_linux_x86.json");
    let current = fixtures_dir().join("current_host_different_arch.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("compare")
        .arg("--baseline")
        .arg(&baseline)
        .arg("--current")
        .arg(&current)
        .arg("--host-mismatch")
        .arg("error")
        .arg("--out")
        .arg(&output_path);

    cmd.assert()
        .code(1)
        .stderr(predicate::str::contains("host mismatch detected"))
        .stderr(predicate::str::contains("architecture mismatch"));
}

/// Test compare with --host-mismatch=error fails with CPU count mismatch (> 2x threshold).
#[test]
fn test_compare_host_mismatch_error_policy_cpu_count_mismatch() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("compare.json");

    let baseline = fixtures_dir().join("baseline_host_linux_x86.json");
    let current = fixtures_dir().join("current_host_different_cpu_count.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("compare")
        .arg("--baseline")
        .arg(&baseline)
        .arg("--current")
        .arg(&current)
        .arg("--host-mismatch")
        .arg("error")
        .arg("--out")
        .arg(&output_path);

    cmd.assert()
        .code(1)
        .stderr(predicate::str::contains("host mismatch detected"))
        .stderr(predicate::str::contains("CPU count differs"));
}

/// Test compare with --host-mismatch=ignore does not produce warnings.
///
/// Exit code should be 0 (pass) and no warning should be printed.
#[test]
fn test_compare_host_mismatch_ignore_policy_no_warnings() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("compare.json");

    let baseline = fixtures_dir().join("baseline_host_linux_x86.json");
    let current = fixtures_dir().join("current_host_different_hostname.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("compare")
        .arg("--baseline")
        .arg(&baseline)
        .arg("--current")
        .arg(&current)
        .arg("--host-mismatch")
        .arg("ignore")
        .arg("--out")
        .arg(&output_path);

    // Should succeed without warnings
    cmd.assert()
        .success()
        .stderr(predicate::str::contains("host mismatch").not());

    // Verify output file exists
    assert!(output_path.exists(), "output file should exist");
}

/// Test compare with --host-mismatch=ignore suppresses all mismatch types.
#[test]
fn test_compare_host_mismatch_ignore_policy_multiple_mismatches() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("compare.json");

    let baseline = fixtures_dir().join("baseline_host_linux_x86.json");
    let current = fixtures_dir().join("current_host_multiple_mismatches.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("compare")
        .arg("--baseline")
        .arg(&baseline)
        .arg("--current")
        .arg(&current)
        .arg("--host-mismatch")
        .arg("ignore")
        .arg("--out")
        .arg(&output_path);

    // Should succeed without any warnings about host mismatch
    cmd.assert()
        .success()
        .stderr(predicate::str::contains("host mismatch").not())
        .stderr(predicate::str::contains("OS mismatch").not())
        .stderr(predicate::str::contains("architecture mismatch").not())
        .stderr(predicate::str::contains("CPU count differs").not())
        .stderr(predicate::str::contains("hostname mismatch").not());
}

/// Test compare with multiple host mismatches reports all reasons with warn policy.
#[test]
fn test_compare_host_mismatch_warn_policy_multiple_mismatches() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("compare.json");

    let baseline = fixtures_dir().join("baseline_host_linux_x86.json");
    let current = fixtures_dir().join("current_host_multiple_mismatches.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("compare")
        .arg("--baseline")
        .arg(&baseline)
        .arg("--current")
        .arg(&current)
        .arg("--host-mismatch")
        .arg("warn")
        .arg("--out")
        .arg(&output_path);

    // Should succeed but report all mismatches
    cmd.assert()
        .success()
        .stderr(predicate::str::contains("OS mismatch"))
        .stderr(predicate::str::contains("architecture mismatch"))
        .stderr(predicate::str::contains("CPU count differs"))
        .stderr(predicate::str::contains("hostname mismatch"));
}

/// Test compare with identical hosts does not produce warnings.
#[test]
fn test_compare_host_mismatch_no_warning_when_hosts_match() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("compare.json");

    // Use the standard baseline and current_pass which have matching hosts
    let baseline = fixtures_dir().join("baseline.json");
    let current = fixtures_dir().join("current_pass.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("compare")
        .arg("--baseline")
        .arg(&baseline)
        .arg("--current")
        .arg(&current)
        .arg("--host-mismatch")
        .arg("warn")
        .arg("--out")
        .arg(&output_path);

    // Should succeed without any host mismatch warnings
    cmd.assert()
        .success()
        .stderr(predicate::str::contains("host mismatch").not());
}

/// Test compare with default host-mismatch policy (warn) produces warnings.
#[test]
fn test_compare_host_mismatch_default_policy_is_warn() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("compare.json");

    let baseline = fixtures_dir().join("baseline_host_linux_x86.json");
    let current = fixtures_dir().join("current_host_different_hostname.json");

    // Note: not specifying --host-mismatch should default to warn
    let mut cmd = perfgate_cmd();
    cmd.arg("compare")
        .arg("--baseline")
        .arg(&baseline)
        .arg("--current")
        .arg(&current)
        .arg("--out")
        .arg(&output_path);

    // Should succeed with warning (default policy is warn)
    cmd.assert()
        .success()
        .stderr(predicate::str::contains("warning: host mismatch"));
}

// ======================================================================
// Check command tests with --host-mismatch policy
// ======================================================================

/// Returns a cross-platform command that exits successfully.
#[cfg(unix)]
fn success_command() -> Vec<&'static str> {
    vec!["true"]
}

#[cfg(windows)]
fn success_command() -> Vec<&'static str> {
    vec!["cmd", "/c", "exit", "0"]
}

/// Create a minimal config file with a single bench.
fn create_config_file(temp_dir: &std::path::Path, bench_name: &str) -> std::path::PathBuf {
    let config_path = temp_dir.join("perfgate.toml");
    let success_cmd = success_command();

    let cmd_str = success_cmd
        .iter()
        .map(|s| format!("\"{}\"", s))
        .collect::<Vec<_>>()
        .join(", ");

    let config_content = format!(
        r#"
[defaults]
repeat = 2
warmup = 0
threshold = 1000.0

[[bench]]
name = "{}"
command = [{}]
"#,
        bench_name, cmd_str
    );

    fs::write(&config_path, config_content).expect("Failed to write config file");
    config_path
}

/// Create a baseline receipt with specific host info for testing.
fn create_baseline_receipt_with_host(
    temp_dir: &std::path::Path,
    bench_name: &str,
    os: &str,
    arch: &str,
    cpu_count: u32,
    hostname_hash: &str,
) -> std::path::PathBuf {
    let baselines_dir = temp_dir.join("baselines");
    fs::create_dir_all(&baselines_dir).expect("Failed to create baselines dir");

    let baseline_path = baselines_dir.join(format!("{}.json", bench_name));

    let receipt = serde_json::json!({
        "schema": "perfgate.run.v1",
        "tool": {
            "name": "perfgate",
            "version": "0.1.0"
        },
        "run": {
            "id": "baseline-run-id",
            "started_at": "2024-01-01T00:00:00Z",
            "ended_at": "2024-01-01T00:01:00Z",
            "host": {
                "os": os,
                "arch": arch,
                "cpu_count": cpu_count,
                "memory_bytes": 17179869184_u64,
                "hostname_hash": hostname_hash
            }
        },
        "bench": {
            "name": bench_name,
            "command": ["echo", "hello"],
            "repeat": 2,
            "warmup": 0
        },
        "samples": [
            {"wall_ms": 10000, "exit_code": 0, "warmup": false, "timed_out": false},
            {"wall_ms": 10200, "exit_code": 0, "warmup": false, "timed_out": false}
        ],
        "stats": {
            "wall_ms": {
                "median": 10100,
                "min": 10000,
                "max": 10200
            }
        }
    });

    fs::write(
        &baseline_path,
        serde_json::to_string_pretty(&receipt).unwrap(),
    )
    .expect("Failed to write baseline");

    baseline_path
}

/// Test check command with --host-mismatch=warn produces warning when baseline host differs.
///
/// The current run's host will differ from the baseline (which has a specific hostname_hash).
#[test]
fn test_check_host_mismatch_warn_policy() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts");
    let config_path = create_config_file(temp_dir.path(), "host-warn-test");

    // Create baseline with specific host info (different from what will be detected at runtime)
    create_baseline_receipt_with_host(
        temp_dir.path(),
        "host-warn-test",
        "some-other-os", // This will differ from the current OS
        "x86_64",
        8,
        "baseline-hostname-hash",
    );

    let mut cmd = perfgate_cmd();
    cmd.current_dir(temp_dir.path())
        .arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("host-warn-test")
        .arg("--host-mismatch")
        .arg("warn")
        .arg("--out-dir")
        .arg(&out_dir);

    let output = cmd.output().expect("failed to execute check");

    // Should succeed (warn policy doesn't fail)
    assert!(
        output.status.success(),
        "check with --host-mismatch=warn should succeed: exit code {:?}, stderr: {}",
        output.status.code(),
        String::from_utf8_lossy(&output.stderr)
    );

    // Should have warning about host mismatch
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("host mismatch") || stderr.contains("OS mismatch"),
        "stderr should mention host mismatch: {}",
        stderr
    );

    // Artifacts should still be created
    assert!(out_dir.join("run.json").exists(), "run.json should exist");
    assert!(
        out_dir.join("compare.json").exists(),
        "compare.json should exist when baseline is present"
    );
}

/// Test check command with --host-mismatch=error fails when baseline host differs.
#[test]
fn test_check_host_mismatch_error_policy() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts");
    let config_path = create_config_file(temp_dir.path(), "host-error-test");

    // Create baseline with specific host info that will differ from current
    create_baseline_receipt_with_host(
        temp_dir.path(),
        "host-error-test",
        "some-other-os", // This will differ from the current OS
        "x86_64",
        8,
        "baseline-hostname-hash",
    );

    let mut cmd = perfgate_cmd();
    cmd.current_dir(temp_dir.path())
        .arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("host-error-test")
        .arg("--host-mismatch")
        .arg("error")
        .arg("--out-dir")
        .arg(&out_dir);

    let output = cmd.output().expect("failed to execute check");

    // Should fail with exit code 1 (tool error)
    assert!(
        !output.status.success(),
        "check with --host-mismatch=error should fail when hosts differ"
    );
    assert_eq!(
        output.status.code(),
        Some(1),
        "exit code should be 1 for tool error"
    );

    // Error should mention host mismatch
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("host mismatch"),
        "stderr should mention host mismatch: {}",
        stderr
    );
}

/// Test check command with --host-mismatch=ignore does not produce warnings.
#[test]
fn test_check_host_mismatch_ignore_policy() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts");
    let config_path = create_config_file(temp_dir.path(), "host-ignore-test");

    // Create baseline with specific host info that will differ from current
    create_baseline_receipt_with_host(
        temp_dir.path(),
        "host-ignore-test",
        "some-other-os", // This will differ from the current OS
        "x86_64",
        8,
        "baseline-hostname-hash",
    );

    let mut cmd = perfgate_cmd();
    cmd.current_dir(temp_dir.path())
        .arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("host-ignore-test")
        .arg("--host-mismatch")
        .arg("ignore")
        .arg("--out-dir")
        .arg(&out_dir);

    let output = cmd.output().expect("failed to execute check");

    // Should succeed without warnings
    assert!(
        output.status.success(),
        "check with --host-mismatch=ignore should succeed: exit code {:?}, stderr: {}",
        output.status.code(),
        String::from_utf8_lossy(&output.stderr)
    );

    // Should NOT have warning about host mismatch
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        !stderr.contains("host mismatch"),
        "stderr should NOT mention host mismatch with ignore policy: {}",
        stderr
    );

    // Artifacts should be created
    assert!(out_dir.join("run.json").exists(), "run.json should exist");
    assert!(
        out_dir.join("compare.json").exists(),
        "compare.json should exist"
    );
}

/// Test check command with explicit --baseline flag and --host-mismatch=error.
#[test]
fn test_check_explicit_baseline_host_mismatch_error() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts");
    let config_path = create_config_file(temp_dir.path(), "explicit-baseline-test");

    // Use a fixture file as the explicit baseline
    let baseline = fixtures_dir().join("baseline_host_linux_x86.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("explicit-baseline-test")
        .arg("--baseline")
        .arg(&baseline)
        .arg("--host-mismatch")
        .arg("error")
        .arg("--out-dir")
        .arg(&out_dir);

    let output = cmd.output().expect("failed to execute check");

    // Should fail if current host differs from the fixture's host info
    // The fixture has os=linux, arch=x86_64, hostname_hash=abc123def456
    // On most CI environments or dev machines, the hostname_hash will differ
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            stderr.contains("host mismatch"),
            "if failing, stderr should mention host mismatch: {}",
            stderr
        );
    }
    // Note: If running on a machine that happens to match, the test will pass
}

/// Test check command with explicit --baseline flag and --host-mismatch=warn.
#[test]
fn test_check_explicit_baseline_host_mismatch_warn() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts");
    let config_path = create_config_file(temp_dir.path(), "explicit-warn-test");

    // Use a fixture file as the explicit baseline
    let baseline = fixtures_dir().join("baseline_host_linux_x86.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("explicit-warn-test")
        .arg("--baseline")
        .arg(&baseline)
        .arg("--host-mismatch")
        .arg("warn")
        .arg("--out-dir")
        .arg(&out_dir);

    let output = cmd.output().expect("failed to execute check");

    // Should succeed (warn policy doesn't fail)
    assert!(
        output.status.success(),
        "check with --host-mismatch=warn should succeed: exit code {:?}, stderr: {}",
        output.status.code(),
        String::from_utf8_lossy(&output.stderr)
    );

    // Artifacts should be created
    assert!(out_dir.join("run.json").exists(), "run.json should exist");
}

// ======================================================================
// Invalid policy value tests
// ======================================================================

/// Test that invalid --host-mismatch policy value is rejected.
#[test]
fn test_compare_host_mismatch_invalid_policy() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("compare.json");

    let baseline = fixtures_dir().join("baseline.json");
    let current = fixtures_dir().join("current_pass.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("compare")
        .arg("--baseline")
        .arg(&baseline)
        .arg("--current")
        .arg(&current)
        .arg("--host-mismatch")
        .arg("invalid-policy")
        .arg("--out")
        .arg(&output_path);

    // Should fail with error about invalid policy
    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("invalid host mismatch policy"));
}

/// Test that invalid --host-mismatch policy value is rejected for check command.
#[test]
fn test_check_host_mismatch_invalid_policy() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts");
    let config_path = create_config_file(temp_dir.path(), "invalid-policy-test");

    let mut cmd = perfgate_cmd();
    cmd.arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("invalid-policy-test")
        .arg("--host-mismatch")
        .arg("invalid-policy")
        .arg("--out-dir")
        .arg(&out_dir);

    let output = cmd.output().expect("failed to execute check");

    assert!(
        !output.status.success(),
        "check with invalid policy should fail"
    );

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("invalid host mismatch policy"),
        "stderr should mention invalid policy: {}",
        stderr
    );
}