keyhog 0.5.73

GPU-accelerated secret scanner for code, Git history, cloud, containers, browser assets, and live credential verification
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
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
//! End-to-end tests that drive the real `keyhog` binary.
//!
//! Per the per-rule contract (CLAUDE.md test type 10), "the product
//! is the binary." These tests:
//!
//! * use `env!("CARGO_BIN_EXE_keyhog")` - cargo points this at the
//!   freshly built `keyhog` binary in `target/<profile>/keyhog`, so we
//!   exercise the same executable users get;
//! * write a planted-credential fixture to `tempfile::TempDir` (out of
//!   the workspace, so `.gitignore` skip rules don't interfere - keyhog
//!   walks `.internal/` etc. as gitignored, which this test would
//!   otherwise trip);
//! * parse `--format json` stdout, verify shape + counts;
//! * verify the documented exit codes.
//!
//! The fixture is small and self-contained so the test is fast
//! enough to live in the normal `cargo test` flow.

use std::path::PathBuf;
use std::process::Command;

use tempfile::TempDir;

#[path = "support/json_report.rs"]
mod json_report_support;

use json_report_support::parse_json_array;

const FUNCTIONAL_E2E_BACKEND: &str = "cpu";

fn binary() -> PathBuf {
    PathBuf::from(env!("CARGO_BIN_EXE_keyhog"))
}

fn repo_root() -> PathBuf {
    let mut root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    root.pop();
    root.pop();
    root
}

fn detector_dir() -> PathBuf {
    repo_root().join("detectors")
}

fn doc_text(rel: &str) -> String {
    std::fs::read_to_string(repo_root().join(rel))
        .unwrap_or_else(|error| panic!("read {rel} for doc/banner coherence contract: {error}"))
}

/// One-line helper: write a temp file with given content, scan it
/// with `--format json`, return (stdout, stderr, exit-code).
fn scan_text_file(content: &str, extra_args: &[&str]) -> (String, String, Option<i32>) {
    let dir = TempDir::new().expect("tempdir");
    let path = dir.path().join("planted.txt");
    std::fs::write(&path, content).expect("write fixture");

    let output = Command::new(binary())
        .arg("scan")
        .arg("--daemon=off")
        .args(["--backend", FUNCTIONAL_E2E_BACKEND])
        .args(extra_args)
        .arg("--format")
        .arg("json")
        .arg(&path)
        .env_remove("KEYHOG_BACKEND")
        .output()
        .expect("spawn keyhog scan");

    (
        String::from_utf8_lossy(&output.stdout).into_owned(),
        String::from_utf8_lossy(&output.stderr).into_owned(),
        output.status.code(),
    )
}

