name: Run tests

on:
    push:
    pull_request:

jobs:
    test:
        strategy:
            matrix:
                node: [16.x, 18.x, 20.x, 22.x]
                os: [ubuntu-latest]
        runs-on: ${{ matrix.os }}
        # Service containers to run with `container-job`
        services:
            # Label used to access the service container
            redis:
                # Docker Hub image
                image: redis
                # Set health checks to wait until redis has started
                options: >-
                    --health-cmd "redis-cli ping"
                    --health-interval 10s
                    --health-timeout 5s
                    --health-retries 5
                ports:
                    - 6379:6379
        steps:
            - uses: actions/checkout@v4

            - name: Start MongoDB
              uses: supercharge/mongodb-github-action@1.7.0
              with:
                  mongodb-version: 6.0
                  mongodb-port: 27017
                  mongodb-db: wildduck-test

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

            - name: Install mongosh
              run: |
                  sudo apt-get update
                  sudo apt-get install -y wget gnupg
                  wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
                  echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
                  sudo apt-get update
                  sudo apt-get install -y mongodb-mongosh

            - run: which mongosh
            - run: npm install
            - name: Run tests
              run: npm run runtest
              env:
                  APPCONF_dbs_redis: redis://127.0.0.1:6379/1
                  APPCONF_dbs_mongodb: 'mongodb://127.0.0.1:27017/wildduck-test?authSource=admin'
