#!/bin/sh
set -e

HEALTH_URL="${AJET_TEST_BASE_URL:-http://auth-gw:3000}/health"
echo "Waiting for auth-gw at $HEALTH_URL..."

attempts=0
max_attempts=60
until wget -qO- "$HEALTH_URL" 2>/dev/null | grep -q "ok"; do
  attempts=$((attempts + 1))
  if [ "$attempts" -ge "$max_attempts" ]; then
    echo "ERROR: auth-gw did not become healthy after $max_attempts attempts"
    exit 1
  fi
  sleep 2
done

echo "Auth-gw is healthy. Running tests..."
exec "$@"
