name: Tests

on:
  pull_request:
    branches:
      - main
  push:
    branches:
      - main

env:
  MONGO_URI: mongodb://localhost:27017
  MONGO_DATABASE: evaluetonsavoir

jobs:
  lint-and-tests:
    strategy:
      matrix:
        directory: [client, server]
      fail-fast: false

    runs-on: ubuntu-latest
    timeout-minutes: 10

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: ${{ matrix.directory }}/package-lock.json

      - name: Process ${{ matrix.directory }}
        working-directory: ${{ matrix.directory }}
        timeout-minutes: 5
        run: |
          echo "Installing dependencies..."
          npm install
          echo "Running ESLint..."
          npx eslint .
          echo "Running tests..."
          echo "::group::Installing dependencies for ${{ matrix.directory }}"
          npm ci
          echo "::endgroup::"

          echo "::group::Running ESLint"
          npx eslint . || {
            echo "ESLint failed with exit code $?"
            exit 1
          }
          echo "::endgroup::"

          echo "::group::Running Tests"
          npm test
          echo "::endgroup::"

