Skip to content

Write HTML. Render video. Built for agents.

test(engine): budget the ffmpeg audio-level tests, and make a stall say why (#3410)

Miguel Ángel92a6076807f7
diff --git a/packages/engine/src/services/audioMixer.level.test.ts b/packages/engine/src/services/audioMixer.level.test.ts
index 2ad7f5a5c4..145485537d 100644
--- a/packages/engine/src/services/audioMixer.level.test.ts
+++ b/packages/engine/src/services/audioMixer.level.test.ts
@@ -55,110 +55,140 @@ function firstAudibleSeconds(path: string): number {
throw new Error(`No audible sample found in ${path}`);
}
-describe.skipIf(!HAS_FFMPEG)("processCompositionAudio levels", () => {
- afterEach(() => {
- for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true });
- });
+/**
+ * These two tests drive real ffmpeg, and the file ran on vitest's 5s default:
+ * `places a delayed track on its authored start` takes ~137ms locally and still
+ * hit that cap on the windows runner, failing an unrelated PR. 60s is what the
+ * rest of the ffmpeg-driven engine tests use. Applied to the suite rather than
+ * each test so the budget has one home.
+ *
+ * Headroom alone would only delay an undiagnosable failure, so the ffmpeg
+ * process timeout is capped well under it too. Its production default is 5
+ * minutes — far above any test budget — so a stalled mix could only ever
+ * surface as a bare "Test timed out", with no stderr and no failing stage.
+ */
+const FFMPEG_TEST_TIMEOUT_MS = 60_000;
+const TEST_FFMPEG_TIMEOUT_MS = 20_000;
- it("preserves the level of a mono source in the stereo mix", async () => {
- const projectDir = mkdtempSync(join(tmpdir(), "hf-mono-level-"));
- const workDir = mkdtempSync(join(tmpdir(), "hf-mono-work-"));
- tempDirs.push(projectDir, workDir);
- const sourcePath = join(projectDir, "voice.wav");
- const outputPath = join(projectDir, "audio.aac");
- const setup = spawnSync(
- getFfmpegBinary(),
- [
- "-nostdin",
- "-v",
- "error",
- "-f",
- "lavfi",
- "-i",
- "sine=frequency=1000:duration=1:sample_rate=48000",
- "-ac",
- "1",
- "-c:a",
- "pcm_s16le",
- sourcePath,
- ],
- { encoding: "utf-8" },
- );
- expect(setup.status, setup.stderr).toBe(0);
+/** Assert the mix succeeded, reporting `failures` rather than a bare `false`. */
+function expectMixed(result: { success: boolean; failures?: unknown }): void {
+ if (!result.success) {
+ throw new Error(`mix failed: ${JSON.stringify(result.failures ?? result, null, 2)}`);
+ }
+}
+
+describe.skipIf(!HAS_FFMPEG)(
+ "processCompositionAudio levels",
+ () => {
+ afterEach(() => {
+ for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true });
+ });
+
+ it("preserves the level of a mono source in the stereo mix", async () => {
+ const projectDir = mkdtempSync(join(tmpdir(), "hf-mono-level-"));
+ const workDir = mkdtempSync(join(tmpdir(), "hf-mono-work-"));
+ tempDirs.push(projectDir, workDir);
+ const sourcePath = join(projectDir, "voice.wav");
+ const outputPath = join(projectDir, "audio.aac");
+ const setup = spawnSync(
+ getFfmpegBinary(),
+ [
+ "-nostdin",
+ "-v",
+ "error",
+ "-f",
+ "lavfi",
+ "-i",
+ "sine=frequency=1000:duration=1:sample_rate=48000",
+ "-ac",
+ "1",
+ "-c:a",
+ "pcm_s16le",
+ sourcePath,
+ ],
+ { encoding: "utf-8" },
+ );
+ expect(setup.status, setup.stderr).toBe(0);
- const result = await processCompositionAudio(
- [
- {
- id: "voice",
- src: "voice.wav",
- start: 0,
- end: 1,
- mediaStart: 0,
- layer: 0,
- volume: 1,
- type: "audio",
- },
- ],
- projectDir,
- workDir,
- outputPath,
- 1,
- );
+ const result = await processCompositionAudio(
+ [
+ {
+ id: "voice",
+ src: "voice.wav",
+ start: 0,
+ end: 1,
+ mediaStart: 0,
+ layer: 0,
+ volume: 1,
+ type: "audio",
+ },
+ ],
+ projectDir,
+ workDir,
+ outputPath,
+ 1,
+ undefined,
+ { ffmpegProcessTimeout: TEST_FFMPEG_TIMEOUT_MS },
+ );
- expect(result.success).toBe(true);
- expect(meanVolumeDb(outputPath) - meanVolumeDb(sourcePath)).toBeGreaterThan(-0.3);
- });
+ expectMixed(result);
+ expect(meanVolumeDb(outputPath) - meanVolumeDb(sourcePath)).toBeGreaterThan(-0.3);
+ });
- it("places a delayed track on its authored start, not one AAC frame later", async () => {
- // The mix is AAC-encoded, and AAC encoders emit ~1024 priming samples. A
- // raw ADTS container has nowhere to record that delay, so it decodes as
- // real leading silence and drags the whole track 21.33 ms late against a
- // frame-accurate video. MIXED_AUDIO_FILENAME picks a container that stores
- // the delay as an edit list instead; this asserts the artifact we actually
- // ship lands on time.
- const projectDir = mkdtempSync(join(tmpdir(), "hf-onset-"));
- const workDir = mkdtempSync(join(tmpdir(), "hf-onset-work-"));
- tempDirs.push(projectDir, workDir);
- const sourcePath = join(projectDir, "tone.wav");
- const outputPath = join(projectDir, MIXED_AUDIO_FILENAME);
- const setup = spawnSync(
- getFfmpegBinary(),
- [
- "-nostdin",
- "-v",
- "error",
- "-f",
- "lavfi",
- "-i",
- "sine=frequency=1000:duration=1:sample_rate=48000",
- "-c:a",
- "pcm_s16le",
- sourcePath,
- ],
- { encoding: "utf-8" },
- );
- expect(setup.status, setup.stderr).toBe(0);
+ it("places a delayed track on its authored start, not one AAC frame later", async () => {
+ // The mix is AAC-encoded, and AAC encoders emit ~1024 priming samples. A
+ // raw ADTS container has nowhere to record that delay, so it decodes as
+ // real leading silence and drags the whole track 21.33 ms late against a
+ // frame-accurate video. MIXED_AUDIO_FILENAME picks a container that stores
+ // the delay as an edit list instead; this asserts the artifact we actually
+ // ship lands on time.
+ const projectDir = mkdtempSync(join(tmpdir(), "hf-onset-"));
+ const workDir = mkdtempSync(join(tmpdir(), "hf-onset-work-"));
+ tempDirs.push(projectDir, workDir);
+ const sourcePath = join(projectDir, "tone.wav");
+ const outputPath = join(projectDir, MIXED_AUDIO_FILENAME);
+ const setup = spawnSync(
+ getFfmpegBinary(),
+ [
+ "-nostdin",
+ "-v",
+ "error",
+ "-f",
+ "lavfi",
+ "-i",
+ "sine=frequency=1000:duration=1:sample_rate=48000",
+ "-c:a",
+ "pcm_s16le",
+ sourcePath,
+ ],
+ { encoding: "utf-8" },
+ );
+ expect(setup.status, setup.stderr).toBe(0);
- const result = await processCompositionAudio(
- [
- {
- id: "tone",
- src: "tone.wav",
- start: 2,
- end: 3,
- mediaStart: 0,
- layer: 0,
- volume: 1,
- type: "audio",
- },
- ],
- projectDir,
- workDir,
- outputPath,
- 4,
- );
+ const result = await processCompositionAudio(
+ [
+ {
+ id: "tone",
+ src: "tone.wav",
+ start: 2,
+ end: 3,
+ mediaStart: 0,
+ layer: 0,
+ volume: 1,
+ type: "audio",
+ },
+ ],
+ projectDir,
+ workDir,
+ outputPath,
+ 4,
+ undefined,
+ { ffmpegProcessTimeout: TEST_FFMPEG_TIMEOUT_MS },
+ );
- expect(result.success).toBe(true);
- expect(firstAudibleSeconds(outputPath)).toBeCloseTo(2, 2);
- });
-});
+ expectMixed(result);
+ expect(firstAudibleSeconds(outputPath)).toBeCloseTo(2, 2);
+ });
+ },
+ FFMPEG_TEST_TIMEOUT_MS,
+);