name: Tests

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main, develop ]

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ['3.9', '3.10', '3.11']

    steps:
    - uses: actions/checkout@v3

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

    - name: Cache pip packages
      uses: actions/cache@v3
      with:
        path: ~/.cache/pip
        key: ${{ runner.os }}-pip-${{ hashFiles('**/pyproject.toml') }}
        restore-keys: |
          ${{ runner.os }}-pip-

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -e ".[dev]"

    - name: Lint with flake8
      run: |
        # Stop the build if there are Python syntax errors or undefined names
        # F82 includes all F820-F829, but we exclude F824 (unused global - false positive for read-only globals)
        flake8 src/sollol --count --select=E9,F63,F7,F82 --extend-ignore=F824 --show-source --statistics
        # Exit-zero treats all errors as warnings
        flake8 src/sollol --count --exit-zero --max-complexity=10 --max-line-length=100 --statistics

    - name: Type check with mypy
      run: |
        mypy src/sollol --ignore-missing-imports
      continue-on-error: true

    - name: Run unit tests
      run: |
        pytest tests/unit/ -v --cov=sollol --cov-report=xml --cov-report=term

    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v3
      with:
        file: ./coverage.xml
        flags: unittests
        name: codecov-umbrella
        fail_ci_if_error: false

  integration-test:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - name: Set up Python 3.10
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -e ".[dev]"

    - name: Start mock Ollama nodes
      run: |
        # Start 3 mock Ollama servers on different ports (avoid 11434 conflict)
        python3 tests/integration/mock_ollama_server.py --port 21434 &
        sleep 2
        python3 tests/integration/mock_ollama_server.py --port 21435 &
        sleep 2
        python3 tests/integration/mock_ollama_server.py --port 21436 &
        sleep 3
        echo "Mock nodes started on ports 21434, 21435, 21436"

    - name: Verify mock nodes are running
      run: |
        curl -f http://localhost:21434/api/tags || exit 1
        curl -f http://localhost:21435/api/tags || exit 1
        curl -f http://localhost:21436/api/tags || exit 1
        echo "All mock nodes responding"

    - name: Run SOLLOL integration test
      run: |
        python3 tests/integration/test_multi_node_routing.py
      timeout-minutes: 5

    - name: Cleanup
      if: always()
      run: |
        pkill -f "mock_ollama_server" || true
