import { describe, expect, it } from "vitest"; import { injectScriptsAtHeadStart, injectScriptsIntoHtml, parseHTMLContent, stripEmbeddedRuntimeScripts, } from "./htmlDocument.js"; describe("htmlDocument helpers", () => { it("wraps fragments before parsing", () => { const doc = parseHTMLContent(""); expect(doc.body.querySelector("template")?.innerHTML).toContain("hello"); }); it("strips every known embedded HyperFrames runtime marker", () => { const html = ` `; const stripped = stripEmbeddedRuntimeScripts(html); expect(stripped).not.toContain("hyperframe.runtime.iife.js"); expect(stripped).not.toContain("hyperframes-runtime.modular.inline.js"); expect(stripped).not.toContain("hyperframe-runtime.modular-runtime.inline.js"); expect(stripped).not.toContain("data-hyperframes-preview-runtime"); expect(stripped).not.toContain("window.__playerReady"); expect(stripped).toContain("window.authored = true"); }); it("does not treat non-script tags as scripts when stripping runtimes", () => { const html = "window.__playerReady = true;"; expect(stripEmbeddedRuntimeScripts(html)).toBe(html); }); it("injects head and body scripts without replacement-token interpolation", () => { const html = ""; const injected = injectScriptsIntoHtml(html, ["window.x = '$&';"], ["window.y = '$&';"]); expect(injected).toContain("\n"); expect(injected).toContain("\n"); }); it("injects early head scripts before authored head scripts", () => { const html = ''; const injected = injectScriptsAtHeadStart(html, ["window.early = true;"]); expect(injected.indexOf("window.early = true")).toBeLessThan(injected.indexOf('id="authored"')); }); it("escapes inline scripts so authored script text cannot break out of the wrapper tag", () => { const html = ""; const injected = injectScriptsIntoHtml( html, ['window.payload = "";'], ["window.comment = '