---
name: 'UI E2E Tests'

on:
  pull_request:
    paths:
      - 'core/http/**'
      - 'tests/e2e-ui/**'
      - 'tests/e2e/mock-backend/**'
  push:
    branches:
      - master

concurrency:
  group: ci-tests-ui-e2e-${{ github.event.pull_request.number || github.sha }}-${{ github.repository }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
  tests-ui-e2e:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        go-version: ['1.26.x']
    steps:
      - name: Clone
        uses: actions/checkout@v7
        with:
          submodules: true
      - name: Configure apt mirror on runner
        uses: ./.github/actions/configure-apt-mirror
      - name: Setup Go ${{ matrix.go-version }}
        uses: actions/setup-go@v5
        with:
          go-version: ${{ matrix.go-version }}
          cache: false
      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '22'
      - name: Setup Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: '1.3.11'
      - name: Proto Dependencies
        run: |
          curl -L -s https://github.com/protocolbuffers/protobuf/releases/download/v26.1/protoc-26.1-linux-x86_64.zip -o protoc.zip && \
          unzip -j -d /usr/local/bin protoc.zip bin/protoc && \
          rm protoc.zip
          go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.34.2
          go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@1958fcbe2ca8bd93af633f11e97d44e567e945af
      - name: System Dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y build-essential libopus-dev
      # Builds an instrumented UI bundle, runs the Playwright specs, and fails
      # if line coverage regressed beyond the jitter tolerance (the gate is
      # in `make test-ui-coverage-check`). PLAYWRIGHT_CHROMIUM_PATH is unset
      # here, so scripts/ensure-playwright-browser.sh installs Chromium via apt.
      - name: Run UI e2e + coverage gate
        run: PATH="$PATH:$HOME/go/bin" make test-ui-coverage-check
      - name: Upload Playwright report
        if: ${{ failure() }}
        uses: actions/upload-artifact@v7
        with:
          name: playwright-report
          path: core/http/react-ui/playwright-report/
          retention-days: 7
      - name: Upload UI coverage report
        if: ${{ always() }}
        uses: actions/upload-artifact@v7
        with:
          name: ui-coverage
          path: core/http/react-ui/coverage/
          if-no-files-found: ignore
          retention-days: 7
      - name: Setup tmate session if tests fail
        if: ${{ failure() }}
        uses: mxschmitt/action-tmate@v3.23
        with:
          detached: true
          connect-timeout-seconds: 180
          limit-access-to-actor: true
