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
#![allow(clippy::iter_cloned_collect, clippy::uninlined_format_args)]
//! Tests for JSON format output across CLI commands.
//!
//! These tests verify that all CLI commands with --format json
//! produce valid, well-structured JSON output suitable for AI agent parsing.

use std::process::Command;

/// Path to the DCG binary (uses same target directory as the test binary).
fn dcg_binary() -> std::path::PathBuf {
    let mut path = std::env::current_exe().unwrap();
    path.pop(); // Remove test binary name
    path.pop(); // Remove deps/
    path.push("dcg");
    path
}

/// Run a dcg command and return stdout, stderr, exit code.
fn run_dcg(args: &[&str]) -> (String, String, i32) {
    let output = Command::new(dcg_binary())
        .args(args)
        .output()
        .expect("failed to run dcg");

    let stdout = String::from_utf8_lossy(&output.stdout).to_string();
    let stderr = String::from_utf8_lossy(&output.stderr).to_string();
    let exit_code = output.status.code().unwrap_or(-1);

    (stdout, stderr, exit_code)
}

// =============================================================================
// Test Command JSON Output
// =============================================================================

#[test]
fn test_test_command_json_valid() {
    let (stdout, stderr, exit_code) = run_dcg(&["test", "--format", "json", "git status"]);

    assert_eq!(
        exit_code, 0,
        "test --format json should exit 0\nstderr: {stderr}"
    );

    let json: serde_json::Value =
        serde_json::from_str(&stdout).expect("test --format json should produce valid JSON");

    // Verify required fields for test output
    assert!(json.get("command").is_some(), "should have 'command' field");
    assert!(
        json.get("decision").is_some(),
        "should have 'decision' field"
    );
}

#[test]
fn test_test_command_json_allowed() {
    let (stdout, _stderr, _) = run_dcg(&["test", "--format", "json", "echo hello"]);

    let json: serde_json::Value =
        serde_json::from_str(&stdout).expect("test --format json should produce valid JSON");

    assert_eq!(
        json["decision"], "allow",
        "safe command should have decision=allow"
    );
}

#[test]
fn test_test_command_json_denied() {
    let (stdout, _stderr, _) = run_dcg(&["test", "--format", "json", "git reset --hard"]);

    let json: serde_json::Value =
        serde_json::from_str(&stdout).expect("test --format json should produce valid JSON");

    assert_eq!(
        json["decision"], "deny",
        "dangerous command should have decision=deny"
    );

    // Denied commands should have additional fields
    assert!(json.get("rule_id").is_some(), "denied should have rule_id");
    assert!(json.get("pack_id").is_some(), "denied should have pack_id");
    assert!(json.get("reason").is_some(), "denied should have reason");
}

#[test]
fn test_test_command_json_denied_has_matched_span() {
    let (stdout, _stderr, _) = run_dcg(&["test", "--format", "json", "git reset --hard"]);

    let json: serde_json::Value =
        serde_json::from_str(&stdout).expect("test --format json should produce valid JSON");

    if json["decision"] == "deny" {
        // matched_span should be present for highlighting
        if let Some(span) = json.get("matched_span") {
            assert!(
                span.is_array(),
                "matched_span should be an array [start, end]"
            );
            let arr = span.as_array().unwrap();
            assert_eq!(arr.len(), 2, "matched_span should have 2 elements");
        }
    }
}

// =============================================================================
// Explain Command JSON Output
// =============================================================================

#[test]
fn test_explain_command_json_valid() {
    let (stdout, stderr, exit_code) = run_dcg(&["explain", "--format", "json", "git reset --hard"]);

    assert_eq!(
        exit_code, 0,
        "explain --format json should exit 0\nstderr: {stderr}"
    );

    let json: serde_json::Value =
        serde_json::from_str(&stdout).expect("explain --format json should produce valid JSON");

    // Verify explain output structure
    assert!(
        json.get("command").is_some(),
        "explain should have 'command' field"
    );
    assert!(
        json.get("decision").is_some(),
        "explain should have 'decision' field"
    );
}

