|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | | -import { inferOutputFormat, inferInputKind, buildEncoderArgs } from "./pipeline.js"; |
| 2 | +import { EventEmitter } from "node:events"; |
| 3 | +import type { spawn } from "node:child_process"; |
| 4 | +import { inferOutputFormat, inferInputKind, buildEncoderArgs, waitForExit } from "./pipeline.js"; |
3 | 5 |
|
4 | 6 | describe("background-removal/pipeline — inferOutputFormat", () => { |
5 | 7 | it("maps .webm → webm", () => { |
@@ -65,3 +67,42 @@ describe("background-removal/pipeline — buildEncoderArgs", () => { |
65 | 67 | expect(args[rIdx + 1]).toBe("24"); |
66 | 68 | }); |
67 | 69 | }); |
| 70 | + |
| 71 | +// Regression: a previous version of waitForExit treated `code === null` as |
| 72 | +// success. Per Node's child_process docs, that's the signal-killed case — |
| 73 | +// reporting it as success means a SIGTERM/SIGKILL'd ffmpeg encoder produces |
| 74 | +// a "successful" render with a missing or truncated output file. |
| 75 | +describe("background-removal/pipeline — waitForExit signal handling", () => { |
| 76 | + function fakeProc(): ReturnType<typeof spawn> { |
| 77 | + return new EventEmitter() as unknown as ReturnType<typeof spawn>; |
| 78 | + } |
| 79 | + |
| 80 | + it("resolves on a clean exit (code=0, signal=null)", async () => { |
| 81 | + const proc = fakeProc(); |
| 82 | + const promise = waitForExit(proc, "ffmpeg encoder", () => ""); |
| 83 | + proc.emit("exit", 0, null); |
| 84 | + await expect(promise).resolves.toBeUndefined(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("rejects when killed by signal (code=null, signal='SIGTERM')", async () => { |
| 88 | + const proc = fakeProc(); |
| 89 | + const promise = waitForExit(proc, "ffmpeg encoder", () => "tail of stderr"); |
| 90 | + proc.emit("exit", null, "SIGTERM"); |
| 91 | + await expect(promise).rejects.toThrow(/killed by SIGTERM/); |
| 92 | + await expect(promise).rejects.toThrow(/tail of stderr/); |
| 93 | + }); |
| 94 | + |
| 95 | + it("rejects on non-zero exit code", async () => { |
| 96 | + const proc = fakeProc(); |
| 97 | + const promise = waitForExit(proc, "ffmpeg encoder", () => ""); |
| 98 | + proc.emit("exit", 1, null); |
| 99 | + await expect(promise).rejects.toThrow(/exited with code 1/); |
| 100 | + }); |
| 101 | + |
| 102 | + it("rejects on SIGKILL", async () => { |
| 103 | + const proc = fakeProc(); |
| 104 | + const promise = waitForExit(proc, "ffmpeg encoder", () => ""); |
| 105 | + proc.emit("exit", null, "SIGKILL"); |
| 106 | + await expect(promise).rejects.toThrow(/killed by SIGKILL/); |
| 107 | + }); |
| 108 | +}); |
0 commit comments