name: Test

on:
  push:
    branches:
    - master
  pull_request:
    branches:
    - master
  # Allow manually triggering tests
  workflow_dispatch:
    branches:
    - master

jobs:
  test:
    name: Node.js ${{ matrix.node }} + mongoDB ${{ matrix.mongodb }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        node:
        - 18
        - 20
        - 22
        mongodb:
        - 4.4
        - 5.0
        - 6.0
        - 7.0
        - 8.0
    services:
      mongodb:
        image: mongo:${{ matrix.mongodb }}
        ports:
        - 27017:27017
    timeout-minutes: 10
    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-node@v4
      with:
        node-version: ${{ matrix.node }}
    - name: Install
      run: npm install
    - name: Lint
      run: npm run lint
    - name: Test
      run: npm run test-cover
    - name: Coveralls
      uses: coverallsapp/github-action@master
      with:
        github-token: ${{ secrets.GITHUB_TOKEN }}
        flag-name: node-${{ matrix.node }}-mongodb-${{ matrix.mongodb }}
        parallel: true

  finish:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - name: Submit coverage
        uses: coverallsapp/github-action@master
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          parallel-finished: true
