name: E2E Tests

on:
  pull_request:
    paths-ignore:
      - 'content/**'
      - 'public/**'
      - 'locales/**'
  push:
    branches:
      - main
    paths-ignore:
      - 'content/**'
      - 'public/**'
      - 'locales/**'

jobs:
  e2e:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Start MinIO
        run: docker compose up -d minio

      - name: Wait for MinIO to be ready
        run: |
          for i in {1..30}; do
            if curl -sf http://127.0.0.1:9000/minio/health/live; then
              echo "MinIO is ready"
              exit 0
            fi
            echo "Waiting for MinIO... ($i/30)"
            sleep 2
          done
          echo "MinIO failed to start"
          exit 1

      - uses: pnpm/action-setup@v4

      - uses: actions/setup-node@v4
        with:
          node-version-file: '.nvmrc'
          cache: 'pnpm'

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

      - name: Install Playwright browsers
        run: pnpm exec playwright install --with-deps

      - name: Run E2E tests
        run: pnpm run test:e2e

      - name: Stop MinIO
        if: always()
        run: docker compose down

      - uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: playwright-report
          path: playwright-report/
          retention-days: 30

      - uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: playwright-traces
          path: test-results/
          retention-days: 30
