File size: 4,113 Bytes
3144483
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// FFmpeg exec tests cover command execution wrappers and error mapping.
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";

const { runExecMock, resolveSystemBinMock } = vi.hoisted(() => ({
  runExecMock: vi.fn(),
  resolveSystemBinMock: vi.fn(),
}));

vi.mock("../process/exec.js", () => ({
  runExec: runExecMock,
}));

vi.mock("../infra/resolve-system-bin.js", () => ({
  resolveSystemBin: resolveSystemBinMock,
}));

let parseFfprobeCodecAndSampleRate: typeof import("./ffmpeg-exec.js").parseFfprobeCodecAndSampleRate;
let resolveFfmpegBin: typeof import("./ffmpeg-exec.js").resolveFfmpegBin;
let runFfprobe: typeof import("./ffmpeg-exec.js").runFfprobe;

beforeAll(async () => {
  vi.resetModules();
  ({ parseFfprobeCodecAndSampleRate, resolveFfmpegBin, runFfprobe } =
    await import("./ffmpeg-exec.js"));
});

afterAll(() => {
  vi.doUnmock("../process/exec.js");
  vi.doUnmock("../infra/resolve-system-bin.js");
  vi.resetModules();
});

beforeEach(() => {
  runExecMock.mockReset();
  resolveSystemBinMock.mockReset();
  resolveSystemBinMock.mockReturnValue("/usr/bin/ffprobe");
});

describe("parseFfprobeCodecAndSampleRate", () => {
  function expectParsedCodecAndSampleRateCase(
    input: string,
    expected: { codec: string | null; sampleRateHz: number | null },
  ) {
    expect(parseFfprobeCodecAndSampleRate(input)).toEqual(expected);
  }

  it.each([
    {
      name: "normalizes codec casing and parses numeric sample rates",
      input: "Opus,48000\n",
      expected: {
        codec: "opus",
        sampleRateHz: 48_000,
      },
    },
    {
      name: "keeps codec when the sample rate is not numeric",
      input: "opus,not-a-number",
      expected: {
        codec: "opus",
        sampleRateHz: null,
      },
    },
    {
      name: "rejects partially numeric sample rates",
      input: "opus,48000hz",
      expected: {
        codec: "opus",
        sampleRateHz: null,
      },
    },
    {
      name: "rejects missing sample rates",
      input: "opus,",
      expected: {
        codec: "opus",
        sampleRateHz: null,
      },
    },
    {
      name: "rejects zero sample rates",
      input: "opus,0",
      expected: {
        codec: "opus",
        sampleRateHz: null,
      },
    },
    {
      name: "rejects signed sample rates",
      input: "opus,-48000",
      expected: {
        codec: "opus",
        sampleRateHz: null,
      },
    },
  ] as const)("$name", ({ input, expected }) => {
    expectParsedCodecAndSampleRateCase(input, expected);
  });
});

describe("runFfprobe", () => {
  it("passes stdin and limits through the canonical exec wrapper", async () => {
    const input = Buffer.from("audio");
    runExecMock.mockResolvedValue({ stdout: "ok", stderr: "" });

    await expect(
      runFfprobe(["pipe:0"], { input, timeoutMs: 1234, maxBufferBytes: 5678 }),
    ).resolves.toBe("ok");

    expect(runExecMock).toHaveBeenCalledWith("/usr/bin/ffprobe", ["pipe:0"], {
      input,
      logOutput: false,
      maxBuffer: 5678,
      timeoutMs: 1234,
    });
  });

  it("passes an inherited file descriptor through the canonical exec wrapper", async () => {
    runExecMock.mockResolvedValue({ stdout: "ok", stderr: "" });

    await expect(runFfprobe(["pipe:0"], { stdinFileDescriptor: 17 })).resolves.toBe("ok");
    expect(runExecMock).toHaveBeenCalledWith("/usr/bin/ffprobe", ["pipe:0"], {
      logOutput: false,
      maxBuffer: 10 * 1024 * 1024,
      stdinFileDescriptor: 17,
      timeoutMs: 10_000,
    });
  });

  it("preserves wrapper execution errors", async () => {
    const childError = new Error("ffprobe failed");
    runExecMock.mockRejectedValue(childError);

    await expect(runFfprobe(["pipe:0"], { input: Buffer.from("audio") })).rejects.toBe(childError);
  });
});

describe("resolveFfmpegBin", () => {
  it("resolves ffmpeg from trusted system paths", () => {
    resolveSystemBinMock.mockReturnValue("/usr/bin/ffmpeg");

    expect(resolveFfmpegBin()).toBe("/usr/bin/ffmpeg");
    expect(resolveSystemBinMock).toHaveBeenCalledWith("ffmpeg", { trust: "standard" });
  });
});