// fallow-ignore-file code-duplication // @vitest-environment happy-dom import { patchRuntimeTweenInPlace } from "../hooks/gsapRuntimePatch"; import { afterEach, describe, it, expect, vi } from "vitest"; import { GSAP_PLUGIN_LOAD_ATTR, NO_PATH_TWEEN_META } from "@hyperframes/core/gsap-cdn"; import { readNestedFiles, applySoftReload, applySoftReloadFinalization, ensureMotionPathPluginLoaded, } from "./gsapSoftReload"; const SCRIPT_TEXT = ` window.__timelines = window.__timelines || {}; const tl = gsap.timeline({ paused: true }); tl.to("#box", { opacity: 0.8 }); window.__timelines["root"] = tl; `; const MOTION_PATH_SCRIPT_TEXT = ` window.__timelines = window.__timelines || {}; const tl = gsap.timeline({ paused: true }); tl.to("#box", { motionPath: { path: [{ x: 0, y: 0 }, { x: 100, y: 50 }] } }); window.__timelines["root"] = tl; `; const PAGE = "http://localhost:3000/preview"; /** The page's own GSAP file, which the plugin loads beside. */ const pageGsap = () => Object.assign(document.createElement("script"), { src: "vendor/gsap.min.js" }); const pageScripts = (sel: string, inline: Element[] = []) => sel === "script:not([src])" ? inline : sel.startsWith("script[src]") ? [pageGsap()] : []; function buildMockIframe(overrides: Record = {}) { const scriptEl = document.createElement("script"); scriptEl.textContent = 'const tl = gsap.timeline({ paused: true }); tl.to("#box", { opacity: 0.5 });'; const container = document.createElement("div"); container.appendChild(scriptEl); const mockTimeline = { kill: vi.fn(), pause: vi.fn() }; const contentWindow = { gsap: { timeline: vi.fn() }, __hfForceTimelineRebind: vi.fn(), __timelines: { root: mockTimeline } as Record, __player: { getTime: () => 2.0, seek: vi.fn() }, __hfStudioManualEditsApply: vi.fn(), __hfSuppressSceneMutations: undefined as undefined | ((fn: () => T) => T), ...overrides, }; // Intercept appendChild: when a `, }); expect(target.style.width).toBe(""); expect(target.style.left).toBe("10px"); }); it("wraps execution in __hfSuppressSceneMutations when available", () => { let suppressionCalled = false; const { iframe } = buildMockIframe({ __hfSuppressSceneMutations: (fn: () => T): T => { suppressionCalled = true; return fn(); }, }); const result = applySoftReload(iframe, SCRIPT_TEXT); expect(result).toBe("applied"); expect(suppressionCalled).toBe(true); }); it('returns "applied" when the re-run re-registers the script\'s expected key', () => { // SCRIPT_TEXT registers __timelines["root"]; buildMockIframe's appendChild // shim repopulates `root` on execution. The hardened verify checks the // expected target key is present (not merely "some key"), so a correct re-run // reliably reports "applied" — it doesn't spuriously hit the transient window. const { iframe, contentWindow } = buildMockIframe(); expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe("applied"); expect(contentWindow.__timelines.root).toBeDefined(); }); it('returns "verify-failed" (transient) when the re-run leaves the key empty', () => { // No appendChild shim repopulation: the body container has no shim, so the // re-run kills __timelines["root"] and the new script doesn't re-register it. // That is the TRANSIENT post-run window — surfaced as "verify-failed" so // callers know NOT to escalate (the live gsap.set already shows the value). const scriptEl = document.createElement("script"); scriptEl.textContent = 'window.__timelines["root"] = gsap.timeline();'; const container = document.createElement("div"); // no appendChild shim container.appendChild(scriptEl); const { iframe } = buildMockIframe(); (iframe as unknown as { contentDocument: unknown }).contentDocument = { querySelectorAll: (sel: string) => (sel === "script:not([src])" ? [scriptEl] : []), createElement: (tag: string) => document.createElement(tag), body: container, head: document.createElement("div"), }; expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe("verify-failed"); }); it("editing composition A leaves composition B's timeline intact (scoped kill)", () => { // Two comps live side by side; the soft reload only re-runs comp "root". // Comp "subscene" must survive untouched — the regression the full remount // (re-inline) used to cause. const subsceneTimeline = { kill: vi.fn(), pause: vi.fn() }; const { iframe, contentWindow, mockTimeline } = buildMockIframe({ __timelines: { root: { kill: vi.fn(), pause: vi.fn() }, subscene: subsceneTimeline, } as Record; pause: ReturnType }>, }); void mockTimeline; expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe("applied"); // Comp B was never killed and is still registered. expect(subsceneTimeline.kill).not.toHaveBeenCalled(); expect(contentWindow.__timelines.subscene).toBe(subsceneTimeline); }); it("runs synchronously (no async plugin load) when MotionPathPlugin is already present", () => { // The preview bootstrap pre-loads MotionPathPlugin, so win.MotionPathPlugin // is set before any motion-path edit. The soft reload must then execute the // script inline — no CDN `; for (const id of held) Object.assign(doc.getElementById(id)!, { _gsap: {} }); const set = vi.fn(); const contentWindow = { gsap: { timeline: vi.fn(), set }, __hfForceTimelineRebind: vi.fn(), __timelines: { root: { kill: vi.fn(), getChildren: () => [] } } as Record, __player: { getTime: () => 0, seek: vi.fn() }, }; const iframe = { contentWindow, contentDocument: doc } as unknown as HTMLIFrameElement; const cleared = () => (set.mock.calls[0]?.[0] as Element[] | undefined)?.map((el) => el.id); return { doc, iframe, cleared }; } it("resets an element a standalone gsap.set holds even when no inline transform is left", () => { const { iframe, cleared } = reloadRoot( `
`, ["held"], ); applySoftReload(iframe, SCRIPT_TEXT); expect(cleared()).toEqual(["held"]); }); it("leaves what a nested composition's script holds alone, but resets that composition's host", () => { const { doc, iframe, cleared } = reloadRoot( `
` + `
`, ["host", "nested"], ); applySoftReload(iframe, SCRIPT_TEXT); expect(cleared()).toEqual(["host"]); expect(doc.getElementById("nested")!.style.transform).toBe("translate(90px, 60px)"); }); it("reloads in full when the re-run builds its DOM a second time", () => { const { doc, iframe } = reloadRoot(`
`, []); const append = doc.body.appendChild.bind(doc.body); // What a script's createElement or template clone does each time it runs. doc.body.appendChild = (node: T): T => { doc.getElementById("root")!.appendChild(doc.createElement("div")); return append(node); }; expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe("cannot-soft-reload"); }); }); describe("a gsap.set a live patch applied", () => { it("is cleared by the next soft reload once the new script no longer sets it, and only once", () => { const markup = `
`; const doc = document.implementation.createHTMLDocument(""); doc.body.innerHTML = `${markup}`; const set = (target: HTMLElement | HTMLElement[], vars: Record) => { for (const el of [target].flat()) { if (vars.clearProps) el.removeAttribute("style"); else for (const [k, v] of Object.entries(vars)) el.style.setProperty(k, `${v}px`); } }; const iframe = { contentDocument: doc, contentWindow: { gsap: { timeline: () => {}, set }, __hfForceTimelineRebind: () => {}, __timelines: {}, __player: { getTime: () => 0, seek: () => {} }, }, } as unknown as HTMLIFrameElement; const el = doc.getElementById("a")!; const reload = () => { const script = `window.__timelines["root"]=gsap.timeline();gsap.set("#a",{width:300});`; applySoftReload(iframe, script, { authoredHtml: `${markup}` }); }; // W 300, then H 200, then a commit that removes height from the set. patchRuntimeTweenInPlace(iframe, "#a", { kind: "global-set", props: { width: 300 } }); patchRuntimeTweenInPlace(iframe, "#a", { kind: "global-set", props: { height: 200 } }); reload(); expect([el.style.height, el.style.left]).toEqual(["", "10px"]); el.style.height = "50px"; reload(); expect(el.style.height).toBe("50px"); }); });