name: Test

# GitHub events that trigger the workflow:
on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - "*"
  workflow_call: # to make the workflow triggerable from other workflows
  workflow_dispatch: # to make the workflow triggerable manually

# The workflow:
jobs:
  test:
    name: Test with Py${{ matrix.python-version }} on ${{ matrix.os }}

    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu, windows, macos]
        python-version: ["3.12", "3.13", "3.14"]

    runs-on: ${{ matrix.os }}-latest
    steps:
      - uses: actions/checkout@v6

      - name: Install uv
        uses: astral-sh/setup-uv@v7
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install just
        uses: taiki-e/install-action@just

      - name: Test
        run: just test-coverage

      - name: Rename the coverage file
        run: mv .coverage .coverage.${{ matrix.python-version }}.${{ matrix.os }}

      - name: Upload coverage as an artifact
        uses: actions/upload-artifact@v5
        with:
          include-hidden-files: true
          name: coverage-${{ matrix.python-version }}-${{ matrix.os }}
          path: .coverage.${{ matrix.python-version }}.${{ matrix.os }}

  report-coverage:
    name: Generate the coverage report
    needs: [test]
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - name: Download coverage files
        uses: actions/download-artifact@v6
        with:
          pattern: coverage-* # download all the uploaded coverage reports
          path: coverage
          merge-multiple: true # download them in the same folder

      - name: Install uv
        uses: astral-sh/setup-uv@v7

      - name: Install just
        uses: taiki-e/install-action@just

      - name: Combine coverage files
        run: |
          uv run --frozen coverage combine coverage
          uv run --frozen coverage report
          uv run --frozen coverage html --show-contexts --title "RenderCV coverage for ${{ github.sha }}"

      - name: Upload the coverage report to smokeshow
        run: uv tool run smokeshow==0.4.0 upload ./htmlcov
        env:
          SMOKESHOW_GITHUB_STATUS_DESCRIPTION: Coverage {coverage-percentage}
          SMOKESHOW_GITHUB_COVERAGE_THRESHOLD: 90
          SMOKESHOW_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SMOKESHOW_GITHUB_PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
