import { extractHeadings, markdownSourceFromString, parseMarkdownTree } from "@src/lib/markdown";
import { describe, expect, it } from "vitest";

describe("extractHeadings", () => {
    it("finds headings nested inside blockquotes", () => {
        const source = "> ## Inside quote\n\n## Top";
        const tree = parseMarkdownTree(source);
        const headings = extractHeadings(tree, markdownSourceFromString(source));

        expect(headings).toHaveLength(2);
        expect(headings.map((h) => h.text)).toEqual(["Inside quote", "Top"]);
    });

    it("finds headings between details HTML blocks", () => {
        const source = `<details>
<summary>Toggle</summary>

### Inside details

</details>`;
        const tree = parseMarkdownTree(source);
        const headings = extractHeadings(tree, markdownSourceFromString(source), {
            depths: [2, 3],
        });

        expect(headings).toHaveLength(1);
        expect(headings[0]?.text).toBe("Inside details");
        expect(headings[0]?.depth).toBe(3);
    });

    it("strips inline emphasis marks from heading text", () => {
        const source = "## Hello **world**";
        const tree = parseMarkdownTree(source);
        const headings = extractHeadings(tree, markdownSourceFromString(source));

        expect(headings).toHaveLength(1);
        expect(headings[0]?.text).toBe("Hello world");
        expect(headings[0]?.id).toBe("hello-world");
    });
});
