#!/usr/bin/env bash
# Checkout-link regression test: drives the real assets/js/widget.js in headless Chrome against a
# controller + three cards (markup captured from a rendered page) and Phil-style page buttons, with
# the price endpoint stubbed — including deliberately out-of-order responses. Exits 1 on any FAIL.
set -euo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
CHROME="$(command -v google-chrome || command -v chromium || true)"
[[ -n "$CHROME" ]] || { echo "google-chrome/chromium not found"; exit 2; }
"$CHROME" --headless=new --disable-gpu --no-sandbox --virtual-time-budget=15000 --window-size=1280,1000 \
	--dump-dom "file://$HERE/checkout-links.html" 2>/dev/null | python3 -c '
import sys, re, html
d = sys.stdin.read()
m = re.search(r"<pre id=\"results\">(.*?)</pre>", d, re.S)
out = html.unescape(m.group(1)) if m else "NO RESULTS"
print(out)
sys.exit(0 if m and "FAIL" not in out and "HARNESS ERROR" not in out and "pending" != out.strip() else 1)
'
