name: Reusable units test workflow

on:
  workflow_call:
    inputs:
      ref:
        description: GitHub ref to test.
        required: false
        type: string
        default: master
      nodeVersion:
        description: Version of node to use.
        required: false
        type: string
        default: 22.x
      collectCoverage:
        required: false
        default: false
        type: boolean
    secrets:
      CODECOV_TOKEN:
        description: 'Codecov upload token.'
        required: false

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

jobs:
  unit-test:
    name: Unit tests
    runs-on: blacksmith-4vcpu-ubuntu-2204
    env:
      COVERAGE_ENABLED: ${{ inputs.collectCoverage }}
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          ref: ${{ inputs.ref }}

      - name: Build
        uses: n8n-io/n8n/.github/actions/setup-nodejs-blacksmith@f5fbbbe0a28a886451c886cac6b49192a39b0eea # v1.104.1
        with:
          node-version: ${{ inputs.nodeVersion }}

      - name: Test
        run: pnpm test:ci

      - name: Upload test results to Codecov
        if: ${{ !cancelled() }}
        uses: codecov/test-results-action@47f89e9acb64b76debcd5ea40642d25a4adced9f # v1.1.1
        with:
          token: ${{ secrets.CODECOV_TOKEN }}

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@18283e04ce6e62d37312384ff67231eb8fd56d24 # v5.4.3
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
