name: Memory Leak Detect

env:
  DEBUG: 'napi:*'

on:
  push:
    branches:
      - main
  pull_request:

jobs:
  build_and_test:
    name: Memory leak detect job
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version: 16
          check-latest: true
          cache: 'yarn'

      - name: Install stable
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable-x86_64-unknown-linux-gnu
          profile: minimal
          override: true

      - name: Cache cargo
        uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: stable-memory-leak-detect-cargo-cache

      - name: Cache NPM dependencies
        uses: actions/cache@v3
        with:
          path: .yarn/cache
          key: memory-leak-detect

      - name: 'Install dependencies'
        run: yarn install --immutable

      - name: 'Build TypeScript'
        run: yarn build

      - name: 'Pull docker image'
        run: docker pull node:lts-slim

      - name: 'Build memory test specs'
        run: yarn build:memory

      - name: Memory leak tests
        run: yarn test:memory
        env:
          RUST_BACKTRACE: 1

      - name: Clear the cargo caches
        run: |
          cargo install cargo-cache --no-default-features --features ci-autoclean
          cargo-cache