---
name: test release
# https://github.com/product-os/flowzone/tree/master/.github/actions
inputs:
  json:
    description: 'JSON stringified object containing all the inputs from the calling workflow'
    required: true
  secrets:
    description: 'JSON stringified object containing all the secrets from the calling workflow'
    required: true

  # --- custom environment
  NODE_VERSION:
    type: string
    default: '20.10'
  VERBOSE:
    type: string
    default: 'true'

runs:
  # https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
  using: 'composite'
  steps:
    # https://github.com/actions/setup-node#caching-global-packages-data
    - name: Setup Node.js
      uses: actions/setup-node@v3
      with:
        node-version: ${{ inputs.NODE_VERSION }}
        cache: npm

    - name: Install host dependencies
      if: runner.os == 'Linux'
      shell: bash
      run: |
        sudo apt-get update && sudo apt-get install -y --no-install-recommends xvfb libudev-dev
        cat < package.json | jq -r '.hostDependencies[][]' - | \
          xargs -L1 echo | sed 's/|//g' | xargs -L1 \
          sudo apt-get --ignore-missing install || true

    - name: Install host dependencies
      if: runner.os == 'macOS'
      # FIXME: Python 3.12 dropped distutils that node-gyp depends upon.
      # This is a temporary workaround to make the job use Python 3.11 until
      # we update to npm 10+.
      uses: actions/setup-python@d27e3f3d7c64b4bbf8e4abfb9b63b83e846e0435 # v4
      with:
        python-version: '3.11'

    - name: Test release
      shell: bash
      run: |
        ## FIXME: causes issues with `xxhash` which tries to load a debug build which doens't exist and cannot be compiled
        # if [[ '${{ inputs.VERBOSE }}' =~ on|On|Yes|yes|true|True ]]; then
        #   export DEBUG='electron-forge:*,sidecar'
        # fi

        npm ci

        # as the shrinkwrap might have been done on mac/linux, this is ensure the package is there for windows
        if [[ "$RUNNER_OS" == "Windows" ]]; then
          npm i -D winusb-driver-generator
        fi

        npm run lint
        npm run package
        npm run wdio # test stage, note that it requires the package to be done first

      env:
        # https://www.electronjs.org/docs/latest/api/environment-variables
        ELECTRON_NO_ATTACH_CONSOLE: 'true'

    - name: Compress custom source
      if: runner.os != 'Windows'
      shell: bash
      run: tar -acf ${{ runner.temp }}/custom.tgz .

    - name: Compress custom source
      if: runner.os == 'Windows'
      shell: pwsh
      run: C:\"Program Files"\Git\usr\bin\tar.exe --force-local -acf ${{ runner.temp }}\custom.tgz .

    - name: Upload custom artifact
      uses: actions/upload-artifact@v4
      with:
        name: custom-${{ github.event.pull_request.head.sha || github.event.head_commit.id }}-${{ runner.os }}-${{ runner.arch }}
        path: ${{ runner.temp }}/custom.tgz
        retention-days: 1
