mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-20 20:38:34 +00:00
* perf(studio): move the live tweens in place after a timeline move instead of re-running the script * fix(studio): pair a live retime with the script's own tweens, not the nested composition timelines * fix(studio): leave the runtime's filler tweens out of a live retime's pairing * refactor(studio): move the live tweens in their own step of the timing sync * perf(studio): read nested files only when a timing edit re-runs the script * fix(studio): move live tweens only when the live timeline is exactly what the script parser read * fix(core): a rebound timeline is padded and timed like a fresh load * refactor(studio): decide unknown tween lengths in the plan and check order swaps only on a move * fix(studio): re-run the script whenever moving the live tweens could differ from a fresh load * fix(studio): keep trims and reformatted scripts on the fast path, re-run for delays and hosts * fix(studio): re-run for tweens inside a sub-composition host or that change order on an element * fix(studio): ignore layout inside raw script values when matching a reformatted preview script * refactor(studio): check each moved element once and scan nested timelines only for moved targets * docs(studio): keep the live-retime comments within the merge hook's rules
141 lines
5.6 KiB
TypeScript
141 lines
5.6 KiB
TypeScript
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<string, RuntimeTimelineLike>,
|
|
durations: Record<string, number> = {},
|
|
): 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 = `<div data-composition-id="main" data-root="true" data-duration="4"></div>`;
|
|
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 = `<div data-composition-id="main" data-root="true" data-duration="6"><div id="a"></div></div>`;
|
|
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 = `<div data-composition-id="main" data-root="true"></div>`;
|
|
expect(lengthOf(html, { main: createMockTimeline(6) })).toBe(6);
|
|
});
|
|
|
|
it("extends to a nested video's end at its absolute start", () => {
|
|
const html =
|
|
`<div data-composition-id="main" data-root="true">` +
|
|
`<div data-composition-id="scene" data-start="3">` +
|
|
`<video id="v" data-start="1" data-duration="4"></video></div></div>`;
|
|
expect(lengthOf(html, { main: createMockTimeline(2) }, { v: 10 })).toBe(8);
|
|
});
|
|
|
|
it("extends to a sub-composition's declared end", () => {
|
|
const html =
|
|
`<div data-composition-id="main" data-root="true">` +
|
|
`<div data-composition-id="scene" data-start="1" data-duration="5"></div></div>`;
|
|
expect(lengthOf(html, { main: createMockTimeline(2) })).toBe(6);
|
|
});
|
|
|
|
it("derives the length from the clips when nothing else gives one", () => {
|
|
const html =
|
|
`<div data-composition-id="main" data-root="true">` +
|
|
`<div data-start="1" data-duration="3"></div></div>`;
|
|
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 =
|
|
`<div data-composition-id="main" data-root="true">` +
|
|
`<div data-start="0" data-duration="3"></div><video data-start="0"></video></div>`;
|
|
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 =
|
|
`<div data-composition-id="main" data-root="true">` +
|
|
`<div data-start="0" data-duration="0.01"></div></div>`;
|
|
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 =
|
|
`<div data-composition-id="card" data-duration="9"></div>` +
|
|
`<div data-composition-id="main" data-root="true" data-duration="4"></div>`;
|
|
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 =
|
|
`<div data-composition-id="card" data-width="800" data-height="600"></div>` +
|
|
`<div data-composition-id="main" data-root="true" data-duration="4" data-width="1280.9" data-height="720"></div>`;
|
|
lengthOf(html, { main: createMockTimeline(4) });
|
|
expect(new Set(sizes)).toEqual(new Set(["1280x720"]));
|
|
});
|
|
});
|