88
99import { describe , expect , it , vi } from "vitest" ;
1010import type { Page } from "puppeteer-core" ;
11- import { pollSubCompositionTimelines } from "./frameCapture.js" ;
11+ import { parseHTML } from "linkedom" ;
12+ import { AUTHORED_SOURCE_BEGIN , AUTHORED_SOURCE_END } from "@hyperframes/core" ;
13+ import { authorScriptsMayRegisterTimelines , pollSubCompositionTimelines } from "./frameCapture.js" ;
1214
15+ // A function argument is the author-script scan; these tests model pages whose scripts may register.
1316function makeMockPage ( evaluateResults : ( expr : string ) => unknown ) : Page {
1417 return {
15- evaluate : vi . fn ( async ( expr : string ) => evaluateResults ( expr ) ) ,
18+ evaluate : vi . fn ( async ( expr : string | ( ( ) => unknown ) ) =>
19+ typeof expr === "function" ? true : evaluateResults ( expr ) ,
20+ ) ,
1621 } as unknown as Page ;
1722}
1823
@@ -21,8 +26,8 @@ describe("pollSubCompositionTimelines fail-fast", () => {
2126 const page = makeMockPage ( ( ) => true ) ;
2227 const outcome = await pollSubCompositionTimelines ( page , 1_000 , { intervalMs : 10 } ) ;
2328 expect ( outcome ) . toBe ( "ready" ) ;
24- // Second evaluate is the __hfForceTimelineRebind call.
25- expect ( ( page . evaluate as ReturnType < typeof vi . fn > ) . mock . calls . length ) . toBe ( 2 ) ;
29+ // Script scan, readiness check, then the __hfForceTimelineRebind call.
30+ expect ( ( page . evaluate as ReturnType < typeof vi . fn > ) . mock . calls . length ) . toBe ( 3 ) ;
2631 } ) ;
2732
2833 it ( "bails after the grace window when a script resource failed to load" , async ( ) => {
@@ -127,3 +132,92 @@ describe("pollSubCompositionTimelines fail-fast", () => {
127132 expect ( outcome ) . toBe ( "ready" ) ;
128133 } ) ;
129134} ) ;
135+
136+ describe ( "pollSubCompositionTimelines when no author script can register a timeline" , ( ) => {
137+ it ( "is ready at once instead of waiting out the timeout" , async ( ) => {
138+ const evaluate = vi . fn ( async ( expr : string | ( ( ) => unknown ) ) =>
139+ typeof expr === "function"
140+ ? false
141+ : expr . includes ( "__hfForceTimelineRebind" )
142+ ? undefined
143+ : false ,
144+ ) ;
145+ const started = Date . now ( ) ;
146+ const outcome = await pollSubCompositionTimelines ( { evaluate } as unknown as Page , 60_000 , {
147+ intervalMs : 10 ,
148+ getScriptLoadFailures : ( ) => [ ] ,
149+ } ) ;
150+ expect ( outcome ) . toBe ( "ready" ) ;
151+ expect ( Date . now ( ) - started ) . toBeLessThan ( 2_000 ) ;
152+ } ) ;
153+ } ) ;
154+
155+ describe ( "authorScriptsMayRegisterTimelines" , ( ) => {
156+ const RUNTIME_ATTR = "data-hyperframes-preview-runtime" ;
157+
158+ async function scan ( html : string , files : Record < string , string > = { } ) : Promise < boolean > {
159+ const { document } = parseHTML ( html ) ;
160+ Object . defineProperty ( document , "readyState" , { value : "complete" } ) ;
161+ vi . stubGlobal ( "document" , document ) ;
162+ vi . stubGlobal ( "location" , new URL ( "http://localhost:3000/index.html" ) ) ;
163+ vi . stubGlobal ( "fetch" , async ( url : URL ) => ( {
164+ text : async ( ) => {
165+ const body = files [ url . pathname ] ;
166+ if ( body === undefined ) throw new Error ( "404" ) ;
167+ return body ;
168+ } ,
169+ } ) ) ;
170+ try {
171+ return await authorScriptsMayRegisterTimelines (
172+ RUNTIME_ATTR ,
173+ AUTHORED_SOURCE_BEGIN ,
174+ AUTHORED_SOURCE_END ,
175+ ) ;
176+ } finally {
177+ vi . unstubAllGlobals ( ) ;
178+ }
179+ }
180+
181+ it ( "is false for scripts that never mention a timeline registration" , async ( ) => {
182+ expect (
183+ await scan (
184+ `<body><script>draw()</script><template><script>animate()</script></template></body>` ,
185+ ) ,
186+ ) . toBe ( false ) ;
187+ } ) ;
188+
189+ it ( "reads only the authored part of a scoped wrapper, which mentions __timelines itself" , async ( ) => {
190+ const wrapped = ( source : string ) =>
191+ `<body><script>(function(){ window.__timelines = window.__timelines || {};\n${ AUTHORED_SOURCE_BEGIN } \n${ source } \n${ AUTHORED_SOURCE_END } \n})();</script></body>` ;
192+ expect ( await scan ( wrapped ( "draw()" ) ) ) . toBe ( false ) ;
193+ expect ( await scan ( wrapped ( "window.__timelines['a'] = tl" ) ) ) . toBe ( true ) ;
194+ } ) ;
195+
196+ it ( "ignores the runtime, which mentions __timelines itself" , async ( ) => {
197+ expect (
198+ await scan ( `<body><script ${ RUNTIME_ATTR } >window.__timelines = {}</script></body>` ) ,
199+ ) . toBe ( false ) ;
200+ } ) ;
201+
202+ it ( "is true when an inline script or a template script registers one" , async ( ) => {
203+ expect ( await scan ( `<body><script>window.__timelines["a"] = tl</script></body>` ) ) . toBe ( true ) ;
204+ expect (
205+ await scan ( `<body><template><script>window.__timelines.b = tl</script></template></body>` ) ,
206+ ) . toBe ( true ) ;
207+ } ) ;
208+
209+ it ( "reads same-origin scripts and answers true for ones it cannot read" , async ( ) => {
210+ expect ( await scan ( `<body><script src="/a.js"></script></body>` , { "/a.js" : "draw()" } ) ) . toBe (
211+ false ,
212+ ) ;
213+ expect (
214+ await scan ( `<body><script src="/a.js"></script></body>` , {
215+ "/a.js" : "window.__timelines.x = 1" ,
216+ } ) ,
217+ ) . toBe ( true ) ;
218+ expect ( await scan ( `<body><script src="/missing.js"></script></body>` ) ) . toBe ( true ) ;
219+ expect ( await scan ( `<body><script src="https://cdn.example.com/lib.js"></script></body>` ) ) . toBe (
220+ true ,
221+ ) ;
222+ } ) ;
223+ } ) ;
0 commit comments