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;