#[test]
fn test_explain_command_json_has_trace() {
    let (stdout, _stderr, _) = run_dcg(&["explain", "--format", "json", "git reset --hard"]);

    let json: serde_json::Value =
        serde_json::from_str(&stdout).expect("explain --format json should produce valid JSON");

    // Explain output should include evaluation trace
    if let Some(trace) = json.get("trace") {
        assert!(
            trace.is_array() || trace.is_object(),
            "trace should be structured"
        );
    }
}

#[test]
fn test_explain_command_json_schema_version() {
    let (stdout, _stderr, _) = run_dcg(&["explain", "--format", "json", "git reset --hard"]);

    let json: serde_json::Value =
        serde_json::from_str(&stdout).expect("explain --format json should produce valid JSON");

    // Schema version should be present for API stability
    if let Some(version) = json.get("schema_version") {
        assert!(
            version.is_number() || version.is_string(),
            "schema_version should be number or string"
        );
    }
}

// =============================================================================
// Packs Command JSON Output
// =============================================================================

#[test]
fn test_packs_command_json_valid() {
    let (stdout, stderr, exit_code) = run_dcg(&["packs", "--format", "json"]);

    assert_eq!(
        exit_code, 0,
        "packs --format json should exit 0\nstderr: {stderr}"
    );

    let json: serde_json::Value =
        serde_json::from_str(&stdout).expect("packs --format json should produce valid JSON");

    // Verify packs output structure
    assert!(json.get("packs").is_some(), "should have 'packs' array");
    assert!(json["packs"].is_array(), "packs should be an array");
}

#[test]
fn test_packs_command_json_pack_structure() {
    let (stdout, _stderr, _) = run_dcg(&["packs", "--format", "json"]);

    let json: serde_json::Value =
        serde_json::from_str(&stdout).expect("packs --format json should produce valid JSON");

    let packs = json["packs"].as_array().expect("packs should be an array");
    assert!(!packs.is_empty(), "should have at least one pack");

    // Verify first pack has required fields
    let first_pack = &packs[0];
    assert!(first_pack.get("id").is_some(), "pack should have 'id'");
    assert!(first_pack.get("name").is_some(), "pack should have 'name'");
    assert!(
        first_pack.get("enabled").is_some(),
        "pack should have 'enabled'"
    );
}

#[test]
fn test_packs_command_json_has_pattern_counts() {
    let (stdout, _stderr, _) = run_dcg(&["packs", "--format", "json"]);

    let json: serde_json::Value =
        serde_json::from_str(&stdout).expect("packs --format json should produce valid JSON");

    let packs = json["packs"].as_array().unwrap();
    let first_pack = &packs[0];

    // Should include pattern counts for agent awareness
    assert!(
        first_pack.get("safe_pattern_count").is_some()
            || first_pack.get("safePatternCount").is_some(),
        "pack should have safe pattern count"
    );
    assert!(
        first_pack.get("destructive_pattern_count").is_some()
            || first_pack.get("destructivePatternCount").is_some(),
        "pack should have destructive pattern count"
    );
}

#[test]
fn test_packs_command_json_contains_core_packs() {
    let (stdout, _stderr, _) = run_dcg(&["packs", "--format", "json"]);

    let json: serde_json::Value =
        serde_json::from_str(&stdout).expect("packs --format json should produce valid JSON");

    let packs = json["packs"].as_array().unwrap();

    // Core packs should always be present
    let pack_ids: Vec<&str> = packs.iter().filter_map(|p| p["id"].as_str()).collect();

    assert!(
        pack_ids.iter().any(|id| id.starts_with("core.")),
        "should contain core.* packs"
    );
}

// =============================================================================
// Scan Command JSON Output (if applicable)
// =============================================================================

#[test]
fn test_scan_command_json_valid() {
    // Create a temp directory with a test file
    let temp_dir = std::env::temp_dir().join("dcg_test_scan");
    let _ = std::fs::create_dir_all(&temp_dir);
    let test_file = temp_dir.join("test.sh");
    std::fs::write(&test_file, "#!/bin/bash\necho hello\n").ok();

    let (stdout, stderr, exit_code) = run_dcg(&[
        "scan",
        "--format",
        "json",
        "--paths",
        temp_dir.to_str().unwrap(),
    ]);

    // Cleanup
    let _ = std::fs::remove_dir_all(&temp_dir);

    assert_eq!(
        exit_code, 0,
        "scan --format json should exit 0\nstderr: {stderr}"
    );

    if !stdout.is_empty() {
        let json: serde_json::Value =
            serde_json::from_str(&stdout).expect("scan --format json should produce valid JSON");

        // Scan output should have findings array
        assert!(
            json.get("findings").is_some() || json.get("results").is_some(),
            "scan should have 'findings' or 'results' field"
        );
    }
}

