|
| 1 | +// @vitest-environment happy-dom |
| 2 | +/** |
| 3 | + * The scale-route resize invariant, swept over the shapes a composition |
| 4 | + * actually produces: WHERE THE USER DROPS THE BOX IS WHERE IT LANDS. |
| 5 | + * |
| 6 | + * Each case builds the geometry the browser reports — a CSS box at a layout |
| 7 | + * position, transformed by GSAP's translate/rotate/scale about the element |
| 8 | + * centre — drives one resize through the real intercept, then re-renders the |
| 9 | + * committed scale and the PERSISTED position and checks the box is still on |
| 10 | + * the drop point. Asserting on the persisted values rather than the live ones |
| 11 | + * is the point: every bug in this class showed as a correct-looking drop frame |
| 12 | + * followed by the element sliding to whatever got written to disk. |
| 13 | + * |
| 14 | + * The model is calibrated against real `hf-resize-debug` output: the same |
| 15 | + * inputs reproduce the drop rect, the post-commit rect and the rect width the |
| 16 | + * browser reported to three decimal places. |
| 17 | + */ |
| 18 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 19 | +import type { GsapAnimation } from "@hyperframes/core/gsap-parser"; |
| 20 | +import type { DomEditSelection } from "../components/editor/domEditingTypes"; |
| 21 | +import { usePlayerStore } from "../player/store/playerStore"; |
| 22 | +import { tryGsapResizeIntercept } from "./gsapResizeIntercept"; |
| 23 | + |
| 24 | +afterEach(() => { |
| 25 | + vi.restoreAllMocks(); |
| 26 | + usePlayerStore.setState({ currentTime: 0, activeKeyframePct: null }); |
| 27 | + document.body.innerHTML = ""; |
| 28 | +}); |
| 29 | + |
| 30 | +interface Pose { |
| 31 | + /** Untransformed CSS box, in px. */ |
| 32 | + box: { w: number; h: number }; |
| 33 | + /** GSAP translate. */ |
| 34 | + pos: { x: number; y: number }; |
| 35 | + /** GSAP scale, per axis. */ |
| 36 | + scale: { x: number; y: number }; |
| 37 | +} |
| 38 | + |
| 39 | +/** Layout position of the untransformed box, shared by every pose in a case. */ |
| 40 | +const LAYOUT = { left: 120, top: 520 }; |
| 41 | + |
| 42 | +/** |
| 43 | + * The AABB a browser reports for `translate() rotate() scale()` about the |
| 44 | + * element centre — the same rect `getBoundingClientRect` returns, which is why |
| 45 | + * a rotated element's anchor is approximate rather than corner-exact. |
| 46 | + */ |
| 47 | +function renderRect( |
| 48 | + pose: Pose, |
| 49 | + rotationDeg: number, |
| 50 | +): { x: number; y: number; w: number; h: number } { |
| 51 | + const rad = (rotationDeg * Math.PI) / 180; |
| 52 | + const [cos, sin] = [Math.abs(Math.cos(rad)), Math.abs(Math.sin(rad))]; |
| 53 | + const [sw, sh] = [pose.box.w * pose.scale.x, pose.box.h * pose.scale.y]; |
| 54 | + const w = sw * cos + sh * sin; |
| 55 | + const h = sw * sin + sh * cos; |
| 56 | + const cx = LAYOUT.left + pose.box.w / 2 + pose.pos.x; |
| 57 | + const cy = LAYOUT.top + pose.box.h / 2 + pose.pos.y; |
| 58 | + return { x: cx - w / 2, y: cy - h / 2, w, h }; |
| 59 | +} |
| 60 | + |
| 61 | +interface ResizeCase { |
| 62 | + name: string; |
| 63 | + /** Untransformed box the stylesheet (or an inline style) gives the element. */ |
| 64 | + box: { w: number; h: number }; |
| 65 | + /** Where the element sat before the gesture. */ |
| 66 | + base: { x: number; y: number }; |
| 67 | + /** Scale already on the element — 1 for a never-resized one. */ |
| 68 | + liveScale: { x: number; y: number }; |
| 69 | + rotation?: number; |
| 70 | + /** The box the user dragged to, and where the draft put it. */ |
| 71 | + drop: { w: number; h: number; x: number; y: number }; |
| 72 | + /** Sized inline rather than by a stylesheet. */ |
| 73 | + inlineSized?: boolean; |
| 74 | + /** The tween states scaleX/scaleY rather than the shorthand. */ |
| 75 | + longhandTween?: boolean; |
| 76 | + /** What already writes this element's position. */ |
| 77 | + positionWrite?: "static-set" | "keyframed-tween" | "none"; |
| 78 | +} |
| 79 | + |
| 80 | +function scaleTween(longhand: boolean): GsapAnimation { |
| 81 | + const at = (v: number) => (longhand ? { scaleX: v, scaleY: v } : { scale: v }); |
| 82 | + return { |
| 83 | + id: "#clip-to-0-scale", |
| 84 | + targetSelector: "#clip", |
| 85 | + propertyGroup: "scale", |
| 86 | + method: "to", |
| 87 | + properties: at(1), |
| 88 | + position: 0, |
| 89 | + resolvedStart: 0, |
| 90 | + duration: 2, |
| 91 | + keyframes: { |
| 92 | + keyframes: [ |
| 93 | + { percentage: 0, properties: at(1) }, |
| 94 | + { percentage: 100, properties: at(1.08) }, |
| 95 | + ], |
| 96 | + }, |
| 97 | + } as unknown as GsapAnimation; |
| 98 | +} |
| 99 | + |
| 100 | +function positionAnimation( |
| 101 | + kind: NonNullable<ResizeCase["positionWrite"]>, |
| 102 | + base: { x: number; y: number }, |
| 103 | +) { |
| 104 | + if (kind === "none") return null; |
| 105 | + if (kind === "static-set") { |
| 106 | + return { |
| 107 | + id: "#clip-set-0-position", |
| 108 | + targetSelector: "#clip", |
| 109 | + propertyGroup: "position", |
| 110 | + method: "set", |
| 111 | + properties: { x: base.x, y: base.y }, |
| 112 | + position: 0, |
| 113 | + resolvedStart: 0, |
| 114 | + duration: 0, |
| 115 | + global: true, |
| 116 | + } as unknown as GsapAnimation; |
| 117 | + } |
| 118 | + return { |
| 119 | + id: "#clip-to-0-position", |
| 120 | + targetSelector: "#clip", |
| 121 | + propertyGroup: "position", |
| 122 | + method: "to", |
| 123 | + properties: { x: base.x, y: base.y }, |
| 124 | + position: 0, |
| 125 | + resolvedStart: 0, |
| 126 | + duration: 2, |
| 127 | + keyframes: { |
| 128 | + keyframes: [ |
| 129 | + { percentage: 0, properties: { x: base.x, y: base.y } }, |
| 130 | + { percentage: 100, properties: { x: base.x + 40, y: base.y + 40 } }, |
| 131 | + ], |
| 132 | + }, |
| 133 | + } as unknown as GsapAnimation; |
| 134 | +} |
| 135 | + |
| 136 | +/** Every x/y a run wrote, in commit order. */ |
| 137 | +function persistedPositions(calls: unknown[][]): Array<{ x?: number; y?: number }> { |
| 138 | + const written: Array<{ x?: number; y?: number }> = []; |
| 139 | + for (const call of calls) { |
| 140 | + const mutation = call[1] as { |
| 141 | + properties?: Record<string, number>; |
| 142 | + keyframes?: Array<{ properties: Record<string, number> }>; |
| 143 | + x?: number; |
| 144 | + y?: number; |
| 145 | + }; |
| 146 | + const sources = [mutation.properties, ...(mutation.keyframes ?? []).map((k) => k.properties)]; |
| 147 | + if (mutation.x != null || mutation.y != null) written.push({ x: mutation.x, y: mutation.y }); |
| 148 | + for (const source of sources) { |
| 149 | + if (source && (source.x != null || source.y != null)) |
| 150 | + written.push({ x: source.x, y: source.y }); |
| 151 | + } |
| 152 | + } |
| 153 | + return written; |
| 154 | +} |
| 155 | + |
| 156 | +/** The scale the run committed AT THE PLAYHEAD (percentage 0 here). */ |
| 157 | +function persistedScale(calls: unknown[][]): { x: number; y: number } | null { |
| 158 | + let found: { x: number; y: number } | null = null; |
| 159 | + const take = (source: Record<string, number> | undefined) => { |
| 160 | + if (!source) return; |
| 161 | + const x = source.scaleX ?? source.scale; |
| 162 | + const y = source.scaleY ?? source.scale; |
| 163 | + if (x != null && y != null) found = { x, y }; |
| 164 | + }; |
| 165 | + for (const call of calls) { |
| 166 | + const mutation = call[1] as { |
| 167 | + properties?: Record<string, number>; |
| 168 | + percentage?: number; |
| 169 | + keyframes?: Array<{ percentage: number; properties: Record<string, number> }>; |
| 170 | + }; |
| 171 | + if (mutation.keyframes) { |
| 172 | + for (const frame of mutation.keyframes) if (frame.percentage === 0) take(frame.properties); |
| 173 | + continue; |
| 174 | + } |
| 175 | + if (mutation.percentage != null && mutation.percentage !== 0) continue; |
| 176 | + take(mutation.properties); |
| 177 | + } |
| 178 | + return found; |
| 179 | +} |
| 180 | + |
| 181 | +async function runCase(testCase: ResizeCase) { |
| 182 | + const rotation = testCase.rotation ?? 0; |
| 183 | + const el = document.createElement("div"); |
| 184 | + el.id = "clip"; |
| 185 | + if (testCase.inlineSized) { |
| 186 | + el.setAttribute("data-hf-studio-original-width", `${testCase.box.w}px`); |
| 187 | + el.setAttribute("data-hf-studio-original-height", `${testCase.box.h}px`); |
| 188 | + } else { |
| 189 | + el.setAttribute("data-hf-studio-original-box-width", `${testCase.box.w}`); |
| 190 | + el.setAttribute("data-hf-studio-original-box-height", `${testCase.box.h}`); |
| 191 | + el.setAttribute("data-hf-studio-original-width", ""); |
| 192 | + el.setAttribute("data-hf-studio-original-height", ""); |
| 193 | + } |
| 194 | + // What the gesture stamps at drag start, and the draft it leaves applied. |
| 195 | + el.setAttribute("data-hf-drag-gsap-base-x", `${testCase.base.x}`); |
| 196 | + el.setAttribute("data-hf-drag-gsap-base-y", `${testCase.base.y}`); |
| 197 | + el.setAttribute("data-hf-studio-box-size", "true"); |
| 198 | + el.style.width = `${testCase.drop.w}px`; |
| 199 | + el.style.height = `${testCase.drop.h}px`; |
| 200 | + document.body.append(el); |
| 201 | + |
| 202 | + // The live pose the intercept reads and mutates. |
| 203 | + const live: Pose = { |
| 204 | + box: { ...testCase.box }, |
| 205 | + pos: { x: testCase.drop.x, y: testCase.drop.y }, |
| 206 | + scale: { ...testCase.liveScale }, |
| 207 | + }; |
| 208 | + el.getBoundingClientRect = () => { |
| 209 | + // The draft's inline box while it is applied, the real one once cleared. |
| 210 | + const w = Number.parseFloat(el.style.width) || live.box.w; |
| 211 | + const h = Number.parseFloat(el.style.height) || live.box.h; |
| 212 | + const rect = renderRect({ ...live, box: { w, h } }, rotation); |
| 213 | + return { ...rect, width: rect.w, height: rect.h } as unknown as DOMRect; |
| 214 | + }; |
| 215 | + const gsapStub = { |
| 216 | + set: (_target: Element, vars: Record<string, number>) => { |
| 217 | + if (vars.x != null) live.pos.x = vars.x; |
| 218 | + if (vars.y != null) live.pos.y = vars.y; |
| 219 | + if (vars.scaleX != null) live.scale.x = vars.scaleX; |
| 220 | + if (vars.scaleY != null) live.scale.y = vars.scaleY; |
| 221 | + }, |
| 222 | + getProperty: (_target: Element, prop: string) => |
| 223 | + ({ |
| 224 | + scaleX: live.scale.x, |
| 225 | + scaleY: live.scale.y, |
| 226 | + x: live.pos.x, |
| 227 | + y: live.pos.y, |
| 228 | + rotation, |
| 229 | + })[prop] ?? 0, |
| 230 | + }; |
| 231 | + Object.assign(window, { gsap: gsapStub }); |
| 232 | + const iframe = { |
| 233 | + contentWindow: { gsap: gsapStub, __timelines: { main: { getChildren: () => [] } } }, |
| 234 | + contentDocument: document, |
| 235 | + } as unknown as HTMLIFrameElement; |
| 236 | + |
| 237 | + const dropPoint = el.getBoundingClientRect(); |
| 238 | + const position = positionAnimation(testCase.positionWrite ?? "static-set", testCase.base); |
| 239 | + const animations = [scaleTween(!!testCase.longhandTween), ...(position ? [position] : [])]; |
| 240 | + const commitMutation = vi.fn(); |
| 241 | + usePlayerStore.setState({ currentTime: 0 }); |
| 242 | + |
| 243 | + await tryGsapResizeIntercept( |
| 244 | + { id: "clip", selector: "#clip", element: el } as DomEditSelection, |
| 245 | + { width: testCase.drop.w, height: testCase.drop.h }, |
| 246 | + animations, |
| 247 | + iframe, |
| 248 | + commitMutation, |
| 249 | + async () => animations, |
| 250 | + ); |
| 251 | + |
| 252 | + // Re-render what the file now says: the committed scale, the persisted |
| 253 | + // position (or the pre-gesture one when nothing was written), the real box. |
| 254 | + const scale = persistedScale(commitMutation.mock.calls) ?? testCase.liveScale; |
| 255 | + const writes = persistedPositions(commitMutation.mock.calls); |
| 256 | + const last = writes.at(-1); |
| 257 | + const settled: Pose = { |
| 258 | + box: testCase.box, |
| 259 | + pos: { x: last?.x ?? testCase.base.x, y: last?.y ?? testCase.base.y }, |
| 260 | + scale, |
| 261 | + }; |
| 262 | + return { |
| 263 | + dropPoint: { x: dropPoint.x, y: dropPoint.y, w: dropPoint.width, h: dropPoint.height }, |
| 264 | + settled: renderRect(settled, rotation), |
| 265 | + writes, |
| 266 | + }; |
| 267 | +} |
| 268 | + |
| 269 | +const CASES: ResizeCase[] = [ |
| 270 | + { |
| 271 | + name: "shrinks a stylesheet-sized element", |
| 272 | + box: { w: 630, h: 252 }, |
| 273 | + base: { x: 489, y: 195 }, |
| 274 | + liveScale: { x: 1.648, y: 1.648 }, |
| 275 | + drop: { w: 320, h: 128, x: 587, y: 235 }, |
| 276 | + }, |
| 277 | + { |
| 278 | + name: "grows a stylesheet-sized element", |
| 279 | + box: { w: 630, h: 252 }, |
| 280 | + base: { x: 489, y: 195 }, |
| 281 | + liveScale: { x: 0.837, y: 0.837 }, |
| 282 | + drop: { w: 1319, h: 527, x: -67, y: -26 }, |
| 283 | + }, |
| 284 | + { |
| 285 | + name: "resizes an element for the first time (no scale of its own yet)", |
| 286 | + box: { w: 630, h: 252 }, |
| 287 | + base: { x: 489, y: 195 }, |
| 288 | + liveScale: { x: 1, y: 1 }, |
| 289 | + drop: { w: 900, h: 360, x: 300, y: 100 }, |
| 290 | + }, |
| 291 | + { |
| 292 | + name: "resizes a rotated element", |
| 293 | + box: { w: 630, h: 200 }, |
| 294 | + base: { x: 329, y: 129 }, |
| 295 | + liveScale: { x: 0.94, y: 0.939 }, |
| 296 | + rotation: -8, |
| 297 | + longhandTween: true, |
| 298 | + drop: { w: 350, h: 111, x: 609, y: 219 }, |
| 299 | + }, |
| 300 | + { |
| 301 | + name: "resizes a steeply rotated element", |
| 302 | + box: { w: 630, h: 252 }, |
| 303 | + base: { x: 17, y: -69 }, |
| 304 | + liveScale: { x: 1.192, y: 1.2 }, |
| 305 | + rotation: -47, |
| 306 | + longhandTween: true, |
| 307 | + drop: { w: 491, h: 196, x: 120, y: 40 }, |
| 308 | + }, |
| 309 | + { |
| 310 | + name: "takes a non-uniform drag", |
| 311 | + box: { w: 630, h: 252 }, |
| 312 | + base: { x: 489, y: 195 }, |
| 313 | + liveScale: { x: 1, y: 1 }, |
| 314 | + longhandTween: true, |
| 315 | + drop: { w: 1200, h: 300, x: -80, y: 60 }, |
| 316 | + }, |
| 317 | + { |
| 318 | + name: "shrinks almost to nothing", |
| 319 | + box: { w: 630, h: 252 }, |
| 320 | + base: { x: 489, y: 195 }, |
| 321 | + liveScale: { x: 1, y: 1 }, |
| 322 | + drop: { w: 13, h: 5, x: 800, y: 320 }, |
| 323 | + }, |
| 324 | + { |
| 325 | + name: "resizes an inline-sized element", |
| 326 | + box: { w: 500, h: 252 }, |
| 327 | + base: { x: 100, y: 100 }, |
| 328 | + liveScale: { x: 0.5, y: 0.5 }, |
| 329 | + inlineSized: true, |
| 330 | + drop: { w: 700, h: 353, x: -40, y: -30 }, |
| 331 | + }, |
| 332 | + { |
| 333 | + name: "resizes an element with no position write at all", |
| 334 | + box: { w: 630, h: 252 }, |
| 335 | + base: { x: 0, y: 0 }, |
| 336 | + liveScale: { x: 1, y: 1 }, |
| 337 | + positionWrite: "none", |
| 338 | + drop: { w: 900, h: 360, x: 140, y: 55 }, |
| 339 | + }, |
| 340 | + { |
| 341 | + name: "resizes an element whose position is animated", |
| 342 | + box: { w: 630, h: 252 }, |
| 343 | + base: { x: 489, y: 195 }, |
| 344 | + liveScale: { x: 1.648, y: 1.648 }, |
| 345 | + positionWrite: "keyframed-tween", |
| 346 | + drop: { w: 320, h: 128, x: 587, y: 235 }, |
| 347 | + }, |
| 348 | +]; |
| 349 | + |
| 350 | +describe("a scale resize lands the element on the drop point", () => { |
| 351 | + for (const testCase of CASES) { |
| 352 | + it(testCase.name, async () => { |
| 353 | + const { dropPoint, settled } = await runCase(testCase); |
| 354 | + // 1px: the commit rounds position to whole pixels and scale to 3dp. |
| 355 | + expect(settled.x).toBeCloseTo(dropPoint.x, 0); |
| 356 | + expect(settled.y).toBeCloseTo(dropPoint.y, 0); |
| 357 | + expect(settled.w).toBeCloseTo(dropPoint.w, 0); |
| 358 | + expect(settled.h).toBeCloseTo(dropPoint.h, 0); |
| 359 | + }); |
| 360 | + } |
| 361 | +}); |
| 362 | + |
| 363 | +/** |
| 364 | + * Two drags in a row on the same element. The first bug in this class only |
| 365 | + * showed on the second one, because the wrong value the first wrote then |
| 366 | + * counted as the element's live pose. |
| 367 | + */ |
| 368 | +it("holds the drop point across a second drag", async () => { |
| 369 | + const first = await runCase({ |
| 370 | + name: "first", |
| 371 | + box: { w: 630, h: 252 }, |
| 372 | + base: { x: 489, y: 195 }, |
| 373 | + liveScale: { x: 1, y: 1 }, |
| 374 | + drop: { w: 900, h: 360, x: 300, y: 100 }, |
| 375 | + }); |
| 376 | + const settledPos = first.writes.at(-1); |
| 377 | + document.body.innerHTML = ""; |
| 378 | + const second = await runCase({ |
| 379 | + name: "second", |
| 380 | + box: { w: 630, h: 252 }, |
| 381 | + base: { x: settledPos?.x ?? 489, y: settledPos?.y ?? 195 }, |
| 382 | + // What the first drag committed: 900/630. |
| 383 | + liveScale: { x: 1.429, y: 1.429 }, |
| 384 | + drop: { w: 420, h: 168, x: 640, y: 260 }, |
| 385 | + }); |
| 386 | + expect(second.settled.x).toBeCloseTo(second.dropPoint.x, 0); |
| 387 | + expect(second.settled.y).toBeCloseTo(second.dropPoint.y, 0); |
| 388 | + expect(second.settled.w).toBeCloseTo(second.dropPoint.w, 0); |
| 389 | +}); |
0 commit comments