name: Test rippled

on:
  workflow_call:
    inputs:
      verify_voidstar:
        description: "Whether to verify the presence of voidstar instrumentation."
        required: true
        type: boolean
      run_tests:
        description: "Whether to run unit tests"
        required: true
        type: boolean

      runs_on:
        description: Runner to run the job on as a JSON string
        required: true
        type: string
      image:
        description: "The image to run in (leave empty to run natively)"
        required: true
        type: string

      config_name:
        description: "The name of the configuration."
        required: true
        type: string

      nproc_subtract:
        description: "The number of processors to subtract when calculating parallelism."
        required: true
        type: number

jobs:
  test:
    name: Test ${{ inputs.config_name }}
    runs-on: ${{ fromJSON(inputs.runs_on) }}
    container: ${{ inputs.image != '' && inputs.image || null }}
    timeout-minutes: 30
    steps:
      - name: Cleanup workspace
        if: ${{ runner.os == 'macOS' }}
        uses: XRPLF/actions/.github/actions/cleanup-workspace@3f044c7478548e3c32ff68980eeb36ece02b364e

      - name: Get number of processors
        uses: XRPLF/actions/.github/actions/get-nproc@046b1620f6bfd6cd0985dc82c3df02786801fe0a
        id: nproc
        with:
          subtract: ${{ inputs.nproc_subtract }}

      - name: Download rippled artifact
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
        with:
          name: rippled-${{ inputs.config_name }}

      - name: Make binary executable (Linux and macOS)
        shell: bash
        if: ${{ runner.os == 'Linux' || runner.os == 'macOS' }}
        run: |
          chmod +x ./rippled

      - name: Check linking (Linux)
        if: ${{ runner.os == 'Linux' }}
        shell: bash
        run: |
          ldd ./rippled
          if [ "$(ldd ./rippled | grep -E '(libstdc\+\+|libgcc)' | wc -l)" -eq 0 ]; then
            echo 'The binary is statically linked.'
          else
            echo 'The binary is dynamically linked.'
            exit 1
          fi

      - name: Verifying presence of instrumentation
        if: ${{ inputs.verify_voidstar }}
        shell: bash
        run: |
          ./rippled --version | grep libvoidstar

      - name: Run the embedded tests
        if: ${{ inputs.run_tests }}
        shell: bash
        env:
          BUILD_NPROC: ${{ steps.nproc.outputs.nproc }}
        run: |
          ./rippled --unittest --unittest-jobs ${BUILD_NPROC}

      - name: Run the separate tests
        if: ${{ inputs.run_tests }}
        env:
          EXT: ${{ runner.os == 'Windows' && '.exe' || '' }}
        shell: bash
        run: |
          for test_file in ./doctest/*${EXT}; do
            echo "Executing $test_file"
            chmod +x "$test_file"
            if [[ "${{ runner.os }}" == "Windows" && "$test_file" == "./doctest/xrpl.test.net.exe" ]]; then
              echo "Skipping $test_file on Windows"
            else
              "$test_file"
            fi
          done

      - name: Debug failure (Linux)
        if: ${{ failure() && runner.os == 'Linux' && inputs.run_tests }}
        shell: bash
        run: |
          echo "IPv4 local port range:"
          cat /proc/sys/net/ipv4/ip_local_port_range
          echo "Netstat:"
          netstat -an
