keyhog 0.5.44

keyhog detects leaked credentials in source trees, git history, archives, and remote sources
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
//! FILE_GATE micro tests for cli crate src files.

use clap::Parser;
use keyhog::args::{Cli, ScanArgs};
use keyhog::testing::{CliTestApi as _, API};
// The `keyhog::daemon::*` modules are unix-only (Unix-domain sockets).
// Gate the imports and the daemon_* tests below so the file compiles
// on Windows.
#[cfg(unix)]
use keyhog::daemon::default_socket_path;
#[cfg(unix)]
use keyhog::daemon::protocol::{Request, Response, MAX_FRAME_BYTES, WIRE_VERSION};
use keyhog_core::{Chunk, ChunkMetadata, MatchLocation, RawMatch, SensitiveString, Severity};
use std::sync::Arc;
use std::time::{Duration, Instant};

// ── crates/cli/src/lib.rs ─────────────────────────────────────────────
#[test]
fn lib_happy() {
    let _guard = API.scan_runtime_guard_for_test();
    API.reset_scan_runtime_state_for_test(&_guard);
    assert_eq!(API.scanned_chunks(&_guard), 0);
}
#[test]
fn lib_error() {
    let _guard = API.scan_runtime_guard_for_test();
    API.reset_scan_runtime_state_for_test(&_guard);
    assert!(!API.scanner_panicked(&_guard));
}

#[test]
fn lib_scan_failure_counters_have_typed_owner() {
    let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
    let lib = std::fs::read_to_string(root.join("src/lib.rs")).expect("read cli lib");
    let dispatch =
        std::fs::read_to_string(root.join("src/orchestrator/dispatch.rs")).expect("read dispatch");
    let fused = std::fs::read_to_string(root.join("src/orchestrator/dispatch/fused.rs"))
        .expect("read fused dispatch");

    assert!(
        lib.contains("enum ScanFailureEvent") && lib.contains("struct RecordedScanFailureEvent"),
        "CLI scan failures need a typed event owner and must-use receipt"
    );
    for (name, source) in [
        ("dispatch", dispatch.as_str()),
        ("fused dispatch", fused.as_str()),
    ] {
        assert!(
            source.contains("record_source_error") || source.contains("record_scanner_panic"),
            "{name} must record failure state through typed recorders"
        );
        assert!(
            !source.contains("SOURCE_ERRORS.fetch_add")
                && !source.contains("FAILED_SOURCES.fetch_add")
                && !source.contains("INCREMENTAL_CACHE_ERRORS.fetch_add")
                && !source.contains("SCANNER_PANICKED.store"),
            "{name} must not mutate scan-failure counters directly"
        );
    }
    assert!(
        dispatch.contains("record_incremental_cache_persist_failed()")
            && dispatch.contains("could not be persisted"),
        "incremental cache persistence failures must go through the typed failure owner and stderr"
    );
    assert!(
        dispatch.contains("fn record_oversized_coalesced_chunk_skip(")
            && fused.contains("super::classify_source_chunk"),
        "coalesced and fused oversized chunk drops must share one loud source-error recorder"
    );
    assert!(
        fused.contains("fused source drain thread panicked")
            && fused.contains("record_scanner_panic()")
            && !fused.contains("let _ = drain.join()"),
        "fused dispatch must fail loud when the source drain thread panics, not ignore the join result"
    );
    assert!(
        dispatch.contains("fn filesystem_source_skipped_unchanged")
            && dispatch.contains("skipped_unchanged_count")
            && dispatch.contains(
                "self.skipped_unchanged += filesystem_source_skipped_unchanged(source.as_ref())"
            )
            && fused.contains("super::filesystem_source_skipped_unchanged(source.as_ref())")
            && fused.contains("drain_skipped_unchanged.fetch_add(source_skipped"),
        "coalesced and fused dispatch must include file-level Merkle skips from FilesystemSource, not only chunk-level skips"
    );
}

