#!/bin/bash
# The self-send exception (act:74a525b1, 2026-11-06): the weekly roundup's sender passes
# without a prompt ONLY when From, To and Reply-To are all the operator's own addresses.
# Everything near it still asks or is refused. Fixtures live in a temp dir; the fixture
# script mirrors the send shape of claude-cabinet's scripts/roundup-send-mail.mjs (the
# repo's test/weekly-roundup/gate-self-send.test.js runs the real script through the gate).
H="${H:-$HOME/.claude/hooks/email-send-gate.sh}"
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
export EMAIL_GATE_OWN_ADDRESSES="roundup@own.example, me@own.example, alt@own.example"
unset ROUNDUP_FROM ROUNDUP_TO ROUNDUP_REPLY_TO
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; tail -2 "$EMAIL_GATE_LOG"; fi
}
logged() { # name, pattern: one of the last two log lines (the reason line, then the decision) must match
  if tail -2 "$EMAIL_GATE_LOG" | grep -q "$2"; then pass=$((pass+1)); echo "ok    $1"; else fail=$((fail+1)); echo "FAIL  $1 — last log lines: $(tail -2 "$EMAIL_GATE_LOG")"; fi
}
T=$(mktemp -d /tmp/email-gate-self.XXXXXX)
mkdir -p "$T/scripts" "$T/other"
send_script() { # $1 = path; $2 = extra body field (optional)
  cat > "$1" <<EOF
import { readFileSync } from 'node:fs';
const env = process.env;
await fetch('https://api.postmarkapp.com/email', {
  method: 'POST',
  headers: { 'X-Postmark-Server-Token': env.ROUNDUP_POSTMARK_TOKEN },
  body: JSON.stringify({
    From: env.ROUNDUP_FROM,
    To: env.ROUNDUP_TO,
    ReplyTo: env.ROUNDUP_REPLY_TO,
    ${2:-}
    Subject: 'x',
    TextBody: readFileSync(process.argv[2], 'utf8'),
  }),
});
EOF
}
send_script "$T/scripts/roundup-send-mail.mjs"
cp "$T/scripts/roundup-send-mail.mjs" "$T/scripts/weekly-send-mail.mjs"
mkdir -p "$T/cc/scripts"; send_script "$T/cc/scripts/roundup-send-mail.mjs" "Bcc: env.ROUNDUP_TO,"
printf '%s\n' "ROUNDUP_FROM=roundup@own.example" "ROUNDUP_TO=me@own.example" "ROUNDUP_REPLY_TO=me@own.example" > "$T/self.env"
printf '%s\n' "ROUNDUP_FROM=roundup@own.example" "ROUNDUP_TO=client@elsewhere.example" "ROUNDUP_REPLY_TO=me@own.example" > "$T/other.env"
printf '%s\n' "ROUNDUP_FROM=roundup@own.example" "ROUNDUP_TO=me@own.example" "ROUNDUP_REPLY_TO=client@elsewhere.example" > "$T/reply.env"
printf '%s\n' "export ROUNDUP_FROM=\"Roundup <roundup@own.example>\"" "ROUNDUP_TO='me@own.example, alt@own.example'  # both mine" "ROUNDUP_REPLY_TO=me@own.example" > "$T/many.env"
printf '%s\n' "ROUNDUP_FROM=roundup@own.example" "ROUNDUP_TO=me@own.example, client@elsewhere.example" "ROUNDUP_REPLY_TO=me@own.example" > "$T/mixed.env"
printf '%s\n' "ROUNDUP_TO=client@elsewhere.example" > "$T/late.env"
J() { printf '{"tool_name":"Bash","tool_input":{"description":"%s","command":"%s"}}' "$1" "$2"; }

check "roundup-self-to-self-allowed" allow "$(J 'Send the roundup' "cd $T && node --env-file=self.env scripts/roundup-send-mail.mjs --body-file b.txt --send")"
logged "roundup-self-to-self-allow-is-logged-with-reason" "allow Bash :: self-send exception: roundup-send-mail.mjs from roundup@own.example to me@own.example"
check "roundup-self-env-file-separate-arg-allowed" allow "$(J 'Send the roundup' "node --env-file $T/self.env $T/scripts/roundup-send-mail.mjs --send")"
check "roundup-several-own-recipients-and-named-from-allowed" allow "$(J 'Send the roundup' "node --env-file=$T/many.env $T/scripts/roundup-send-mail.mjs --send")"
check "roundup-self-to-other-still-refused" deny "$(J 'Send the roundup' "node --env-file=$T/other.env $T/scripts/roundup-send-mail.mjs --send")"
logged "roundup-self-to-other-logs-why" "noexception Bash :: .*To client@elsewhere.example is not on the own-address list"
check "roundup-one-foreign-recipient-among-own-refused" deny "$(J 'Send the roundup' "node --env-file=$T/mixed.env $T/scripts/roundup-send-mail.mjs --send")"
check "roundup-reply-to-other-refused" deny "$(J 'Send the roundup' "node --env-file=$T/reply.env $T/scripts/roundup-send-mail.mjs --send")"
check "roundup-later-env-file-overrides-to-other-refused" deny "$(J 'Send the roundup' "node --env-file=$T/self.env --env-file=$T/late.env $T/scripts/roundup-send-mail.mjs --send")"
check "another-script-self-to-self-still-refused" deny "$(J 'Send the roundup' "node --env-file=$T/self.env $T/scripts/weekly-send-mail.mjs --send")"
check "roundup-name-outside-a-scripts-dir-refused" deny "$(J 'Send' "cp $T/scripts/roundup-send-mail.mjs $T/other/ && node --env-file=$T/self.env $T/other/roundup-send-mail.mjs --send")"
check "roundup-script-with-bcc-refused" deny "$(J 'Send the roundup' "node --env-file=$T/self.env $T/cc/scripts/roundup-send-mail.mjs --send")"
check "roundup-inline-recipient-override-refused" deny "$(J 'Send the roundup' "ROUNDUP_TO=client@elsewhere.example node --env-file=$T/self.env $T/scripts/roundup-send-mail.mjs --send")"
check "roundup-without-env-file-refused" deny "$(J 'Send the roundup' "node $T/scripts/roundup-send-mail.mjs --send")"
check "roundup-plus-a-cli-mailer-not-allowed" deny "$(J 'Send the roundup' "node --env-file=$T/self.env $T/scripts/roundup-send-mail.mjs --send; sendmail x@y.example < m.txt")"
EMAIL_GATE_OWN_ADDRESSES="" check "roundup-with-empty-own-list-refused" deny "$(J 'Send the roundup' "node --env-file=$T/self.env $T/scripts/roundup-send-mail.mjs --send")"
ROUNDUP_TO=client@elsewhere.example check "roundup-process-env-override-refused" deny "$(J 'Send the roundup' "node --env-file=$T/self.env $T/scripts/roundup-send-mail.mjs --send")"
rm -rf "$T"
echo "passed=$pass failed=$fail"
[ -n "${_GATE_LOG_TMP:-}" ] && rm -f "$EMAIL_GATE_LOG"
[ "$fail" -eq 0 ]
