name: openvidu-components-angular E2E

on:
  push:
    branches:
      - master
    paths:
      - 'openvidu-components-angular/**'
      - '.github/workflows/openvidu-components-angular-tests.yml'
  pull_request:
    branches:
      - master
  workflow_dispatch:
    inputs:
      commit_sha:
        description: 'Commit SHA'
        required: false
        default: ''

env:
  NODE_VERSION: '20'
  CHROME_IMAGE: selenium/standalone-chrome:138.0

jobs:
  test_setup:
    name: Test setup
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.commit_sha || github.sha }}
      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
      - name: Commit URL
        run: echo https://github.com/OpenVidu/openvidu/commit/${{ inputs.commit_sha || github.sha }}
      - name: Send Dispatch Event
        env:
          GITHUB_TOKEN: ${{ secrets.OPENVIDU_DISPATCH_EVENT_GA }}
          COMMIT_MESSAGE: ${{ github.event.head_commit.message || 'Manually' }}
          COMMIT_URL: ${{ github.event.commits[0].url || 'Manually' }}
          BRANCH_NAME: ${{ github.ref_name }}
        run: |
          curl -X POST \
          -H "Accept: application/vnd.github+json" \
          -H "Authorization: Bearer ${GITHUB_TOKEN}" \
          https://api.github.com/repos/OpenVidu/openvidu-tutorials/dispatches \
          -d '{"event_type":"openvidu-components-angular","client_payload":{"commit-message":"'"$COMMIT_MESSAGE"'","commit-ref":"'"$COMMIT_URL"'", "branch-name":"'"$BRANCH_NAME"'"}}'

  e2e_tests:
    needs: test_setup
    name: ${{ matrix.name }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: Nested events
            script: e2e:nested-events
          - name: Nested Structural Directives
            script: e2e:nested-structural-directives
          - name: Nested Attribute Directives
            script: e2e:nested-attribute-directives
          - name: API Directives Tests
            script: e2e:lib-directives
          - name: Internal Directives Tests
            script: e2e:lib-internal-directives
          - name: Chat E2E
            script: e2e:lib-chat
          - name: Events E2E
            script: e2e:lib-events
          - name: Media devices E2E
            script: e2e:lib-media-devices
          - name: Panels E2E
            script: e2e:lib-panels
          - name: Screen sharing E2E
            script: e2e:lib-screensharing
          - name: Stream E2E
            script: e2e:lib-stream
            mount_assets: true
          - name: Toolbar E2E
            script: e2e:lib-toolbar
          - name: Virtual Backgrounds E2E
            script: e2e:lib-virtual-backgrounds
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.commit_sha || github.sha }}
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
      - name: Install wait-on package
        run: npm install -g wait-on
      - name: Run Chrome
        run: |
          if [ "${{ matrix.mount_assets }}" = "true" ]; then
            docker run --network=host -d -p 4444:4444 -v $(pwd)/openvidu-components-angular/e2e/assets:/e2e-assets ${{ env.CHROME_IMAGE }}
          else
            docker run --network=host -d -p 4444:4444 ${{ env.CHROME_IMAGE }}
          fi
      - name: Run openvidu-local-deployment
        uses: OpenVidu/actions/start-openvidu-local-deployment@main
      - name: Start OpenVidu Call backend
        uses: OpenVidu/actions/start-openvidu-call@main
      - name: Build and Serve openvidu-components-angular Testapp
        uses: OpenVidu/actions/start-openvidu-components-testapp@main
      - name: Run Tests
        env:
          LAUNCH_MODE: CI
        run: npm run ${{ matrix.script }} --prefix openvidu-components-angular
      - name: Cleanup
        if: always()
        uses: OpenVidu/actions/cleanup@main
