|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { limitDeliveryPeaks } from "./audioDeliveryLimit.js"; |
| 3 | + |
| 4 | +const INPUT = { |
| 5 | + audioPath: "/w/audio.m4a", |
| 6 | + sourcePath: "/w/audio-source.wav", |
| 7 | + targetDurationSeconds: 8, |
| 8 | + timeoutMs: 1000, |
| 9 | +}; |
| 10 | +const LIMITED = "/w/audio-source-limited.wav"; |
| 11 | + |
| 12 | +function deps(peak: number) { |
| 13 | + return { |
| 14 | + measure: vi.fn(async () => peak), |
| 15 | + applyChain: vi.fn(async () => ({ path: LIMITED, envelopeBaked: false })), |
| 16 | + encode: vi.fn(async () => ({ success: true })), |
| 17 | + rename: vi.fn(), |
| 18 | + sourceExists: vi.fn(() => true), |
| 19 | + }; |
| 20 | +} |
| 21 | +type Deps = NonNullable<Parameters<typeof limitDeliveryPeaks>[1]>; |
| 22 | + |
| 23 | +describe("limitDeliveryPeaks", () => { |
| 24 | + it("leaves a mix under the ceiling alone, with no browser and no encode", async () => { |
| 25 | + const d = deps(-1); |
| 26 | + await expect(limitDeliveryPeaks(INPUT, d as unknown as Deps)).resolves.toEqual({ |
| 27 | + success: true, |
| 28 | + }); |
| 29 | + expect(d.applyChain).not.toHaveBeenCalled(); |
| 30 | + expect(d.encode).not.toHaveBeenCalled(); |
| 31 | + }); |
| 32 | + |
| 33 | + it("re-encodes a mix over the ceiling through a true-peak limit 2 dB under it", async () => { |
| 34 | + const d = deps(2.4); |
| 35 | + await limitDeliveryPeaks(INPUT, d as unknown as Deps); |
| 36 | + expect(d.applyChain).toHaveBeenCalledWith( |
| 37 | + INPUT.sourcePath, |
| 38 | + { |
| 39 | + version: 1, |
| 40 | + nodes: [{ type: "truepeak", id: "delivery", params: { ceiling: -3 } }], |
| 41 | + }, |
| 42 | + LIMITED, |
| 43 | + expect.anything(), |
| 44 | + ); |
| 45 | + expect(d.encode).toHaveBeenCalledWith(LIMITED, INPUT.audioPath, 8, expect.anything()); |
| 46 | + // The whole-mix cut behind this measures against the limited mix, not the original. |
| 47 | + expect(d.rename).toHaveBeenCalledWith(LIMITED, INPUT.sourcePath, { copyFallback: true }); |
| 48 | + }); |
| 49 | + |
| 50 | + it("leaves the first encode for the whole-mix cut when the limit cannot run, and says so", async () => { |
| 51 | + const d = deps(2.4); |
| 52 | + d.applyChain.mockRejectedValueOnce(new Error("no browser")); |
| 53 | + const warn = vi.fn(); |
| 54 | + await expect(limitDeliveryPeaks({ ...INPUT, warn }, d as unknown as Deps)).resolves.toEqual({ |
| 55 | + success: true, |
| 56 | + }); |
| 57 | + expect(warn).toHaveBeenCalledWith(expect.stringContaining("whole-mix"), { |
| 58 | + error: "no browser", |
| 59 | + }); |
| 60 | + expect(d.encode).not.toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("fails the render when the limited mix cannot be encoded", async () => { |
| 64 | + const d = deps(2.4); |
| 65 | + d.encode.mockResolvedValueOnce({ |
| 66 | + success: false, |
| 67 | + exitCode: 1, |
| 68 | + stderr: "encoder died", |
| 69 | + terminationReason: "exit", |
| 70 | + } as never); |
| 71 | + const result = await limitDeliveryPeaks(INPUT, d as unknown as Deps); |
| 72 | + expect(result.success).toBe(false); |
| 73 | + expect(result.error).toContain("audioDeliveryLimit"); |
| 74 | + expect(d.rename).not.toHaveBeenCalled(); |
| 75 | + }); |
| 76 | + |
| 77 | + it("does nothing when the mix left no source to limit", async () => { |
| 78 | + const d = deps(2.4); |
| 79 | + d.sourceExists.mockReturnValueOnce(false); |
| 80 | + await limitDeliveryPeaks(INPUT, d as unknown as Deps); |
| 81 | + expect(d.measure).not.toHaveBeenCalled(); |
| 82 | + }); |
| 83 | +}); |
0 commit comments