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
//! Template for pack unit tests.
//!
//! This file provides a complete example of how to test a pack. Copy this
//! template when creating tests for a new pack and adapt it to your specific
//! patterns.
//!
//! # Structure
//!
//! Every pack test module should include:
//!
//! 1. **Pack creation test** - Verify the pack initializes correctly
//! 2. **Destructive pattern tests** - One test per destructive pattern
//! 3. **Safe pattern tests** - One test per safe pattern
//! 4. **Edge case tests** - Special characters, quoting, etc.
//! 5. **Specificity tests** - Verify patterns don't over-match
//! 6. **Severity tests** - Verify correct severity classification
//! 7. **Performance tests** - Verify patterns don't have backtracking issues
//!
//! # Naming Conventions
//!
//! - Test functions: `test_<pattern_name>_<scenario>`
//! - Pattern names: Use the exact pattern name from the pack definition
//! - Command fixtures: Use realistic commands from actual usage
//!
//! # Example Usage
//!
//! To create tests for a new pack (e.g., `database.postgresql`):
//!
//! 1. Copy this file to `src/packs/database/postgresql_tests.rs`
//! 2. Replace `example_pack` references with your pack module
//! 3. Update the test cases for your specific patterns
//! 4. Add `#[path = "postgresql_tests.rs"] mod tests;` to your pack file

#[cfg(test)]
mod pack_test_template {
    //! This template module demonstrates the complete test structure.
    //!
    //! In actual usage, you would:
    //! 1. Place tests in the same file as the pack, or
    //! 2. Create a separate `<pack>_tests.rs` file

    use crate::packs::Severity;
    use crate::packs::test_helpers::*;
    // Import your pack module here:
    // use crate::packs::your_category::your_pack;

    // For this template, we'll use core.git as an example
    use crate::packs::core::git as example_pack;

    // =========================================================================
    // SECTION 1: Pack Creation Tests
    // =========================================================================

    /// Verify the pack initializes correctly with all expected fields.
    #[test]
    fn test_pack_creation() {
        let pack = example_pack::create_pack();
        validate_pack(&pack);
    }

    // =========================================================================
    // SECTION 2: Destructive Pattern Tests
    // =========================================================================
    //
    // Create one test per destructive pattern. Test:
    // - The canonical command form
    // - Common variations (flags, paths, etc.)
    // - Edge cases specific to this pattern

    /// Test: git reset --hard pattern (CRITICAL severity)
    #[test]
    fn test_destructive_reset_hard_basic() {
        let pack = example_pack::create_pack();

        // Canonical form
        assert_blocks(&pack, "git reset --hard", "destroys uncommitted changes");
    }

    #[test]
    fn test_destructive_reset_hard_with_ref() {
        let pack = example_pack::create_pack();

        // With various refs
        assert_blocks(&pack, "git reset --hard HEAD", "destroys uncommitted");
        assert_blocks(&pack, "git reset --hard HEAD~1", "destroys uncommitted");
        assert_blocks(
            &pack,
            "git reset --hard origin/main",
            "destroys uncommitted",
        );
        assert_blocks(&pack, "git reset --hard abc123", "destroys uncommitted");
    }

    #[test]
    fn test_destructive_reset_hard_severity() {
        let pack = example_pack::create_pack();

        // Verify Critical severity (most dangerous)
        assert_blocks_with_severity(&pack, "git reset --hard", Severity::Critical);
    }

    #[test]
    fn test_destructive_reset_hard_pattern_name() {
        let pack = example_pack::create_pack();

        // Verify pattern name for allowlisting
        assert_blocks_with_pattern(&pack, "git reset --hard", "reset-hard");
    }

    /// Test: git push --force pattern (CRITICAL severity)
    #[test]
    fn test_destructive_push_force_variations() {
        let pack = example_pack::create_pack();

        // Long flag
        assert_blocks(&pack, "git push --force", "destroy remote history");
        assert_blocks(
            &pack,
            "git push origin main --force",
            "destroy remote history",
        );
        assert_blocks(
            &pack,
            "git push --force origin main",
            "destroy remote history",
        );

        // Short flag
        assert_blocks(&pack, "git push -f", "destroy remote history");
        assert_blocks(&pack, "git push origin main -f", "destroy remote history");
    }

    // =========================================================================
    // SECTION 3: Safe Pattern Tests
    // =========================================================================
    //
    // Test that safe patterns correctly allow commands that would otherwise
    // be blocked by destructive patterns.

