destructive_command_guard 0.4.3

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

use destructive_command_guard::suggest::{
    AllowlistSuggestion, CommandCluster, ConfidenceTier, PathPattern, RiskLevel, SuggestionReason,
    analyze_path_patterns, assess_risk_level, calculate_confidence_tier,
    calculate_suggestion_score, determine_primary_reason,
};

// ============================================================================
// ConfidenceTier Tests
// ============================================================================

#[test]
fn confidence_tier_as_str() {
    assert_eq!(ConfidenceTier::High.as_str(), "high");
    assert_eq!(ConfidenceTier::Medium.as_str(), "medium");
    assert_eq!(ConfidenceTier::Low.as_str(), "low");
}

#[test]
fn confidence_tier_scores() {
    // High confidence should have the highest score
    assert!((ConfidenceTier::High.score() - 1.0).abs() < f32::EPSILON);
    // Medium should be between high and low
    assert!(ConfidenceTier::Medium.score() > ConfidenceTier::Low.score());
    assert!(ConfidenceTier::Medium.score() < ConfidenceTier::High.score());
    // Low should be the lowest
    assert!(ConfidenceTier::Low.score() < 0.5);
}

#[test]
fn confidence_tier_display() {
    assert_eq!(format!("{}", ConfidenceTier::High), "high");
    assert_eq!(format!("{}", ConfidenceTier::Medium), "medium");
    assert_eq!(format!("{}", ConfidenceTier::Low), "low");
}

#[test]
fn confidence_tier_serialization() {
    let high = ConfidenceTier::High;
    let json = serde_json::to_string(&high).unwrap();
    assert_eq!(json, "\"high\"");

    let deserialized: ConfidenceTier = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized, ConfidenceTier::High);
}

#[test]
fn confidence_tier_all_variants_serialize_deserialize() {
    for tier in [
        ConfidenceTier::High,
        ConfidenceTier::Medium,
        ConfidenceTier::Low,
    ] {
        let json = serde_json::to_string(&tier).unwrap();
        let deserialized: ConfidenceTier = serde_json::from_str(&json).unwrap();
        assert_eq!(tier, deserialized);
    }
}

// ============================================================================
// RiskLevel Tests
// ============================================================================

#[test]
fn risk_level_as_str() {
    assert_eq!(RiskLevel::Low.as_str(), "low");
    assert_eq!(RiskLevel::Medium.as_str(), "medium");
    assert_eq!(RiskLevel::High.as_str(), "high");
}

#[test]
fn risk_level_scores() {
    // Low risk should have the lowest score (best)
    assert!(RiskLevel::Low.score() < RiskLevel::Medium.score());
    // High risk should have the highest score (worst)
    assert!(RiskLevel::High.score() > RiskLevel::Medium.score());
    // Scores should be in valid range [0, 1]
    assert!(RiskLevel::Low.score() >= 0.0 && RiskLevel::Low.score() <= 1.0);
    assert!(RiskLevel::High.score() >= 0.0 && RiskLevel::High.score() <= 1.0);
}

#[test]
fn risk_level_display() {
    assert_eq!(format!("{}", RiskLevel::Low), "low");
    assert_eq!(format!("{}", RiskLevel::Medium), "medium");
    assert_eq!(format!("{}", RiskLevel::High), "high");
}

#[test]
fn risk_level_serialization() {
    let high = RiskLevel::High;
    let json = serde_json::to_string(&high).unwrap();
    assert_eq!(json, "\"high\"");

    let deserialized: RiskLevel = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized, RiskLevel::High);
}

#[test]
fn risk_level_all_variants_serialize_deserialize() {
    for level in [RiskLevel::Low, RiskLevel::Medium, RiskLevel::High] {
        let json = serde_json::to_string(&level).unwrap();
        let deserialized: RiskLevel = serde_json::from_str(&json).unwrap();
        assert_eq!(level, deserialized);
    }
}

// ============================================================================
// SuggestionReason Tests
// ============================================================================

