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
#!/usr/bin/env bats
# Unit tests for agent configuration functions in install.sh
#
# Tests:
# - Claude Code configuration (configure_claude_code)
# - Gemini CLI configuration (configure_gemini)
# - Configuration idempotency
# - Existing settings preservation

load test_helper

setup() {
    setup_isolated_home
    setup_test_log "$BATS_TEST_NAME"
    extract_install_functions

    # Set default DEST for configuration
    DEST="$TEST_TMPDIR/bin"
    mkdir -p "$DEST"

    # Create mock dcg binary for path references
    cat > "$DEST/dcg" << 'MOCKEOF'
#!/bin/bash
echo "dcg 1.0.0"
MOCKEOF
    chmod +x "$DEST/dcg"
}

teardown() {
    log_test "=== Test completed: $BATS_TEST_NAME (status: $status) ==="
    teardown_isolated_home
}

# ============================================================================
# Claude Code Configuration Tests
# ============================================================================

@test "configure_claude_code: creates settings.json when directory missing" {
    log_test "Testing Claude Code configuration with missing directory..."

    CLAUDE_SETTINGS="$HOME/.claude/settings.json"

    # Directory doesn't exist yet
    [ ! -d "$HOME/.claude" ]

    configure_claude_code "$CLAUDE_SETTINGS" "0"

    log_test "Settings file exists: $([ -f "$CLAUDE_SETTINGS" ] && echo yes || echo no)"
    log_test "Settings content: $(cat "$CLAUDE_SETTINGS" 2>/dev/null || echo 'N/A')"

    [ -f "$CLAUDE_SETTINGS" ]
    grep -q "dcg" "$CLAUDE_SETTINGS"
}

@test "configure_claude_code: creates settings.json with correct hook structure" {
    log_test "Testing Claude Code hook structure..."

    CLAUDE_SETTINGS="$HOME/.claude/settings.json"

    configure_claude_code "$CLAUDE_SETTINGS" "0"

    log_test "Settings content: $(cat "$CLAUDE_SETTINGS")"

    # Check for required structure
    grep -q "PreToolUse" "$CLAUDE_SETTINGS"
    grep -q "Bash" "$CLAUDE_SETTINGS"
    grep -q "dcg" "$CLAUDE_SETTINGS"
}

@test "configure_claude_code: preserves existing settings" {
    log_test "Testing Claude Code existing settings preservation..."

    CLAUDE_SETTINGS="$HOME/.claude/settings.json"
    mkdir -p "$HOME/.claude"

    # Create existing settings with other content
    cat > "$CLAUDE_SETTINGS" << 'EOF'
{
  "theme": "dark",
  "fontSize": 14,
  "someOtherSetting": true
}
EOF

    log_test "Initial settings: $(cat "$CLAUDE_SETTINGS")"

    configure_claude_code "$CLAUDE_SETTINGS" "0"

    log_test "Final settings: $(cat "$CLAUDE_SETTINGS")"

    # Should have dcg hook
    grep -q "dcg" "$CLAUDE_SETTINGS"

    # Should preserve existing settings (python3 merge should keep them)
    # Note: This depends on python3 being available for merge
    if command -v python3 &>/dev/null; then
        grep -q "theme" "$CLAUDE_SETTINGS"
        grep -q "dark" "$CLAUDE_SETTINGS"
    fi
}

@test "configure_claude_code: is idempotent" {
    log_test "Testing Claude Code config idempotency..."

    CLAUDE_SETTINGS="$HOME/.claude/settings.json"
    mkdir -p "$HOME/.claude"

    # Create settings with dcg hook already present
    cat > "$CLAUDE_SETTINGS" << EOF
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {"type": "command", "command": "$DEST/dcg"}
        ]
      }
    ]
  }
}
EOF

    local before
    before=$(cat "$CLAUDE_SETTINGS")
    log_test "Before: $before"

    configure_claude_code "$CLAUDE_SETTINGS" "0"

    local after
    after=$(cat "$CLAUDE_SETTINGS")
    log_test "After: $after"

    # CLAUDE_STATUS should be "already"
    [ "$CLAUDE_STATUS" = "already" ]
}

