name: API tests

on:
  pull_request:
    branches: [main]
    paths:
      - "api/**"
      - "pipecat/**"
      - "scripts/**"
      - ".github/workflows/api-tests.yml"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  pytest:
    if: ${{ !startsWith(github.head_ref, 'release-please--') }}
    runs-on: ubuntu-latest
    timeout-minutes: 30

    services:
      postgres:
        # pgvector image: api/alembic/versions/dc33eef8dabe_add_document_tables.py
        # runs CREATE EXTENSION vector, which plain postgres doesn't ship.
        image: pgvector/pgvector:pg17
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: postgres
        ports:
          - 5432:5432
        options: >-
          --health-cmd "pg_isready -U postgres"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

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

    env:
      # api/conftest.py creates test_db via the postgres admin DB.
      DATABASE_URL: postgresql+asyncpg://postgres:postgres@localhost:5432/test_db
      REDIS_URL: redis://localhost:6379/0
      ENABLE_AWS_S3: "false"
      # MINIO is not actually contacted by tests, but storage.py constructs
      # the MinioFileSystem at import time and requires MINIO_PUBLIC_ENDPOINT.
      MINIO_PUBLIC_ENDPOINT: http://localhost:9000
      DEPLOYMENT_MODE: oss
      ENVIRONMENT: test
      LOG_LEVEL: DEBUG
      PYTHONPATH: ${{ github.workspace }}

    steps:
      - name: Checkout repo (with pipecat submodule)
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Set up Python 3.13
        uses: actions/setup-python@v5
        with:
          python-version: "3.13"

      - name: Set up Node 22 (test_ts_bridge.py shells out to node)
        uses: actions/setup-node@v4
        with:
          node-version: "22"

      - name: Create Python virtual environment
        run: |
          python -m venv .venv
          echo "${{ github.workspace }}/.venv/bin" >> "$GITHUB_PATH"

      - name: Install api and pipecat dependencies
        run: ./scripts/setup_requirements.sh --dev

      - name: Install ts_validator npm deps
        working-directory: api/mcp_server/ts_validator
        run: npm install

      - name: Run pytest
        working-directory: api
        run: pytest tests/ -xvs

      - name: Send Slack notification - Failure
        if: failure()
        uses: slackapi/slack-github-action@v1.26.0
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
        with:
          payload: |
            {
              "text": "❌ Dograh API tests failed on ${{ github.ref_name }} by ${{ github.actor }} - <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View Logs>"
            }
