#!/bin/sh set -e APP_DIR="${APP_DIR:-$HOME/docker/buyingagent-dev}" COMPOSE_FILE="${COMPOSE_FILE:-docker-compose.dev-env.yml}" BACKEND_DIR="${APP_DIR}/backend" echo "pytest uses Dev Postgres (${APP_DIR})" cd "$APP_DIR" i=1 while [ "$i" -le 60 ]; do if [ -f "$BACKEND_DIR/pyproject.toml" ] \ && grep -q "api-test:" "$COMPOSE_FILE" \ && docker compose -f "$COMPOSE_FILE" exec -T api true >/dev/null 2>&1; then echo "api and test profile ready (attempt $i)" break fi if [ "$i" -eq 60 ]; then echo "timeout waiting for Dev checkout/api" docker compose -f "$COMPOSE_FILE" ps || true ls -la "$APP_DIR" "$BACKEND_DIR" || true exit 1 fi i=$((i + 1)) sleep 5 done PGUSER="$(docker compose -f "$COMPOSE_FILE" exec -T postgres printenv POSTGRES_USER | tr -d '\r')" echo "test database buyingagent_test on the existing Dev Postgres (owner ${PGUSER})" docker compose -f "$COMPOSE_FILE" exec -T postgres \ psql -U "$PGUSER" -d postgres -v ON_ERROR_STOP=1 \ -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'buyingagent_test' AND pid <> pg_backend_pid();" docker compose -f "$COMPOSE_FILE" exec -T postgres \ psql -U "$PGUSER" -d postgres -v ON_ERROR_STOP=1 \ -c "DROP DATABASE IF EXISTS buyingagent_test;" docker compose -f "$COMPOSE_FILE" exec -T postgres \ psql -U "$PGUSER" -d postgres -v ON_ERROR_STOP=1 \ -c "CREATE DATABASE buyingagent_test OWNER ${PGUSER};" docker compose -f "$COMPOSE_FILE" --profile test build --pull=false api-test docker compose -f "$COMPOSE_FILE" --profile test run --rm --no-deps \ -v "${APP_DIR}:/src:ro" \ -w /src/backend \ -e PYTHONPATH=/src/backend \ -e DB_NAME=buyingagent_test \ -e ENVIRONMENT=test \ -e ENGINE_BUILD=test \ -e CLOCK_NOW=2026-10-03T12:00:00Z \ api-test /app/.venv/bin/alembic upgrade head docker compose -f "$COMPOSE_FILE" --profile test run --rm --no-deps \ -v "${APP_DIR}:/src:ro" \ -w /src/backend \ -e PYTHONPATH=/src/backend \ -e DB_NAME=buyingagent_test \ -e ENVIRONMENT=test \ -e ENGINE_BUILD=test \ -e CLOCK_NOW=2026-10-03T12:00:00Z \ api-test /app/.venv/bin/pytest tests -m "not slow" -ra --tb=short