name: Run Tests

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

jobs:
  test:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:16-alpine
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: alemno_test_db
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

      redis:
        image: redis:7-alpine
        ports:
          - 6379:6379
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v5
        with:
          enable-cache: true
          python-version: "3.14"

      - name: Run Tests with Coverage
        env:
          DATABASE_URL: postgresql+asyncpg://postgres:postgres@localhost:5432/alemno_test_db
          REDIS_URL: redis://localhost:6379/0
          TESTING: "True"
        run: |
          uv run pytest --cov=app --cov-report=xml --cov-report=term-missing -v

      - name: Upload Coverage Report
        uses: actions/upload-artifact@v4
        with:
          name: code-coverage-report
          path: coverage.xml