#[test]
fn scan_exit_precedence_keeps_system_failure_above_source_coverage_gap() {
    let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
    let run = std::fs::read_to_string(root.join("src/orchestrator/run.rs")).expect("read run");
    let findings_pos = run
        .find("} else if outcome.has_new_entries {")
        .expect("findings exit branch");
    let incremental_pos = run
        .find("} else if outcome.incremental_cache_failed {")
        .expect("incremental cache exit branch");
    let source_gap_pos = run
        .find("} else if outcome.source_coverage_incomplete {")
        .expect("source coverage exit branch");

    assert!(
        findings_pos < incremental_pos && incremental_pos < source_gap_pos,
        "exit precedence must be live -> panic -> findings -> system/cache failure -> \
         source coverage failure. A source coverage warning must not mask a system \
         cache failure when there are no findings."
    );
    assert!(
        run.contains("crate::reporting::CoverageCounts::current().fail_class_total()"),
        "source, scanner, and orchestrator coverage gaps must reach the canonical FAIL-class total before a clean-looking scan can exit successfully"
    );
    let reporting = std::fs::read_to_string(root.join("src/orchestrator/reporting.rs"))
        .expect("read terminal reporting");
    let report = std::fs::read_to_string(root.join("src/reporting.rs")).expect("read report");
    // Both surfaces now render the ONE canonical coverage-gap set: the terminal
    // summary iterates `CoverageGapKind::ALL`, and each category's terminal
    // (human) and structured (SARIF) wording lives with the kind in reporting.rs.
    // Assert the terminal summary renders the canonical set, and that the generic
    // source-error category carries BOTH its terminal and structured wording
    // there, so a partial source can never be surfaced on one summary and
    // silently dropped from the other.
    assert!(
        reporting.contains("CoverageGapKind::ALL") && reporting.contains("human_reason"),
        "terminal coverage summary must render the canonical CoverageGapKind set"
    );
    assert!(
        report.contains("source error row(s) emitted")
            && report.contains("requested input was NOT fully scanned"),
        "canonical set must carry the terminal (human) wording for generic source errors"
    );
    assert!(
        report.contains("source emitted error rows")
            && report.contains("requested input was not fully scanned"),
        "canonical set must carry the structured (SARIF) wording for generic source errors"
    );
}

#[test]
fn git_object_coverage_gaps_are_reported_separately() {
    let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
    let run = std::fs::read_to_string(root.join("src/orchestrator/run.rs")).expect("read run");
    let reporting = std::fs::read_to_string(root.join("src/orchestrator/reporting.rs"))
        .expect("read reporting");
    let report = std::fs::read_to_string(root.join("src/reporting.rs")).expect("read report");

    assert!(
        report.contains("Self::GitObjectUnreadable => c.git_object_unreadable"),
        "git object drops must flow from the central skip snapshot into their distinct coverage category"
    );
    assert!(
        reporting.contains("CoverageGapKind::ALL") && reporting.contains("kind.severity()"),
        "terminal summary must render the canonical set (which surfaces the Git-object gap as its own category, not lumped under unreadable files)"
    );
    assert!(
        report.contains("Git object(s) NOT scanned")
            && report.contains("Git object unreadable or wrong object kind")
            && report.contains("c.git_object_unreadable"),
        "canonical set must surface the Git-object gap separately with both terminal and structured wording, keyed off the central skip snapshot"
    );
}

#[test]
fn scan_runtime_reset_clears_process_global_scan_state() {
    let _guard = API.scan_runtime_guard_for_test();
    API.seed_scan_runtime_state_for_test(&_guard);
    let seeded = API.scan_runtime_snapshot(&_guard);
    assert!(
        seeded.scanned_chunks > 0
            && seeded.total_chunks > 0
            && seeded.findings_count > 0
            && seeded.gpu_scanned_chunks > 0
            && seeded.source_errors > 0
            && seeded.failed_sources > 0
            && seeded.incremental_cache_errors > 0
            && seeded.scanner_panicked
            && seeded.dogfood_enabled
            && seeded.example_suppressions > 0,
        "test setup must seed every runtime counter that can leak across scans: {seeded:?}"
    );

    API.reset_scan_runtime_state_for_test(&_guard);

    assert_eq!(
        API.scan_runtime_snapshot(&_guard),
        keyhog::testing::ScanRuntimeSnapshot::default(),
        "per-scan runtime reset must clear CLI totals, failure flags, scanner dogfood state, \
         suppression counts, and scanner coverage-gap counters"
    );
}

