11import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
2- import { RenderTooSlowError , checkRenderEta , startRenderEtaGuard } from "./renderEtaGuard.js" ;
2+ import {
3+ RenderTooSlowError ,
4+ checkCalibratedRenderEta ,
5+ checkRenderEta ,
6+ startRenderEtaGuard ,
7+ } from "./renderEtaGuard.js" ;
38
49const RENDER_TOO_SLOW = "RENDER_TOO_SLOW" ;
510
611describe ( "checkRenderEta" , ( ) => {
712 const base = { maxRenderMs : 100_000 , elapsedMs : 0 , framesCompleted : 0 , totalFrames : 100 } ;
813
9- it ( "passes at the 1. 2x margin and fails just over it" , ( ) => {
10- expect ( checkRenderEta ( "capture" , { ...base , msPerFrame : 1_200 } ) ) . toBeUndefined ( ) ;
11- const error = checkRenderEta ( "capture" , { ...base , msPerFrame : 1_201 } ) ;
14+ it ( "passes at the 2x margin and fails just over it" , ( ) => {
15+ expect ( checkRenderEta ( "capture" , { ...base , msPerFrame : 2_000 } ) ) . toBeUndefined ( ) ;
16+ const error = checkRenderEta ( "capture" , { ...base , msPerFrame : 2_001 } ) ;
1217 expect ( error ) . toBeInstanceOf ( RenderTooSlowError ) ;
1318 expect ( error ?. message ) . toMatch ( / ^ r e n d e r t o o s l o w : / ) ;
14- expect ( error ?. publicMetadata ) . toMatchObject ( { stage : "capture" , etaMs : 120_100 } ) ;
19+ expect ( error ?. publicMetadata ) . toMatchObject ( { stage : "capture" , etaMs : 200_100 } ) ;
1520 // The producer server reads these off the error to build the wire envelope.
1621 expect ( error ?. code ) . toBe ( RENDER_TOO_SLOW ) ;
1722 expect ( error ?. owner ) . toBe ( "user" ) ;
@@ -20,7 +25,7 @@ describe("checkRenderEta", () => {
2025
2126 it ( "counts time already spent" , ( ) => {
2227 expect (
23- checkRenderEta ( "capture" , { ...base , elapsedMs : 30_000 , msPerFrame : 1_000 } ) ,
28+ checkRenderEta ( "capture" , { ...base , elapsedMs : 110_000 , msPerFrame : 1_000 } ) ,
2429 ) . toBeInstanceOf ( RenderTooSlowError ) ;
2530 } ) ;
2631
@@ -44,7 +49,7 @@ describe("checkRenderEta", () => {
4449 msPerFrame : 784 / 4 ,
4550 } ) ,
4651 ) . toBeUndefined ( ) ;
47- // 570 frames, 1 worker, p95 15.8 s: 150 min, caught before capture .
52+ // 570 frames, 1 worker, 15.8 s a frame: 160 min, so it runs to the attempt limit as before .
4853 expect (
4954 checkRenderEta ( "capture_calibration" , {
5055 maxRenderMs : budget ,
@@ -53,8 +58,60 @@ describe("checkRenderEta", () => {
5358 totalFrames : 570 ,
5459 msPerFrame : 15_800 / 1 ,
5560 } ) ,
61+ ) . toBeUndefined ( ) ;
62+ // Twice as many frames at that cost: over 5 h, caught before capture.
63+ expect (
64+ checkRenderEta ( "capture_calibration" , {
65+ maxRenderMs : budget ,
66+ elapsedMs : 600_000 ,
67+ framesCompleted : 0 ,
68+ totalFrames : 1_140 ,
69+ msPerFrame : 15_800 / 1 ,
70+ } ) ,
71+ ) . toBeInstanceOf ( RenderTooSlowError ) ;
72+ } ) ;
73+ } ) ;
74+
75+ describe ( "checkCalibratedRenderEta" , ( ) => {
76+ const budget = { maxRenderMs : 5_100_000 , elapsedMs : 15_000 } ;
77+
78+ it ( "lets a render through whose slowest calibration frame alone would refuse it" , ( ) => {
79+ // One slow calibration frame among fast ones: the slowest predicts about 3 h, the typical one under 1 h.
80+ const input = { maxRenderMs : 5_100_000 , elapsedMs : 33_000 , totalFrames : 1_800 , workerCount : 1 } ;
81+ expect ( checkCalibratedRenderEta ( { ...input , estimate : { medianMs : 1_421 } } ) ) . toBeUndefined ( ) ;
82+ expect (
83+ checkRenderEta ( "capture_calibration" , { ...input , framesCompleted : 0 , msPerFrame : 5_772 } ) ,
5684 ) . toBeInstanceOf ( RenderTooSlowError ) ;
5785 } ) ;
86+
87+ it ( "lets through a long many-worker render whose typical frame fits the budget" , ( ) => {
88+ const input = {
89+ maxRenderMs : 5_100_000 ,
90+ elapsedMs : 438_000 ,
91+ totalFrames : 61_092 ,
92+ workerCount : 6 ,
93+ } ;
94+ expect ( checkCalibratedRenderEta ( { ...input , estimate : { medianMs : 613 } } ) ) . toBeUndefined ( ) ;
95+ } ) ;
96+
97+ it ( "still refuses a render that would take hours on its typical frame" , ( ) => {
98+ const tooSlow = checkCalibratedRenderEta ( {
99+ ...budget ,
100+ totalFrames : 7_890 ,
101+ workerCount : 1 ,
102+ estimate : { medianMs : 1_905 } ,
103+ } ) ;
104+ expect ( tooSlow ?. publicMetadata ) . toMatchObject ( {
105+ stage : "capture_calibration" ,
106+ totalFrames : 7_890 ,
107+ } ) ;
108+ } ) ;
109+
110+ it ( "skips the check without a calibration estimate" , ( ) => {
111+ expect (
112+ checkCalibratedRenderEta ( { ...budget , totalFrames : 2_145 , workerCount : 1 } ) ,
113+ ) . toBeUndefined ( ) ;
114+ } ) ;
58115} ) ;
59116
60117describe ( "startRenderEtaGuard" , ( ) => {
0 commit comments