@test "configure_claude_code: does not duplicate hooks" {
    log_test "Testing Claude Code no duplicate hooks..."

    CLAUDE_SETTINGS="$HOME/.claude/settings.json"
    mkdir -p "$HOME/.claude"
    echo '{}' > "$CLAUDE_SETTINGS"

    # Configure twice
    configure_claude_code "$CLAUDE_SETTINGS" "0"
    configure_claude_code "$CLAUDE_SETTINGS" "0"

    log_test "Final settings: $(cat "$CLAUDE_SETTINGS")"

    # Count dcg occurrences in command fields
    local dcg_count
    dcg_count=$(grep -o '"command".*dcg' "$CLAUDE_SETTINGS" | wc -l)
    log_test "dcg command count: $dcg_count"

    # Second call should detect already configured
    [ "$dcg_count" -le 1 ]
}

# ============================================================================
# Gemini CLI Configuration Tests
# ============================================================================

@test "configure_gemini: skips when not installed" {
    log_test "Testing Gemini CLI skips when not installed..."

    GEMINI_SETTINGS="$HOME/.gemini/settings.json"

    # Gemini not installed (no directory, no command)
    configure_gemini "$GEMINI_SETTINGS"

    log_test "GEMINI_STATUS: $GEMINI_STATUS"

    [ "$GEMINI_STATUS" = "skipped" ]
}

@test "configure_gemini: creates settings.json when directory exists" {
    log_test "Testing Gemini CLI configuration..."

    GEMINI_SETTINGS="$HOME/.gemini/settings.json"
    setup_mock_gemini
    rm -f "$GEMINI_SETTINGS"  # Remove the mock settings

    configure_gemini "$GEMINI_SETTINGS"

    log_test "Settings file exists: $([ -f "$GEMINI_SETTINGS" ] && echo yes || echo no)"
    log_test "Settings content: $(cat "$GEMINI_SETTINGS" 2>/dev/null || echo 'N/A')"

    [ -f "$GEMINI_SETTINGS" ]
    grep -q "dcg" "$GEMINI_SETTINGS"
}

@test "configure_gemini: uses BeforeTool hook type" {
    log_test "Testing Gemini CLI uses BeforeTool..."

    GEMINI_SETTINGS="$HOME/.gemini/settings.json"
    setup_mock_gemini
    rm -f "$GEMINI_SETTINGS"

    configure_gemini "$GEMINI_SETTINGS"

    log_test "Settings content: $(cat "$GEMINI_SETTINGS")"

    # Gemini uses BeforeTool instead of PreToolUse
    grep -q "BeforeTool" "$GEMINI_SETTINGS"
    grep -q "run_shell_command" "$GEMINI_SETTINGS"
}

@test "configure_gemini: is idempotent" {
    log_test "Testing Gemini CLI config idempotency..."

    GEMINI_SETTINGS="$HOME/.gemini/settings.json"
    setup_mock_gemini

    # Create settings with dcg hook already present
    cat > "$GEMINI_SETTINGS" << EOF
{
  "hooks": {
    "BeforeTool": [
      {
        "matcher": "run_shell_command",
        "hooks": [
          {"name": "dcg", "type": "command", "command": "$DEST/dcg", "timeout": 5000}
        ]
      }
    ]
  }
}
EOF

    configure_gemini "$GEMINI_SETTINGS"

    log_test "GEMINI_STATUS: $GEMINI_STATUS"

    [ "$GEMINI_STATUS" = "already" ]
}

# ============================================================================
# Predecessor Migration Tests
# ============================================================================