#[test]
fn scan_runtime_test_facade_guards_in_process_global_state() {
    let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
    let testing = std::fs::read_to_string(root.join("src/testing.rs")).expect("read test facade");

    assert!(
        testing.contains("#[must_use = \"hold ScanRuntimeGuard"),
        "ScanRuntimeGuard must be must-use so ignored guard acquisition is a warning"
    );
    for required in [
        "fn report_findings(\n        &self,\n        findings: &[VerifiedFinding],\n        args: &ScanArgs,\n        _guard: &ScanRuntimeGuard,",
        "fn scan_orchestrator_scan_sources_for_test(\n        &self,\n        orchestrator: &ScanOrchestrator,\n        sources: Vec<Box<dyn Source>>,\n        show_progress: bool,\n        merkle: Option<Arc<keyhog_core::MerkleIndex>>,\n        _guard: &ScanRuntimeGuard,",
        "fn seed_scan_runtime_state_for_test(&self, _guard: &ScanRuntimeGuard)",
        "fn reset_scan_runtime_state_for_test(&self, _guard: &ScanRuntimeGuard)",
        "fn scan_runtime_snapshot(&self, _guard: &ScanRuntimeGuard)",
    ] {
        assert!(
            testing.contains(required),
            "CLI test facade must require ScanRuntimeGuard for process-global scan state seam: {required}"
        );
    }

    let orchestrator_dir = root.join("tests/unit/orchestrator");
    for entry in std::fs::read_dir(&orchestrator_dir).expect("read orchestrator tests") {
        let entry = entry.expect("orchestrator test entry");
        let path = entry.path();
        if path.extension().and_then(|ext| ext.to_str()) != Some("rs")
            || path.file_name().and_then(|name| name.to_str()) == Some("support.rs")
        {
            continue;
        }
        let source = std::fs::read_to_string(&path).expect("read orchestrator test");
        assert!(
            !source.contains("scan_orchestrator_scan_sources_for_test("),
            "{} must use support::scan_sources_for_test so the scan-runtime guard is held",
            path.display()
        );
    }
}

#[test]
fn scan_runtime_guard_recovers_from_poisoned_test_lock() {
    let joined = std::thread::spawn(|| {
        let _guard = API.scan_runtime_guard_for_test();
        panic!("poison CLI scan-runtime test lock");
    })
    .join();
    assert!(
        joined.is_err(),
        "poisoning setup should panic inside thread"
    );

    let guard = API.scan_runtime_guard_for_test();
    API.reset_scan_runtime_state_for_test(&guard);
    assert_eq!(API.scan_runtime_snapshot(&guard), Default::default());
}

#[test]
fn scan_runtime_reset_runs_before_dogfood_enablement() {
    let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
    let run = std::fs::read_to_string(root.join("src/orchestrator/run.rs")).expect("read run");
    let lib = std::fs::read_to_string(root.join("src/lib.rs")).expect("read cli lib");

    let reset_pos = run
        .find("reset_scan_runtime_state();")
        .expect("run boundary must reset process-global scan state");
    let dogfood_pos = run
        .find("enable_dogfood();")
        .expect("dogfood enablement still happens in run");
    assert!(
        reset_pos < dogfood_pos,
        "reset must happen before --dogfood enablement so stale dogfood state is cleared \
         without disabling the current scan's requested trace"
    );
    for token in [
        "SCANNED_CHUNKS.store(0",
        "TOTAL_CHUNKS.store(0",
        "FINDINGS_COUNT.store(0",
        "GPU_SCANNED_CHUNKS.store(0",
        "SOURCE_ERRORS.store(0",
        "FAILED_SOURCES.store(0",
        "INCREMENTAL_CACHE_ERRORS.store(0",
        "SCANNER_PANICKED.store(false",
        "keyhog_scanner::telemetry::reset_for_scan()",
    ] {
        assert!(
            lib.contains(token),
            "reset_scan_runtime_state must clear {token}"
        );
    }
}

