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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
//! Integration tests for `perfgate check --mode cockpit`
//!
//! **Validates: Cockpit integration mode**
//!
//! Tests:
//! - Schema conformance (output matches sensor.report.v1)
//! - Determinism (same input -> byte-identical output fields)
//! - Survivability (tool errors produce valid receipts)
//! - Artifact layout (correct file structure)
//! - Exit code contract (exit 0 unless catastrophic)

use serde_json::Value;
use std::fs;
use tempfile::tempdir;

mod common;
use common::perfgate_cmd;

/// 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"]
}

/// Returns a cross-platform command that is guaranteed to take some time.
#[cfg(unix)]
fn slow_command() -> Vec<&'static str> {
    vec!["sh", "-c", "sleep 0.05"]
}

#[cfg(windows)]
fn slow_command() -> Vec<&'static str> {
    vec!["powershell", "-Command", "Start-Sleep -Milliseconds 50"]
}

/// 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 = 0.20

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

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

/// Create a config file with a slow command.
fn create_slow_config_file(temp_dir: &std::path::Path, bench_name: &str) -> std::path::PathBuf {
    let config_path = temp_dir.join("perfgate_slow.toml");
    let slow_cmd = slow_command();

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

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

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

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

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

    let config = serde_json::json!({
        "defaults": {
            "repeat": 1,
            "warmup": 0,
            "threshold": 0.20
        },
        "bench": [{
            "name": bench_name,
            "command": cmd
        }]
    });

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

/// Create a baseline receipt for testing.
fn create_baseline_receipt(temp_dir: &std::path::Path, bench_name: &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": "linux",
                "arch": "x86_64"
            }
        },
        "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 cockpit mode produces sensor.report.v1 schema
#[test]
fn test_cockpit_mode_produces_sensor_report_schema() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");
    let config_path = create_config_file(temp_dir.path(), "test-bench");

    let mut cmd = perfgate_cmd();
    cmd.arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("test-bench")
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("cockpit");

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

    assert!(
        output.status.success(),
        "cockpit mode should exit 0: stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Check report.json exists at root
    let report_path = out_dir.join("report.json");
    assert!(report_path.exists(), "report.json should exist at root");

    // Parse and verify schema
    let report_content = fs::read_to_string(&report_path).expect("failed to read report");
    let report: Value = serde_json::from_str(&report_content).expect("failed to parse report");

    assert_eq!(
        report["schema"], "sensor.report.v1",
        "schema should be sensor.report.v1"
    );
}

/// Test cockpit mode artifact layout
#[test]
fn test_cockpit_mode_artifact_layout() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");
    let config_path = create_config_file(temp_dir.path(), "test-bench");
    let _baseline_path = create_baseline_receipt(temp_dir.path(), "test-bench");

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

    let output = cmd.output().expect("failed to execute check");
    assert!(
        output.status.success(),
        "cockpit mode should exit 0: stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Verify cockpit artifact layout:
    // artifacts/perfgate/
    // ├── report.json                    # sensor.report.v1 envelope
    // ├── comment.md                     # Markdown summary
    // └── extras/
    //     ├── perfgate.run.v1.json       # perfgate.run.v1
    //     ├── perfgate.compare.v1.json   # perfgate.compare.v1 (if baseline)
    //     └── perfgate.report.v1.json    # perfgate.report.v1 (native)

    assert!(out_dir.join("report.json").exists(), "report.json at root");
    assert!(out_dir.join("comment.md").exists(), "comment.md at root");
    assert!(out_dir.join("extras").is_dir(), "extras/ directory");
    assert!(
        out_dir.join("extras/perfgate.run.v1.json").exists(),
        "extras/perfgate.run.v1.json"
    );
    assert!(
        out_dir.join("extras/perfgate.compare.v1.json").exists(),
        "extras/perfgate.compare.v1.json (baseline present)"
    );
    assert!(
        out_dir.join("extras/perfgate.report.v1.json").exists(),
        "extras/perfgate.report.v1.json"
    );

    // Verify the root report.json has sensor.report.v1 schema
    let root_report: Value =
        serde_json::from_str(&fs::read_to_string(out_dir.join("report.json")).unwrap()).unwrap();
    assert_eq!(root_report["schema"], "sensor.report.v1");

    // Verify extras/perfgate.report.v1.json has perfgate.report.v1 schema
    let native_report: Value = serde_json::from_str(
        &fs::read_to_string(out_dir.join("extras/perfgate.report.v1.json")).unwrap(),
    )
    .unwrap();
    assert_eq!(native_report["report_type"], "perfgate.report.v1");
}

/// Test cockpit mode honors --emit-repair-context for passing checks.
#[test]
fn test_cockpit_mode_emit_repair_context_writes_extras_artifact() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");
    let config_path = create_config_file(temp_dir.path(), "test-bench");

    let mut cmd = perfgate_cmd();
    cmd.current_dir(temp_dir.path())
        .arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("test-bench")
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("cockpit")
        .arg("--emit-repair-context");

    let output = cmd.output().expect("failed to execute check");
    assert!(
        output.status.success(),
        "cockpit mode should exit 0: stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    assert!(
        out_dir.join("extras/repair_context.json").exists(),
        "extras/repair_context.json should exist when --emit-repair-context is set"
    );
}

