"use strict";
var import_sectionGrouper = require("./sectionGrouper");
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 el(overrides) {
  return {
    id: "test-el-" + Math.random(),
    ancestry: { landmarkAncestor: null, sectioningAncestor: null, headingAncestry: [] },
    ...overrides
  };
}
test("resolveSections: landmark beats sectioning beats heading", () => {
  const inputs = [
    el({
      id: "a",
      ancestry: {
        landmarkAncestor: { landmark: "main", nameHint: "main", anchorKey: "ak1" },
        sectioningAncestor: { tagName: "section", accessibleName: "Inner", anchorKey: "ak2" },
        headingAncestry: [{ level: 2, text: "Deepest", anchorKey: "ak3" }]
      }
    })
  ];
  const out = (0, import_sectionGrouper.resolveSections)(inputs);
  assertEqual(out.assignments, { a: "main" });
});
test("resolveSections: sectioning without name falls through to implicit heading", () => {
  const inputs = [
    el({
      id: "a",
      ancestry: {
        landmarkAncestor: null,
        sectioningAncestor: { tagName: "section", accessibleName: null, anchorKey: "ak1" },
        headingAncestry: [{ level: 2, text: "Orders", anchorKey: "ak2" }]
      }
    })
  ];
  const out = (0, import_sectionGrouper.resolveSections)(inputs);
  assertEqual(out.assignments, { a: "orders" });
});
test("resolveSections: G1 drops empty-interactive implicit heading", () => {
  const inputs = [
    el({
      id: "a",
      ancestry: {
        landmarkAncestor: { landmark: "main", nameHint: "main", anchorKey: "main-ak" },
        sectioningAncestor: null,
        headingAncestry: [{ level: 2, text: "Decorative", anchorKey: "ak1" }]
      }
    })
  ];
  const out = (0, import_sectionGrouper.resolveSections)(inputs);
  assertEqual(out.assignments, { a: "main" });
  assertEqual(out.sections.find((s) => s.name === "decorative"), void 0);
});
test("resolveSections: G2 (leaf-only) drops ancestor heading when descendant has elements", () => {
  const inputs = [
    el({
      id: "a",
      ancestry: {
        landmarkAncestor: null,
        sectioningAncestor: null,
        headingAncestry: [
          { level: 1, text: "Dashboard", anchorKey: "dash-ak" },
          { level: 2, text: "Orders", anchorKey: "orders-ak" }
        ]
      }
    })
  ];
  const out = (0, import_sectionGrouper.resolveSections)(inputs);
  assertEqual(out.assignments, { a: "orders" });
});
test("resolveSections: G2 keeps two sibling headings at same level", () => {
  const inputs = [
    el({
      id: "a",
      ancestry: {
        landmarkAncestor: null,
        sectioningAncestor: null,
        headingAncestry: [{ level: 2, text: "Orders", anchorKey: "orders-ak" }]
      }
    }),
    el({
      id: "b",
      ancestry: {
        landmarkAncestor: null,
        sectioningAncestor: null,
        headingAncestry: [{ level: 2, text: "Products", anchorKey: "products-ak" }]
      }
    })
  ];
  const out = (0, import_sectionGrouper.resolveSections)(inputs);
  assertEqual(out.assignments, { a: "orders", b: "products" });
});
test("resolveSections: G3 rejects junk slug \u2014 fall through to implicit heading", () => {
  const inputs = [
    el({
      id: "a",
      ancestry: {
        landmarkAncestor: null,
        sectioningAncestor: { tagName: "section", accessibleName: "Untitled", anchorKey: "sec-ak" },
        headingAncestry: [{ level: 2, text: "Orders", anchorKey: "orders-ak" }]
      }
    })
  ];
  const out = (0, import_sectionGrouper.resolveSections)(inputs);
  assertEqual(out.assignments, { a: "orders" });
});
test("resolveSections: G3 rejects all signals \u2192 page fallback", () => {
  const inputs = [
    el({
      id: "a",
      ancestry: {
        landmarkAncestor: null,
        sectioningAncestor: { tagName: "section", accessibleName: "Content", anchorKey: "sec-ak" },
        headingAncestry: [{ level: 2, text: "Overview", anchorKey: "ov-ak" }]
      }
    })
  ];
  const out = (0, import_sectionGrouper.resolveSections)(inputs);
  assertEqual(out.assignments, { a: "page" });
});
test("resolveSections: sectioning null-name and junk-name behave identically (fall through)", () => {
  const inputsNull = [
    el({
      id: "null-a",
      ancestry: {
        landmarkAncestor: null,
        sectioningAncestor: { tagName: "section", accessibleName: null, anchorKey: "s1" },
        headingAncestry: [{ level: 2, text: "Orders", anchorKey: "o1" }]
      }
    })
  ];
  const inputsJunk = [
    el({
      id: "junk-a",
      ancestry: {
        landmarkAncestor: null,
        sectioningAncestor: { tagName: "section", accessibleName: "Untitled", anchorKey: "s2" },
        headingAncestry: [{ level: 2, text: "Orders", anchorKey: "o2" }]
      }
    })
  ];
  const outNull = (0, import_sectionGrouper.resolveSections)(inputsNull);
  const outJunk = (0, import_sectionGrouper.resolveSections)(inputsJunk);
  assertEqual(outNull.assignments["null-a"], "orders");
  assertEqual(outJunk.assignments["junk-a"], "orders");
});
test("resolveSections: two headings same text different anchorKeys \u2192 separate sections", () => {
  const inputs = [
    el({
      id: "a",
      ancestry: {
        landmarkAncestor: null,
        sectioningAncestor: null,
        headingAncestry: [{ level: 2, text: "Orders", anchorKey: "orders-1" }]
      }
    }),
    el({
      id: "b",
      ancestry: {
        landmarkAncestor: null,
        sectioningAncestor: null,
        headingAncestry: [{ level: 2, text: "Orders", anchorKey: "orders-2" }]
      }
    })
  ];
  const out = (0, import_sectionGrouper.resolveSections)(inputs);
  assertEqual(out.assignments, { a: "orders", b: "orders_2" });
});
test("resolveSections: element in landmark with inner heading \u2014 landmark wins", () => {
  const inputs = [
    el({
      id: "a",
      ancestry: {
        landmarkAncestor: { landmark: "main", nameHint: "main", anchorKey: "main-ak" },
        sectioningAncestor: null,
        headingAncestry: [{ level: 3, text: "Filters", anchorKey: "h3-ak" }]
      }
    })
  ];
  const out = (0, import_sectionGrouper.resolveSections)(inputs);
  assertEqual(out.assignments, { a: "main" });
});
test("resolveSections: element in landmark+sectioning+heading \u2014 landmark wins (strongest)", () => {
  const inputs = [
    el({
      id: "a",
      ancestry: {
        landmarkAncestor: { landmark: "main", nameHint: "main", anchorKey: "main-ak" },
        sectioningAncestor: { tagName: "section", accessibleName: "Orders", anchorKey: "ord-ak" },
        headingAncestry: [{ level: 3, text: "Filters", anchorKey: "h3-ak" }]
      }
    })
  ];
  const out = (0, import_sectionGrouper.resolveSections)(inputs);
  assertEqual(out.assignments, { a: "main" });
});
test("resolveSections: G2 nested drop cascade \u2014 A > B > C, only C has elements", () => {
  const inputs = [
    el({
      id: "a",
      ancestry: {
        landmarkAncestor: null,
        sectioningAncestor: null,
        headingAncestry: [
          { level: 1, text: "Dashboard", anchorKey: "dash-ak" },
          { level: 2, text: "Orders", anchorKey: "orders-ak" },
          { level: 3, text: "Recent", anchorKey: "recent-ak" }
        ]
      }
    })
  ];
  const out = (0, import_sectionGrouper.resolveSections)(inputs);
  assertEqual(out.assignments, { a: "recent" });
  assertEqual(out.sections.find((s) => s.name === "dashboard"), void 0);
  assertEqual(out.sections.find((s) => s.name === "orders"), void 0);
});
test("resolveSections: G2 keeps ancestor heading that has its own elements", () => {
  const inputs = [
    el({
      id: "a",
      ancestry: {
        landmarkAncestor: null,
        sectioningAncestor: null,
        headingAncestry: [
          { level: 1, text: "Dashboard", anchorKey: "dash-ak" }
        ]
      }
    }),
    el({
      id: "b",
      ancestry: {
        landmarkAncestor: null,
        sectioningAncestor: null,
        headingAncestry: [
          { level: 1, text: "Dashboard", anchorKey: "dash-ak" },
          { level: 2, text: "Orders", anchorKey: "orders-ak" }
        ]
      }
    })
  ];
  const out = (0, import_sectionGrouper.resolveSections)(inputs);
  assertEqual(out.assignments, { a: "page", b: "orders" });
});
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`);
