name: tests

on:
  workflow_dispatch:
  push:
    branches:
      - "main"
    paths:
      - "**/*.py"
      - "pyproject.toml"
      - "Makefile"
      - ".github/workflows/*.yml"
  pull_request:
    branches:
      - "main"
    paths:
      - "**/*.py"
      - "pyproject.toml"
      - "Makefile"
      - ".github/workflows/*.yml"

jobs:
  tests:
    strategy:
      fail-fast: false
      matrix:
        python:
          - "3.11"
          - "3.12"
          - "3.13"
        os:
          - "ubuntu-latest"
          - "windows-latest"
          - "macos-latest"
        transformers:
          - ""
        include:  # test backward compatibility
          - python: "3.11"
            os: "ubuntu-latest"
            transformers: "4.55.0"
          - python: "3.11"
            os: "ubuntu-latest"
            transformers: "4.57.1"

    runs-on: ${{ matrix.os }}

    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}-${{ matrix.os }}-${{ matrix.python }}-${{ matrix.transformers }}
      cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

    env:
      HF_TOKEN: ${{ secrets.HF_TOKEN }}
      OS_NAME: ${{ matrix.os }}
      UV_NO_SYNC: 1

    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Install uv
        uses: astral-sh/setup-uv@v7
        with:
          python-version: ${{ matrix.python }}
          github-token: ${{ github.token }}
          enable-cache: false

      - name: Install dependencies
        run: |
          uv venv
          uv pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
          uv pip install -e .
          uv pip install -r requirements/dev.txt

      - name: Install transformers
        if: ${{ matrix.transformers }}
        run: |
          uv pip install "transformers==${{ matrix.transformers }}"

      - name: Cache files
        id: hf-hub-cache
        uses: actions/cache@v5
        with:
          path: ${{ runner.temp }}/huggingface
          key: huggingface-${{ matrix.os }}-${{ matrix.python }}-${{ matrix.transformers }}-${{ hashFiles('tests/version.txt') }}

      - name: Check quality
        run: |
          make style && make quality

      - name: Check license
        run: |
          make license

      - name: Check build
        run: |
          make build

      - name: Test with pytest
        run: |
          make test
        env:
          HF_HOME: ${{ runner.temp }}/huggingface
          HF_HUB_OFFLINE: "${{ steps.hf-hub-cache.outputs.cache-hit == 'true' && '1' || '0' }}"