#[test]
fn suggestion_reason_as_str() {
    assert_eq!(SuggestionReason::HighFrequency.as_str(), "high_frequency");
    assert_eq!(SuggestionReason::PathClustered.as_str(), "path_clustered");
    assert_eq!(
        SuggestionReason::ManuallyBypassed.as_str(),
        "manually_bypassed"
    );
    assert_eq!(
        SuggestionReason::SafePatternMatch.as_str(),
        "safe_pattern_match"
    );
}

#[test]
fn suggestion_reason_description() {
    // All reasons should have non-empty descriptions
    assert!(!SuggestionReason::HighFrequency.description().is_empty());
    assert!(!SuggestionReason::PathClustered.description().is_empty());
    assert!(!SuggestionReason::ManuallyBypassed.description().is_empty());
    assert!(!SuggestionReason::SafePatternMatch.description().is_empty());

    // Descriptions should be human-readable (contain spaces)
    assert!(SuggestionReason::HighFrequency.description().contains(' '));
}

#[test]
fn suggestion_reason_display() {
    assert_eq!(
        format!("{}", SuggestionReason::HighFrequency),
        "high_frequency"
    );
    assert_eq!(
        format!("{}", SuggestionReason::ManuallyBypassed),
        "manually_bypassed"
    );
}

#[test]
fn suggestion_reason_serialization() {
    let reason = SuggestionReason::PathClustered;
    let json = serde_json::to_string(&reason).unwrap();
    assert_eq!(json, "\"path_clustered\"");

    let deserialized: SuggestionReason = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized, SuggestionReason::PathClustered);
}

// ============================================================================
// calculate_confidence_tier Tests
// ============================================================================

#[test]
fn confidence_tier_high_frequency_consistent() {
    // High frequency (>=10) with consistent pattern (ratio >= 2.0) = High
    let tier = calculate_confidence_tier(20, 5); // ratio = 4.0
    assert_eq!(tier, ConfidenceTier::High);
}

#[test]
fn confidence_tier_high_frequency_inconsistent() {
    // High frequency but inconsistent pattern (ratio < 2.0) = Medium
    let tier = calculate_confidence_tier(10, 10); // ratio = 1.0
    assert_eq!(tier, ConfidenceTier::Medium);
}

#[test]
fn confidence_tier_medium_frequency() {
    // Medium frequency (5-9) = Medium
    let tier = calculate_confidence_tier(7, 3);
    assert_eq!(tier, ConfidenceTier::Medium);
}

#[test]
fn confidence_tier_low_frequency() {
    // Low frequency (<5) = Low
    let tier = calculate_confidence_tier(3, 2);
    assert_eq!(tier, ConfidenceTier::Low);
}

#[test]
fn confidence_tier_zero_variants() {
    // Zero variants should handle gracefully
    let tier = calculate_confidence_tier(10, 0);
    assert_eq!(tier, ConfidenceTier::Medium);
}

#[test]
fn confidence_tier_single_occurrence() {
    let tier = calculate_confidence_tier(1, 1);
    assert_eq!(tier, ConfidenceTier::Low);
}

#[test]
fn confidence_tier_boundary_conditions() {
    // At exactly 10 frequency (HIGH_CONFIDENCE_MIN_FREQUENCY)
    let tier_10_high_ratio = calculate_confidence_tier(10, 3); // ratio = 3.33
    assert_eq!(tier_10_high_ratio, ConfidenceTier::High);

    // At exactly 5 frequency (MEDIUM_CONFIDENCE_MIN_FREQUENCY)
    let tier_5 = calculate_confidence_tier(5, 2);
    assert_eq!(tier_5, ConfidenceTier::Medium);

    // Just below medium threshold
    let tier_4 = calculate_confidence_tier(4, 2);
    assert_eq!(tier_4, ConfidenceTier::Low);
}

// ============================================================================
// assess_risk_level Tests
// ============================================================================

#[test]
fn risk_level_high_for_rm_rf() {
    let commands = vec!["rm -rf /tmp/test".to_string()];
    assert_eq!(assess_risk_level(&commands), RiskLevel::High);
}

#[test]
fn risk_level_high_for_force_flag() {
    let commands = vec!["git push --force origin main".to_string()];
    assert_eq!(assess_risk_level(&commands), RiskLevel::High);
}

