import assert from "node:assert/strict"; import { it } from "node:test"; import { runInNewContext } from "node:vm"; import { parseHTMLContent } from "@hyperframes/core/compiler/html-document"; import { PREVIEW_INLINE_TEXT } from "../../src/studio/engine/preview-inline-text.ts"; import { PREVIEW_SOURCE_IDENTITY } from "../../src/studio/engine/preview-bridge.ts"; function pick(html: string, variables?: Record, scoped?: Record) { const document = parseHTMLContent(html); const el = document.getElementById("text")!; document.elementFromPoint = () => el; return runInNewContext(PREVIEW_SOURCE_IDENTITY + PREVIEW_INLINE_TEXT + ';editInlineText({phase:"pick",editId:"1",x:0.5,y:0.5})', { document, window: { __hfVariables: variables, __hfVariablesByComp: scoped }, HTMLElement: document.defaultView!.HTMLElement, innerWidth: 100, innerHeight: 100, sourceVersions: { "index.html": "sha" }, describe: (node: Element) => ({ sourceFile: "index.html", elementId: node.id }), }); } const declaration = "data-composition-variables='" + JSON.stringify([{ id: "message", type: "string", default: "Root" }]) + "'"; const label = 'Text'; it("uses the last effective declaration, while a transient render override cannot become a file edit", () => { const html = '
' + label + ''; assert.equal(pick(html)?.binding.elementId, "later"); assert.equal(pick(html, { message: "Preview only" }), null); }); it("targets the exact instance for a scoped value and never falls through to an unrelated global", () => { const html = '
' + label + '
'; assert.equal(pick(html, { message: "global override" }, { "instance-b": { message: "Scoped" } })?.binding.attribute, "data-variable-values"); assert.equal(pick(html, {}, { "instance-b": { message: "Scoped" } })?.binding.elementId, "second"); assert.equal(pick(html, {}, { "instance-b": { different: "unrelated" } }), null); }); it("leaves ordinary rich text editable and refuses to overwrite child layers with a bound scalar", () => { assert.equal(pick('

A word

')?.elementId, "text"); assert.equal(pick('

A word

'), null); });