2026-08-30 23:26:59 +07:00
|
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
2026-08-31 16:08:55 +07:00
|
|
|
import * as streamWatch from "../src/modules/voice-recording/streamWatchReceiver.js";
|
2026-08-30 23:26:59 +07:00
|
|
|
import {
|
2026-08-31 12:44:36 +07:00
|
|
|
__resetVideoRecorderState,
|
2026-08-30 23:26:59 +07:00
|
|
|
setVideoRecorderClient,
|
|
|
|
|
setVideoRecordingsDir,
|
|
|
|
|
startVideoRecording,
|
|
|
|
|
stopVideoRecording,
|
|
|
|
|
trackChannel,
|
|
|
|
|
untrackChannel,
|
|
|
|
|
} from "../src/modules/voice-recording/videoRecorder.js";
|
|
|
|
|
|
2026-08-31 13:46:24 +07:00
|
|
|
// ─── mocks ─────────────────────────────────────────────────────────────
|
2026-08-30 23:26:59 +07:00
|
|
|
function makeChannel(guildId = "g1", channelId = "c1") {
|
|
|
|
|
return {
|
|
|
|
|
id: channelId,
|
|
|
|
|
guild: { id: guildId },
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function makeClient() {
|
|
|
|
|
return {
|
|
|
|
|
user: { id: "bot1" },
|
|
|
|
|
on: vi.fn(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let seq = 0;
|
|
|
|
|
function makeUser() {
|
|
|
|
|
seq += 1;
|
|
|
|
|
return `user-${seq}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2026-08-31 12:44:36 +07:00
|
|
|
__resetVideoRecorderState();
|
2026-08-31 13:46:24 +07:00
|
|
|
vi.restoreAllMocks();
|
2026-08-30 23:26:59 +07:00
|
|
|
setVideoRecordingsDir("/tmp/gmw-vidrec-test");
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-31 13:46:24 +07:00
|
|
|
describe("videoRecorder (stream-watch orchestration)", () => {
|
|
|
|
|
it("registers a voiceStateUpdate + raw listener on the client (idempotent)", () => {
|
2026-08-30 23:26:59 +07:00
|
|
|
const client = makeClient();
|
|
|
|
|
setVideoRecorderClient(client);
|
2026-08-31 13:46:24 +07:00
|
|
|
setVideoRecorderClient(client); // idempotent (videoRecorder + streamWatch)
|
|
|
|
|
// one raw (streamWatch) + one voiceStateUpdate (videoRecorder)
|
|
|
|
|
expect(client.on).toHaveBeenCalledTimes(2);
|
2026-08-30 23:26:59 +07:00
|
|
|
expect(client.on).toHaveBeenCalledWith(
|
|
|
|
|
"voiceStateUpdate",
|
|
|
|
|
expect.any(Function),
|
|
|
|
|
);
|
2026-08-31 13:46:24 +07:00
|
|
|
expect(client.on).toHaveBeenCalledWith("raw", expect.any(Function));
|
2026-08-30 23:26:59 +07:00
|
|
|
});
|
|
|
|
|
|
2026-08-31 13:46:24 +07:00
|
|
|
it("startVideoRecording delegates to streamWatchReceiver.startStreamWatch", () => {
|
2026-08-31 16:08:55 +07:00
|
|
|
const spy = vi
|
|
|
|
|
.spyOn(streamWatch, "startStreamWatch")
|
|
|
|
|
.mockImplementation(() => {});
|
2026-08-31 13:46:24 +07:00
|
|
|
const ch = makeChannel();
|
|
|
|
|
startVideoRecording(ch, makeUser());
|
|
|
|
|
expect(spy).toHaveBeenCalledWith(ch, expect.any(String));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("refuses to record the bot's own video", () => {
|
|
|
|
|
setVideoRecorderClient(makeClient());
|
2026-08-31 16:08:55 +07:00
|
|
|
const spy = vi
|
|
|
|
|
.spyOn(streamWatch, "startStreamWatch")
|
|
|
|
|
.mockImplementation(() => {});
|
2026-08-31 13:46:24 +07:00
|
|
|
startVideoRecording(makeChannel(), "bot1");
|
|
|
|
|
expect(spy).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("stopVideoRecording calls streamWatchReceiver.stopStreamWatch", () => {
|
2026-08-31 16:08:55 +07:00
|
|
|
const spy = vi
|
|
|
|
|
.spyOn(streamWatch, "stopStreamWatch")
|
|
|
|
|
.mockImplementation(() => {});
|
2026-08-31 13:46:24 +07:00
|
|
|
const u = makeUser();
|
|
|
|
|
stopVideoRecording("g1", u);
|
|
|
|
|
expect(spy).toHaveBeenCalledWith("g1", u);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("untrackChannel tears down all stream watches for the guild", () => {
|
2026-08-31 16:08:55 +07:00
|
|
|
const spy = vi
|
|
|
|
|
.spyOn(streamWatch, "stopAllStreamWatches")
|
|
|
|
|
.mockImplementation(() => {});
|
2026-08-31 13:46:24 +07:00
|
|
|
untrackChannel("g1");
|
|
|
|
|
expect(spy).toHaveBeenCalledWith("g1");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("the voiceStateUpdate handler starts a watch when a member starts streaming", () => {
|
|
|
|
|
const client = makeClient() as any;
|
2026-08-30 23:26:59 +07:00
|
|
|
setVideoRecorderClient(client);
|
|
|
|
|
trackChannel("g1", makeChannel());
|
2026-08-31 16:08:55 +07:00
|
|
|
const startSpy = vi
|
|
|
|
|
.spyOn(streamWatch, "startStreamWatch")
|
|
|
|
|
.mockImplementation(() => {});
|
2026-08-31 13:46:24 +07:00
|
|
|
const listener = client.on.mock.calls.find(
|
|
|
|
|
(c: unknown[]) => c[0] === "voiceStateUpdate",
|
|
|
|
|
)?.[1];
|
2026-08-31 16:08:55 +07:00
|
|
|
listener(null, {
|
|
|
|
|
id: "u1",
|
|
|
|
|
guild: { id: "g1" },
|
|
|
|
|
channelId: "c1",
|
|
|
|
|
streaming: true,
|
|
|
|
|
});
|
2026-08-31 13:46:24 +07:00
|
|
|
expect(startSpy).toHaveBeenCalled();
|
2026-08-30 23:26:59 +07:00
|
|
|
});
|
|
|
|
|
|
2026-08-31 13:46:24 +07:00
|
|
|
it("the voiceStateUpdate handler stops a watch when a member stops streaming", () => {
|
|
|
|
|
const client = makeClient() as any;
|
2026-08-30 23:26:59 +07:00
|
|
|
setVideoRecorderClient(client);
|
2026-08-31 13:46:24 +07:00
|
|
|
trackChannel("g1", makeChannel());
|
2026-08-31 16:08:55 +07:00
|
|
|
const stopSpy = vi
|
|
|
|
|
.spyOn(streamWatch, "stopStreamWatch")
|
|
|
|
|
.mockImplementation(() => {});
|
2026-08-31 13:46:24 +07:00
|
|
|
const listener = client.on.mock.calls.find(
|
|
|
|
|
(c: unknown[]) => c[0] === "voiceStateUpdate",
|
|
|
|
|
)?.[1];
|
2026-08-31 16:08:55 +07:00
|
|
|
listener(null, {
|
|
|
|
|
id: "u1",
|
|
|
|
|
guild: { id: "g1" },
|
|
|
|
|
channelId: "c1",
|
|
|
|
|
streaming: false,
|
|
|
|
|
});
|
2026-08-31 13:46:24 +07:00
|
|
|
expect(stopSpy).toHaveBeenCalledWith("g1", "u1");
|
2026-08-31 12:44:36 +07:00
|
|
|
});
|
2026-08-31 15:48:20 +07:00
|
|
|
|
|
|
|
|
it("trackChannel scans pre-existing streamers already in the channel (bot join case)", () => {
|
|
|
|
|
const client = makeClient() as any;
|
|
|
|
|
setVideoRecorderClient(client);
|
2026-08-31 16:08:55 +07:00
|
|
|
const spy = vi
|
|
|
|
|
.spyOn(streamWatch, "startStreamWatch")
|
|
|
|
|
.mockImplementation(() => {});
|
2026-08-31 15:48:20 +07:00
|
|
|
const ch = {
|
|
|
|
|
id: "c1",
|
|
|
|
|
guild: { id: "g1" },
|
|
|
|
|
members: new Map([
|
|
|
|
|
["u1", { id: "u1", voice: { streaming: true } }], // already streaming
|
|
|
|
|
["u2", { id: "u2", voice: { streaming: false } }], // not streaming
|
|
|
|
|
["bot1", { id: "bot1", voice: { streaming: true } }], // bot itself
|
|
|
|
|
]),
|
|
|
|
|
};
|
|
|
|
|
trackChannel("g1", ch as any);
|
|
|
|
|
// only the already-streaming non-bot member gets watched
|
|
|
|
|
expect(spy).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(spy).toHaveBeenCalledWith(ch, "u1");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("trackChannel is a no-op when the channel has no members (or none streaming)", () => {
|
2026-08-31 16:08:55 +07:00
|
|
|
const spy = vi
|
|
|
|
|
.spyOn(streamWatch, "startStreamWatch")
|
|
|
|
|
.mockImplementation(() => {});
|
2026-08-31 15:48:20 +07:00
|
|
|
trackChannel("g1", makeChannel()); // no members
|
|
|
|
|
expect(spy).not.toHaveBeenCalled();
|
|
|
|
|
});
|
2026-08-30 23:26:59 +07:00
|
|
|
});
|