Newer
Older
cortex-hub / run_integration_tests.sh
#!/bin/bash
set -e

echo "=========================================="
echo "    CORTEX HUB INTEGRATION TESTS SETUP    "
echo "=========================================="

# 1. Provide an .env if missing
if [ ! -f ".env" ]; then
    echo "Creating default .env for testing..."
    cat <<EOF > .env
CORTEX_ADMIN_PASSWORD=admin
SECRET_KEY=integration-secret-key-123
SUPER_ADMINS=admin@jerxie.com
GEMINI_API_KEY=your_gemini_api_key
EOF
    echo "Please edit the .env file with your actual GEMINI_API_KEY and run this script again."
    exit 1
fi

# Load variables to use them in this script
export $(grep -v '^#' .env | xargs)

# Parse flags
NO_REBUILD=false
if [[ "$*" == *"--no-rebuild"* ]]; then
    NO_REBUILD=true
fi

# Check if services are already running
IS_RUNNING=false
# We check if specifically our ai-hub containers are "Up" via compose
if docker compose ps | grep -q 'Up'; then
    IS_RUNNING=true
fi

if [ "$NO_REBUILD" = true ] && [ "$IS_RUNNING" = true ]; then
    echo "Service is already running and --no-rebuild flag provided."
    echo "Skipping rebuild and starting tests directly..."
else
    # 2. Clean start: purge the database / volumes
    echo "Purging database and old containers..."
    docker compose down -v --remove-orphans
    docker kill test-node-1 test-node-2 2>/dev/null || true
    docker rm test-node-1 test-node-2 2>/dev/null || true

    # 3. Build & start the Hub stack
    echo "Starting AI Hub mesh..."
    # Ensure permissions are clean
    mkdir -p data
    
    # Force rebuild and clean start if we are explicitly rebuilding
    docker compose build ai-hub ai-frontend
    docker compose up -d

    # Wait for healthy
    echo "Waiting for AI Hub to be ready..."
    sleep 5
    until curl -I -s http://localhost:8002/api/v1/users/login/local | grep -q "405"; do
        echo "Waiting for AI Hub Backend..."
        sleep 2
    done

    # Wait for DB to fully initialize its tables
    sleep 3
    echo "AI Hub Backend is online."
fi

# 4. User setup via Pytest fixtures
echo "=========================================="
echo "    EXECUTING E2E INTEGRATION SUITE       "
echo "=========================================="
source /tmp/venv/bin/activate || echo "No venv found, hoping pytest is in global PATH."

pytest ai-hub/integration_tests/ -v

if [ "$NO_REBUILD" = false ] || [ "$IS_RUNNING" = false ]; then
    echo "=========================================="
    echo "    TEARING DOWN INTEGRATION ENVIRONMENT  "
    echo "=========================================="
    docker compose down -v
    echo "Done!"
else
    echo "=========================================="
    echo " SKIPPING TEARDOWN DUE TO --no-rebuild    "
    echo "=========================================="
    echo "Done!"
fi