# Publicly credit Sauce Labs because they generously support open source
# projects.
name: Frontend Tests

on:
  workflow_call:

jobs:
  test-frontend:
    runs-on: ubuntu-latest

    steps:
      -
        name: Check out Etherpad core
        uses: actions/checkout@v6
        with:
          repository: ether/etherpad-lite
          path: etherpad-lite
      - uses: actions/setup-node@v6
        name: Install Node.js
        with:
          node-version: 25
      - uses: pnpm/action-setup@v6
        name: Install pnpm
        with:
          package_json_file: ./etherpad-lite/package.json
          run_install: false
      - name: Get pnpm store directory
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
      - uses: actions/cache@v5
        name: Setup pnpm cache
        with:
          path: ${{ env.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-
      -
        name: Checkout plugin repository
        uses: actions/checkout@v6
        with:
          path: plugin
      -
        name: Install Etherpad core dependencies
        working-directory: ./etherpad-lite
        run: bin/installDeps.sh
      - name: Install plugin
        working-directory: ./etherpad-lite
        run: |
          pnpm run plugins i --path ../../plugin
      - name: Create settings.json
        working-directory: ./etherpad-lite
        run: cp ./src/tests/settings.json settings.json
      - name: Run the frontend tests
        working-directory: ./etherpad-lite
        shell: bash
        run: |
          pnpm run dev &
          connected=false
          can_connect() {
            curl -sSfo /dev/null http://localhost:9001/ || return 1
            connected=true
          }
          now() { date +%s; }
          start=$(now)
          while [ $(($(now) - $start)) -le 30 ] && ! can_connect; do
            sleep 1
          done
          cd src
          pnpm exec playwright install chromium --with-deps
          pnpm run test-ui --project=chromium