/// Test cockpit mode exits 0 even on verdict fail
#[test]
fn test_cockpit_mode_exits_zero_on_fail() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");
    let config_path = create_slow_config_file(temp_dir.path(), "test-bench");

    // Create a baseline with very low wall_ms to trigger a regression
    let baselines_dir = temp_dir.path().join("baselines");
    fs::create_dir_all(&baselines_dir).expect("Failed to create baselines dir");
    let baseline_path = baselines_dir.join("test-bench.json");

    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": "linux", "arch": "x86_64" }
        },
        "bench": {
            "name": "test-bench",
            "command": ["echo", "hello"],
            "repeat": 2,
            "warmup": 0
        },
        "samples": [
            {"wall_ms": 1, "exit_code": 0, "warmup": false, "timed_out": false},
            {"wall_ms": 1, "exit_code": 0, "warmup": false, "timed_out": false}
        ],
        "stats": {
            "wall_ms": { "median": 1, "min": 1, "max": 1 }
        }
    });

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

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

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

    // In cockpit mode, should still exit 0 even if verdict is fail
    assert!(
        output.status.success(),
        "cockpit mode should exit 0 even on fail: exit code {:?}, stderr: {}",
        output.status.code(),
        String::from_utf8_lossy(&output.stderr)
    );

    // But the report should contain the fail verdict
    let report_path = out_dir.join("report.json");
    let report: Value =
        serde_json::from_str(&fs::read_to_string(&report_path).expect("read report"))
            .expect("parse report");

    // The verdict status should be fail (actual runs will be much slower than 1ms baseline)
    let verdict_status = report["verdict"]["status"].as_str().unwrap();
    assert!(
        verdict_status == "fail" || verdict_status == "warn",
        "verdict should be fail or warn, got: {}",
        verdict_status
    );
}

/// Test cockpit mode report structure completeness
#[test]
fn test_cockpit_mode_report_structure() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");
    let config_path = create_config_file(temp_dir.path(), "test-bench");
    let _baseline_path = create_baseline_receipt(temp_dir.path(), "test-bench");

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

    let output = cmd.output().expect("failed to execute check");
    assert!(output.status.success());

    let report_path = out_dir.join("report.json");
    let report: Value =
        serde_json::from_str(&fs::read_to_string(&report_path).expect("read report"))
            .expect("parse report");

    // Verify required fields exist
    assert!(report.get("schema").is_some(), "schema field missing");
    assert!(report.get("tool").is_some(), "tool field missing");
    assert!(report.get("run").is_some(), "run field missing");
    assert!(report.get("verdict").is_some(), "verdict field missing");
    assert!(report.get("findings").is_some(), "findings field missing");
    assert!(report.get("data").is_some(), "data field missing");

    // Verify tool info
    assert_eq!(report["tool"]["name"], "perfgate");

    // Verify run metadata
    let run = &report["run"];
    assert!(run.get("started_at").is_some(), "started_at missing");
    assert!(run.get("ended_at").is_some(), "ended_at missing");
    assert!(run.get("duration_ms").is_some(), "duration_ms missing");
    assert!(run.get("capabilities").is_some(), "capabilities missing");

    // Verify capabilities (baseline should be available since we created one)
    assert_eq!(run["capabilities"]["baseline"]["status"], "available");

    // Verify verdict structure
    let verdict = &report["verdict"];
    assert!(verdict.get("status").is_some(), "verdict.status missing");
    assert!(verdict.get("counts").is_some(), "verdict.counts missing");
    assert!(verdict.get("reasons").is_some(), "verdict.reasons missing");

    // Verify counts use cockpit vocabulary (info/warn/error not pass/warn/fail)
    let counts = &verdict["counts"];
    assert!(counts.get("info").is_some(), "counts.info missing");
    assert!(counts.get("warn").is_some(), "counts.warn missing");
    assert!(counts.get("error").is_some(), "counts.error missing");

    // Verify data section: has summary, no compare key
    let data = &report["data"];
    assert!(data.get("summary").is_some(), "data.summary missing");
    assert!(
        data.get("compare").is_none(),
        "data should not have compare key"
    );
}

/// Test cockpit mode no baseline shows unavailable capability
#[test]
fn test_cockpit_mode_no_baseline_capability() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");
    let config_path = create_config_file(temp_dir.path(), "no-baseline-bench");
    // Don't create a baseline

    let mut cmd = perfgate_cmd();
    cmd.current_dir(temp_dir.path())
        .arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("no-baseline-bench")
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("cockpit");

    let output = cmd.output().expect("failed to execute check");
    assert!(
        output.status.success(),
        "cockpit mode should exit 0: stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let report_path = out_dir.join("report.json");
    let report: Value =
        serde_json::from_str(&fs::read_to_string(&report_path).expect("read report"))
            .expect("parse report");

    // Baseline capability should be unavailable
    assert_eq!(
        report["run"]["capabilities"]["baseline"]["status"],
        "unavailable"
    );

    // Reason should be the normalized token
    let reason = report["run"]["capabilities"]["baseline"]["reason"]
        .as_str()
        .unwrap_or("");
    assert_eq!(
        reason, "no_baseline",
        "reason should be 'no_baseline' token, got: {}",
        reason
    );
}

/// Test cockpit mode handles config errors gracefully
#[test]
fn test_cockpit_mode_config_error_produces_report() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");

    // Create an invalid config file
    let config_path = temp_dir.path().join("invalid.toml");
    fs::write(&config_path, "this is not valid toml {{{").expect("write invalid config");

    let mut cmd = perfgate_cmd();
    cmd.arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("test-bench")
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("cockpit");

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

    // Should still exit 0 (error recorded in report)
    assert!(
        output.status.success(),
        "cockpit mode should exit 0 on config error: exit code {:?}, stderr: {}",
        output.status.code(),
        String::from_utf8_lossy(&output.stderr)
    );

    // Report should exist with error
    let report_path = out_dir.join("report.json");
    assert!(
        report_path.exists(),
        "report.json should exist even on error"
    );

    let report: Value =
        serde_json::from_str(&fs::read_to_string(&report_path).expect("read report"))
            .expect("parse report");

    assert_eq!(report["schema"], "sensor.report.v1");
    assert_eq!(report["verdict"]["status"], "fail");
    assert_eq!(report["verdict"]["reasons"][0], "tool_error");

    // Should have error finding with tool.runtime check_id
    let findings = report["findings"].as_array().expect("findings array");
    assert!(!findings.is_empty(), "should have at least one finding");
    assert_eq!(findings[0]["severity"], "error");
    assert_eq!(findings[0]["check_id"], "tool.runtime");
    assert_eq!(findings[0]["code"], "runtime_error");
    // Finding should have structured data with stage and error_kind
    let finding_data = &findings[0]["data"];
    assert!(
        finding_data.get("stage").is_some(),
        "finding should have stage"
    );
    assert!(
        finding_data.get("error_kind").is_some(),
        "finding should have error_kind"
    );
}

