name: Test

on:
  push:
    branches: [main, master, develop, feature/*]
  pull_request:
    branches: [main, master, develop, feature/*]

jobs:
  test-api:
    name: Test API (Jest)
    runs-on: ubuntu-latest
    permissions:
      contents: read
    defaults:
      run:
        working-directory: ./api

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

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

      - name: Cache node modules
        uses: actions/cache@v4
        with:
          path: ./api/node_modules
          key: ${{ runner.os }}-api-node-${{ hashFiles('api/package.json') }}
          restore-keys: |
            ${{ runner.os }}-api-node-

      - name: Install dependencies
        run: npm install --prefer-offline --no-audit

      - name: Generate Prisma client
        run: npm run prisma:generate

      - name: Run tests
        run: npm run test:cov

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

  test-web:
    name: Test Web (Vitest)
    runs-on: ubuntu-latest
    permissions:
      contents: read
    defaults:
      run:
        working-directory: ./web

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

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

      - name: Cache node modules
        uses: actions/cache@v4
        with:
          path: ./web/node_modules
          key: ${{ runner.os }}-web-node-${{ hashFiles('web/package.json') }}
          restore-keys: |
            ${{ runner.os }}-web-node-

      - name: Install dependencies
        run: npm install --prefer-offline --no-audit

      - name: Run tests with coverage
        run: npm test -- --coverage --run

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