@test "configure_claude_code: removes predecessor hook when requested" {
    log_test "Testing predecessor removal..."

    # Skip if python3 not available (needed for JSON manipulation)
    command -v python3 &>/dev/null || skip "python3 not available"

    CLAUDE_SETTINGS="$HOME/.claude/settings.json"
    mkdir -p "$HOME/.claude"

    # Create settings with predecessor hook
    cat > "$CLAUDE_SETTINGS" << 'EOF'
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {"type": "command", "command": "/path/to/git_safety_guard.py"}
        ]
      }
    ]
  }
}
EOF

    log_test "Before: $(cat "$CLAUDE_SETTINGS")"

    # Configure with cleanup_predecessor=1
    configure_claude_code "$CLAUDE_SETTINGS" "1"

    log_test "After: $(cat "$CLAUDE_SETTINGS")"

    # Should have dcg
    grep -q "dcg" "$CLAUDE_SETTINGS"

    # Should NOT have git_safety_guard
    ! grep -q "git_safety_guard" "$CLAUDE_SETTINGS"
}

@test "configure_claude_code: keeps predecessor when not requested" {
    log_test "Testing predecessor preservation..."

    # Skip if python3 not available
    command -v python3 &>/dev/null || skip "python3 not available"

    CLAUDE_SETTINGS="$HOME/.claude/settings.json"
    mkdir -p "$HOME/.claude"

    # Create settings with predecessor hook
    cat > "$CLAUDE_SETTINGS" << 'EOF'
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {"type": "command", "command": "/path/to/git_safety_guard.py"}
        ]
      }
    ]
  }
}
EOF

    log_test "Before: $(cat "$CLAUDE_SETTINGS")"

    # Configure with cleanup_predecessor=0
    configure_claude_code "$CLAUDE_SETTINGS" "0"

    log_test "After: $(cat "$CLAUDE_SETTINGS")"

    # Should have dcg
    grep -q "dcg" "$CLAUDE_SETTINGS"

    # Should still have git_safety_guard
    grep -q "git_safety_guard" "$CLAUDE_SETTINGS"
}

# ============================================================================
# Edge Cases
# ============================================================================

@test "configure_claude_code: handles malformed JSON gracefully" {
    log_test "Testing malformed JSON handling..."

    CLAUDE_SETTINGS="$HOME/.claude/settings.json"
    mkdir -p "$HOME/.claude"

    # Create malformed JSON
    echo "not valid json {{{" > "$CLAUDE_SETTINGS"

    log_test "Malformed content: $(cat "$CLAUDE_SETTINGS")"

    # This might fail or succeed depending on implementation
    # The key is it shouldn't crash
    configure_claude_code "$CLAUDE_SETTINGS" "0" || true

    log_test "CLAUDE_STATUS: $CLAUDE_STATUS"
    log_test "After: $(cat "$CLAUDE_SETTINGS" 2>/dev/null || echo 'N/A')"

    # Either status should be set
    [ -n "$CLAUDE_STATUS" ]
}

@test "configure_claude_code: handles empty settings file" {
    log_test "Testing empty settings file..."

    CLAUDE_SETTINGS="$HOME/.claude/settings.json"
    mkdir -p "$HOME/.claude"

    # Create empty file
    touch "$CLAUDE_SETTINGS"

    configure_claude_code "$CLAUDE_SETTINGS" "0"

    log_test "CLAUDE_STATUS: $CLAUDE_STATUS"
    log_test "After: $(cat "$CLAUDE_SETTINGS")"

    # Should have added dcg hook
    grep -q "dcg" "$CLAUDE_SETTINGS"
}

@test "configure_claude_code: handles settings with empty hooks array" {
    log_test "Testing empty hooks array..."

    CLAUDE_SETTINGS="$HOME/.claude/settings.json"
    mkdir -p "$HOME/.claude"

    cat > "$CLAUDE_SETTINGS" << 'EOF'
{
  "hooks": {}
}
EOF

    configure_claude_code "$CLAUDE_SETTINGS" "0"

    log_test "CLAUDE_STATUS: $CLAUDE_STATUS"
    log_test "After: $(cat "$CLAUDE_SETTINGS")"

    # Should have added dcg hook
    grep -q "dcg" "$CLAUDE_SETTINGS"
}

# ============================================================================
# Aider Configuration Tests
# ============================================================================

