name: API Models Tests

on:
  workflow_dispatch:

jobs:
  tests:
    name: Run API Models Tests
    runs-on: ubuntu-latest
    env:
      OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
      ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
      GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
      DOTTXT_API_KEY: ${{ secrets.DOTTXT_API_KEY }}
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.10"]

    steps:
    - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
      with:
        ref: ${{ github.ref }}

    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5
      with:
        python-version: ${{ matrix.python-version }}
        cache: 'pip'
        cache-dependency-path: 'pyproject.toml'

    - name: Free disk space
      run: |
        set -eux
        sudo rm -rf /usr/share/dotnet || true
        sudo rm -rf /opt/ghc || true
        sudo rm -rf /usr/local/lib/android || true
        sudo apt-get clean
        df -h

    - name: Install Ollama
      run: |
        curl -fsSL https://ollama.com/install.sh | sh
        ollama --version
        ollama pull tinyllama
        ollama pull moondream

    - name: Set up test environment
      run: |
        python -m pip install --upgrade pip
        pip install uv
        uv sync --no-group test-gpu --extra test

    - name: cache HuggingFace models
      uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4
      with:
        path: ~/.cache/huggingface
        key: hf-${{ runner.os }}-${{ hashFiles('**/pyproject.toml') }}
        restore-keys: |
          hf-${{ runner.os }}-

    - name: Create matrix id
      id: matrix-id
      env:
        MATRIX_CONTEXT: ${{ toJson(matrix) }}
      run: |
        echo $MATRIX_CONTEXT
        export MATRIX_ID=`echo $MATRIX_CONTEXT | md5sum | cut -c 1-32`
        echo $MATRIX_ID
        echo "::set-output name=id::$MATRIX_ID"

    - name: Run tests
      run: |
        uv run pytest -m 'api_call' --ignore=tests/models/test_dottxt.py
      env:
        COVERAGE_FILE: .coverage.${{ steps.matrix-id.outputs.id }}