import { describe, it, expect } from "vitest"; import { ensureHfIds, mintHfId } from "./hfIds.js"; import { parseHTML } from "linkedom"; function ids(html: string): string[] { const { document } = parseHTML(html); return Array.from(document.querySelectorAll("[data-hf-id]")).map( (e) => e.getAttribute("data-hf-id") as string, ); } // data-hf-id of the first element matching `selector`. function idOf(html: string, selector: string): string | null { const { document } = parseHTML(html); return document.querySelector(selector)?.getAttribute("data-hf-id") ?? null; } const doc = (body: string) => `${body}`; describe("ensureHfIds", () => { it("mints a hf- id on every editable element node in body", () => { const html = `

Hi

x
`; const out = ensureHfIds(html); for (const id of ids(out)) expect(id).toMatch(/^hf-[a-z0-9]{4}$/); // div, h1, img, span = 4 ids expect(ids(out)).toHaveLength(4); }); it("skips script/style/template/meta and head", () => { const html = `

keep

`; const out = ensureHfIds(html); // only the

gets an id expect(ids(out)).toHaveLength(1); expect(out).not.toContain("