name: Tests

on:
  push:
    branches: [main, master, develop]
  pull_request:
    branches: [main, master]

jobs:
  test:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:15
        env:
          POSTGRES_DB: aegisone
          POSTGRES_USER: aegisone
          POSTGRES_PASSWORD: aegisone_pass
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 5s
          --health-timeout 5s
          --health-retries 5

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Install dependencies
        working-directory: py_service
        run: |
          pip install -r requirements.txt
          pip install ruff

      - name: Pre-test lint & static analysis
        working-directory: py_service
        run: |
          ruff check .
          pytest tests/test_routes_defined.py -v --no-header -q
          python -c "from tests.test_00_discover_routes import collect_app_routes; r=collect_app_routes(); print(f'Discovered {len(r)} routes')"

      - name: Start server
        working-directory: py_service
        run: |
          uvicorn app.main:app --host 0.0.0.0 --port 8000 &
          sleep 3
          curl -sf http://localhost:8000/health || (echo "Server failed to start" && exit 1)

      - name: Run tests
        working-directory: py_service
        run: |
          pytest -v --tb=short -x

      - name: Test summary
        if: always()
        run: echo "CI test run complete"

  test-max-bot:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:15
        env:
          POSTGRES_DB: aegisone
          POSTGRES_USER: aegisone
          POSTGRES_PASSWORD: aegisone_pass
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 5s
          --health-timeout 5s
          --health-retries 5

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Install dependencies
        working-directory: max_bot
        run: |
          pip install -r requirements.txt

      - name: Run tests
        working-directory: max_bot
        run: |
          pytest tests/ -v --tb=short -x

      - name: Test summary
        if: always()
        run: echo "max_bot CI test run complete"
