name: ci

on: [push, pull_request]

jobs:
  test:
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        node-version: [16, 18, 20]
        os: [ubuntu-latest, macos-latest]
        exclude:
          - os: macos-latest
            node-version: 16
          - os: macos-latest
            node-version: 18

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Update APT
        if: matrix.os == 'ubuntu-latest'
        run: sudo apt update

      - name: Install OS dependencies
        if: matrix.os == 'ubuntu-latest'
        run: |
          sudo apt update
          sudo apt install lcov

      - name: Display versions
        if: matrix.os == 'ubuntu-latest'
        run: |
          g++ --version
          lcov --version

      - name: Print number of CPUs
        if: matrix.os == 'ubuntu-latest'
        run: nproc

      - name: Print number of CPUs
        if: matrix.os == 'macos-latest'
        run: sysctl -n hw.logicalcpu

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

      - name: Install packages
        run: npm ci

      - name: Lint
        if: matrix.os == 'ubuntu-latest'
        run: npx grunt lint

      - name: Coverage
        if: matrix.os == 'ubuntu-latest'
        run: npm run coverage

      - name: Coveralls
        if: matrix.os == 'ubuntu-latest'
        uses: coverallsapp/github-action@master
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          path-to-lcov: coverage/lcov_final.info

      - name: Test
        if: matrix.os == 'macos-latest'
        run: npx grunt test

      - name: Test Docker build
        if: matrix.os == 'ubuntu-latest'
        run: docker buildx build -f test/Dockerfile .
