name: Linux

on: [push, pull_request]

jobs:

  test_linux:
    runs-on: "ubuntu-24.04"
    name: "Ubuntu 24.04 - Python ${{ matrix.python-version }}"
    env:
      USING_COVERAGE: '3.11'

    strategy:
      matrix:
        python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "pypy3.8", "pypy3.9", "pypy3.10"]
        redis-version: [7]

    steps:
      - uses: "actions/checkout@v3"
      - uses: "actions/setup-python@v4"
        with:
          python-version: "${{ matrix.python-version }}"
          cache: "pip"
          cache-dependency-path: |
            **/setup.py
            **/requirements*.txt

      - name: "Install dependencies"
        run: |
          set -xe
          sudo apt-get install -y libxml2-dev libxslt-dev
          python -VV
          python -m pip install --upgrade pip setuptools wheel codecov
          python -m pip install -e .
          python -m pip install -r requirements-pytest.txt
          python -m pip install -r requirements-docs.txt

      - name: Start Redis
        uses: supercharge/redis-github-action@1.2.0
        with:
          redis-version: ${{ matrix.redis-version }}

      - name: "Run tests for ${{ matrix.python-version }}"
        env: 
          CLUBLOG_APIKEY: ${{ secrets.CLUBLOG_APIKEY }}
          QRZ_USERNAME: ${{ secrets.QRZ_USERNAME }}
          QRZ_PWD: ${{ secrets.QRZ_PWD }}
          PYTHON_VERSION: ${{ matrix.python-version }}
        # delay the execution randomly by a couple of seconds to reduce the amount 
        # of concurrent API calls on Clublog and QRZ.com when all CI jobs execute simultaneously
        run: |
          sleep $[ ( $RANDOM % 60 )  + 1 ]s 
          if [[ $PYTHON_VERSION == 3.11 ]]
          then 
            pytest --cov=test/
            codecov
          else
            pytest test/
          fi
          cd docs && make html

  # publish_package:
  #   runs-on: "ubuntu-latest"
  #   needs: ["test_linux"]
  #   if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
  #   steps: 
  #     - name: Publish package
  #       uses: pypa/gh-action-pypi-publish@release/v1
  #       with:
  #         user: __token__
  #         password: ${{ secrets.PYPI_API_TOKEN }}


  test_macos:
    runs-on: "macos-15"
    name: "MacOS 15 - Python ${{ matrix.python-version }}"
    strategy:
      matrix:
        python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "pypy3.8", "pypy3.9", "pypy3.10"]
        redis-version: [7.2]

    steps:
      - uses: "actions/checkout@v3"
      - uses: "actions/setup-python@v4"
        with:
          python-version: "${{ matrix.python-version }}"
          cache: "pip"
          cache-dependency-path: |
            **/setup.py
            **/requirements*.txt

      - name: "Install dependencies"
        run: |
          set -xe
          python -VV
          python -m pip install --upgrade pip setuptools
          python -m pip install -e .
          python -m pip install -r requirements-pytest.txt
      
      - name: Start Redis
        uses: shogo82148/actions-setup-redis@v1
        with:
          redis-version: ${{ matrix.redis-version }}

      - name: "Run tests for ${{ matrix.python-version }}"
        env: 
          CLUBLOG_APIKEY: ${{ secrets.CLUBLOG_APIKEY }}
          QRZ_USERNAME: ${{ secrets.QRZ_USERNAME }}
          QRZ_PWD: ${{ secrets.QRZ_PWD }}
          PYTHON_VERSION: ${{ matrix.python-version }}
        # delay the execution randomly by a couple of seconds to reduce the amount 
        # of concurrent API calls on Clublog and QRZ.com when all CI jobs execute simultaneously
        run: |
          sleep $[ ( $RANDOM % 60 )  + 1 ]
          pytest ./test

  test_windows:
    runs-on: "windows-2022"
    name: "Windows latest - Python ${{ matrix.python-version }}"

    strategy:
      matrix:
        python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]

    steps:
      - uses: "actions/checkout@v3"
      - uses: "actions/setup-python@v4"
        with:
          python-version: "${{ matrix.python-version }}"
          cache: "pip"
          cache-dependency-path: |
                setup.py
                requirements*.txt
      - name: "Install dependencies"
        run: |
          python -VV
          python -m pip install --upgrade pip setuptools wheel codecov
          python -m pip install -e .
          python -m pip install -r requirements-pytest.txt
          python -m pip install -r requirements-docs.txt
      - name: Setup redis
        # There are no github-actions supporting redis on windows.
        # Github Actions Container services are also not available for windows.
        # We have to download and install a non-official redis windows port
        # since there is no official redis version for windows.
        # Redis is then installed an run as a service
        run: |
          C:\msys64\usr\bin\wget.exe https://github.com/redis-windows/redis-windows/releases/download/7.0.14/Redis-7.0.14-Windows-x64-msys2-with-Service.zip
          C:\msys64\usr\bin\pacman.exe -S --noconfirm unzip
          C:\msys64\usr\bin\unzip.exe Redis-7.0.14-Windows-x64-msys2-with-Service.zip
          sc.exe create Redis binpath=${{ github.workspace }}\Redis-7.0.14-Windows-x64-msys2-with-Service\RedisService.exe start= auto
          echo "Redis service created, now starting it"
          net start Redis
          echo "Redis service started"

      - name: "Run tests for ${{ matrix.python-version }}"
        env: 
          CLUBLOG_APIKEY: ${{ secrets.CLUBLOG_APIKEY }}
          QRZ_USERNAME: ${{ secrets.QRZ_USERNAME }}
          QRZ_PWD: ${{ secrets.QRZ_PWD }}
          PYTHON_VERSION: ${{ matrix.python-version }}
        # give redis service time to startup and
        # delay the execution randomly by 5-20sec to reduce the
        # amount of concurrent API calls on Clublog and QRZ.com
        # when all CI jobs execute simultaneously
        run: |
          start-sleep -Seconds (5..60 | get-random)
          pytest