@@ -2,6 +2,7 @@ import gsap from "gsap";
22import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
33import { initSandboxRuntimeModular } from "./init" ;
44import type { RuntimeTimelineLike } from "./types" ;
5+ import { STUDIO_MANUAL_EDIT_GESTURE_ATTR } from "../editing/draftMarkers" ;
56import { resetRuntimeFixtureDom } from "./runtimeSeekFixture.test-helpers" ;
67
78// Pins where a running runtime places a sub-composition's timeline, against what a fresh load does.
@@ -14,6 +15,7 @@ describe("runtime sub-composition placement", () => {
1415 afterEach ( ( ) => {
1516 window . __hfRuntimeTeardown ?.( ) ;
1617 vi . restoreAllMocks ( ) ;
18+ vi . unstubAllGlobals ( ) ;
1719 delete window . __player ;
1820 delete window . __playerReady ;
1921 delete window . __hf ;
@@ -98,6 +100,123 @@ describe("runtime sub-composition placement", () => {
98100 expect ( intro . startTime ( ) ) . toBe ( 2 ) ;
99101 } ) ;
100102
103+ /** Transport frames on demand; the clock moves a second per frame, past the change-service rate limit. */
104+ function stubFrames ( ) {
105+ const frames = new Map < number , FrameRequestCallback > ( ) ;
106+ let next = 0 ;
107+ vi . stubGlobal ( "requestAnimationFrame" , ( cb : FrameRequestCallback ) => {
108+ frames . set ( ++ next , cb ) ;
109+ return next ;
110+ } ) ;
111+ vi . stubGlobal ( "cancelAnimationFrame" , ( id : number ) => frames . delete ( id ) ) ;
112+ let now = Date . now ( ) ;
113+ vi . spyOn ( Date , "now" ) . mockImplementation ( ( ) => now ) ;
114+ return async ( count : number ) => {
115+ for ( let frame = 1 ; frame <= count ; frame ++ ) {
116+ now += 1000 ;
117+ await Promise . resolve ( ) ;
118+ const due = [ ...frames . values ( ) ] ;
119+ frames . clear ( ) ;
120+ for ( const run of due ) run ( frame * 16 ) ;
121+ }
122+ } ;
123+ }
124+
125+ /** A root with no timeline of its own around one scene clip, whose timeline slides `#s` from 0 to 100 over 4 s. */
126+ function loadWithoutRootTimeline ( rootMarkup = "" , rootLength = ` data-duration="10"` ) {
127+ document . body . innerHTML =
128+ `<div data-composition-id="main" data-root="true"${ rootLength } >${ rootMarkup } ` +
129+ `<div id="host" class="clip" data-composition-id="scene" data-start="1" data-duration="4">` +
130+ `<div id="s"></div></div></div>` ;
131+ const scene = gsap
132+ . timeline ( { paused : true } )
133+ . to ( "#s" , { x : 100 , duration : 4 , ease : "none" } , 0 ) ;
134+ window . __timelines = { scene } as unknown as Record < string , RuntimeTimelineLike > ;
135+ initSandboxRuntimeModular ( ) ;
136+ return scene ;
137+ }
138+
139+ it ( "keeps a nested scene where the playhead left it during a drag when the root has no timeline" , async ( ) => {
140+ const runFrames = stubFrames ( ) ;
141+ loadWithoutRootTimeline ( ) ;
142+ window . __player ?. seek ( 4 ) ;
143+ expect ( shownX ( ) ) . toBe ( 75 ) ;
144+
145+ // Every transform GSAP writes while the drag is held, including ones a later write in the same tick hides.
146+ const shown : string [ ] = [ ] ;
147+ const writes = new MutationObserver ( ( records ) =>
148+ shown . push ( ...records . map ( ( r ) => r . oldValue ?? "" ) ) ,
149+ ) ;
150+ writes . observe ( document . getElementById ( "s" ) ! , {
151+ attributes : true ,
152+ attributeFilter : [ "style" ] ,
153+ attributeOldValue : true ,
154+ } ) ;
155+ // A Studio press marks the element; a timing edit then wakes the transport's rebind mid-drag.
156+ document . getElementById ( "s" ) ! . setAttribute ( STUDIO_MANUAL_EDIT_GESTURE_ATTR , "gesture-1:move" ) ;
157+ document . getElementById ( "host" ) ! . setAttribute ( "data-duration" , "4" ) ;
158+ await runFrames ( 30 ) ;
159+
160+ shown . push (
161+ ...writes . takeRecords ( ) . map ( ( r ) => r . oldValue ?? "" ) ,
162+ document . getElementById ( "s" ) ! . getAttribute ( "style" ) ?? "" ,
163+ ) ;
164+ writes . disconnect ( ) ;
165+ const xs = shown . map ( ( style ) => / t r a n s l a t e (?: 3 d ) ? \( ( [ - \d . ] + ) / . exec ( style ) ?. [ 1 ] ) . filter ( Boolean ) ;
166+ expect ( xs . length ) . toBeGreaterThan ( 0 ) ;
167+ expect ( new Set ( xs ) ) . toEqual ( new Set ( [ "75" ] ) ) ;
168+ } ) ;
169+
170+ it ( "keeps a dragged scene where the playhead left it when another scene registers mid-drag" , async ( ) => {
171+ const runFrames = stubFrames ( ) ;
172+ loadWithoutRootTimeline (
173+ `<div id="later" class="clip" data-composition-id="later" data-start="0" data-duration="2"><div id="l"></div></div>` ,
174+ ) ;
175+ window . __player ?. seek ( 4 ) ;
176+ document . getElementById ( "s" ) ! . setAttribute ( STUDIO_MANUAL_EDIT_GESTURE_ATTR , "gesture-1:move" ) ;
177+ ( window . __timelines as Record < string , unknown > ) . later = gsap
178+ . timeline ( { paused : true } )
179+ . to ( "#l" , { x : 100 , duration : 2 , ease : "none" } , 0 ) ;
180+ await runFrames ( 30 ) ;
181+
182+ expect ( shownX ( ) ) . toBe ( 75 ) ;
183+ } ) ;
184+
185+ it ( "reports a scene's new length after a script extends it, when the root declares none" , async ( ) => {
186+ const runFrames = stubFrames ( ) ;
187+ const scene = loadWithoutRootTimeline ( "" , "" ) ;
188+ await runFrames ( 3 ) ;
189+ window . __player ?. seek ( 2 ) ;
190+ scene . to ( { } , { duration : 8 } , 0 ) ;
191+ await runFrames ( 5 ) ;
192+
193+ expect ( window . __player ?. getDuration ( ) ) . toBe ( 9 ) ;
194+ } ) ;
195+
196+ it ( "binds a root timeline that registers after the runtime started on its children" , async ( ) => {
197+ const runFrames = stubFrames ( ) ;
198+ loadWithoutRootTimeline ( `<div id="r"></div>` ) ;
199+ const root = gsap
200+ . timeline ( { paused : true } )
201+ . to ( "#r" , { x : 100 , duration : 10 , ease : "none" } , 0 ) ;
202+ ( window . __timelines as Record < string , unknown > ) . main = root ;
203+ await runFrames ( 30 ) ;
204+
205+ window . __player ?. seek ( 5 ) ;
206+ expect ( Number ( gsap . getProperty ( "#r" , "x" ) ) ) . toBe ( 50 ) ;
207+ expect ( shownX ( ) ) . toBe ( 100 ) ;
208+ } ) ;
209+
210+ it ( "places a scene at its host's new start on a rebind when the root has no timeline" , ( ) => {
211+ const scene = loadWithoutRootTimeline ( ) ;
212+ document . getElementById ( "host" ) ! . setAttribute ( "data-start" , "4" ) ;
213+ window . __hfForceTimelineRebind ?.( ) ;
214+
215+ expect ( scene . startTime ( ) ) . toBe ( 4 ) ;
216+ window . __player ?. seek ( 5 ) ;
217+ expect ( shownX ( ) ) . toBe ( 25 ) ;
218+ } ) ;
219+
101220 it ( "leaves an unmoved scene in place on a rebind, whatever its start rounds to" , ( ) => {
102221 const { root, scene } = load ( "0.33333333" ) ;
103222 const remove = vi . spyOn ( root , "remove" ) ;
0 commit comments