/// Test cockpit mode can parse JSON config files
#[test]
fn test_cockpit_mode_json_config_parsing() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");
    let config_path = create_json_config_file(temp_dir.path(), "json-bench");

    let mut cmd = perfgate_cmd();
    cmd.arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("json-bench")
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("cockpit");

    let output = cmd.output().expect("failed to execute check");
    assert!(
        output.status.success(),
        "cockpit mode should succeed with JSON config: stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        out_dir.join("report.json").exists(),
        "report.json should exist"
    );
}

/// Test cockpit mode reports an error when extras directory cannot be created
#[test]
fn test_cockpit_mode_extras_dir_creation_error() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");
    let config_path = create_config_file(temp_dir.path(), "extras-error");

    fs::create_dir_all(&out_dir).expect("create out_dir");
    // Create a file where extras directory should be
    fs::write(out_dir.join("extras"), "not a dir").expect("write extras file");

    let mut cmd = perfgate_cmd();
    cmd.arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("extras-error")
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("cockpit");

    let output = cmd.output().expect("failed to execute check");
    assert!(
        output.status.success(),
        "cockpit mode should exit 0 on extras dir error: stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let report_path = out_dir.join("report.json");
    assert!(report_path.exists(), "report.json should exist");
}

/// Test cockpit mode fails when it cannot write the error report
#[test]
fn test_cockpit_mode_catastrophic_report_write_failure() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("not-a-dir");
    fs::write(&out_dir, "not a directory").expect("write out_dir file");

    let missing_config = temp_dir.path().join("missing.toml");

    let mut cmd = perfgate_cmd();
    cmd.arg("check")
        .arg("--config")
        .arg(&missing_config)
        .arg("--bench")
        .arg("bench")
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("cockpit");

    let output = cmd.output().expect("failed to execute check");
    assert!(
        !output.status.success(),
        "cockpit mode should fail when report write fails"
    );
    assert_eq!(output.status.code(), Some(1));
}

/// Test standard mode still works (not affected by cockpit changes)
#[test]
fn test_standard_mode_still_works() {
    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(), "test-bench");

    let mut cmd = perfgate_cmd();
    cmd.arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("test-bench")
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("standard"); // Explicit standard mode

    let output = cmd.output().expect("failed to execute check");
    assert!(output.status.success());

    // Standard mode writes perfgate.report.v1 directly to report.json
    let report_path = out_dir.join("report.json");
    assert!(report_path.exists());

    let report: Value =
        serde_json::from_str(&fs::read_to_string(&report_path).expect("read report"))
            .expect("parse report");

    // Standard mode should NOT produce sensor.report.v1
    assert_eq!(
        report["report_type"], "perfgate.report.v1",
        "standard mode should produce perfgate.report.v1"
    );
}

/// Test default mode is standard (backward compatibility)
#[test]
fn test_default_mode_is_standard() {
    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(), "test-bench");

    let mut cmd = perfgate_cmd();
    cmd.arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("test-bench")
        .arg("--out-dir")
        .arg(&out_dir);
    // No --mode argument - should default to standard

    let output = cmd.output().expect("failed to execute check");
    assert!(output.status.success());

    let report_path = out_dir.join("report.json");
    let report: Value =
        serde_json::from_str(&fs::read_to_string(&report_path).expect("read report"))
            .expect("parse report");

    // Default should be standard mode (perfgate.report.v1)
    assert_eq!(
        report["report_type"], "perfgate.report.v1",
        "default mode should produce perfgate.report.v1"
    );
}

/// Test cockpit mode with missing bench produces error report
#[test]
fn test_cockpit_mode_missing_bench_produces_error_report() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");
    let config_path = create_config_file(temp_dir.path(), "real-bench");

    let mut cmd = perfgate_cmd();
    cmd.arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("nonexistent-bench") // Not in config
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("cockpit");

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

    // Should exit 0 in cockpit mode (error recorded in report)
    assert!(
        output.status.success(),
        "cockpit mode should exit 0 on missing bench: stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Report should exist with error
    let report_path = out_dir.join("report.json");
    assert!(report_path.exists(), "report.json should exist");

    let report: Value =
        serde_json::from_str(&fs::read_to_string(&report_path).expect("read report"))
            .expect("parse report");

    assert_eq!(report["schema"], "sensor.report.v1");
    assert_eq!(report["verdict"]["status"], "fail");
}

/// Test cockpit mode rejects path-traversal bench names with error report
#[test]
fn test_cockpit_mode_rejects_path_traversal_bench_name() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");

    // Config with a path-traversal bench name
    let config_path = temp_dir.path().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 = 0.20

[[bench]]
name = "../evil"
command = [{}]
"#,
        cmd_str
    );
    fs::write(&config_path, config_content).expect("write config");

    let mut cmd = perfgate_cmd();
    cmd.arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("../evil")
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("cockpit");

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

    // Cockpit mode: exit 0 with error recorded in report
    assert!(
        output.status.success(),
        "cockpit mode should exit 0 on validation error: exit code {:?}, stderr: {}",
        output.status.code(),
        String::from_utf8_lossy(&output.stderr)
    );

    let report_path = out_dir.join("report.json");
    assert!(
        report_path.exists(),
        "report.json should exist even on validation error"
    );

    let report: Value =
        serde_json::from_str(&fs::read_to_string(&report_path).expect("read report"))
            .expect("parse report");

    assert_eq!(report["schema"], "sensor.report.v1");
    assert_eq!(report["verdict"]["status"], "fail");
    assert_eq!(report["verdict"]["reasons"][0], "tool_error");

    // Should have error finding with config_parse stage
    let findings = report["findings"].as_array().expect("findings array");
    assert!(!findings.is_empty(), "should have at least one finding");
    assert_eq!(findings[0]["check_id"], "tool.runtime");
    assert_eq!(findings[0]["code"], "runtime_error");
    let finding_data = &findings[0]["data"];
    assert_eq!(finding_data["stage"], "config_parse");
}

// ======================================================================
// Multi-bench cockpit integration tests
// ======================================================================

