2022-09-28 08:22:50 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-09-28 08:22:50 +00:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00: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-09-28 08:22:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
|
|
|
|
|
|
|
import {
|
|
|
|
VoiceBroadcastRecordingsStore,
|
|
|
|
VoiceBroadcastRecordingsStoreEvent,
|
|
|
|
VoiceBroadcastRecording,
|
2022-10-19 16:02:48 +00:00
|
|
|
VoiceBroadcastInfoState,
|
2022-09-28 08:22:50 +00:00
|
|
|
} from "../../../src/voice-broadcast";
|
2022-10-19 16:02:48 +00:00
|
|
|
import { mkStubRoom, stubClient } from "../../test-utils";
|
|
|
|
import { mkVoiceBroadcastInfoStateEvent } from "../utils/test-utils";
|
2022-09-28 08:22:50 +00:00
|
|
|
|
|
|
|
describe("VoiceBroadcastRecordingsStore", () => {
|
|
|
|
const roomId = "!room:example.com";
|
|
|
|
let client: MatrixClient;
|
|
|
|
let room: Room;
|
|
|
|
let infoEvent: MatrixEvent;
|
2022-10-19 16:02:48 +00:00
|
|
|
let otherInfoEvent: MatrixEvent;
|
2022-09-28 08:22:50 +00:00
|
|
|
let recording: VoiceBroadcastRecording;
|
2022-10-19 16:02:48 +00:00
|
|
|
let otherRecording: VoiceBroadcastRecording;
|
2022-09-28 08:22:50 +00:00
|
|
|
let recordings: VoiceBroadcastRecordingsStore;
|
2023-05-12 08:49:37 +00:00
|
|
|
let onCurrentChanged: (recording: VoiceBroadcastRecording | null) => void;
|
2022-09-28 08:22:50 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client = stubClient();
|
|
|
|
room = mkStubRoom(roomId, "test room", client);
|
|
|
|
mocked(client.getRoom).mockImplementation((roomId: string) => {
|
|
|
|
if (roomId === room.roomId) {
|
|
|
|
return room;
|
|
|
|
}
|
2023-02-13 17:01:43 +00:00
|
|
|
return null;
|
2022-09-28 08:22:50 +00:00
|
|
|
});
|
2022-10-21 07:30:02 +00:00
|
|
|
infoEvent = mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoState.Started,
|
2023-02-16 09:38:44 +00:00
|
|
|
client.getUserId()!,
|
|
|
|
client.getDeviceId()!,
|
2022-10-21 07:30:02 +00:00
|
|
|
);
|
|
|
|
otherInfoEvent = mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoState.Started,
|
2023-02-16 09:38:44 +00:00
|
|
|
client.getUserId()!,
|
|
|
|
client.getDeviceId()!,
|
2022-10-21 07:30:02 +00:00
|
|
|
);
|
2022-10-19 16:02:48 +00:00
|
|
|
recording = new VoiceBroadcastRecording(infoEvent, client);
|
|
|
|
otherRecording = new VoiceBroadcastRecording(otherInfoEvent, client);
|
2022-09-28 08:22:50 +00:00
|
|
|
recordings = new VoiceBroadcastRecordingsStore();
|
|
|
|
onCurrentChanged = jest.fn();
|
|
|
|
recordings.on(VoiceBroadcastRecordingsStoreEvent.CurrentChanged, onCurrentChanged);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2022-10-19 16:02:48 +00:00
|
|
|
recording.destroy();
|
2022-09-28 08:22:50 +00:00
|
|
|
recordings.off(VoiceBroadcastRecordingsStoreEvent.CurrentChanged, onCurrentChanged);
|
|
|
|
});
|
|
|
|
|
2023-01-02 11:05:51 +00:00
|
|
|
it("when setting a recording without info event Id, it should raise an error", () => {
|
|
|
|
infoEvent.event.event_id = undefined;
|
|
|
|
expect(() => {
|
|
|
|
recordings.setCurrent(recording);
|
2023-03-01 15:23:35 +00:00
|
|
|
}).toThrow("Got broadcast info event without Id");
|
2023-01-02 11:05:51 +00:00
|
|
|
});
|
|
|
|
|
2022-09-28 08:22:50 +00:00
|
|
|
describe("when setting a current Voice Broadcast recording", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
recordings.setCurrent(recording);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return it as current", () => {
|
2022-11-18 07:36:20 +00:00
|
|
|
expect(recordings.hasCurrent()).toBe(true);
|
2022-10-11 22:31:28 +00:00
|
|
|
expect(recordings.getCurrent()).toBe(recording);
|
2022-09-28 08:22:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should return it by id", () => {
|
|
|
|
expect(recordings.getByInfoEvent(infoEvent, client)).toBe(recording);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should emit a CurrentChanged event", () => {
|
|
|
|
expect(onCurrentChanged).toHaveBeenCalledWith(recording);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and setting the same again", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(onCurrentChanged).mockClear();
|
|
|
|
recordings.setCurrent(recording);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not emit a CurrentChanged event", () => {
|
|
|
|
expect(onCurrentChanged).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2022-10-13 12:02:53 +00:00
|
|
|
|
|
|
|
describe("and calling clearCurrent()", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
recordings.clearCurrent();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should clear the current recording", () => {
|
2022-11-18 07:36:20 +00:00
|
|
|
expect(recordings.hasCurrent()).toBe(false);
|
2022-10-13 12:02:53 +00:00
|
|
|
expect(recordings.getCurrent()).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should emit a current changed event", () => {
|
|
|
|
expect(onCurrentChanged).toHaveBeenCalledWith(null);
|
|
|
|
});
|
2022-10-19 16:02:48 +00:00
|
|
|
|
|
|
|
it("and calling it again should work", () => {
|
|
|
|
recordings.clearCurrent();
|
|
|
|
expect(recordings.getCurrent()).toBeNull();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and setting another recording and stopping the previous recording", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
recordings.setCurrent(otherRecording);
|
|
|
|
recording.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should keep the current recording", () => {
|
|
|
|
expect(recordings.getCurrent()).toBe(otherRecording);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and the recording stops", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
recording.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should clear the current recording", () => {
|
|
|
|
expect(recordings.getCurrent()).toBeNull();
|
|
|
|
});
|
2022-10-13 12:02:53 +00:00
|
|
|
});
|
2022-09-28 08:22:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("getByInfoEventId", () => {
|
|
|
|
let returnedRecording: VoiceBroadcastRecording;
|
|
|
|
|
|
|
|
describe("when retrieving a known recording", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
recordings.setCurrent(recording);
|
|
|
|
returnedRecording = recordings.getByInfoEvent(infoEvent, client);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return the recording", () => {
|
|
|
|
expect(returnedRecording).toBe(recording);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when retrieving an unknown recording", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
returnedRecording = recordings.getByInfoEvent(infoEvent, client);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return the recording", () => {
|
2022-10-19 16:02:48 +00:00
|
|
|
expect(returnedRecording.infoEvent).toBe(infoEvent);
|
2022-09-28 08:22:50 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|