name: Run Bridge UI E2E Tests

on:
  pull_request:
    branches:
      - main
    paths:
      - 'bridge-ui/**'
      - '.github/workflows/bridge-ui-e2e-tests.yml'
      - '!bridge-ui/**/*.md'
  push:
    branches:
      - main
    paths:
      - 'bridge-ui/**'
      - '.github/workflows/bridge-ui-e2e-tests.yml'
      - '!bridge-ui/**/*.md'

jobs:
  run-e2e-tests:
    if: github.event.pull_request.head.repo.fork == false
    runs-on: gha-runner-scale-set-ubuntu-22.04-amd64-med
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      # Use for testing workflow only (and not publish to Docker)
      - uses: actions/cache@v4.2.0
        with:
          # This workflow requires a ~1GB cache
          path: |
            ~/.npm
            ~/.cache/ms-playwright
            ~/_work/.pnpm-store/v3
            ${{ github.workspace }}/bridge-ui/.next/cache
            ${{ github.workspace }}/bridge-ui/.cache-synpress
          # Don't make cache key too specific as
          # i.) Risk exceeding Github cache storage limit (10GB)
          # ii.) Add ~3 minutes to each workflow run to save cache for a new cache key
          key: ${{ runner.os }}-bridge-ui-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-bridge-ui-${{ hashFiles('**/pnpm-lock.yaml') }}

      - name: Setup nodejs environment
        uses: ./.github/actions/setup-nodejs
        with:
          node-version: 20.17.0
          pnpm-install-options: '--frozen-lockfile --prefer-offline'

      - name: Install Playwright
        run: pnpm dlx playwright@1.51.1 install --with-deps chromium

      - name: Build Bridge UI
        run: pnpm run -F bridge-ui build
        env:
          NEXT_PUBLIC_WALLET_CONNECT_ID: ${{ secrets.PUBLIC_WALLET_CONNECT_ID }}
          NEXT_PUBLIC_INFURA_ID: ${{ secrets.PUBLIC_BRIDGE_UI_INFURA_ID }}
          NEXT_PUBLIC_QUICKNODE_ID: ${{ secrets.PUBLIC_BRIDGE_UI_QUICKNODE_ID }}
          NEXT_PUBLIC_DYNAMIC_ENVIRONMENT_ID: ${{ secrets.PUBLIC_DYNAMIC_SANDBOX_ENVIRONMENT_ID }}
          NEXT_PUBLIC_LIFI_API_KEY: ${{ secrets.PUBLIC_LIFI_API_KEY }}
          NEXT_PUBLIC_LIFI_INTEGRATOR_NAME: ${{ secrets.PUBLIC_LIFI_INTEGRATOR_NAME }}
          NEXT_PUBLIC_ONRAMPER_API_KEY: ${{ secrets.PUBLIC_ONRAMPER_API_KEY }}
          NEXT_PUBLIC_LAYERSWAP_API_KEY: ${{ secrets.PUBLIC_LAYERSWAP_API_KEY }}
          NEXT_PUBLIC_ENVIRONMENT: development

      - name: Install linux dependencies
        run: |
          sudo apt-get install --no-install-recommends -y xvfb

      - name: Initialize Metamask testing fixture
        run: xvfb-run pnpm run -F bridge-ui build:cache
        env:
          E2E_TEST_PRIVATE_KEY: ${{ secrets.BRIDGE_UI_E2E_TESTS_PRIVATE_KEY }}
          E2E_TEST_SEED_PHRASE: "test test test test test test test test test test test junk"
          E2E_TEST_WALLET_PASSWORD: "TestPassword!"
          NEXT_PUBLIC_INFURA_ID: ${{ secrets.PUBLIC_BRIDGE_UI_INFURA_ID }}

      - name: Run unit tests
        run: DOTENV_CONFIG_PATH=.env.production pnpm run -F bridge-ui test:unit
        env:
          # Can run unit tests in parallel
          CI: "false"
          NEXT_PUBLIC_INFURA_ID: ${{ secrets.PUBLIC_BRIDGE_UI_INFURA_ID }}
          NEXT_PUBLIC_LIFI_API_KEY: "placeholder"
          NEXT_PUBLIC_LIFI_INTEGRATOR_NAME: "placeholder"
          NEXT_PUBLIC_WALLET_CONNECT_ID: "placeholder"
          NEXT_PUBLIC_DYNAMIC_ENVIRONMENT_ID: "placeholder"
          NEXT_PUBLIC_QUICKNODE_ID: "placeholder"

      # Prerequisite - Testing wallet must have >0 ETH and USDC on Sepolia
      - name: Run E2E tests
        run: xvfb-run pnpm run -F bridge-ui test:e2e:headful
        env:
          # Do not run E2E tests in parallel. Especially blockchain tx where nonces can collide.
          CI: "true"
          E2E_TEST_PRIVATE_KEY: ${{ secrets.BRIDGE_UI_E2E_TESTS_PRIVATE_KEY }}
          E2E_TEST_SEED_PHRASE: "test test test test test test test test test test test junk"
          E2E_TEST_WALLET_PASSWORD: "TestPassword!"
          NEXT_PUBLIC_INFURA_ID: ${{ secrets.PUBLIC_BRIDGE_UI_INFURA_ID }}

      - name: Archive Playwright report
        uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: playwright-report-headful
          path: |
            bridge-ui/playwright-report-headful/
          if-no-files-found: error
