name: Test

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

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

    steps:
    - uses: actions/checkout@v4
      with:
        # Fetch full history for setuptools-scm to work properly
        fetch-depth: 0

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

    - name: Install uv
      uses: astral-sh/setup-uv@v4

    - name: Cache uv dependencies
      uses: actions/cache@v4
      with:
        path: ~/.cache/uv
        key: ${{ runner.os }}-uv-${{ matrix.python-version }}-${{ hashFiles('pyproject.toml') }}
        restore-keys: |
          ${{ runner.os }}-uv-${{ matrix.python-version }}-
          ${{ runner.os }}-uv-

    - name: Install dependencies
      run: |
        uv pip install --system -e .[test]
        
    - name: Check version detection
      run: |
        python -c "import mcpcap; print('Version:', mcpcap.__version__)"
        
    - name: Run linting with ruff
      run: |
        uv pip install --system ruff
        ruff check .
        ruff format --check .
        
    - name: Run tests with pytest
      run: |
        python -m pytest tests/ -v

  check-version:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0
        
    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: "3.11"

    - name: Install uv
      uses: astral-sh/setup-uv@v4

    - name: Cache uv dependencies
      uses: actions/cache@v4
      with:
        path: ~/.cache/uv
        key: ${{ runner.os }}-uv-check-version-${{ hashFiles('pyproject.toml') }}
        restore-keys: |
          ${{ runner.os }}-uv-check-version-
          ${{ runner.os }}-uv-
        
    - name: Install setuptools-scm
      run: |
        uv pip install --system setuptools-scm[toml]
        
    - name: Verify version can be determined
      run: |
        python -c "from setuptools_scm import get_version; print('Detected version:', get_version())"
