// src/common/utils.ts
import { elementByPositionWithElementInfo } from "@midscene_spp/core/ai-model";
import { uploadTestInfoToServer } from "@midscene_spp/core/utils";
import { MIDSCENE_REPORT_TAG_NAME, getAIConfig } from "@midscene_spp/shared/env";
import {
  generateElementByPosition,
  getNodeFromCacheList,
  traverseTree,
  treeToList
} from "@midscene_spp/shared/extractor";
import { resizeImgBase64 } from "@midscene_spp/shared/img";
import { assert, logMsg, uuid } from "@midscene_spp/shared/utils";
import dayjs from "dayjs";
function reportFileName(tag = "web") {
  const reportTagName = getAIConfig(MIDSCENE_REPORT_TAG_NAME);
  const dateTimeInFileName = dayjs().format("YYYY-MM-DD_HH-mm-ss");
  const uniqueId = uuid().substring(0, 8);
  return `${reportTagName || tag}-${dateTimeInFileName}-${uniqueId}`;
}
function printReportMsg(filepath) {
  logMsg(`Midscene - report file updated: ${filepath}`);
}
function replaceIllegalPathCharsAndSpace(str) {
  return str.replace(/[/\\:*?"<>| ]/g, "-");
}

// src/playwright/reporter/index.ts
import { writeDumpReport } from "@midscene_spp/core/utils";
function logger(...message) {
  if (process.env.DEBUG === "true") {
    console.log("Midscene e2e report:", ...message);
  }
}
var testDataList = [];
var mergedFilename;
var testTitleToFilename = /* @__PURE__ */ new Map();
function getStableFilename(testTitle) {
  if (!testTitleToFilename.has(testTitle)) {
    const baseTag = `playwright-${replaceIllegalPathCharsAndSpace(testTitle)}`;
    const generatedFilename = reportFileName(baseTag);
    testTitleToFilename.set(testTitle, generatedFilename);
  }
  return testTitleToFilename.get(testTitle);
}
function updateReport(mode, testId) {
  if (mode === "separate") {
    const testData = testDataList.find(
      (data) => data.attributes?.playwright_test_id === testId
    );
    if (testData) {
      const stableFilename = getStableFilename(
        testData.attributes?.playwright_test_title
      );
      const reportPath = writeDumpReport(stableFilename, [testData]);
      reportPath && printReportMsg(reportPath);
    }
  } else if (mode === "merged") {
    if (!mergedFilename) {
      mergedFilename = reportFileName("playwright-merged");
    }
    const reportPath = writeDumpReport(mergedFilename, testDataList);
    reportPath && printReportMsg(reportPath);
  } else {
    throw new Error(
      `Unknown reporter type in playwright config: ${mode}, only support 'merged' or 'separate'`
    );
  }
}
function getMode(reporterType) {
  if (!reporterType) {
    return "merged";
  }
  if (reporterType !== "merged" && reporterType !== "separate") {
    throw new Error(
      `Unknown reporter type in playwright config: ${reporterType}, only support 'merged' or 'separate'`
    );
  }
  return reporterType;
}
var MidsceneReporter = class {
  async onBegin(config, suite) {
    const reporterType = config.reporter?.[1]?.[1]?.type;
    this.mode = getMode(reporterType);
  }
  onTestBegin(test, _result) {
  }
  onTestEnd(test, result) {
    const dumpAnnotation = test.annotations.find((annotation) => {
      return annotation.type === "MIDSCENE_DUMP_ANNOTATION";
    });
    if (!dumpAnnotation?.description)
      return;
    const retry = result.retry ? `(retry #${result.retry})` : "";
    const testId = `${test.id}${retry}`;
    const testData = {
      dumpString: dumpAnnotation.description,
      attributes: {
        playwright_test_id: testId,
        playwright_test_title: `${test.title}${retry}`,
        playwright_test_status: result.status,
        playwright_test_duration: result.duration
      }
    };
    testDataList.push(testData);
    updateReport(this.mode, testId);
    test.annotations = test.annotations.filter(
      (annotation) => annotation.type !== "MIDSCENE_DUMP_ANNOTATION"
    );
  }
  onEnd(result) {
    updateReport(this.mode);
    logger(`Finished the run: ${result.status}`);
  }
};
var reporter_default = MidsceneReporter;
export {
  reporter_default as default
};

//# sourceMappingURL=playwright-report.js.map