// =============================================================================
// JSON Output Consistency Tests
// =============================================================================

#[test]
fn test_all_json_outputs_are_objects() {
    // All JSON outputs should be objects (not arrays or primitives at root)
    let commands = [
        vec!["test", "--format", "json", "echo hello"],
        vec!["explain", "--format", "json", "git status"],
        vec!["packs", "--format", "json"],
    ];

    for cmd_args in commands {
        let (stdout, stderr, exit_code) = run_dcg(&cmd_args.iter().copied().collect::<Vec<_>>());

        assert_eq!(
            exit_code, 0,
            "command {:?} should exit 0\nstderr: {stderr}",
            cmd_args
        );

        if !stdout.is_empty() {
            let json: serde_json::Value = serde_json::from_str(&stdout).unwrap_or_else(|e| {
                panic!(
                    "command {:?} should produce valid JSON: {e}\nstdout: {stdout}",
                    cmd_args
                )
            });

            assert!(
                json.is_object(),
                "command {:?} JSON root should be an object",
                cmd_args
            );
        }
    }
}

#[test]
fn test_json_outputs_parseable_by_jq() {
    // Verify outputs can be parsed by common JSON tools (simulated by serde)
    let commands = [
        vec!["test", "--format", "json", "git reset --hard"],
        vec!["packs", "--format", "json"],
    ];

    for cmd_args in commands {
        let (stdout, _stderr, _) = run_dcg(&cmd_args.iter().copied().collect::<Vec<_>>());

        if !stdout.is_empty() {
            // Strict parsing - should not have trailing content
            let trimmed = stdout.trim();
            let _: serde_json::Value = serde_json::from_str(trimmed).unwrap_or_else(|e| {
                panic!("command {:?} JSON should be strictly valid: {e}", cmd_args)
            });
        }
    }
}

#[test]
fn test_json_no_trailing_newlines_or_garbage() {
    let (stdout, _stderr, _) = run_dcg(&["test", "--format", "json", "git reset --hard"]);

    if !stdout.is_empty() {
        // JSON should be a single valid document
        let trimmed = stdout.trim();

        // Should start with { and end with }
        assert!(
            trimmed.starts_with('{') && trimmed.ends_with('}'),
            "JSON should be a single object\nstdout: {stdout}"
        );

        // Should parse without extra content
        let _: serde_json::Value =
            serde_json::from_str(trimmed).expect("JSON should be valid without trailing content");
    }
}

// =============================================================================
// Decision Field Tests
// =============================================================================

#[test]
fn test_decision_values_are_lowercase() {
    let commands = [("git status", "allow"), ("git reset --hard", "deny")];

    for (cmd, expected_decision) in commands {
        let (stdout, _stderr, _) = run_dcg(&["test", "--format", "json", cmd]);

        let json: serde_json::Value =
            serde_json::from_str(&stdout).expect("should produce valid JSON");

        let decision = json["decision"].as_str().unwrap();
        assert_eq!(
            decision, expected_decision,
            "decision should be lowercase '{expected_decision}' for '{cmd}'"
        );
        assert_eq!(
            decision,
            decision.to_lowercase(),
            "decision should be lowercase"
        );
    }
}

#[test]
fn test_severity_values_are_lowercase() {
    let (stdout, _stderr, _) = run_dcg(&["test", "--format", "json", "git reset --hard"]);

    let json: serde_json::Value = serde_json::from_str(&stdout).expect("should produce valid JSON");

    // Check hook output if present
    if let Some(hook_output) = json.get("hookSpecificOutput") {
        if let Some(severity) = hook_output.get("severity") {
            let sev_str = severity.as_str().unwrap();
            assert_eq!(
                sev_str,
                sev_str.to_lowercase(),
                "severity should be lowercase"
            );
        }
    }
}