name: ci-test

on:  # yamllint disable-line rule:truthy
  schedule:
    - cron: '0 3 * * 0'
  workflow_call:
  workflow_dispatch:

permissions:
  contents: read

jobs:
  test:
    runs-on: ${{ matrix.os }}
    timeout-minutes: 30
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-24.04
          - macos-15
          - windows-2025
        mpi:
          - mpich
          - openmpi
          - msmpi
        py:
          - "3.13"
          - "3.13t"
          - "3.14"
          - "3.14t"
          - "pypy-3.11"
        exclude:
          - os: ubuntu-24.04
            mpi: msmpi
          - os: macos-15
            mpi: msmpi
          - os: windows-2025
            mpi: mpich
          - os: windows-2025
            mpi: openmpi

    steps:

    - uses: step-security/harden-runner@v2
      with:
        egress-policy: audit

    - name: Configure hostname
      run:  echo 127.0.0.1 `hostname` | sudo tee -a /etc/hosts > /dev/null
      if:   runner.os == 'Linux' || runner.os == 'macOS'

    - name: Checkout
      uses: actions/checkout@v5

    - name: Setup MPI (${{ matrix.mpi }})
      uses: mpi4py/setup-mpi@v1
      with:
        mpi: ${{ matrix.mpi }}

    - name: Setup Python (${{ matrix.py }})
      uses: actions/setup-python@v6
      with:
        python-version: ${{ matrix.py }}
        allow-prereleases: true
        check-latest: true
        cache: pip
        cache-dependency-path: |
          conf/requirements-build-cython.txt
          conf/requirements-test.txt

    - name: Upgrade pip
      run:  python -m pip install -U pip

    - name: Build package
      run:  python -m pip wheel -v --wheel-dir=dist .
    - name: Upload package artifacts
      uses: actions/upload-artifact@v4
      with:
        name: mpi4py-${{ matrix.os }}-${{ matrix.mpi }}-${{ matrix.py }}
        path: dist/mpi4py-*.whl

    - name: Install package for testing
      run:  python -m pip install mpi4py
              --verbose --no-index --find-links=dist

    - name: Install Python packages (test)
      run:  python -m pip install -U -r conf/requirements-test.txt
      if:   ${{ !endsWith(matrix.py, '-dev') &&
                !contains(matrix.py, '-alpha') }}

    - name: Test mpi4py (np=1)
      run:  mpiexec -n 1 python test/main.py -v
    - name: Test mpi4py (np=2)
      run:  mpiexec -n 2 python test/main.py -v -f -e spawn

    - name: Test mpi4py.futures (np=1)
      run:  mpiexec -n 1 python demo/futures/test_futures.py -v
    - name: Test mpi4py.futures (np=2)
      run:  mpiexec -n 2 python demo/futures/test_futures.py -v

    - name: Test mpi4py.run
      run:  python demo/test-run/test_run.py -v
      if:   matrix.mpi != 'msmpi'
    - name: Test init-fini
      run:  bash demo/init-fini/run.sh
      if:   matrix.mpi != 'msmpi'
    - name: Test check-mpiexec
      run:  bash demo/check-mpiexec/run.sh
      if:   matrix.mpi != 'msmpi'

    - name: Uninstall package after testing
      run:  python -m pip uninstall --yes mpi4py
