name: Component Tests

on:
  push:
    branches: [ main, develop ]
    paths:
      - 'src/client/**'
      - 'src/client/__integration_tests__/**'
      - 'vitest.config.components.ts'
      - '.github/workflows/component-tests.yml'
  pull_request:
    branches: [ main ]
    paths:
      - 'src/client/**'
      - 'src/client/__integration_tests__/**'
      - 'vitest.config.components.ts'
      - '.github/workflows/component-tests.yml'
  workflow_dispatch:

jobs:
  component-tests:
    runs-on: ubuntu-latest
    timeout-minutes: 10

    steps:
    - name: Checkout code
      uses: actions/checkout@v4

    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '20'
        cache: 'pnpm'

    - name: Install pnpm
      uses: pnpm/action-setup@v2
      with:
        version: 8

    - name: Install dependencies
      run: pnpm install --frozen-lockfile

    - name: Run component tests
      run: pnpm test:components

    - name: Upload test results
      if: always()
      uses: actions/upload-artifact@v4
      with:
        name: component-test-results
        path: |
          test-results/
          coverage/
        retention-days: 7

    - name: Generate coverage report
      if: success()
      run: pnpm test:components:coverage

    - name: Upload coverage to Codecov
      if: success()
      uses: codecov/codecov-action@v3
      with:
        file: ./coverage/coverage-final.json
        flags: component-tests

    - name: Generate test summary
      if: always()
      uses: test-summary/action@v2
      with:
        paths: test-results/junit.xml

    - name: Notify on failure
      if: failure()
      uses: 8398a7/action-slack@v3
      with:
        status: ${{ job.status }}
        channel: '#ci-notifications'
        webhook_url: ${{ secrets.SLACK_WEBHOOK }}
      env:
        SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}

    - name: Send test results to GitHub
      if: always()
      uses: dorny/test-reporter@v1
      with:
        name: Component Tests
        path: test-results/junit.xml
        reporter: jest-junit
        fail-on-error: false