name: Unit Tests
on:
  pull_request:
    paths:
      - src/**
      - tests/**
      - pyproject.toml
      - requirements-dev.txt
      - '.github/workflows/unit_tests.yml'
  push:
    branches:
      - main

jobs:
  pytest:
    name: pytest
    runs-on: ${{matrix.os}}
    strategy:
      matrix:
        python-version: ['3.12', '3.13']
        os: [ubuntu-latest, windows-latest, macos-latest]
      fail-fast: False
    steps:
      - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          persist-credentials: false

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
        with:
          python-version: ${{ matrix.python-version }}
          cache: 'pip'
          cache-dependency-path: |
            **/requirements*.txt
            pyproject.toml

      - name: Install dependencies
        run: |
          python -W ignore -m pip install --upgrade pip
          python -W ignore -m pip install . --group tests

      - name: Test with pytest
        run: |
          pytest --cov -v --junit-xml=.test_junit.xml

      - name: Test Summary
        id: test_summary
        uses: test-summary/action@31493c76ec9e7aa675f1585d3ed6f1da69269a86 # v2.4
        if: always()  # always run, even if tests fail
        with:
          paths: |
            .test_junit.xml

      - name: Submit coverage
        uses: codecov/codecov-action@671740ac38dd9b0130fbe1cec585b89eea48d3de  # v5.5.2
        with:
          env_vars: OS,PYTHON
          name: ${{ matrix.os }}-${{ matrix.python-version }}
          fail_ci_if_error: true
          token: ${{ secrets.CODECOV_TOKEN }}

      - name: Upload test results to Codecov
        uses: codecov/test-results-action@47f89e9acb64b76debcd5ea40642d25a4adced9f # v1.1.1
        if: ${{ !cancelled() }}
        with:
          files: .test_junit.xml
          token: ${{ secrets.CODECOV_TOKEN }}
