name: 🔂 Unit tests

concurrency:
  group: ${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
    paths:
      - "strawberry/**"
      - "tests/**"
      - "noxfile.py"
      - "pyproject.toml"
      - "uv.lock"
      - ".github/workflows/test.yml"

permissions:
  contents: read

jobs:
  generate-jobs-tests:
    name: 💻 Generate test matrix
    runs-on: ubuntu-latest
    outputs:
      sessions: ${{ steps.set-matrix.outputs.sessions }}
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
      - name: Install uv
        uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 # v8.0.0
      - run: uv sync
      - id: set-matrix
        shell: bash
        run: |
          echo sessions=$(
            uv run nox --json -t tests -l |
            jq 'map(
              {
                session,
                name: "\( .name ) on \( .python )\( if .call_spec != {} then " (\(.call_spec | to_entries | map("\(.key)=\(.value)") | join(", ")))" else "" end )"
              }
            )'
          ) | tee --append $GITHUB_OUTPUT

  unit-tests:
    name: 🔬 ${{ matrix.session.name }}
    needs: [generate-jobs-tests]
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        session: ${{ fromJson(needs.generate-jobs-tests.outputs.sessions) }}

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
      - name: Install uv
        uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 # v8.0.0
        with:
          enable-cache: true

      - run: uv sync
      - run: uv run nox -r -t tests -s "${{ matrix.session.session }}"
      - name: Rename coverage data
        if: ${{ always() }}
        run: |
          if [ -f .coverage ]; then
            mv .coverage ".coverage.${{ matrix.session.session }}"
          fi
      - name: Upload coverage data
        uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
        if: ${{ always() }}
        with:
          name: coverage-data-${{ matrix.session.session }}
          path: ".coverage.${{ matrix.session.session }}"
          include-hidden-files: true
          if-no-files-found: ignore

  coverage:
    name: 📊 Combine & check coverage
    if: always()
    needs: [unit-tests, unit-tests-on-windows]
    runs-on: ubuntu-latest
    permissions:
      contents: read

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
          fetch-depth: 0
      - name: Install uv
        uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 # v8.0.0

      - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          pattern: coverage-data-*
          merge-multiple: true

      - name: Combine coverage & generate report
        run: |
          uv run --with coverage[toml] --with diff-cover coverage combine
          uv run --with coverage[toml] coverage xml
          uv run --with coverage[toml] coverage html --skip-covered --skip-empty
          uv run --with coverage[toml] coverage report --format=markdown >> $GITHUB_STEP_SUMMARY

      - name: Generate diff coverage report
        if: github.event_name == 'pull_request'
        env:
          BASE_REF: ${{ github.base_ref }}
        run: |
          echo "## Diff Coverage" >> $GITHUB_STEP_SUMMARY
          uv run --with coverage[toml] --with diff-cover diff-cover coverage.xml --compare-branch="origin/$BASE_REF" --markdown-report diff-cover.md --fail-under=0 || true
          cat diff-cover.md >> $GITHUB_STEP_SUMMARY

      - name: Upload HTML report
        uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
        with:
          name: html-report
          path: coverage_html_report

  benchmarks:
    name: 📈 Benchmarks

    # Using this version because CodSpeed doesn't support Ubuntu 24.04 LTS yet
    runs-on: ubuntu-22.04

    permissions:
      contents: read
      id-token: write

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
      - name: Install uv
        uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 # v8.0.0
        with:
          enable-cache: true
          python-version: "3.14"

      - run: uv sync

      - name: Run benchmarks
        uses: CodSpeedHQ/action@db35df748deb45fdef0960669f57d627c1956c30 # v4.13.1
        with:
          mode: simulation
          run: uv run pytest tests/benchmarks --codspeed -p no:codeflash-benchmark

  lint:
    name: ✨ Lint
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
      - name: Install uv
        uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 # v8.0.0
        with:
          enable-cache: true
          python-version: "3.12"

      - run: uv sync

      - run: |
          mkdir -p .mypy_cache

          uv run mypy --install-types --non-interactive --cache-dir=.mypy_cache/ --config-file mypy.ini

  unit-tests-on-windows:
    name: 🪟 Tests on Windows
    runs-on: windows-latest

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
      - name: Install uv
        uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 # v8.0.0
        with:
          enable-cache: true
          python-version: "3.11"

      - run: uv sync

      # we use uv directly instead of nox since we want to
      # test all integrations at once on windows
      - run: |
          uv run pytest --cov=. --cov-append -n auto --showlocals -vv

      - name: Rename coverage data
        if: ${{ always() }}
        shell: bash
        run: |
          if [ -f .coverage ]; then
            mv .coverage .coverage.windows
          fi

      - name: Upload coverage data
        uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
        if: ${{ always() }}
        with:
          name: coverage-data-windows
          path: .coverage.windows
          include-hidden-files: true
          if-no-files-found: ignore
