|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +const timingModule = await import("./authoredTiming.js").catch(() => null); |
| 4 | + |
| 5 | +describe("resolveAuthoredTimingWindow", () => { |
| 6 | + const resolve = (values: { |
| 7 | + start?: string | null; |
| 8 | + duration?: string | null; |
| 9 | + authoredDuration?: string | null; |
| 10 | + end?: string | null; |
| 11 | + authoredEnd?: string | null; |
| 12 | + }) => { |
| 13 | + expect(timingModule, "canonical authored timing helper must exist").not.toBeNull(); |
| 14 | + return timingModule?.resolveAuthoredTimingWindow(values) ?? null; |
| 15 | + }; |
| 16 | + |
| 17 | + it("uses public timing before preserved timing and duration before end", () => { |
| 18 | + expect( |
| 19 | + resolve({ |
| 20 | + start: "2", |
| 21 | + duration: "3", |
| 22 | + authoredDuration: "4", |
| 23 | + end: "20", |
| 24 | + authoredEnd: "30", |
| 25 | + }), |
| 26 | + ).toEqual({ start: 2, duration: 3, end: 5 }); |
| 27 | + }); |
| 28 | + |
| 29 | + it("falls back through preserved duration, public end, and preserved end", () => { |
| 30 | + expect(resolve({ start: "1", duration: "", authoredDuration: "2.5" })).toEqual({ |
| 31 | + start: 1, |
| 32 | + duration: 2.5, |
| 33 | + end: 3.5, |
| 34 | + }); |
| 35 | + expect(resolve({ start: "1", duration: "0", end: "4", authoredEnd: "8" })).toEqual({ |
| 36 | + start: 1, |
| 37 | + duration: 3, |
| 38 | + end: 4, |
| 39 | + }); |
| 40 | + expect(resolve({ start: "1", duration: "-1", end: "NaN", authoredEnd: "6" })).toEqual({ |
| 41 | + start: 1, |
| 42 | + duration: 5, |
| 43 | + end: 6, |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it("rejects unusable values and preserves a usable start-only window", () => { |
| 48 | + expect(resolve({ start: null, duration: "2" })).toBeNull(); |
| 49 | + expect(resolve({ start: "Infinity", duration: "2" })).toBeNull(); |
| 50 | + expect(resolve({ start: "1.25", duration: "NaN", end: "1.25" })).toEqual({ |
| 51 | + start: 1.25, |
| 52 | + duration: null, |
| 53 | + end: null, |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it.each([ |
| 58 | + ["blank public duration", { start: "0", duration: " ", authoredDuration: "2" }, 2], |
| 59 | + ["invalid public duration", { start: "0", duration: "NaN", authoredDuration: "2" }, 2], |
| 60 | + ["infinite public duration", { start: "0", duration: "Infinity", authoredDuration: "2" }, 2], |
| 61 | + ["zero public duration", { start: "0", duration: "0", authoredDuration: "2" }, 2], |
| 62 | + ["negative public duration", { start: "0", duration: "-2", authoredDuration: "2" }, 2], |
| 63 | + ])("uses a usable preserved duration after %s", (_label, values, expectedEnd) => { |
| 64 | + expect(resolve(values)).toEqual({ start: 0, duration: 2, end: expectedEnd }); |
| 65 | + }); |
| 66 | + |
| 67 | + it("uses preserved end when public end does not create a positive window", () => { |
| 68 | + expect(resolve({ start: "4", end: "3", authoredEnd: "5.5" })).toEqual({ |
| 69 | + start: 4, |
| 70 | + duration: 1.5, |
| 71 | + end: 5.5, |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it("clamps a finite negative absolute start before deriving the window", () => { |
| 76 | + expect(resolve({ start: "-1", duration: "3" })).toEqual({ |
| 77 | + start: 0, |
| 78 | + duration: 3, |
| 79 | + end: 3, |
| 80 | + }); |
| 81 | + }); |
| 82 | +}); |
0 commit comments