name: Playwright E2E Tests

on:
  pull_request:
    paths:
      - 'template/tinyvue/**'
  workflow_dispatch:

jobs:
  e2e-test:
    runs-on: ubuntu-latest
    permissions:
      checks: write
      pull-requests: write
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
        name: Install pnpm
        with:
          version: 10
          run_install: false
      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'pnpm'
      - name: Install dependencies
        run: pnpm install --frozen-lockfile
      - name: Install Playwright browsers
        run: pnpm exec playwright install --with-deps
      - name: Start Backend Services
        run: |
          cd template/nestJs
          cp .env.example .env
          docker compose up -d
      - name: Wait for Backend Services
        run: |
          echo "Waiting for Backend Services to start..."
          cd template/nestJs
          timeout 120s bash -c 'until curl -f http://localhost:3000/healthCheck 2>/dev/null; do
            echo "Backend API not ready yet..."
            sleep 3
          done'
          echo "Backend Services are ready!"
      - name: Run E2E tests
        run: pnpm test:e2e