name: Unit Test

on:
  push:
    branches-ignore:
      - main

jobs:
  build-test:
    runs-on: ubuntu-latest
    environment: recette

    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: latest

      - name: Use Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '24'
          cache: 'pnpm'

      - name: Install dependencies and build
        run: |
          pnpm install --frozen-lockfile
          pnpm run build
        env:
          VITE_GITHUB_TOKEN: ${{ secrets.VITE_GITHUB_TOKEN }}
          VITE_EMAILJS_SERVICE_ID: ${{ secrets.VITE_EMAILJS_SERVICE_ID }}
          VITE_EMAILJS_TEMPLATE_ID: ${{ secrets.VITE_EMAILJS_TEMPLATE_ID }}
          VITE_EMAILJS_USER_ID: ${{ secrets.VITE_EMAILJS_USER_ID }}

      - name: Install Cypress binary
        run: pnpx cypress install

      - name: Install http-server
        run: pnpm add -D http-server

      - name: Start HTTP server
        run: pnpx http-server ./dist -p 8080 &
        env:
          CI: true

      - name: Run Cypress tests and Browserslist
        run: pnpm run test
        env:
          CYPRESS_baseUrl: http://localhost:8080

      - name: Upload Cypress screenshots
        if: failure()
        uses: actions/upload-artifact@v5
        with:
          name: cypress-screenshots
          path: cypress/screenshots
