name: Run Pytest with Coverage
on: 
  workflow_call:
    inputs:
      BEC_CORE_BRANCH:
        description: 'Branch of BEC Core to install'
        required: false
        default: 'main'
        type: string
      OPHYD_DEVICES_BRANCH:
        description: 'Branch of Ophyd Devices to install'
        required: false
        default: 'main'
        type: string
    secrets:
      CODECOV_TOKEN:
        required: true



permissions:
  pull-requests: write

jobs:
  pytest:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Ophyd Devices
        uses: actions/checkout@v4
        with:
          repository: bec-project/ophyd_devices
          ref: ${{ inputs.OPHYD_DEVICES_BRANCH }}

      - name: Install Ophyd Devices and dependencies
        uses: ./.github/actions/ophyd_devices_install
        with:
          BEC_CORE_BRANCH: ${{ inputs.BEC_CORE_BRANCH }}
          OPHYD_DEVICES_BRANCH: ${{ inputs.OPHYD_DEVICES_BRANCH }}
          PYTHON_VERSION: 3.11

      - name: Run Pytest with Coverage
        id: coverage
        run: |
          cd ./ophyd_devices
          coverage run --source=./ophyd_devices --omit=*/ophyd_devices/tests/* -m pytest -v --junitxml=report.xml --random-order --full-trace ./tests
          coverage report
          coverage xml

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          slug: bec-project/ophyd_devices