import { describe, expect, it } from "vitest"; import { ensureRuntimeBeforeBodyScripts } from "./runtime-in-srcdoc.js"; const URL = "https://cdn.example/hyperframe.runtime.iife.js"; /** What a pasted component looks like: it reads its variables during parse. */ const COMPONENT_PAGE = `t
`; describe("ensureRuntimeBeforeBodyScripts", () => { it("puts the runtime ahead of the script that reads variables", () => { // The whole bug: the probe appended the runtime after load, so this guard // always took the empty branch and the component used its hardcoded // defaults. badge-pop with count 10 rendered 3. const out = ensureRuntimeBeforeBodyScripts(COMPONENT_PAGE, URL); expect(out).toContain(``); expect(out.indexOf(URL)).toBeLessThan(out.indexOf("getVariables")); }); it("puts it inside head, where an external script blocks the parser", () => { const out = ensureRuntimeBeforeBodyScripts(COMPONENT_PAGE, URL); // Landing after would not block body parsing in the same way. expect(out.indexOf(URL)).toBeGreaterThan(out.indexOf("")); expect(out.indexOf(URL)).toBeLessThan(out.indexOf("")); }); it("does not add a second copy when the page already links it", () => { const already = ``; expect(ensureRuntimeBeforeBodyScripts(already, URL)).toBe(already); }); it("leaves a CLI-rendered page alone, which inlines the runtime already", () => { // The engine inlines it and defines the global on the way in. A second // copy would re-initialise the runtime underneath a live composition. const rendered = ``; expect(ensureRuntimeBeforeBodyScripts(rendered, URL)).toBe(rendered); }); it("falls back to before when there is no head", () => { const out = ensureRuntimeBeforeBodyScripts("", URL); expect(out.indexOf(URL)).toBeLessThan(out.indexOf("")); }); it("handles a bare fragment by going first", () => { const out = ensureRuntimeBeforeBodyScripts("
hi
", URL); expect(out.startsWith(``)).toBe(true); }); it("is a no-op on empty input", () => { expect(ensureRuntimeBeforeBodyScripts("", URL)).toBe(""); }); it("survives a head tag carrying attributes", () => { const out = ensureRuntimeBeforeBodyScripts( ``, URL, ); expect(out.indexOf(URL)).toBeLessThan(out.indexOf("read()")); }); it("does not mistake a longer tag name for head", () => { const out = ensureRuntimeBeforeBodyScripts( `
`, URL, ); expect(out.indexOf(URL)).toBeLessThan(out.indexOf("")); expect(out.indexOf(URL)).toBeGreaterThan(out.indexOf("")); }); });