#[test]
fn ak_p4_cli_hot_paths_stay_linear() {
    let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
    let orchestrator =
        std::fs::read_to_string(root.join("src/orchestrator/mod.rs")).expect("read orchestrator");
    let scan =
        std::fs::read_to_string(root.join("src/subcommands/scan.rs")).expect("read scan command");

    assert!(
        orchestrator.contains("detectors.retain(|d| !disabled_detectors.contains(d.id.as_str()))"),
        "disabled detector filtering must use the resolved HashSet, not a linear search per detector"
    );
    assert!(
        !orchestrator.contains("disabled_detectors.iter().any(|id| id == &d.id)"),
        "disabled detector filtering must not regress to O(detectors * disabled ids)"
    );
    assert!(
        scan.contains("let filesystem_source = std::sync::Arc::<str>::from(\"filesystem\");")
            && scan.contains("m.location.source = filesystem_source.clone();"),
        "daemon scan-path suppression normalization must hoist the filesystem Arc outside the finding loop"
    );
    assert!(
        !scan.contains("m.location.source = std::sync::Arc::from(\"filesystem\");"),
        "daemon scan-path suppression normalization must not allocate a fresh Arc per finding"
    );
}

// ── crates/cli/src/main.rs ────────────────────────────────────────────
#[test]
fn main_happy() {
    let cli = Cli::try_parse_from(["keyhog", "--version"]).unwrap();
    assert!(cli.version);
}
#[test]
fn main_error() {
    assert!(Cli::try_parse_from(["keyhog", "--bad-flag"]).is_err());
}

// ── crates/cli/src/args.rs ────────────────────────────────────────────
#[test]
fn args_happy() {
    let args = ScanArgs::try_parse_from(["scan", "."]).unwrap();
    assert_eq!(args.input, vec![std::path::PathBuf::from(".")]);
}
#[test]
fn args_error() {
    assert!(ScanArgs::try_parse_from(["scan", "--min-confidence", "not-a-float"]).is_err());
}

// ── crates/cli/src/baseline.rs ────────────────────────────────────────
#[test]
fn baseline_happy() {
    let baseline = API.baseline_from_findings(&[]);
    assert!(baseline.entries.is_empty());
}
#[test]
fn baseline_error() {
    assert!(API
        .baseline_load(std::path::Path::new("/nonexistent/baseline.json"))
        .is_err());
}

// ── crates/cli/src/benchmark.rs ───────────────────────────────────────
#[test]
fn benchmark_happy() {
    assert!(!API.format_gpu_summary().is_empty());
}

// ── crates/cli/src/config.rs ──────────────────────────────────────────
#[test]
fn config_happy() {
    let dir = tempfile::tempdir().unwrap();
    assert!(API.find_config_file(Some(dir.path())).is_none());
}
#[test]
fn config_error() {
    assert!(API
        .find_config_file(Some(std::path::Path::new("/nonexistent")))
        .is_none());
}

// ── crates/cli/src/daemon/mod.rs ──────────────────────────────────────
// Daemon tests are unix-only - see file header.
#[cfg(unix)]
#[test]
fn daemon_mod_happy() {
    let path = default_socket_path();
    assert!(!path.as_os_str().is_empty());
}

// ── crates/cli/src/daemon/client.rs ─────────────────────────────────────
#[cfg(unix)]
#[test]
fn daemon_client_happy() {
    let path = default_socket_path();
    assert!(path.to_string_lossy().contains("keyhog") || path.ends_with(".sock"));
}

