name: Run Tests

on: [pull_request]

permissions:
  contents: read

jobs:
  changes:
    name: Detect changes
    runs-on: ubuntu-latest
    outputs:
      code: ${{ steps.filter.outputs.code }}
    steps:
      - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
      - uses: dorny/paths-filter@d1c1ffe0248fe513906c8e24db8ea791d46f8590 # v3
        id: filter
        with:
          filters: |
            code:
              - '!docs/**'
              - '!**/*.md'

  tests-matrix:
    name: tests (${{ matrix.python-version }})
    needs: changes
    if: needs.changes.outputs.code == 'true'
    runs-on: ubuntu-latest
    timeout-minutes: 15
    strategy:
      fail-fast: true
      matrix:
        python-version: ['3.10', '3.11', '3.12', '3.13']
        group: [1, 2, 3, 4, 5, 6, 7, 8]
    steps:
      - name: Checkout code
        uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
        with:
          fetch-depth: 0  # Fetch all history for proper diff

      - name: Restore global uv cache
        id: cache-restore
        uses: actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
        with:
          path: |
            ~/.cache/uv
            ~/.local/share/uv
            .venv
          key: uv-main-py${{ matrix.python-version }}-${{ hashFiles('uv.lock') }}
          restore-keys: |
            uv-main-py${{ matrix.python-version }}-

      - name: Install uv
        uses: astral-sh/setup-uv@d0cc045d04ccac9d8b7881df0226f9e82c39688e # v6
        with:
          version: "0.11.3"
          python-version: ${{ matrix.python-version }}
          enable-cache: false

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

      - name: Restore test durations
        uses: actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
        with:
          path: .test_durations_py*
          key: test-durations-py${{ matrix.python-version }}

      - name: Run tests (group ${{ matrix.group }} of 8)
        run: |
          PYTHON_VERSION_SAFE=$(echo "${{ matrix.python-version }}" | tr '.' '_')
          DURATION_FILE="../../.test_durations_py${PYTHON_VERSION_SAFE}"

          # Temporarily always skip cached durations to fix test splitting
          # When durations don't match, pytest-split runs duplicate tests instead of splitting
          echo "Using even test splitting (duration cache disabled until fix merged)"
          DURATIONS_ARG=""

          # Original logic (disabled temporarily):
          # if [ ! -f "$DURATION_FILE" ]; then
          #   echo "No cached durations found, tests will be split evenly"
          #   DURATIONS_ARG=""
          # elif git diff origin/${{ github.base_ref }}...HEAD --name-only 2>/dev/null | grep -q "^tests/.*\.py$"; then
          #   echo "Test files have changed, skipping cached durations to avoid mismatches"
          #   DURATIONS_ARG=""
          # else
          #   echo "No test changes detected, using cached test durations for optimal splitting"
          #   DURATIONS_ARG="--durations-path=${DURATION_FILE}"
          # fi

          cd lib/crewai && uv run pytest \
            -vv \
            --splits 8 \
            --group ${{ matrix.group }} \
            $DURATIONS_ARG \
            --durations=10 \
            --maxfail=3

      - name: Run tool tests (group ${{ matrix.group }} of 8)
        run: |
          cd lib/crewai-tools && uv run pytest \
            -vv \
            --splits 8 \
            --group ${{ matrix.group }} \
            --durations=10 \
            --maxfail=3


      - name: Save uv caches
        if: steps.cache-restore.outputs.cache-hit != 'true'
        uses: actions/cache/save@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
        with:
          path: |
            ~/.cache/uv
            ~/.local/share/uv
            .venv
          key: uv-main-py${{ matrix.python-version }}-${{ hashFiles('uv.lock') }}

  # Summary job to provide single status for branch protection
  tests:
    name: tests
    runs-on: ubuntu-latest
    needs: [changes, tests-matrix]
    if: always()
    steps:
      - name: Check results
        run: |
          if [ "${{ needs.changes.outputs.code }}" != "true" ]; then
            echo "Docs-only change, skipping tests"
            exit 0
          fi
          if [ "${{ needs.tests-matrix.result }}" == "success" ]; then
            echo "All tests passed"
          else
            echo "Tests failed"
            exit 1
          fi
