"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 yaml_test_exports = {};
__export(yaml_test_exports, {
  YamlTest: () => YamlTest
});
module.exports = __toCommonJS(yaml_test_exports);
var import_node_path = require("node:path");
var import_test = require("../device/test.js");
var import_terminal = require("../docker/terminal.js");
var import_text = require("../util/text.js");
var import_chip_test_common = require("./chip-test-common.js");
var import_config = require("./config.js");
var import_source = require("./pics/source.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class YamlTest extends import_test.BaseTest {
  #localControllerPort;
  constructor(descriptor, container, localControllerPort) {
    super(descriptor, container);
    this.#localControllerPort = localControllerPort;
  }
  async initializeSubject(subject) {
    if (this.#localControllerPort !== void 0) {
      await this.#initializeSubjectViaWebSocket(subject);
    } else {
      await this.#initializeSubjectViaChipTool(subject);
    }
  }
  async #initializeSubjectViaChipTool(subject) {
    const command = ["chip-tool", "pairing"];
    const { kind, passcode, discriminator, network } = subject.commissioning;
    switch (kind) {
      case "on-network":
        command.push("onnetwork-long", "0x12344321");
        break;
      case "ble-wifi":
        if (network?.kind !== "wifi") {
          throw new Error(`Must specify WiFi network for commissioning of subject ${subject.id}`);
        }
        command.push("ble-wifi", "0x12344321", network.ssid, network.password);
        break;
      case "ble-thread":
        if (network?.kind !== "thread") {
          throw new Error(`Must specify Thread network for commissioning of subject ${subject.id}`);
        }
        command.push("ble-wifi", "0x12344321", network.datasetHex);
        break;
      default:
        throw new Error(`Unknown commissioning method ${subject.commissioning.kind} for subject ${subject.id}`);
    }
    command.push(`${passcode}`, `${discriminator}`);
    const terminal = await this.container.exec(command, import_terminal.Terminal.Line);
    try {
      for await (const line of terminal) {
        MockLogger.injectExternalMessage("PAIR", line);
      }
    } catch (e) {
      throw new Error("Error pairing test app", { cause: e });
    }
  }
  /**
   * Commission via the local matter.js WebSocket controller.  The YAML runner's WebSocketRunner connects to the
   * host-side controller, which handles the `pairing code <nodeId> <qrCode>` text command.
   */
  async #initializeSubjectViaWebSocket(subject) {
    const { qrPairingCode } = subject.commissioning;
    const terminal = await this.container.exec(
      [
        "python3",
        import_config.ContainerPaths.yamlRunner,
        "pairing",
        "code",
        "0x12344321",
        qrPairingCode,
        "--server_path",
        import_config.ContainerPaths.dummyWsServer
      ],
      import_terminal.Terminal.Line
    );
    try {
      for await (const line of terminal) {
        MockLogger.injectExternalMessage("PAIR", line);
      }
    } catch (e) {
      throw new Error("Error pairing test app via WebSocket controller", { cause: e });
    }
  }
  async invoke(subject, step, args) {
    if (!args.includes("--PICS")) {
      args.push("--PICS", await import_source.PicsSource.install(subject.pics));
    }
    const runnerArgs = this.#localControllerPort !== void 0 ? import_config.Constants.YamlRunnerArgs.map(
      (arg, i, arr) => arr[i - 1] === "--server_path" ? import_config.ContainerPaths.dummyWsServer : arg
    ) : import_config.Constants.YamlRunnerArgs;
    const terminal = await this.container.exec(
      ["python3", import_config.ContainerPaths.yamlRunner, "tests", (0, import_node_path.basename)(this.descriptor.path), ...runnerArgs, ...args],
      import_terminal.Terminal.Line
    );
    let passed = false;
    for await (let line of terminal) {
      line = line.replaceAll("\r\n", "\n").replaceAll("	", "  ");
      line = (0, import_chip_test_common.parseStep)(line, step);
      if ((0, import_text.deansify)(line).match(/Test finished.+ 0 errors .+/)) {
        passed = true;
      }
      let first = true;
      for (let part of line.split("\r")) {
        if (first) {
          first = false;
        } else {
          part = `    ${part}`;
        }
        MockLogger.injectExternalMessage("CHIP", part);
      }
    }
    if (!passed) {
      throw new Error("Yaml test exited without error but did not indicate successful test");
    }
  }
}
//# sourceMappingURL=yaml-test.js.map
