# Leave the powered by Sauce Labs bit in as this means we get additional concurrency
name: "Frontend tests powered by Sauce Labs"

on:
  push:
    paths-ignore:
      - 'doc/**'

permissions:
  contents: read # to fetch code (actions/checkout)

jobs:
  playwright-chrome:
    name: Playwright Chrome
    runs-on: ubuntu-latest
    steps:
      -
        name: Generate Sauce Labs strings
        id: sauce_strings
        run: |
          printf %s\\n '::set-output name=name::${{ github.workflow }} - ${{ github.job }}'
          printf %s\\n '::set-output name=tunnel_id::${{ github.run_id }}-${{ github.run_number }}-${{ github.job }}'
      -
        name: Checkout repository
        uses: actions/checkout@v4
      -
        uses: actions/setup-node@v4
        with:
          node-version: 22
      - uses: pnpm/action-setup@v4
        name: Install pnpm
        with:
          version: 9.0.4
          run_install: false
      - name: Get pnpm store directory
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
      - uses: actions/cache@v4
        name: Setup pnpm cache
        if: always()
        with:
          path: ${{ env.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-
      - name: Only install direct dependencies
        run: pnpm config set auto-install-peers false
      -
        name: Install all dependencies and symlink for ep_etherpad-lite
        run: bin/installDeps.sh
      -
        name: export GIT_HASH to env
        id: environment
        run: echo "::set-output name=sha_short::$(git rev-parse --short ${{ github.sha }})"
      -
        name: Create settings.json
        run: cp ./src/tests/settings.json settings.json
      - name: Cache playwright binaries
        uses: actions/cache@v4
        id: playwright-cache
        with:
          path: |
            ~/.cache/ms-playwright
          key: ${{ runner.os }}-playwright-${{ env.PLAYWRIGHT_VERSION }}
      - name: Run the frontend tests
        shell: bash
        run: |
          pnpm run prod &
          connected=false
          can_connect() {
          curl -sSfo /dev/null http://localhost:9001/ || return 1
          connected=true
          }
          now() { date +%s; }
          start=$(now)
          while [ $(($(now) - $start)) -le 15 ] && ! can_connect; do
          sleep 1
          done
          cd src
          pnpm exec playwright install chromium  --with-deps
          pnpm run test-ui --project=chromium
      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: playwright-report-${{ matrix.node }}-chrome
          path: src/playwright-report/
          retention-days: 30
  playwright-firefox:
    name: Playwright Firefox
    runs-on: ubuntu-latest
    steps:
      - name: Generate Sauce Labs strings
        id: sauce_strings
        run: |
          printf %s\\n '::set-output name=name::${{ github.workflow }} - ${{ github.job }}'
          printf %s\\n '::set-output name=tunnel_id::${{ github.run_id }}-${{ github.run_number }}-${{ github.job }}'
      - name: Checkout repository
        uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
      - uses: pnpm/action-setup@v4
        name: Install pnpm
        with:
          version: 9.0.4
          run_install: false
      - name: Get pnpm store directory
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
      - uses: actions/cache@v4
        name: Setup pnpm cache
        if: always()
        with:
          path: ${{ env.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-
      - name: Only install direct dependencies
        run: pnpm config set auto-install-peers false
      - name: Install all dependencies and symlink for ep_etherpad-lite
        run: bin/installDeps.sh
      - name: export GIT_HASH to env
        id: environment
        run: echo "::set-output name=sha_short::$(git rev-parse --short ${{ github.sha }})"
      - name: Create settings.json
        run: cp ./src/tests/settings.json settings.json
      - name: Cache playwright binaries
        uses: actions/cache@v4
        id: playwright-cache
        with:
          path: |
            ~/.cache/ms-playwright
          key: ${{ runner.os }}-playwright-${{ env.PLAYWRIGHT_VERSION }}
      - name: Run the frontend tests
        shell: bash
        run: |
          pnpm run prod &
          connected=false
          can_connect() {
          curl -sSfo /dev/null http://localhost:9001/ || return 1
          connected=true
          }
          now() { date +%s; }
          start=$(now)
          while [ $(($(now) - $start)) -le 15 ] && ! can_connect; do
          sleep 1
          done
          cd src
          pnpm exec playwright install firefox  --with-deps
          pnpm run test-ui --project=firefox
      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: playwright-report-${{ matrix.node }}-firefox
          path: src/playwright-report/
          retention-days: 30
  playwright-webkit:
    name: Playwright Webkit
    runs-on: ubuntu-latest

    steps:
      -
        name: Generate Sauce Labs strings
        id: sauce_strings
        run: |
          printf %s\\n '::set-output name=name::${{ github.workflow }} - ${{ github.job }}'
          printf %s\\n '::set-output name=tunnel_id::${{ github.run_id }}-${{ github.run_number }}-${{ github.job }}'
      -
        name: Checkout repository
        uses: actions/checkout@v4
      -
        uses: actions/setup-node@v4
        with:
          node-version: 22
      - name: Cache playwright binaries
        uses: actions/cache@v4
        id: playwright-cache
        with:
          path: |
            ~/.cache/ms-playwright
          key: ${{ runner.os }}-playwright-${{ env.PLAYWRIGHT_VERSION }}
      - uses: pnpm/action-setup@v4
        name: Install pnpm
        with:
          version: 9.0.4
          run_install: false
      - name: Get pnpm store directory
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
      - uses: actions/cache@v4
        name: Setup pnpm cache
        if: always()
        with:
          path: ${{ env.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-
      - name: Only install direct dependencies
        run: pnpm config set auto-install-peers false
      -
        name: Install all dependencies and symlink for ep_etherpad-lite
        run: bin/installDeps.sh
      -
        name: export GIT_HASH to env
        id: environment
        run: echo "::set-output name=sha_short::$(git rev-parse --short ${{ github.sha }})"
      -
        name: Create settings.json
        run: cp ./src/tests/settings.json settings.json
      - name: Run the frontend tests
        shell: bash
        run: |
          pnpm run prod &
          connected=false
          can_connect() {
          curl -sSfo /dev/null http://localhost:9001/ || return 1
          connected=true
          }
          now() { date +%s; }
          start=$(now)
          while [ $(($(now) - $start)) -le 15 ] && ! can_connect; do
          sleep 1
          done
          cd src
          pnpm exec playwright install webkit --with-deps
          pnpm run test-ui --project=webkit || true
      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: playwright-report-${{ matrix.node }}-webkit
          path: src/playwright-report/
          retention-days: 30



