1- import { readFileSync } from "node:fs" ;
2- import { describe , expect , it } from "vitest" ;
3- import { remainingVideoDownloadTimeoutMs } from "./mediaCapture.js" ;
1+ import { mkdirSync , mkdtempSync , rmSync , writeFileSync } from "node:fs" ;
2+ import { tmpdir } from "node:os" ;
3+ import { join } from "node:path" ;
4+ import type { Browser , Page } from "puppeteer-core" ;
5+ import { afterEach , describe , expect , it , vi } from "vitest" ;
6+ import {
7+ captureVideoManifest ,
8+ remainingVideoDownloadTimeoutMs ,
9+ renderLottiePreviews ,
10+ saveLottieAnimations ,
11+ } from "./mediaCapture.js" ;
412
5- const source = readFileSync ( new URL ( "./mediaCapture.ts" , import . meta. url ) , "utf-8" ) ;
13+ const tempDirs : string [ ] = [ ] ;
14+
15+ function tempDir ( ) : string {
16+ const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-media-budget-" ) ) ;
17+ tempDirs . push ( dir ) ;
18+ return dir ;
19+ }
20+
21+ afterEach ( ( ) => {
22+ vi . useRealTimers ( ) ;
23+ vi . unstubAllGlobals ( ) ;
24+ for ( const dir of tempDirs ) rmSync ( dir , { recursive : true , force : true } ) ;
25+ tempDirs . length = 0 ;
26+ } ) ;
27+
28+ describe ( "Lottie capture budget" , ( ) => {
29+ it ( "does not start another Lottie fetch after the live budget expires" , async ( ) => {
30+ const dir = tempDir ( ) ;
31+ let remainingMs = 10_000 ;
32+ const fetchMock = vi . fn ( async ( ) => {
33+ remainingMs = 0 ;
34+ return new Response ( JSON . stringify ( { w : 100 , h : 100 , layers : [ ] } ) , { status : 200 } ) ;
35+ } ) ;
36+ vi . stubGlobal ( "fetch" , fetchMock ) ;
37+
38+ const saved = await saveLottieAnimations (
39+ [ { url : "https://one.example/a.json" } , { url : "https://two.example/b.json" } ] ,
40+ dir ,
41+ { remainingMs : ( ) => remainingMs } ,
42+ ) ;
43+
44+ expect ( saved ) . toBe ( 1 ) ;
45+ expect ( fetchMock ) . toHaveBeenCalledTimes ( 1 ) ;
46+ } ) ;
47+
48+ it ( "does not open another Lottie preview page after the live budget expires" , async ( ) => {
49+ const dir = tempDir ( ) ;
50+ const lottieDir = join ( dir , "assets" , "lottie" ) ;
51+ mkdirSync ( join ( dir , "extracted" ) , { recursive : true } ) ;
52+ mkdirSync ( lottieDir , { recursive : true } ) ;
53+ const lottie = JSON . stringify ( { w : 100 , h : 100 , fr : 30 , ip : 0 , op : 30 , layers : [ ] } ) ;
54+ writeFileSync ( join ( lottieDir , "a.json" ) , lottie ) ;
55+ writeFileSync ( join ( lottieDir , "b.json" ) , lottie ) ;
56+
57+ let remainingMs = 10_000 ;
58+ const previewPage = {
59+ setViewport : vi . fn ( async ( ) => undefined ) ,
60+ setContent : vi . fn ( async ( ) => undefined ) ,
61+ evaluate : vi . fn ( async ( ) => undefined ) ,
62+ waitForFunction : vi . fn ( async ( ) => undefined ) ,
63+ screenshot : vi . fn ( async ( ) => undefined ) ,
64+ close : vi . fn ( async ( ) => undefined ) ,
65+ } ;
66+ const newPage = vi . fn ( async ( ) => {
67+ remainingMs = 0 ;
68+ return previewPage ;
69+ } ) ;
70+ const browser = { newPage } as unknown as Browser ;
71+
72+ await renderLottiePreviews ( browser , lottieDir , dir , { remainingMs : ( ) => remainingMs } ) ;
73+
74+ expect ( newPage ) . toHaveBeenCalledTimes ( 1 ) ;
75+ expect ( previewPage . setViewport ) . not . toHaveBeenCalled ( ) ;
76+ expect ( previewPage . screenshot ) . not . toHaveBeenCalled ( ) ;
77+ } ) ;
78+ } ) ;
79+
80+ describe ( "video capture live budget" , ( ) => {
81+ it ( "does not preview or download when the budget expires during DOM sampling" , async ( ) => {
82+ vi . useFakeTimers ( ) ;
83+ vi . setSystemTime ( 0 ) ;
84+ const dir = tempDir ( ) ;
85+ mkdirSync ( join ( dir , "extracted" ) , { recursive : true } ) ;
86+ const descriptor = {
87+ src : "https://video.example/hero.mp4" ,
88+ filename : "hero.mp4" ,
89+ width : 640 ,
90+ height : 360 ,
91+ sourceWidth : 640 ,
92+ sourceHeight : 360 ,
93+ top : 0 ,
94+ left : 0 ,
95+ heading : "Hero" ,
96+ caption : "Demo" ,
97+ ariaLabel : "" ,
98+ } ;
99+ const screenshot = vi . fn ( async ( ) => Buffer . from ( "preview" ) ) ;
100+ const evaluate = vi . fn ( async ( expression : unknown ) =>
101+ typeof expression === "function" ? { x : 0 , y : 0 , width : 640 , height : 360 } : [ descriptor ] ,
102+ ) ;
103+ const page = { evaluate, screenshot } as unknown as Page ;
104+ const fetchMock = vi . fn (
105+ async ( ) =>
106+ new Response ( Buffer . alloc ( 2048 ) , {
107+ status : 200 ,
108+ headers : { "content-type" : "video/mp4" } ,
109+ } ) ,
110+ ) ;
111+ vi . stubGlobal ( "fetch" , fetchMock ) ;
112+
113+ const capture = captureVideoManifest ( page , dir , ( ) => { } , {
114+ sampleMs : 10_000 ,
115+ downloadBudgetMs : 10_000 ,
116+ remainingMs : ( ) => Math . max ( 0 , 1_000 - Date . now ( ) ) ,
117+ } ) ;
118+ await vi . runAllTimersAsync ( ) ;
119+ await capture ;
120+
121+ expect ( screenshot ) . not . toHaveBeenCalled ( ) ;
122+ expect ( fetchMock ) . not . toHaveBeenCalled ( ) ;
123+ } ) ;
124+ } ) ;
6125
7126describe ( "remainingVideoDownloadTimeoutMs" , ( ) => {
8127 it ( "caps a video request to the remaining capture budget" , ( ) => {
@@ -16,10 +135,4 @@ describe("remainingVideoDownloadTimeoutMs", () => {
16135 it ( "retains the existing per-request ceiling when more budget remains" , ( ) => {
17136 expect ( remainingVideoDownloadTimeoutMs ( 1_000 , 300_000 , 2_000 ) ) . toBe ( 120_000 ) ;
18137 } ) ;
19-
20- it ( "checks the aggregate budget before starting each preview/download iteration" , ( ) => {
21- expect ( source ) . toContain (
22- "if (remainingVideoDownloadTimeoutMs(dlStart, downloadBudgetMs) <= 0) break;" ,
23- ) ;
24- } ) ;
25138} ) ;
0 commit comments