@test "configure_aider: skips when not installed" {
    log_test "Testing Aider skips when not installed..."

    AIDER_SETTINGS="$HOME/.aider.conf.yml"

    # Aider not installed (no command in our isolated PATH)
    configure_aider "$AIDER_SETTINGS"

    log_test "AIDER_STATUS: $AIDER_STATUS"

    [ "$AIDER_STATUS" = "skipped" ]
}

@test "configure_aider: creates config file when installed" {
    log_test "Testing Aider configuration creation..."

    setup_mock_aider
    AIDER_SETTINGS="$HOME/.aider.conf.yml"

    # No existing config
    [ ! -f "$AIDER_SETTINGS" ]

    configure_aider "$AIDER_SETTINGS"

    log_test "AIDER_STATUS: $AIDER_STATUS"
    log_test "Config content: $(cat "$AIDER_SETTINGS" 2>/dev/null || echo 'N/A')"

    [ -f "$AIDER_SETTINGS" ]
    [ "$AIDER_STATUS" = "created" ]
    grep -q "git-commit-verify: true" "$AIDER_SETTINGS"
}

@test "configure_aider: sets git-commit-verify to true" {
    log_test "Testing Aider git-commit-verify setting..."

    setup_mock_aider
    AIDER_SETTINGS="$HOME/.aider.conf.yml"

    configure_aider "$AIDER_SETTINGS"

    log_test "Config content: $(cat "$AIDER_SETTINGS")"

    # Must have git-commit-verify: true
    grep -qE "git-commit-verify:\s*true" "$AIDER_SETTINGS"
}

@test "configure_aider: updates false to true" {
    log_test "Testing Aider updates git-commit-verify from false to true..."

    setup_mock_aider
    AIDER_SETTINGS="$HOME/.aider.conf.yml"

    # Create config with git-commit-verify: false
    cat > "$AIDER_SETTINGS" << 'EOF'
# Aider config
model: gpt-4
git-commit-verify: false
auto-commits: true
EOF

    log_test "Before: $(cat "$AIDER_SETTINGS")"

    configure_aider "$AIDER_SETTINGS"

    log_test "AIDER_STATUS: $AIDER_STATUS"
    log_test "After: $(cat "$AIDER_SETTINGS")"

    # Should now be true
    grep -qE "git-commit-verify:\s*true" "$AIDER_SETTINGS"
    [ "$AIDER_STATUS" = "merged" ]
}

@test "configure_aider: appends setting to existing config" {
    log_test "Testing Aider appends to existing config..."

    setup_mock_aider
    AIDER_SETTINGS="$HOME/.aider.conf.yml"

    # Create config without git-commit-verify
    cat > "$AIDER_SETTINGS" << 'EOF'
# Aider config
model: gpt-4
auto-commits: true
EOF

    log_test "Before: $(cat "$AIDER_SETTINGS")"

    configure_aider "$AIDER_SETTINGS"

    log_test "AIDER_STATUS: $AIDER_STATUS"
    log_test "After: $(cat "$AIDER_SETTINGS")"

    # Should have the setting added
    grep -qE "git-commit-verify:\s*true" "$AIDER_SETTINGS"
    # Should preserve existing settings
    grep -q "model: gpt-4" "$AIDER_SETTINGS"
    [ "$AIDER_STATUS" = "merged" ]
}

@test "configure_aider: is idempotent" {
    log_test "Testing Aider config idempotency..."

    setup_mock_aider
    AIDER_SETTINGS="$HOME/.aider.conf.yml"

    # Create config with git-commit-verify already true
    cat > "$AIDER_SETTINGS" << 'EOF'
# Aider config
git-commit-verify: true
model: gpt-4
EOF

    configure_aider "$AIDER_SETTINGS"

    log_test "AIDER_STATUS: $AIDER_STATUS"

    [ "$AIDER_STATUS" = "already" ]
}

