name: pytest

on: [ push, pull_request ]

jobs:
  pytest:
    name: Run tests
    runs-on: ubuntu-22.04

    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'
          cache: 'pip'

      - name: Install system dependencies
        run: |
          # dont run update, it is slow
          # sudo apt-get update
          sudo apt-get install -y --no-install-recommends \
            libxkbcommon-x11-0 \
            x11-utils \
            libyaml-dev \
            libegl1 \
            libxcb-icccm4 \
            libxcb-image0 \
            libxcb-keysyms1 \
            libxcb-randr0 \
            libxcb-render-util0 \
            libxcb-xinerama0 \
            libopengl0 \
            libxcb-cursor0 \
            libpulse0

      - name: Install dependencies
        run: |
          python -m pip install --upgrade uv
          uv pip install --system -r requirements.txt
          uv pip install --system -r requirements-dev.txt

      - name: Run pytest
        run: |
          xvfb-run pytest --cov-report xml --cov=tagstudio

      - name: Store coverage
        uses: actions/upload-artifact@v4
        with:
          name: 'coverage'
          path: 'coverage.xml'

  coverage:
    name: Check Code Coverage
    runs-on: ubuntu-latest
    needs: pytest

    steps:
      - name: Load coverage
        uses: actions/download-artifact@v4
        with:
          name: 'coverage'

      - name: Check Code Coverage
        uses: yedpodtrzitko/coverage@main
        with:
          thresholdAll: 0.5
          thresholdNew: 0.5
          thresholdModified: 0.5
          coverageFile: coverage.xml
          token: ${{ secrets.GITHUB_TOKEN }}
          sourceDir: tagstudio/src
