name: Reusable e2e workflow

on:
  workflow_call:
    inputs:
      branch:
        description: 'GitHub branch to test.'
        required: false
        type: string
      user:
        description: 'User who kicked this off.'
        required: false
        type: string
        default: 'schedule'
      spec:
        description: 'Specify specs.'
        required: false
        default: 'e2e/*'
        type: string
      run-env:
        description: 'Node env version to run tests with.'
        required: false
        default: 'browsers:node18.12.0-chrome107'
        type: string
      record:
        description: 'Record test run.'
        required: false
        default: true
        type: boolean
      parallel:
        description: 'Run tests in parallel.'
        required: false
        default: true
        type: boolean
      containers:
        description: 'Number of containers to run tests in.'
        required: false
        default: '[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]'
        type: string
      pr_number:
        description: 'PR number to run tests for.'
        required: false
        type: number
      node_view_version:
        description: 'Node View version to run tests with.'
        required: false
        default: '1'
        type: string
    secrets:
      CYPRESS_RECORD_KEY:
        description: 'Cypress record key.'
        required: true
    outputs:
      tests_passed:
        description: 'True if all E2E tests passed, otherwise false'
        value: ${{ jobs.check_testing_matrix.outputs.all_tests_passed }}

jobs:
  # single job that generates and outputs a common id
  prepare:
    runs-on: ubuntu-latest
    outputs:
      uuid: ${{ steps.uuid.outputs.value }}
    steps:
      - name: Generate unique ID 💎
        id: uuid
        # take the current commit + timestamp together
        # the typical value would be something like
        # "sha-5d3fe...35d3-time-1620841214"
        run: echo "value=sha-$GITHUB_SHA-time-$(date +"%s")" >> $GITHUB_OUTPUT

  install:
    runs-on: ubuntu-latest
    needs: ['prepare']
    container:
      image: cypress/${{ inputs.run-env }}
      options: --user 1001
    steps:
      - uses: actions/checkout@v4.1.1
        with:
          ref: ${{ inputs.branch }}

      - name: Checkout PR
        if: ${{ inputs.pr_number }}
        run: |
          git fetch origin pull/${{ inputs.pr_number }}/head
          git checkout FETCH_HEAD

      - uses: pnpm/action-setup@v4.0.0

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Cypress build
        uses: cypress-io/github-action@v6.6.1
        with:
          # Disable running of tests within install job
          runTests: false
          install: false
          build: pnpm build

      - name: Cypress install
        working-directory: cypress
        run: pnpm cypress:install

      - name: Cache build artifacts
        uses: actions/cache/save@v4.0.0
        with:
          path: |
            /github/home/.cache
            /github/home/.pnpm-store
            ./packages/**/dist
          key: ${{ github.sha }}-e2e

  testing:
    runs-on: ubuntu-latest
    container:
      image: cypress/${{ inputs.run-env }}
      options: --user 1001
    needs: ['prepare', 'install']
    strategy:
      fail-fast: false
      matrix:
        # If spec is not e2e/* then we run only one container to prevent
        # running the same tests multiple times
        containers: ${{ fromJSON( inputs.spec == 'e2e/*' && inputs.containers || '[1]' ) }}
    steps:
      - uses: actions/checkout@v4.1.1
        with:
          ref: ${{ inputs.branch }}

      - name: Checkout PR
        if: ${{ inputs.pr_number }}
        run: |
          git fetch origin pull/${{ inputs.pr_number }}/head
          git checkout FETCH_HEAD

      - uses: pnpm/action-setup@v4.0.0

      - name: Restore cached pnpm modules
        uses: actions/cache/restore@v4.0.0
        with:
          path: |
            /github/home/.cache
            /github/home/.pnpm-store
            ./packages/**/dist
          key: ${{ github.sha }}-e2e

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Cypress run
        uses: cypress-io/github-action@v6.6.1
        with:
          working-directory: cypress
          install: false
          start: pnpm start
          wait-on: 'http://localhost:5678'
          wait-on-timeout: 120
          record: ${{ inputs.record }}
          parallel: ${{ fromJSON( inputs.spec == 'e2e/*' && inputs.parallel || false ) }}
          # We have to provide custom ci-build-id key to make sure that this workflow could be run multiple times
          # in the same parent workflow
          ci-build-id: ${{ needs.prepare.outputs.uuid }}
          spec: '${{ inputs.spec }}'
        env:
          NODE_OPTIONS: --dns-result-order=ipv4first
          CYPRESS_NODE_VIEW_VERSION: ${{ inputs.node_view_version }}
          CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          E2E_TESTS: true
          COMMIT_INFO_MESSAGE: 🌳 ${{ inputs.branch }} 🖥️ ${{ inputs.run-env }} 🤖 ${{ inputs.user }} 🗃️ ${{ inputs.spec }}
          SHELL: /bin/sh

  # Check if all tests passed and set the output variable
  check_testing_matrix:
    runs-on: ubuntu-latest
    needs: [testing]
    outputs:
      all_tests_passed: ${{ steps.all_tests_passed.outputs.result }}
    steps:
      - name: Check all tests passed
        id: all_tests_passed
        run: |
          success=true
          for status in ${{ needs.testing.result }}; do
            if [ $status != "success" ]; then
              success=false
              break
            fi
          done
          echo "::set-output name=result::$success"