# This action helps run Playwright tests within one of the build_* stages.
on:
    workflow_call:
        inputs:
            runs-on:
                type: string
                required: true
                description: "The runner image to use"
            artifact:
                type: string
                required: true
                description: "The name of the artifact to download"
            project:
                type: string
                required: true
                description: "The Playwright project to use for testing"
            executable:
                type: string
                required: true
                description: "Path to the executable to test"
            prepare_cmd:
                type: string
                required: false
                description: "Command to run to prepare the executable or environment for testing"
            blob_report:
                type: boolean
                default: false
                description: "Whether to upload a blob report instead of the HTML report"
            args:
                type: string
                required: false
                description: "Additional arguments to pass to playwright, for e.g. skipping specific tests"
permissions: {}
jobs:
    test:
        name: Test ${{ inputs.project }}
        runs-on: ${{ inputs.runs-on }}
        steps:
            - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
              with:
                  repository: element-hq/element-web
                  persist-credentials: false

            - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v5
            - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6
              with:
                  node-version-file: apps/desktop/.node-version
                  cache: "pnpm"

            - name: Install Deps
              run: "pnpm install --frozen-lockfile --filter element-desktop"

            - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
              with:
                  name: ${{ inputs.artifact }}
                  path: apps/desktop/dist

            - name: Prepare for tests
              working-directory: apps/desktop
              # This is set by the caller of the reusable workflow, they have the ability to run the command they specify
              # directly without our help so this is fine.
              run: ${{ inputs.prepare_cmd }} # zizmor: ignore[template-injection]
              if: inputs.prepare_cmd

            - name: Expand executable path
              id: executable
              working-directory: apps/desktop
              shell: bash
              env:
                  EXECUTABLE: ${{ inputs.executable }}
              run: |
                  FILES=($EXECUTABLE)
                  echo "path=${FILES[0]}" >> $GITHUB_OUTPUT

            # We previously disabled the `EnableNodeCliInspectArguments` fuse, but Playwright requires
            # it to be enabled to test Electron apps, so turn it back on.
            - name: Set EnableNodeCliInspectArguments fuse enabled
              run: $RUN_AS npx @electron/fuses write --app "$EXECUTABLE" EnableNodeCliInspectArguments=on
              working-directory: apps/desktop
              shell: bash
              env:
                  # We need sudo on Linux as it is installed in /opt/
                  RUN_AS: ${{ runner.os == 'Linux' && 'sudo' || '' }}
                  EXECUTABLE: ${{ steps.executable.outputs.path }}

            - name: Run tests
              timeout-minutes: 20
              shell: bash
              working-directory: apps/desktop
              run: |
                  $PREFIX pnpm playwright test \
                      ${{ runner.os != 'Linux' && '--ignore-snapshots' || '' }} \
                      ${{ inputs.blob_report == false && '--reporter=html' || '' }} \
                      $ARGS
              env:
                  PREFIX: ${{ runner.os == 'Linux' && 'xvfb-run' || '' }}
                  PW_TAG: ${{ inputs.project }}
                  ELEMENT_DESKTOP_EXECUTABLE: ${{ steps.executable.outputs.path }}
                  ARGS: ${{ inputs.args }}
                  DEBUG: pw:browser

            - name: Upload blob report
              if: always() && inputs.blob_report
              uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
              with:
                  name: blob-report-${{ inputs.artifact }}
                  path: apps/desktop/blob-report
                  retention-days: 1
                  if-no-files-found: error

            - name: Upload HTML report
              if: always() && inputs.blob_report == false
              uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
              with:
                  name: ${{ inputs.artifact }}-test
                  path: apps/desktop/playwright-report
                  retention-days: 14
                  if-no-files-found: error
