@@ -13,11 +13,11 @@ import sharp from 'sharp'
1313import { createServer } from 'vite'
1414
1515const repoRoot = path . resolve ( import . meta. dirname , '..' )
16- const motion = '/images/hero-palm-cutout.webp '
16+ const motion = '/images/hero-palm-loop.mp4 '
1717const motionFile = path . join ( repoRoot , 'public' , motion )
1818const stillFile = path . join (
1919 repoRoot ,
20- 'src/components/home/hero-palm-cutout -still.webp' ,
20+ 'src/components/home/hero-palm-loop -still.webp' ,
2121)
2222const stillPrefix = 'data:image/webp;base64,'
2323const vite = await createServer ( {
@@ -45,46 +45,23 @@ const chromePath = [
4545 chromium . executablePath ( ) ,
4646] . find ( ( candidate ) => candidate && fs . existsSync ( candidate ) )
4747
48- test ( 'the palm loop stays at 24 fps, matches its still, and fits the media budget' , async ( ) => {
49- const animation = await sharp ( motionFile ) . metadata ( )
50- const poster = await sharp ( stillFile ) . metadata ( )
51- // The source has 121 frames at 24 fps. WebP stores whole milliseconds.
52- const delays = Array . from (
53- { length : 121 } ,
54- ( _ , i ) => Math . round ( ( ( i + 1 ) * 1000 ) / 24 ) - Math . round ( ( i * 1000 ) / 24 ) ,
55- )
56- assert . deepEqual ( animation . delay , delays )
57- // The MP4 and its posters transferred 1.91 MB in the mobile baseline.
48+ test ( 'the palm video and still fit the media budget' , async ( ) => {
5849 assert . ok (
5950 fs . statSync ( motionFile ) . size + fs . statSync ( stillFile ) . size < 1_900_000 ,
6051 'the combined palm assets should stay under 1.90 MB' ,
6152 )
62- assert . equal ( animation . loop , 0 )
63- assert . ok ( animation . hasAlpha && poster . hasAlpha )
64- assert . equal ( animation . width , poster . width )
65- assert . equal ( animation . height , poster . height )
66- const firstFrame = await sharp ( motionFile ) . ensureAlpha ( ) . raw ( ) . toBuffer ( )
67- const stillFrame = await sharp ( stillFile ) . ensureAlpha ( ) . raw ( ) . toBuffer ( )
68- assert . ok (
69- firstFrame . equals ( stillFrame ) ,
70- 'the still must match the first frame' ,
71- )
53+ const poster = await sharp ( stillFile ) . metadata ( )
54+ assert . equal ( poster . hasAlpha , false )
7255} )
7356
74- test ( 'the server inlines one visible still and preloads motion when allowed ' , ( ) => {
57+ test ( 'the server inlines one visible still' , ( ) => {
7558 assert . doesNotMatch ( serverMarkup , / < s o u r c e \b | < v i d e o \b / )
7659 assert . equal ( serverMarkup . match ( / < i m g \b / g) ?. length , 1 )
7760 assert . ok (
7861 serverMarkup . includes (
7962 `src="${ stillPrefix } ${ fs . readFileSync ( stillFile , 'base64' ) } "` ,
8063 ) ,
8164 )
82- const [ preload , ...extraPreloads ] =
83- serverMarkup . match ( / < l i n k [ ^ > ] + r e l = " p r e l o a d " [ ^ > ] * > / g) ?? [ ]
84- assert . ok ( preload )
85- assert . equal ( extraPreloads . length , 0 )
86- assert . ok ( preload . includes ( `href="${ motion } "` ) )
87- assert . match ( preload , / m e d i a = " \( p r e f e r s - r e d u c e d - m o t i o n : n o - p r e f e r e n c e \) " / )
8865} )
8966
9067test (
@@ -135,7 +112,7 @@ test(
135112 if ( url . pathname === '/' ) {
136113 return route . fulfill ( {
137114 contentType : 'text/html' ,
138- body : `<!doctype html><div id="root">${ serverMarkup } </div>` ,
115+ body : `<!doctype html><style>.opacity-0{opacity:0}</style>< div id="root">${ serverMarkup } </div>` ,
139116 } )
140117 }
141118 requests . push ( url . pathname )
@@ -157,12 +134,12 @@ test(
157134 requests,
158135 hydrate,
159136 still : page . locator ( `img[src^="${ stillPrefix } "]` ) ,
160- motion : page . locator ( `img [src="${ motion } "]` ) ,
137+ motion : page . locator ( `video [src="${ motion } "]` ) ,
161138 releaseAnimation : ( ) => animation . emit ( 'release' ) ,
162139 }
163140 }
164141
165- for ( const width of [ 390 , 844 , 1024 , 1512 ] ) {
142+ for ( const width of [ 390 , 1512 ] ) {
166143 const hero = await openHero ( width )
167144 await hero . still . waitFor ( { state : 'visible' } )
168145 assert . ok (
@@ -172,26 +149,34 @@ test(
172149 )
173150 await hero . hydrate ( )
174151 await hero . motion . waitFor ( { state : 'attached' } )
175- assert . equal ( await hero . motion . isHidden ( ) , true )
176- assert . equal ( await hero . still . isVisible ( ) , true )
152+ assert . equal (
153+ await hero . motion . evaluate ( ( video ) => getComputedStyle ( video ) . opacity ) ,
154+ '0' ,
155+ )
177156 hero . releaseAnimation ( )
178- await hero . motion . waitFor ( { state : 'visible' } )
179- assert . deepEqual ( hero . requests , [ motion ] )
180- assert . equal ( await hero . still . isHidden ( ) , true )
157+ await hero . page . waitForFunction ( ( ) => {
158+ const video = document . querySelector ( 'video' )
159+ return (
160+ video &&
161+ video . currentTime > 0 &&
162+ getComputedStyle ( video ) . opacity === '1'
163+ )
164+ } )
165+ assert . ok ( hero . requests . length > 0 )
166+ assert . ok ( hero . requests . every ( ( request ) => request === motion ) )
181167 await hero . page . close ( )
182168 }
183169
184170 const reduced = await openHero ( 390 , true )
185171 reduced . releaseAnimation ( )
186- await reduced . still . waitFor ( { state : 'visible' } )
187172 await reduced . hydrate ( )
188173 assert . equal ( await reduced . motion . count ( ) , 0 )
189174 assert . deepEqual ( reduced . requests , [ ] )
190175 await reduced . page . emulateMedia ( { reducedMotion : 'no-preference' } )
191- await reduced . motion . waitFor ( { state : 'visible ' } )
176+ await reduced . motion . waitFor ( { state : 'attached ' } )
192177 await reduced . page . emulateMedia ( { reducedMotion : 'reduce' } )
193- await reduced . still . waitFor ( { state : 'visible ' } )
194- assert . equal ( await reduced . motion . isHidden ( ) , true )
178+ await reduced . motion . waitFor ( { state : 'detached ' } )
179+ assert . equal ( await reduced . still . isVisible ( ) , true )
195180 await reduced . page . close ( )
196181
197182 const failed = await openHero ( 1512 , false , true )
@@ -202,7 +187,10 @@ test(
202187 )
203188 failed . releaseAnimation ( )
204189 await requestFailed
205- assert . equal ( await failed . motion . isHidden ( ) , true )
190+ assert . equal (
191+ await failed . motion . evaluate ( ( video ) => getComputedStyle ( video ) . opacity ) ,
192+ '0' ,
193+ )
206194 assert . equal ( await failed . still . isVisible ( ) , true )
207195 await failed . page . close ( )
208196 } ,
0 commit comments