2022-10-14 12:13:17 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-10-14 12:13:17 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-10-14 12:13:17 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
|
|
|
|
|
|
|
import {
|
2022-10-24 22:06:58 +08:00
|
|
|
VoiceBroadcastInfoState,
|
2022-10-14 12:13:17 +08:00
|
|
|
VoiceBroadcastPlayback,
|
2022-10-24 22:06:58 +08:00
|
|
|
VoiceBroadcastPlaybackEvent,
|
2022-10-14 12:13:17 +08:00
|
|
|
VoiceBroadcastPlaybacksStore,
|
|
|
|
VoiceBroadcastPlaybacksStoreEvent,
|
2022-10-24 22:06:58 +08:00
|
|
|
VoiceBroadcastPlaybackState,
|
2023-01-02 20:21:33 +08:00
|
|
|
VoiceBroadcastRecordingsStore,
|
2024-10-15 21:57:26 +08:00
|
|
|
} from "../../../../src/voice-broadcast";
|
|
|
|
import { mkStubRoom, stubClient } from "../../../test-utils";
|
2022-10-24 22:06:58 +08:00
|
|
|
import { mkVoiceBroadcastInfoStateEvent } from "../utils/test-utils";
|
2022-10-14 12:13:17 +08:00
|
|
|
|
|
|
|
describe("VoiceBroadcastPlaybacksStore", () => {
|
|
|
|
const roomId = "!room:example.com";
|
|
|
|
let client: MatrixClient;
|
2022-11-07 22:19:49 +08:00
|
|
|
let userId: string;
|
|
|
|
let deviceId: string;
|
2022-10-14 12:13:17 +08:00
|
|
|
let room: Room;
|
2022-10-24 22:06:58 +08:00
|
|
|
let infoEvent1: MatrixEvent;
|
|
|
|
let infoEvent2: MatrixEvent;
|
|
|
|
let playback1: VoiceBroadcastPlayback;
|
|
|
|
let playback2: VoiceBroadcastPlayback;
|
2022-10-14 12:13:17 +08:00
|
|
|
let playbacks: VoiceBroadcastPlaybacksStore;
|
2023-05-12 16:49:37 +08:00
|
|
|
let onCurrentChanged: (playback: VoiceBroadcastPlayback | null) => void;
|
2022-10-14 12:13:17 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client = stubClient();
|
2022-11-07 22:19:49 +08:00
|
|
|
userId = client.getUserId() || "";
|
|
|
|
deviceId = client.getDeviceId() || "";
|
|
|
|
mocked(client.relations).mockClear();
|
|
|
|
mocked(client.relations).mockResolvedValue({ events: [] });
|
|
|
|
|
2022-10-14 12:13:17 +08:00
|
|
|
room = mkStubRoom(roomId, "test room", client);
|
2022-11-07 22:19:49 +08:00
|
|
|
mocked(client.getRoom).mockImplementation((roomId: string): Room | null => {
|
2022-10-14 12:13:17 +08:00
|
|
|
if (roomId === room.roomId) {
|
|
|
|
return room;
|
|
|
|
}
|
2022-11-07 22:19:49 +08:00
|
|
|
|
|
|
|
return null;
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
2022-10-24 22:06:58 +08:00
|
|
|
|
|
|
|
infoEvent1 = mkVoiceBroadcastInfoStateEvent(roomId, VoiceBroadcastInfoState.Started, userId, deviceId);
|
|
|
|
infoEvent2 = mkVoiceBroadcastInfoStateEvent(roomId, VoiceBroadcastInfoState.Started, userId, deviceId);
|
2023-01-02 20:21:33 +08:00
|
|
|
const recordings = new VoiceBroadcastRecordingsStore();
|
|
|
|
playback1 = new VoiceBroadcastPlayback(infoEvent1, client, recordings);
|
2022-10-24 22:06:58 +08:00
|
|
|
jest.spyOn(playback1, "off");
|
2023-01-02 20:21:33 +08:00
|
|
|
playback2 = new VoiceBroadcastPlayback(infoEvent2, client, recordings);
|
2022-10-24 22:06:58 +08:00
|
|
|
jest.spyOn(playback2, "off");
|
|
|
|
|
2023-01-02 20:21:33 +08:00
|
|
|
playbacks = new VoiceBroadcastPlaybacksStore(recordings);
|
2022-10-24 22:06:58 +08:00
|
|
|
jest.spyOn(playbacks, "removeAllListeners");
|
2022-10-14 12:13:17 +08:00
|
|
|
onCurrentChanged = jest.fn();
|
|
|
|
playbacks.on(VoiceBroadcastPlaybacksStoreEvent.CurrentChanged, onCurrentChanged);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
playbacks.off(VoiceBroadcastPlaybacksStoreEvent.CurrentChanged, onCurrentChanged);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when setting a current Voice Broadcast playback", () => {
|
|
|
|
beforeEach(() => {
|
2022-10-24 22:06:58 +08:00
|
|
|
playbacks.setCurrent(playback1);
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should return it as current", () => {
|
2022-10-24 22:06:58 +08:00
|
|
|
expect(playbacks.getCurrent()).toBe(playback1);
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should return it by id", () => {
|
2022-10-24 22:06:58 +08:00
|
|
|
expect(playbacks.getByInfoEvent(infoEvent1, client)).toBe(playback1);
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should emit a CurrentChanged event", () => {
|
2022-10-24 22:06:58 +08:00
|
|
|
expect(onCurrentChanged).toHaveBeenCalledWith(playback1);
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("and setting the same again", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(onCurrentChanged).mockClear();
|
2022-10-24 22:06:58 +08:00
|
|
|
playbacks.setCurrent(playback1);
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not emit a CurrentChanged event", () => {
|
|
|
|
expect(onCurrentChanged).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2022-10-24 22:06:58 +08:00
|
|
|
|
|
|
|
describe("and setting another playback and start both", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
playbacks.setCurrent(playback2);
|
|
|
|
playback1.start();
|
|
|
|
playback2.start();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should set playback1 to paused", () => {
|
|
|
|
expect(playback1.getState()).toBe(VoiceBroadcastPlaybackState.Paused);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should set playback2 to buffering", () => {
|
|
|
|
// buffering because there are no chunks, yet
|
|
|
|
expect(playback2.getState()).toBe(VoiceBroadcastPlaybackState.Buffering);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and calling destroy", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
playbacks.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should remove all listeners", () => {
|
|
|
|
expect(playbacks.removeAllListeners).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should deregister the listeners on the playbacks", () => {
|
|
|
|
expect(playback1.off).toHaveBeenCalledWith(
|
|
|
|
VoiceBroadcastPlaybackEvent.StateChanged,
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
expect(playback2.off).toHaveBeenCalledWith(
|
|
|
|
VoiceBroadcastPlaybackEvent.StateChanged,
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("getByInfoEventId", () => {
|
|
|
|
let returnedPlayback: VoiceBroadcastPlayback;
|
|
|
|
|
|
|
|
describe("when retrieving a known playback", () => {
|
|
|
|
beforeEach(() => {
|
2022-10-24 22:06:58 +08:00
|
|
|
playbacks.setCurrent(playback1);
|
|
|
|
returnedPlayback = playbacks.getByInfoEvent(infoEvent1, client);
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should return the playback", () => {
|
2022-10-24 22:06:58 +08:00
|
|
|
expect(returnedPlayback).toBe(playback1);
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when retrieving an unknown playback", () => {
|
|
|
|
beforeEach(() => {
|
2022-10-24 22:06:58 +08:00
|
|
|
returnedPlayback = playbacks.getByInfoEvent(infoEvent1, client);
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should return the playback", () => {
|
2022-10-24 22:06:58 +08:00
|
|
|
expect(returnedPlayback.infoEvent).toBe(infoEvent1);
|
2022-10-14 12:13:17 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|