import gsap from "gsap"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { initSandboxRuntimeModular } from "./init"; import { RUNTIME_FILLER } from "./protocol"; import type { RuntimeTimelineLike } from "./types"; import { createMockTimeline, installImmediateAnimationFrame, resetRuntimeFixtureDom, stubDuration, } from "./runtimeSeekFixture.test-helpers"; // Pins what the running runtime reports as a composition's length and size. describe("runtime composition length and size", () => { const originalRequestAnimationFrame = window.requestAnimationFrame; const originalCancelAnimationFrame = window.cancelAnimationFrame; beforeEach(() => { resetRuntimeFixtureDom(); installImmediateAnimationFrame(); }); afterEach(() => { window.__hfRuntimeTeardown?.(); window.requestAnimationFrame = originalRequestAnimationFrame; window.cancelAnimationFrame = originalCancelAnimationFrame; vi.restoreAllMocks(); delete window.__player; delete window.__playerReady; delete window.__hf; }); const lengthOf = ( html: string, timelines: Record, durations: Record = {}, ): number | undefined => { document.body.innerHTML = html; for (const [id, seconds] of Object.entries(durations)) { stubDuration(document.getElementById(id) as HTMLMediaElement, seconds); } window.__timelines = timelines; initSandboxRuntimeModular(); return window.__player?.getDuration(); }; it("cuts a longer timeline at the root's declared length", () => { const html = `
`; expect(lengthOf(html, { main: createMockTimeline(10) })).toBe(4); }); it("re-pads a timeline that survives a rebind to the root's new length, as a fresh load does", () => { // GSAP's ticker re-enters an animation frame that runs at once. window.requestAnimationFrame = originalRequestAnimationFrame; document.body.innerHTML = `
`; const timeline = gsap.timeline({ paused: true }); timeline.to("#a", { x: 1, duration: 1 }, 0); window.__timelines = { main: timeline as unknown as RuntimeTimelineLike }; initSandboxRuntimeModular(); const fillers = () => timeline .getChildren(false, true, false) .filter((child) => child.data === RUNTIME_FILLER) .map((child) => child.startTime()); expect(fillers()).toEqual([6]); document.getElementById("a")!.parentElement!.setAttribute("data-duration", "9"); window.__hfForceTimelineRebind?.(); expect(fillers()).toEqual([9]); expect(window.__player?.getDuration()).toBe(9); }); it("takes the timeline's length when the root declares none", () => { const html = `
`; expect(lengthOf(html, { main: createMockTimeline(6) })).toBe(6); }); it("extends to a nested video's end at its absolute start", () => { const html = `
` + `
` + `
`; expect(lengthOf(html, { main: createMockTimeline(2) }, { v: 10 })).toBe(8); }); it("extends to a sub-composition's declared end", () => { const html = `
` + `
`; expect(lengthOf(html, { main: createMockTimeline(2) })).toBe(6); }); it("stops at the sub-composition's end when a tween repeats forever", () => { const html = `
` + `
`; // GSAP's length for a timeline holding a `repeat: -1` tween. expect(lengthOf(html, { main: createMockTimeline(1e10 + 2) })).toBe(6); }); it("derives the length from the clips when nothing else gives one", () => { const html = `
` + `
`; expect(lengthOf(html, {})).toBe(4); expect(window.__hf?.durationSource).toEqual({ source: "derived", seconds: 4, pendingClips: 0 }); }); it("reports 0 while a clip's length is pending", () => { const html = `
` + `
`; expect(lengthOf(html, {})).toBe(0); expect(window.__hf?.durationSource).toEqual({ source: "unresolved", seconds: null, pendingClips: 1, }); }); it("ignores a timeline shorter than a frame and keeps a tiny derived clip", () => { const html = `
` + `
`; expect(lengthOf(html, { main: createMockTimeline(0.01) })).toBe(0.01); }); it("reads the data-root composition, not the first one in the document", () => { const html = `
` + `
`; expect(lengthOf(html, { main: createMockTimeline(10) })).toBe(4); }); it("reports the root's size to the host", () => { const sizes: string[] = []; vi.spyOn(window.parent, "postMessage").mockImplementation((message: unknown) => { const m = message as { type?: string; width?: number; height?: number }; if (m?.type === "stage-size") sizes.push(`${m.width}x${m.height}`); }); const html = `
` + `
`; lengthOf(html, { main: createMockTimeline(4) }); expect(new Set(sizes)).toEqual(new Set(["1280x720"])); }); });