From 83ea025ed8168456ab3573d3397efbdeb6f5c235 Mon Sep 17 00:00:00 2001 From: Enriquefft Date: Sun, 12 Apr 2026 18:17:50 -0500 Subject: [PATCH] fix: handle NaN in zero-scan fallback and symmetric divergence check - validateDuration returns 0 instead of NaN when both container is NaN and scanned is zero - Use Math.abs for divergence check so container under-reporting is also corrected (not just over-reporting) --- src/lib/exporter/streamingDecoder.test.ts | 8 ++++++++ src/lib/exporter/streamingDecoder.ts | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/lib/exporter/streamingDecoder.test.ts b/src/lib/exporter/streamingDecoder.test.ts index e3a0ecc..55b9123 100644 --- a/src/lib/exporter/streamingDecoder.test.ts +++ b/src/lib/exporter/streamingDecoder.test.ts @@ -27,9 +27,17 @@ describe("validateDuration", () => { expect(validateDuration(15.0, 15.3)).toBe(15.0); }); + it("returns scanned duration when container under-reports beyond threshold", () => { + expect(validateDuration(10, 15.3)).toBe(15.3); + }); + it("returns container duration when scanned is zero (corrupted/empty file)", () => { expect(validateDuration(10, 0)).toBe(10); }); + + it("returns 0 when both container is NaN and scanned is zero", () => { + expect(validateDuration(NaN, 0)).toBe(0); + }); }); describe("shouldFailDecodeEndedEarly", () => { diff --git a/src/lib/exporter/streamingDecoder.ts b/src/lib/exporter/streamingDecoder.ts index cc5ded5..b0866f5 100644 --- a/src/lib/exporter/streamingDecoder.ts +++ b/src/lib/exporter/streamingDecoder.ts @@ -86,12 +86,12 @@ export function validateDuration(containerDuration: number, scannedDuration: num if (scannedDuration <= 0) { // Zero scanned duration means corrupted/empty file — fall back to container // (downstream shouldFailDecodeEndedEarly will catch truly empty files) - return Math.max(containerDuration, 0); + return Number.isFinite(containerDuration) ? Math.max(containerDuration, 0) : 0; } if (!Number.isFinite(containerDuration) || containerDuration <= 0) { return scannedDuration; } - if (containerDuration - scannedDuration > DURATION_DIVERGENCE_THRESHOLD_SEC) { + if (Math.abs(containerDuration - scannedDuration) > DURATION_DIVERGENCE_THRESHOLD_SEC) { return scannedDuration; } return containerDuration;