/// Create a config file with multiple benches for cockpit tests.
fn create_multi_bench_config(
    temp_dir: &std::path::Path,
    bench_names: &[&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 mut config_content = String::from(
        r#"
[defaults]
repeat = 2
warmup = 0
threshold = 0.20
"#,
    );

    for name in bench_names {
        config_content.push_str(&format!(
            r#"
[[bench]]
name = "{}"
command = [{}]
"#,
            name, cmd_str
        ));
    }

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

/// Create a config file with multiple slow benches.
fn create_slow_multi_bench_config(
    temp_dir: &std::path::Path,
    bench_names: &[&str],
) -> std::path::PathBuf {
    let config_path = temp_dir.join("perfgate_slow_multi.toml");
    let slow_cmd = slow_command();

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

    let mut config_content = String::from(
        r#"
[defaults]
repeat = 2
warmup = 0
threshold = 0.20
"#,
    );

    for name in bench_names {
        config_content.push_str(&format!(
            r#"
[[bench]]
name = "{}"
command = [{}]
"#,
            name, cmd_str
        ));
    }

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

/// Load the vendored schema validator.
fn load_vendored_schema_validator() -> jsonschema::Validator {
    let schema_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("../../contracts/schemas/sensor.report.v1.schema.json");
    let schema_content = fs::read_to_string(&schema_path).expect("read schema");
    let schema_value: Value = serde_json::from_str(&schema_content).expect("parse schema");
    jsonschema::validator_for(&schema_value).expect("compile schema")
}

/// Run perfgate check --all --mode cockpit and return the parsed report.json.
fn run_cockpit_multi_bench(
    temp_dir: &tempfile::TempDir,
    bench_names: &[&str],
    create_baselines: bool,
) -> (Value, std::path::PathBuf) {
    let out_dir = temp_dir.path().join("artifacts/perfgate");
    let config_path = create_multi_bench_config(temp_dir.path(), bench_names);

    if create_baselines {
        for name in bench_names {
            create_baseline_receipt(temp_dir.path(), name);
        }
    }

    let mut cmd = perfgate_cmd();
    cmd.current_dir(temp_dir.path())
        .arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--all")
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("cockpit");

    let output = cmd.output().expect("failed to execute check");
    assert!(
        output.status.success(),
        "cockpit multi-bench should exit 0: stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let report_path = out_dir.join("report.json");
    let content = fs::read_to_string(&report_path).expect("read report.json");
    let report: Value = serde_json::from_str(&content).expect("parse report.json");

    (report, out_dir)
}

/// Test multi-bench cockpit artifact layout (no baselines).
#[test]
fn test_cockpit_multi_bench_artifact_layout() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let (_report, out_dir) = run_cockpit_multi_bench(&temp_dir, &["bench-a", "bench-b"], false);

    // Root artifacts
    assert!(out_dir.join("report.json").exists(), "report.json at root");
    assert!(out_dir.join("comment.md").exists(), "comment.md at root");

    // Per-bench subdirectories under extras/
    for name in &["bench-a", "bench-b"] {
        let prefix = out_dir.join("extras").join(name);
        assert!(prefix.is_dir(), "extras/{} directory", name);
        assert!(
            prefix.join("perfgate.run.v1.json").exists(),
            "extras/{}/perfgate.run.v1.json",
            name
        );
        assert!(
            prefix.join("perfgate.report.v1.json").exists(),
            "extras/{}/perfgate.report.v1.json",
            name
        );
        // No baseline → no compare file
        assert!(
            !prefix.join("perfgate.compare.v1.json").exists(),
            "extras/{}/perfgate.compare.v1.json should NOT exist without baseline",
            name
        );
    }
}

/// Test multi-bench cockpit artifact layout with baselines.
#[test]
fn test_cockpit_multi_bench_artifact_layout_with_baselines() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let (_report, out_dir) = run_cockpit_multi_bench(&temp_dir, &["bench-a", "bench-b"], true);

    for name in &["bench-a", "bench-b"] {
        let prefix = out_dir.join("extras").join(name);
        assert!(
            prefix.join("perfgate.compare.v1.json").exists(),
            "extras/{}/perfgate.compare.v1.json should exist with baseline",
            name
        );
    }
}

/// Test multi-bench cockpit output validates against vendored schema.
#[test]
fn test_cockpit_multi_bench_schema_validation() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let (report, _out_dir) = run_cockpit_multi_bench(&temp_dir, &["bench-a", "bench-b"], false);

    let validator = load_vendored_schema_validator();
    let errors: Vec<_> = validator.iter_errors(&report).collect();
    assert!(
        errors.is_empty(),
        "multi-bench report failed schema validation:\n{}",
        errors
            .iter()
            .map(|e| format!("  - {}", e))
            .collect::<Vec<_>>()
            .join("\n")
    );
}

/// Test multi-bench verdict is worst-of across benches.
#[test]
fn test_cockpit_multi_bench_verdict_worst_wins() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");
    let config_path = create_slow_multi_bench_config(temp_dir.path(), &["bench-a", "bench-b"]);

    // Create a baseline with very low wall_ms for bench-a to trigger fail
    let baselines_dir = temp_dir.path().join("baselines");
    fs::create_dir_all(&baselines_dir).expect("create baselines dir");

    let fast_baseline = serde_json::json!({
        "schema": "perfgate.run.v1",
        "tool": { "name": "perfgate", "version": "0.1.0" },
        "run": {
            "id": "baseline-id",
            "started_at": "2024-01-01T00:00:00Z",
            "ended_at": "2024-01-01T00:01:00Z",
            "host": { "os": "linux", "arch": "x86_64" }
        },
        "bench": {
            "name": "bench-a",
            "command": ["echo", "hello"],
            "repeat": 2,
            "warmup": 0
        },
        "samples": [
            {"wall_ms": 1, "exit_code": 0, "warmup": false, "timed_out": false},
            {"wall_ms": 1, "exit_code": 0, "warmup": false, "timed_out": false}
        ],
        "stats": { "wall_ms": { "median": 1, "min": 1, "max": 1 } }
    });
    fs::write(
        baselines_dir.join("bench-a.json"),
        serde_json::to_string_pretty(&fast_baseline).unwrap(),
    )
    .expect("write bench-a baseline");
    // No baseline for bench-b

    let mut cmd = perfgate_cmd();
    cmd.current_dir(temp_dir.path())
        .arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--all")
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("cockpit");

    let output = cmd.output().expect("failed to execute check");
    assert!(output.status.success());

    let report: Value =
        serde_json::from_str(&fs::read_to_string(out_dir.join("report.json")).unwrap()).unwrap();

    let verdict_status = report["verdict"]["status"].as_str().unwrap();
    assert!(
        verdict_status == "fail" || verdict_status == "warn",
        "aggregated verdict should be fail or warn (worst-of), got: {}",
        verdict_status
    );
}

