#!/usr/bin/env bash
# Spin up Postgres + MySQL, run the live DB integration tests, tear down.
# Requires Docker. From the repo root: bun run test:db:live
set -euo pipefail

cd "$(dirname "$0")/.."
COMPOSE="docker compose -f docker-compose.yml"

cleanup() { $COMPOSE down -v >/dev/null 2>&1 || true; }
trap cleanup EXIT

echo "Starting Postgres + MySQL…"
$COMPOSE up -d

echo "Waiting for databases to become healthy…"
for _ in $(seq 1 60); do
  pg=$(docker inspect --format '{{.State.Health.Status}}' "$($COMPOSE ps -q postgres)" 2>/dev/null || echo starting)
  my=$(docker inspect --format '{{.State.Health.Status}}' "$($COMPOSE ps -q mysql)" 2>/dev/null || echo starting)
  [ "$pg" = healthy ] && [ "$my" = healthy ] && break
  sleep 2
done

export WIREFW_PG_URL="postgres://wire:wire@localhost:5433/wire_test"
export WIREFW_MYSQL_URL="mysql://wire:wire@localhost:3307/wire_test"

echo "Running live DB tests…"
bun test packages/db/test/live-db.test.ts