@test "configure_aider: creates backup when modifying" {
    log_test "Testing Aider creates backup..."

    setup_mock_aider
    AIDER_SETTINGS="$HOME/.aider.conf.yml"

    # Create config with git-commit-verify: false
    cat > "$AIDER_SETTINGS" << 'EOF'
model: gpt-4
git-commit-verify: false
EOF

    configure_aider "$AIDER_SETTINGS"

    log_test "AIDER_BACKUP: $AIDER_BACKUP"

    # Should have created backup
    [ -n "$AIDER_BACKUP" ]
    [ -f "$AIDER_BACKUP" ]
}

# ============================================================================
# Continue Configuration Tests
# ============================================================================

@test "configure_continue: skips when not installed" {
    log_test "Testing Continue skips when not installed..."

    # Continue not installed (no directory, no command)
    configure_continue

    log_test "CONTINUE_STATUS: $CONTINUE_STATUS"

    [ "$CONTINUE_STATUS" = "skipped" ]
}

@test "configure_continue: detects via ~/.continue directory" {
    log_test "Testing Continue detection via directory..."

    setup_mock_continue

    configure_continue

    log_test "CONTINUE_STATUS: $CONTINUE_STATUS"

    # Should be unsupported (detected but no hooks available)
    [ "$CONTINUE_STATUS" = "unsupported" ]
}

@test "configure_continue: detects via cn command" {
    log_test "Testing Continue detection via cn command..."

    # Create mock cn binary
    mkdir -p "$TEST_TMPDIR/bin"
    cat > "$TEST_TMPDIR/bin/cn" << 'EOF'
#!/bin/bash
echo "Continue CLI v1.0.0"
EOF
    chmod +x "$TEST_TMPDIR/bin/cn"
    export PATH="$TEST_TMPDIR/bin:$PATH"

    configure_continue

    log_test "CONTINUE_STATUS: $CONTINUE_STATUS"

    # Should be unsupported (detected but no hooks available)
    [ "$CONTINUE_STATUS" = "unsupported" ]
}

@test "configure_continue: reports unsupported (no shell command hooks)" {
    log_test "Testing Continue reports unsupported status..."

    setup_mock_continue

    configure_continue

    log_test "CONTINUE_STATUS: $CONTINUE_STATUS"

    # Continue does not have shell command hooks like Claude Code or Gemini
    # Status should be "unsupported" to indicate detection but no auto-config
    [ "$CONTINUE_STATUS" = "unsupported" ]
}

# ============================================================================
# Codex CLI Detection Tests
# ============================================================================

@test "configure_codex: skips when not installed" {
    log_test "Testing Codex detection when not installed..."

    # Make sure .codex doesn't exist
    rm -rf "$HOME/.codex"

    configure_codex

    log_test "CODEX_STATUS: $CODEX_STATUS"

    # Should be skipped when not installed
    [ "$CODEX_STATUS" = "skipped" ]
}

@test "configure_codex: detects via .codex directory" {
    log_test "Testing Codex detection via .codex directory..."

    setup_mock_codex

    configure_codex

    log_test "CODEX_STATUS: $CODEX_STATUS"

    # Should be unsupported (detected but no pre-execution hooks)
    [ "$CODEX_STATUS" = "unsupported" ]
}

@test "configure_codex: detects via codex command" {
    log_test "Testing Codex detection via codex command..."

    # Create mock codex binary
    mkdir -p "$TEST_TMPDIR/bin"
    cat > "$TEST_TMPDIR/bin/codex" << 'EOF'
#!/bin/bash
echo "Codex CLI v1.0.0"
EOF
    chmod +x "$TEST_TMPDIR/bin/codex"
    export PATH="$TEST_TMPDIR/bin:$PATH"

    configure_codex

    log_test "CODEX_STATUS: $CODEX_STATUS"

    # Should be unsupported (detected but no pre-execution hooks)
    [ "$CODEX_STATUS" = "unsupported" ]
}

@test "configure_codex: reports unsupported (no pre-execution hooks)" {
    log_test "Testing Codex reports unsupported status..."

    setup_mock_codex

    configure_codex

    log_test "CODEX_STATUS: $CODEX_STATUS"

    # Codex CLI does not have pre-execution command hooks
    # Status should be "unsupported" to indicate detection but no auto-config
    [ "$CODEX_STATUS" = "unsupported" ]
}