|
| 1 | +// @vitest-environment happy-dom |
| 2 | +import { afterEach, expect, it, vi } from "vitest"; |
| 3 | +import type { GsapAnimation } from "@hyperframes/core/gsap-parser"; |
| 4 | +import type { DomEditSelection } from "../components/editor/domEditingTypes"; |
| 5 | +import { usePlayerStore } from "../player/store/playerStore"; |
| 6 | +import { tryGsapResizeIntercept } from "./gsapResizeIntercept"; |
| 7 | + |
| 8 | +afterEach(() => { |
| 9 | + vi.restoreAllMocks(); |
| 10 | + usePlayerStore.setState({ currentTime: 0, activeKeyframePct: null }); |
| 11 | +}); |
| 12 | + |
| 13 | +/** |
| 14 | + * Scale-route resize: an element whose visual size is driven by a scale-group |
| 15 | + * tween. The intercept must (a) route the commit through SCALE, never |
| 16 | + * width/height, and (b) resolve convert-to-keyframes from-values through the |
| 17 | + * group filter — an opacity-touching intro tween on the same element must not |
| 18 | + * ride into the converted keyframes (the disappearance bake class). |
| 19 | + */ |
| 20 | +function makeGradedElement(): HTMLElement { |
| 21 | + const el = document.createElement("img"); |
| 22 | + el.id = "clip"; |
| 23 | + el.setAttribute("data-hf-studio-original-width", "640"); |
| 24 | + el.setAttribute("data-hf-studio-original-height", "360"); |
| 25 | + // Grading contract: source hidden, canvas carries effective opacity. |
| 26 | + el.setAttribute("data-hf-color-grading-source-hidden", ""); |
| 27 | + const canvas = document.createElement("canvas"); |
| 28 | + canvas.id = "__hf_color_grading_clip"; |
| 29 | + canvas.style.opacity = "0.98"; |
| 30 | + document.body.append(el, canvas); |
| 31 | + return el; |
| 32 | +} |
| 33 | + |
| 34 | +function fakeIframe(el: HTMLElement, gsapValues: Record<string, number>) { |
| 35 | + // The element's OPACITY intro tween lives on the timeline: unfiltered |
| 36 | + // capture would pick `opacity` up via the other-tween sweep. |
| 37 | + const opacityIntro = { targets: () => [el], vars: { opacity: 0, duration: 0.8 } }; |
| 38 | + return { |
| 39 | + contentWindow: { |
| 40 | + __timelines: { main: { getChildren: () => [opacityIntro] } }, |
| 41 | + gsap: { getProperty: (_el: Element, prop: string) => gsapValues[prop] ?? 0 }, |
| 42 | + }, |
| 43 | + contentDocument: document, |
| 44 | + } as unknown as HTMLIFrameElement; |
| 45 | +} |
| 46 | + |
| 47 | +function scaleFromTween(): GsapAnimation { |
| 48 | + return { |
| 49 | + id: "#clip-from-200-scale", |
| 50 | + targetSelector: "#clip", |
| 51 | + propertyGroup: "scale", |
| 52 | + method: "from", |
| 53 | + properties: { scale: 0.9 }, |
| 54 | + position: 0.2, |
| 55 | + resolvedStart: 0.2, |
| 56 | + duration: 0.8, |
| 57 | + } as unknown as GsapAnimation; |
| 58 | +} |
| 59 | + |
| 60 | +function keyframedScaleFixture(): GsapAnimation { |
| 61 | + return { |
| 62 | + ...scaleFromTween(), |
| 63 | + keyframes: { |
| 64 | + keyframes: [ |
| 65 | + { percentage: 0, properties: { scale: 0.9 } }, |
| 66 | + { percentage: 100, properties: { scale: 1 } }, |
| 67 | + ], |
| 68 | + }, |
| 69 | + } as unknown as GsapAnimation; |
| 70 | +} |
| 71 | + |
| 72 | +/** Drive one resize through the intercept, returning every committed mutation. */ |
| 73 | +async function runResize( |
| 74 | + el: HTMLElement, |
| 75 | + iframe: HTMLIFrameElement, |
| 76 | + size: { width: number; height: number }, |
| 77 | +): Promise<Array<Record<string, unknown>>> { |
| 78 | + const selection = { id: "clip", selector: "#clip", element: el } as unknown as DomEditSelection; |
| 79 | + usePlayerStore.setState({ currentTime: 0.5 }); // inside the tween's range |
| 80 | + const committed: Array<Record<string, unknown>> = []; |
| 81 | + const commitMutation = vi.fn(async (_sel: unknown, mutation: Record<string, unknown>) => { |
| 82 | + committed.push(mutation); |
| 83 | + }); |
| 84 | + const handled = await tryGsapResizeIntercept( |
| 85 | + selection, |
| 86 | + size, |
| 87 | + [scaleFromTween()], |
| 88 | + iframe, |
| 89 | + commitMutation as never, |
| 90 | + async () => [keyframedScaleFixture()], |
| 91 | + ); |
| 92 | + expect(handled).toBe(true); |
| 93 | + return committed; |
| 94 | +} |
| 95 | + |
| 96 | +it("scale-route resize converts via the group filter and commits scale, not width/height", async () => { |
| 97 | + const el = makeGradedElement(); |
| 98 | + const iframe = fakeIframe(el, { scale: 1, scaleX: 1, scaleY: 1, opacity: 0, rotation: 0 }); |
| 99 | + // uniform: 800/640 === 450/360 |
| 100 | + const committed = await runResize(el, iframe, { width: 800, height: 450 }); |
| 101 | + |
| 102 | + const convert = committed.find((m) => m.type === "convert-to-keyframes"); |
| 103 | + expect(convert).toBeDefined(); |
| 104 | + const fromValues = convert!.resolvedFromValues as Record<string, number>; |
| 105 | + // Group filter: the opacity intro tween must NOT leak into the conversion. |
| 106 | + expect(fromValues).not.toHaveProperty("opacity"); |
| 107 | + expect(fromValues).toHaveProperty("scale"); |
| 108 | + |
| 109 | + // Every committed property is scale-group — the resize never writes |
| 110 | + // width/height for a scale-driven element (the double-apply bug class). |
| 111 | + const allProps = committed.flatMap((m) => [ |
| 112 | + ...Object.keys((m.properties as Record<string, unknown>) ?? {}), |
| 113 | + ...Object.keys((m.resolvedFromValues as Record<string, unknown>) ?? {}), |
| 114 | + ]); |
| 115 | + expect(allProps).not.toContain("width"); |
| 116 | + expect(allProps).not.toContain("height"); |
| 117 | + expect(allProps.some((p) => p === "scale" || p === "scaleX")).toBe(true); |
| 118 | +}); |
| 119 | + |
| 120 | +it("non-uniform drag commits scaleX/scaleY longhands", async () => { |
| 121 | + const el = makeGradedElement(); |
| 122 | + const iframe = fakeIframe(el, { scale: 1, scaleX: 1, scaleY: 1, opacity: 0 }); |
| 123 | + // scaleX 1.25 vs scaleY 1.0 → non-uniform |
| 124 | + const committed = await runResize(el, iframe, { width: 800, height: 360 }); |
| 125 | + |
| 126 | + const serialized = JSON.stringify(committed); |
| 127 | + expect(serialized).toContain("scaleX"); |
| 128 | + expect(serialized).toContain("scaleY"); |
| 129 | +}); |
0 commit comments