"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var integrationTestSuite_exports = {};
__export(integrationTestSuite_exports, {
  integrationTest: () => integrationTest
});
module.exports = __toCommonJS(integrationTestSuite_exports);
var import_node = require("@zwave-js/core/bindings/fs/node");
var import_shared = require("@zwave-js/shared");
var import_async = require("alcalzone-shared/async");
var import_node_crypto = __toESM(require("node:crypto"), 1);
var import_promises = __toESM(require("node:fs/promises"), 1);
var import_node_os = __toESM(require("node:os"), 1);
var import_node_path = __toESM(require("node:path"), 1);
var import_vitest = require("vitest");
var import_integrationTestSuiteShared = require("./integrationTestSuiteShared.js");
function suite(name, options, modifier) {
  const { controllerCapabilities, nodeCapabilities, customSetup, testBody, debug = false, provisioningDirectory, clearMessageStatsBeforeTest = true, additionalDriverOptions } = options;
  let driver;
  let node;
  let mockPort;
  let serial;
  let continueStartup;
  let mockController;
  let mockNode;
  const cacheDir = import_node_path.default.join(import_node_os.default.tmpdir(), `zjs_test_cache_${import_node_crypto.default.randomBytes(4).toString("hex")}`);
  async function prepareTest() {
    if (debug) {
      console.log(`Running integration test in directory ${cacheDir}`);
    }
    await import_promises.default.rm(cacheDir, { recursive: true, force: true }).catch(import_shared.noop);
    await import_promises.default.mkdir(cacheDir, { recursive: true });
    if (provisioningDirectory) {
      await (0, import_shared.copyFilesRecursive)(import_node.fs, provisioningDirectory, cacheDir);
    }
    ({ driver, continueStartup, mockPort, serial } = await (0, import_integrationTestSuiteShared.prepareDriver)(cacheDir, debug, additionalDriverOptions));
    ({
      mockController,
      mockNodes: [mockNode]
    } = await (0, import_integrationTestSuiteShared.prepareMocks)(mockPort, serial, {
      capabilities: controllerCapabilities,
      securityKeys: driver.options.securityKeys
    }, [
      {
        id: 2,
        capabilities: nodeCapabilities
      }
    ]));
    if (customSetup) {
      await customSetup(driver, mockController, mockNode);
    }
    return new Promise((resolve) => {
      driver.once("driver ready", () => {
        const onReady = /* @__PURE__ */ __name(() => {
          if (clearMessageStatsBeforeTest) {
            mockNode.clearReceivedControllerFrames();
            mockNode.clearSentControllerFrames();
            mockController.clearReceivedHostMessages();
          }
          process.nextTick(resolve);
        }, "onReady");
        node = driver.controller.nodes.getOrThrow(mockNode.id);
        if (options.additionalDriverOptions?.testingHooks?.skipNodeInterview) {
          onReady();
        } else {
          node.once("ready", onReady);
        }
      });
      if (options.additionalDriverOptions?.bootloaderMode === "stay" || options.additionalDriverOptions?.bootloaderMode === "allow") {
        driver.once("bootloader ready", () => {
          process.nextTick(resolve);
        });
      }
      continueStartup();
    });
  }
  __name(prepareTest, "prepareTest");
  const fn = modifier === "only" ? import_vitest.test.sequential.only : modifier === "skip" ? import_vitest.test.sequential.skip : import_vitest.test.sequential;
  fn(name, async (t) => {
    t.onTestFinished(async () => {
      await (0, import_async.wait)(100);
      await driver.destroy();
      if (!debug) {
        await import_promises.default.rm(cacheDir, { recursive: true, force: true }).catch(import_shared.noop);
      }
    });
    await prepareTest();
    await testBody(t, driver, node, mockController, mockNode);
  }, 3e4);
}
__name(suite, "suite");
const integrationTest = /* @__PURE__ */ __name(((name, options) => {
  suite(name, options);
}), "integrationTest");
integrationTest.only = (name, options) => {
  suite(name, options, "only");
};
integrationTest.skip = (name, options) => {
  suite(name, options, "skip");
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  integrationTest
});
//# sourceMappingURL=integrationTestSuite.js.map
