#!/bin/bash
# Unit tests for ~/.claude/hooks/email-send-gate.sh. Run as: bash test_gate.sh
# (kept in a file so the Bash command line itself never contains the trigger words)
H="${H:-$HOME/.claude/hooks/email-send-gate.sh}"
# Test runs never append to the real gate log: the judge honours EMAIL_GATE_LOG.
if [ -z "${EMAIL_GATE_LOG:-}" ]; then EMAIL_GATE_LOG=$(mktemp /tmp/email-gate-test-log.XXXXXX); _GATE_LOG_TMP=1; fi; export EMAIL_GATE_LOG
pass=0; fail=0
check() { # name expected payload
  local got; got=$(printf '%s' "$3" | "$H")
  local dec="allow"
  case "$got" in *'"permissionDecision":"ask"'*) dec=ask;; *'"permissionDecision":"deny"'*) dec=deny;; esac
  if [ "$dec" = "$2" ]; then pass=$((pass+1)); echo "ok    $1 -> $dec"; else fail=$((fail+1)); echo "FAIL  $1 -> $dec (expected $2)"; echo "      $got" | head -c 400; echo; fi
}
D='"description":"Send email from oren.magid@marquette.edu to david.winograd@marquette.edu, subject \"Module 9 test\""'
check "apple-mail-send-with-sender+desc" ask '{"tool_name":"Bash","tool_input":{'"$D"',"command":"osascript <<X\ntell application \"Mail\"\n set m to make new outgoing message with properties {subject:\"Module 9 test\", sender:\"Oren Magid <oren.magid@marquette.edu>\"}\n make new to recipient at end of to recipients with properties {address:\"david.winograd@marquette.edu\"}\n send\nend tell\nX"}}'
check "apple-mail-send-no-description" deny '{"tool_name":"Bash","tool_input":{"description":"Send the notice to the instructor","command":"osascript <<X\ntell application \"Mail\"\n set m to make new outgoing message with properties {subject:\"Module 9 test\", sender:\"Oren Magid <oren.magid@marquette.edu>\"}\n make new to recipient at end of to recipients with properties {address:\"david.winograd@marquette.edu\"}\n send\nend tell\nX"}}'
check "apple-mail-send-desc-wrong-from" deny '{"tool_name":"Bash","tool_input":{"description":"Send email from oren.michael.magid@gmail.com to david.winograd@marquette.edu, subject \"Module 9 test\"","command":"osascript <<X\ntell application \"Mail\"\n set m to make new outgoing message with properties {subject:\"Module 9 test\", sender:\"Oren Magid <oren.magid@marquette.edu>\"}\n make new to recipient at end of to recipients with properties {address:\"david.winograd@marquette.edu\"}\n send\nend tell\nX"}}'
check "apple-mail-send-desc-missing-recipient" deny '{"tool_name":"Bash","tool_input":{"description":"Send email from oren.magid@marquette.edu to the instructor, subject \"Module 9 test\"","command":"osascript <<X\ntell application \"Mail\"\n set m to make new outgoing message with properties {subject:\"Module 9 test\", sender:\"Oren Magid <oren.magid@marquette.edu>\"}\n make new to recipient at end of to recipients with properties {address:\"david.winograd@marquette.edu\"}\n send\nend tell\nX"}}'
check "apple-mail-send-sender-set-later+desc" ask '{"tool_name":"Bash","tool_input":{"description":"Send email from oren@orenmagid.com to a@b.edu, subject \"x\"","command":"osascript -e '"'"'tell application \"Mail\"\nset m to make new outgoing message with properties {subject:\"x\"}\nset sender of m to \"Oren Magid <oren@orenmagid.com>\"\nmake new to recipient at end of to recipients of m with properties {address:\"a@b.edu\"}\nsend m\nend tell'"'"'"}}'
check "apple-mail-send-no-sender" deny '{"tool_name":"Bash","tool_input":{"description":"Send email from x to y","command":"osascript -e '"'"'tell application \"Mail\" to tell (make new outgoing message with properties {subject:\"x\"}) to send'"'"'"}}'
check "apple-mail-send-no-recipient-parsed" deny '{"tool_name":"Bash","tool_input":{"description":"Send email from oren@orenmagid.com to a@b.edu, subject \"x\"","command":"osascript -e '"'"'tell application \"Mail\"\nset r to reply m with opening window\nset sender of r to \"Oren Magid <oren@orenmagid.com>\"\nsend r\nend tell'"'"'"}}'
check "apple-mail-draft-only" allow '{"tool_name":"Bash","tool_input":{"description":"Save a draft","command":"osascript -e '"'"'tell application \"Mail\"\nset m to make new outgoing message with properties {subject:\"x\", visible:true}\nset sender of m to \"Oren Magid <oren@orenmagid.com>\"\nsave m\nend tell'"'"'"}}'
check "apple-mail-read-only" allow '{"tool_name":"Bash","tool_input":{"description":"Read inbox","command":"osascript -e '"'"'tell application \"Mail\" to get {subject, sender} of messages of inbox'"'"'"}}'
check "unrelated-words" allow '{"tool_name":"Bash","tool_input":{"description":"grep","command":"grep sender foo; echo sent; echo send-off; git send-email --dry-run"}}'
check "sendmail-cli-no-desc" deny '{"tool_name":"Bash","tool_input":{"description":"mail it","command":"echo hi | sendmail x@y.com"}}'
check "sendmail-cli-with-desc" ask '{"tool_name":"Bash","tool_input":{"description":"Send email from me@a.com to x@y.com, subject \"hi\"","command":"echo hi | sendmail x@y.com"}}'
check "gmail-connector" ask '{"tool_name":"mcp__claude_ai_Gmail__send_message","tool_input":{"to":["a@b.com"],"subject":"Hello"}}'
check "gmail-connector-reply" ask '{"tool_name":"mcp__claude_ai_Gmail__reply","tool_input":{"message_id":"abc"}}'
check "other-tool" allow '{"tool_name":"Read","tool_input":{"file_path":"/tmp/x"}}'
check "garbage-stdin" allow 'not json'
check_reason_has() { # name needle payload
  local got; got=$(printf '%s' "$3" | "$H")
  case "$got" in *"$2"*) pass=$((pass+1)); echo "ok    $1 (reason names: $2)";; *) fail=$((fail+1)); echo "FAIL  $1 — reason lacks: $2"; echo "      $got" | head -c 400; echo;; esac
}
# The Gmail connector's From is configured, not read from the call: EMAIL_GATE_GMAIL_ACCOUNT wins,
# then ~/.claude-cabinet/email-gate.json {"gmailAccount"}, else the prompt still asks but names no account.
CFG_HOME=$(mktemp -d /tmp/gate-cfg-home.XXXXXX); mkdir -p "$CFG_HOME/.claude-cabinet"
printf '{"gmailAccount":"cfg@example.com"}' > "$CFG_HOME/.claude-cabinet/email-gate.json"
NOCFG_HOME=$(mktemp -d /tmp/gate-nocfg-home.XXXXXX)
HOME="$NOCFG_HOME" EMAIL_GATE_GMAIL_ACCOUNT=env@example.com check_reason_has "gmail-connector-env-names-account" "from env@example.com (Gmail connector)" '{"tool_name":"mcp__claude_ai_Gmail__send_message","tool_input":{"to":["a@b.com"],"subject":"Hello"}}'
HOME="$CFG_HOME" EMAIL_GATE_GMAIL_ACCOUNT= check_reason_has "gmail-connector-config-names-account" "from cfg@example.com (Gmail connector)" '{"tool_name":"mcp__claude_ai_Gmail__send_message","tool_input":{"to":["a@b.com"],"subject":"Hello"}}'
HOME="$CFG_HOME" EMAIL_GATE_GMAIL_ACCOUNT=env@example.com check_reason_has "gmail-connector-env-beats-config" "from env@example.com (Gmail connector)" '{"tool_name":"mcp__claude_ai_Gmail__send_message","tool_input":{"to":["a@b.com"],"subject":"Hello"}}'
HOME="$NOCFG_HOME" EMAIL_GATE_GMAIL_ACCOUNT= check "gmail-connector-unconfigured-still-asks" ask '{"tool_name":"mcp__claude_ai_Gmail__send_message","tool_input":{"to":["a@b.com"],"subject":"Hello"}}'
HOME="$NOCFG_HOME" EMAIL_GATE_GMAIL_ACCOUNT= check_reason_has "gmail-connector-unconfigured-is-unnamed" "the connected Gmail account (unnamed" '{"tool_name":"mcp__claude_ai_Gmail__send_message","tool_input":{"to":["a@b.com"],"subject":"Hello"}}'
rm -rf "$CFG_HOME" "$NOCFG_HOME"
# The log override: this suite's ask lines landed in EMAIL_GATE_LOG, not the real log.
if grep -q 'ask mcp__claude_ai_Gmail__send_message' "$EMAIL_GATE_LOG"; then pass=$((pass+1)); echo "ok    log-override-honoured"; else fail=$((fail+1)); echo "FAIL  log-override-honoured — no ask line in $EMAIL_GATE_LOG"; fi
echo "passed=$pass failed=$fail"
[ -n "${_GATE_LOG_TMP:-}" ] && rm -f "$EMAIL_GATE_LOG"
[ "$fail" -eq 0 ]
