name: Test
on:
  pull_request:
    paths:
      - ".github/workflows/test.yml"
      - "package.json"
      - "index.js"
      - "lib/**"
      - "test/**.js"
  push:
    paths:
      - ".github/workflows/test.yml"
      - "package.json"
      - "index.js"
      - "lib/**"
      - "test/**.js"
permissions:
  contents: read

jobs:
  lint:
    runs-on: ubuntu-latest
    name: Lint JS-Files
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Setup node
        uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          node-version: 22

      - run: npm install

      - name: Lint JS-Files
        run: npm run lint-js

  test:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        node: [20, 22, 24]
        os: [ubuntu-22.04, ubuntu-24.04]
        mongodb: [6.0.15, 7.0.12, 8.2.0, 9.0.2]
        include:
          - os: ubuntu-22.04 # customize on which matrix the coverage will be collected on
            mongodb: 6.0.15
            node: 22
            coverage: true
    name: Node ${{ matrix.node }} MongoDB ${{ matrix.mongodb }} OS ${{ matrix.os }}
    env:
      MONGOMS_VERSION: ${{ matrix.mongodb }}
      MONGOMS_PREFER_GLOBAL_PATH: 1
      FORCE_COLOR: true
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Setup node
        uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          node-version: ${{ matrix.node }}

      - name: Load MongoDB binary cache
        id: cache-mongodb-binaries
        uses: actions/cache@v6
        with:
          path: ~/.cache/mongodb-binaries
          key: ${{ matrix.os }}-${{ matrix.mongodb }}

      - run: npm install
      # Create ./node_modules/mongoose-separate-require-instance for testing multiple require instances
      - run: npm run create-separate-require-instance
      - name: NPM Test without Coverage
        run: npm run test:ci
        if: matrix.coverage != true
      - name: NPM Test with Coverage
        run: npm run test-coverage:ci
        if: matrix.coverage == true

  test-deno:
    runs-on: ubuntu-22.04
    name: Deno tests
    env:
      MONGOMS_VERSION: 6.0.15
      MONGOMS_PREFER_GLOBAL_PATH: 1
      MONGOMS_DOWNLOAD_DIR: /home/runner/.cache/mongodb-binaries
      FORCE_COLOR: true
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - name: Setup node
        uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          node-version: 22
      - name: Load MongoDB binary cache
        id: cache-mongodb-binaries
        uses: actions/cache@v6
        with:
          path: ~/.cache/mongodb-binaries
          key: deno-${{ env.MONGOMS_VERSION }}
      - name: Setup Deno
        uses: denoland/setup-deno@22d081ff2d3a40755e97629de92e3bcbfa7cf2ed # v2.0.5
        with:
          deno-version: v2.6.x
      - run: deno --version
      - run: npm install
      - name: Run Deno tests
        run: npm run test-deno:ci

  test-atlas-cli:
    runs-on: ubuntu-22.04
    name: Atlas CLI tests
    env:
      IS_ATLAS: true
      MONGOOSE_TEST_URI: mongodb://127.0.0.1:27017/mongoose_test?directConnection=true
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - name: Setup AtlasCLI
        uses: mongodb/atlas-github-action@e3c9e0204659bafbb3b65e1eb1ee745cca0e9f3b # v0.2.2
      - name: Setup local Atlas deployment
        run: atlas deployments setup mycluster --type local --force --port 27017
      - name: Setup node
        uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          node-version: 22
      - run: npm install
      - name: Run tests
        run: npm run test:ci
      - name: Stop deployment
        run: atlas deployments stop mycluster

  test-replica-sets:
    needs:
      - test
    runs-on: ubuntu-22.04
    name: Replica Set tests
    env:
      FORCE_COLOR: true
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - name: Setup node
        uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          node-version: 22
      - run: npm install
      - name: Test
        run: npm run test-rs:ci

  dependency-review:
    name: Dependency Review
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - name: Check out repo
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - name: Dependency review
        uses: actions/dependency-review-action@v5