|
| 1 | +import { promisify } from "node:util"; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +interface ExecFileCall { |
| 5 | + file: string; |
| 6 | + args: readonly string[]; |
| 7 | +} |
| 8 | + |
| 9 | +type ExecFileOutcome = |
| 10 | + | { kind: "ok"; stdout: string; stderr?: string } |
| 11 | + | { kind: "exit_nonzero"; code: number; stdout?: string; stderr?: string } |
| 12 | + | { kind: "enoent" }; |
| 13 | + |
| 14 | +// Node's built-in `child_process.execFile` carries a `util.promisify.custom` |
| 15 | +// implementation that resolves to `{stdout, stderr}`. A plain-callback mock |
| 16 | +// without that Symbol would be promisified as a single-result function, so |
| 17 | +// `{stdout} = await execFileP(...)` would silently destructure to `undefined` |
| 18 | +// — the exact hazard psnr.ts documents. Stamp the custom impl on the mock so |
| 19 | +// promisify keeps the `{stdout, stderr}` shape. |
| 20 | +function createExecFileSpy(outcome: ExecFileOutcome): { |
| 21 | + execFile: ( |
| 22 | + file: string, |
| 23 | + args: readonly string[], |
| 24 | + options: unknown, |
| 25 | + callback: (err: Error | null, stdout?: string, stderr?: string) => void, |
| 26 | + ) => void; |
| 27 | + calls: ExecFileCall[]; |
| 28 | +} { |
| 29 | + const calls: ExecFileCall[] = []; |
| 30 | + |
| 31 | + async function run( |
| 32 | + file: string, |
| 33 | + args: readonly string[], |
| 34 | + ): Promise<{ stdout: string; stderr: string }> { |
| 35 | + calls.push({ file, args }); |
| 36 | + if (outcome.kind === "enoent") { |
| 37 | + const err = new Error("spawn ffmpeg ENOENT") as NodeJS.ErrnoException; |
| 38 | + err.code = "ENOENT"; |
| 39 | + throw err; |
| 40 | + } |
| 41 | + if (outcome.kind === "exit_nonzero") { |
| 42 | + const err = new Error(`Command failed: ffmpeg (exit ${outcome.code})`) as Error & { |
| 43 | + code: number; |
| 44 | + stdout?: string; |
| 45 | + stderr?: string; |
| 46 | + }; |
| 47 | + err.code = outcome.code; |
| 48 | + err.stdout = outcome.stdout ?? ""; |
| 49 | + err.stderr = outcome.stderr ?? ""; |
| 50 | + throw err; |
| 51 | + } |
| 52 | + return { stdout: outcome.stdout, stderr: outcome.stderr ?? "" }; |
| 53 | + } |
| 54 | + |
| 55 | + const execFile = (( |
| 56 | + file: string, |
| 57 | + args: readonly string[], |
| 58 | + _options: unknown, |
| 59 | + callback: (err: Error | null, stdout?: string, stderr?: string) => void, |
| 60 | + ) => { |
| 61 | + run(file, args).then( |
| 62 | + ({ stdout, stderr }) => process.nextTick(() => callback(null, stdout, stderr)), |
| 63 | + (err: Error) => process.nextTick(() => callback(err)), |
| 64 | + ); |
| 65 | + }) as (( |
| 66 | + file: string, |
| 67 | + args: readonly string[], |
| 68 | + options: unknown, |
| 69 | + callback: (err: Error | null, stdout?: string, stderr?: string) => void, |
| 70 | + ) => void) & { [key: symbol]: unknown }; |
| 71 | + (execFile as { [k: symbol]: unknown })[promisify.custom] = ( |
| 72 | + file: string, |
| 73 | + args: readonly string[], |
| 74 | + ) => run(file, args); |
| 75 | + |
| 76 | + return { execFile, calls }; |
| 77 | +} |
| 78 | + |
| 79 | +beforeEach(() => { |
| 80 | + vi.resetModules(); |
| 81 | +}); |
| 82 | + |
| 83 | +afterEach(() => { |
| 84 | + vi.doUnmock("node:child_process"); |
| 85 | +}); |
| 86 | + |
| 87 | +describe("isPsnrFilterAvailable", () => { |
| 88 | + it("returns true when `ffmpeg -filters` output lists the psnr filter", async () => { |
| 89 | + const { execFile } = createExecFileSpy({ |
| 90 | + kind: "ok", |
| 91 | + stdout: [ |
| 92 | + "Filters:", |
| 93 | + " T.. overlay VV->V Overlay a video source on top of the input.", |
| 94 | + " T.. psnr VV->V Calculate the PSNR between two video streams.", |
| 95 | + " ... yadif V->V Deinterlace the input image.", |
| 96 | + ].join("\n"), |
| 97 | + }); |
| 98 | + vi.doMock("node:child_process", () => ({ execFile })); |
| 99 | + |
| 100 | + const { isPsnrFilterAvailable } = await import("./psnrFilterAvailability.js"); |
| 101 | + await expect(isPsnrFilterAvailable()).resolves.toBe(true); |
| 102 | + }); |
| 103 | + |
| 104 | + it("returns false when `ffmpeg -filters` output omits the psnr filter", async () => { |
| 105 | + const { execFile } = createExecFileSpy({ |
| 106 | + kind: "ok", |
| 107 | + stdout: [ |
| 108 | + "Filters:", |
| 109 | + " T.. overlay VV->V Overlay a video source on top of the input.", |
| 110 | + " ... yadif V->V Deinterlace the input image.", |
| 111 | + ].join("\n"), |
| 112 | + }); |
| 113 | + vi.doMock("node:child_process", () => ({ execFile })); |
| 114 | + |
| 115 | + const { isPsnrFilterAvailable } = await import("./psnrFilterAvailability.js"); |
| 116 | + await expect(isPsnrFilterAvailable()).resolves.toBe(false); |
| 117 | + }); |
| 118 | + |
| 119 | + it("returns false when the ffmpeg binary is missing (ENOENT from execFile)", async () => { |
| 120 | + const { execFile } = createExecFileSpy({ kind: "enoent" }); |
| 121 | + vi.doMock("node:child_process", () => ({ execFile })); |
| 122 | + |
| 123 | + const { isPsnrFilterAvailable } = await import("./psnrFilterAvailability.js"); |
| 124 | + await expect(isPsnrFilterAvailable()).resolves.toBe(false); |
| 125 | + }); |
| 126 | + |
| 127 | + it("returns false on a non-zero exit from `ffmpeg -filters`", async () => { |
| 128 | + const { execFile } = createExecFileSpy({ |
| 129 | + kind: "exit_nonzero", |
| 130 | + code: 1, |
| 131 | + stderr: "Unrecognized option '-filters'.", |
| 132 | + }); |
| 133 | + vi.doMock("node:child_process", () => ({ execFile })); |
| 134 | + |
| 135 | + const { isPsnrFilterAvailable } = await import("./psnrFilterAvailability.js"); |
| 136 | + await expect(isPsnrFilterAvailable()).resolves.toBe(false); |
| 137 | + }); |
| 138 | + |
| 139 | + it("memoizes the probe across calls and re-probes after resetPsnrFilterAvailabilityCache", async () => { |
| 140 | + const { execFile, calls } = createExecFileSpy({ |
| 141 | + kind: "ok", |
| 142 | + stdout: " T.. psnr VV->V Calculate the PSNR", |
| 143 | + }); |
| 144 | + vi.doMock("node:child_process", () => ({ execFile })); |
| 145 | + |
| 146 | + const { isPsnrFilterAvailable, resetPsnrFilterAvailabilityCache } = |
| 147 | + await import("./psnrFilterAvailability.js"); |
| 148 | + |
| 149 | + await isPsnrFilterAvailable(); |
| 150 | + await isPsnrFilterAvailable(); |
| 151 | + await isPsnrFilterAvailable(); |
| 152 | + expect(calls.length).toBe(1); |
| 153 | + |
| 154 | + resetPsnrFilterAvailabilityCache(); |
| 155 | + await isPsnrFilterAvailable(); |
| 156 | + expect(calls.length).toBe(2); |
| 157 | + }); |
| 158 | + |
| 159 | + it("does not treat a whole-string 'psnr' inside another word as the filter", async () => { |
| 160 | + const { execFile } = createExecFileSpy({ |
| 161 | + kind: "ok", |
| 162 | + stdout: [ |
| 163 | + "Filters:", |
| 164 | + " T.. bpsnrx V->V (hypothetical extended filter, not the real psnr)", |
| 165 | + ].join("\n"), |
| 166 | + }); |
| 167 | + vi.doMock("node:child_process", () => ({ execFile })); |
| 168 | + |
| 169 | + const { isPsnrFilterAvailable } = await import("./psnrFilterAvailability.js"); |
| 170 | + await expect(isPsnrFilterAvailable()).resolves.toBe(false); |
| 171 | + }); |
| 172 | +}); |
0 commit comments