const { exec } = require('node:child_process');

// you can use any other library for copying directories recursively
const fse = require('fs-extra');

let controller; // this will be used to terminate the PocketBase process

const srcTestDirPath = "./test_pb_data";
const tempTestDirPath = "./temp_test_pb_data";

beforeEach(() => {
  // copy test_pb_date to a temp location
  fse.copySync(srcTestDirPath, tempTestDirPath);

  controller = new AbortController();

  // start PocketBase with the test_pb_data
  exec('./pocketbase serve --dir=' + tempTestDirPath, { signal: controller.signal});
});

afterEach(() => {
  // stop the PocketBase process
  controller.abort();

  // clean up the temp test directory
  fse.removeSync(tempTestDirPath);
});
