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

jobs:
  e2e-room-functionality-test:
    name: E2E Room Functionality Tests
    runs-on: ov-actions-runner
    steps:
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '22.13.1'
      - 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: Setup OpenVidu Meet
        uses: OpenVidu/actions/start-openvidu-meet@main
        env:
          MEET_WEBHOOK_ENABLED: true
      - name: Start OpenVidu Meet Testapp
        uses: OpenVidu/actions/start-openvidu-meet-testapp@main
      - name: Run tests
        run: |
          cd frontend/webcomponent
          # Install Playwright browsers
          mkdir -p /tmp/ms-playwright
          PLAYWRIGHT_BROWSERS_PATH=/tmp/ms-playwright npx playwright install --with-deps chromium
          npm run test:e2e-core-room
        env:
          RUN_MODE: CI
          PLAYWRIGHT_BROWSERS_PATH: /tmp/ms-playwright
      - name: Upload failed test videos
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: test-videos-room-functionality
          path: |
            frontend/webcomponent/test-results/
          retention-days: 2
      - name: Clean up
        if: always()
        uses: OpenVidu/actions/cleanup@main

  e2e-events-functionality-test:
    name: E2E Events Functionality Tests
    runs-on: ov-actions-runner
    steps:
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '22.13'
      - 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: Setup OpenVidu Meet
        uses: OpenVidu/actions/start-openvidu-meet@main
        env:
          MEET_WEBHOOK_ENABLED: true
      - name: Start OpenVidu Meet Testapp
        uses: OpenVidu/actions/start-openvidu-meet-testapp@main
      - name: Run tests
        run: |
          cd frontend/webcomponent
          # Install Playwright browsers
          mkdir -p /tmp/ms-playwright
          PLAYWRIGHT_BROWSERS_PATH=/tmp/ms-playwright npx playwright install --with-deps chromium
          npm run test:e2e-core-events
        env:
          RUN_MODE: CI
          PLAYWRIGHT_BROWSERS_PATH: /tmp/ms-playwright
      - name: Upload failed test videos
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: test-videos-events-functionality
          path: |
            frontend/webcomponent/test-results/
          retention-days: 2
      - name: Clean up
        if: always()
        uses: OpenVidu/actions/cleanup@main

  e2e-webhooks-functionality-test:
    name: E2E Webhooks Functionality Tests
    runs-on: ov-actions-runner
    steps:
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '22.13'
      - 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: Setup OpenVidu Meet
        uses: OpenVidu/actions/start-openvidu-meet@main
        env:
          MEET_WEBHOOK_ENABLED: true
      - name: Start OpenVidu Meet Testapp
        uses: OpenVidu/actions/start-openvidu-meet-testapp@main
      - name: Run tests
        run: |
          cd frontend/webcomponent
          # Install Playwright browsers
          mkdir -p /tmp/ms-playwright
          PLAYWRIGHT_BROWSERS_PATH=/tmp/ms-playwright npx playwright install --with-deps chromium
          npm run test:e2e-core-webhooks
        env:
          RUN_MODE: CI
          PLAYWRIGHT_BROWSERS_PATH: /tmp/ms-playwright
      - name: Collect TestApp logs
        if: always()
        run: |
          echo "=== Collecting TestApp logs ==="
          echo "Current directory: $(pwd)"
          echo "Looking for log files..."
          find . -name "testapp.log" -type f 2>/dev/null | head -10

          # Try multiple possible locations
          if [ -f testapp.log ]; then
            echo "TestApp log file found at ./testapp.log"
            cp testapp.log testapp-logs-webhooks.log
          elif [ -f testapp/testapp.log ]; then
            echo "TestApp log file found at testapp/testapp.log"
            cp testapp/testapp.log testapp-logs-webhooks.log
          elif [ -f testapp/dist/testapp/testapp.log ]; then
            echo "TestApp log file found at testapp/dist/testapp/testapp.log"
            cp testapp/dist/testapp/testapp.log testapp-logs-webhooks.log
          elif [ -f testapp/dist/testapp.log ]; then
            echo "TestApp log file found at testapp/dist/testapp.log"
            cp testapp/dist/testapp.log testapp-logs-webhooks.log
          else
            echo "TestApp log file not found in any expected locations"
            echo "Root directory contents:"
            ls -la | head -10
            echo "Testapp directory contents:"
            ls -la testapp/ 2>/dev/null || echo "testapp directory not found"
            ls -la testapp/dist/ 2>/dev/null || echo "testapp/dist directory not found"
            echo "Creating empty log file for artifact upload"
            echo "TestApp log file not found during CI run at $(date)" > testapp-logs-webhooks.log
            echo "Process information that might be helpful:"
            ps aux | grep -i testapp || echo "No testapp processes found"
          fi
      - name: Upload failed test videos
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: test-videos-webhooks-functionality
          path: |
            frontend/webcomponent/test-results/
          retention-days: 2
      - name: Upload TestApp logs
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: testapp-logs-webhooks-functionality
          path: testapp-logs-webhooks.log
          retention-days: 2
      - name: Clean up
        if: always()
        uses: OpenVidu/actions/cleanup@main


  e2e-ui-features-test:
    name: E2E UI Features Tests
    runs-on: ov-actions-runner
    steps:
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '22.13'
      - 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: Setup OpenVidu Meet
        uses: OpenVidu/actions/start-openvidu-meet@main
        env:
          MEET_WEBHOOK_ENABLED: true
      - name: Start OpenVidu Meet Testapp
        uses: OpenVidu/actions/start-openvidu-meet-testapp@main
      - name: Run tests
        run: |
          cd frontend/webcomponent
          # Install Playwright browsers
          mkdir -p /tmp/ms-playwright
          PLAYWRIGHT_BROWSERS_PATH=/tmp/ms-playwright npx playwright install --with-deps chromium
          npm run test:e2e-ui-features
        env:
          RUN_MODE: CI
          PLAYWRIGHT_BROWSERS_PATH: /tmp/ms-playwright
      - name: Collect TestApp logs
        if: always()
        run: |
          echo "=== Collecting TestApp logs ==="
          echo "Current directory: $(pwd)"
          echo "Looking for log files..."
          find . -name "testapp.log" -type f 2>/dev/null | head -10

          # Try multiple possible locations
          if [ -f testapp.log ]; then
            echo "TestApp log file found at ./testapp.log"
            cp testapp.log testapp-logs-ui-features.log
          elif [ -f testapp/testapp.log ]; then
            echo "TestApp log file found at testapp/testapp.log"
            cp testapp/testapp.log testapp-logs-ui-features.log
          elif [ -f testapp/dist/testapp/testapp.log ]; then
            echo "TestApp log file found at testapp/dist/testapp/testapp.log"
            cp testapp/dist/testapp/testapp.log testapp-logs-ui-features.log
          elif [ -f testapp/dist/testapp.log ]; then
            echo "TestApp log file found at testapp/dist/testapp.log"
            cp testapp/dist/testapp.log testapp-logs-ui-features.log
          else
            echo "TestApp log file not found in any expected locations"
            echo "Root directory contents:"
            ls -la | head -10
            echo "Testapp directory contents:"
            ls -la testapp/ 2>/dev/null || echo "testapp directory not found"
            ls -la testapp/dist/ 2>/dev/null || echo "testapp/dist directory not found"
            echo "Creating empty log file for artifact upload"
            echo "TestApp log file not found during CI run at $(date)" > testapp-logs-ui-features.log
            echo "Process information that might be helpful:"
            ps aux | grep -i testapp || echo "No testapp processes found"
          fi
      - name: Upload failed test videos
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: test-videos-ui-features
          path: |
            frontend/webcomponent/test-results/
          retention-days: 2
      - name: Upload TestApp logs
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: testapp-logs-ui-features
          path: testapp-logs-ui-features.log
          retention-days: 2
      - name: Clean up
        if: always()
        uses: OpenVidu/actions/cleanup@main

  e2e-recording-access-test:
    name: E2E Recording Access Tests
    runs-on: ov-actions-runner
    steps:
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '22.13'
      - 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: Setup OpenVidu Meet
        uses: OpenVidu/actions/start-openvidu-meet@main
        env:
          MEET_WEBHOOK_ENABLED: true
      - name: Start OpenVidu Meet Testapp
        uses: OpenVidu/actions/start-openvidu-meet-testapp@main
      - name: Run tests
        run: |
          docker ps -a
          cd frontend/webcomponent
          # Install Playwright browsers
          mkdir -p /tmp/ms-playwright
          PLAYWRIGHT_BROWSERS_PATH=/tmp/ms-playwright npx playwright install --with-deps chromium
          npm run test:e2e-recording-access
        env:
          RUN_MODE: CI
          PLAYWRIGHT_BROWSERS_PATH: /tmp/ms-playwright
      - name: Collect TestApp logs
        if: always()
        run: |
          echo "=== Collecting TestApp logs ==="
          echo "Current directory: $(pwd)"
          echo "Looking for log files..."
          find . -name "testapp.log" -type f 2>/dev/null | head -10

          # Try multiple possible locations
          if [ -f testapp.log ]; then
            echo "TestApp log file found at ./testapp.log"
            cp testapp.log testapp-logs-recording-access.log
          elif [ -f testapp/testapp.log ]; then
            echo "TestApp log file found at testapp/testapp.log"
            cp testapp/testapp.log testapp-logs-recording-access.log
          elif [ -f testapp/dist/testapp/testapp.log ]; then
            echo "TestApp log file found at testapp/dist/testapp/testapp.log"
            cp testapp/dist/testapp/testapp.log testapp-logs-recording-access.log
          elif [ -f testapp/dist/testapp.log ]; then
            echo "TestApp log file found at testapp/dist/testapp.log"
            cp testapp/dist/testapp.log testapp-logs-recording-access.log
          else
            echo "TestApp log file not found in any expected locations"
            echo "Root directory contents:"
            ls -la | head -10
            echo "Testapp directory contents:"
            ls -la testapp/ 2>/dev/null || echo "testapp directory not found"
            ls -la testapp/dist/ 2>/dev/null || echo "testapp/dist directory not found"
            echo "Creating empty log file for artifact upload"
            echo "TestApp log file not found during CI run at $(date)" > testapp-logs-recording-access.log
            echo "Process information that might be helpful:"
            ps aux | grep -i testapp || echo "No testapp processes found"
          fi
      - name: Dump TestApp logs on failure
        if: failure()
        run: |
          echo "=== TestApp logs ==="
          docker logs testapp || echo "TestApp container logs not available"

          echo "=== OpenVidu Meet Backend logs ==="
          docker logs openvidu-meet || echo "OpenVidu Meet container logs not available"
      - name: Upload failed test videos
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: test-videos-recording-access
          path: |
            frontend/webcomponent/test-results/
          retention-days: 2
      - name: Upload TestApp logs
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: testapp-logs-recording-access
          path: testapp-logs-recording-access.log
          retention-days: 2
      - name: Dump OpenVidu Local Deployment logs
        if: always()
        shell: bash
        run: |
          docker logs openvidu &> openvidu-server.logs
          docker logs egress &> egress.logs
      - name: Upload OpenVidu Local Deployment logs
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: openvidu-local-deployment-logs
          path: |
            openvidu-server.logs
            egress.logs
          retention-days: 2
      - name: Clean up
        if: always()
        uses: OpenVidu/actions/cleanup@main
