1- import { execFileSync } from "node:child_process" ;
21import { tmpdir } from "node:os" ;
32import { join } from "node:path" ;
43import { afterEach , describe , expect , it , vi } from "vitest" ;
54import { findFFmpeg , findFFprobe , H264EncoderUnavailableError } from "./ffmpeg.js" ;
5+ import { runCancellableProcess } from "../utils/cancellableProcess.js" ;
66
7- // Only child_process is mocked: the H264 encoder probe shells out, while the
8- // wrapper tests below resolve via env overrides and need the real `existsSync`.
7+ // Only child_process is mocked, so no lookup shells out, while the wrapper
8+ // tests below resolve via env overrides and need the real `existsSync`.
99vi . mock ( "node:child_process" , ( ) => ( { execFileSync : vi . fn ( ) , execSync : vi . fn ( ) } ) ) ;
1010
1111// Only the distro probe is faked; `ffmpegInstallCommand` stays real so the
@@ -16,7 +16,8 @@ vi.mock("./linuxDeps.js", async (importOriginal) => ({
1616 detectLinuxDistro : ( ) => ( { family : linuxFamily , isWsl : false , prettyName : null } ) ,
1717} ) ) ;
1818
19- const mockExecFile = vi . mocked ( execFileSync ) ;
19+ vi . mock ( "../utils/cancellableProcess.js" , ( ) => ( { runCancellableProcess : vi . fn ( ) } ) ) ;
20+ const mockRun = vi . mocked ( runCancellableProcess ) ;
2021
2122afterEach ( ( ) => {
2223 vi . clearAllMocks ( ) ;
@@ -45,56 +46,76 @@ describe("findFFmpeg / findFFprobe", () => {
4546} ) ;
4647
4748describe ( "resolveH264EncoderMode" , ( ) => {
48- it ( "falls back to VideoToolbox when libx264 is absent" , async ( ) => {
49- const { resolveH264EncoderMode } = await import ( "./ffmpeg.js" ) ;
50- const encoders = `
51- V....D h264_videotoolbox VideoToolbox H.264 Encoder
52- ` ;
49+ // The shape of an LGPL FFmpeg build: OpenH264 and VideoToolbox, no libx264.
50+ const noX264 = " V....D libopenh264 OpenH264\n V....D h264_videotoolbox VideoToolbox H.264\n" ;
5351
54- expect ( resolveH264EncoderMode ( encoders , false ) ) . toBe ( "gpu" ) ;
52+ it ( "falls back to a GPU encoder that works when libx264 is absent" , async ( ) => {
53+ const { resolveH264EncoderMode } = await import ( "./ffmpeg.js" ) ;
54+ expect ( resolveH264EncoderMode ( noX264 , false , true ) ) . toBe ( "gpu" ) ;
5555 } ) ;
5656
57- it ( "does not treat a compiled Linux hardware encoder as usable " , async ( ) => {
57+ it ( "refuses a compiled GPU encoder whose probe failed, with or without --gpu " , async ( ) => {
5858 const { resolveH264EncoderMode } = await import ( "./ffmpeg.js" ) ;
59- const encoders = `
60- V....D h264_vaapi H.264/AVC (VAAPI)
61- ` ;
59+ expect ( ( ) => resolveH264EncoderMode ( noX264 , false , false ) ) . toThrow ( H264EncoderUnavailableError ) ;
60+ expect ( ( ) => resolveH264EncoderMode ( noX264 , true , false ) ) . toThrow ( H264EncoderUnavailableError ) ;
61+ } ) ;
6262
63- expect ( ( ) => resolveH264EncoderMode ( encoders , false ) ) . toThrow ( H264EncoderUnavailableError ) ;
63+ it ( "encodes --gpu renders with libx264 when no GPU encoder works" , async ( ) => {
64+ const { resolveH264EncoderMode } = await import ( "./ffmpeg.js" ) ;
65+ expect ( resolveH264EncoderMode ( " V....D libx264 H.264\n" , true , false ) ) . toBe ( "software" ) ;
66+ expect ( resolveH264EncoderMode ( " V....D libx264 H.264\n" , true , true ) ) . toBe ( "gpu" ) ;
6467 } ) ;
6568
6669 it ( "prefers libx264 when both encoders are available" , async ( ) => {
6770 const { resolveH264EncoderMode } = await import ( "./ffmpeg.js" ) ;
6871 expect (
69- resolveH264EncoderMode ( " V....D libx264 H.264\n V....D h264_videotoolbox H.264\n" , false ) ,
72+ resolveH264EncoderMode (
73+ " V....D libx264 H.264\n V....D h264_videotoolbox H.264\n" ,
74+ false ,
75+ true ,
76+ ) ,
7077 ) . toBe ( "software" ) ;
7178 } ) ;
7279
73- it ( "distinguishes a successful unsupported probe from an unavailable probe " , async ( ) => {
74- const { detectH264EncoderMode } = await import ( "./ffmpeg.js" ) ;
75- mockExecFile . mockReturnValue ( " V....D libvpx-vp9 VP9\n" ) ;
76- expect ( ( ) => detectH264EncoderMode ( "/custom/ffmpeg" , false ) ) . toThrow (
77- H264EncoderUnavailableError ,
78- ) ;
79- const timeout = new Error ( "ETIMEDOUT" ) ;
80- mockExecFile . mockImplementationOnce ( ( ) => {
81- throw timeout ;
82- } ) ;
83- expect ( ( ) => detectH264EncoderMode ( "/custom/ffmpeg" , false ) ) . toThrow ( timeout ) ;
80+ it ( "says whether HEVC could still encode when H.264 cannot " , async ( ) => {
81+ const { resolveH264EncoderMode } = await import ( "./ffmpeg.js" ) ;
82+ const refusal = ( encoders : string ) => {
83+ try {
84+ resolveH264EncoderMode ( encoders , false , false ) ;
85+ } catch ( error ) {
86+ return error as InstanceType < typeof H264EncoderUnavailableError > ;
87+ }
88+ } ;
89+ expect ( refusal ( " V....D libx265 HEVC\n" ) ?. hevcEncoderAvailable ) . toBe ( true ) ;
90+ expect ( refusal ( noX264 ) ?. hevcEncoderAvailable ) . toBe ( false ) ;
8491 } ) ;
8592
86- it ( "inspects the configured FFmpeg binary" , async ( ) => {
87- mockExecFile . mockReturnValue (
88- " V....D h264_videotoolbox VideoToolbox H.264 Encoder\n" as never ,
93+ it ( "asks the configured FFmpeg, and probes the GPU only when libx264 is missing or --gpu is set" , async ( ) => {
94+ const { detectH264EncoderModeForRender } = await import ( "./ffmpeg.js" ) ;
95+ const signal = new AbortController ( ) . signal ;
96+ const probeGpu = vi . fn ( async ( ) => null ) ;
97+ mockRun . mockResolvedValue ( { stdout : " V....D libx264 H.264\n" } as never ) ;
98+ expect ( await detectH264EncoderModeForRender ( "/custom/ffmpeg" , false , signal , probeGpu ) ) . toBe (
99+ "software" ,
89100 ) ;
90- const { detectH264EncoderMode } = await import ( "./ffmpeg.js" ) ;
91-
92- expect ( detectH264EncoderMode ( "/custom/ffmpeg" , false ) ) . toBe ( "gpu" ) ;
93- expect ( mockExecFile ) . toHaveBeenCalledWith (
101+ expect ( probeGpu ) . not . toHaveBeenCalled ( ) ;
102+ expect ( mockRun ) . toHaveBeenCalledWith (
94103 "/custom/ffmpeg" ,
95104 [ "-hide_banner" , "-encoders" ] ,
96- expect . objectContaining ( { encoding : "utf-8" , windowsHide : true } ) ,
105+ expect . objectContaining ( { signal } ) ,
97106 ) ;
107+
108+ mockRun . mockResolvedValue ( { stdout : " V....D h264_videotoolbox H.264\n" } as never ) ;
109+ await expect (
110+ detectH264EncoderModeForRender ( "/custom/ffmpeg" , true , signal , probeGpu ) ,
111+ ) . rejects . toThrow ( H264EncoderUnavailableError ) ;
112+ expect ( probeGpu ) . toHaveBeenCalledOnce ( ) ;
113+
114+ const timeout = new Error ( "ETIMEDOUT" ) ;
115+ mockRun . mockRejectedValueOnce ( timeout ) ;
116+ await expect (
117+ detectH264EncoderModeForRender ( "/custom/ffmpeg" , false , signal , probeGpu ) ,
118+ ) . rejects . toThrow ( timeout ) ;
98119 } ) ;
99120} ) ;
100121
0 commit comments