"use strict";
var import_logicalNameResolver = require("./logicalNameResolver");
const cases = [];
function test(name, run) {
  cases.push({ name, run });
}
function assertEqual(actual, expected, msg) {
  const a = JSON.stringify(actual);
  const e = JSON.stringify(expected);
  if (a !== e) throw new Error(`${msg ?? "assertEqual"} \u2014 expected ${e}, got ${a}`);
}
function input(overrides = {}) {
  return {
    initialName: "save_btn",
    sectionName: "main",
    testId: null,
    stableId: null,
    nearestAncestorId: null,
    ...overrides
  };
}
test("resolveCollisions: single element no collision, pass through unchanged", () => {
  const out = (0, import_logicalNameResolver.resolveCollisions)([input({ initialName: "save_btn" })]);
  assertEqual(out, ["save_btn"]);
});
test("resolveCollisions: testId wins when element has one (slugged)", () => {
  const out = (0, import_logicalNameResolver.resolveCollisions)([
    input({ initialName: "save_btn", testId: "save-action" }),
    input({ initialName: "save_btn", testId: "confirm-action" })
  ]);
  assertEqual(out, ["save_action_save_btn", "confirm_action_save_btn"]);
});
test("resolveCollisions: stableId used when testId absent (slugged)", () => {
  const out = (0, import_logicalNameResolver.resolveCollisions)([
    input({ initialName: "save_btn", stableId: "top-save" }),
    input({ initialName: "save_btn", stableId: "bottom-save" })
  ]);
  assertEqual(out, ["top_save_save_btn", "bottom_save_save_btn"]);
});
test("resolveCollisions: nearestAncestorId used when self has no id (slugged)", () => {
  const out = (0, import_logicalNameResolver.resolveCollisions)([
    input({ initialName: "save_btn", nearestAncestorId: "form-a" }),
    input({ initialName: "save_btn", nearestAncestorId: "form-b" })
  ]);
  assertEqual(out, ["form_a_save_btn", "form_b_save_btn"]);
});
test("resolveCollisions: position-hinted fallback when nothing disambiguates", () => {
  const out = (0, import_logicalNameResolver.resolveCollisions)([
    input({ initialName: "save_btn", sectionName: "main" }),
    input({ initialName: "save_btn", sectionName: "main" }),
    input({ initialName: "save_btn", sectionName: "main" })
  ]);
  assertEqual(out, ["main_save_btn_1", "main_save_btn_2", "main_save_btn_3"]);
});
test("resolveCollisions: duplicate testIds fall through to position-hinted fallback", () => {
  const out = (0, import_logicalNameResolver.resolveCollisions)([
    input({ initialName: "save_btn", testId: "save", sectionName: "main" }),
    input({ initialName: "save_btn", testId: "save", sectionName: "main" })
  ]);
  assertEqual(out, ["main_save_btn_1", "main_save_btn_2"]);
});
test("resolveCollisions: ancestor id with spaces and caps is slugged", () => {
  const out = (0, import_logicalNameResolver.resolveCollisions)([
    input({
      initialName: "view_details_btn",
      sectionName: "page",
      nearestAncestorId: "order-detail-product-Bose QuietComfort 35 II"
    }),
    input({
      initialName: "view_details_btn",
      sectionName: "page",
      nearestAncestorId: "order-detail-product-Apple iWatch"
    })
  ]);
  assertEqual(out, [
    "order_detail_product_bose_quietcomfort_35_ii_view_details_btn",
    "order_detail_product_apple_iwatch_view_details_btn"
  ]);
});
test("resolveCollisions: testId with capitals is slugged", () => {
  const out = (0, import_logicalNameResolver.resolveCollisions)([
    input({ initialName: "save_btn", testId: "MainSaveBtn" }),
    input({ initialName: "save_btn", testId: "FooterSaveBtn" })
  ]);
  assertEqual(out, ["mainsavebtn_save_btn", "footersavebtn_save_btn"]);
});
test("resolveCollisions: prefix that slugs to empty falls through tier", () => {
  const out = (0, import_logicalNameResolver.resolveCollisions)([
    input({ initialName: "save_btn", sectionName: "main", nearestAncestorId: "!!!" }),
    input({ initialName: "save_btn", sectionName: "main", nearestAncestorId: "---" })
  ]);
  assertEqual(out, ["main_save_btn_1", "main_save_btn_2"]);
});
test("resolveCollisions: ladder falls through when testId slugs to empty", () => {
  const out = (0, import_logicalNameResolver.resolveCollisions)([
    input({ initialName: "save_btn", testId: "!!!", stableId: "top-save" }),
    input({ initialName: "save_btn", testId: "???", stableId: "bottom-save" })
  ]);
  assertEqual(out, ["top_save_save_btn", "bottom_save_save_btn"]);
});
test("resolveCollisions: CJK ancestor id preserved after slugging", () => {
  const out = (0, import_logicalNameResolver.resolveCollisions)([
    input({ initialName: "edit_btn", nearestAncestorId: "\u8CC7\u7523\u7BA1\u7406_\u30D1\u30CD\u30EB" }),
    input({ initialName: "edit_btn", nearestAncestorId: "\u5C65\u6B74\u60C5\u5831_\u30D1\u30CD\u30EB" })
  ]);
  assertEqual(out, ["\u8CC7\u7523\u7BA1\u7406_\u30D1\u30CD\u30EB_edit_btn", "\u5C65\u6B74\u60C5\u5831_\u30D1\u30CD\u30EB_edit_btn"]);
});
test("resolveCollisions: no cross-section collision \u2014 same initialName in different sections", () => {
  const out = (0, import_logicalNameResolver.resolveCollisions)([
    input({ initialName: "save_btn", sectionName: "header" }),
    input({ initialName: "save_btn", sectionName: "footer" })
  ]);
  assertEqual(out, ["save_btn", "save_btn"]);
});
test("resolveCollisions: mixed group \u2014 one with testId, one without", () => {
  const out = (0, import_logicalNameResolver.resolveCollisions)([
    input({ initialName: "save_btn", testId: "save-top" }),
    input({ initialName: "save_btn" })
  ]);
  assertEqual(out, ["main_save_btn_1", "main_save_btn_2"]);
});
test("resolveCollisions: preserves document order in position hints", () => {
  const out = (0, import_logicalNameResolver.resolveCollisions)([
    input({ initialName: "item", sectionName: "list" }),
    input({ initialName: "item", sectionName: "list" }),
    input({ initialName: "item", sectionName: "list" })
  ]);
  assertEqual(out, ["list_item_1", "list_item_2", "list_item_3"]);
});
test("resolveCollisions: multi-signal precedence \u2014 testId beats stableId beats nearestAncestorId", () => {
  const out = (0, import_logicalNameResolver.resolveCollisions)([
    input({ initialName: "save_btn", testId: "save-all", stableId: "id-a", nearestAncestorId: "anc-a" }),
    input({ initialName: "save_btn", testId: null, stableId: "id-b", nearestAncestorId: "anc-b" })
  ]);
  assertEqual(out, ["save_all_save_btn", "id_b_save_btn"]);
});
test("resolveCollisions: multiple simultaneous collision groups in one input", () => {
  const out = (0, import_logicalNameResolver.resolveCollisions)([
    input({ initialName: "save_btn", testId: "save-a", sectionName: "main" }),
    input({ initialName: "cancel_btn", testId: "cancel-a", sectionName: "main" }),
    input({ initialName: "save_btn", testId: "save-b", sectionName: "main" }),
    input({ initialName: "cancel_btn", testId: "cancel-b", sectionName: "main" })
  ]);
  assertEqual(out, [
    "save_a_save_btn",
    "cancel_a_cancel_btn",
    "save_b_save_btn",
    "cancel_b_cancel_btn"
  ]);
});
test("resolveCollisions: mixed collision and non-collision elements in one input", () => {
  const out = (0, import_logicalNameResolver.resolveCollisions)([
    input({ initialName: "unique_btn", testId: "u", sectionName: "main" }),
    input({ initialName: "save_btn", testId: "a", sectionName: "main" }),
    input({ initialName: "save_btn", testId: "b", sectionName: "main" }),
    input({ initialName: "another_btn", testId: "x", sectionName: "main" })
  ]);
  assertEqual(out, [
    "unique_btn",
    "a_save_btn",
    "b_save_btn",
    "another_btn"
  ]);
});
let failed = 0;
for (const { name, run } of cases) {
  try {
    run();
    console.log("  \u2713", name);
  } catch (e) {
    failed++;
    console.log("  \u2717", name);
    console.log("     ", e.message);
  }
}
if (failed > 0) {
  console.log(`
${failed}/${cases.length} failed`);
  process.exit(1);
}
console.log(`
${cases.length} passed`);