/// Test multi-bench counts are summed across benches.
#[test]
fn test_cockpit_multi_bench_counts_summed() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let (report, _out_dir) = run_cockpit_multi_bench(&temp_dir, &["bench-a", "bench-b"], false);

    // Both benches without baseline: each produces warn=1 (no_baseline)
    let counts = &report["verdict"]["counts"];
    let warn = counts["warn"].as_u64().unwrap();
    let info = counts["info"].as_u64().unwrap();
    let error = counts["error"].as_u64().unwrap();

    // Two benches, each with 1 warn finding → combined should have 2 warn
    assert_eq!(warn, 2, "warn counts should be summed");
    assert_eq!(info, 0, "info counts should be summed");
    assert_eq!(error, 0, "error counts should be summed");

    // Total in data.summary should match
    let summary = &report["data"]["summary"];
    assert_eq!(
        summary["total_count"].as_u64().unwrap(),
        info + warn + error,
        "total_count should be sum of counts"
    );
}

/// Test cockpit mode writes GitHub outputs when requested.
#[test]
fn test_cockpit_mode_output_github_writes_outputs() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");
    let config_path = create_config_file(temp_dir.path(), "gh-cockpit-bench");
    let github_output = temp_dir.path().join("github_output.txt");

    let mut cmd = perfgate_cmd();
    cmd.current_dir(temp_dir.path())
        .arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("gh-cockpit-bench")
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("cockpit")
        .arg("--output-github")
        .env("GITHUB_OUTPUT", &github_output);

    let output = cmd.output().expect("failed to execute check");
    assert!(
        output.status.success(),
        "cockpit mode should exit 0: stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let content = fs::read_to_string(&github_output).expect("read GITHUB_OUTPUT");
    assert!(content.contains("verdict="));
    assert!(content.contains("pass_count="));
    assert!(content.contains("warn_count="));
    assert!(content.contains("fail_count="));
    assert!(content.contains("bench_count=1"));
}

/// Test cockpit mode applies --md-template to generated comment.md.
#[test]
fn test_cockpit_mode_md_template_customizes_comment() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");
    let config_path = create_config_file(temp_dir.path(), "template-bench");
    let _baseline_path = create_baseline_receipt(temp_dir.path(), "template-bench");
    let template_path = temp_dir.path().join("comment.hbs");

    fs::write(
        &template_path,
        r#"bench={{bench.name}}
{{#each rows}}metric={{metric}} status={{status}}
{{/each}}
"#,
    )
    .expect("write template");

    let mut cmd = perfgate_cmd();
    cmd.current_dir(temp_dir.path())
        .arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("template-bench")
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("cockpit")
        .arg("--md-template")
        .arg(&template_path);

    let output = cmd.output().expect("failed to execute check");
    assert!(
        output.status.success(),
        "cockpit mode should succeed: stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let content = fs::read_to_string(out_dir.join("comment.md")).expect("read comment.md");
    assert!(content.contains("bench=template-bench"));
    assert!(content.contains("metric=wall_ms"));
}

/// Test multi-bench findings are prefixed with bench name and have bench_name in data.
#[test]
fn test_cockpit_multi_bench_findings_prefixed() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let (report, _out_dir) = run_cockpit_multi_bench(&temp_dir, &["bench-a", "bench-b"], false);

    let findings = report["findings"].as_array().expect("findings array");
    assert!(findings.len() >= 2, "should have at least 2 findings");

    let has_bench_a_prefix = findings
        .iter()
        .any(|f| f["message"].as_str().unwrap_or("").starts_with("[bench-a]"));
    let has_bench_b_prefix = findings
        .iter()
        .any(|f| f["message"].as_str().unwrap_or("").starts_with("[bench-b]"));
    assert!(has_bench_a_prefix, "should have [bench-a] prefix");
    assert!(has_bench_b_prefix, "should have [bench-b] prefix");

    // Finding data should include bench_name
    for finding in findings {
        let data = finding.get("data");
        if let Some(data) = data {
            assert!(
                data.get("bench_name").is_some(),
                "finding data should have bench_name"
            );
        }
    }
}

/// Test multi-bench fingerprints are unique and 64-char hex.
#[test]
fn test_cockpit_multi_bench_fingerprints_unique() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let (report, _out_dir) = run_cockpit_multi_bench(&temp_dir, &["bench-a", "bench-b"], false);

    let findings = report["findings"].as_array().expect("findings array");
    let mut fingerprints: Vec<&str> = Vec::new();

    for finding in findings {
        let fp = finding["fingerprint"]
            .as_str()
            .expect("finding should have fingerprint");
        assert_eq!(fp.len(), 64, "fingerprint should be 64-char hex: {}", fp);
        assert!(
            fp.chars()
                .all(|c| c.is_ascii_hexdigit() && !c.is_ascii_uppercase()),
            "fingerprint should be lowercase hex: {}",
            fp
        );
        assert!(
            !fingerprints.contains(&fp),
            "fingerprints should be unique: {} seen twice",
            fp
        );
        fingerprints.push(fp);
    }
}

/// Test multi-bench reasons: no_baseline appears exactly once.
#[test]
fn test_cockpit_multi_bench_reasons_deduped() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let (report, _out_dir) = run_cockpit_multi_bench(&temp_dir, &["bench-a", "bench-b"], false);

    let reasons = report["verdict"]["reasons"]
        .as_array()
        .expect("reasons array");
    let no_baseline_count = reasons
        .iter()
        .filter(|r| r.as_str() == Some("no_baseline"))
        .count();
    assert_eq!(
        no_baseline_count, 1,
        "no_baseline should appear exactly once in reasons"
    );
}

