test: add unit tests for playTranscodedPreparedStream and transcoder functionality

This commit is contained in:
MythEclipse
2026-05-17 17:54:39 +07:00
parent a3e6c4695a
commit 4931e6d1ca
2 changed files with 111 additions and 0 deletions
+52
View File
@@ -0,0 +1,52 @@
import { describe, it, expect, vi } from "vitest";
import { PassThrough } from "node:stream";
// Mock spawn to avoid calling real ffmpeg
vi.mock("node:child_process", async () => {
const actual = await vi.importActual("node:child_process");
return {
...actual,
spawn: (cmd: string, args: string[], opts: any) => {
const stdout = new PassThrough();
const stderr = new PassThrough();
const listeners: Record<string, Function[]> = {};
const proc: any = {
stdout,
stderr,
kill: vi.fn(() => {
// emit exit when killed
(listeners.exit || []).forEach((fn) => fn(0, "SIGKILL"));
}),
on: (ev: string, fn: Function) => {
listeners[ev] = listeners[ev] || [];
listeners[ev].push(fn);
},
off: (ev: string, fn: Function) => {
listeners[ev] = (listeners[ev] || []).filter((f) => f !== fn);
},
stdoutWrite: (d: Buffer | string) => stdout.write(d),
};
// simulate async start
setTimeout(() => {
(listeners.exit || []).forEach((fn) => fn(null, null));
}, 10);
return proc;
},
};
});
import { prepareTranscoder } from "../../src/streaming/transcoder";
describe("Transcoder", () => {
it("starts ffmpeg and returns output stream and command", () => {
const { transcoder, command, output } = prepareTranscoder("http://example.test/video", { fps: 24 });
expect(transcoder).toBeTruthy();
expect(command).toBeTruthy();
expect(output).toBeTruthy();
expect(typeof command.kill).toBe("function");
// write some data and ensure output is readable
const wrote = command.stdoutWrite?.("hello");
expect(output.readable).toBe(true);
transcoder.stop();
});
});