1+ // fallow-ignore-file code-duplication
2+
13import type { ExtractedFrames , VideoElement , VideoMetadata } from "@hyperframes/engine" ;
24import { describe , expect , it } from "vitest" ;
35import {
@@ -35,9 +37,9 @@ function makeVideo(overrides: Partial<VideoElement> & { id: string }): VideoElem
3537function makeExtracted (
3638 videoId : string ,
3739 delivered : number ,
38- options : { fps ?: number ; durationSeconds ?: number } = { } ,
40+ options : { fps ?: number ; durationSeconds ?: number ; isVFR ?: boolean } = { } ,
3941) : ExtractedFrames {
40- const { fps = 30 , durationSeconds = Number . POSITIVE_INFINITY } = options ;
42+ const { fps = 30 , durationSeconds = Number . POSITIVE_INFINITY , isVFR = false } = options ;
4143 const framePaths = new Map < number , string > ( ) ;
4244 for ( let i = 0 ; i < delivered ; i += 1 ) framePaths . set ( i , `/tmp/${ videoId } /${ i } .jpg` ) ;
4345 const metadata : VideoMetadata = {
@@ -48,7 +50,7 @@ function makeExtracted(
4850 fps,
4951 videoCodec : "h264" ,
5052 hasAudio : false ,
51- isVFR : false ,
53+ isVFR,
5254 hasAlpha : false ,
5355 colorSpace : null ,
5456 } ;
@@ -72,9 +74,23 @@ describe("expectedFramesForClip", () => {
7274 expect ( expectedFramesForClip ( 2 , 1 , 30 ) ) . toBe ( 0 ) ; // negative window collapses to 0
7375 } ) ;
7476
75- it ( "ceils fractional-fps windows so a 29.97fps 1s clip demands 30 frames " , ( ) => {
77+ it ( "defaults to fail-closed ceil rounding " , ( ) => {
7678 expect ( expectedFramesForClip ( 0 , 1 , 29.97 ) ) . toBe ( 30 ) ;
7779 expect ( expectedFramesForClip ( 0 , 5 , 30 ) ) . toBe ( 150 ) ;
80+ expect ( expectedFramesForClip ( 0 , 0.616666 , 30 ) ) . toBe ( 19 ) ;
81+ expect ( expectedFramesForClip ( 0 , 0.316666 , 30 ) ) . toBe ( 10 ) ;
82+ } ) ;
83+
84+ it ( "matches the CFR fps filter's nearest-boundary rounding when requested" , ( ) => {
85+ expect ( expectedFramesForClip ( 0 , 0.616666 , 30 , "nearest" ) ) . toBe ( 18 ) ;
86+ expect ( expectedFramesForClip ( 0 , 0.316666 , 30 , "nearest" ) ) . toBe ( 9 ) ;
87+ expect ( expectedFramesForClip ( 0 , 0.633333 , 30 , "nearest" ) ) . toBe ( 19 ) ;
88+ } ) ;
89+
90+ it ( "requires one frame for every positive sub-frame clip" , ( ) => {
91+ expect ( expectedFramesForClip ( 0 , 0.001 , 30 ) ) . toBe ( 1 ) ;
92+ expect ( expectedFramesForClip ( 0 , 0.001 , 30 , "nearest" ) ) . toBe ( 1 ) ;
93+ expect ( expectedFramesForClip ( 1 , 1 , 30 ) ) . toBe ( 0 ) ;
7894 } ) ;
7995} ) ;
8096
@@ -123,6 +139,43 @@ describe("computeVideoFrameCoverage", () => {
123139 } ) ;
124140 } ) ;
125141
142+ it ( "reports full coverage for a short CFR clip matching FFmpeg boundary rounding" , ( ) => {
143+ const videos = [ makeVideo ( { id : "short" , start : 0 , end : 0.616666 } ) ] ;
144+ const reports = computeVideoFrameCoverage ( videos , [ makeExtracted ( "short" , 18 ) ] , 30 ) ;
145+ expect ( reports [ 0 ] ) . toMatchObject ( {
146+ expectedFrames : 18 ,
147+ capturedFrames : 18 ,
148+ ratio : 1 ,
149+ } ) ;
150+ expect ( ( ) => assertVideoFrameCoverage ( reports , 0.95 ) ) . not . toThrow ( ) ;
151+ } ) ;
152+
153+ it ( "keeps ceil coverage for the VFR extraction branch" , ( ) => {
154+ const videos = [ makeVideo ( { id : "short-vfr" , start : 0 , end : 0.616666 } ) ] ;
155+ const reports = computeVideoFrameCoverage (
156+ videos ,
157+ [ makeExtracted ( "short-vfr" , 18 , { isVFR : true } ) ] ,
158+ 30 ,
159+ ) ;
160+ expect ( reports [ 0 ] ) . toMatchObject ( {
161+ expectedFrames : 19 ,
162+ capturedFrames : 18 ,
163+ ratio : 18 / 19 ,
164+ } ) ;
165+ expect ( ( ) => assertVideoFrameCoverage ( reports , 0.95 ) ) . toThrow ( VideoFrameCoverageError ) ;
166+ } ) ;
167+
168+ it ( "still fails closed when a positive sub-frame clip captured zero frames" , ( ) => {
169+ const videos = [ makeVideo ( { id : "blank-sub-frame" , start : 0 , end : 0.001 } ) ] ;
170+ const reports = computeVideoFrameCoverage ( videos , [ makeExtracted ( "blank-sub-frame" , 0 ) ] , 30 ) ;
171+ expect ( reports [ 0 ] ) . toMatchObject ( {
172+ expectedFrames : 1 ,
173+ capturedFrames : 0 ,
174+ ratio : 0 ,
175+ } ) ;
176+ expect ( ( ) => assertVideoFrameCoverage ( reports , 0.95 ) ) . toThrow ( VideoFrameCoverageError ) ;
177+ } ) ;
178+
126179 it ( "reports 0 capturedFrames when a video was never extracted (injection failure)" , ( ) => {
127180 // Field signal ts=1784139267: later-injected video clips silently drop
128181 // out of the extractor under injection-count saturation.
0 commit comments