name: E2E Tests

on:
  push:
    branches:
      - main
      - 'release-please--branches--main'
  pull_request:
    types: [opened, labeled, synchronize]

jobs:
  e2e:
    name: E2E (${{ matrix.browser }})
    if: github.event_name == 'push' || contains(github.event.pull_request.labels.*.name, 'test:e2e')
    runs-on: ubuntu-latest
    container:
      image: mcr.microsoft.com/playwright:v1.59.1-noble
    timeout-minutes: 15
    strategy:
      fail-fast: false
      matrix:
        browser: [chromium, webkit]

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

      - name: Setup pnpm
        uses: pnpm/action-setup@v4

      - name: Setup Node.js
        uses: actions/setup-node@v5
        with:
          node-version-file: '.nvmrc'
          cache: pnpm

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

      - name: Cache turbo build
        uses: actions/cache@v5
        with:
          path: .turbo
          key: ${{ runner.os }}-turbo-e2e-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-turbo-e2e-
            ${{ runner.os }}-turbo-

      - name: Build packages
        run: pnpm build:packages

      - name: Build CDN bundles
        run: pnpm build:cdn

      - name: Build ejected skins
        run: pnpm -F site ejected-skins

      - name: Generate E2E test pages
        run: pnpm --dir apps/e2e generate-pages

      - name: Sync ejected skins to E2E app
        run: pnpm --dir apps/e2e sync-ejected-skins

      - name: Run E2E tests
        run: pnpm --dir apps/e2e exec playwright test --project=vite-${{ matrix.browser }}

      - name: Run sandbox E2E tests
        if: matrix.browser == 'chromium'
        run: pnpm --dir apps/e2e test:sandbox

      - name: Upload test report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: playwright-report-${{ matrix.browser }}
          path: apps/e2e/playwright-report/
          retention-days: 14

      - name: Upload test traces
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: playwright-traces-${{ matrix.browser }}
          path: apps/e2e/test-results/
          retention-days: 7