// ── crates/cli/src/daemon/frame.rs ──────────────────────────────────────
#[cfg(unix)]
#[test]
fn daemon_frame_happy() {
    let json = serde_json::to_string(&Request::Hello).unwrap();
    assert!(json.contains("hello"));
}
#[cfg(unix)]
#[test]
fn daemon_frame_error() {
    let json = serde_json::to_string(&Response::Hello {
        wire_version: WIRE_VERSION,
        keyhog_version: "0.0.0".into(),
        git_hash: "test".into(),
        detector_rules_digest: "test".into(),
        backend_policy: "autoroute".into(),
        detector_count: 0,
        uptime_secs: 0,
    })
    .unwrap();
    assert!(json.contains("wire_version"));
}

// ── crates/cli/src/daemon/protocol.rs ───────────────────────────────────
#[cfg(unix)]
#[test]
fn daemon_protocol_happy() {
    assert_eq!(WIRE_VERSION, 6);
}
#[cfg(unix)]
#[test]
fn daemon_protocol_error() {
    assert!(MAX_FRAME_BYTES > 0);
}

// ── crates/cli/src/daemon/server.rs ─────────────────────────────────────
#[cfg(unix)]
#[test]
fn daemon_server_happy() {
    let path = default_socket_path();
    assert!(!path.as_os_str().is_empty());
}
#[cfg(unix)]
#[test]
fn daemon_server_error() {
    assert_ne!(WIRE_VERSION, 0);
}

// ── crates/cli/src/inline_suppression.rs ──────────────────────────────
#[test]
fn inline_suppression_happy() {
    let m = RawMatch {
        detector_id: Arc::from("demo"),
        detector_name: Arc::from("Demo"),
        service: Arc::from("demo"),
        severity: Severity::Low,
        credential: keyhog_core::SensitiveString::from("abc"),
        credential_hash: [7u8; 32].into(),
        companions: Default::default(),
        location: MatchLocation {
            source: Arc::from("stdin"),
            file_path: None,
            line: None,
            offset: 0,
            commit: None,
            author: None,
            date: None,
        },
        entropy: None,
        confidence: None,
    };
    assert_eq!(API.filter_inline_suppressions(vec![m]).len(), 1);
}
#[test]
fn inline_suppression_error() {
    assert!(API.filter_inline_suppressions(vec![]).is_empty());
}

// ── crates/cli/src/orchestrator.rs ────────────────────────────────────
#[test]
fn orchestrator_happy() {
    assert!(!API.format_gpu_summary().is_empty());
}
#[test]
fn orchestrator_error() {
    assert!(API
        .validate_cli_path_arg(std::path::Path::new("/nonexistent/keyhog-path"), "scan")
        .is_err());
}

// ── crates/cli/src/orchestrator_config.rs ─────────────────────────────
#[test]
fn orchestrator_config_happy() {
    let args = ScanArgs::try_parse_from(["scan", ".", "--fast"]).unwrap();
    assert!(args.fast);
}

// ── crates/cli/src/path_validation.rs ─────────────────────────────────
#[test]
fn path_validation_error() {
    assert!(API
        .validate_cli_path_arg(std::path::Path::new("/nonexistent/keyhog-path"), "scan")
        .is_err());
}

// ── crates/cli/src/reporting.rs ───────────────────────────────────────
#[test]
fn reporting_error() {
    let _guard = API.scan_runtime_guard_for_test();
    let args = ScanArgs::try_parse_from(["scan", ".", "--output", "/"]).unwrap();
    assert!(API.report_findings(&[], &args, &_guard).is_err());
}

