name: WebComponent E2E Tests
on:
  push:
  pull_request:
  workflow_dispatch:

jobs:
  e2e-tests:
    name: WebComponent E2E Tests
    runs-on: ${{ vars.LABEL_WORKER_SELFHOSTED }}
    steps:
      - name: Setup Node.js
        uses: actions/setup-node@v5
        with:
          node-version: '22.13'
      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 10.18.3
      - name: Install LK CLI
        run: curl -sSL https://get.livekit.io/cli | bash
      - name: Setup OpenVidu Local Deployment
        uses: OpenVidu/actions/start-openvidu-local-deployment@main
        with:
          ref-openvidu-local-deployment: development
          pre_startup_commands: |
            cat <<'BASH' > pre_startup_commands.sh
            #!/bin/bash
            echo "Commenting out openvidu-meet container in docker-compose.yaml"
            if [ -f docker-compose.yaml ]; then
              yq e 'del(.services.openvidu-meet)' -i docker-compose.yaml
            fi
            BASH
            chmod +x pre_startup_commands.sh && ./pre_startup_commands.sh

      - name: Build and Start OpenVidu Meet
        uses: OpenVidu/actions/start-openvidu-meet@main
        env:
          MEET_INITIAL_WEBHOOK_ENABLED: true
          MEET_INITIAL_WEBHOOK_URL: 'http://localhost:5080/webhook'

      - name: Start OpenVidu Meet Testapp
        uses: OpenVidu/actions/start-openvidu-meet-testapp@main
        with:
          skip_install: 'true'
          skip_checkout: 'true'
          skip_build_typings: 'true'
          skip_build_webcomponent: 'true'

      - name: Run tests
        run: ./meet.sh test-e2e-webcomponent --skip-install
        env:
          RUN_MODE: CI
          PLAYWRIGHT_BROWSERS_PATH: /tmp/ms-playwright

      - name: Upload test results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: room-test-results
          path: meet-ce/frontend/webcomponent/test-results/
          retention-days: 2

      - name: Upload TestApp logs on failure
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: room-test-testapp-logs
          path: testapp.log
          retention-days: 2

      - name: Upload OpenVidu Meet logs on failure
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: room-test-openvidu-meet-logs
          path: meet_backend.log
          retention-days: 2

      - name: Clean up
        if: always()
        uses: OpenVidu/actions/cleanup@main