fn portable_progress_banner() -> String {
    let dir = TempDir::new().expect("tempdir");
    let path = dir.path().join("clean.txt");
    std::fs::write(&path, "hello world\n").expect("write fixture");

    let output = Command::new(binary())
        .args([
            "scan",
            "--no-config",
            "--daemon=off",
            "--progress",
            "--format",
            "json",
            "--backend",
            FUNCTIONAL_E2E_BACKEND,
        ])
        .arg(&path)
        .output()
        .expect("spawn keyhog scan --progress");
    assert_eq!(
        output.status.code(),
        Some(0),
        "clean progress scan should exit 0; stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    stderr
        .lines()
        .find(|line| {
            line.contains("detectors (") && line.contains("patterns)") && line.contains("backend=")
        })
        .unwrap_or_else(|| panic!("progress banner missing from stderr:\n{stderr}"))
        .to_owned()
}

fn parse_banner_counts(line: &str) -> (usize, usize) {
    let marker = " detectors (";
    let detector_end = line
        .find(marker)
        .unwrap_or_else(|| panic!("progress banner missing detector marker: {line}"));
    let detector_count = line[..detector_end]
        .split_whitespace()
        .last()
        .unwrap_or_else(|| panic!("progress banner missing detector count: {line}"))
        .parse()
        .unwrap_or_else(|error| panic!("progress banner detector count is not numeric: {error}"));
    let pattern_count = line[detector_end + marker.len()..]
        .split_whitespace()
        .next()
        .unwrap_or_else(|| panic!("progress banner missing pattern count: {line}"))
        .parse()
        .unwrap_or_else(|error| panic!("progress banner pattern count is not numeric: {error}"));
    (detector_count, pattern_count)
}

#[test]
fn scan_finds_planted_aws_key_and_returns_exit_1() {
    let fixture = concat!("AWS_ACCESS_KEY_ID = \"AKIA", "QYLPMN5HFIQR7XYA\"\n");
    let (stdout, _stderr, code) = scan_text_file(fixture, &[]);

    // Documented exit codes: 0 = clean, 1 = unverified findings.
    // Planted key with no `--verify` should land us at 1.
    assert_eq!(
        code,
        Some(1),
        "expected exit 1 (unverified findings); got {code:?}"
    );

    let findings: serde_json::Value = serde_json::from_str(&stdout).expect("stdout is valid JSON");
    let arr = findings.as_array().expect("findings JSON is an array");
    // Non-emptiness is proven by the exact AWS-detector assert below; a bare
    // shape assert here would pass on a single junk finding.
    // Accelerated and portable paths must keep the canonical TOML detector id.
    let aws = arr.iter().find(|f| {
        matches!(
            f.get("detector_id").and_then(|v| v.as_str()),
            Some("aws-access-key")
        )
    });
    assert!(aws.is_some(), "expected an AWS key finding; got: {arr:?}");
}

#[test]
fn scan_returns_exit_0_on_clean_file() {
    let fixture = "fn main() { println!(\"hello\"); }\n";
    let (stdout, _stderr, code) = scan_text_file(fixture, &[]);

    assert_eq!(code, Some(0), "expected exit 0 on clean file; got {code:?}");
    let findings: serde_json::Value = serde_json::from_str(&stdout).expect("stdout is valid JSON");
    let arr = findings.as_array().expect("findings JSON is an array");
    assert!(arr.is_empty(), "expected zero findings; got: {arr:?}");
}

/// G1 binary proof: a planted long-term AWS Bedrock API key surfaces through
/// the real binary under the `aws-bedrock-api-key` detector and lands exit 1
/// (findings present, none verified live). Split-literal so the test file
/// itself isn't a planted-secret tripwire.
#[test]
fn scan_finds_planted_bedrock_key_and_returns_exit_1() {
    let fixture = concat!(
        "AWS_BEARER_TOKEN_BEDROCK=\"ABSKQmVkcm9ja0FQSUtleS",
        "y2J0fajDUXD1efoRCtqKODGGBi8UWr7UJsq2tkhFhx8ZEDEd9hnKHivse0YHShMdeCAbPEOXOxyhkg5cqNGHA1grwAyKC3Y8HDD62wLdl37iKN\"\n",
    );
    let (stdout, _stderr, code) = scan_text_file(fixture, &[]);
    assert_eq!(
        code,
        Some(1),
        "planted Bedrock key should exit 1; got {code:?}"
    );
    let arr: Vec<serde_json::Value> = serde_json::from_str(&stdout).expect("stdout is valid JSON");
    let bedrock = arr
        .iter()
        .find(|f| f.get("detector_id").and_then(|v| v.as_str()) == Some("aws-bedrock-api-key"));
    assert!(
        bedrock.is_some(),
        "expected an aws-bedrock-api-key finding; got: {arr:?}",
    );
    assert_eq!(
        bedrock.unwrap().get("severity").and_then(|v| v.as_str()),
        Some("critical"),
        "Bedrock key must be critical severity",
    );
}

/// Exit-code contract (`docs/src/reference/exit-codes.md`, row `2`): an
/// unknown CLI flag is user error → exit 2, never 1 or 3.
#[test]
fn scan_unknown_flag_exits_2() {
    let dir = TempDir::new().expect("tempdir");
    let output = Command::new(binary())
        .arg("scan")
        .arg("--this-flag-does-not-exist")
        .arg(dir.path())
        .output()
        .expect("spawn keyhog scan");
    assert_eq!(
        output.status.code(),
        Some(2),
        "unknown flag must exit 2 (user error); stderr={}",
        String::from_utf8_lossy(&output.stderr),
    );
}

/// Exit-code contract: a source backend the user named that can't read its
/// input (`--git-history` on a non-git directory) is a distinct source failure
/// -> exit 13, not generic user-error 2 or system-error 3.
#[test]
fn scan_git_history_on_non_repo_exits_13() {
    let dir = TempDir::new().expect("tempdir");
    std::fs::write(dir.path().join("a.txt"), "nothing here\n").expect("write");
    let output = Command::new(binary())
        .arg("scan")
        .arg("--git-history")
        .arg(dir.path())
        .output()
        .expect("spawn keyhog scan --git-history");
    assert_eq!(
        output.status.code(),
        Some(13),
        "--git-history on a non-git dir must exit 13 (source failed), not 2/3; stderr={}",
        String::from_utf8_lossy(&output.stderr),
    );
}

/// Exit-code contract: `diff` with a baseline file the user named that does
/// not exist is user error → exit 2 (not 1 = "no new entries", not 3).
#[test]
fn diff_missing_baseline_exits_2() {
    let dir = TempDir::new().expect("tempdir");
    let output = Command::new(binary())
        .arg("diff")
        .arg(dir.path().join("before.json"))
        .arg(dir.path().join("after.json"))
        .output()
        .expect("spawn keyhog diff");
    assert_eq!(
        output.status.code(),
        Some(2),
        "diff with a missing baseline must exit 2 (user error); stderr={}",
        String::from_utf8_lossy(&output.stderr),
    );
}

#[test]
fn scan_json_schema_carries_required_fields() {
    let fixture = "GH_TOKEN = \"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\"\n";
    let (stdout, _stderr, _code) = scan_text_file(fixture, &[]);

    let findings: serde_json::Value = serde_json::from_str(&stdout).expect("stdout is valid JSON");
    let arr = findings.as_array().expect("findings JSON is an array");
    // Truth assert (not "some finding"): the planted ghp_ token fired a GitHub
    // detector on line 1, otherwise the field-presence loop below would pass
    // vacuously over an empty array.
    let gh = arr.iter().find(|f| {
        let det = f.get("detector_id").and_then(|v| v.as_str()).unwrap_or("");
        let svc = f.get("service").and_then(|v| v.as_str()).unwrap_or("");
        (det.contains("github") || svc.contains("github"))
            && f.pointer("/location/line").and_then(|v| v.as_u64()) == Some(1)
    });
    assert!(
        gh.is_some(),
        "expected the planted ghp_ token to fire a GitHub detector on line 1; got {arr:?}"
    );

    // Every finding MUST carry the contract fields downstream
    // consumers (CI gates, SARIF converters, IDE plugins) depend on.
    for f in arr {
        for required in [
            "detector_id",
            "detector_name",
            "service",
            "severity",
            "credential_redacted",
            "credential_hash",
            "location",
            "verification",
        ] {
            assert!(
                f.get(required).is_some(),
                "finding is missing required field `{required}`: {f}",
            );
        }
        let loc = f.get("location").unwrap();
        for required in ["source", "file_path", "line", "offset"] {
            assert!(
                loc.get(required).is_some(),
                "location is missing required field `{required}`: {loc}",
            );
        }
    }
}

/// Shipped-artifact binding test: the binary must advertise exactly the
/// detector + pattern corpus it was built from. Both expected counts are
/// DERIVED from `keyhog_core::load_detectors` over the on-disk `detectors/`
/// tree, the same set `build.rs` embeds, so adding/removing a detector
/// never requires editing a literal here (the count is single-sourced from
/// the loader; the README headline is pinned separately in
/// `scanner/tests/readme_claims.rs`).
#[test]
fn readme_banner_counts_match_loaded_corpus() {
    let detector_dir = detector_dir();
    let specs = keyhog_core::load_detectors(&detector_dir).expect("load detectors/ corpus");
    let expected_detectors = specs.len();
    let expected_patterns: usize = specs.iter().map(|d| d.patterns.len()).sum();

    let output = Command::new(binary())
        .arg("detectors")
        .args(["--format", "json"])
        .output()
        .expect("spawn keyhog detectors --format json");
    assert_eq!(output.status.code(), Some(0));
    let arr: Vec<serde_json::Value> =
        serde_json::from_slice(&output.stdout).expect("detectors JSON parse");
    let actual_patterns: usize = arr
        .iter()
        .map(|d| {
            d.get("patterns")
                .and_then(|v| v.as_array())
                .map(|a| a.len())
                .unwrap_or(0)
        })
        .sum();

    assert_eq!(
        arr.len(),
        expected_detectors,
        "binary advertises {} detectors but the on-disk corpus has {expected_detectors}. \
         The shipped binary embeds a stale set, rebuild, or a detector silently failed \
         to embed.",
        arr.len(),
    );
    assert_eq!(
        actual_patterns, expected_patterns,
        "binary advertises {actual_patterns} patterns but the on-disk corpus has \
         {expected_patterns}. Binary/corpus pattern drift.",
    );
}

#[test]
fn docs_scan_banners_match_live_binary_banner_contract() {
    let detector_dir = detector_dir();
    let specs = keyhog_core::load_detectors(&detector_dir).expect("load detectors/ corpus");
    let expected_detectors = specs.len();
    // Compile the same loaded specs the binary embeds. `patterns.len()` only
    // counts authored TOML regexes; the progress banner reports the canonical
    // compiled plan after required matcher projections have been added.
    let expected_patterns = keyhog_scanner::CompiledScanner::compile_with_gpu_policy(
        specs,
        keyhog_scanner::GpuInitPolicy::ForceDisabled,
    )
    .expect("compile detectors/ corpus")
    .runtime_status()
    .pattern_count;

    let version_output = Command::new(binary())
        .arg("--version")
        .output()
        .expect("spawn keyhog --version");
    assert_eq!(
        version_output.status.code(),
        Some(0),
        "--version must exit 0; stderr={}",
        String::from_utf8_lossy(&version_output.stderr)
    );
    let version_stdout = String::from_utf8_lossy(&version_output.stdout);
    assert!(
        version_stdout.contains(env!("CARGO_PKG_VERSION")),
        "--version output must expose the workspace version {}; got {version_stdout}",
        env!("CARGO_PKG_VERSION")
    );

    let progress_banner = portable_progress_banner();
    let (banner_detectors, banner_patterns) = parse_banner_counts(&progress_banner);
    assert_eq!(
        banner_detectors, expected_detectors,
        "live progress banner detector count drifted from loaded corpus; banner={progress_banner}"
    );
    assert_eq!(
        banner_patterns, expected_patterns,
        "live progress banner pattern count drifted from the scanner compiled from the same corpus; banner={progress_banner}"
    );

    let version_fragment = format!(
        "v{} · secret scanner · {expected_detectors} detectors",
        env!("CARGO_PKG_VERSION")
    );
    let compiled_count_fragment =
        format!("{expected_detectors} detectors ({banner_patterns} patterns)");
    for rel in ["docs/src/introduction.md", "docs/src/first-scan.md"] {
        let doc = doc_text(rel);
        assert!(
            doc.contains("K E Y H O G") && doc.contains("by santh"),
            "{rel} must show the real multi-line KeyHog banner"
        );
        assert!(
            doc.contains(&version_fragment),
            "{rel} must use the live --version/detector banner `{version_fragment}`"
        );
        assert!(
            doc.contains(&compiled_count_fragment),
            "{rel} must pin the live compiled scanner pattern count `{compiled_count_fragment}`"
        );
        assert!(
            doc.contains("backend=") && doc.contains("gpu="),
            "{rel} must show operator-visible backend/gpu decision fields"
        );
        assert!(
            !doc.contains("AVX-512 + Hyperscan + CUDA") && !doc.contains("1666 patterns"),
            "{rel} still contains the stale one-line fabricated banner"
        );
    }
}

#[test]
fn detectors_subcommand_emits_json_array() {
    let output = Command::new(binary())
        .arg("detectors")
        .args(["--format", "json"])
        .output()
        .expect("spawn keyhog detectors --format json");
    assert_eq!(
        output.status.code(),
        Some(0),
        "detectors --format json should exit 0; stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    let parsed: serde_json::Value =
        serde_json::from_str(&stdout).expect("detectors --format json stdout is valid JSON");
    let arr = parsed
        .as_array()
        .expect("--format json output is a JSON array");
    assert!(
        arr.len() > 100,
        "expected hundreds of detectors; got {}",
        arr.len()
    );
    // Spot-check one well-known detector.
    let aws = arr
        .iter()
        .find(|d| d.get("id").and_then(|v| v.as_str()) == Some("aws-access-key"));
    assert!(
        aws.is_some(),
        "aws-access-key should appear in --format json output"
    );
    let aws = aws.unwrap();
    assert_eq!(
        aws.get("service").and_then(|v| v.as_str()),
        Some("aws"),
        "aws-access-key should have service=aws",
    );

    let allowed = ["info", "client-safe", "low", "medium", "high", "critical"];
    for detector in arr {
        let severity = detector
            .get("severity")
            .and_then(|value| value.as_str())
            .expect("detector severity must be a string");
        assert!(
            allowed.contains(&severity),
            "detector JSON emitted noncanonical severity {severity:?}: {detector}"
        );
    }
}

#[test]
fn detectors_format_json_is_canonical_and_json_alias_is_retired() {
    let retired = Command::new(binary())
        .args(["detectors", "--json"])
        .output()
        .expect("spawn retired detector json flag");
    let canonical = Command::new(binary())
        .args(["detectors", "--format", "json"])
        .output()
        .expect("spawn keyhog detectors --format json");

    assert_eq!(
        retired.status.code(),
        Some(2),
        "retired detectors --json must exit 2; stderr={}",
        String::from_utf8_lossy(&retired.stderr)
    );
    assert_eq!(
        canonical.status.code(),
        Some(0),
        "detectors --format json should exit 0; stderr={}",
        String::from_utf8_lossy(&canonical.stderr)
    );
    assert!(String::from_utf8_lossy(&retired.stderr).contains("unexpected argument '--json'"));
    let parsed: serde_json::Value = serde_json::from_slice(&canonical.stdout)
        .expect("detectors --format json stdout is valid JSON");
    assert!(
        parsed.as_array().is_some_and(|items| items.len() > 100),
        "detectors --format json must emit the detector array, got {parsed}"
    );
}

/// Tier-B suppression flag: by default keyhog suppresses Stripe's
/// public docs demo key (and other documented test fixtures), so
/// scanning a fixture containing it surfaces 0 findings. Passing
/// `--no-suppress-test-fixtures` flips that - the same fixture
/// produces the finding gitleaks and trufflehog also report.
///
/// This is the binding test for the Tier-B move (task #60). If
/// someone deletes the bundled `test-fixtures.toml` entry for
/// Stripe, the default-mode assertion below catches it; if someone
/// drops the `--no-suppress-test-fixtures` arg, the opt-out branch
/// catches it.
#[test]
fn no_suppress_test_fixtures_surfaces_stripe_demo_key() {
    // The canonical Stripe public-docs demo key. Split via `concat!`
    // so GitHub Push Protection doesn't scan this source file as a
    // live secret leak.
    let stripe_key = concat!("sk_", "live_", "4eC39HqLyjWDarjtT1zdp7dc");
    let fixture = format!("STRIPE_KEY = \"{stripe_key}\"\n");

    let dir = TempDir::new().expect("tempdir");
    let path = dir.path().join("planted.txt");
    std::fs::write(&path, &fixture).expect("write fixture");

    // ----- default: suppressed -----------------------------------
    let default_out = Command::new(binary())
        .arg("scan")
        .arg("--daemon=off")
        .arg("--backend")
        .arg(FUNCTIONAL_E2E_BACKEND)
        .arg("--format")
        .arg("json")
        .arg(&path)
        .output()
        .expect("spawn keyhog scan (default)");
    let default_json = String::from_utf8_lossy(&default_out.stdout);
    let default_findings: serde_json::Value =
        serde_json::from_str(&default_json).expect("default-mode stdout is JSON");
    let default_arr = default_findings.as_array().expect("array");
    let has_stripe = default_arr
        .iter()
        .any(|f| f.get("service").and_then(|v| v.as_str()) == Some("stripe"));
    assert!(
        !has_stripe,
        "default mode MUST suppress the Stripe demo key; got findings: {default_arr:?}"
    );

    // ----- --no-suppress-test-fixtures: surfaced -----------------
    let optout_out = Command::new(binary())
        .arg("scan")
        .arg("--daemon=off")
        .arg("--backend")
        .arg(FUNCTIONAL_E2E_BACKEND)
        .arg("--no-suppress-test-fixtures")
        .arg("--format")
        .arg("json")
        .arg(&path)
        .output()
        .expect("spawn keyhog scan (opt-out)");
    let optout_json = String::from_utf8_lossy(&optout_out.stdout);
    let optout_findings: serde_json::Value =
        serde_json::from_str(&optout_json).expect("opt-out stdout is JSON");
    let optout_arr = optout_findings.as_array().expect("array");
    let has_stripe_now = optout_arr
        .iter()
        .any(|f| f.get("service").and_then(|v| v.as_str()) == Some("stripe"));
    assert!(
        has_stripe_now,
        "--no-suppress-test-fixtures MUST surface the Stripe demo key; \
         got findings: {optout_arr:?}"
    );
}

#[test]
fn no_suppress_test_fixtures_surfaces_test_path_findings() {
    // A URL-shaped password is intentionally owned by `url-credentials`, so it
    // cannot prove that the generic-password test-path haircut is reversible.
    // Keep this a pure PASSWORD assignment: default suppresses it, while the
    // explicit opt-out must restore this exact generic finding and span.
    let fixture = "DATABASE_PASSWORD = \"S3cr3tP4ssw0rd\"\n";

    let dir = TempDir::new().expect("tempdir");
    let fixture_dir = dir.path().join("tests").join("fixtures");
    std::fs::create_dir_all(&fixture_dir).expect("create fixture dir");
    let path = fixture_dir.join("planted.env");
    std::fs::write(&path, fixture).expect("write fixture");

    let default_out = Command::new(binary())
        .arg("scan")
        .arg("--daemon=off")
        .arg("--backend")
        .arg(FUNCTIONAL_E2E_BACKEND)
        .arg("--format")
        .arg("json")
        .arg("--min-confidence")
        .arg("0.0")
        .arg(&path)
        .output()
        .expect("spawn keyhog scan (default)");
    assert_eq!(
        default_out.status.code(),
        Some(0),
        "the suppressed test-path fixture must be a clean scan; stderr={}",
        String::from_utf8_lossy(&default_out.stderr)
    );
    let default_json = String::from_utf8_lossy(&default_out.stdout);
    let default_findings: serde_json::Value =
        serde_json::from_str(&default_json).expect("default-mode stdout is JSON");
    assert_eq!(
        default_findings.as_array().map(Vec::len),
        Some(0),
        "default mode should suppress low-confidence test-path findings; got {default_json}"
    );

    let optout_out = Command::new(binary())
        .arg("scan")
        .arg("--daemon=off")
        .arg("--backend")
        .arg(FUNCTIONAL_E2E_BACKEND)
        .arg("--no-suppress-test-fixtures")
        .arg("--format")
        .arg("json")
        .arg("--min-confidence")
        .arg("0.0")
        .arg(&path)
        .output()
        .expect("spawn keyhog scan (opt-out)");
    let optout_json = String::from_utf8_lossy(&optout_out.stdout);
    let optout_findings: serde_json::Value =
        serde_json::from_str(&optout_json).expect("opt-out stdout is JSON");
    let optout_arr = optout_findings.as_array().expect("array");
    assert_eq!(
        optout_out.status.code(),
        Some(1),
        "surfacing the opted-out test-path finding must use the findings exit; stderr={}",
        String::from_utf8_lossy(&optout_out.stderr)
    );
    assert_eq!(
        optout_arr.len(),
        1,
        "--no-suppress-test-fixtures must surface exactly the planted finding; got {optout_json}"
    );
    let surfaced = &optout_arr[0];
    assert_eq!(
        surfaced.get("detector_id").and_then(|v| v.as_str()),
        Some("generic-password"),
        "the PASSWORD assignment must stay with its detector-data owner; got {optout_json}"
    );
    assert_eq!(
        surfaced.pointer("/location/line").and_then(|v| v.as_u64()),
        Some(1),
        "the finding must map to the planted line; got {optout_json}"
    );
    assert_eq!(
        surfaced.pointer("/location/offset").and_then(|v| v.as_u64()),
        Some(21),
        "the finding span must start at the password value, after the opening quote; got {optout_json}"
    );
    let surfaced_path = surfaced
        .pointer("/location/file_path")
        .and_then(|v| v.as_str())
        .expect("finding file path");
    assert_eq!(
        PathBuf::from(surfaced_path),
        path,
        "the finding must stay attributed to the exact test fixture"
    );
    assert_eq!(
        surfaced.get("credential_redacted").and_then(|v| v.as_str()),
        Some("S...d"),
        "the report must use keyhog_core::redact's one-character edges for this short credential"
    );
    let confidence = surfaced
        .get("confidence")
        .and_then(|v| v.as_f64())
        .unwrap_or_default();
    assert!(
        confidence >= 0.69,
        "fixture opt-out must bypass pre-ML test-path down-weighting; got {confidence}"
    );
}

/// Regression for the demo-secret.env UX bug originally flagged internally
/// on 2026-05-17: scanning a file that holds an
/// AWS-published EXAMPLE credential (AKIAIOSFODNN7EXAMPLE) used to
/// print "No secrets found. Your code is clean." - identical to a
/// genuinely clean repo - because the test-fixture suppression
/// filtered the match BEFORE the example-suppression telemetry
/// counter saw it. The reporter then read counter=0 and chose the
/// clean-repo summary.
///
/// v0.5.6 wired `record_example_suppression` for the engine-side
/// EXAMPLE token check, but missed this orchestrator-level
/// test-fixture filter, so the bug came back as soon as the AWS
/// fixture went through the substring suppression instead of the
/// engine path. This test pins the right behaviour:
///
/// * Default mode → output contains "example/test key" and does
///   NOT contain the all-clean summary.
/// * The bundled AWS-EXAMPLE entry must still suppress (no
///   finding shown in the matches list).
#[test]
fn demo_secret_aws_example_summary_distinguishes_suppression_from_clean() {
    let fixture = "AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE\n";
    let dir = TempDir::new().expect("tempdir");
    let path = dir.path().join("demo-secret.env");
    std::fs::write(&path, fixture).expect("write fixture");

    // --daemon=off to guarantee the in-process orchestrator path is
    // exercised (the daemon path lives in `subcommands/scan.rs` and
    // is locked by `daemon_route_test_fixture_suppression_records_telemetry`
    // below).
    let out = Command::new(binary())
        .arg("scan")
        .arg("--daemon=off")
        .arg("--backend")
        .arg(FUNCTIONAL_E2E_BACKEND)
        .arg("--format")
        .arg("text")
        .arg(&path)
        .output()
        .expect("spawn keyhog scan demo-secret.env");
    let stdout = String::from_utf8_lossy(&out.stdout);

    assert!(
        stdout.contains("example/test key") && stdout.contains("suppressed"),
        "demo-secret.env summary must distinguish suppressed-example from a \
         clean repo. Got stdout: {stdout}"
    );
    assert!(
        !stdout.contains("Your code is clean."),
        "the clean-repo summary must NOT fire when an example credential was \
         suppressed. Got stdout: {stdout}"
    );
}

#[test]
fn explicit_format_text_does_not_emit_json() {
    let fixture = concat!("AWS_ACCESS_KEY_ID = \"AKIA", "QYLPMN5HFIQR7XYA\"\n");
    let dir = TempDir::new().expect("tempdir");
    let path = dir.path().join("planted.txt");
    std::fs::write(&path, fixture).expect("write fixture");

    // Don't share the json-format helper here - text-format is the
    // contrast case we're asserting.
    let output = Command::new(binary())
        .arg("scan")
        .arg("--daemon=off")
        .arg("--backend")
        .arg(FUNCTIONAL_E2E_BACKEND)
        .arg("--format")
        .arg("text")
        .arg(&path)
        .output()
        .expect("spawn keyhog scan --format text");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    let combined = format!("{stdout}\n{stderr}");

    // Text mode is the human-facing default. The hard contract:
    // (1) stdout MUST NOT start with `[` (would mean JSON leaked
    //     through), and (2) the combined stream must reference the
    //     finding somewhere - text reporter writes to stdout or
    //     stderr depending on `--output`; we accept either.
    assert!(
        !stdout.trim_start().starts_with('['),
        "text format must not start with JSON `[`; got: {stdout}",
    );
    assert!(
        combined.to_lowercase().contains("aws") || combined.contains("AKIA"),
        "text format should mention the finding somewhere; \
         stdout={stdout:?}, stderr={stderr:?}, exit={:?}",
        output.status.code(),
    );
}

/// `--scan-comments` end-to-end: pins the wiring all the way from the
/// clap flag → ScanArgs → orchestrator_config::scan_comments →
/// ScannerConfig.scan_comments → fallback_generic + engine context-
/// penalty gates. The invariant under test is that `--scan-comments`
/// never loses findings versus the default and surfaces a credential
/// planted in a `// TODO: rotate this …` comment. (A strong known-prefix
/// key like AWS clears the comment-context penalty in both modes; a
/// weaker token would be the one the opt-in lifts above the floor.)
#[test]
fn scan_comments_flag_surfaces_credentials_in_comments() {
    // A genuine-shape AWS access key, exactly 20 chars (`AKIA` + 16)
    // inside a `//`-style comment. The length is load-bearing: the
    // aws-access-key detector requires the canonical 20-char form, so a
    // longer `AKIA…` string is correctly rejected as malformed and would
    // never reach the comment-context path this test exercises.
    let aws_key = concat!("AKIA", "ROTATIONNEEDED77");
    let fixture = format!("// TODO: rotate this - {aws_key}\n");

    let dir = TempDir::new().expect("tempdir");
    let path = dir.path().join("comment_planted.go");
    std::fs::write(&path, &fixture).expect("write fixture");

    // Default: comment-context penalty in effect; AWS prefix is
    // strong enough to still fire on this one, so we don't assert
    // the *absence* of the finding (that would be brittle to
    // confidence-floor tuning). What we DO assert is that
    // --scan-comments AT LEAST matches the default - never silently
    // hides findings the default would surface.
    let default_out = Command::new(binary())
        .arg("scan")
        .arg("--daemon=off")
        .arg("--backend")
        .arg(FUNCTIONAL_E2E_BACKEND)
        .arg("--format")
        .arg("json")
        .arg(&path)
        .output()
        .expect("spawn keyhog scan (default)");
    let default_json = String::from_utf8_lossy(&default_out.stdout);
    let default_findings: serde_json::Value =
        serde_json::from_str(&default_json).expect("default-mode stdout is JSON");
    let default_count = default_findings.as_array().map(|a| a.len()).unwrap_or(0);

    let opt_in_out = Command::new(binary())
        .arg("scan")
        .arg("--daemon=off")
        .arg("--backend")
        .arg(FUNCTIONAL_E2E_BACKEND)
        .arg("--scan-comments")
        .arg("--format")
        .arg("json")
        .arg(&path)
        .output()
        .expect("spawn keyhog scan --scan-comments");
    let opt_in_json = String::from_utf8_lossy(&opt_in_out.stdout);
    let opt_in_findings: serde_json::Value =
        serde_json::from_str(&opt_in_json).expect("opt-in stdout is JSON");
    let opt_in_count = opt_in_findings.as_array().map(|a| a.len()).unwrap_or(0);

    assert!(
        opt_in_count >= default_count,
        "--scan-comments must not LOSE findings vs default; \
         default={default_count}, --scan-comments={opt_in_count}, \
         default_json={default_json}, opt_in_json={opt_in_json}"
    );

    // At minimum --scan-comments fires on this AKIA-prefixed key
    // (the keyhog known-prefix floor keeps it above any penalty).
    assert!(
        opt_in_count >= 1,
        "--scan-comments MUST surface the AKIA-prefixed key in the \
         comment; got {opt_in_count} findings: {opt_in_json}"
    );
}

#[cfg(feature = "git")]
fn init_git_repo(repo_path: &std::path::Path) {
    use std::process::Command;
    for args in [
        ["init", "-b", "main"],
        ["config", "user.email", "test@example.com"],
        ["config", "user.name", "Test User"],
    ] {
        let output = Command::new("git")
            .args(args)
            .current_dir(repo_path)
            .output()
            .expect("git setup");
        assert!(output.status.success(), "git setup failed: {output:?}");
    }
}

#[cfg(feature = "git")]
#[test]
fn git_staged_scan_finds_only_staged_secret() {
    use std::process::Command;

    let repo = TempDir::new().expect("tempdir");
    let repo_path = repo.path();
    init_git_repo(repo_path);

    std::fs::write(
        repo_path.join("staged.env"),
        concat!("AWS_ACCESS_KEY_ID = \"AKIA", "QYLPMN5HFIQR7XYA\"\n"),
    )
    .unwrap();
    std::fs::write(
        repo_path.join("unstaged.env"),
        "AWS_ACCESS_KEY_ID = \"AKIAQYLPMN5HUNSTAGEDKEY000000000000\"\n",
    )
    .unwrap();
    Command::new("git")
        .args(["add", "staged.env"])
        .current_dir(repo_path)
        .output()
        .unwrap();

    let output = Command::new(binary())
        .current_dir(repo_path)
        .args([
            "scan",
            "--git-staged",
            "--daemon=off",
            "--backend",
            FUNCTIONAL_E2E_BACKEND,
            "--format",
            "json",
            "--path",
            ".",
        ])
        .output()
        .expect("git-staged scan");

    assert_eq!(
        output.status.code(),
        Some(1),
        "staged secret must exit 1; stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    let findings: serde_json::Value =
        serde_json::from_slice(&output.stdout).expect("stdout is JSON");
    let arr = findings.as_array().expect("array");
    assert!(
        arr.iter().any(|f| {
            f.get("location")
                .and_then(|l| l.get("file_path"))
                .and_then(|p| p.as_str())
                .is_some_and(|p| p.ends_with("staged.env"))
        }),
        "must find staged file secret; got {arr:?}"
    );
    assert!(
        !arr.iter().any(|f| {
            f.get("location")
                .and_then(|l| l.get("file_path"))
                .and_then(|p| p.as_str())
                .is_some_and(|p| p.contains("unstaged.env"))
        }),
        "unstaged file must not be scanned; got {arr:?}"
    );
}

#[test]
fn baseline_suppresses_acknowledged_findings_on_rescan() {
    let dir = TempDir::new().expect("tempdir");
    let fixture = dir.path().join("planted.txt");
    std::fs::write(
        &fixture,
        concat!("AWS_ACCESS_KEY_ID = \"AKIA", "QYLPMN5HFIQR7XYA\"\n"),
    )
    .unwrap();
    let baseline_path = dir.path().join("baseline.json");

    let create = Command::new(binary())
        .args([
            "scan",
            "--daemon=off",
            "--backend",
            FUNCTIONAL_E2E_BACKEND,
            "--create-baseline",
            baseline_path.to_str().unwrap(),
            "--format",
            "json",
        ])
        .arg(&fixture)
        .output()
        .expect("create baseline");
    assert_eq!(
        create.status.code(),
        Some(0),
        "create-baseline must exit 0; stderr={}",
        String::from_utf8_lossy(&create.stderr)
    );
    assert!(baseline_path.exists(), "baseline file must be written");

    let filtered = Command::new(binary())
        .args([
            "scan",
            "--daemon=off",
            "--backend",
            FUNCTIONAL_E2E_BACKEND,
            "--baseline",
            baseline_path.to_str().unwrap(),
            "--format",
            "json",
        ])
        .arg(&fixture)
        .output()
        .expect("baseline-filter scan");
    assert_eq!(
        filtered.status.code(),
        Some(0),
        "baseline-filtered rescan must exit 0; stderr={}",
        String::from_utf8_lossy(&filtered.stderr)
    );
    let findings: serde_json::Value =
        serde_json::from_slice(&filtered.stdout).expect("filtered stdout is JSON");
    assert!(
        findings.as_array().is_some_and(|a| a.is_empty()),
        "baseline must suppress known findings; got {findings:?}"
    );
}

#[test]
fn lockdown_bails_on_verify_flag() {
    let dir = TempDir::new().expect("tempdir");
    let fixture = dir.path().join("planted.txt");
    std::fs::write(
        &fixture,
        concat!("AWS_ACCESS_KEY_ID = \"AKIA", "QYLPMN5HFIQR7XYA\"\n"),
    )
    .unwrap();

    // Lockdown requires RLIMIT_CORE=0 on Linux so coredump_filter checks
    // pass; `prlimit --core=0` sets that for the child without touching
    // the test runner's own limits.
    let mut cmd = Command::new("prlimit");
    cmd.args(["--core=0"])
        .arg(binary())
        .args([
            "scan",
            "--daemon=off",
            "--backend",
            FUNCTIONAL_E2E_BACKEND,
            "--lockdown",
            "--verify",
            "--format",
            "json",
        ])
        .arg(&fixture);
    let output = match cmd.output() {
        Ok(out) => out,
        Err(_) => Command::new(binary())
            .args([
                "scan",
                "--daemon=off",
                "--backend",
                FUNCTIONAL_E2E_BACKEND,
                "--lockdown",
                "--verify",
                "--format",
                "json",
            ])
            .arg(&fixture)
            .output()
            .expect("lockdown+verify scan"),
    };

    assert_eq!(
        output.status.code(),
        Some(2),
        "lockdown+verify must exit 2 (user error); got {:?}",
        output.status.code()
    );
    let combined = format!(
        "{}\n{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        combined.contains("lockdown mode forbids --verify")
            || combined.contains("protections failed to apply"),
        "must refuse outbound verification in lockdown (or fail closed on \
         hardening); got: {combined}"
    );
    if !combined.contains("protections failed to apply") {
        assert!(
            combined.contains("lockdown mode forbids --verify"),
            "when lockdown protections apply, --verify must be refused; got: {combined}"
        );
    }
}

/// Start a real `keyhog daemon` over a Unix socket in a throwaway
/// `XDG_RUNTIME_DIR`, blocking until the socket binds (or panicking on
/// a 30s timeout). Returns the runtime `TempDir` (its `keyhog.sock`
/// lives at `<runtime>/keyhog.sock`) plus the daemon `Child` so the
/// caller can `XDG_RUNTIME_DIR`-pin its scan client to the same socket
/// and tear the daemon down with `stop_daemon` afterwards.
///
/// Factored out of the per-route daemon e2e tests so the start/wait
/// boilerplate isn't copy-pasted (NO DUPLICATION): the ScanPath,
/// ScanText/stdin, example-suppression-wire, and `daemon status`
/// tests all drive the same real listener through this one helper.
#[cfg(unix)]
static DAEMON_E2E_SLOT: std::sync::Mutex<()> = std::sync::Mutex::new(());

#[cfg(unix)]
fn start_daemon() -> (
    std::sync::MutexGuard<'static, ()>,
    TempDir,
    std::process::Child,
) {
    use std::process::{Command, Stdio};
    use std::time::{Duration, Instant};

    let slot = DAEMON_E2E_SLOT
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    let runtime = TempDir::new().expect("runtime dir");
    // Use the embedded corpus (no `--detectors`) so daemon warm identity's
    // detector-rules digest matches the client's `keyhog_core::detector_digest()`
    // stamp. Passing workspace detectors made the daemon advertise
    // `compute_spec_hash` while `keyhog scan --daemon` expected the embedded
    // `<count>-<fnv>` stamp, failing every wire e2e with identity mismatch.
    let mut daemon = Command::new(binary())
        .env("XDG_RUNTIME_DIR", runtime.path())
        .args(["daemon", "start", "--backend", FUNCTIONAL_E2E_BACKEND])
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .expect("spawn daemon");

    let socket = runtime.path().join("keyhog.sock");
    let deadline = Instant::now() + Duration::from_secs(60);
    while !socket.exists() {
        if let Some(status) = daemon.try_wait().expect("poll daemon process") {
            panic!("daemon exited before binding its socket with status {status}");
        }
        if Instant::now() >= deadline {
            let _ = daemon.kill();
            let status = daemon.wait();
            panic!("daemon socket did not appear in time; final child status: {status:?}");
        }
        std::thread::sleep(Duration::from_millis(50));
    }
    (slot, runtime, daemon)
}

/// Tear down a daemon started by `start_daemon`: ask it to stop over
/// the socket, then make sure the child is reaped.
#[cfg(unix)]
fn stop_daemon(runtime: &TempDir, daemon: &mut std::process::Child) {
    use std::process::Command;
    let _ = Command::new(binary())
        .env("XDG_RUNTIME_DIR", runtime.path())
        .args(["daemon", "stop"])
        .output();
    let _ = daemon.kill();
    let _ = daemon.wait();
}

#[cfg(unix)]
#[test]
fn daemon_wire_scan_path_finds_planted_secret() {
    use std::process::Command;

    let dir = TempDir::new().expect("fixture dir");
    let fixture = dir.path().join("daemon_planted.txt");
    std::fs::write(
        &fixture,
        concat!("AWS_ACCESS_KEY_ID = \"ASIA", "Y34FZKBOKMUTVV7A\"\n"),
    )
    .unwrap();

    let (_slot, runtime, mut daemon) = start_daemon();

    let scan = Command::new(binary())
        .env("XDG_RUNTIME_DIR", runtime.path())
        .args(["scan", "--daemon", "--format", "json"])
        .arg(&fixture)
        .output()
        .expect("daemon scan");

    stop_daemon(&runtime, &mut daemon);

    assert_eq!(
        scan.status.code(),
        Some(1),
        "daemon scan must find secret (exit 1); stderr={}",
        String::from_utf8_lossy(&scan.stderr)
    );
    let findings: serde_json::Value =
        serde_json::from_slice(&scan.stdout).expect("daemon stdout is JSON");
    let arr = findings.as_array().expect("array");
    assert!(
        arr.iter().any(|f| matches!(
            f.get("detector_id").and_then(|v| v.as_str()),
            Some("aws-access-key")
        )),
        "daemon wire path must return an AWS finding; got {arr:?}"
    );
}

/// ScanText twin of `daemon_wire_scan_path_finds_planted_secret`.
///
/// `keyhog scan --daemon` has two client routes (subcommands/scan.rs
/// `run_via_daemon`): `--stdin` sends `Request::ScanText`, a single
/// file path sends `Request::ScanPath`. The path route is covered by
/// the test above; this drives the stdin/ScanText route - the
/// stdin / IDE-save fast path the daemon exists for (see the
/// `daemon/protocol.rs` ScanText doc) - over a REAL bound socket
/// rather than the in-memory `tokio::io::duplex` mock the unit test
/// uses. Pipes a planted AWS key into `keyhog scan --daemon --stdin
/// --format json` and asserts exit 1 + the AWS finding came back over
/// the wire.
#[cfg(unix)]
#[test]
fn daemon_wire_scan_stdin_finds_planted_secret() {
    use std::io::Write;
    use std::process::{Command, Stdio};

    let (_slot, runtime, mut daemon) = start_daemon();

    let fixture = concat!("AWS_ACCESS_KEY_ID = \"ASIA", "Y34FZKBOKMUTVV7A\"\n");
    let mut child = Command::new(binary())
        .env("XDG_RUNTIME_DIR", runtime.path())
        .args(["scan", "--daemon", "--stdin", "--format", "json"])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn daemon stdin scan");
    child
        .stdin
        .take()
        .expect("child stdin")
        .write_all(fixture.as_bytes())
        .expect("pipe fixture to stdin");
    let scan = child.wait_with_output().expect("daemon stdin scan output");

    stop_daemon(&runtime, &mut daemon);

    assert_eq!(
        scan.status.code(),
        Some(1),
        "daemon --stdin scan must find secret (exit 1); stderr={}",
        String::from_utf8_lossy(&scan.stderr)
    );
    let findings: serde_json::Value =
        serde_json::from_slice(&scan.stdout).expect("daemon stdin stdout is JSON");
    let arr = findings.as_array().expect("array");
    assert_eq!(
        arr.len(),
        1,
        "daemon ScanText/stdin must resolve the planted AWS key to one finding; got {arr:?}"
    );
    assert!(
        matches!(
            arr[0].get("detector_id").and_then(|v| v.as_str()),
            Some("aws-access-key")
        ),
        "daemon ScanText/stdin wire path must return the named AWS finding, not a generic entropy duplicate; got {arr:?}"
    );
}

/// Daemon telemetry over the real socket on the ScanText/stdin route.
///
/// `daemon/protocol.rs` bumped the wire to v2 specifically so
/// `ScanResults` could carry `engine_example_suppressions` (and
/// `dogfood_events`) back to the client - the suppressed-example
/// counter that drives the reporter's "matched + suppressed N as known
/// examples" summary. That field was previously only round-tripped in
/// the `tokio::io::duplex` unit test (`unit/daemon_wire.rs`), never
/// asserted end-to-end. Here we pipe an AWS-published EXAMPLE token
/// (suppressed by the bundled test-fixture entry on the daemon side)
/// into `keyhog scan --daemon --stdin --format text`, and assert the
/// client reporter distinguishes suppressed-example from a clean repo -
/// which is only possible if the daemon's `engine_example_suppressions`
/// count survived the wire and was merged into the client's telemetry
/// (`run_via_daemon` -> `unwrap_scan_results` -> `add_example_suppressions`).
#[cfg(unix)]
#[test]
fn daemon_wire_stdin_example_suppression_summary_propagates() {
    use std::io::Write;
    use std::process::{Command, Stdio};

    let (_slot, runtime, mut daemon) = start_daemon();

    // AWS-published EXAMPLE credential: matched then suppressed as a
    // known example on the daemon side, so the daemon suppression
    // count - not a finding - is what must reach the client.
    let fixture = "AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE\n";
    let mut child = Command::new(binary())
        .env("XDG_RUNTIME_DIR", runtime.path())
        .args(["scan", "--daemon", "--stdin", "--format", "text"])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn daemon stdin example scan");
    child
        .stdin
        .take()
        .expect("child stdin")
        .write_all(fixture.as_bytes())
        .expect("pipe example fixture to stdin");
    let scan = child
        .wait_with_output()
        .expect("daemon stdin example scan output");

    stop_daemon(&runtime, &mut daemon);

    let stdout = String::from_utf8_lossy(&scan.stdout);
    assert!(
        stdout.contains("example/test key") && stdout.contains("suppressed"),
        "engine_example_suppressions must propagate over the real daemon \
         socket so the daemon client distinguishes suppressed-example from a \
         clean repo. Got stdout: {stdout}"
    );
    assert!(
        !stdout.contains("Your code is clean."),
        "the clean-repo summary must NOT fire when the daemon suppressed an \
         example credential and reported a non-zero daemon count. \
         Got stdout: {stdout}"
    );
}

/// `keyhog daemon status` against a RUNNING daemon over the real
/// socket. The previously-covered daemon e2e only drove start ->
/// `--daemon` scan -> stop; the documented Status payload (args.rs:
/// "uptime, scans served, active scans, and detector count") was only
/// exercised by orphaned adversarial tests for the *absent*-daemon
/// error path. Here we start a daemon, issue one real scan over the
/// socket (so scans-served increments off zero), then run `keyhog
/// daemon status` against the live socket and assert exit 0 + the
/// payload reports scans-served and a real detector count.
#[cfg(unix)]
#[test]
fn daemon_status_reports_payload_after_live_scan() {
    use std::process::Command;

    let dir = TempDir::new().expect("fixture dir");
    let fixture = dir.path().join("daemon_status_planted.txt");
    std::fs::write(
        &fixture,
        concat!("AWS_ACCESS_KEY_ID = \"ASIA", "Y34FZKBOKMUTVV7A\"\n"),
    )
    .unwrap();

    let (_slot, runtime, mut daemon) = start_daemon();

    // One real scan over the socket so the served counter is provably
    // non-zero in the status payload below.
    let scan = Command::new(binary())
        .env("XDG_RUNTIME_DIR", runtime.path())
        .args(["scan", "--daemon", "--format", "json"])
        .arg(&fixture)
        .output()
        .expect("daemon scan before status");
    assert_eq!(
        scan.status.code(),
        Some(1),
        "pre-status daemon scan must find the planted key; stderr={}",
        String::from_utf8_lossy(&scan.stderr)
    );

    let status = Command::new(binary())
        .env("XDG_RUNTIME_DIR", runtime.path())
        .args(["daemon", "status"])
        .output()
        .expect("daemon status");

    stop_daemon(&runtime, &mut daemon);

    assert_eq!(
        status.status.code(),
        Some(0),
        "`daemon status` against a live daemon must exit 0; stderr={}",
        String::from_utf8_lossy(&status.stderr)
    );
    let out = String::from_utf8_lossy(&status.stdout);
    assert!(
        out.contains("scans served"),
        "status payload must report scans-served; got: {out}"
    );
    assert!(
        out.contains("detectors"),
        "status payload must report the detector count; got: {out}"
    );
    // The served counter must reflect the real scan we issued, not a
    // hardcoded zero: "0 scans served" would mean the live Health
    // payload didn't see our request.
    assert!(
        !out.contains("0 scans served"),
        "status must report the scan we issued (non-zero scans-served); got: {out}"
    );
}

#[test]
fn doctor_reports_corpus_and_passes_scan_self_test() {
    // `keyhog doctor` is the install health check. On a healthy host it must
    // exit 0, report the real embedded detector corpus (not 0), and PASS the
    // end-to-end scan self-test (plant -> scan -> match). Asserting the
    // displayed count equals the binary's own embedded count proves the
    // report reflects reality, not a hardcoded banner number.
    let output = Command::new(binary())
        .arg("doctor")
        .output()
        .expect("run keyhog doctor");
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert_eq!(
        output.status.code(),
        Some(0),
        "doctor must exit 0 on a healthy host (PATH warning is non-fatal); stdout:\n{stdout}"
    );
    assert!(
        stdout.contains("self-test"),
        "doctor must run a self-test section; got:\n{stdout}"
    );
    assert!(
        stdout.contains("PASS"),
        "the scan-engine self-test must PASS; got:\n{stdout}"
    );
    // Autoroute calibration coverage: doctor must surface whether the default
    // scan path is calibrated, so a user understands an "autoroute calibration
    // required" exit-2 scan. The section is informational and never flips the
    // exit code (uncalibrated is the expected pre-`--calibrate` state).
    assert!(
        stdout.contains("autoroute") && stdout.contains("calibration"),
        "doctor must report autoroute calibration coverage; got:\n{stdout}"
    );
    let corpus = keyhog_core::embedded_detector_count();
    assert!(corpus > 0, "binary must embed a detector corpus");
    assert!(
        stdout.contains(&corpus.to_string()),
        "doctor must display the real embedded corpus count ({corpus}); got:\n{stdout}"
    );
}

#[test]
fn update_subcommand_is_wired_with_its_flags() {
    // `keyhog update`'s download/replace path is network-bound (it queries the
    // GitHub releases API), so it can't be a deterministic offline snapshot -
    // its pure logic (asset selection, semver compare, executable-magic guard)
    // is unit-tested in subcommands::update. This e2e confirms the subcommand
    // and its flags are actually registered in the CLI (a wiring regression
    // would otherwise only surface when a user runs it).
    let output = Command::new(binary())
        .arg("update")
        .arg("--help")
        .output()
        .expect("run keyhog update --help");
    assert!(
        output.status.success(),
        "`keyhog update --help` must succeed; stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let help = String::from_utf8_lossy(&output.stdout);
    for flag in ["--check", "--version"] {
        assert!(
            help.contains(flag),
            "`keyhog update --help` must document {flag}; got:\n{help}"
        );
    }
    assert!(
        help.contains("SEMVER")
            && help.contains("Canonical SemVer")
            && help.contains("leading `v` is normalized")
            && help.contains("Valid prereleases are accepted"),
        "update help must describe exact accepted version syntax; got:\n{help}"
    );
}

#[test]
fn repair_subcommand_is_wired_with_its_flags() {
    // Like `update`, `repair`'s download/reinstall path is network-bound; its
    // shared logic is unit-tested in crate::installer. This confirms the
    // subcommand + flags are registered.
    let output = Command::new(binary())
        .arg("repair")
        .arg("--help")
        .output()
        .expect("run keyhog repair --help");
    assert!(
        output.status.success(),
        "`keyhog repair --help` must succeed; stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let help = String::from_utf8_lossy(&output.stdout);
    for flag in ["--force", "--version"] {
        assert!(
            help.contains(flag),
            "`keyhog repair --help` must document {flag}; got:\n{help}"
        );
    }
    assert!(
        help.contains("SEMVER")
            && help.contains("Canonical SemVer")
            && help.contains("leading `v` is normalized")
            && help.contains("Valid prereleases are accepted"),
        "repair help must describe exact accepted version syntax; got:\n{help}"
    );
}

#[test]
fn maintenance_version_validation_rejects_hostile_values_before_execution() {
    // Why: clap is the earliest production boundary; malformed values must
    // fail there rather than reaching either resolver URL construction or I/O.
    for command in ["update", "repair"] {
        for invalid in ["v1.2.3/../../latest", "v1.2.3?draft=true", "v1.2.3-rc..1"] {
            let output = Command::new(binary())
                .args([command, "--version", invalid])
                .output()
                .unwrap_or_else(|error| panic!("run keyhog {command}: {error}"));
            assert_eq!(
                output.status.code(),
                Some(2),
                "{command} must reject invalid version `{invalid}` during parsing"
            );
            let stderr = String::from_utf8_lossy(&output.stderr);
            assert!(
                stderr.contains(invalid)
                    && stderr.contains("not canonical SemVer")
                    && stderr.contains("--version v1.2.3"),
                "{command} must name the invalid value and remediation: {stderr}"
            );
        }
    }
}

#[test]
fn uninstall_dry_run_does_not_remove_the_binary() {
    // Safety contract: `keyhog uninstall` without `--yes` must be a no-op dry
    // run - it must NOT delete the binary. (Running it against the test binary
    // is safe precisely because of this guarantee; a regression here would
    // delete the test runner's own binary.)
    let bin = binary();
    let output = Command::new(&bin)
        .arg("uninstall")
        .output()
        .expect("run keyhog uninstall");
    assert!(
        output.status.success(),
        "dry-run uninstall must exit 0; stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let out = String::from_utf8_lossy(&output.stdout).to_lowercase();
    assert!(
        out.contains("dry run"),
        "uninstall without --yes must announce it's a dry run; got:\n{out}"
    );
    assert!(
        bin.exists(),
        "dry-run uninstall MUST NOT delete the binary at {}",
        bin.display()
    );
}

/// Write `content` + a `.keyhog.toml` of `config` into a temp dir, scan the
/// dir, return (stdout, stderr, exit-code). Exercises the real config-load
/// path (`.keyhog.toml` discovery + `apply_config_file`).
fn scan_dir_with_config(
    content: &str,
    config: &str,
    extra: &[&str],
) -> (String, String, Option<i32>) {
    let dir = TempDir::new().expect("tempdir");
    std::fs::write(dir.path().join("planted.txt"), content).expect("write fixture");
    std::fs::write(dir.path().join(".keyhog.toml"), config).expect("write config");
    let output = Command::new(binary())
        .args([
            "scan",
            "--daemon=off",
            "--backend",
            FUNCTIONAL_E2E_BACKEND,
            "--format",
            "json",
        ])
        .args(extra)
        .arg(dir.path())
        .output()
        .expect("spawn keyhog scan");
    (
        String::from_utf8_lossy(&output.stdout).into_owned(),
        String::from_utf8_lossy(&output.stderr).into_owned(),
        output.status.code(),
    )
}

#[test]
fn config_detector_disable_drops_findings() {
    // `[detector.<id>] enabled = false` must actually drop the detector. This
    // README-documented toggle was parsed and SILENTLY IGNORED before being
    // wired, so a user disabling a noisy detector kept seeing it fire. The
    // Accelerated and portable paths share the TOML `aws-access-key` id.
    let aws = concat!("AWS_ACCESS_KEY_ID = \"AKIA", "QYLPMN5HFIQR7XYA\"\n");
    let (_o, _e, before) = scan_dir_with_config(aws, "", &[]);
    assert_eq!(before, Some(1), "baseline: the AWS key must be found");
    let (out, _e, code) = scan_dir_with_config(
        aws,
        "[detector.aws-access-key]\nenabled = false\n[detector.entropy-api-key]\nenabled = false\n",
        &[],
    );
    assert_eq!(
        code,
        Some(0),
        "disabling the AWS detectors via .keyhog.toml must yield zero findings; stdout={out}"
    );
}

#[test]
fn config_detector_disable_all_loaded_detectors_fails_closed() {
    let dir = TempDir::new().expect("tempdir");
    let detectors_dir = dir.path().join("detectors");
    std::fs::create_dir_all(&detectors_dir).expect("mkdir detectors");
    std::fs::write(
        detectors_dir.join("demo-only.toml"),
        r#"
        [detector]
        id = "demo-only"
        name = "Demo Only"
        service = "demo"
        severity = "high"
        ml = { match_mode = "disabled", entropy_mode = "disabled", weight = 0.0, context_radius_lines = 0 }
        match_confidence = { literal_prefix_weight = 0.35, context_anchor_weight = 0.20, entropy_weight = 0.20, high_entropy_partial_weight = 0.12, moderate_entropy_threshold = 3.0, moderate_entropy_weight = 0.05, low_entropy_penalty_floor = 2.0, low_entropy_min_match_length = 10, low_entropy_penalty_multiplier = 0.60, keyword_nearby_weight = 0.10, sensitive_file_weight = 0.10, companion_weight = 0.05, very_high_entropy_margin = 1.3, named_anchor_floor = 0.50, assignment_context_multiplier = 1.0, string_literal_context_multiplier = 0.9, unknown_context_multiplier = 0.8, documentation_context_multiplier = 0.3, comment_context_multiplier = 0.4, test_context_multiplier = 0.3, encrypted_context_multiplier = 0.05, soft_context_suppression_threshold = 0.5, encrypted_context_suppression_threshold = 0.8, post_match = { placeholder_multiplier = 0.05, minimum_byte_diversity = 0.1, low_diversity_multiplier = 0.1, maximum_repeat_ratio = 0.8, degenerate_run_min_length = 10, degenerate_repeat_multiplier = 0.1, fixture_path_multiplier = 0.5, ml_context_reapply_below = 0.95 } }
        keywords = ["demo_secret_"]

        [[detector.patterns]]
        regex = "demo_secret_[A-Z0-9]{8}"
        "#,
    )
    .expect("write detector");
    std::fs::write(
        dir.path().join("planted.txt"),
        "token = demo_secret_ABCD1234\n",
    )
    .expect("write fixture");
    std::fs::write(
        dir.path().join(".keyhog.toml"),
        "[detector.demo-only]\nenabled = false\n",
    )
    .expect("write config");

    let output = Command::new(binary())
        .args([
            "scan",
            "--daemon=off",
            "--backend",
            FUNCTIONAL_E2E_BACKEND,
            "--format",
            "json",
            "--detectors",
        ])
        .arg(&detectors_dir)
        .arg(dir.path())
        .output()
        .expect("spawn keyhog scan");
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    assert_eq!(
        output.status.code(),
        Some(2),
        "disabling the entire loaded detector corpus must be a user-visible scan error, not a clean no-findings scan.\n--- stdout ---\n{stdout}\n--- stderr ---\n{stderr}"
    );
    assert!(
        stderr.contains("all 1 loaded detector(s) were disabled")
            && stderr.contains("demo-only")
            && stderr.contains("Refusing to scan with no detectors loaded"),
        "stderr must explain the zero-detector corpus and the disabled id.\n--- stderr ---\n{stderr}"
    );
    assert!(
        !stdout.contains("demo_secret_ABCD1234"),
        "failed setup must not emit a misleading finding payload after refusing the empty detector corpus"
    );
}

#[test]
fn config_detector_min_confidence_floor_drops_findings() {
    // `[detector.<id>] min_confidence = <f>` is a per-detector confidence
    // floor: a finding from that detector below the floor is dropped, taking
    // precedence over the global --min-confidence. README-documented but
    // parsed-and-silently-ignored before this wiring (it was decoded into
    // DetectorSection.min_confidence and never consumed).
    let dir = TempDir::new().expect("tempdir");
    let detectors_dir = dir.path().join("detectors");
    std::fs::create_dir_all(&detectors_dir).expect("mkdir detectors");
    std::fs::write(
        detectors_dir.join("demo-only.toml"),
        r#"
        [detector]
        id = "demo-only"
        name = "Demo Only"
        service = "demo"
        severity = "high"
        ml = { match_mode = "disabled", entropy_mode = "disabled", weight = 0.0, context_radius_lines = 0 }
        match_confidence = { literal_prefix_weight = 0.35, context_anchor_weight = 0.20, entropy_weight = 0.20, high_entropy_partial_weight = 0.12, moderate_entropy_threshold = 3.0, moderate_entropy_weight = 0.05, low_entropy_penalty_floor = 2.0, low_entropy_min_match_length = 10, low_entropy_penalty_multiplier = 0.60, keyword_nearby_weight = 0.10, sensitive_file_weight = 0.10, companion_weight = 0.05, very_high_entropy_margin = 1.3, named_anchor_floor = 0.50, assignment_context_multiplier = 1.0, string_literal_context_multiplier = 0.9, unknown_context_multiplier = 0.8, documentation_context_multiplier = 0.3, comment_context_multiplier = 0.4, test_context_multiplier = 0.3, encrypted_context_multiplier = 0.05, soft_context_suppression_threshold = 0.5, encrypted_context_suppression_threshold = 0.8, post_match = { placeholder_multiplier = 0.05, minimum_byte_diversity = 0.1, low_diversity_multiplier = 0.1, maximum_repeat_ratio = 0.8, degenerate_run_min_length = 10, degenerate_repeat_multiplier = 0.1, fixture_path_multiplier = 0.5, ml_context_reapply_below = 0.95 } }
        keywords = ["demo_secret_"]

        [[detector.patterns]]
        regex = "demo_secret_[A-Z0-9]{8}"
        "#,
    )
    .expect("write detector");
    std::fs::write(
        dir.path().join("planted.txt"),
        "token = demo_secret_ABCD1234\n",
    )
    .expect("write fixture");

    let run = |config: &str| {
        std::fs::write(dir.path().join(".keyhog.toml"), config).expect("write config");
        let output = Command::new(binary())
            .args([
                "scan",
                "--daemon=off",
                "--backend",
                FUNCTIONAL_E2E_BACKEND,
                "--format",
                "json",
                "--detectors",
            ])
            .arg(&detectors_dir)
            .arg(dir.path())
            .output()
            .expect("spawn keyhog scan");
        (
            String::from_utf8_lossy(&output.stdout).into_owned(),
            String::from_utf8_lossy(&output.stderr).into_owned(),
            output.status.code(),
        )
    };

    // Baseline: the custom detector emits the planted token at confidence 0.5.
    let (out_base, _e, before) = run("");
    assert_eq!(
        before,
        Some(1),
        "baseline finding must fire; stdout={out_base}"
    );
    assert!(
        out_base.contains("\"confidence\":0.5"),
        "fixture must stay below the high floor so this test proves filtering; stdout={out_base}"
    );

    let (out_hi, _e, code_hi) = run("[detector.demo-only]\nmin_confidence = 0.6\n");
    assert_eq!(
        code_hi,
        Some(0),
        "a per-detector min_confidence floor above the finding confidence must suppress it; stdout={out_hi}"
    );

    let (_out_lo, _e, code_lo) = run("[detector.demo-only]\nmin_confidence = 0.4\n");
    assert_eq!(
        code_lo,
        Some(1),
        "a per-detector min_confidence floor below the finding confidence must keep it"
    );

    // Lowering override: the detector now self-declares a floor above the
    // finding's 0.5 score. The operator's 0.4 override must be compiled into the
    // active detector policy before scanning; applying it only after the engine
    // would be too late because the 0.8 detector floor would already drop the
    // candidate.
    std::fs::write(
        detectors_dir.join("demo-only.toml"),
        r#"
        [detector]
        id = "demo-only"
        name = "Demo Only"
        service = "demo"
        severity = "high"
        ml = { match_mode = "disabled", entropy_mode = "disabled", weight = 0.0, context_radius_lines = 0 }
        match_confidence = { literal_prefix_weight = 0.35, context_anchor_weight = 0.20, entropy_weight = 0.20, high_entropy_partial_weight = 0.12, moderate_entropy_threshold = 3.0, moderate_entropy_weight = 0.05, low_entropy_penalty_floor = 2.0, low_entropy_min_match_length = 10, low_entropy_penalty_multiplier = 0.60, keyword_nearby_weight = 0.10, sensitive_file_weight = 0.10, companion_weight = 0.05, very_high_entropy_margin = 1.3, named_anchor_floor = 0.50, assignment_context_multiplier = 1.0, string_literal_context_multiplier = 0.9, unknown_context_multiplier = 0.8, documentation_context_multiplier = 0.3, comment_context_multiplier = 0.4, test_context_multiplier = 0.3, encrypted_context_multiplier = 0.05, soft_context_suppression_threshold = 0.5, encrypted_context_suppression_threshold = 0.8, post_match = { placeholder_multiplier = 0.05, minimum_byte_diversity = 0.1, low_diversity_multiplier = 0.1, maximum_repeat_ratio = 0.8, degenerate_run_min_length = 10, degenerate_repeat_multiplier = 0.1, fixture_path_multiplier = 0.5, ml_context_reapply_below = 0.95 } }
        min_confidence = 0.8
        keywords = ["demo_secret_"]

        [[detector.patterns]]
        regex = "demo_secret_[A-Z0-9]{8}"
        "#,
    )
    .expect("rewrite detector with a self-declared floor");

    let (out_self, err_self, code_self) = run("");
    assert_eq!(
        code_self,
        Some(0),
        "the detector's own 0.8 floor must suppress its 0.5 finding; stdout={out_self}\nstderr={err_self}"
    );
    let (out_lowered, err_lowered, code_lowered) =
        run("[detector.demo-only]\nmin_confidence = 0.4\n");
    assert_eq!(
        code_lowered,
        Some(1),
        "an operator floor below the detector default must preserve the 0.5 finding before engine adjudication; stdout={out_lowered}\nstderr={err_lowered}"
    );
    assert!(
        out_lowered.contains("\"detector_id\":\"demo-only\""),
        "the lowered-floor run must emit a finding attributed to the custom detector; stdout={out_lowered}"
    );

    for invalid in ["5.0", "-1.0", "nan", "inf"] {
        let config = format!("[detector.demo-only]\nmin_confidence = {invalid}\n");
        let (stdout, stderr, code) = run(&config);
        assert_eq!(
            code,
            Some(2),
            "invalid per-detector floor {invalid} must fail closed; stdout={stdout}\nstderr={stderr}"
        );
        assert!(
            stderr.contains("min_confidence must be between 0.0 and 1.0"),
            "invalid per-detector floor must state the accepted range; stderr={stderr}"
        );
    }
}

#[test]
fn config_lockdown_require_refuses_without_flag() {
    // `[lockdown] require = true` is a fail-closed security control: refuse to
    // run unless --lockdown is passed (README: "refuse to run without
    // --lockdown"). It was parsed and silently ignored, so a repo that believed
    // it mandated lockdown ran unprotected. The refusal must be explicit.
    let (_o, err, code) =
        scan_dir_with_config("ordinary content\n", "[lockdown]\nrequire = true\n", &[]);
    assert_ne!(
        code,
        Some(0),
        "a repo whose .keyhog.toml requires lockdown must NOT run without --lockdown"
    );
    assert!(
        err.to_lowercase().contains("lockdown"),
        "the refusal must name lockdown so the operator knows why; stderr={err}"
    );
}

/// `--precision` is a high-precision mass-scan preset: it must keep genuine
/// high-confidence secrets while dropping weaker (sub-0.85) findings that the
/// default floor admits. The AWS secret key scores far higher than a generic
/// password assignment, so precision keeps the former and drops the latter.
#[test]
fn precision_mode_keeps_strong_drops_weak() {
    let fixture = concat!(
        "aws_secret_access_key = \"kP8xQ2mNvR7tZ4wL9bYsH3jD6fG1cA0eXuViK5oT\"\n",
        "DATABASE_PASSWORD = \"hunter2hunter2\"\n",
    );
    let (def_out, _e, def_code) = scan_text_file(fixture, &[]);
    let (prec_out, _e2, prec_code) = scan_text_file(fixture, &["--precision"]);
    assert_eq!(def_code, Some(1), "default scan must report findings");
    assert_eq!(
        prec_code,
        Some(1),
        "precision keeps the strong finding, so it must retain the findings exit"
    );
    let def_findings = parse_json_array(&def_out, "default precision-mode scan");
    let prec_findings = parse_json_array(&prec_out, "explicit precision-mode scan");
    let def: Vec<String> = def_findings
        .iter()
        .filter_map(|finding| {
            finding
                .get("detector_id")
                .and_then(|value| value.as_str())
                .map(String::from)
        })
        .collect();
    let prec: Vec<String> = prec_findings
        .iter()
        .filter_map(|finding| {
            finding
                .get("detector_id")
                .and_then(|value| value.as_str())
                .map(String::from)
        })
        .collect();

    // `generic-password.toml` is the detector-data owner for PASSWORD-family
    // assignments. The previous short dictionary value scores 0.879 under the
    // structural-password policy and therefore intentionally clears precision's
    // 0.85 floor. This planted weak value scores below that floor, so it proves
    // the mode transition rather than expecting the product to hide a strong hit.
    assert_eq!(
        def.len(),
        2,
        "default mode should surface exactly the weak generic password and the AWS secret; got {def:?}"
    );
    assert!(
        def.iter().any(|d| d == "aws-secret-access-key"),
        "default must find the secret key; got {def:?}"
    );
    assert!(
        def.iter().any(|d| d == "generic-password"),
        "default must find the weak generic password; got {def:?}"
    );
    let weak = def_findings
        .iter()
        .find(|finding| {
            finding.get("detector_id").and_then(|v| v.as_str()) == Some("generic-password")
        })
        .expect("default generic-password finding");
    assert_eq!(
        weak.get("credential_redacted").and_then(|v| v.as_str()),
        Some("h...2"),
        "the weak finding must retain the core redaction shape"
    );
    assert_eq!(
        weak.pointer("/location/line").and_then(|v| v.as_u64()),
        Some(2),
        "the weak finding must stay on the PASSWORD assignment"
    );
    assert_eq!(
        weak.pointer("/location/offset").and_then(|v| v.as_u64()),
        Some(88),
        "the weak finding span must start at the planted password value"
    );
    let weak_confidence = weak
        .get("confidence")
        .and_then(|v| v.as_f64())
        .expect("weak finding confidence");
    assert!(
        (0.5..0.85).contains(&weak_confidence),
        "the fixture must remain reportable by default and below precision's floor; got {weak_confidence}"
    );
    assert!(
        prec.iter().any(|d| d == "aws-secret-access-key"),
        "precision must KEEP the high-confidence secret key; got {prec:?}"
    );
    assert_eq!(
        prec.len(),
        1,
        "precision must retain exactly the strong finding; got {prec:?}"
    );
    assert!(
        prec.len() < def.len(),
        "precision must be strictly tighter than default; default={def:?} precision={prec:?}"
    );
    assert!(
        !prec.iter().any(|d| d == "generic-password"),
        "precision must drop the weaker generic-password finding (below the 0.85 bar); got {prec:?}"
    );
}

/// The scan modes are mutually exclusive: clap must reject `--precision --fast`
/// rather than silently letting one win.
#[test]
fn precision_mode_conflicts_with_fast() {
    let (_o, err, code) = scan_text_file("ordinary content\n", &["--precision", "--fast"]);
    assert_eq!(
        code,
        Some(2),
        "clap usage error (exit 2) expected for conflicting --precision --fast; got {code:?}"
    );
    assert!(
        err.contains("cannot be used with") || err.to_lowercase().contains("precision"),
        "the usage error must name the conflict; stderr={err}"
    );
}