# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
    push:
        branches: [main]
    pull_request:
        branches: [main]

jobs:
    build:
        runs-on: ubuntu-latest
        services:
            postgres:
                image: postgres:15
                env:
                    POSTGRES_PASSWORD: postgres
                    POSTGRES_DB: postgres
                ports:
                    - 5432:5432
                options: >-
                    --health-cmd "pg_isready -U postgres"
                    --health-interval 10s
                    --health-timeout 5s
                    --health-retries 5

        strategy:
            matrix:
                node-version: [22.x]
                # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

        steps:
            - uses: actions/checkout@v4
            - name: Use Node.js ${{ matrix.node-version }}
              uses: actions/setup-node@v4
              with:
                  node-version: ${{ matrix.node-version }}
                  cache: "npm"
            - name: Start MongoDB
              uses: supercharge/mongodb-github-action@1.11.0
              with:
                  mongodb-version: "7.0"
                  mongodb-replica-set: rs0
            - name: unit-test
              run: |
                  npm install
                  npm test
              env:
                  COSMOSDB_CONNECTION_STRING: ${{ secrets.COSMOSDB_CONNECTION_STRING }}
                  POSTGRES_CONNECTION_STRING: postgres://postgres:postgres@localhost:5432/postgres
