name: Run tests

on:
  push:
    branches:
      - main
  pull_request:
  workflow_dispatch:

env:
  DEBIAN_FRONTEND: noninteractive

concurrency:
  # cancel jobs on PRs only
  group: "${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}"
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
  test:
    name: Test on ${{ matrix.os }}  (NodeJS ${{ matrix.node-version }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          - "ubuntu-22.04"
          - "macos-14"
        node-version: ["18", "20", "22"]

    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}

      - name: setup Linux dependencies
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y \
              curl \
              wget \
              libcairo2-dev \
              libgles2-mesa-dev \
              libgbm-dev \
              libllvm11 \
              libuv1-dev \
              libprotobuf-dev \
              libxxf86vm-dev \
              libwebp-dev \
              xvfb \
              x11-utils
            wget --no-verbose http://archive.ubuntu.com/ubuntu/pool/main/i/icu/libicu66_66.1-2ubuntu2.1_amd64.deb
            sudo apt-get install -y ./libicu66_66.1-2ubuntu2.1_amd64.deb

      - run: |
          npm ci
          npm install coveralls

      - name: test on Linux
        if: runner.os == 'Linux'
        run: |
          xvfb-run -a --server-args="-screen 0 1024x768x24 -ac +render -noreset" \
            npx jest --silent --coverage tests

      - name: test on MacOS
        if: runner.os == 'macos'
        run: npx jest --silent --coverage tests

      - name: Coveralls
        if: runner.os == 'Linux'
        run: curl -sL https://coveralls.io/coveralls-linux.tar.gz | tar -xz && ./coveralls
        env:
          COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
