name: Run tests

on:
  workflow_call:
    inputs:
      node-version:
        required: true
        type: string
      runs-on:
        required: true
        type: string

permissions:
  contents: read

jobs:
  test:
    name: Test with Node.js ${{ inputs.node-version }} on ${{ inputs.runs-on }}
    timeout-minutes: 15
    runs-on: ${{ inputs.runs-on }}
    steps:
      - name: Checkout
        uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
        with:
          persist-credentials: false

      - name: Setup Node.js@${{ inputs.node-version }}
        uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
        with:
          node-version: ${{ inputs.node-version }}

      - name: Print version information
        run: |
          echo OS: $(node -p "os.version()")
          echo Node.js: $(node --version)
          echo npm: $(npm --version)
          echo git: $(git --version)

      - name: Install dependencies
        run: npm install

      - name: Print installed dependencies
        run: npm ls --all
        continue-on-error: true

      - name: Run tests with coverage
        id: coverage
        if: inputs.runs-on == 'ubuntu-latest' && inputs.node-version == 22
        run: npm run coverage:ci
        env:
          CI: true
          NODE_V8_COVERAGE: ./coverage/tmp

      - name: Run tests
        if: steps.coverage.outcome == 'skipped'
        run: npm run test:javascript
        env:
          CI: true

      - name: Coverage Report
        if: inputs.runs-on == 'ubuntu-latest' && inputs.node-version == 20
        uses: codecov/codecov-action@1e68e06f1dbfde0e4cefc87efeba9e4643565303 # v5.1.2
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
