#!/bin/bash
# False-positive cases: interpreters editing text that mentions sends (2026-10-27, this desk + prospect desk).
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
}
check "perl-edit-memory-mentioning-send" allow '{"tool_name":"Bash","tool_input":{"description":"Update memory file","command":"perl -0pi -e '"'"'s/old line/REFUSES a send; sender: and Send email from x to y/'"'"' ~/.claude/projects/x/memory/foo.md"}}'
check "python-heredoc-mentioning-send-email" allow '{"tool_name":"Bash","tool_input":{"description":"Record the rule","command":"cd ~/x && python3 - <<'"'"'EOF'"'"'\np=\"m.md\"; s=open(p).read()\ns+=\"So when sending email: write Send email from <From> to <To>, subject\"\nopen(p,\"w\").write(s)\nEOF"}}'
check "node-eval-string-mentioning-mail" allow '{"tool_name":"Bash","tool_input":{"description":"print","command":"node -e '"'"'console.log(\"mail send smtp\")'"'"'"}}'
check "sed-mentioning-send" allow '{"tool_name":"Bash","tool_input":{"description":"edit","command":"sed -i '"'"''"'"' '"'"'s/x/send mail/'"'"' notes.md"}}'
check "node-helper-send-slug-still-caught" deny '{"tool_name":"Bash","tool_input":{"description":"Send the follow-up","command":"cd ~/flow && node scripts/prospect/helper.mjs send abe-timmons"}}'
check "python-mailer-script-still-caught" deny '{"tool_name":"Bash","tool_input":{"description":"run","command":"python3 tools/send_mail.py --to a@b.com"}}'
check "bash-script-named-outreach-still-caught" deny '{"tool_name":"Bash","tool_input":{"description":"run","command":"bash ./outreach-send.sh roxo"}}'
# Postmark: account-admin API allowed, send API still caught (2026-10-31, the Platform Kit executor).
# The admin and send APIs share one host, so the host alone is no longer evidence; a send path, or a
# host with no readable admin path, still is. Fixtures live in a temp dir the judge reads in full.
PM=$(mktemp -d /tmp/email-gate-pm.XXXXXX)
printf '%s\n' "export const BASE = 'https://api.postmarkapp.com';" "export const list = () => fetch(BASE + '/servers?count=1');" "export const dom = () => fetch(BASE + '/domains');" > "$PM/admin.mjs"
printf '%s\n' "import { list } from './admin.mjs';" "await list();" > "$PM/run-admin.mjs"
printf '%s\n' "export const BASE = 'https://api.postmarkapp.com';" "export const servers = () => fetch(BASE + '/servers');" "export const go = (m) => fetch(BASE + '/email', { method: 'POST', body: m });" > "$PM/mixed.mjs"
printf '%s\n' "const HOST = 'https://api.postmarkapp.com';" "export const go = (p, m) => fetch(HOST + p, { method: 'POST', body: m });" > "$PM/bare.mjs"
check "postmark-admin-api-script-allowed" allow "{\"tool_name\":\"Bash\",\"tool_input\":{\"description\":\"provision\",\"command\":\"node $PM/run-admin.mjs\"}}"
check "postmark-admin-plus-send-path-still-caught" deny "{\"tool_name\":\"Bash\",\"tool_input\":{\"description\":\"provision\",\"command\":\"node $PM/mixed.mjs\"}}"
check "postmark-bare-host-unreadable-path-still-caught" deny "{\"tool_name\":\"Bash\",\"tool_input\":{\"description\":\"provision\",\"command\":\"node $PM/bare.mjs\"}}"
check "curl-postmark-admin-url-allowed" allow '{"tool_name":"Bash","tool_input":{"description":"list servers","command":"curl -s -H \"X-Postmark-Account-Token: $T\" https://api.postmarkapp.com/servers?count=1"}}'
check "curl-postmark-send-url-still-caught" deny '{"tool_name":"Bash","tool_input":{"description":"post","command":"curl -s -X POST -H \"X-Postmark-Server-Token: $T\" https://api.postmarkapp.com/email -d @m.json"}}'
rm -rf "$PM"
echo "passed=$pass failed=$fail"
[ -n "${_GATE_LOG_TMP:-}" ] && rm -f "$EMAIL_GATE_LOG"
[ "$fail" -eq 0 ]
