name: Test
on:
  pull_request:
  push:
    branches:
      - master

concurrency:
  # Cancel previous workflow run when a new commit is pushed to a feature branch
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

env:
  PY_COLORS: 1

jobs:
  test:
    name: Run tests
    strategy:
      fail-fast: false
      matrix:
        platform: [ubuntu-latest, windows-latest]
        python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
    runs-on: ${{ matrix.platform }}
    env:
      IS_MAIN_PYTHON: ${{ matrix.python-version == '3.10' && matrix.platform == 'ubuntu-latest' }}
    steps:
      - uses: actions/checkout@v5
      - name: Install Python tools
        uses: BrandonLWhite/pipx-install-action@v1.0.3
      - name: Setup Python with poetry caching
        # poetry cache requires poetry to already be installed, weirdly
        uses: actions/setup-python@v6
        with:
          python-version: ${{ matrix.python-version }}
          cache: poetry

      - name: Install system dependencies on Windows
        if: matrix.platform == 'windows-latest'
        shell: pwsh
        run: |
          $maxAttempts = 3
          for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) {
            # mp3gain's Chocolatey install script pulls from a flaky SourceForge
            # HTTP URL and has no upstream checksums, so allow it and retry.
            choco install mp3gain -y --allow-empty-checksums --no-progress
            if ($LASTEXITCODE -eq 0) {
              break
            }
            if ($attempt -eq $maxAttempts) {
              throw "mp3gain install failed after $maxAttempts attempts."
            }
            Start-Sleep -Seconds 3
          }

      - name: Install system dependencies on Ubuntu
        if: matrix.platform == 'ubuntu-latest'
        run: |
          sudo apt update
          sudo apt install --yes --no-install-recommends \
            ffmpeg \
            gobject-introspection \
            gstreamer1.0-plugins-base \
            imagemagick \
            libcairo2-dev \
            libgirepository-2.0-dev \
            mp3gain \
            pandoc \
            python3-gst-1.0

      - name: Get changed lyrics files
        id: lyrics-update
        uses: tj-actions/changed-files@v46
        with:
          files: |
            beetsplug/lyrics.py
            test/plugins/test_lyrics.py

      - name: Add pytest annotator
        uses: liskin/gh-problem-matcher-wrap@v3
        with:
          linters: pytest
          action: add

      - if: ${{ env.IS_MAIN_PYTHON != 'true' }}
        name: Test without coverage
        run: |
          poetry install --without=lint --extras=autobpm --extras=discogs --extras=lyrics --extras=replaygain --extras=reflink --extras=fetchart --extras=chroma --extras=sonosupdate
          poe test

      - if: ${{ env.IS_MAIN_PYTHON == 'true' }}
        name: Test with coverage
        env:
          LYRICS_UPDATED: ${{ steps.lyrics-update.outputs.any_changed }}
        run: |
          poetry install --extras=autobpm --extras=discogs --extras=lyrics --extras=docs --extras=replaygain --extras=reflink --extras=fetchart --extras=chroma --extras=sonosupdate
          poe docs
          poe test-with-coverage

      - if: ${{ !cancelled() }}
        name: Upload test results to Codecov
        uses: codecov/test-results-action@v1
        with:
          token: ${{ secrets.CODECOV_TOKEN }}

      - if: ${{ env.IS_MAIN_PYTHON == 'true' }}
        name: Store the coverage report
        uses: actions/upload-artifact@v4
        with:
          name: coverage-report
          path: .reports/coverage.xml

  upload-coverage:
    name: Upload coverage report
    needs: test
    runs-on: ubuntu-latest
    permissions:
      id-token: write
    steps:
      - uses: actions/checkout@v5

      - name: Get the coverage report
        uses: actions/download-artifact@v5
        with:
          name: coverage-report

      - name: Upload code coverage
        uses: codecov/codecov-action@v5
        with:
          files: ./coverage.xml
          use_oidc: ${{ !(github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork) }}
