import { test, expect } from "bun:test";
import { extractAnchors } from "./b32_anchor_extract.ts";
test("extracts h2 with explicit id", () => {
const html = `
Getting started
`;
expect(extractAnchors(html)).toEqual([
{ level: 2, text: "Getting started", id: "getting-started" },
]);
});
test("extracts h3 with explicit id", () => {
const html = `Why
`;
expect(extractAnchors(html)).toEqual([
{ level: 3, text: "Why", id: "why" },
]);
});
test("ignores h1 and h4+", () => {
const html = `T
A
B
`;
const anchors = extractAnchors(html);
expect(anchors.map((a) => a.id)).toEqual(["a"]);
});
test("slugifies when id attribute is missing", () => {
const html = `What this number does *not* measure
`;
const anchors = extractAnchors(html);
expect(anchors[0]?.id).toBe("what-this-number-does-not-measure");
});
test("strips inline tags from text and id source", () => {
const html = `red: phase
`;
const anchors = extractAnchors(html);
expect(anchors[0]?.text).toBe("red: phase");
expect(anchors[0]?.id).toBe("red-phase");
});
test("returns multiple anchors in document order", () => {
const html = `One
x
Two
Three
`;
const anchors = extractAnchors(html);
expect(anchors.map((a) => `${a.level}:${a.id}`)).toEqual([
"2:one",
"3:two",
"2:three",
]);
});
test("skips empty headings", () => {
const html = `Real
`;
expect(extractAnchors(html).length).toBe(1);
});
test("handles HTML entities in text", () => {
const html = `Tom & Jerry
`;
const anchors = extractAnchors(html);
expect(anchors[0]?.text).toBe("Tom & Jerry");
expect(anchors[0]?.id).toBe("tom-jerry");
});