"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var state_exports = {};
__export(state_exports, {
  State: () => State
});
module.exports = __toCommonJS(state_exports);
var import_js_tools = require("@nacho-iot/js-tools");
var import_promises = require("node:fs/promises");
var import_node_os = require("node:os");
var import_node_path = require("node:path");
var import_docker = require("../docker/docker.js");
var import_edit = require("../docker/edit.js");
var import_image = require("../docker/image.js");
var import_volume = require("../docker/volume.js");
var import_mocha = require("../mocha.js");
var import_runner = require("../runner.js");
var import_test_descriptor = require("../test-descriptor.js");
var import_accessory_server = require("./accessory-server.js");
var import_config = require("./config.js");
var import_container_command_pipe = require("./container-command-pipe.js");
var import_source = require("./pics/source.js");
var import_python_test = require("./python-test.js");
var import_yaml_test = require("./yaml-test.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const Values = {
  isInitialized: false,
  subject: void 0,
  test: void 0,
  mainContainer: void 0,
  mdnsContainer: void 0,
  defaultPics: void 0,
  defaultPicsFilename: void 0,
  tests: void 0,
  initializedSubjects: /* @__PURE__ */ new WeakSet(),
  activeSubject: void 0,
  singleUseSubject: false,
  closers: Array(),
  subjects: /* @__PURE__ */ new Map(),
  subjectsByApp: /* @__PURE__ */ new Map(),
  snapshots: /* @__PURE__ */ new Map(),
  containerLifecycleInstalled: false,
  testMap: /* @__PURE__ */ new Map(),
  pullBeforeTesting: true,
  commandPipe: void 0,
  localControllerPort: void 0,
  restartFlagHostDir: void 0
};
const State = {
  get container() {
    const container = Values.mainContainer;
    if (container === void 0) {
      throw new Error("Docker container is not initialized");
    }
    return container;
  },
  set subject(subject) {
    Values.subject = subject;
  },
  get subject() {
    const subject = Values.subject;
    if (subject === void 0) {
      throw new Error("no default subject configured");
    }
    return subject;
  },
  registerSubjectForApp(app, factory) {
    if (Values.subjectsByApp.has(app)) {
      throw new Error(`Subject factory already registered for app "${app}"`);
    }
    Values.subjectsByApp.set(app, factory);
  },
  subjectForApp(app) {
    return Values.subjectsByApp.get(app);
  },
  get pullBeforeTesting() {
    return Values.pullBeforeTesting;
  },
  set pullBeforeTesting(value) {
    Values.pullBeforeTesting = value;
  },
  get defaultPics() {
    if (Values.defaultPics === void 0) {
      throw new Error("PICS not initialized");
    }
    return Values.defaultPics;
  },
  get defaultPicsFilename() {
    if (Values.defaultPicsFilename === void 0) {
      throw new Error("PICS not initialized");
    }
    return Values.defaultPicsFilename;
  },
  get tests() {
    if (Values.tests === void 0) {
      throw new Error("CHIP test descriptor not loaded");
    }
    return Values.tests;
  },
  get test() {
    if (Values.test === void 0) {
      throw new Error("No active test");
    }
    return Values.test;
  },
  get localControllerPort() {
    return Values.localControllerPort;
  },
  get restartFlagHostDir() {
    return Values.restartFlagHostDir;
  },
  get isInitialized() {
    return Values.isInitialized;
  },
  /**
   * Setup.
   */
  async initialize() {
    if (Values.isInitialized) {
      return;
    }
    const { progress } = import_runner.TestRunner.current;
    progress.update("Initializing containers");
    try {
      await initialize();
      const image = await State.container.image;
      const info = await image.inspect();
      const chipCommit = formatSha(info.Config.Labels["org.opencontainers.image.revision"] ?? "(unknown)");
      const imageVersion = info.Config.Labels["org.opencontainers.image.version"] ?? "(unknown)";
      const arch = info.Architecture;
      progress.success(
        `Initialized CHIP ${import_js_tools.ansi.bold(chipCommit)} image ${import_js_tools.ansi.bold(imageVersion)} for ${import_js_tools.ansi.bold(arch)}`
      );
    } catch (e) {
      progress.failure("Initializing containers");
      throw e;
    }
  },
  /**
   * Teardown.
   */
  async close() {
    await State.deactivateSubject();
    let closer;
    while (closer = Values.closers.pop()) {
      try {
        await closer();
      } catch (e) {
        console.error("Teardown error:", e);
      }
    }
    Values.isInitialized = false;
  },
  /**
   * Add cleanup logic.
   */
  onClose(fn) {
    Values.closers.push(fn);
  },
  /**
   * Hook mocha to initialize CHIP testing.
   */
  install() {
    if (Values.containerLifecycleInstalled) {
      return;
    }
    Values.containerLifecycleInstalled = true;
    (0, import_mocha.beforeRun)(State.initialize);
    (0, import_mocha.afterRun)(State.close);
  },
  /**
   * Run a CHIP test.
   */
  async run(test, args, beforeTest) {
    const { reporter } = import_runner.TestRunner.current;
    const subject = Values.activeSubject;
    try {
      Values.test = test;
      await beforeTest(subject, test);
      await test.invoke(subject, reporter.beginStep.bind(reporter), args);
    } finally {
      Values.test = void 0;
    }
  },
  /**
   * Pass a backchannel command to the active subject.
   */
  backchannel(command) {
    if (Values.activeSubject === void 0) {
      throw new Error(`Backchannel ${command.name} without active test subject`);
    }
    return Values.activeSubject.backchannel(command);
  },
  /**
   * Open a back-channel command pipe.
   */
  async openPipe(name) {
    if (Values.commandPipe === void 0) {
      Values.commandPipe = new import_container_command_pipe.ContainerCommandPipe(State.container, this);
      await Values.commandPipe.initialize();
      State.onClose(async () => {
        await Values.commandPipe?.close();
        Values.commandPipe = void 0;
      });
    }
    await Values.commandPipe.installForApp(name);
  },
  /**
   * Obtain a {@link Test}.
   */
  testFor(identifier) {
    if (typeof identifier === "string") {
      const maybeDescriptor = this.tests.stat(identifier)?.descriptor;
      if (maybeDescriptor === void 0) {
        throw new Error(`No such test ${identifier}`);
      }
      identifier = maybeDescriptor;
    }
    let test = Values.testMap.get(identifier);
    if (!test) {
      test = createTest(identifier);
      Values.testMap.set(identifier, test);
    }
    return test;
  },
  /**
   * Prepare the test environment for a subject.
   *
   * On first activation, commissions the subject.  Thereafter the subject is either already active or reactivated
   * here.
   */
  async activateSubject(factory, startCommissioned, test, beforeStart, appArgs) {
    let subject;
    if (startCommissioned) {
      subject = loadSubject(factory, test.descriptor.kind, appArgs);
    } else {
      subject = factory(test.descriptor.kind ?? "unknown", { appArgs });
    }
    if (Values.activeSubject === subject) {
      return;
    }
    const { progress } = import_runner.TestRunner.current;
    await progress.subtask("activating subject", async () => {
      await State.container.exec(["bash", "-c", 'export GLOBIGNORE="/tmp/*_fifo_*"; rm -rf /tmp/*']);
      if (!startCommissioned) {
        await subject.initialize();
        State.onClose(subject.close.bind(subject));
        await beforeStart?.(subject, test);
        await subject.start();
      } else if (!Values.initializedSubjects.has(subject)) {
        await subject.initialize();
        State.onClose(subject.close.bind(subject));
        await beforeStart?.(subject, test);
        await subject.start();
        await test.initializeSubject(subject);
        const dir = storageDirFor(subject);
        Values.snapshots.set(subject, await subject.snapshot());
        await State.container.exec(["bash", "-c", `mkdir -p ${dir} && cp -a /tmp/* ${dir}`]);
        Values.initializedSubjects.add(subject);
      } else {
        const snapshot = Values.snapshots.get(subject);
        if (snapshot === void 0) {
          throw new Error(`No snapshot captured for ${subject.id}`);
        }
        await subject.restore(snapshot);
        await State.container.exec(["bash", "-c", `cp -a ${storageDirFor(subject)}/* /tmp`]);
        await beforeStart?.(subject, test);
        await subject.start();
      }
    });
    Values.activeSubject = subject;
    Values.singleUseSubject = !startCommissioned;
  },
  /**
   * Stop the current test subject, if any.
   *
   * This stops the subject but leaves it initialized (commissioned).  This allows us to quickly swap subjects depending
   * on the current test.
   *
   * Final teardown of subjects occurs once all tests complete.
   */
  async deactivateSubject() {
    if (Values.activeSubject === void 0) {
      return;
    }
    try {
      if (Values.singleUseSubject) {
        await Values.activeSubject.close();
      } else {
        await Values.activeSubject.stop();
      }
    } catch (e) {
      console.warn("Error deactivating test subject", e);
    } finally {
      Values.activeSubject = void 0;
    }
  },
  /**
   * Clear the MDNS cache.
   */
  async clearMdns() {
    if (!Values.mdnsContainer) {
      throw new Error("Cannot reset MDNS because MDNS container is not initialized");
    }
    await this.deactivateSubject();
    await Values.mdnsContainer.exec("/bin/mdns-clear");
  }
};
async function initialize() {
  await configureContainer();
  await configureScripts();
  await configurePics();
  await configureTests();
  await configureNetwork();
  if (import_config.Constants.useLocalController) {
    await configureLocalController();
  }
  Values.isInitialized = true;
}
async function configureContainer() {
  const docker = new import_docker.Docker();
  let platform = import_config.Constants.platform;
  if (Values.pullBeforeTesting) {
    await docker.pull(import_config.Constants.imageName, platform);
  } else if (import_config.Constants.selectedPlatform === void 0) {
    const arch = (await (0, import_image.Image)(docker, import_config.Constants.imageName).inspect()).Architecture;
    platform = `linux/${arch}`;
  }
  const mdnsVolume = (0, import_volume.Volume)(docker, import_config.Constants.mdnsVolumeName);
  await mdnsVolume.open();
  const restartFlagHostDir = await (0, import_promises.mkdtemp)((0, import_node_path.join)((0, import_node_os.tmpdir)(), "matter-restart-"));
  Values.restartFlagHostDir = restartFlagHostDir;
  const composition = docker.compose("matter.js", {
    image: import_config.Constants.imageName,
    platform,
    binds: { [mdnsVolume.name]: "/run/dbus" },
    autoRemove: true,
    // Meh.  Don't have non-host network working yet
    network: "host"
    //Network(docker, Constants.networkName),
  });
  await composition.add({
    name: "dbus",
    command: ["/usr/bin/dbus-daemon", "--nopidfile", "--system", "--nofork"]
  });
  Values.mdnsContainer = await composition.add({
    name: "mdns",
    command: ["/bin/mdns-run"]
  });
  Values.mainContainer = await composition.add({
    name: "chip",
    recreate: true,
    binds: {
      [mdnsVolume.name]: "/run/dbus",
      [restartFlagHostDir]: import_config.Constants.RestartFlagDir
    }
  });
  State.onClose(async () => {
    try {
      await composition.close();
    } catch (e) {
      console.error("Error terminating containers:", e);
    }
    Values.mainContainer = void 0;
    try {
      await (0, import_promises.rm)(restartFlagHostDir, { recursive: true, force: true });
    } catch (e) {
      console.warn("Error cleaning up restart flag directory:", e);
    }
    Values.restartFlagHostDir = void 0;
  });
}
async function configureScripts() {
  await State.container.edit(
    import_edit.edit.sed("s/sleep(0.001)/sleep(.02)/"),
    // This is the one we actually use
    "/usr/local/lib/python3.12/dist-packages/matter/testing/matter_testing.py",
    // Patching here too just for completeness
    "/src/python_testing/matter_testing_infrastructure/matter/testing/matter_testing.py"
  );
}
async function configurePics() {
  Values.defaultPics = await import_source.PicsSource.load(import_config.Constants.defaultPics);
  Values.defaultPicsFilename = await import_source.PicsSource.install(Values.defaultPics);
}
async function configureTests() {
  const { container } = State;
  const descriptor = JSON.parse(await container.read(import_config.ContainerPaths.descriptorFile));
  if (descriptor.format !== import_test_descriptor.TestDescriptor.CURRENT_FORMAT) {
    throw new Error(`Invalid descriptor format "${descriptor.format}" (expected ${import_test_descriptor.TestDescriptor.CURRENT_FORMAT})`);
  }
  if (!Array.isArray(descriptor.members)) {
    throw new Error(`CHIP test descriptor has no members`);
  }
  Values.tests = import_test_descriptor.TestDescriptor.Filesystem(descriptor);
}
async function configureNetwork() {
  const accessoryServer = await import_accessory_server.AccessoryServer.create(State);
  State.onClose(async () => {
    try {
      await accessoryServer.close();
    } catch (e) {
      console.error("Error closing accessory server:", e);
    }
  });
  await State.container.edit(
    import_edit.edit.sed("s/10.10.10.5/127.0.0.1/g", `s/_PORT = 9000/_PORT = ${accessoryServer.port}/g`),
    import_config.ContainerPaths.accessoryClient
  );
}
async function configureLocalController() {
  const dummyScript = '#!/bin/bash\necho "== WebSocket Server Ready"\nexec sleep infinity\n';
  await State.container.exec([
    "bash",
    "-c",
    `echo '${dummyScript}' > /bin/mjs-ws-dummy && chmod +x /bin/mjs-ws-dummy`
  ]);
  Values.localControllerPort = import_config.Constants.controllerPort;
}
function loadSubject(factory, kind, appArgs) {
  let forFactory = Values.subjects.get(factory);
  if (forFactory === void 0) {
    Values.subjects.set(factory, forFactory = {});
  }
  let subject = forFactory[kind];
  if (subject === void 0) {
    subject = forFactory[kind] = factory(kind, { appArgs });
  }
  return subject;
}
function storageDirFor(subject) {
  return `"/storage/${subject.id}"`;
}
function createTest(descriptor) {
  if (descriptor.name === void 0) {
    throw new Error(`A CHIP test has no name`);
  }
  if (descriptor.path === void 0) {
    throw new Error(`CHIP test ${descriptor.name} has no path`);
  }
  switch (descriptor.kind) {
    case "yaml":
      return new import_yaml_test.YamlTest(descriptor, State.container, Values.localControllerPort);
    case "py":
      return new import_python_test.PythonTest(descriptor, State.container, Values.restartFlagHostDir);
    default:
      throw new Error(`Cannot implement CHIP test ${descriptor.name} of kind ${descriptor.kind}`);
  }
}
function formatSha(sha) {
  if (sha.startsWith("sha256:")) {
    sha = sha.substring(7);
  }
  return import_js_tools.ansi.bold(sha.substring(0, 12));
}
//# sourceMappingURL=state.js.map
