name: Test

on:
  push:
    branches:
      - "**"
    paths-ignore: &paths-ignore
      - ".github/workflows/docs.yml"
      - ".github/workflows/wheels*"
      - ".gitmodules"
      - "docs/**"
      - "wheels/**"
  pull_request:
    paths-ignore: *paths-ignore
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  FORCE_COLOR: 1
  PIP_DISABLE_PIP_VERSION_CHECK: 1

jobs:
  build:

    strategy:
      fail-fast: false
      matrix:
        os: [
          "macos-latest",
          "ubuntu-latest",
        ]
        python-version: [
          "pypy3.11",
          "3.15t",
          "3.15",
          "3.14t",
          "3.14",
          "3.13",
          "3.12",
          "3.11",
        ]
        include:
        - { python-version: "3.13", PYTHONOPTIMIZE: 1, REVERSE: "--reverse" }
        - { python-version: "3.12", PYTHONOPTIMIZE: 2 }
        # Intel
        - { os: "macos-26-intel", python-version: "3.11" }
        exclude:
        - { os: "macos-latest", python-version: "3.11" }

    runs-on: ${{ matrix.os }}
    name: ${{ matrix.os }} Python ${{ matrix.python-version }}

    steps:
    - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      with:
        persist-credentials: false

    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
      with:
        python-version: ${{ matrix.python-version }}
        allow-prereleases: true
        cache: pip
        cache-dependency-path: |
          ".ci/*.sh"
          "pyproject.toml"

    - name: Build system information
      run: python3 .github/workflows/system-info.py

    - name: Cache libavif
      if: startsWith(matrix.os, 'ubuntu')
      uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
      id: cache-libavif
      with:
        path: ~/cache-libavif
        key: ${{ runner.os }}-libavif-${{ hashFiles('depends/install_libavif.sh') }}

    - name: Cache libimagequant
      if: startsWith(matrix.os, 'ubuntu')
      uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
      id: cache-libimagequant
      with:
        path: ~/cache-libimagequant
        key: ${{ runner.os }}-libimagequant-${{ hashFiles('depends/install_imagequant.sh') }}

    - name: Cache libwebp
      if: startsWith(matrix.os, 'ubuntu')
      uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
      id: cache-libwebp
      with:
        path: ~/cache-libwebp
        key: ${{ runner.os }}-libwebp-${{ hashFiles('depends/install_webp.sh') }}

    - name: Install Linux dependencies
      if: startsWith(matrix.os, 'ubuntu')
      run: |
        .ci/install.sh
      env:
        GHA_PYTHON_VERSION: ${{ matrix.python-version }}
        GHA_LIBAVIF_CACHE_HIT: ${{ steps.cache-libavif.outputs.cache-hit }}
        GHA_LIBIMAGEQUANT_CACHE_HIT: ${{ steps.cache-libimagequant.outputs.cache-hit }}
        GHA_LIBWEBP_CACHE_HIT: ${{ steps.cache-libwebp.outputs.cache-hit }}

    - name: Install macOS dependencies
      if: startsWith(matrix.os, 'macOS')
      run: |
        .github/workflows/macos-install.sh
      env:
        GHA_PYTHON_VERSION: ${{ matrix.python-version }}

    - name: Register gcc problem matcher
      if: "matrix.os == 'ubuntu-latest' && matrix.python-version == '3.14'"
      run: echo "::add-matcher::.github/problem-matchers/gcc.json"

    - name: Build
      run: |
        .ci/build.sh

    - name: Test
      run: |
        if [ $REVERSE ]; then
          python3 -m pip install pytest-reverse
        fi
        if [ "${{ matrix.os }}" = "ubuntu-latest" ]; then
          xvfb-run -s '-screen 0 1024x768x24' sway&
          export WAYLAND_DISPLAY=wayland-1
          .ci/test.sh
        else
          .ci/test.sh
        fi
      env:
        PYTHONOPTIMIZE: ${{ matrix.PYTHONOPTIMIZE }}
        REVERSE: ${{ matrix.REVERSE }}

    - name: Prepare to upload errors
      if: failure()
      run: |
        mkdir -p Tests/errors

    - name: Upload errors
      uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
      if: failure()
      with:
        name: errors
        path: Tests/errors

    - name: After success
      run: |
        .ci/after_success.sh

    - name: Upload coverage
      uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0
      with:
        flags: ${{ matrix.os == 'ubuntu-latest' && 'GHA_Ubuntu' || 'GHA_macOS' }}
        name: ${{ matrix.os }} Python ${{ matrix.python-version }}

  success:
    permissions:
      contents: none
    needs: build
    runs-on: ubuntu-latest
    name: Test Successful
    steps:
      - name: Success
        run: echo Test Successful
