|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + clampFadesToDuration, |
| 4 | + fadeGain, |
| 5 | + formatFadeSeconds, |
| 6 | + readElementFades, |
| 7 | + readFadeSeconds, |
| 8 | +} from "./audioFade.js"; |
| 9 | + |
| 10 | +describe("readFadeSeconds", () => { |
| 11 | + it("reads a positive number and treats everything else as no fade", () => { |
| 12 | + expect(readFadeSeconds("0.5")).toBe(0.5); |
| 13 | + expect(readFadeSeconds("2")).toBe(2); |
| 14 | + expect(readFadeSeconds(null)).toBe(0); |
| 15 | + expect(readFadeSeconds("")).toBe(0); |
| 16 | + expect(readFadeSeconds("-1")).toBe(0); |
| 17 | + expect(readFadeSeconds("NaN")).toBe(0); |
| 18 | + expect(readFadeSeconds("abc")).toBe(0); |
| 19 | + }); |
| 20 | + |
| 21 | + it("reads both attributes off an element", () => { |
| 22 | + const attrs: Record<string, string> = { "data-fade-in": "0.25", "data-fade-out": "1.5" }; |
| 23 | + expect(readElementFades({ getAttribute: (n) => attrs[n] ?? null })).toEqual({ |
| 24 | + fadeIn: 0.25, |
| 25 | + fadeOut: 1.5, |
| 26 | + }); |
| 27 | + expect(readElementFades({ getAttribute: () => null })).toEqual({ fadeIn: 0, fadeOut: 0 }); |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +describe("clampFadesToDuration", () => { |
| 32 | + it("leaves fades that fit alone", () => { |
| 33 | + expect(clampFadesToDuration({ fadeIn: 1, fadeOut: 2 }, 10)).toEqual({ fadeIn: 1, fadeOut: 2 }); |
| 34 | + }); |
| 35 | + it("scales fades that outrun the clip so they meet inside it", () => { |
| 36 | + expect(clampFadesToDuration({ fadeIn: 6, fadeOut: 6 }, 6)).toEqual({ fadeIn: 3, fadeOut: 3 }); |
| 37 | + expect(clampFadesToDuration({ fadeIn: 3, fadeOut: 1 }, 2)).toEqual({ |
| 38 | + fadeIn: 1.5, |
| 39 | + fadeOut: 0.5, |
| 40 | + }); |
| 41 | + }); |
| 42 | + it("keeps authored fades on a clip of unknown duration", () => { |
| 43 | + expect(clampFadesToDuration({ fadeIn: 4, fadeOut: 4 }, Number.POSITIVE_INFINITY)).toEqual({ |
| 44 | + fadeIn: 4, |
| 45 | + fadeOut: 4, |
| 46 | + }); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +describe("fadeGain", () => { |
| 51 | + const fades = { fadeIn: 2, fadeOut: 1 }; |
| 52 | + it("is unity with no fades", () => { |
| 53 | + expect(fadeGain(0, 10, { fadeIn: 0, fadeOut: 0 })).toBe(1); |
| 54 | + expect(fadeGain(10, 10, { fadeIn: 0, fadeOut: 0 })).toBe(1); |
| 55 | + }); |
| 56 | + it("ramps linearly from silence over the fade-in", () => { |
| 57 | + expect(fadeGain(0, 10, fades)).toBe(0); |
| 58 | + expect(fadeGain(1, 10, fades)).toBe(0.5); |
| 59 | + expect(fadeGain(2, 10, fades)).toBe(1); |
| 60 | + expect(fadeGain(5, 10, fades)).toBe(1); |
| 61 | + }); |
| 62 | + it("ramps linearly to silence over the fade-out, anchored at the clip end", () => { |
| 63 | + expect(fadeGain(9, 10, fades)).toBe(1); |
| 64 | + expect(fadeGain(9.5, 10, fades)).toBe(0.5); |
| 65 | + expect(fadeGain(10, 10, fades)).toBe(0); |
| 66 | + expect(fadeGain(11, 10, fades)).toBe(0); |
| 67 | + }); |
| 68 | + it("multiplies where the two fades overlap on a short clip", () => { |
| 69 | + // 2 s clip, both fades scaled to 1 s each: midpoint is the seam. |
| 70 | + expect(fadeGain(1, 2, { fadeIn: 2, fadeOut: 2 })).toBe(1); |
| 71 | + expect(fadeGain(0.5, 2, { fadeIn: 2, fadeOut: 2 })).toBe(0.5); |
| 72 | + }); |
| 73 | + it("never applies a fade-out to a clip of unknown duration", () => { |
| 74 | + expect(fadeGain(100, Number.POSITIVE_INFINITY, fades)).toBe(1); |
| 75 | + expect(fadeGain(1, Number.POSITIVE_INFINITY, fades)).toBe(0.5); |
| 76 | + }); |
| 77 | + it("treats 0, negative, and NaN fades as no fade", () => { |
| 78 | + expect(fadeGain(0, 10, { fadeIn: 0, fadeOut: 0 })).toBe(1); |
| 79 | + expect(fadeGain(0, 10, { fadeIn: -1, fadeOut: -4 })).toBe(1); |
| 80 | + expect(fadeGain(0, 10, { fadeIn: Number.NaN, fadeOut: Number.NaN })).toBe(1); |
| 81 | + }); |
| 82 | + it("returns a finite 0..1 gain for NaN elapsed and for fades longer than the clip", () => { |
| 83 | + expect(fadeGain(Number.NaN, 10, fades)).toBe(1); |
| 84 | + const overlapping = fadeGain(0.5, 2, { fadeIn: 10, fadeOut: 10 }); |
| 85 | + expect(overlapping).toBeGreaterThanOrEqual(0); |
| 86 | + expect(overlapping).toBeLessThanOrEqual(1); |
| 87 | + expect(Number.isFinite(overlapping)).toBe(true); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +describe("formatFadeSeconds", () => { |
| 92 | + it("writes short attribute text", () => { |
| 93 | + expect(formatFadeSeconds(0)).toBe("0"); |
| 94 | + expect(formatFadeSeconds(1)).toBe("1"); |
| 95 | + expect(formatFadeSeconds(0.5)).toBe("0.5"); |
| 96 | + expect(formatFadeSeconds(0.333)).toBe("0.33"); |
| 97 | + expect(formatFadeSeconds(-2)).toBe("0"); |
| 98 | + }); |
| 99 | +}); |
0 commit comments