/// Test multi-bench baseline capability: available when ALL have baselines.
#[test]
fn test_cockpit_multi_bench_baseline_all_available() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let (report, _out_dir) = run_cockpit_multi_bench(&temp_dir, &["bench-a", "bench-b"], true);

    assert_eq!(
        report["run"]["capabilities"]["baseline"]["status"], "available",
        "all baselines → status = available"
    );
}

/// Test multi-bench baseline capability: unavailable when some lack baselines.
#[test]
fn test_cockpit_multi_bench_baseline_partial() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");
    let config_path = create_multi_bench_config(temp_dir.path(), &["bench-a", "bench-b"]);

    // Only create baseline for bench-a
    create_baseline_receipt(temp_dir.path(), "bench-a");

    let mut cmd = perfgate_cmd();
    cmd.current_dir(temp_dir.path())
        .arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--all")
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("cockpit");

    let output = cmd.output().expect("failed to execute check");
    assert!(output.status.success());

    let report: Value =
        serde_json::from_str(&fs::read_to_string(out_dir.join("report.json")).unwrap()).unwrap();

    assert_eq!(
        report["run"]["capabilities"]["baseline"]["status"], "unavailable",
        "partial baselines → status = unavailable"
    );
    // reason should be null when some have baselines
    assert!(
        report["run"]["capabilities"]["baseline"]["reason"].is_null(),
        "partial baselines → reason = null"
    );
}

/// Test multi-bench baseline capability: unavailable with no_baseline reason when none.
#[test]
fn test_cockpit_multi_bench_baseline_none() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let (report, _out_dir) = run_cockpit_multi_bench(&temp_dir, &["bench-a", "bench-b"], false);

    assert_eq!(
        report["run"]["capabilities"]["baseline"]["status"],
        "unavailable"
    );
    assert_eq!(
        report["run"]["capabilities"]["baseline"]["reason"],
        "no_baseline"
    );
}

/// Test multi-bench cockpit exits 0 even on aggregated fail.
#[test]
fn test_cockpit_multi_bench_exits_zero_on_fail() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");
    let config_path = create_multi_bench_config(temp_dir.path(), &["bench-a", "bench-b"]);

    // Create fast baseline for both to trigger fail
    let baselines_dir = temp_dir.path().join("baselines");
    fs::create_dir_all(&baselines_dir).expect("create baselines dir");
    for name in &["bench-a", "bench-b"] {
        let baseline = serde_json::json!({
            "schema": "perfgate.run.v1",
            "tool": { "name": "perfgate", "version": "0.1.0" },
            "run": {
                "id": "baseline-id",
                "started_at": "2024-01-01T00:00:00Z",
                "ended_at": "2024-01-01T00:01:00Z",
                "host": { "os": "linux", "arch": "x86_64" }
            },
            "bench": {
                "name": name,
                "command": ["echo", "hello"],
                "repeat": 2,
                "warmup": 0
            },
            "samples": [
                {"wall_ms": 1, "exit_code": 0, "warmup": false, "timed_out": false},
                {"wall_ms": 1, "exit_code": 0, "warmup": false, "timed_out": false}
            ],
            "stats": { "wall_ms": { "median": 1, "min": 1, "max": 1 } }
        });
        fs::write(
            baselines_dir.join(format!("{}.json", name)),
            serde_json::to_string_pretty(&baseline).unwrap(),
        )
        .expect("write baseline");
    }

    let mut cmd = perfgate_cmd();
    cmd.current_dir(temp_dir.path())
        .arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--all")
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("cockpit");

    let output = cmd.output().expect("failed to execute check");
    assert!(
        output.status.success(),
        "cockpit multi-bench should exit 0 even on fail: exit {:?}, stderr: {}",
        output.status.code(),
        String::from_utf8_lossy(&output.stderr)
    );
}

/// Test multi-bench comment.md contains both bench names and --- separator.
#[test]
fn test_cockpit_multi_bench_comment_md_combined() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let (_report, out_dir) = run_cockpit_multi_bench(&temp_dir, &["bench-a", "bench-b"], false);

    let md = fs::read_to_string(out_dir.join("comment.md")).expect("read comment.md");
    assert!(md.contains("bench-a"), "markdown should contain bench-a");
    assert!(md.contains("bench-b"), "markdown should contain bench-b");
    // Separator format is a perfgate-private detail; the unit test in
    // sensor_report.rs covers the exact `\n---\n\n` contract.
}

/// Test multi-bench artifacts are sorted by (type, path).
#[test]
fn test_cockpit_multi_bench_artifacts_sorted() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let (report, _out_dir) = run_cockpit_multi_bench(&temp_dir, &["bench-a", "bench-b"], true);

    let artifacts = report["artifacts"].as_array().expect("artifacts array");
    for window in artifacts.windows(2) {
        let a_type = window[0]["type"].as_str().unwrap();
        let a_path = window[0]["path"].as_str().unwrap();
        let b_type = window[1]["type"].as_str().unwrap();
        let b_path = window[1]["path"].as_str().unwrap();
        assert!(
            (a_type, a_path) <= (b_type, b_path),
            "artifacts not sorted: ({}, {}) > ({}, {})",
            a_type,
            a_path,
            b_type,
            b_path
        );
    }
}

/// Test multi-bench empty config produces error sensor report.
#[test]
fn test_cockpit_multi_bench_empty_config_error() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");

    // Create a config with no benches
    let config_path = temp_dir.path().join("perfgate.toml");
    fs::write(
        &config_path,
        r#"
[defaults]
repeat = 2
"#,
    )
    .expect("write config");

    let mut cmd = perfgate_cmd();
    cmd.current_dir(temp_dir.path())
        .arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--all")
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("cockpit");

    let output = cmd.output().expect("failed to execute check");
    assert!(
        output.status.success(),
        "cockpit should exit 0 even on empty config: stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let report: Value =
        serde_json::from_str(&fs::read_to_string(out_dir.join("report.json")).unwrap()).unwrap();

    assert_eq!(report["schema"], "sensor.report.v1");
    assert_eq!(report["verdict"]["status"], "fail");
    assert_eq!(report["verdict"]["reasons"][0], "tool_error");
}

