name: Core tests

on:
  push:
    branches:
    - main
    paths-ignore:
      - "*.md"
  pull_request:
    types: [opened, synchronize, reopened, edited]
    paths-ignore:
      - "*.md"
  workflow_dispatch:

env:
  MODULE_NAME: 'spacy_llm'
  RUN_MYPY: 'false'

jobs:
  run:
    strategy:
      fail-fast: true
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        python_version: ["3.12"]
        include:
          - os: windows-latest
            python_version: "3.7"
          - os: macos-latest
            python_version: "3.8"
          - os: ubuntu-latest
            python_version: "3.9"
          - os: windows-latest
            python_version: "3.10"
          - os: macos-latest
            python_version: "3.11"

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

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python_version }}

      - name: Build sdist
        run: |
          python -m pip install --upgrade pip
          python -m pip install -U build pip setuptools
          python -m pip install -U -r requirements.txt
          python -m build --sdist

      - name: Run mypy
        shell: bash
        if: ${{ env.RUN_MYPY == 'true' }}
        run: |
          python -m mypy $MODULE_NAME

      - name: Delete source directory
        shell: bash
        run: |
          rm -rf $MODULE_NAME

      - name: Uninstall all packages
        run: |
          python -m pip freeze > installed.txt
          python -m pip uninstall -y -r installed.txt

      - name: Install from sdist
        shell: bash
        run: |
          SDIST=$(python -c "import os;print(os.listdir('./dist')[-1])" 2>&1)
          python -m pip install dist/$SDIST

      - name: Test import
        shell: bash
        run: |
          python -c "import $MODULE_NAME" -Werror

      - name: Install test requirements
        run: |
          python -m pip install -U -r requirements.txt
          python -m pip install -U -r requirements-dev.txt
          python -m spacy download en_core_web_md

      - name: Run spacy_llm tests
        shell: bash
        run: |
          python -m pytest --pyargs $MODULE_NAME

      - name: Run usage tests
        shell: bash
        run: |
          python -m pytest --pyargs usage_examples
