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
      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]'
        type: string
      pr_number:
        description: 'PR number to run tests for.'
        required: false
        type: number
    secrets:
      CURRENTS_RECORD_KEY:
        description: 'Currents record key.'
        required: true

env:
  NODE_OPTIONS: --max-old-space-size=3072

jobs:
  testing:
    runs-on: blacksmith-2vcpu-ubuntu-2204
    outputs:
      dashboardUrl: ${{ steps.cypress.outputs.dashboardUrl }}
    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@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
      - name: Set up and build
        uses: ./.github/actions/setup-nodejs-blacksmith

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

      - name: Cypress run
        id: cypress
        uses: cypress-io/github-action@be1bab96b388bbd9ce3887e397d373c8557e15af # v6.9.2
        with:
          working-directory: cypress
          install: false
          start: pnpm start
          wait-on: 'http://localhost:5678'
          wait-on-timeout: 120
          record: false
          spec: ${{ inputs.spec == 'e2e/*' && format('e2e/group{0}/**/*.cy.ts', matrix.containers) || inputs.spec }}
        env:
          NODE_OPTIONS: --dns-result-order=ipv4first
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          E2E_TESTS: true
          COMMIT_INFO_MESSAGE: 🌳 ${{ inputs.branch }} 🤖 ${{ inputs.user }} 🗃️ ${{ inputs.spec }}
          SHELL: /bin/sh

      - name: Upload test results artifact
        if: always()
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: test-results-${{ matrix.containers }}
          path: cypress/test-results-*.xml

  upload-to-currents:
    needs: testing
    if: always()
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Download all test results
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
        with:
          path: test-results

      - name: Merge and upload to Currents
        run: |
          npm install -g @currents/cmd junit-report-merger
          # Merge all XML files, so Currents can show a single view for Cypress
          jrm combined-results.xml "test-results/**/test-results-*.xml"

      - name: Upload merged XML as artifact
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: merged-junit-results
          path: combined-results.xml

      - name: Convert and upload to Currents
        run: |
          currents convert \
            --input-format=junit \
            --input-file=combined-results.xml \
            --output-dir=.currents \
            --framework=node \
            --framework-version=cypress-14.4.0

          currents upload \
            --project-id=I0yzoc \
            --key=${{ secrets.CURRENTS_RECORD_KEY }} \
            --ci-build-id=n8n-io/n8n-${{ github.run_id }}-${{ github.run_attempt }} \
            --report-dir=.currents \
            --tag=cypress
