name: "Before e2e"
description: "Before e2e"

inputs:
  from:
    description: 'path to download the artifact'
    required: true
    type: string
  to:
    description: 'path to save artifact to'
    required: true
    type: string
  id:
    description: 'test suite id'
    required: true
    type: number
  aws-access-key-id:
    description: 'aws access key id'
    required: true
    type: string
  aws-secret-access-key:
    description: 'aws secret access key'
    required: true
    type: string
  aws-region:
    description: 'aws region'
    required: true
    type: string

runs:
  using: "composite"
  steps:
    - name: Check content UP
      shell: bash
      run: ./node_modules/@alfresco/adf-cli/bin/adf-cli check-cs-env --host $APP_CONFIG_ECM_HOST -u $ADMIN_EMAIL -p $ADMIN_PASSWORD || exit 1

    - name: Download artifacts
      uses: ./.github/actions/download-job-artifact
      with:
        artifact: ${{ inputs.from }}
        output: ${{ inputs.to }}
        aws-access-key-id: ${{ inputs.aws-access-key-id }}
        aws-secret-access-key: ${{ inputs.aws-secret-access-key }}
        aws-region: ${{ inputs.aws-region }}

    - name: Replace variables in app.config.json
      shell: bash
      run: |
        APP_CONFIG_FILE_PATH="${{ inputs.to }}/app.config.json"
        npx envsub --all $APP_CONFIG_FILE_PATH $APP_CONFIG_FILE_PATH || exit 1
        echo  -n "     \_ Validating replaced config file ... ";
        npx ajv validate -s ./node_modules/@alfresco/adf-core/app.config.schema.json -d $APP_CONFIG_FILE_PATH --errors=text --verbose || exit 4
        if grep -E -q '\$\{[A-Z0-9_]*\}' $APP_CONFIG_FILE_PATH; then
            echo -e "\e[31m        \_ ERROR: Variables are still present in the app.config.json file. Some of them might not have default value set.\e[0m";
            exit 5;
        fi

    - name: Update webdriver-manager
      shell: bash
      run: |
        npm install -g webdriver-manager
        if [ "$CI" = "true" ]; then
          export chrome=$(google-chrome --product-version)
          echo "Updating wevdriver-manager with chromedriver: $chrome."
          webdriver-manager update --gecko=false --versions.chrome=$chrome
        else
          echo "Updating wedriver-manager with latest chromedriver, be sure to use evergreen Chrome."
          webdriver-manager update --gecko=false
        fi