/// Test multi-bench report structure has all required fields.
#[test]
fn test_cockpit_multi_bench_report_structure() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let (report, _out_dir) = run_cockpit_multi_bench(&temp_dir, &["bench-a", "bench-b"], true);

    // Verify required fields
    assert_eq!(report["schema"], "sensor.report.v1");
    assert!(report.get("tool").is_some(), "tool field missing");
    assert_eq!(report["tool"]["name"], "perfgate");
    assert!(report.get("run").is_some(), "run field missing");
    assert!(
        report["run"].get("started_at").is_some(),
        "started_at missing"
    );
    assert!(report["run"].get("ended_at").is_some(), "ended_at missing");
    assert!(
        report["run"].get("duration_ms").is_some(),
        "duration_ms missing"
    );
    assert!(
        report["run"].get("capabilities").is_some(),
        "capabilities missing"
    );
    assert!(report.get("verdict").is_some(), "verdict field missing");
    assert!(
        report["verdict"].get("status").is_some(),
        "verdict.status missing"
    );
    assert!(
        report["verdict"].get("counts").is_some(),
        "verdict.counts missing"
    );
    assert!(
        report["verdict"]["counts"].get("info").is_some(),
        "counts.info missing"
    );
    assert!(
        report["verdict"]["counts"].get("warn").is_some(),
        "counts.warn missing"
    );
    assert!(
        report["verdict"]["counts"].get("error").is_some(),
        "counts.error missing"
    );
    assert!(report.get("findings").is_some(), "findings field missing");
    assert!(report.get("data").is_some(), "data field missing");
    assert!(
        report["data"].get("summary").is_some(),
        "data.summary missing"
    );
}

/// Test multi-bench report has no truncation fields when under limit.
#[test]
fn test_cockpit_multi_bench_no_truncation_fields() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let (report, _out_dir) = run_cockpit_multi_bench(&temp_dir, &["bench-a", "bench-b"], false);

    assert!(
        report["data"].get("findings_total").is_none()
            || report["data"]["findings_total"].is_null(),
        "findings_total should be absent when under limit"
    );
    assert!(
        report["data"].get("findings_emitted").is_none()
            || report["data"]["findings_emitted"].is_null(),
        "findings_emitted should be absent when under limit"
    );
}

/// Test multi-bench extras contain valid native schemas.
#[test]
fn test_cockpit_multi_bench_extras_valid_native() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let (_report, out_dir) = run_cockpit_multi_bench(&temp_dir, &["bench-a", "bench-b"], true);

    for name in &["bench-a", "bench-b"] {
        let prefix = out_dir.join("extras").join(name);

        // Run receipt should have correct schema
        let run_content =
            fs::read_to_string(prefix.join("perfgate.run.v1.json")).expect("read run receipt");
        let run: Value = serde_json::from_str(&run_content).expect("parse run receipt");
        assert_eq!(
            run["schema"], "perfgate.run.v1",
            "extras/{}/perfgate.run.v1.json should have correct schema",
            name
        );

        // Report should have correct report_type
        let report_content =
            fs::read_to_string(prefix.join("perfgate.report.v1.json")).expect("read native report");
        let native_report: Value =
            serde_json::from_str(&report_content).expect("parse native report");
        assert_eq!(
            native_report["report_type"], "perfgate.report.v1",
            "extras/{}/perfgate.report.v1.json should have correct report_type",
            name
        );

        // Compare receipt should have correct schema
        let compare_content = fs::read_to_string(prefix.join("perfgate.compare.v1.json"))
            .expect("read compare receipt");
        let compare: Value = serde_json::from_str(&compare_content).expect("parse compare receipt");
        assert_eq!(
            compare["schema"], "perfgate.compare.v1",
            "extras/{}/perfgate.compare.v1.json should have correct schema",
            name
        );
    }
}

/// Test cockpit mode error report validates against schema
#[test]
fn test_cockpit_mode_error_report_validates_schema() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");
    let config_path = temp_dir.path().join("broken.toml");
    fs::write(&config_path, "not valid toml {{{").expect("write broken config");

    let mut cmd = perfgate_cmd();
    cmd.arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--bench")
        .arg("test-bench")
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("cockpit");

    let output = cmd.output().expect("failed to execute check");
    assert!(output.status.success());

    // Load vendored schema
    let schema_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("../../contracts/schemas/sensor.report.v1.schema.json");
    let schema_content = fs::read_to_string(&schema_path).expect("read schema");
    let schema_value: Value = serde_json::from_str(&schema_content).expect("parse schema");
    let validator = jsonschema::validator_for(&schema_value).expect("compile schema");

    let report_path = out_dir.join("report.json");
    let content = fs::read_to_string(&report_path).expect("read report");
    let instance: Value = serde_json::from_str(&content).expect("parse report");

    let errors: Vec<_> = validator.iter_errors(&instance).collect();
    assert!(
        errors.is_empty(),
        "error report failed schema validation:\n{}",
        errors
            .iter()
            .map(|e| format!("  - {}", e))
            .collect::<Vec<_>>()
            .join("\n")
    );

    // Verify fingerprint is present on error findings
    let findings = instance["findings"].as_array().expect("findings");
    for finding in findings {
        assert!(
            finding.get("fingerprint").is_some(),
            "error finding should have fingerprint"
        );
    }
}

// ======================================================================
// Mixed-outcome (per-bench error boundary) tests
// ======================================================================

/// Returns a cross-platform command that will fail to spawn (nonexistent binary).
fn nonexistent_command() -> Vec<&'static str> {
    vec!["perfgate_nonexistent_command_that_does_not_exist_xyz"]
}