#[test]
fn risk_level_high_for_reset_hard() {
    let commands = vec!["git reset --hard HEAD".to_string()];
    assert_eq!(assess_risk_level(&commands), RiskLevel::High);
}

#[test]
fn risk_level_high_for_clean_f() {
    let commands = vec!["git clean -fd".to_string()];
    assert_eq!(assess_risk_level(&commands), RiskLevel::High);
}

#[test]
fn risk_level_high_for_drop() {
    let commands = vec!["DROP TABLE users;".to_string()];
    assert_eq!(assess_risk_level(&commands), RiskLevel::High);
}

#[test]
fn risk_level_high_for_truncate() {
    let commands = vec!["truncate -s 0 /var/log/syslog".to_string()];
    assert_eq!(assess_risk_level(&commands), RiskLevel::High);
}

#[test]
fn risk_level_medium_for_rm_without_rf() {
    let commands = vec!["rm test.txt".to_string()];
    assert_eq!(assess_risk_level(&commands), RiskLevel::Medium);
}

#[test]
fn risk_level_medium_for_git_reset() {
    let commands = vec!["git reset HEAD~1".to_string()];
    assert_eq!(assess_risk_level(&commands), RiskLevel::Medium);
}

#[test]
fn risk_level_medium_for_sudo() {
    let commands = vec!["sudo npm install".to_string()];
    assert_eq!(assess_risk_level(&commands), RiskLevel::Medium);
}

#[test]
fn risk_level_medium_for_docker_rm() {
    let commands = vec!["docker rm container_name".to_string()];
    assert_eq!(assess_risk_level(&commands), RiskLevel::Medium);
}

#[test]
fn risk_level_high_for_kubectl_delete() {
    // kubectl delete contains "delete " which is a high-risk pattern
    let commands = vec!["kubectl delete pod my-pod".to_string()];
    assert_eq!(assess_risk_level(&commands), RiskLevel::High);
}

#[test]
fn risk_level_low_for_safe_commands() {
    let commands = vec!["git status".to_string()];
    assert_eq!(assess_risk_level(&commands), RiskLevel::Low);
}

#[test]
fn risk_level_low_for_npm_run() {
    let commands = vec!["npm run build".to_string()];
    assert_eq!(assess_risk_level(&commands), RiskLevel::Low);
}

#[test]
fn risk_level_high_takes_precedence_in_cluster() {
    // If any command in cluster is high risk, whole cluster is high risk
    let commands = vec![
        "npm run build".to_string(),
        "git status".to_string(),
        "rm -rf /tmp/cache".to_string(), // High risk
    ];
    assert_eq!(assess_risk_level(&commands), RiskLevel::High);
}

#[test]
fn risk_level_empty_commands() {
    let commands: Vec<String> = vec![];
    assert_eq!(assess_risk_level(&commands), RiskLevel::Low);
}

#[test]
fn risk_level_case_insensitive() {
    // Risk detection should be case insensitive
    let commands = vec!["RM -RF /tmp".to_string()];
    assert_eq!(assess_risk_level(&commands), RiskLevel::High);
}

// ============================================================================
// calculate_suggestion_score Tests
// ============================================================================

#[test]
fn suggestion_score_high_confidence_low_risk_best() {
    let score = calculate_suggestion_score(ConfidenceTier::High, RiskLevel::Low);
    // This should be the best score
    assert!(score > 0.8, "High confidence + low risk should score > 0.8");
}

#[test]
fn suggestion_score_low_confidence_high_risk_worst() {
    let score = calculate_suggestion_score(ConfidenceTier::Low, RiskLevel::High);
    // This should be the worst score
    assert!(score < 0.3, "Low confidence + high risk should score < 0.3");
}

