#!/usr/bin/env sh
# Run the home/ inline self-tests every push (~14s, no external deps),
# plus the dev-server smoke when the server is already up.
# To run fully authenticated: COCKPIT_SESSION_TOKEN=<token> git push

set -e

echo "→ test:home"
npm run test:home

echo "→ test:autopilot-defaults"
npm run test:autopilot-defaults

echo "→ test:digest"
npm run test:digest

BASE="${COCKPIT_URL:-http://localhost:3000}"

if ! curl -sf --max-time 2 "$BASE/api/health" >/dev/null 2>&1; then
  echo "→ smoke: server not running at $BASE — skipping (start with npm run dev or install-app)"
  exit 0
fi

echo "→ smoke: server detected at $BASE"
npm run smoke
# >>> fleetcrown push-deploy >>>
# Pushing main deploys to the Hetzner box (background; see /tmp/push-deploy-fleetcrown.log)
if git symbolic-ref --short HEAD 2>/dev/null | grep -qx main; then
  ( sleep 5 && env -u CI -u VERCEL bash /home/g/dev/fleetcrown/scripts/deploy-hetzner.sh ) >> /tmp/push-deploy-fleetcrown.log 2>&1 & disown 2>/dev/null || true
  echo "[push-deploy] fleetcrown: deploy started in background → /tmp/push-deploy-fleetcrown.log"
fi
# <<< fleetcrown push-deploy <<<
