#!/bin/bash
#
# Pre-push hook for remstack/rem
#
# Runs integration tests before push, skipping:
# - LLM tests (expensive and require API keys)
# - Tests with TestClient + database (event loop conflicts with asyncpg)
#

set -e

echo "=== Pre-push: Running integration tests ==="

echo ""
echo ">>> Starting test database..."
docker compose -f docker-compose.test.yml up -d

echo ">>> Waiting for database to be ready..."
for i in {1..30}; do
    if docker exec rem-postgres-test pg_isready -U rem > /dev/null 2>&1; then
        echo "Database ready"
        break
    fi
    sleep 1
done

echo ""
echo ">>> Running integration tests (skipping LLM tests and files with event loop conflicts)..."
# Skip LLM-marked tests which require real API calls
# Ignore files that have TestClient + asyncpg event loop conflicts
POSTGRES__CONNECTION_STRING="postgresql://rem:rem@localhost:5051/rem" \
    uv run pytest tests/integration/ -v -m "not llm" \
    --ignore=tests/integration/test_session_management.py \
    --ignore=tests/integration/test_api_endpoints.py \
    --ignore=tests/integration/test_messages_sessions_access.py \
    --ignore=tests/integration/test_agent_delegation_streaming.py \
    --ignore=tests/integration/test_ask_agent_streaming.py || {
        echo ""
        echo ">>> Stopping test database..."
        docker compose -f docker-compose.test.yml down -v
        exit 1
    }

echo ""
echo ">>> Stopping test database..."
docker compose -f docker-compose.test.yml down -v

echo ""
echo "✓ Integration tests passed"
echo ""
echo "=== Pre-push: OK ==="
