name: pika tests (reusable)

on:
  workflow_call:
    inputs:
      python-versions:
        description: 'JSON array of Python versions to test, e.g. ''["3.10","3.11"]'''
        required: true
        type: string
      ubuntu-runner:
        description: 'Ubuntu runner image, e.g. ubuntu-latest or ubuntu-22.04'
        required: true
        type: string
      legacy:
        description: 'Apply legacy-Python tweaks (virtualenv pin)'
        required: false
        type: boolean
        default: false
      allow-prereleases:
        description: 'Let setup-python resolve pre-release Python builds (alpha, beta, rc)'
        required: false
        type: boolean
        default: false
      linux-only:
        description: 'Run only the Linux jobs, skipping the windows leg'
        required: false
        type: boolean
        default: false
      upload-coverage:
        description: 'Upload a coverage artifact for the coverage job to collect'
        required: false
        type: boolean
        default: true

jobs:
  build-windows:
    name: build/test on windows-latest py${{ matrix.python-version }} tls=${{ matrix.test-tls }}
    if: ${{ !inputs.linux-only }}
    runs-on: windows-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: ${{ fromJSON(inputs.python-versions) }}
        test-tls: [true, false]
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
        with:
          python-version: ${{ matrix.python-version }}
          allow-prereleases: ${{ inputs.allow-prereleases }}
      - name: Resolve RabbitMQ and Erlang versions
        id: versions
        shell: pwsh
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          $rmq = (gh release view --repo rabbitmq/rabbitmq-server --json tagName --jq '.tagName | ltrimstr("v")')
          if (-Not $rmq) { throw 'could not resolve RabbitMQ version' }
          $otp_table = (gh api 'repos/erlang/otp/contents/otp_versions.table' -H 'Accept: application/vnd.github.raw')
          $otp = ($otp_table -split "`n" `
              | Select-String -Pattern '^OTP-27\.' `
              | ForEach-Object { ($_.Line -split '\s+')[0] -replace '^OTP-','' } `
              | Sort-Object -Property { [version]$_ } `
              | Select-Object -Last 1)
          if (-Not $otp) { throw 'could not resolve Erlang 27 version' }
          "rabbitmq=$rmq" >> $env:GITHUB_OUTPUT
          "erlang=$otp"   >> $env:GITHUB_OUTPUT
          Write-Host "rabbitmq=$rmq erlang=$otp"
      - name: Cache installers
        uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
        with:
          path: ~/installers
          key: ${{ runner.os }}-rmq${{ steps.versions.outputs.rabbitmq }}-otp${{ steps.versions.outputs.erlang }}
      - name: Install and start RabbitMQ
        env:
          RABBITMQ_VERSION: ${{ steps.versions.outputs.rabbitmq }}
          ERLANG_VERSION: ${{ steps.versions.outputs.erlang }}
        run: ./.ci/windows/gha-setup.ps1
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip wheel
          python -m pip install hatch ${{ inputs.legacy && '"virtualenv<21.0.0"' || '' }}
      - name: Test with pytest
        run: hatch run test -v --cov=pika --cov-report=xml:coverage.xml ${{ matrix.test-tls && '--use-tls' || '' }}
      - name: Upload coverage artifact
        if: ${{ inputs.upload-coverage }}
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: coverage-windows-py${{ matrix.python-version }}-tls-${{ matrix.test-tls }}
          path: coverage.xml
          retention-days: 7
          if-no-files-found: error

  build-linux:
    name: build/test on ${{ inputs.ubuntu-runner }} py${{ matrix.python-version }} tls=${{ matrix.test-tls }}
    runs-on: ${{ inputs.ubuntu-runner }}
    strategy:
      fail-fast: false
      matrix:
        python-version: ${{ fromJSON(inputs.python-versions) }}
        test-tls: [true, false]
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
        with:
          python-version: ${{ matrix.python-version }}
          allow-prereleases: ${{ inputs.allow-prereleases }}
      - name: Install and start RabbitMQ
        run: ./.ci/linux/gha-setup.sh
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip wheel
          python -m pip install hatch ${{ inputs.legacy && '"virtualenv<21.0.0"' || '' }}
      - name: Test with pytest
        run: hatch run test -v --cov=pika --cov-report=xml:coverage.xml ${{ matrix.test-tls && '--use-tls' || '' }}
      - name: Upload coverage artifact
        if: ${{ inputs.upload-coverage }}
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: coverage-linux-py${{ matrix.python-version }}-tls-${{ matrix.test-tls }}
          path: coverage.xml
          retention-days: 7
          if-no-files-found: error