#[test]
fn reporting_sarif_includes_scanner_decode_truncation_gap() {
    let _guard = API.scan_runtime_guard_for_test();
    API.reset_scan_runtime_state_for_test(&_guard);
    let chunk = Chunk {
        data: SensitiveString::from("plain inert text"),
        metadata: ChunkMetadata {
            path: Some("encoded/audit.txt".into()),
            ..Default::default()
        },
    };
    let past_deadline = Instant::now() - Duration::from_millis(1);
    let _decoded =
        keyhog_scanner::testing::decode_chunk(&chunk, 1, false, Some(past_deadline), None);
    assert!(
        API.scan_runtime_snapshot(&_guard).decode_truncations > 0,
        "test setup must create a real scanner decode-through truncation"
    );

    let dir = tempfile::tempdir().expect("tempdir");
    let out = dir.path().join("report.sarif");
    let out_s = out.to_string_lossy().into_owned();
    let args = ScanArgs::try_parse_from(["scan", ".", "--format", "sarif", "--output", &out_s])
        .expect("parse sarif output args");
    API.report_findings(&[], &args, &_guard)
        .expect("write SARIF report with scanner coverage gap");

    let sarif: serde_json::Value =
        serde_json::from_slice(&std::fs::read(&out).expect("read SARIF")).expect("SARIF JSON");
    let notifications = sarif["runs"][0]["invocations"][0]["toolExecutionNotifications"]
        .as_array()
        .expect("scanner decode truncation must create SARIF notifications");
    assert!(
        notifications.iter().any(|notification| {
            notification["properties"]["reason"].as_str()
                == Some("scanner decode-through truncated by budget/cap (raw bytes scanned; deeper encoded layers not expanded)")
                && notification["properties"]["count"].as_u64().is_some_and(|count| count >= 1)
        }),
        "SARIF notifications must include the scanner decode truncation gap; sarif={sarif}"
    );
    API.reset_scan_runtime_state_for_test(&_guard);
}

// ── crates/cli/src/sources.rs ───────────────────────────────────────
#[test]
fn sources_error() {
    let args = ScanArgs::try_parse_from(["scan", "--path", "/nonexistent/keyhog-path"]).unwrap();
    assert!(API.build_sources(&args, vec![], None).is_err());
}

#[test]
fn sources_wires_no_default_excludes_into_git_sources() {
    let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
    let sources = std::fs::read_to_string(root.join("src/sources.rs")).expect("read sources.rs");

    for constructor in [
        "keyhog_sources::GitSource::new(path.clone())",
        "keyhog_sources::GitDiffSource::new(repo_path, base_ref.clone())",
        "keyhog_sources::GitHistorySource::new(path.clone())",
    ] {
        let start = sources.find(constructor).unwrap_or_else(|| {
            panic!("sources.rs must construct {constructor} through the resolved config path")
        });
        let tail = &sources[start..];
        let end = tail
            .find("));")
            .expect("git source constructor chain must close");
        let chain = &tail[..end];
        assert!(
            chain.contains(".with_default_excludes(!resolved.no_default_excludes)"),
            "{constructor} must receive the resolved no-default-excludes flag"
        );
    }

    assert!(
        sources.contains("create_source_with_http_config_limits_and_policy"),
        "CLI source construction must use the policy-aware source factory"
    );
    for source_name in [
        "\"github-org\"",
        "\"gitlab-group\"",
        "\"bitbucket-workspace\"",
        "\"docker\"",
        "source_name",
    ] {
        let start = sources.find(source_name).unwrap_or_else(|| {
            panic!("sources.rs must construct {source_name} through the resolved config path")
        });
        let tail = &sources[start..];
        let call_end = tail.find(")?").unwrap_or_else(|| {
            tail.find(") {")
                .expect("dynamic source factory call must close")
        });
        let call = &tail[..call_end];
        assert!(
            call.contains("!resolved.no_default_excludes"),
            "{source_name} source factory call must receive the resolved no-default-excludes flag"
        );
    }
}

// ── crates/cli/src/subcommands/mod.rs ─────────────────────────────────
#[test]
fn subcommands_mod_happy() {
    let cli = Cli::try_parse_from(["keyhog", "scan", "."]).unwrap();
    assert!(matches!(cli.command, Some(keyhog::args::Command::Scan(_))));
}
#[test]
fn subcommands_mod_error() {
    assert!(Cli::try_parse_from(["keyhog", "not-a-command"]).is_err());
}

// ── crates/cli/src/subcommands/backend.rs ─────────────────────────────
#[test]
fn subcommands_backend_error() {
    assert!(Cli::try_parse_from(["keyhog", "backend", "--not-real"]).is_err());
}

// ── crates/cli/src/subcommands/calibrate.rs ───────────────────────────
#[test]
fn subcommands_calibrate_error() {
    assert!(Cli::try_parse_from(["keyhog", "calibrate", "--not-real"]).is_err());
}

