-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathsdkCutover.test.ts
More file actions
659 lines (601 loc) · 20.9 KB
/
Copy pathsdkCutover.test.ts
File metadata and controls
659 lines (601 loc) · 20.9 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
import { describe, expect, it, vi } from "vitest";
import {
shouldUseSdkCutover,
sdkCutoverPersist,
sdkDeletePersist,
sdkTimingPersist,
sdkGsapTweenPersist,
sdkGsapKeyframePersist,
} from "./sdkCutover";
import { openComposition } from "@hyperframes/sdk";
import { createMemoryAdapter } from "@hyperframes/sdk/adapters/memory";
import type { PatchOperation } from "./sourcePatcher";
import type { MutableRefObject } from "react";
vi.mock("../components/editor/manualEditingAvailability", () => ({
STUDIO_SDK_CUTOVER_ENABLED: true,
}));
vi.mock("./studioTelemetry", () => ({
trackStudioEvent: vi.fn(),
}));
const styleOp = (property: string, value: string): PatchOperation => ({
type: "inline-style",
property,
value,
});
const textOp = (value: string): PatchOperation => ({
type: "text-content",
property: "text",
value,
});
const attrOp = (property: string, value: string): PatchOperation => ({
type: "attribute",
property,
value,
});
const htmlAttrOp = (property: string, value: string): PatchOperation => ({
type: "html-attribute",
property,
value,
});
describe("shouldUseSdkCutover", () => {
it("returns false when flag disabled", () => {
expect(shouldUseSdkCutover(false, true, "hf-abc", [styleOp("color", "red")])).toBe(false);
});
it("returns false when no session", () => {
expect(shouldUseSdkCutover(true, false, "hf-abc", [styleOp("color", "red")])).toBe(false);
});
it("returns false when no hfId", () => {
expect(shouldUseSdkCutover(true, true, null, [styleOp("color", "red")])).toBe(false);
expect(shouldUseSdkCutover(true, true, undefined, [styleOp("color", "red")])).toBe(false);
});
it("returns false when ops empty", () => {
expect(shouldUseSdkCutover(true, true, "hf-abc", [])).toBe(false);
});
it("returns true for inline-style ops", () => {
expect(shouldUseSdkCutover(true, true, "hf-abc", [styleOp("color", "red")])).toBe(true);
});
it("returns true for text-content ops", () => {
expect(shouldUseSdkCutover(true, true, "hf-abc", [textOp("hello")])).toBe(true);
});
it("returns true for attribute ops", () => {
expect(shouldUseSdkCutover(true, true, "hf-abc", [attrOp("data-x", "10")])).toBe(true);
});
it("returns true for html-attribute ops", () => {
expect(shouldUseSdkCutover(true, true, "hf-abc", [htmlAttrOp("class", "foo")])).toBe(true);
});
it("returns true when ops mix all supported types", () => {
expect(
shouldUseSdkCutover(true, true, "hf-abc", [
styleOp("color", "red"),
textOp("hello"),
attrOp("x", "1"),
htmlAttrOp("class", "foo"),
]),
).toBe(true);
});
});
describe("sdkCutoverPersist", () => {
const makeRef = <T>(val: T): MutableRefObject<T> => ({ current: val });
const makeDeps = (overrides: Partial<Parameters<typeof sdkCutoverPersist>[5]> = {}) => ({
editHistory: { recordEdit: vi.fn().mockResolvedValue(undefined) },
writeProjectFile: vi.fn().mockResolvedValue(undefined),
reloadPreview: vi.fn(),
domEditSaveTimestampRef: makeRef(0),
...overrides,
});
const makeSession = (hasEl = true) =>
({
getElement: vi.fn().mockReturnValue(hasEl ? { inlineStyles: {} } : null),
dispatch: vi.fn(),
serialize: vi.fn().mockReturnValue("<html></html>"),
batch: vi.fn((fn: () => void) => fn()),
}) as unknown as Parameters<typeof sdkCutoverPersist>[4];
it("returns false when session is null", async () => {
const deps = makeDeps();
const sel = { hfId: "hf-abc" } as never;
const result = await sdkCutoverPersist(
sel,
[styleOp("color", "red")],
"before",
"/path.html",
null,
deps,
);
expect(result).toBe(false);
});
it("returns false when element not found in session", async () => {
const deps = makeDeps();
const session = makeSession(false);
const sel = { hfId: "hf-abc" } as never;
const result = await sdkCutoverPersist(
sel,
[styleOp("color", "red")],
"before",
"/path.html",
session,
deps,
);
expect(result).toBe(false);
});
it("dispatches setStyle for inline-style ops", async () => {
const deps = makeDeps();
const session = makeSession(true);
const sel = { hfId: "hf-abc" } as never;
const result = await sdkCutoverPersist(
sel,
[styleOp("color", "red"), styleOp("opacity", "0.5")],
"before",
"/comp.html",
session,
deps,
);
expect(result).toBe(true);
expect(session!.dispatch).toHaveBeenCalledWith({
type: "setStyle",
target: "hf-abc",
styles: { color: "red", opacity: "0.5" },
});
expect(deps.writeProjectFile).toHaveBeenCalledWith("/comp.html", "<html></html>");
expect(deps.reloadPreview).toHaveBeenCalled();
});
it("dispatches setText for text-content op", async () => {
const deps = makeDeps();
const session = makeSession(true);
const sel = { hfId: "hf-abc" } as never;
const result = await sdkCutoverPersist(
sel,
[textOp("Hello world")],
"before",
"/comp.html",
session,
deps,
);
expect(result).toBe(true);
expect(session!.dispatch).toHaveBeenCalledWith({
type: "setText",
target: "hf-abc",
value: "Hello world",
});
});
it("dispatches setAttribute for attribute op with data- prefix", async () => {
const deps = makeDeps();
const session = makeSession(true);
const sel = { hfId: "hf-abc" } as never;
const result = await sdkCutoverPersist(
sel,
[attrOp("x", "42")],
"before",
"/comp.html",
session,
deps,
);
expect(result).toBe(true);
expect(session!.dispatch).toHaveBeenCalledWith({
type: "setAttribute",
target: "hf-abc",
name: "data-x",
value: "42",
});
});
it("dispatches setAttribute for html-attribute op", async () => {
const deps = makeDeps();
const session = makeSession(true);
const sel = { hfId: "hf-abc" } as never;
const result = await sdkCutoverPersist(
sel,
[htmlAttrOp("class", "foo bar")],
"before",
"/comp.html",
session,
deps,
);
expect(result).toBe(true);
expect(session!.dispatch).toHaveBeenCalledWith({
type: "setAttribute",
target: "hf-abc",
name: "class",
value: "foo bar",
});
});
it("passes caller label to recordEdit", async () => {
const deps = makeDeps();
const session = makeSession(true);
const sel = { hfId: "hf-abc" } as never;
await sdkCutoverPersist(sel, [styleOp("color", "red")], "before", "/comp.html", session, deps, {
label: "Resize layer box",
});
expect(deps.editHistory.recordEdit).toHaveBeenCalledWith(
expect.objectContaining({ label: "Resize layer box" }),
);
});
it("passes caller coalesceKey to recordEdit", async () => {
const deps = makeDeps();
const session = makeSession(true);
const sel = { hfId: "hf-abc" } as never;
await sdkCutoverPersist(sel, [styleOp("color", "red")], "before", "/comp.html", session, deps, {
coalesceKey: "my-key",
});
expect(deps.editHistory.recordEdit).toHaveBeenCalledWith(
expect.objectContaining({ coalesceKey: "my-key" }),
);
});
it("returns false and does not throw on dispatch error", async () => {
const deps = makeDeps();
const session = makeSession(true);
(session!.dispatch as ReturnType<typeof vi.fn>).mockImplementation(() => {
throw new Error("dispatch failed");
});
const sel = { hfId: "hf-abc" } as never;
const result = await sdkCutoverPersist(
sel,
[styleOp("color", "red")],
"before",
"/comp.html",
session,
deps,
);
expect(result).toBe(false);
expect(deps.reloadPreview).not.toHaveBeenCalled();
});
it("wraps all dispatches in session.batch() for atomic rollback", async () => {
const deps = makeDeps();
const session = makeSession(true);
const sel = { hfId: "hf-abc" } as never;
await sdkCutoverPersist(
sel,
[styleOp("color", "red"), styleOp("opacity", "0.5")],
"before",
"/comp.html",
session,
deps,
);
expect(
(session as unknown as { batch: ReturnType<typeof vi.fn> }).batch,
).toHaveBeenCalledOnce();
});
it("returns false when second dispatch throws (batch prevents partial mutation)", async () => {
// inline-style ops coalesce into one setStyle dispatch; use style+text to produce two dispatches.
const deps = makeDeps();
const session = makeSession(true);
let callCount = 0;
(session!.dispatch as ReturnType<typeof vi.fn>).mockImplementation(() => {
callCount++;
if (callCount === 2) throw new Error("2nd op failed");
});
const sel = { hfId: "hf-abc" } as never;
const result = await sdkCutoverPersist(
sel,
[styleOp("color", "red"), textOp("hello")],
"before",
"/comp.html",
session,
deps,
);
expect(result).toBe(false);
expect(deps.writeProjectFile).not.toHaveBeenCalled();
expect(deps.reloadPreview).not.toHaveBeenCalled();
});
});
describe("sdkDeletePersist", () => {
const makeRef = <T>(val: T): MutableRefObject<T> => ({ current: val });
const makeDeps = () => ({
editHistory: { recordEdit: vi.fn().mockResolvedValue(undefined) },
writeProjectFile: vi.fn().mockResolvedValue(undefined),
reloadPreview: vi.fn(),
domEditSaveTimestampRef: makeRef(0),
});
const makeSession = (hasEl = true) =>
({
getElement: vi.fn().mockReturnValue(hasEl ? { id: "hf-abc" } : null),
removeElement: vi.fn(),
serialize: vi.fn().mockReturnValue("<html>after</html>"),
}) as unknown as Parameters<typeof sdkDeletePersist>[3];
it("returns false when session is null", async () => {
expect(await sdkDeletePersist("hf-abc", "before", "/comp.html", null, makeDeps())).toBe(false);
});
it("returns false when element not found in session", async () => {
const session = makeSession(false);
expect(await sdkDeletePersist("hf-abc", "before", "/comp.html", session, makeDeps())).toBe(
false,
);
});
it("calls removeElement and writes serialized content", async () => {
const deps = makeDeps();
const session = makeSession(true);
const result = await sdkDeletePersist("hf-abc", "before", "/comp.html", session, deps);
expect(result).toBe(true);
expect(session!.removeElement).toHaveBeenCalledWith("hf-abc");
expect(deps.writeProjectFile).toHaveBeenCalledWith("/comp.html", "<html>after</html>");
});
it("records edit history with before/after diff", async () => {
const deps = makeDeps();
const session = makeSession(true);
await sdkDeletePersist("hf-abc", "before-content", "/comp.html", session, deps);
expect(deps.editHistory.recordEdit).toHaveBeenCalledWith(
expect.objectContaining({
label: "Delete element",
files: { "/comp.html": { before: "before-content", after: "<html>after</html>" } },
}),
);
});
it("calls reloadPreview on success", async () => {
const deps = makeDeps();
const session = makeSession(true);
await sdkDeletePersist("hf-abc", "before", "/comp.html", session, deps);
expect(deps.reloadPreview).toHaveBeenCalled();
});
it("returns false and does not write on removeElement error", async () => {
const deps = makeDeps();
const session = makeSession(true);
(session!.removeElement as ReturnType<typeof vi.fn>).mockImplementation(() => {
throw new Error("remove failed");
});
const result = await sdkDeletePersist("hf-abc", "before", "/comp.html", session, deps);
expect(result).toBe(false);
expect(deps.writeProjectFile).not.toHaveBeenCalled();
expect(deps.reloadPreview).not.toHaveBeenCalled();
});
});
describe("sdkTimingPersist", () => {
const makeRef = <T>(val: T): MutableRefObject<T> => ({ current: val });
const makeDeps = () => ({
editHistory: { recordEdit: vi.fn().mockResolvedValue(undefined) },
writeProjectFile: vi.fn().mockResolvedValue(undefined),
reloadPreview: vi.fn(),
domEditSaveTimestampRef: makeRef(0),
});
const makeSession = (hasEl = true) =>
({
getElement: vi.fn().mockReturnValue(hasEl ? { id: "hf-clip" } : null),
setTiming: vi.fn(),
serialize: vi
.fn()
.mockReturnValueOnce("<html>before</html>")
.mockReturnValue("<html>after</html>"),
}) as unknown as Parameters<typeof sdkTimingPersist>[3];
it("returns false when session is null", async () => {
expect(await sdkTimingPersist("hf-clip", "/comp.html", { start: 1 }, null, makeDeps())).toBe(
false,
);
});
it("returns false when element not found in session", async () => {
const session = makeSession(false);
expect(await sdkTimingPersist("hf-clip", "/comp.html", { start: 1 }, session, makeDeps())).toBe(
false,
);
});
it("calls setTiming with provided update and writes serialized content", async () => {
const deps = makeDeps();
const session = makeSession(true);
const result = await sdkTimingPersist(
"hf-clip",
"/comp.html",
{ start: 2, duration: 5, trackIndex: 1 },
session,
deps,
);
expect(result).toBe(true);
expect(session!.setTiming).toHaveBeenCalledWith("hf-clip", {
start: 2,
duration: 5,
trackIndex: 1,
});
expect(deps.writeProjectFile).toHaveBeenCalledWith("/comp.html", "<html>after</html>");
});
it("captures before-state before setTiming dispatch", async () => {
const deps = makeDeps();
const session = makeSession(true);
await sdkTimingPersist("hf-clip", "/comp.html", { start: 3 }, session, deps);
expect(deps.editHistory.recordEdit).toHaveBeenCalledWith(
expect.objectContaining({
files: { "/comp.html": { before: "<html>before</html>", after: "<html>after</html>" } },
}),
);
});
it("returns false and does not write on setTiming error", async () => {
const deps = makeDeps();
const session = makeSession(true);
(session!.setTiming as ReturnType<typeof vi.fn>).mockImplementation(() => {
throw new Error("timing error");
});
const result = await sdkTimingPersist("hf-clip", "/comp.html", { start: 1 }, session, deps);
expect(result).toBe(false);
expect(deps.writeProjectFile).not.toHaveBeenCalled();
});
});
describe("sdkGsapTweenPersist", () => {
const makeRef = <T>(val: T): MutableRefObject<T> => ({ current: val });
const makeDeps = () => ({
editHistory: { recordEdit: vi.fn().mockResolvedValue(undefined) },
writeProjectFile: vi.fn().mockResolvedValue(undefined),
reloadPreview: vi.fn(),
domEditSaveTimestampRef: makeRef(0),
});
const makeSession = (opts?: { addGsapTween?: string; hasEl?: boolean }) =>
({
getElement: vi.fn().mockReturnValue(opts?.hasEl !== false ? { id: "hf-box" } : null),
addGsapTween: vi.fn().mockReturnValue(opts?.addGsapTween ?? "tw-1"),
setGsapTween: vi.fn(),
removeGsapTween: vi.fn(),
serialize: vi
.fn()
.mockReturnValueOnce("<html>before</html>")
.mockReturnValue("<html>after</html>"),
}) as unknown as Parameters<typeof sdkGsapTweenPersist>[2];
it("returns false when session is null", async () => {
expect(
await sdkGsapTweenPersist(
"/comp.html",
{ kind: "remove", animationId: "tw-1" },
null,
makeDeps(),
),
).toBe(false);
});
it("calls addGsapTween and writes for kind=add", async () => {
const deps = makeDeps();
const session = makeSession();
const result = await sdkGsapTweenPersist(
"/comp.html",
{
kind: "add",
target: "hf-box",
spec: { method: "to", duration: 1, properties: { opacity: 1 } },
},
session,
deps,
);
expect(result).toBe(true);
expect(session!.addGsapTween).toHaveBeenCalledWith(
"hf-box",
expect.objectContaining({ method: "to" }),
);
expect(deps.writeProjectFile).toHaveBeenCalledWith("/comp.html", "<html>after</html>");
});
it("returns false for kind=add when element not found", async () => {
const deps = makeDeps();
const session = makeSession({ hasEl: false });
const result = await sdkGsapTweenPersist(
"/comp.html",
{ kind: "add", target: "hf-box", spec: { method: "to", properties: { x: 100 } } },
session,
deps,
);
expect(result).toBe(false);
expect(deps.writeProjectFile).not.toHaveBeenCalled();
});
it("calls setGsapTween and writes for kind=set", async () => {
const deps = makeDeps();
const session = makeSession();
const result = await sdkGsapTweenPersist(
"/comp.html",
{ kind: "set", animationId: "tw-1", properties: { ease: "power3.in" } },
session,
deps,
);
expect(result).toBe(true);
expect(session!.setGsapTween).toHaveBeenCalledWith("tw-1", { ease: "power3.in" });
expect(deps.reloadPreview).toHaveBeenCalled();
});
it("calls removeGsapTween for kind=remove", async () => {
const deps = makeDeps();
const session = makeSession();
const result = await sdkGsapTweenPersist(
"/comp.html",
{ kind: "remove", animationId: "tw-1" },
session,
deps,
);
expect(result).toBe(true);
expect(session!.removeGsapTween).toHaveBeenCalledWith("tw-1");
});
it("returns false and does not write on SDK error", async () => {
const deps = makeDeps();
const session = makeSession();
(session!.removeGsapTween as ReturnType<typeof vi.fn>).mockImplementation(() => {
throw new Error("gsap error");
});
const result = await sdkGsapTweenPersist(
"/comp.html",
{ kind: "remove", animationId: "tw-1" },
session,
deps,
);
expect(result).toBe(false);
expect(deps.writeProjectFile).not.toHaveBeenCalled();
});
});
describe("sdkGsapKeyframePersist", () => {
const makeRef = <T>(val: T): MutableRefObject<T> => ({ current: val });
const makeDeps = () => ({
editHistory: { recordEdit: vi.fn().mockResolvedValue(undefined) },
writeProjectFile: vi.fn().mockResolvedValue(undefined),
reloadPreview: vi.fn(),
domEditSaveTimestampRef: makeRef(0),
});
const makeSession = () =>
({
dispatch: vi.fn(),
serialize: vi
.fn()
.mockReturnValueOnce("<html>before</html>")
.mockReturnValue("<html>after</html>"),
}) as unknown as Parameters<typeof sdkGsapKeyframePersist>[4];
it("returns false when session is null", async () => {
expect(
await sdkGsapKeyframePersist("/comp.html", "tw-1", 50, { opacity: 0.5 }, null, makeDeps()),
).toBe(false);
});
it("dispatches addGsapKeyframe and writes serialized content", async () => {
const deps = makeDeps();
const session = makeSession();
const result = await sdkGsapKeyframePersist(
"/comp.html",
"tw-1",
50,
{ opacity: 0.5 },
session,
deps,
);
expect(result).toBe(true);
expect(session!.dispatch).toHaveBeenCalledWith({
type: "addGsapKeyframe",
animationId: "tw-1",
position: 50,
value: { opacity: 0.5 },
});
expect(deps.writeProjectFile).toHaveBeenCalledWith("/comp.html", "<html>after</html>");
expect(deps.reloadPreview).toHaveBeenCalled();
});
it("returns false and does not write on dispatch error", async () => {
const deps = makeDeps();
const session = makeSession();
(session!.dispatch as ReturnType<typeof vi.fn>).mockImplementation(() => {
throw new Error("dispatch failed");
});
const result = await sdkGsapKeyframePersist(
"/comp.html",
"tw-1",
25,
{ x: 100 },
session,
deps,
);
expect(result).toBe(false);
expect(deps.writeProjectFile).not.toHaveBeenCalled();
});
});
describe("sdkCutoverPersist — GSAP script preservation (integration)", () => {
const makeRef = <T>(val: T): MutableRefObject<T> => ({ current: val });
const makeDeps = () => ({
editHistory: { recordEdit: vi.fn().mockResolvedValue(undefined) },
writeProjectFile: vi.fn().mockResolvedValue(undefined),
reloadPreview: vi.fn(),
domEditSaveTimestampRef: makeRef(0),
});
it("preserves GSAP <script> block and data-position-mode through setStyle dispatch", async () => {
const html = `<!DOCTYPE html><html><head></head><body>
<div data-hf-id="hf-layer" style="color: blue; opacity: 1"></div>
<script data-hf-gsap data-position-mode="relative">
gsap.timeline().to('[data-hf-id="hf-layer"]', { duration: 1, x: 100 });
</script>
</body></html>`;
const comp = await openComposition(html, { persist: createMemoryAdapter() });
const deps = makeDeps();
const sel = { hfId: "hf-layer" } as never;
const result = await sdkCutoverPersist(
sel,
[{ type: "inline-style", property: "color", value: "red" }],
html,
"/comp.html",
comp,
deps,
);
expect(result).toBe(true);
const written = (deps.writeProjectFile as ReturnType<typeof vi.fn>).mock
.calls[0]?.[1] as string;
expect(written).toContain("data-hf-gsap");
expect(written).toContain('data-position-mode="relative"');
expect(written).toContain("gsap.timeline()");
});
});