    /// Test: git checkout -b creates new branch (safe)
    #[test]
    fn test_safe_checkout_new_branch() {
        let pack = example_pack::create_pack();

        // Should be explicitly allowed by safe pattern
        assert_safe_pattern_matches(&pack, "git checkout -b feature");
        assert_safe_pattern_matches(&pack, "git checkout -b feature/new-thing");
        assert_safe_pattern_matches(&pack, "git checkout -b fix-123");
    }

    /// Test: git restore --staged only affects index (safe)
    #[test]
    fn test_safe_restore_staged() {
        let pack = example_pack::create_pack();

        // Unstaging is safe (doesn't touch working tree)
        assert_allows(&pack, "git restore --staged file.txt");
        assert_allows(&pack, "git restore -S file.txt");
    }

    /// Test: git clean --dry-run just previews (safe)
    #[test]
    fn test_safe_clean_dry_run() {
        let pack = example_pack::create_pack();

        assert_allows(&pack, "git clean -n");
        assert_allows(&pack, "git clean -dn");
        assert_allows(&pack, "git clean --dry-run");
    }

    // =========================================================================
    // SECTION 4: Edge Case Tests
    // =========================================================================
    //
    // Test unusual but valid command forms that patterns should handle.

    /// Test: Commands with extra whitespace
    #[test]
    fn test_edge_case_extra_whitespace() {
        let pack = example_pack::create_pack();

        // Multiple spaces should still match
        assert_blocks(&pack, "git  reset  --hard", "destroys uncommitted");
        assert_blocks(&pack, "git   push   --force", "destroy remote history");
    }

    /// Test: Empty and minimal commands
    #[test]
    fn test_edge_case_minimal_commands() {
        let pack = example_pack::create_pack();

        // Empty string should not match
        assert_no_match(&pack, "");

        // Just the keyword should not match destructive patterns
        assert_no_match(&pack, "git");
    }

    /// Test: Commands with quoted arguments
    #[test]
    fn test_edge_case_quoted_arguments() {
        let pack = example_pack::create_pack();

        // Quoted paths should still be blocked
        assert_blocks(&pack, "git reset --hard \"HEAD\"", "destroys uncommitted");
    }

    /// Test: Commands with special characters in paths
    #[test]
    fn test_edge_case_special_characters() {
        let pack = example_pack::create_pack();

        // Paths with special characters
        assert_blocks(
            &pack,
            "git push --force origin feature/my-branch",
            "destroy remote",
        );
        assert_blocks(
            &pack,
            "git push --force origin bugfix/issue#123",
            "destroy remote",
        );
    }

    // =========================================================================
    // SECTION 5: Specificity Tests (False Positive Prevention)
    // =========================================================================
    //
    // Verify patterns don't accidentally match unrelated commands.

    /// Test: Unrelated commands should not match
    #[test]
    fn test_specificity_unrelated_commands() {
        let pack = example_pack::create_pack();

        // Common commands that should NOT match
        assert_no_match(&pack, "ls -la");
        assert_no_match(&pack, "cat file.txt");
        assert_no_match(&pack, "cargo build");
        assert_no_match(&pack, "npm install");
    }

    /// Test: Similar but safe git commands should not match
    #[test]
    fn test_specificity_safe_git_commands() {
        let pack = example_pack::create_pack();

        // These git commands are safe
        assert_allows(&pack, "git status");
        assert_allows(&pack, "git log");
        assert_allows(&pack, "git diff");
        assert_allows(&pack, "git add .");
        assert_allows(&pack, "git commit -m 'message'");
        assert_allows(&pack, "git push"); // Without --force
        assert_allows(&pack, "git pull");
        assert_allows(&pack, "git fetch");
        assert_allows(&pack, "git branch -d feature"); // Safe delete with -d
    }

    /// Test: Substring matches should not trigger (keyword boundary)
    #[test]
    fn test_specificity_substring_not_matched() {
        let pack = example_pack::create_pack();

        // "git" appearing as substring should not trigger
        assert_no_match(&pack, "cat .gitignore");
        assert_no_match(&pack, "echo digit");
        assert_no_match(&pack, "legitimate command with git in path");
    }

    // =========================================================================
    // SECTION 6: Severity Classification Tests
    // =========================================================================
    //
    // Verify patterns have correct severity levels.

    /// Test: Critical patterns (most dangerous, always block)
    #[test]
    fn test_severity_critical_patterns() {
        let pack = example_pack::create_pack();

        // These should all be Critical
        assert_blocks_with_severity(&pack, "git reset --hard", Severity::Critical);
        assert_blocks_with_severity(&pack, "git clean -f", Severity::Critical);
        assert_blocks_with_severity(&pack, "git push --force", Severity::Critical);
        assert_blocks_with_severity(&pack, "git stash clear", Severity::Critical);
    }