// ── crates/cli/src/subcommands/completion.rs ──────────────────────────
#[test]
fn subcommands_completion_error() {
    assert!(Cli::try_parse_from(["keyhog", "completion"]).is_err());
}

// ── crates/cli/src/subcommands/daemon.rs ──────────────────────────────
#[test]
fn subcommands_daemon_error() {
    assert!(Cli::try_parse_from(["keyhog", "daemon", "not-a-sub"]).is_err());
}

// ── crates/cli/src/subcommands/detectors.rs ───────────────────────────
#[test]
fn subcommands_detectors_error() {
    assert!(Cli::try_parse_from(["keyhog", "detectors", "--not-real"]).is_err());
}

// ── crates/cli/src/subcommands/diff.rs ────────────────────────────────
#[test]
fn subcommands_diff_happy() {
    let dir = tempfile::tempdir().unwrap();
    let a = dir.path().join("a.json");
    let b = dir.path().join("b.json");
    std::fs::write(&a, b"[]").unwrap();
    std::fs::write(&b, b"[]").unwrap();
    assert!(
        Cli::try_parse_from(["keyhog", "diff", a.to_str().unwrap(), b.to_str().unwrap()]).is_ok()
    );
}
#[test]
fn subcommands_diff_error() {
    assert!(Cli::try_parse_from(["keyhog", "diff", "--not-real"]).is_err());
}

// ── crates/cli/src/subcommands/explain.rs ─────────────────────────────
#[test]
fn subcommands_explain_error() {
    assert!(Cli::try_parse_from(["keyhog", "explain"]).is_err());
}

// ── crates/cli/src/subcommands/hook.rs ────────────────────────────────
#[test]
fn subcommands_hook_error() {
    assert!(Cli::try_parse_from(["keyhog", "hook"]).is_err());
}

// ── crates/cli/src/subcommands/repair.rs ──────────────────────────────
#[test]
fn subcommands_repair_self_test_errors_are_visible() {
    let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
    let repair = std::fs::read_to_string(root.join("src/subcommands/repair.rs"))
        .expect("read repair subcommand");
    assert!(
        !repair.contains("scan_engine_self_test().unwrap_or(false)"),
        "repair must not collapse self-test errors into a boolean"
    );
    assert!(
        repair.contains("Err(error)") && repair.contains("({error}) - reinstalling"),
        "repair must print the self-test error reason before reinstalling"
    );
    assert!(
        repair.contains("planted secret was not detected"),
        "repair must distinguish a false self-test from an errored self-test"
    );
}

// ── crates/cli/src/subcommands/scan.rs ────────────────────────────────
#[test]
fn subcommands_scan_error() {
    assert!(Cli::try_parse_from(["keyhog", "scan", "--min-confidence", "bad"]).is_err());
}

// ── crates/cli/src/subcommands/scan_system.rs ─────────────────────────
#[test]
fn subcommands_scan_system_error() {
    assert!(Cli::try_parse_from(["keyhog", "scan-system", "--not-real"]).is_err());
}

// ── crates/cli/src/subcommands/watch.rs ───────────────────────────────
#[test]
fn subcommands_watch_error() {
    assert!(Cli::try_parse_from(["keyhog", "watch", "--not-real"]).is_err());
}

// ── crates/cli/src/test_fixture_suppressions.rs ───────────────────────
#[test]
fn test_fixture_suppressions_happy() {
    let s = API.bundled_test_fixture_suppressions();
    assert!(API.test_fixture_exact_count(&s) >= 1);
}
#[test]
fn test_fixture_suppressions_error() {
    let s = API.empty_test_fixture_suppressions();
    assert!(!API.test_fixture_suppresses(&s, "sk_live_realistic_token_value"));
}

// ── crates/cli/src/value_parsers.rs ───────────────────────────────────
#[test]
fn value_parsers_happy() {
    assert_eq!(API.parse_min_confidence("0.5").unwrap(), 0.5);
}
#[test]
fn value_parsers_error() {
    assert!(API.parse_decode_depth("not-a-number").is_err());
}