#!/bin/sh
# Smoke-test the tradein-browser service (camoufox HTTP wrapper, #905) from
# inside the tradein docker network. Verifies /health and a real /fetch.
#
# Run from the deploy host (after `git pull`), piping the script into the
# backend container (which shares the tradein-net with tradein-browser):
#
#   docker exec -i tradein-backend sh < tradein-mvp/scripts/browser-smoke.sh
#
# Override the target URL:
#   URL=https://example.com docker exec -i -e URL tradein-backend sh < tradein-mvp/scripts/browser-smoke.sh
set -eu

B="${BROWSER_HTTP_ENDPOINT:-http://tradein-browser:3000}"
U="${URL:-https://www.avito.ru/ekaterinburg/kvartiry/prodam}"

printf 'health:    '
curl -s --max-time 10 "$B/health" || echo "(no response)"
echo

echo "{\"url\":\"$U\"}" > /tmp/smoke_payload.json
curl -s --max-time 120 -X POST "$B/fetch" \
  -H 'Content-Type: application/json' \
  -d @/tmp/smoke_payload.json \
  -o /tmp/smoke_result.html || true

echo "url:       $U"
echo "bytes:     $(wc -c < /tmp/smoke_result.html 2>/dev/null || echo 0)"
echo "preloaded: $(grep -c preloadedState /tmp/smoke_result.html 2>/dev/null || echo 0)"
echo "firewall:  $(grep -ci firewall-container /tmp/smoke_result.html 2>/dev/null || echo 0)"
echo "--- first 200 bytes (error JSON shows here if /fetch failed) ---"
head -c 200 /tmp/smoke_result.html 2>/dev/null || true
echo
