#!/bin/bash
# Cases from the 2026-10-26 peer report (flow-prospect-niches-19) plus the description requirement.
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() {
  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@orenmagid.com to k@roxo.com, subject \"x\""'
# the exact command shape from the peer report, with and without a proper description
check "osascript-e-singlequote-send+desc" ask '{"tool_name":"Bash","tool_input":{'"$D"',"command":"osascript -e '"'"'tell application \"Mail\"'"'"' -e '"'"'set m to make new outgoing message with properties {sender:\"oren@orenmagid.com\", subject:\"x\"}'"'"' -e '"'"'make new to recipient at end of to recipients of m with properties {address:\"k@roxo.com\"}'"'"' -e '"'"'send m'"'"' -e '"'"'end tell'"'"'"}}'
check "osascript-e-singlequote-send-no-desc" deny '{"tool_name":"Bash","tool_input":{"description":"Send the day-14 email","command":"osascript -e '"'"'tell application \"Mail\"'"'"' -e '"'"'set m to make new outgoing message with properties {sender:\"oren@orenmagid.com\", subject:\"x\"}'"'"' -e '"'"'make new to recipient at end of to recipients of m with properties {address:\"k@roxo.com\"}'"'"' -e '"'"'send m'"'"' -e '"'"'end tell'"'"'"}}'
check "osascript-e-doublequote-send+desc" ask '{"tool_name":"Bash","tool_input":{'"$D"',"command":"osascript -e \"tell application \\\"Mail\\\"\" -e \"set m to make new outgoing message with properties {sender:\\\"oren@orenmagid.com\\\", subject:\\\"x\\\"}\" -e \"make new to recipient at end of to recipients of m with properties {address:\\\"k@roxo.com\\\"}\" -e \"send m\" -e \"end tell\""}}'
check "osascript-e-singlequote-no-sender" deny '{"tool_name":"Bash","tool_input":{'"$D"',"command":"osascript -e '"'"'tell application \"Mail\"'"'"' -e '"'"'set m to make new outgoing message with properties {subject:\"x\"}'"'"' -e '"'"'send m'"'"' -e '"'"'end tell'"'"'"}}'
check "osascript-e-no-recipient-inline" deny '{"tool_name":"Bash","tool_input":{'"$D"',"command":"osascript -e '"'"'tell application \"Mail\" to set m to make new outgoing message with properties {sender:\"oren@orenmagid.com\"}'"'"' -e '"'"'tell application \"Mail\" to send m'"'"'"}}'
check "node-helper-send-slug-no-desc" deny '{"tool_name":"Bash","tool_input":{"description":"Send the follow-up","command":"cd ~/flow && node scripts/prospect/helper.mjs send abe-timmons"}}'
check "node-helper-send-slug+desc" ask '{"tool_name":"Bash","tool_input":{"description":"Send email from oren@orenmagid.com to abe@timmons.law, subject \"Following up\"","command":"cd ~/flow && node scripts/prospect/helper.mjs send abe-timmons"}}'
check "node-send-mail-script-no-desc" deny '{"tool_name":"Bash","tool_input":{"description":"run mailer","command":"node send-mail.mjs --to x@y.com"}}'
check "osascript-script-file-no-desc" deny '{"tool_name":"Bash","tool_input":{"description":"run outreach script","command":"osascript ~/bin/outreach.scpt abe"}}'
check "osascript-js-file+desc" ask '{"tool_name":"Bash","tool_input":{"description":"Send email from oren@orenmagid.com to a@b.com, subject \"hi\"","command":"osascript ~/bin/mailer.js"}}'
check "osascript-e-read-only-multi" allow '{"tool_name":"Bash","tool_input":{"description":"count inbox","command":"osascript -e '"'"'tell application \"Mail\"'"'"' -e '"'"'get count of messages of inbox'"'"' -e '"'"'end tell'"'"'"}}'
check "node-build" allow '{"tool_name":"Bash","tool_input":{"description":"build","command":"node scripts/build.mjs && npm test"}}'
check "git-send-email" allow '{"tool_name":"Bash","tool_input":{"description":"dry run","command":"git send-email --dry-run HEAD~1"}}'
check "node-script-word-sendoff" allow '{"tool_name":"Bash","tool_input":{"description":"report","command":"node scripts/report.mjs --title sendoff"}}'
check "calendar-osascript" allow '{"tool_name":"Bash","tool_input":{"description":"list calendars","command":"osascript -e '"'"'tell application \"Calendar\" to get name of every calendar'"'"'"}}'
echo "passed=$pass failed=$fail"
[ -n "${_GATE_LOG_TMP:-}" ] && rm -f "$EMAIL_GATE_LOG"
[ "$fail" -eq 0 ]
