name: Test

# Coverage thresholds are managed in vitest.config.ts
# Do not add manual coverage checks to this workflow

# This workflow is called by the main CI workflow
# No direct triggers - only invoked by ci.yml
on:
  workflow_call:
    inputs:
      run_integration_tests:
        description: Explicitly run integration tests
        required: false
        type: boolean
        default: false
    secrets:
      GH_TOKEN:
        description: Token to run integration tests against GitHub API (passed from orchestrator)
        required: false

jobs:
  test:
    runs-on: ubuntu-latest
    env:
      ROLLUP_FORCE_JS: 1
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '22'
          cache: 'npm'
      
      - name: Install dependencies
        run: npm ci
      
      - name: Build
        run: npm run build
      
      - name: Run tests with coverage
        # Coverage thresholds are centrally managed in vitest.config.ts
        # Do not add manual coverage checks here - Vitest handles this automatically
        run: npm run test:coverage
      
      - name: Upload coverage reports
        uses: codecov/codecov-action@v3
        if: always()
        with:
          files: ./coverage/coverage-final.json

  integration-tests:
    runs-on: ubuntu-latest
    needs: test
    # Only run integration tests when explicitly requested or on main branch
    # Skip if no proper GitHub token is available
    if: (github.ref == 'refs/heads/main' || github.event_name == 'pull_request') && (inputs.run_integration_tests == true)
    env:
      ROLLUP_FORCE_JS: 1
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '22'
          cache: 'npm'
      
      - name: Install dependencies
        run: npm ci
      
      - name: Build
        run: npm run build
      
      - name: Run integration tests
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
        if: env.GITHUB_TOKEN != ''
        run: npm run test:integration
      
      - name: Run CLI integration tests
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
        if: env.GITHUB_TOKEN != ''
        run: npm run test:cli