#[test]
fn suggestion_score_ordering() {
    let high_low = calculate_suggestion_score(ConfidenceTier::High, RiskLevel::Low);
    let high_medium = calculate_suggestion_score(ConfidenceTier::High, RiskLevel::Medium);
    let high_high = calculate_suggestion_score(ConfidenceTier::High, RiskLevel::High);
    let medium_low = calculate_suggestion_score(ConfidenceTier::Medium, RiskLevel::Low);
    let low_low = calculate_suggestion_score(ConfidenceTier::Low, RiskLevel::Low);

    // Higher confidence should beat lower confidence at same risk
    assert!(high_low > medium_low);
    assert!(medium_low > low_low);

    // Lower risk should beat higher risk at same confidence
    assert!(high_low > high_medium);
    assert!(high_medium > high_high);
}

#[test]
fn suggestion_score_always_in_valid_range() {
    // Test all combinations
    for confidence in [
        ConfidenceTier::High,
        ConfidenceTier::Medium,
        ConfidenceTier::Low,
    ] {
        for risk in [RiskLevel::Low, RiskLevel::Medium, RiskLevel::High] {
            let score = calculate_suggestion_score(confidence, risk);
            assert!(
                (0.0..=1.0).contains(&score),
                "Score {score} for {confidence:?}/{risk:?} out of range [0,1]"
            );
        }
    }
}

// ============================================================================
// determine_primary_reason Tests
// ============================================================================

#[test]
fn primary_reason_bypassed_takes_precedence() {
    let reason = determine_primary_reason(5, true, &[]);
    assert_eq!(reason, SuggestionReason::ManuallyBypassed);
}

#[test]
fn primary_reason_path_clustered_with_concentration() {
    let path_patterns = vec![PathPattern {
        pattern: "/data/projects/test".to_string(),
        occurrence_count: 8,
        is_project_dir: true,
    }];
    let reason = determine_primary_reason(10, false, &path_patterns);
    assert_eq!(reason, SuggestionReason::PathClustered);
}

#[test]
fn primary_reason_high_frequency_default() {
    let reason = determine_primary_reason(10, false, &[]);
    assert_eq!(reason, SuggestionReason::HighFrequency);
}

#[test]
fn primary_reason_low_frequency_still_frequency() {
    let reason = determine_primary_reason(2, false, &[]);
    assert_eq!(reason, SuggestionReason::HighFrequency);
}

#[test]
fn primary_reason_bypass_beats_path_clustering() {
    let path_patterns = vec![PathPattern {
        pattern: "/data/projects/test".to_string(),
        occurrence_count: 10,
        is_project_dir: true,
    }];
    let reason = determine_primary_reason(10, true, &path_patterns);
    assert_eq!(reason, SuggestionReason::ManuallyBypassed);
}

// ============================================================================
// analyze_path_patterns Tests
// ============================================================================

#[test]
fn analyze_path_patterns_empty_input() {
    let (patterns, suggest_path_specific) = analyze_path_patterns(&[]);
    assert!(patterns.is_empty());
    assert!(!suggest_path_specific);
}

#[test]
fn analyze_path_patterns_single_dir() {
    let dirs = vec!["/data/projects/test".to_string()];
    let (patterns, _) = analyze_path_patterns(&dirs);
    assert!(!patterns.is_empty());
}

#[test]
fn analyze_path_patterns_clustered_dirs() {
    let dirs = vec![
        "/data/projects/myapp".to_string(),
        "/data/projects/myapp".to_string(),
        "/data/projects/myapp".to_string(),
        "/data/projects/myapp".to_string(),
    ];
    let (patterns, suggest_path_specific) = analyze_path_patterns(&dirs);
    assert!(!patterns.is_empty());
    // All in same dir should suggest path-specific
    assert!(suggest_path_specific);
}

#[test]
fn analyze_path_patterns_scattered_dirs() {
    let dirs = vec![
        "/home/user/project1".to_string(),
        "/tmp/build".to_string(),
        "/var/data".to_string(),
        "/opt/app".to_string(),
    ];
    let (patterns, suggest_path_specific) = analyze_path_patterns(&dirs);
    // Scattered dirs should not suggest path-specific
    assert!(!suggest_path_specific);
    // But patterns should still be extracted
    assert!(patterns.len() <= 3); // Max 3 patterns
}