/// Create a config file with per-bench commands (some may be nonexistent).
fn create_mixed_outcome_config(
    temp_dir: &std::path::Path,
    benches: &[(&str, &[&str])],
) -> std::path::PathBuf {
    let config_path = temp_dir.join("perfgate.toml");

    let mut config_content = String::from(
        r#"
[defaults]
repeat = 1
warmup = 0
threshold = 0.20
"#,
    );

    for (name, cmd) in benches {
        let cmd_str = cmd
            .iter()
            .map(|s| format!("\"{}\"", s))
            .collect::<Vec<_>>()
            .join(", ");
        config_content.push_str(&format!(
            r#"
[[bench]]
name = "{}"
command = [{}]
"#,
            name, cmd_str
        ));
    }

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

/// Test mixed-outcome multi-bench: one bench succeeds, one fails to spawn.
///
/// Validates per-bench error boundary:
/// - Exit 0 (cockpit contract)
/// - Schema-valid output
/// - Verdict = fail (error bench contributes)
/// - error >= 1
/// - Reasons include tool_error and no_baseline
/// - Findings prefixed with bench names
/// - Error finding has {stage, error_kind, bench_name}
/// - Good bench has extras, bad bench does NOT
#[test]
fn test_cockpit_multi_bench_mixed_outcome_error_and_warn() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let out_dir = temp_dir.path().join("artifacts/perfgate");

    let good_cmd = slow_command();
    let bad_cmd = nonexistent_command();

    let config_path = create_mixed_outcome_config(
        temp_dir.path(),
        &[("good-bench", &good_cmd), ("bad-bench", &bad_cmd)],
    );

    let mut cmd = perfgate_cmd();
    cmd.current_dir(temp_dir.path())
        .arg("check")
        .arg("--config")
        .arg(&config_path)
        .arg("--all")
        .arg("--out-dir")
        .arg(&out_dir)
        .arg("--mode")
        .arg("cockpit");

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

    // Cockpit contract: exit 0 even when a bench fails
    assert!(
        output.status.success(),
        "cockpit mixed-outcome should exit 0: exit code {:?}, stderr: {}",
        output.status.code(),
        String::from_utf8_lossy(&output.stderr)
    );

    // Parse report
    let report_path = out_dir.join("report.json");
    assert!(report_path.exists(), "report.json should exist");
    let report: Value =
        serde_json::from_str(&fs::read_to_string(&report_path).expect("read report"))
            .expect("parse report");

    // Schema validation
    let validator = load_vendored_schema_validator();
    let errors: Vec<_> = validator.iter_errors(&report).collect();
    assert!(
        errors.is_empty(),
        "mixed-outcome report failed schema validation:\n{}",
        errors
            .iter()
            .map(|e| format!("  - {}", e))
            .collect::<Vec<_>>()
            .join("\n")
    );

    // Verdict: fail (error bench makes it fail)
    assert_eq!(
        report["verdict"]["status"], "fail",
        "mixed-outcome verdict should be fail"
    );

    // Counts: error >= 1
    let error_count = report["verdict"]["counts"]["error"].as_u64().unwrap();
    assert!(error_count >= 1, "should have at least 1 error count");

    // Reasons include tool_error and no_baseline
    let reasons = report["verdict"]["reasons"]
        .as_array()
        .expect("reasons array");
    let has_tool_error = reasons.iter().any(|r| r.as_str() == Some("tool_error"));
    let has_no_baseline = reasons.iter().any(|r| r.as_str() == Some("no_baseline"));
    assert!(has_tool_error, "reasons should include tool_error");
    assert!(has_no_baseline, "reasons should include no_baseline");

    // Findings: should have prefixed messages
    let findings = report["findings"].as_array().expect("findings array");
    assert!(
        findings.len() >= 2,
        "should have at least 2 findings (good-bench warn + bad-bench error)"
    );

    let has_good_prefix = findings.iter().any(|f| {
        f["message"]
            .as_str()
            .unwrap_or("")
            .starts_with("[good-bench]")
    });
    let has_bad_prefix = findings.iter().any(|f| {
        f["message"]
            .as_str()
            .unwrap_or("")
            .starts_with("[bad-bench]")
    });
    assert!(has_good_prefix, "should have [good-bench] prefixed finding");
    assert!(has_bad_prefix, "should have [bad-bench] prefixed finding");

    // Error finding should have structured data with stage, error_kind, bench_name
    let error_finding = findings
        .iter()
        .find(|f| f["check_id"].as_str() == Some("tool.runtime"))
        .expect("should have tool.runtime finding");
    let finding_data = &error_finding["data"];
    assert!(
        finding_data.get("stage").is_some() && !finding_data["stage"].is_null(),
        "error finding should have stage"
    );
    assert!(
        finding_data.get("error_kind").is_some() && !finding_data["error_kind"].is_null(),
        "error finding should have error_kind"
    );
    assert_eq!(
        finding_data["bench_name"], "bad-bench",
        "error finding should have bench_name = bad-bench"
    );

    // Good bench should have extras, bad bench should NOT
    let good_extras = out_dir.join("extras").join("good-bench");
    let bad_extras = out_dir.join("extras").join("bad-bench");
    assert!(
        good_extras.join("perfgate.run.v1.json").exists(),
        "good-bench should have extras/good-bench/perfgate.run.v1.json"
    );
    // Bad bench may or may not have the extras dir created, but should NOT have run receipt
    let bad_has_run = bad_extras.join("perfgate.run.v1.json").exists();
    assert!(
        !bad_has_run,
        "bad-bench should NOT have perfgate.run.v1.json"
    );

    // Artifacts in report should reference good-bench but NOT bad-bench
    let artifacts = report["artifacts"].as_array().expect("artifacts array");
    let has_good_artifact = artifacts
        .iter()
        .any(|a| a["path"].as_str().unwrap_or("").contains("good-bench"));
    let has_bad_artifact = artifacts
        .iter()
        .any(|a| a["path"].as_str().unwrap_or("").contains("bad-bench"));
    assert!(has_good_artifact, "artifacts should reference good-bench");
    assert!(
        !has_bad_artifact,
        "artifacts should NOT reference bad-bench"
    );

    // bench_count should include both
    assert_eq!(
        report["data"]["summary"]["bench_count"], 2,
        "bench_count should be 2 (both benches counted)"
    );
}