name: FastAPI Tests with PostgreSQL and uv

on:
  push:
    branches: [main]
    paths:
      - '**.py'
      - '**.ts'
      - '**.js'
      - '**.tsx'
      - '**.jsx'
      - 'pyproject.toml'
      - 'uv.lock'
      - 'sdks/typescript/package.json'
      - 'sdks/typescript/bun.lock'
      - '.github/workflows/unittest.yml'
  pull_request:
    branches: [main]
    paths:
      - '**.py'
      - '**.ts'
      - '**.js'
      - '**.tsx'
      - '**.jsx'
      - 'pyproject.toml'
      - 'uv.lock'
      - 'sdks/typescript/package.json'
      - 'sdks/typescript/bun.lock'
      - '.github/workflows/unittest.yml'

permissions:
  contents: read
  pull-requests: read

jobs:
  # Determine which tests to run based on changed files
  changes:
    runs-on: ubuntu-latest
    outputs:
      python: ${{ steps.filter.outputs.python }}
    steps:
      - uses: actions/checkout@v4
      - uses: dorny/paths-filter@v3
        id: filter
        with:
          filters: |
            python:
              - '**.py'
              - 'pyproject.toml'
              - 'uv.lock'
              - 'migrations/**'
              - 'sdks/typescript/**'
              - '.github/workflows/unittest.yml'

  test-python:
    needs: changes
    if: ${{ needs.changes.outputs.python == 'true' }}
    runs-on: ubuntu-latest

    services:
      database:
        image: pgvector/pgvector:pg15
        env:
          POSTGRES_DB: test_db
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_HOST_AUTH_METHOD: trust
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v2
        with:
          enable-cache: true
          cache-dependency-glob: "uv.lock"

      - name: "Set up Python"
        uses: actions/setup-python@v5
        with:
          python-version-file: "pyproject.toml"

      - name: Install bun
        uses: oven-sh/setup-bun@v2

      - name: Install TypeScript SDK dependencies
        run: bun install
        working-directory: sdks/typescript

      - name: Install the project
        run: uv sync --all-extras --dev

      - name: Run Tests
        run: uv run pytest -x
        env:
          DB_CONNECTION_URI: postgresql+psycopg://postgres:postgres@localhost:5432/test_db
          AUTH_USE_AUTH: false
          SENTRY_ENABLED: false
          LLM_OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
          LLM_ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
          LLM_OPENAI_COMPATIBLE_API_KEY: test-key
          LLM_OPENAI_COMPATIBLE_BASE_URL: http://localhost:8000
          DERIVER_PROVIDER: openai
          DERIVER_MODEL: test
          DIALECTIC_LEVELS__minimal__PROVIDER: openai
          DIALECTIC_LEVELS__minimal__MODEL: test
          DIALECTIC_LEVELS__minimal__THINKING_BUDGET_TOKENS: 0
          DIALECTIC_LEVELS__minimal__MAX_TOOL_ITERATIONS: 2
          DIALECTIC_LEVELS__low__PROVIDER: openai
          DIALECTIC_LEVELS__low__MODEL: test
          DIALECTIC_LEVELS__low__THINKING_BUDGET_TOKENS: 0
          DIALECTIC_LEVELS__low__MAX_TOOL_ITERATIONS: 5
          DIALECTIC_LEVELS__medium__PROVIDER: openai
          DIALECTIC_LEVELS__medium__MODEL: test
          DIALECTIC_LEVELS__medium__THINKING_BUDGET_TOKENS: 0
          DIALECTIC_LEVELS__medium__MAX_TOOL_ITERATIONS: 4
          DIALECTIC_LEVELS__high__PROVIDER: openai
          DIALECTIC_LEVELS__high__MODEL: test
          DIALECTIC_LEVELS__high__THINKING_BUDGET_TOKENS: 0
          DIALECTIC_LEVELS__high__MAX_TOOL_ITERATIONS: 4
          DIALECTIC_LEVELS__max__PROVIDER: openai
          DIALECTIC_LEVELS__max__MODEL: test
          DIALECTIC_LEVELS__max__THINKING_BUDGET_TOKENS: 0
          DIALECTIC_LEVELS__max__MAX_TOOL_ITERATIONS: 10
          DIALECTIC_QUERY_GENERATION_PROVIDER: openai
          DIALECTIC_QUERY_GENERATION_MODEL: test
          SUMMARY_PROVIDER: openai
          SUMMARY_MODEL: test

  # Status check for branch protection rules
  # This job always runs and reports success only if all required jobs pass
  test-status:
    runs-on: ubuntu-latest
    needs: [changes, test-python]
    if: always()
    steps:
      - name: Check test results
        run: |
          if [[ "${{ needs.changes.outputs.python }}" == "true" && "${{ needs.test-python.result }}" != "success" && "${{ needs.test-python.result }}" != "skipped" ]]; then
            echo "Python tests failed or were cancelled"
            exit 1
          fi
          echo "All required tests passed!"