#[test]
fn analyze_path_patterns_common_prefix() {
    let dirs = vec![
        "/data/projects/app1".to_string(),
        "/data/projects/app2".to_string(),
        "/data/projects/app3".to_string(),
    ];
    let (patterns, _) = analyze_path_patterns(&dirs);
    // Should find common prefix
    assert!(
        patterns
            .iter()
            .any(|p| p.pattern.contains("/data/projects")),
        "Should find /data/projects prefix"
    );
}

#[test]
fn analyze_path_patterns_project_dir_detection() {
    let dirs = vec![
        "/home/user/workspace/project".to_string(),
        "/home/user/workspace/project".to_string(),
    ];
    let (patterns, _) = analyze_path_patterns(&dirs);
    // Should detect as project directory
    assert!(
        patterns.iter().any(|p| p.is_project_dir),
        "Should detect project directory"
    );
}

#[test]
fn analyze_path_patterns_max_three_patterns() {
    // Create many different directories
    let dirs: Vec<String> = (0..20).map(|i| format!("/path{}/subdir/app", i)).collect();
    let (patterns, _) = analyze_path_patterns(&dirs);
    assert!(patterns.len() <= 3, "Should return at most 3 patterns");
}

// ============================================================================
// AllowlistSuggestion Builder Tests
// ============================================================================

fn create_test_cluster() -> CommandCluster {
    CommandCluster {
        commands: vec![
            "git reset --hard HEAD".to_string(),
            "git reset --hard origin/main".to_string(),
        ],
        normalized: vec![
            "git reset --hard HEAD".to_string(),
            "git reset --hard origin/main".to_string(),
        ],
        proposed_pattern: "^git reset --hard".to_string(),
        frequency: 10,
        unique_count: 2,
    }
}

#[test]
fn allowlist_suggestion_from_cluster_basic() {
    let cluster = create_test_cluster();
    let suggestion = AllowlistSuggestion::from_cluster(cluster.clone());

    assert_eq!(suggestion.cluster.frequency, 10);
    assert_eq!(suggestion.cluster.unique_count, 2);
    // High frequency (10) with ratio 5.0 = High confidence
    assert_eq!(suggestion.confidence, ConfidenceTier::High);
    // git reset --hard is high risk
    assert_eq!(suggestion.risk, RiskLevel::High);
    assert!(suggestion.score > 0.0);
}

#[test]
fn allowlist_suggestion_from_cluster_low_frequency() {
    let cluster = CommandCluster {
        commands: vec!["npm run build".to_string()],
        normalized: vec!["npm run build".to_string()],
        proposed_pattern: "^npm run build$".to_string(),
        frequency: 2,
        unique_count: 1,
    };
    let suggestion = AllowlistSuggestion::from_cluster(cluster);

    // Low frequency = Low confidence
    assert_eq!(suggestion.confidence, ConfidenceTier::Low);
    // npm run build is low risk
    assert_eq!(suggestion.risk, RiskLevel::Low);
}

#[test]
fn allowlist_suggestion_with_path_analysis() {
    let cluster = create_test_cluster();
    let working_dirs = vec![
        "/data/projects/myapp".to_string(),
        "/data/projects/myapp".to_string(),
        "/data/projects/myapp".to_string(),
        "/data/projects/myapp".to_string(),
        "/data/projects/myapp".to_string(),
    ];

    let suggestion = AllowlistSuggestion::from_cluster(cluster).with_path_analysis(&working_dirs);

    assert!(!suggestion.path_patterns.is_empty());
    assert!(suggestion.suggest_path_specific);
    assert!(
        suggestion
            .contributing_factors
            .contains(&SuggestionReason::PathClustered)
    );
}

#[test]
fn allowlist_suggestion_with_bypass_count() {
    let cluster = CommandCluster {
        commands: vec!["git status".to_string()],
        normalized: vec!["git status".to_string()],
        proposed_pattern: "^git status$".to_string(),
        frequency: 2,
        unique_count: 1,
    };

    let suggestion = AllowlistSuggestion::from_cluster(cluster).with_bypass_count(5);

    // Bypass should set confidence to High
    assert_eq!(suggestion.confidence, ConfidenceTier::High);
    // Reason should be ManuallyBypassed
    assert_eq!(suggestion.reason, SuggestionReason::ManuallyBypassed);
    assert!(
        suggestion
            .contributing_factors
            .contains(&SuggestionReason::ManuallyBypassed)
    );
    assert_eq!(suggestion.bypass_count, 5);
}

