"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 machine_reporter_exports = {};
__export(machine_reporter_exports, {
  MachineReporter: () => MachineReporter
});
module.exports = __toCommonJS(machine_reporter_exports);
var import_js_tools = require("@nacho-iot/js-tools");
var import_node_fs = require("node:fs");
var import_node_path = require("node:path");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class MachineReporter {
  #progress;
  #currentFile;
  #currentSuite = [];
  #testNumber = 0;
  #failureCount = 0;
  #passCount = 0;
  #failuresDir = "build/failures";
  #currentTest;
  constructor(progress) {
    this.#progress = progress;
  }
  beginRun(_name, _stats, _supportsSuites) {
    this.#testNumber = 0;
    this.#failureCount = 0;
    this.#passCount = 0;
    this.#currentFile = void 0;
    this.#currentSuite = [];
    (0, import_node_fs.rmSync)(this.#failuresDir, { recursive: true, force: true });
  }
  beginSuite(name, _stats, file) {
    if (file) {
      file = this.#toSourcePath(file);
    }
    const suitePath = name;
    const topName = suitePath[0];
    if (topName !== void 0 && (file !== this.#currentFile || topName !== this.#currentSuite[0])) {
      this.#currentFile = file;
      this.#currentSuite = [];
      if (file) {
        process.stdout.write(`${topName} (${file})
`);
      } else {
        process.stdout.write(`${topName}
`);
      }
    }
    let common = 0;
    while (common < this.#currentSuite.length && common < suitePath.length && this.#currentSuite[common] === suitePath[common]) {
      common++;
    }
    for (let i = Math.max(common, 1); i < suitePath.length; i++) {
      const indent = "  ".repeat(i);
      process.stdout.write(`${indent}* ${suitePath[i]}
`);
    }
    this.#currentSuite = suitePath;
  }
  beginTest(name, _stats) {
    this.#currentTest = name;
  }
  beginStep(_name) {
  }
  passTest(_name) {
    this.#testNumber++;
    this.#passCount++;
    const indent = "  ".repeat(this.#currentSuite.length);
    process.stdout.write(`${indent}${this.#testNumber}. ${this.#currentTest} PASS
`);
  }
  failTest(name, detail, descriptor) {
    this.#testNumber++;
    this.#failureCount++;
    const failureNum = String(this.#failureCount).padStart(3, "0");
    const failurePath = `${this.#failuresDir}/${failureNum}.md`;
    (0, import_node_fs.mkdirSync)(this.#failuresDir, { recursive: true });
    (0, import_node_fs.writeFileSync)(failurePath, this.#formatFailureFile(name, detail, descriptor));
    const indent = "  ".repeat(this.#currentSuite.length);
    const code = this.#stripAnsi(detail.id ? detail.id : detail.message.split("\n")[0]);
    process.stdout.write(`${indent}${this.#testNumber}. ${name} FAIL [${code}] (see ./${failurePath})
`);
  }
  endRun(_stats) {
    process.stdout.write(`--- ${this.#failureCount} failed, ${this.#passCount} passed ---
`);
    if (this.#failureCount > 0) {
      this.#progress.status = import_js_tools.Progress.Status.Failure;
    } else if (this.#passCount > 0) {
      this.#progress.status = import_js_tools.Progress.Status.Success;
    } else {
      this.#progress.status = import_js_tools.Progress.Status.Failure;
    }
  }
  failRun(detail) {
    process.stdout.write(`FATAL: ${detail.message}
`);
    if (detail.stack) {
      process.stdout.write(`${detail.stack}
`);
    }
    process.exit(1);
  }
  #toSourcePath(buildPath) {
    let rel = (0, import_node_path.relative)(process.cwd(), buildPath);
    rel = rel.replace(/^build\/(?:esm|cjs)\//, "");
    rel = rel.replace(/\.js$/, ".ts");
    if (!rel.startsWith(".")) {
      rel = `./${rel}`;
    }
    return rel;
  }
  #formatFailureFile(name, detail, descriptor) {
    const suitePath = this.#currentSuite.join(" > ");
    const sections = Array();
    sections.push(`# ${suitePath ? `${suitePath} > ` : ""}${name}`);
    sections.push(`## Error

${this.#stripAnsi(detail.message)}`);
    if (detail.stack) {
      sections.push(`## Stack

${this.#stripAnsi(detail.stack)}`);
    }
    if (detail.actual !== void 0 && detail.expected !== void 0) {
      sections.push(
        `## Diff

Actual:
\`\`\`
${this.#stripAnsi(detail.actual)}
\`\`\`

Expected:
\`\`\`
${this.#stripAnsi(detail.expected)}
\`\`\``
      );
    }
    if (detail.cause) {
      const causeStack = detail.cause.stack ? `

${this.#stripAnsi(detail.cause.stack)}` : "";
      sections.push(`## Cause

${this.#stripAnsi(detail.cause.message)}${causeStack}`);
    }
    if (detail.logs) {
      sections.push(`## Logs

${this.#stripAnsi(detail.logs)}`);
    }
    if (detail.diagnostics) {
      sections.push(`## Diagnostics

${detail.diagnostics}`);
    }
    if (descriptor) {
      const meta = Array();
      for (const key of ["kind", "description", "pics", "app", "timeoutMs", "config", "picsValues"]) {
        const value = descriptor[key];
        if (value !== void 0) {
          meta.push(`- **${key}**: ${typeof value === "object" ? JSON.stringify(value) : value}`);
        }
      }
      if (meta.length) {
        sections.push(`## Metadata

${meta.join("\n")}`);
      }
    }
    return sections.join("\n\n") + "\n";
  }
  #stripAnsi(text) {
    return text.replace(/\x1b\[[0-9;]*m/g, "");
  }
}
//# sourceMappingURL=machine-reporter.js.map
