name: Test npm package

on:
  workflow_call:
    inputs:
      package:
        type: string
        default: '@mmomtchev/ffmpeg@latest'
        description: package tarball or npm name to install
      download:
        type: boolean
        default: false
        description: whether the package artifact needs to be downloaded
  workflow_dispatch:
    inputs:
      package:
        type: string
        default: '@mmomtchev/ffmpeg@latest'
        description: package tarball or npm name to install


jobs:
  test_npm_install:
    runs-on: ${{ matrix.platform }}

    strategy:
      fail-fast: false
      matrix:
        platform:
          - ubuntu-22.04
          - ubuntu-24.04
          - windows-2022
          - windows-2025
          - macos-13
          - macos-14
        node-version: [18.x, 20.x, 22.x]
        options:
          - ''
          - '--build-from-source'

    steps:
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
      - uses: actions/setup-python@v5
        with:
          python-version: "3.11"

      - name: (if on Ubuntu) install fonts
        run: sudo apt-get install -y fonts-ubuntu || true
        if: ${{ contains(matrix.platform, 'ubuntu') }}

      - name: Download the package
        uses: robinraju/release-downloader@v1.12
        with:
          tag: ${{ github.ref_name }}
          fileName: ${{ inputs.package }}
        if: inputs.download
      - name: Install @mmomtchev/ffmpeg from ${{ inputs.package }}
        run: npm install ${{ inputs.package }} --foreground-scripts --verbose ${{ matrix.options }}
        shell: bash
        env:
          PKG_CONFIG_PATH:
      - name: Set up the unit tests
        run: |
          mv node_modules/@mmomtchev/ffmpeg/test .
          npm install @types/chai@4.3.16 @types/mocha @types/node chai@4.3.7 magickwand.js mocha ts-node readable-stream-clone
        shell: bash
      - name: Node options
        shell: bash
        run: echo 'MOCHA_NODE_OPTIONS=--v8-expose-gc' >> $GITHUB_ENV
        if: matrix.node-version != '23.x'
      - name: Node options
        shell: bash
        run: echo 'MOCHA_NODE_OPTIONS=--node-option=no-experimental-strip-types --node-option=expose-gc' >> $GITHUB_ENV
        if: matrix.node-version == '23.x'
      - name: Run the unit tests
        run: |
          cd test
          npx mocha ${{ env.MOCHA_NODE_OPTIONS }} --reporter=tap -r ts-node/register --timeout 120000 *.cjs *.mjs *.ts
        env:
          TS_NODE_PROJECT: tsconfig.test.json
