name: Unit Tests

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

jobs:
  unit-test:
    name: Vitest Unit Tests
    runs-on: ubuntu-latest

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

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: server/package-lock.json

      - name: Install dependencies
        working-directory: server
        run: npm ci

      - name: Run unit tests
        working-directory: server
        run: npm run test:unit

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

      - name: Check coverage thresholds
        working-directory: server
        run: |
          echo "## Coverage Report" >> $GITHUB_STEP_SUMMARY
          cat coverage/coverage-summary.json 2>/dev/null || echo "Coverage report not available"