#!/usr/bin/env bash set -Eeuo pipefail env_file="${1:-compose/.env}" admin_compose_file="${ADMIN_COMPOSE_FILE:-compose/docker-compose.admin.yml}" infra_compose_file="${COMPOSE_FILE:-compose/docker-compose.infra.yml}" for required_command in awk curl docker jq; do if ! command -v "$required_command" >/dev/null 2>&1; then printf 'Required command is unavailable: %s\n' "$required_command" >&2 exit 1 fi done env_value() { awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_file" } project_name="$(env_value ADMIN_COMPOSE_PROJECT_NAME)" backend_url="http://$(env_value ADMIN_BACKEND_BIND_ADDRESS):$(env_value ADMIN_BACKEND_HOST_PORT)" frontend_url="http://$(env_value ADMIN_FRONTEND_BIND_ADDRESS):$(env_value ADMIN_FRONTEND_HOST_PORT)" admin_database="$(env_value ADMIN_POSTGRES_DB)" postgres_user="$(env_value POSTGRES_USER)" admin_compose=(docker compose --project-name "$project_name" --env-file "$env_file" -f "$admin_compose_file") infra_compose=(docker compose --env-file "$env_file" -f "$infra_compose_file") "${admin_compose[@]}" config --quiet backend_health="" frontend_health="" for _attempt in $(seq 1 24); do backend_health="$(curl --fail --silent --show-error --max-time 5 "$backend_url/actuator/health" 2>/dev/null || true)" frontend_health="$(curl --fail --silent --show-error --max-time 5 "$frontend_url/actuator/health" 2>/dev/null || true)" if printf '%s' "$backend_health" | jq -e '.status == "UP"' >/dev/null 2>&1 \ && printf '%s' "$frontend_health" | jq -e '.status == "UP"' >/dev/null 2>&1; then break fi backend_health="" frontend_health="" sleep 5 done if [[ -z "$backend_health" || -z "$frontend_health" ]]; then printf 'Admin health checks did not become ready.\n' >&2 "${admin_compose[@]}" ps >&2 exit 1 fi index_html="$(curl --fail --silent --show-error --max-time 5 "$frontend_url/")" module_config="$(curl --fail --silent --show-error --max-time 5 "$frontend_url/assets/module-config.json")" if ! printf '%s' "$index_html" | grep -q 'id="app"'; then printf 'Admin frontend index is invalid.\n' >&2 exit 1 fi if [[ "$(printf '%s' "$module_config" | jq -r '[.modules[].code] | join(",")')" != "AI_SOULMATE,CONSOLE" ]]; then printf 'Admin frontend is not using the AI SoulMate local module configuration.\n' >&2 exit 1 fi super_admin_count="$("${infra_compose[@]}" exec -T postgres psql \ --username "$postgres_user" --dbname "$admin_database" --tuples-only --no-align \ --command "SELECT count(*) FROM poly_admin_users WHERE role_type = 'SUPER_ADMIN'")" if [[ "$super_admin_count" != "1" ]]; then printf 'Expected exactly one local SUPER_ADMIN, found: %s\n' "$super_admin_count" >&2 exit 1 fi "${infra_compose[@]}" exec -T postgres psql \ --username "$postgres_user" --dbname "$(env_value POSTGRES_DB)" --tuples-only --no-align \ --command "SELECT count(*) FROM pg_tables WHERE schemaname = 'public'" | grep -Eq '^[1-9][0-9]*$' printf '%s\n' "$frontend_health" | jq . "${admin_compose[@]}" ps printf 'AI SoulMate local admin, local PostgreSQL, and MinIO configuration checks passed.\n'