name: Run Tests

on:
  # Run tests on push to main, master, or any release/ branch
  push:
    branches: [main, master, 'release/**']
  # Always test on pull requests targeting main/master
  pull_request:
    branches: [main, master]
  # Allow manual triggering via GitHub UI
  workflow_dispatch:

jobs:
  test:
    name: Python ${{ matrix.python-version }} – ${{ matrix.tox_env }} on ${{ matrix.os }}
    runs-on: ${{ matrix.os || 'ubuntu-latest' }}

    strategy:
      fail-fast: false  # Let all jobs run, even if one fails
      matrix:
        include:
          - python-version: "3.9"
            tox_env: qa
          - python-version: "3.10"
            tox_env: qa
          - python-version: "3.11"
            tox_env: qa
          - python-version: "3.12"
            tox_env: qa

          # Pre-release testing (won't fail entire workflow if this fails)
          - python-version: "3.13-dev"
            tox_env: qa
            continue-on-error: true

          # Platform validation only (one version)
          - os: windows-latest
            python-version: "3.12"
            tox_env: qa

          - os: macos-latest
            python-version: "3.12"
            tox_env: qa

          # Minimal test run on latest Python only
          # this verifies Apprise still works when extra libraries are not available
          - python-version: "3.12"
            tox_env: minimal

    steps:
      - uses: actions/checkout@v4

      # Install tox for isolated environment and plugin test orchestration
      - name: Install tox
        run: python -m pip install tox

      # Run tox with the specified environment (qa, minimal, etc.)
      - name: Run tox for ${{ matrix.tox_env }}
        run: tox -e ${{ matrix.tox_env }}

      - name: Upload coverage report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: coverage-${{ matrix.os }}-${{ matrix.python-version }}-${{ matrix.tox_env }}
          path: .coverage
          include-hidden-files: true

  codecov:
    name: Upload merged coverage to Codecov
    runs-on: ubuntu-latest
    needs: test  # Waits for all matrix jobs to complete
    if: always()  # Even if a test fails, still attempt to upload what we have

    steps:
      - uses: actions/checkout@v4

      - name: Download all coverage reports
        uses: actions/download-artifact@v4
        with:
          path: coverage-artifacts

      - name: Combine and generate coverage
        run: |
          pip install coverage
      
          # Create a consistent temp dir
          mkdir -p coverage-inputs
      
          # Copy and rename each coverage file to .coverage.job_name
          i=0
          for f in $(find coverage-artifacts -name .coverage); do
            cp "$f" "coverage-inputs/.coverage.$i"
            i=$((i+1))
          done
      
          # Confirm files staged
          ls -alh coverage-inputs
      
          # Combine them all
          coverage combine coverage-inputs
          coverage report
          coverage xml -o coverage.xml

      # Upload merged coverage results to Codecov for visualization
      - name: Upload to Codecov
        uses: codecov/codecov-action@v4
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          file: coverage.xml
          verbose: false  # Used for debugging only
          fail_ci_if_error: false  # Avoid failing job if Codecov is down
        env:
          CODECOV_PR:  ${{ github.event.pull_request.number }}
          CODECOV_SHA: ${{ github.sha }}
