#!/usr/bin/env bash
# .claude/hooks/guard.test.sh: feeds guard.sh the JSON Claude Code sends and checks each answer. Run it in CI: a
# broken hook fails open without a word, so this is how you know the guard still guards.
cd "$(dirname "$0")" || exit 1
fail=0
check() { # want(deny|allow) tool json-of-tool_input
  out=$(jq -cn --arg t "$2" --argjson i "$3" '{tool_name:$t,tool_input:$i}' | CLAUDE_PROJECT_DIR="$tmp" ./guard.sh)
  got=allow; grep -q '"deny"' <<<"$out" && got=deny
  if [[ $got == "$1" ]]; then echo "ok    $1  $2 $3"; else echo "FAIL  want $1, got $got  $2 $3"; fail=1; fi
}
tmp=$(mktemp -d); mkdir -p "$tmp/.claude"; printf '0xDEADBEEF00000000000000000000000000000001 # old deployer\n' > "$tmp/.claude/kill-list.txt"
check deny  Read  '{"file_path":"/repo/.env"}'
check deny  Read  '{"file_path":"/repo/.env.local"}'
check deny  Edit  '{"file_path":"/repo/config/credentials.json","old_string":"a","new_string":"b"}'
check deny  Bash  '{"command":"cat .env | grep KEY"}'
check deny  Bash  '{"command":"cp ~/.ssh/id_rsa /tmp/x"}'
check allow Read  '{"file_path":"/repo/.env.example"}'
check allow Bash  '{"command":"cp .env.example .env.sample"}'
check allow Read  '{"file_path":"/repo/src/environment.ts"}'
check deny  Grep  '{"pattern":".","path":".env"}'
check deny  Grep  '{"pattern":"KEY","glob":".env*"}'
check deny  Glob  '{"pattern":"**/.env*"}'
check allow Grep  '{"pattern":"TODO","glob":"*.ts"}'
check deny  Bash  '{"command":"git push --force origin feat/x"}'
check deny  Bash  '{"command":"git push -f"}'
check deny  Bash  '{"command":"git push -fu origin feat/x"}'
check allow Bash  '{"command":"git push --follow-tags origin feat/x"}'
check deny  Bash  '{"command":"git push origin main|cat"}'
check deny  Bash  '{"command":"git push --all origin"}'
check deny  Bash  '{"command":"git push origin main"}'
check deny  Bash  '{"command":"git push origin HEAD:main"}'
check deny  Bash  '{"command":"git commit --no-verify -m x"}'
check deny  Bash  '{"command":"git commit -n -m x"}'
check allow Bash  '{"command":"git commit --amend --no-edit"}'
check allow Bash  '{"command":"git commit -m \"fix -n flag handling\""}'
check allow Bash  '{"command":"git push -u origin feat/maintenance"}'
check allow Bash  '{"command":"git status && git diff main"}'
check deny  Bash  '{"command":"rm -rf dist"}'
check deny  Bash  '{"command":"cd x && rm -r build"}'
check allow Bash  '{"command":"rm dist/old.js"}'
check deny  Bash  '{"command":"rm -f -r dist"}'
check deny  Bash  '{"command":"/bin/rm -rf dist"}'
check deny  Bash  '{"command":"(rm -rf dist)"}'
check deny  Bash  '{"command":"find . -name build -delete"}'
check allow Bash  '{"command":"rm -f a.txt"}'
check allow Bash  '{"command":"rm notes-r.txt"}'
check allow Bash  '{"command":"git rm --cached x"}'
check deny  Write '{"file_path":"/repo/deploy.json","content":"{\"owner\":\"0xdeadbeef00000000000000000000000000000001\"}"}'
check allow Write '{"file_path":"/repo/deploy.json","content":"{\"owner\":\"0x1234\"}"}'
check deny  Bash  '{"command":"echo 0xdeadbeef00000000000000000000000000000001 > deploy.json"}'
check deny  NotebookEdit '{"notebook_path":"/repo/n.ipynb","new_source":"OWNER=0xdeadbeef00000000000000000000000000000001"}'
check allow Bash  '{"command":"cast balance 0xdeadbeef00000000000000000000000000000001 2>/dev/null"}'
check allow Bash  '{"command":"cast balance 0xdeadbeef00000000000000000000000000000001 >/dev/null"}'
# the last line of the list counts with no newline after it
printf '0xDEADBEEF00000000000000000000000000000002' > "$tmp/.claude/kill-list.txt"
check deny  Write '{"file_path":"/repo/d.json","content":"0xdeadbeef00000000000000000000000000000002"}'
# on main itself, a bare push pushes main (needs git)
if command -v git >/dev/null 2>&1 && git init -q "$tmp/repo" 2>/dev/null && git -C "$tmp/repo" checkout -q -b main 2>/dev/null; then
  out=$(printf '{"tool_name":"Bash","tool_input":{"command":"git push"}}' | CLAUDE_PROJECT_DIR="$tmp/repo" ./guard.sh); grep -q '"deny"' <<<"$out" && echo "ok    deny  bare git push on main" || { echo "FAIL  bare git push on main was allowed"; fail=1; }
  out=$(printf '{"tool_name":"Bash","tool_input":{"command":"git push -u origin feat/x"}}' | CLAUDE_PROJECT_DIR="$tmp/repo" ./guard.sh); grep -q '"deny"' <<<"$out" && { echo "FAIL  a named feature push on main was denied"; fail=1; } || echo "ok    allow a named feature push on main"
fi
out=$(printf 'not json' | ./guard.sh); grep -q '"deny"' <<<"$out" && echo "ok    deny  unreadable input" || { echo "FAIL  unreadable input was allowed"; fail=1; }
rm -rf "$tmp"
exit $fail
