/** * Page Fragments Tests * * Tests the fragment collector for: * - Filtering contributions by placement * - Deduplication by key and src * - HTML rendering of script and raw HTML fragments */ import { describe, it, expect } from "vitest"; import { resolveFragments, renderFragments } from "../../../src/page/fragments.js"; import type { PageFragmentContribution } from "../../../src/plugins/types.js"; describe("resolveFragments", () => { it("filters by placement", () => { const contributions: PageFragmentContribution[] = [ { kind: "html", placement: "head", html: "" }, { kind: "html", placement: "body:end", html: "
footer
" }, { kind: "html", placement: "head", html: "" }, ]; const result = resolveFragments(contributions, "head"); expect(result).toHaveLength(2); expect(result[0]!.kind).toBe("html"); expect((result[0] as { html: string }).html).toBe(""); expect((result[1] as { html: string }).html).toBe(""); }); it("dedupes by key + placement", () => { const contributions: PageFragmentContribution[] = [ { kind: "html", placement: "head", html: "", key: "my-styles" }, { kind: "html", placement: "head", html: "", key: "my-styles" }, ]; const result = resolveFragments(contributions, "head"); expect(result).toHaveLength(1); expect((result[0] as { html: string }).html).toBe(""); }); it("dedupes external scripts by src", () => { const contributions: PageFragmentContribution[] = [ { kind: "external-script", placement: "body:end", src: "https://cdn.example.com/lib.js", async: true, }, { kind: "external-script", placement: "body:end", src: "https://cdn.example.com/lib.js", defer: true, }, ]; const result = resolveFragments(contributions, "body:end"); expect(result).toHaveLength(1); expect((result[0] as { async?: boolean }).async).toBe(true); }); it("allows different placements of same key", () => { const contributions: PageFragmentContribution[] = [ { kind: "html", placement: "head", html: "", key: "seo" }, { kind: "html", placement: "body:end", html: "