#[test]
fn allowlist_suggestion_score_recalculated_on_updates() {
    let cluster = CommandCluster {
        commands: vec!["npm run test".to_string()],
        normalized: vec!["npm run test".to_string()],
        proposed_pattern: "^npm run test$".to_string(),
        frequency: 2,
        unique_count: 1,
    };

    let initial_suggestion = AllowlistSuggestion::from_cluster(cluster.clone());
    let initial_score = initial_suggestion.score;

    // Adding bypass should increase confidence, thus increasing score
    let with_bypass = AllowlistSuggestion::from_cluster(cluster).with_bypass_count(3);

    assert!(
        with_bypass.score > initial_score,
        "Score should increase after adding bypass"
    );
}

#[test]
fn allowlist_suggestion_serialization() {
    let cluster = create_test_cluster();
    let suggestion = AllowlistSuggestion::from_cluster(cluster);

    let json = serde_json::to_string(&suggestion).unwrap();
    assert!(json.contains("\"confidence\":\"high\""));
    assert!(json.contains("\"risk\":\"high\""));

    let deserialized: AllowlistSuggestion = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized.confidence, suggestion.confidence);
    assert_eq!(deserialized.risk, suggestion.risk);
}

// ============================================================================
// PathPattern Tests
// ============================================================================

#[test]
fn path_pattern_serialization() {
    let pattern = PathPattern {
        pattern: "/data/projects/test".to_string(),
        occurrence_count: 10,
        is_project_dir: true,
    };

    let json = serde_json::to_string(&pattern).unwrap();
    assert!(json.contains("\"is_project_dir\":true"));

    let deserialized: PathPattern = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized.pattern, pattern.pattern);
    assert_eq!(deserialized.occurrence_count, pattern.occurrence_count);
    assert_eq!(deserialized.is_project_dir, pattern.is_project_dir);
}

// ============================================================================
// Integration: Full Suggestion Generation Flow
// ============================================================================

#[test]
fn full_suggestion_flow_high_frequency_clustered() {
    // Simulate a real-world scenario: same command blocked many times
    let cluster = CommandCluster {
        commands: vec!["git reset --hard HEAD".to_string()],
        normalized: vec!["git reset --hard HEAD".to_string()],
        proposed_pattern: "^git reset --hard HEAD$".to_string(),
        frequency: 25,
        unique_count: 1,
    };

    let working_dirs: Vec<String> = (0..25)
        .map(|_| "/data/projects/myapp".to_string())
        .collect();

    let suggestion = AllowlistSuggestion::from_cluster(cluster)
        .with_path_analysis(&working_dirs)
        .with_bypass_count(3);

    // Should have high confidence due to frequency and bypasses
    assert_eq!(suggestion.confidence, ConfidenceTier::High);
    // Should recommend path-specific due to clustering
    assert!(suggestion.suggest_path_specific);
    // Should have ManuallyBypassed as reason due to bypasses
    assert_eq!(suggestion.reason, SuggestionReason::ManuallyBypassed);
    // Score should be reasonable despite high risk
    assert!(suggestion.score > 0.5);
}

#[test]
fn full_suggestion_flow_low_frequency_scattered() {
    let cluster = CommandCluster {
        commands: vec!["npm run build".to_string()],
        normalized: vec!["npm run build".to_string()],
        proposed_pattern: "^npm run build$".to_string(),
        frequency: 3,
        unique_count: 1,
    };

    let working_dirs = vec![
        "/project1".to_string(),
        "/project2".to_string(),
        "/project3".to_string(),
    ];

    let suggestion = AllowlistSuggestion::from_cluster(cluster).with_path_analysis(&working_dirs);

    // Low frequency = low confidence
    assert_eq!(suggestion.confidence, ConfidenceTier::Low);
    // Scattered paths = no path-specific suggestion
    assert!(!suggestion.suggest_path_specific);
    // Low risk command
    assert_eq!(suggestion.risk, RiskLevel::Low);
}