    /// Test: High severity patterns (dangerous, block by default)
    #[test]
    fn test_severity_high_patterns() {
        let pack = example_pack::create_pack();

        // These should be High severity
        assert_blocks_with_severity(&pack, "git checkout -- file.txt", Severity::High);
        assert_blocks_with_severity(&pack, "git restore file.txt", Severity::High);
    }

    /// Test: Medium severity patterns (recoverable, warn by default)
    #[test]
    fn test_severity_medium_patterns() {
        let pack = example_pack::create_pack();

        // These were moved to Medium (recoverable via reflog/fsck)
        assert_blocks_with_severity(&pack, "git stash drop", Severity::Medium);
        assert_blocks_with_severity(&pack, "git branch -D feature", Severity::Medium);
    }

    // =========================================================================
    // SECTION 7: Performance Tests
    // =========================================================================
    //
    // Verify patterns don't have catastrophic backtracking.

    /// Test: Normal commands should match quickly
    #[test]
    fn test_performance_normal_commands() {
        let pack = example_pack::create_pack();

        // These should all complete within budget
        assert_matches_within_budget(&pack, "git reset --hard");
        assert_matches_within_budget(&pack, "git push --force origin main");
        assert_matches_within_budget(&pack, "git checkout -b feature/new-thing");
    }

    /// Test: Pathological inputs should not cause catastrophic backtracking
    #[test]
    fn test_performance_pathological_inputs() {
        let pack = example_pack::create_pack();

        // Long repeated characters
        let long_flags = format!("git {}", "-".repeat(1000));
        assert_matches_within_budget(&pack, &long_flags);

        // Many spaces
        let many_spaces = format!("git{}{}", " ".repeat(100), "status");
        assert_matches_within_budget(&pack, &many_spaces);
    }

    // =========================================================================
    // SECTION 8: Batch Tests (Optional but Recommended)
    // =========================================================================
    //
    // Use batch tests to verify multiple related commands at once.

    /// Test: All reset variants should be blocked
    #[test]
    fn test_batch_reset_variants() {
        let pack = example_pack::create_pack();

        let reset_commands = vec![
            "git reset --hard",
            "git reset --hard HEAD",
            "git reset --hard HEAD~1",
            "git reset --hard HEAD~10",
            "git reset --hard origin/main",
            "git reset --hard abc123def456",
        ];

        test_batch_blocks(&pack, &reset_commands, "reset");
    }

    /// Test: Safe read-only commands should all be allowed
    #[test]
    fn test_batch_readonly_commands() {
        let pack = example_pack::create_pack();

        let readonly_commands = vec![
            "git status",
            "git log",
            "git log --oneline",
            "git diff",
            "git diff --cached",
            "git show HEAD",
            "git branch",
            "git branch -a",
            "git remote -v",
        ];

        test_batch_allows(&pack, &readonly_commands);
    }

    // =========================================================================
    // SECTION 9: Logged Batch Tests (Recommended for CI)
    // =========================================================================
    //
    // Use LoggedPackTestRunner for detailed JSON reporting of test results.
    // This is especially useful for CI/CD pipelines.

    #[test]
    fn test_logged_batch_execution() {
        let pack = example_pack::create_pack();
        let mut runner = LoggedPackTestRunner::debug(&pack);

        // Batch test blocking commands
        runner.assert_blocks("git reset --hard", "destroys uncommitted");
        runner.assert_blocks("git push --force", "destroy remote");

        // Batch test allowing commands
        runner.assert_allows("git status");
        runner.assert_allows("git log");

        // Finish and get report (in CI this would be written to a file)
        let report = runner.finish();
        assert!(report.contains("core.git"));
        assert!(report.contains("passed"));
    }
}

// =========================================================================
// How to Use This Template
// =========================================================================
//
// 1. For a new pack in `src/packs/category/my_pack.rs`:
//
//    ```rust
//    // At the end of my_pack.rs, add:
//    #[cfg(test)]
//    mod tests {
//        use super::*;
//        use crate::packs::test_helpers::*;
//        use crate::packs::Severity;
//
//        #[test]
//        fn test_pack_creation() {
//            let pack = create_pack();
//            assert_patterns_compile(&pack);
//            // ... more tests
//        }
//
//        // Copy relevant test patterns from this template
//    }
//    ```
//
// 2. For extensive tests, create a separate file:
//
//    - Create `src/packs/category/my_pack_tests.rs`
//    - Add `#[cfg(test)] #[path = "my_pack_tests.rs"] mod tests;` to my_pack.rs
//
// 3. Run tests:
//
//    ```bash
//    cargo test packs::category::my_pack
//    ```