|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Functional tests for Claude Code hooks. |
| 3 | +# Run from project root: ./.claude/hooks/test-hooks.sh |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +PROJ="$(cd "$(dirname "$0")/../.." && pwd)" |
| 8 | +PASS=0 |
| 9 | +FAIL=0 |
| 10 | + |
| 11 | +run_test() { |
| 12 | + local name="$1" hook="$2" input="$3" expect_exit="$4" expect_stderr="$5" |
| 13 | + |
| 14 | + actual_stderr=$(echo "$input" | CLAUDE_PROJECT_DIR="$PROJ" uv run python "$PROJ/.claude/hooks/$hook" 2>&1 1>/dev/null || true) |
| 15 | + actual_exit=$(echo "$input" | CLAUDE_PROJECT_DIR="$PROJ" uv run python "$PROJ/.claude/hooks/$hook" >/dev/null 2>/dev/null; echo $?) |
| 16 | + |
| 17 | + local ok=true |
| 18 | + |
| 19 | + if [[ "$actual_exit" != "$expect_exit" ]]; then |
| 20 | + echo "FAIL: $name — expected exit $expect_exit, got $actual_exit" |
| 21 | + ok=false |
| 22 | + fi |
| 23 | + |
| 24 | + if [[ -n "$expect_stderr" && "$actual_stderr" != *"$expect_stderr"* ]]; then |
| 25 | + echo "FAIL: $name — stderr missing '$expect_stderr'" |
| 26 | + echo " got: $actual_stderr" |
| 27 | + ok=false |
| 28 | + fi |
| 29 | + |
| 30 | + if [[ -z "$expect_stderr" && -n "$actual_stderr" ]]; then |
| 31 | + echo "FAIL: $name — expected no stderr, got: $actual_stderr" |
| 32 | + ok=false |
| 33 | + fi |
| 34 | + |
| 35 | + if $ok; then |
| 36 | + echo "PASS: $name" |
| 37 | + PASS=$((PASS + 1)) |
| 38 | + else |
| 39 | + FAIL=$((FAIL + 1)) |
| 40 | + fi |
| 41 | +} |
| 42 | + |
| 43 | +echo "=== guard.py (PreToolUse Write|Edit) ===" |
| 44 | + |
| 45 | +run_test "blocks .env" "guard.py" \ |
| 46 | + '{"tool_input":{"file_path":"'"$PROJ"'/.env"}}' \ |
| 47 | + "2" "BLOCKED: .env is a secrets file" |
| 48 | + |
| 49 | +run_test "blocks .env.production" "guard.py" \ |
| 50 | + '{"tool_input":{"file_path":"'"$PROJ"'/.env.production"}}' \ |
| 51 | + "2" "BLOCKED: .env.production is a secrets file" |
| 52 | + |
| 53 | +run_test "blocks idl/ files" "guard.py" \ |
| 54 | + '{"tool_input":{"file_path":"'"$PROJ"'/idl/pump_fun_idl.json"}}' \ |
| 55 | + "2" "source-of-truth from on-chain" |
| 56 | + |
| 57 | +run_test "blocks wallet.enc" "guard.py" \ |
| 58 | + '{"tool_input":{"file_path":"'"$PROJ"'/some/path/wallet.enc"}}' \ |
| 59 | + "2" "encrypted wallet keystore" |
| 60 | + |
| 61 | +run_test "blocks .pem files" "guard.py" \ |
| 62 | + '{"tool_input":{"file_path":"'"$PROJ"'/certs/server.pem"}}' \ |
| 63 | + "2" "credential file" |
| 64 | + |
| 65 | +run_test "blocks .key files" "guard.py" \ |
| 66 | + '{"tool_input":{"file_path":"'"$PROJ"'/certs/private.key"}}' \ |
| 67 | + "2" "credential file" |
| 68 | + |
| 69 | +run_test "allows normal Python files" "guard.py" \ |
| 70 | + '{"tool_input":{"file_path":"'"$PROJ"'/src/pumpfun_cli/cli.py"}}' \ |
| 71 | + "0" "" |
| 72 | + |
| 73 | +run_test "allows .env.example" "guard.py" \ |
| 74 | + '{"tool_input":{"file_path":"'"$PROJ"'/.env.example"}}' \ |
| 75 | + "0" "" |
| 76 | + |
| 77 | +run_test "allows README.md" "guard.py" \ |
| 78 | + '{"tool_input":{"file_path":"'"$PROJ"'/README.md"}}' \ |
| 79 | + "0" "" |
| 80 | + |
| 81 | +echo "" |
| 82 | +echo "=== bash-guard.py (PreToolUse Bash) — advisory only ===" |
| 83 | + |
| 84 | +run_test "warns on pumpfun buy (exit 0)" "bash-guard.py" \ |
| 85 | + '{"tool_input":{"command":"uv run pumpfun buy ABC123 0.1"}}' \ |
| 86 | + "0" "sends a Solana transaction" |
| 87 | + |
| 88 | +run_test "warns on pumpfun sell (exit 0)" "bash-guard.py" \ |
| 89 | + '{"tool_input":{"command":"pumpfun sell ABC123 100%"}}' \ |
| 90 | + "0" "sends a Solana transaction" |
| 91 | + |
| 92 | +run_test "warns on pumpfun transfer (exit 0)" "bash-guard.py" \ |
| 93 | + '{"tool_input":{"command":"uv run pumpfun transfer 1.0 dest"}}' \ |
| 94 | + "0" "sends a Solana transaction" |
| 95 | + |
| 96 | +run_test "warns on pumpfun launch (exit 0)" "bash-guard.py" \ |
| 97 | + '{"tool_input":{"command":"pumpfun launch --name TEST"}}' \ |
| 98 | + "0" "sends a Solana transaction" |
| 99 | + |
| 100 | +run_test "warns on pumpfun migrate (exit 0)" "bash-guard.py" \ |
| 101 | + '{"tool_input":{"command":"uv run pumpfun migrate ABC123"}}' \ |
| 102 | + "0" "sends a Solana transaction" |
| 103 | + |
| 104 | +run_test "warns on mainnet-test.sh (exit 0)" "bash-guard.py" \ |
| 105 | + '{"tool_input":{"command":"./scripts/mainnet-test.sh"}}' \ |
| 106 | + "0" "mainnet end-to-end tests" |
| 107 | + |
| 108 | +run_test "warns on rm .env (exit 0)" "bash-guard.py" \ |
| 109 | + '{"tool_input":{"command":"rm .env"}}' \ |
| 110 | + "0" "deletes a sensitive file" |
| 111 | + |
| 112 | +run_test "warns on rm wallet.enc (exit 0)" "bash-guard.py" \ |
| 113 | + '{"tool_input":{"command":"rm -f ~/.config/pumpfun-cli/wallet.enc"}}' \ |
| 114 | + "0" "deletes a sensitive file" |
| 115 | + |
| 116 | +run_test "warns on rm -rf idl/ (exit 0)" "bash-guard.py" \ |
| 117 | + '{"tool_input":{"command":"rm -rf idl/"}}' \ |
| 118 | + "0" "deletes IDL files" |
| 119 | + |
| 120 | +run_test "silent on pytest (exit 0)" "bash-guard.py" \ |
| 121 | + '{"tool_input":{"command":"uv run pytest tests/ -q"}}' \ |
| 122 | + "0" "" |
| 123 | + |
| 124 | +run_test "silent on git status (exit 0)" "bash-guard.py" \ |
| 125 | + '{"tool_input":{"command":"git status"}}' \ |
| 126 | + "0" "" |
| 127 | + |
| 128 | +run_test "silent on uv sync (exit 0)" "bash-guard.py" \ |
| 129 | + '{"tool_input":{"command":"uv sync --dev"}}' \ |
| 130 | + "0" "" |
| 131 | + |
| 132 | +echo "" |
| 133 | +echo "=== lint.py (PostToolUse Write|Edit) ===" |
| 134 | + |
| 135 | +run_test "runs on Python files (exit 0)" "lint.py" \ |
| 136 | + '{"tool_input":{"file_path":"'"$PROJ"'/src/pumpfun_cli/cli.py"}}' \ |
| 137 | + "0" "" |
| 138 | + |
| 139 | +run_test "skips non-Python files (exit 0)" "lint.py" \ |
| 140 | + '{"tool_input":{"file_path":"'"$PROJ"'/README.md"}}' \ |
| 141 | + "0" "" |
| 142 | + |
| 143 | +run_test "skips files outside project (exit 0)" "lint.py" \ |
| 144 | + '{"tool_input":{"file_path":"/tmp/random.py"}}' \ |
| 145 | + "0" "" |
| 146 | + |
| 147 | +echo "" |
| 148 | +echo "================================" |
| 149 | +echo "PASS: $PASS FAIL: $FAIL" |
| 150 | +if [[ $FAIL -gt 0 ]]; then |
| 151 | + echo "SOME TESTS FAILED" |
| 152 | + exit 1 |
| 153 | +else |
| 154 | + echo "ALL TESTS PASSED" |
| 155 | +fi |
0 commit comments