#!/usr/bin/env bash
# Regression test for quote-render-pdf-conformance.sh.
#
# PostToolUse hook on browser-pdf-save. For a gated client-quote PDF whose render
# receipt names the source HTML, it blocks when the PDF dropped links (its /URI
# count is below the HTML's absolute-link count) or when an embedded https link
# is not live. Everything else passes; it fails open on anything uninspectable.
#
# Covers (all offline-deterministic — the one network case points at a closed
# local port so the connection refusal is immediate and reproducible):
#   A. non-quote PDF                                  → ALLOWED (not gated)
#   B. quote PDF, no receipt                          → ALLOWED (PreToolUse owns that block)
#   C. quote PDF, HTML has no absolute links          → ALLOWED (0 >= 0, no fetch)
#   D. quote PDF, PDF /URI < HTML links               → BLOCKED (link-loss, no fetch)
#   E. quote PDF, /URI ok but an https link is dead   → BLOCKED (link not live)
#   F. quote PDF, /URI ok and the only link is 200    → not asserted (needs a live server)
#   G. empty stdin                                    → ALLOWED (fail-open)
#   H. non-browser tool                               → ALLOWED (matcher defence-in-depth)
#   I. a block emits the op=bypass fault line

set -u

HOOK="$(cd "$(dirname "$0")/.." && pwd)/quote-render-pdf-conformance.sh"
if [[ ! -x "$HOOK" ]]; then
  echo "FAIL: $HOOK not executable" >&2
  exit 1
fi

ACCT=$(mktemp -d)
cleanup() { rm -rf "$ACCT"; }
trap cleanup EXIT

DOCS="$ACCT/memory/users/44700900000/documents"
mkdir -p "$DOCS"

# Receipt + filed HTML for each scenario job. The receipt names the filed quote
# HTML; the hook reads its absolute links and compares to the PDF's /URI count.
mk_job() { # jobId, html_body, pdf_uri_repeat
  local job="$1" body="$2" uris="$3"
  mkdir -p "$ACCT/quoting/jobs/$job"
  local html="$DOCS/quote-$job.html"
  printf '%s' "$body" > "$html"
  printf '{"jobId":"%s","filed":{"internal":"x","breakdown":"x","quote":"%s"}}' "$job" "$html" \
    > "$ACCT/quoting/jobs/$job/render-receipt.json"
  # A minimal PDF-ish file carrying `uris` copies of the /URI token.
  { printf '%%PDF-1.4\n'; local i=0; while [[ $i -lt $uris ]]; do printf '/URI (x)\n'; i=$((i+1)); done; } \
    > "$DOCS/quote-$job.pdf"
}

# C: no absolute links, PDF has none → allow.
mk_job "clean" '<html><body><a href="#top">top</a><p>no links</p></body></html>' 0
# D: two https links, PDF dropped them (0 /URI) → block on count.
mk_job "dropped" '<html><body><a href="https://a.example/1">a</a><a href="https://b.example/2">b</a></body></html>' 0
# E: one https link, PDF kept a /URI, but the link points at a closed local port → block on liveness.
mk_job "dead" '<html><body><a href="https://127.0.0.1:1/never">sign</a></body></html>' 1

PASS=0
FAIL=0

run_case() {
  local name="$1" stdin="$2" expected_exit="$3" actual_exit
  ( cd "$ACCT" && printf '%s' "$stdin" | bash "$HOOK" >/dev/null 2>/dev/null )
  actual_exit=$?
  if [[ "$actual_exit" -eq "$expected_exit" ]]; then
    echo "PASS: $name (exit=$actual_exit)"; PASS=$((PASS + 1))
  else
    echo "FAIL: $name (expected exit=$expected_exit, got=$actual_exit)" >&2; FAIL=$((FAIL + 1))
  fi
}

envelope() { printf '{"hook_event_name":"PostToolUse","tool_name":"%s","tool_input":{"path":"%s"}}' "$1" "$2"; }
BROWSER="mcp__plugin_browser_browser__browser-pdf-save"

run_case "A: non-quote PDF → ALLOWED" \
  "$(envelope "$BROWSER" "$DOCS/brochure.pdf")" 0
run_case "B: quote PDF, no receipt → ALLOWED (PreToolUse owns it)" \
  "$(envelope "$BROWSER" "$DOCS/quote-nojob.pdf")" 0
run_case "C: HTML has no absolute links → ALLOWED" \
  "$(envelope "$BROWSER" "$DOCS/quote-clean.pdf")" 0
run_case "D: PDF /URI below HTML link count → BLOCKED" \
  "$(envelope "$BROWSER" "$DOCS/quote-dropped.pdf")" 2
run_case "E: an embedded https link is not live → BLOCKED" \
  "$(envelope "$BROWSER" "$DOCS/quote-dead.pdf")" 2
run_case "G: empty stdin → ALLOWED (fail-open)" \
  "" 0
run_case "H: non-browser tool → ALLOWED" \
  '{"hook_event_name":"PostToolUse","tool_name":"Write","tool_input":{"path":"'"$DOCS"'/quote-dropped.pdf"}}' 0

# I: a block emits the op=bypass fault line on stderr.
STDERR=$( cd "$ACCT" && printf '%s' "$(envelope "$BROWSER" "$DOCS/quote-dropped.pdf")" | bash "$HOOK" 2>&1 >/dev/null )
if printf '%s' "$STDERR" | grep -q '\[quote-render\] op=bypass jobId=dropped reason=pdf-link-conformance'; then
  echo "PASS: I: block emits op=bypass jobId=dropped"; PASS=$((PASS + 1))
else
  echo "FAIL: I: op=bypass line absent (got: $STDERR)" >&2; FAIL=$((FAIL + 1))
fi

echo "──────── quote-render-pdf-conformance test summary ────────"
echo "PASS=$PASS FAIL=$FAIL"
[[ "$FAIL" -eq 0 ]]
