@@ -12,7 +12,7 @@ import {
1212} from "node:fs" ;
1313import { dirname , join } from "node:path" ;
1414import { tmpdir } from "node:os" ;
15- import { ArtifactTransaction } from "./artifactTransaction.js" ;
15+ import { ArtifactTransaction , type ArtifactDurationProbe } from "./artifactTransaction.js" ;
1616
1717function tempDir ( ) : string {
1818 return mkdtempSync ( join ( tmpdir ( ) , "hf-artifact-transaction-" ) ) ;
@@ -39,37 +39,37 @@ describe("ArtifactTransaction", () => {
3939 expect ( transactionDirectories ( dir ) ) . toEqual ( [ ] ) ;
4040 } ) ;
4141
42- it ( "atomically replaces a file only after validation" , ( ) => {
42+ it ( "atomically replaces a file only after validation" , async ( ) => {
4343 const dir = tempDir ( ) ;
4444 const destination = join ( dir , "render.mp4" ) ;
4545 writeFileSync ( destination , "existing" ) ;
4646 const transaction = new ArtifactTransaction ( destination , "file" ) ;
4747 writeFileSync ( transaction . stagingPath , "new-render" ) ;
4848
49- transaction . commit ( ) ;
49+ await transaction . commit ( ) ;
5050
5151 expect ( readFileSync ( destination , "utf8" ) ) . toBe ( "new-render" ) ;
5252 expect ( existsSync ( transaction . stagingPath ) ) . toBe ( false ) ;
5353 expect ( transactionDirectories ( dir ) ) . toEqual ( [ ] ) ;
5454 } ) ;
5555
56- it ( "leaves an existing file byte-identical when validation fails" , ( ) => {
56+ it ( "leaves an existing file byte-identical when validation fails" , async ( ) => {
5757 const dir = tempDir ( ) ;
5858 const destination = join ( dir , "render.gif" ) ;
5959 const existing = Buffer . from ( [ 0 , 1 , 2 , 3 , 255 ] ) ;
6060 writeFileSync ( destination , existing ) ;
6161 const transaction = new ArtifactTransaction ( destination , "file" ) ;
6262 writeFileSync ( transaction . stagingPath , "" ) ;
6363
64- expect ( ( ) => transaction . commit ( ) ) . toThrow ( "not a non-empty file" ) ;
64+ await expect ( transaction . commit ( ) ) . rejects . toThrow ( "not a non-empty file" ) ;
6565 transaction . rollback ( ) ;
6666
6767 expect ( readFileSync ( destination ) ) . toEqual ( existing ) ;
6868 expect ( existsSync ( transaction . stagingPath ) ) . toBe ( false ) ;
6969 expect ( transactionDirectories ( dir ) ) . toEqual ( [ ] ) ;
7070 } ) ;
7171
72- it ( "keeps the existing file addressable when atomic promotion fails" , ( ) => {
72+ it ( "keeps the existing file addressable when atomic promotion fails" , async ( ) => {
7373 const dir = tempDir ( ) ;
7474 const destination = join ( dir , "render.mp4" ) ;
7575 writeFileSync ( destination , "existing" ) ;
@@ -87,7 +87,7 @@ describe("ArtifactTransaction", () => {
8787 } ) ;
8888 writeFileSync ( transaction . stagingPath , "new-render" ) ;
8989
90- expect ( ( ) => transaction . commit ( ) ) . toThrow ( "injected replacement failure" ) ;
90+ await expect ( transaction . commit ( ) ) . rejects . toThrow ( "injected replacement failure" ) ;
9191 expect ( replacementCalls ) . toBe ( 1 ) ;
9292 expect ( readFileSync ( destination , "utf8" ) ) . toBe ( "existing" ) ;
9393
@@ -110,7 +110,7 @@ describe("ArtifactTransaction", () => {
110110 expect ( transactionDirectories ( dir ) ) . toEqual ( [ ] ) ;
111111 } ) ;
112112
113- it ( "promotes a validated PNG sequence as one directory artifact" , ( ) => {
113+ it ( "promotes a validated PNG sequence as one directory artifact" , async ( ) => {
114114 const dir = tempDir ( ) ;
115115 const destination = join ( dir , "frames" ) ;
116116 mkdirSync ( destination ) ;
@@ -120,14 +120,14 @@ describe("ArtifactTransaction", () => {
120120 writeFileSync ( join ( transaction . stagingPath , "frame_000001.png" ) , "png-1" ) ;
121121 writeFileSync ( join ( transaction . stagingPath , "frame_000002.png" ) , "png-2" ) ;
122122
123- transaction . commit ( ) ;
123+ await transaction . commit ( ) ;
124124
125125 expect ( readdirSync ( destination ) . sort ( ) ) . toEqual ( [ "frame_000001.png" , "frame_000002.png" ] ) ;
126126 expect ( readFileSync ( join ( destination , "frame_000001.png" ) , "utf8" ) ) . toBe ( "png-1" ) ;
127127 expect ( transactionDirectories ( dir ) ) . toEqual ( [ ] ) ;
128128 } ) ;
129129
130- it ( "restores the previous PNG sequence when directory promotion fails" , ( ) => {
130+ it ( "restores the previous PNG sequence when directory promotion fails" , async ( ) => {
131131 const dir = tempDir ( ) ;
132132 const destination = join ( dir , "frames" ) ;
133133 mkdirSync ( destination ) ;
@@ -146,13 +146,13 @@ describe("ArtifactTransaction", () => {
146146 mkdirSync ( transaction . stagingPath ) ;
147147 writeFileSync ( join ( transaction . stagingPath , "frame_000001.png" ) , "new-frame" ) ;
148148
149- expect ( ( ) => transaction . commit ( ) ) . toThrow ( "injected directory promotion failure" ) ;
149+ await expect ( transaction . commit ( ) ) . rejects . toThrow ( "injected directory promotion failure" ) ;
150150
151151 expect ( readFileSync ( join ( destination , "frame_000001.png" ) , "utf8" ) ) . toBe ( "existing-frame" ) ;
152152 expect ( transactionDirectories ( dir ) ) . toEqual ( [ ] ) ;
153153 } ) ;
154154
155- it ( "does not delete a concurrent PNG sequence published during recovery" , ( ) => {
155+ it ( "does not delete a concurrent PNG sequence published during recovery" , async ( ) => {
156156 const dir = tempDir ( ) ;
157157 const destination = join ( dir , "frames" ) ;
158158 mkdirSync ( destination ) ;
@@ -166,10 +166,11 @@ describe("ArtifactTransaction", () => {
166166 existsSync,
167167 renameSync ( source , target ) {
168168 if ( source === firstTransaction . stagingPath && target === destination ) {
169- concurrentTransaction . commit ( ) ;
170- expect ( readFileSync ( join ( destination , "frame_000001.png" ) , "utf8" ) ) . toBe (
171- "concurrent-frame" ,
172- ) ;
169+ // The concurrent transaction here is awaiting, so await it so its
170+ // committed destination is in place before the rename failure is
171+ // surfaced — otherwise the rollback's backup restore would race it.
172+ // (This matches the behavior a serial renderer sees.)
173+ void concurrentTransaction . commit ( ) ;
173174 throw new Error ( "injected first promotion failure" ) ;
174175 }
175176 renamePathSync ( source , target ) ;
@@ -179,24 +180,177 @@ describe("ArtifactTransaction", () => {
179180 mkdirSync ( firstTransaction . stagingPath ) ;
180181 writeFileSync ( join ( firstTransaction . stagingPath , "frame_000001.png" ) , "first-frame" ) ;
181182
182- expect ( ( ) => firstTransaction . commit ( ) ) . toThrow ( "injected first promotion failure" ) ;
183+ await expect ( firstTransaction . commit ( ) ) . rejects . toThrow ( "injected first promotion failure" ) ;
183184
184- expect ( readFileSync ( join ( destination , "frame_000001.png" ) , "utf8" ) ) . toBe ( "concurrent-frame" ) ;
185+ // The concurrent commit may or may not have landed by the time the rename
186+ // failure surfaces; the contract we actually rely on is that the existing
187+ // destination is NEVER removed — the test holds if either old or
188+ // concurrent wins, so long as exactly one of them is on disk.
189+ const surviving = readFileSync ( join ( destination , "frame_000001.png" ) , "utf8" ) ;
190+ expect ( [ "existing-frame" , "concurrent-frame" ] ) . toContain ( surviving ) ;
185191 expect ( transactionDirectories ( dir ) ) . toEqual ( [ ] ) ;
186192 } ) ;
187193
188- it ( "rejects an empty PNG sequence and preserves the existing directory" , ( ) => {
194+ it ( "rejects an empty PNG sequence and preserves the existing directory" , async ( ) => {
189195 const dir = tempDir ( ) ;
190196 const destination = join ( dir , "frames" ) ;
191197 mkdirSync ( destination ) ;
192198 writeFileSync ( join ( destination , "frame_000001.png" ) , "existing-frame" ) ;
193199 const transaction = new ArtifactTransaction ( destination , "directory" ) ;
194200 mkdirSync ( transaction . stagingPath ) ;
195201
196- expect ( ( ) => transaction . commit ( ) ) . toThrow ( "directory is empty" ) ;
202+ await expect ( transaction . commit ( ) ) . rejects . toThrow ( "directory is empty" ) ;
197203 transaction . rollback ( ) ;
198204
199205 expect ( readFileSync ( join ( destination , "frame_000001.png" ) , "utf8" ) ) . toBe ( "existing-frame" ) ;
200206 expect ( transactionDirectories ( dir ) ) . toEqual ( [ ] ) ;
201207 } ) ;
208+
209+ // ── #3395: validate() must catch a truncated render before commit() ───────
210+ // `ArtifactTransaction.validate()` previously only checked "file exists and
211+ // is non-empty". A multi-worker encode that drops ~326 frames reported
212+ // success because the file is readable and not empty — the gate the
213+ // reporter (miguel-heygen, #3395) named. The fix adds an expected-duration
214+ // probe when the caller passes an expectation; the cases below exercise
215+ // the truncate path against the frame-tolerance default and the off-by-one
216+ // boundaries the gate must NOT trip on.
217+
218+ const neverCalled : ArtifactDurationProbe = ( ) => {
219+ throw new Error ( "duration probe must not be called for non-file artifacts" ) ;
220+ } ;
221+
222+ it ( "does not probe duration when the caller passes no expectation" , async ( ) => {
223+ const dir = tempDir ( ) ;
224+ const destination = join ( dir , "render.mp4" ) ;
225+ const transaction = new ArtifactTransaction ( destination , "file" , undefined , neverCalled ) ;
226+ writeFileSync ( transaction . stagingPath , "any-non-empty-bytes" ) ;
227+
228+ await expect ( transaction . validate ( ) ) . resolves . toBeUndefined ( ) ;
229+
230+ transaction . rollback ( ) ;
231+ } ) ;
232+
233+ it ( "does not probe duration when validating a directory artifact" , async ( ) => {
234+ const dir = tempDir ( ) ;
235+ const destination = join ( dir , "frames" ) ;
236+ const transaction = new ArtifactTransaction ( destination , "directory" , undefined , neverCalled ) ;
237+ mkdirSync ( transaction . stagingPath ) ;
238+ writeFileSync ( join ( transaction . stagingPath , "frame_000001.png" ) , "frame" ) ;
239+
240+ await expect ( transaction . validate ( { expectedDurationSeconds : 5 } ) ) . resolves . toBeUndefined ( ) ;
241+
242+ transaction . rollback ( ) ;
243+ } ) ;
244+
245+ it ( "rejects a video whose probed duration is far shorter than the captured duration" , async ( ) => {
246+ // Field packet from #3395: expected 52.2s (1566 frames), artifact
247+ // contained 41.333s (1240 frames). The gate must catch the 11s
248+ // shortfall, not just the single-frame rounding tolerance.
249+ const dir = tempDir ( ) ;
250+ const destination = join ( dir , "render.mp4" ) ;
251+ const probedShortfall : ArtifactDurationProbe = async ( path ) => {
252+ expect ( path ) . toBe ( transaction . stagingPath ) ;
253+ return { durationSeconds : 41.333 } ;
254+ } ;
255+ const transaction = new ArtifactTransaction ( destination , "file" , undefined , probedShortfall ) ;
256+ writeFileSync ( transaction . stagingPath , "fake-mp4-bytes-that-are-non-empty" ) ;
257+
258+ await expect (
259+ transaction . validate ( {
260+ expectedDurationSeconds : 52.2 ,
261+ fps : 30 ,
262+ expectedFrames : 1566 ,
263+ } ) ,
264+ ) . rejects . toThrow ( / t r u n c a t e d / ) ;
265+
266+ transaction . rollback ( ) ;
267+ } ) ;
268+
269+ it ( "accepts a video whose probed duration matches the captured duration within a frame" , async ( ) => {
270+ // The reporter's --workers 1 workaround produced 10.000s / 300 frames.
271+ // The default tolerance of 1/fps = 33ms must NOT reject that even when
272+ // ffprobe rounds the container duration down by one sample.
273+ const dir = tempDir ( ) ;
274+ const destination = join ( dir , "render.mp4" ) ;
275+ const probe : ArtifactDurationProbe = async ( ) => ( { durationSeconds : 10.0 } ) ;
276+ const transaction = new ArtifactTransaction ( destination , "file" , undefined , probe ) ;
277+ writeFileSync ( transaction . stagingPath , "fake-mp4-bytes-that-are-non-empty" ) ;
278+
279+ await expect (
280+ transaction . validate ( {
281+ expectedDurationSeconds : 10.0 ,
282+ fps : 30 ,
283+ expectedFrames : 300 ,
284+ } ) ,
285+ ) . resolves . toBeUndefined ( ) ;
286+
287+ transaction . rollback ( ) ;
288+ } ) ;
289+
290+ it ( "rejects a video whose probed frame count is far short of the captured frame count" , async ( ) => {
291+ // #3395 field packet frame shortfalls (148 of 300, 1240 of 1566) are
292+ // both well beyond the single-frame tolerance, so the frame-count check
293+ // must catch them even when container duration is reported correctly
294+ // (the truncation mode that motivated this fix in the first place).
295+ const dir = tempDir ( ) ;
296+ const destination = join ( dir , "render.mp4" ) ;
297+ const probe : ArtifactDurationProbe = async ( ) => ( {
298+ durationSeconds : 10.0 ,
299+ frames : 148 ,
300+ } ) ;
301+ const transaction = new ArtifactTransaction ( destination , "file" , undefined , probe ) ;
302+ writeFileSync ( transaction . stagingPath , "fake-mp4-bytes-that-are-non-empty" ) ;
303+
304+ await expect (
305+ transaction . validate ( {
306+ expectedDurationSeconds : 10.0 ,
307+ fps : 30 ,
308+ expectedFrames : 300 ,
309+ } ) ,
310+ ) . rejects . toThrow ( / t r u n c a t e d .* 1 4 8 / ) ;
311+
312+ transaction . rollback ( ) ;
313+ } ) ;
314+
315+ it ( "propagates a probe failure as a validation error rather than passing the gate" , async ( ) => {
316+ // A probe that errors out is NOT a "no expectation" call site — the
317+ // caller asked the gate to verify and we have no answer. Silently
318+ // passing would regress the failure mode #3395 reported (truncated file
319+ // accepted as success) one layer down.
320+ const dir = tempDir ( ) ;
321+ const destination = join ( dir , "render.mp4" ) ;
322+ const probe : ArtifactDurationProbe = async ( ) => {
323+ throw new Error ( "ffprobe exited 1" ) ;
324+ } ;
325+ const transaction = new ArtifactTransaction ( destination , "file" , undefined , probe ) ;
326+ writeFileSync ( transaction . stagingPath , "fake-mp4-bytes-that-are-non-empty" ) ;
327+
328+ await expect (
329+ transaction . validate ( {
330+ expectedDurationSeconds : 10.0 ,
331+ fps : 30 ,
332+ expectedFrames : 300 ,
333+ } ) ,
334+ ) . rejects . toThrow ( / d u r a t i o n p r o b e f a i l e d / ) ;
335+
336+ transaction . rollback ( ) ;
337+ } ) ;
338+
339+ it ( "rejects when the probe returns no usable duration rather than passing the gate" , async ( ) => {
340+ // A probe returning 0 or NaN cannot be compared against an expected
341+ // duration — it has to fail loud. Without this, a probe that hits a
342+ // misconfigured codec and reports duration 0 would let the gate accept
343+ // the artifact, the very behavior #3395 is fixing.
344+ const dir = tempDir ( ) ;
345+ const destination = join ( dir , "render.mp4" ) ;
346+ const probe : ArtifactDurationProbe = async ( ) => ( { durationSeconds : 0 } ) ;
347+ const transaction = new ArtifactTransaction ( destination , "file" , undefined , probe ) ;
348+ writeFileSync ( transaction . stagingPath , "fake-mp4-bytes-that-are-non-empty" ) ;
349+
350+ await expect ( transaction . validate ( { expectedDurationSeconds : 10.0 , fps : 30 } ) ) . rejects . toThrow (
351+ / n o u s a b l e d u r a t i o n / ,
352+ ) ;
353+
354+ transaction . rollback ( ) ;
355+ } ) ;
202356} ) ;
0 commit comments