forked from heygen-com/hyperframes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhyperframes.test.ts
More file actions
304 lines (243 loc) · 10.5 KB
/
Copy pathhyperframes.test.ts
File metadata and controls
304 lines (243 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* @vitest-environment jsdom
*/
import { describe, it, expect } from "vitest";
import {
generateHyperframesHtml,
generateGsapTimelineScript,
generateHyperframesStyles,
} from "./hyperframes.js";
import { GSAP_CDN } from "../templates/constants.js";
import type { TimelineTextElement, TimelineMediaElement } from "../core.types";
function makeTextElement(overrides: Partial<TimelineTextElement> = {}): TimelineTextElement {
return {
id: "text-1",
type: "text",
name: "Title",
content: "Hello World",
startTime: 0,
duration: 5,
zIndex: 1,
...overrides,
};
}
function makeVideoElement(overrides: Partial<TimelineMediaElement> = {}): TimelineMediaElement {
return {
id: "vid-1",
type: "video",
name: "Background",
src: "video.mp4",
startTime: 0,
duration: 10,
zIndex: 0,
...overrides,
};
}
describe("generateHyperframesHtml", () => {
it("generates valid HTML with proper data attributes", () => {
const elements = [makeTextElement()];
const html = generateHyperframesHtml(elements, 5);
expect(html).toContain("<!DOCTYPE html>");
expect(html).toContain("data-composition-id=");
expect(html).toContain("data-composition-duration=");
expect(html).toContain('id="stage"');
expect(html).toContain('id="stage-zoom-container"');
});
it("includes element data attributes", () => {
const elements = [makeTextElement({ id: "my-text", startTime: 2, duration: 3 })];
const html = generateHyperframesHtml(elements, 5);
expect(html).toContain('id="my-text"');
expect(html).toContain('data-start="2"');
expect(html).toContain('data-end="5"');
expect(html).toContain('data-layer="1"');
});
it("includes GSAP CDN script tag when includeScripts is true", () => {
const elements = [makeTextElement()];
const html = generateHyperframesHtml(elements, 5, { includeScripts: true });
expect(html).toContain(`<script src="${GSAP_CDN}"></script>`);
});
it("does NOT include GSAP CDN by default", () => {
const elements = [makeTextElement()];
const html = generateHyperframesHtml(elements, 5);
expect(html).not.toContain(GSAP_CDN);
});
it("includes timeline script when includeScripts is true", () => {
const elements = [makeTextElement()];
const html = generateHyperframesHtml(elements, 5, { includeScripts: true });
expect(html).toContain("gsap.timeline({ paused: true })");
});
it("generates GSAP timeline with visibility animations when includeScripts is true", () => {
const elements = [makeTextElement({ id: "el-1", startTime: 1, duration: 3 })];
const html = generateHyperframesHtml(elements, 5, { includeScripts: true });
// Default animations include visibility bookends
expect(html).toContain('tl.set("#el-1"');
expect(html).toContain('visibility: "hidden"');
expect(html).toContain('visibility: "visible"');
});
it("sets resolution data attribute", () => {
const elements = [makeTextElement()];
const landscapeHtml = generateHyperframesHtml(elements, 5, { resolution: "landscape" });
expect(landscapeHtml).toContain('data-resolution="landscape"');
const portraitHtml = generateHyperframesHtml(elements, 5, { resolution: "portrait" });
expect(portraitHtml).toContain('data-resolution="portrait"');
});
it("generates video elements with proper tags", () => {
const elements = [makeVideoElement()];
const html = generateHyperframesHtml(elements, 10);
expect(html).toContain("<video");
expect(html).toContain('src="video.mp4"');
expect(html).toContain("playsinline");
});
it("generates text elements with content wrapper div", () => {
const elements = [makeTextElement({ content: "My Content" })];
const html = generateHyperframesHtml(elements, 5);
expect(html).toContain("<div>My Content</div>");
});
it("includes custom compositionId", () => {
const elements = [makeTextElement()];
const html = generateHyperframesHtml(elements, 5, { compositionId: "test-comp-123" });
expect(html).toContain('data-composition-id="test-comp-123"');
});
it("calculates total duration from elements if they exceed provided duration", () => {
const elements = [makeTextElement({ startTime: 0, duration: 15 })];
const html = generateHyperframesHtml(elements, 5);
expect(html).toContain('data-composition-duration="15"');
});
it("includes style tags when includeStyles is true", () => {
const elements = [makeTextElement()];
const html = generateHyperframesHtml(elements, 5, { includeStyles: true });
expect(html).toContain('<style data-hf-core="true">');
});
it("includes custom styles when provided", () => {
const elements = [makeTextElement()];
const html = generateHyperframesHtml(elements, 5, {
styles: ".custom { color: red; }",
includeStyles: true,
});
expect(html).toContain('<style data-hf-custom="true">');
expect(html).toContain(".custom { color: red; }");
});
it("serializes keyframes as data attributes", () => {
const elements = [makeTextElement({ id: "text-kf" })];
const keyframes = {
"text-kf": [
{ id: "kf1", time: 0, properties: { opacity: 0 } },
{ id: "kf2", time: 1, properties: { opacity: 1 } },
],
};
const html = generateHyperframesHtml(elements, 5, { keyframes });
expect(html).toContain("data-keyframes=");
expect(html).toContain("kf1");
expect(html).toContain("kf2");
});
it("serializes zoom keyframes on zoom container", () => {
const elements = [makeTextElement()];
const stageZoomKeyframes = [
{ id: "z1", time: 0, zoom: { scale: 1, focusX: 960, focusY: 540 } },
{ id: "z2", time: 5, zoom: { scale: 2, focusX: 400, focusY: 300 } },
];
const html = generateHyperframesHtml(elements, 10, { stageZoomKeyframes });
expect(html).toContain("data-zoom-keyframes=");
});
it("includes x, y, scale data attributes for non-default values", () => {
const elements = [makeVideoElement({ x: 100, y: 200, scale: 1.5, opacity: 0.8 })];
const html = generateHyperframesHtml(elements, 10);
expect(html).toContain('data-x="100"');
expect(html).toContain('data-y="200"');
expect(html).toContain('data-scale="1.5"');
expect(html).toContain('data-opacity="0.8"');
});
it("omits x, y, scale, opacity data attributes when at default values", () => {
const elements = [makeVideoElement({ x: 0, y: 0, scale: 1, opacity: 1 })];
const html = generateHyperframesHtml(elements, 10);
expect(html).not.toContain("data-x=");
expect(html).not.toContain("data-y=");
expect(html).not.toContain("data-scale=");
expect(html).not.toContain("data-opacity=");
});
});
describe("generateGsapTimelineScript", () => {
it("generates a timeline script with visibility animations", () => {
const elements = [makeTextElement({ id: "el1", startTime: 1, duration: 4 })];
const script = generateGsapTimelineScript(elements, 5);
expect(script).toContain("const tl = gsap.timeline({ paused: true });");
expect(script).toContain('tl.set("#el1"');
expect(script).toContain('visibility: "hidden"');
expect(script).toContain('visibility: "visible"');
});
it("generates empty timeline for no elements", () => {
const script = generateGsapTimelineScript([], 5);
expect(script).toContain("const tl = gsap.timeline({ paused: true });");
expect(script).toContain("duration: 5");
});
it("includes media sync for video elements", () => {
const elements = [makeVideoElement()];
const script = generateGsapTimelineScript(elements, 10);
expect(script).toContain("Sync media playback");
expect(script).toContain("media.currentTime");
});
it("generates initial position sets for elements with x/y offsets", () => {
const elements = [makeVideoElement({ id: "vid-pos", x: 100, y: 200 })];
const script = generateGsapTimelineScript(elements, 10);
expect(script).toContain('tl.set("#vid-pos", { x: 100, y: 200 }');
});
it("generates animations from keyframes", () => {
const elements = [makeTextElement({ id: "el-kf", startTime: 0, duration: 5 })];
const keyframes = {
"el-kf": [
{ id: "kf1", time: 0, properties: { opacity: 0 } },
{ id: "kf2", time: 1, properties: { opacity: 1 } },
],
};
const script = generateGsapTimelineScript(elements, 5, { keyframes });
// Should contain keyframe-based animations
expect(script).toContain("el-kf");
});
});
describe("generateHyperframesStyles", () => {
it("generates core CSS with stage dimensions for landscape", () => {
const elements = [makeTextElement()];
const { coreCss } = generateHyperframesStyles(elements, "landscape");
expect(coreCss).toContain("width: 1920px");
expect(coreCss).toContain("height: 1080px");
expect(coreCss).toContain("#stage");
});
it("generates core CSS with stage dimensions for portrait", () => {
const elements = [makeTextElement()];
const { coreCss } = generateHyperframesStyles(elements, "portrait");
expect(coreCss).toContain("width: 1080px");
expect(coreCss).toContain("height: 1920px");
});
it("generates element-specific styles for text", () => {
const elements = [makeTextElement({ id: "styled-text", fontSize: 72, color: "red" })];
const { coreCss } = generateHyperframesStyles(elements, "landscape");
expect(coreCss).toContain("#styled-text");
expect(coreCss).toContain("position: absolute");
});
it("generates element-specific styles for video", () => {
const elements = [makeVideoElement({ id: "vid-styled" })];
const { coreCss } = generateHyperframesStyles(elements, "landscape");
expect(coreCss).toContain("#vid-styled");
expect(coreCss).toContain("object-fit: contain");
});
it("includes custom CSS when provided", () => {
const elements = [makeTextElement()];
const { customCss } = generateHyperframesStyles(
elements,
"landscape",
".custom { color: blue; }",
);
expect(customCss).toContain(".custom { color: blue; }");
});
it("generates Google Fonts link for Inter (always included)", () => {
const elements = [makeTextElement()];
const { googleFontsLink } = generateHyperframesStyles(elements, "landscape");
expect(googleFontsLink).toContain("fonts.googleapis.com");
expect(googleFontsLink).toContain("Inter");
});
it("includes additional font families from text elements", () => {
const elements = [makeTextElement({ fontFamily: "Montserrat" })];
const { googleFontsLink } = generateHyperframesStyles(elements, "landscape");
expect(googleFontsLink).toContain("Montserrat");
});
});