name: Test

# CI runs only when a PR is opened or updated (not on every push to main/develop).
on:
  pull_request:
    branches: [ main, develop ]

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false  # Don't cancel other jobs if one fails - see all test results
      matrix:
        python-version: ['3.11', '3.12', '3.13']
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -e ".[dev]"
      
      - name: Run tests
        run: |
          pytest
      
      # Use same config as .pre-commit-config.yaml so CI and local formatting match
      - name: Run linting
        run: |
          black --check --config pyproject.toml src/pycharter tests
          isort --check --settings-path pyproject.toml src/pycharter tests
      
      - name: Run type checking
        run: |
          mypy pycharter || true  # Allow failures for now

      - name: Set up Node.js (UI type-check)
        if: hashFiles('src/pycharter/ui/package.json') != ''
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: src/pycharter/ui/package-lock.json

      - name: Install UI dependencies and type-check
        if: hashFiles('src/pycharter/ui/package.json') != ''
        run: |
          cd src/pycharter/ui
          npm ci
          npm run type-check
