name: Tests

on:
  push:
    branches:
      - main
  pull_request:
    # All PRs, including stacked PRs

permissions:
  contents: read

env:
  UV_FROZEN: "1"

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0
      - name: Detect code changes
        id: changes
        run: ./.github/scripts/detect-changes.sh code "${{ github.event.pull_request.base.sha || github.event.before }}" "${{ github.sha }}"
      - name: Setup uv
        if: steps.changes.outputs.run == 'true'
        uses: astral-sh/setup-uv@fac544c07dec837d0ccb6301d7b5580bf5edae39 # setup-uv v8.1.0; uv 0.11.14
        with:
          version: "0.11.14"
          enable-cache: true
      - name: Install dependencies
        if: steps.changes.outputs.run == 'true'
        run: make sync
      - name: Verify formatting
        if: steps.changes.outputs.run == 'true'
        run: make format-check
      - name: Run lint
        if: steps.changes.outputs.run == 'true'
        run: make lint
      - name: Skip lint
        if: steps.changes.outputs.run != 'true'
        run: echo "Skipping lint for non-code changes."

  typecheck:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0
      - name: Detect code changes
        id: changes
        run: ./.github/scripts/detect-changes.sh code "${{ github.event.pull_request.base.sha || github.event.before }}" "${{ github.sha }}"
      - name: Setup uv
        if: steps.changes.outputs.run == 'true'
        uses: astral-sh/setup-uv@fac544c07dec837d0ccb6301d7b5580bf5edae39 # setup-uv v8.1.0; uv 0.11.14
        with:
          version: "0.11.14"
          enable-cache: true
      - name: Install dependencies
        if: steps.changes.outputs.run == 'true'
        run: make sync
      - name: Run typecheck
        if: steps.changes.outputs.run == 'true'
        run: make typecheck
      - name: Skip typecheck
        if: steps.changes.outputs.run != 'true'
        run: echo "Skipping typecheck for non-code changes."

  tests:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version:
          - "3.10"
          - "3.11"
          - "3.12"
          - "3.13"
          - "3.14"
    env:
      OPENAI_API_KEY: fake-for-tests
    steps:
      - name: Checkout repository
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0
      - name: Detect code changes
        id: changes
        run: ./.github/scripts/detect-changes.sh code "${{ github.event.pull_request.base.sha || github.event.before }}" "${{ github.sha }}"
      - name: Setup uv
        if: steps.changes.outputs.run == 'true'
        uses: astral-sh/setup-uv@fac544c07dec837d0ccb6301d7b5580bf5edae39 # setup-uv v8.1.0; uv 0.11.14
        with:
          version: "0.11.14"
          enable-cache: true
          python-version: ${{ matrix.python-version }}
      - name: Install dependencies
        if: steps.changes.outputs.run == 'true'
        run: make sync
      - name: Run tests with coverage
        if: steps.changes.outputs.run == 'true' && matrix.python-version == '3.12'
        run: make coverage
      - name: Run tests
        if: steps.changes.outputs.run == 'true' && matrix.python-version != '3.12'
        run: make tests
      - name: Run async teardown stability tests
        if: steps.changes.outputs.run == 'true' && (matrix.python-version == '3.10' || matrix.python-version == '3.14')
        run: make tests-asyncio-stability
      - name: Skip tests
        if: steps.changes.outputs.run != 'true'
        run: echo "Skipping tests for non-code changes."

  tests-windows:
    runs-on: windows-latest
    env:
      OPENAI_API_KEY: fake-for-tests
    steps:
      - name: Checkout repository
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0
      - name: Detect code changes
        id: changes
        shell: bash
        run: ./.github/scripts/detect-changes.sh code "${{ github.event.pull_request.base.sha || github.event.before }}" "${{ github.sha }}"
      - name: Setup uv
        if: steps.changes.outputs.run == 'true'
        uses: astral-sh/setup-uv@fac544c07dec837d0ccb6301d7b5580bf5edae39 # setup-uv v8.1.0; uv 0.11.14
        with:
          version: "0.11.14"
          enable-cache: true
          python-version: "3.13"
      - name: Install dependencies
        if: steps.changes.outputs.run == 'true'
        run: uv sync --all-extras --all-packages --group dev
      - name: Run tests
        if: steps.changes.outputs.run == 'true'
        run: uv run pytest
      - name: Skip tests
        if: steps.changes.outputs.run != 'true'
        run: echo "Skipping tests for non-code changes."

  build-docs:
    runs-on: ubuntu-latest
    env:
      OPENAI_API_KEY: fake-for-tests
    steps:
      - name: Checkout repository
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0
      - name: Detect docs changes
        id: changes
        run: ./.github/scripts/detect-changes.sh docs "${{ github.event.pull_request.base.sha || github.event.before }}" "${{ github.sha }}"
      - name: Setup uv
        if: steps.changes.outputs.run == 'true'
        uses: astral-sh/setup-uv@fac544c07dec837d0ccb6301d7b5580bf5edae39 # setup-uv v8.1.0; uv 0.11.14
        with:
          version: "0.11.14"
          enable-cache: true
      - name: Install dependencies
        if: steps.changes.outputs.run == 'true'
        run: make sync
      - name: Build docs
        if: steps.changes.outputs.run == 'true'
        run: make build-docs
      - name: Skip docs build
        if: steps.changes.outputs.run != 'true'
        run: echo "Skipping docs build for non-docs changes."
