2022-11-10 16:38:48 +08:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { mocked } from "jest-mock";
|
2022-11-30 18:16:22 +08:00
|
|
|
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
2022-11-10 16:38:48 +08:00
|
|
|
|
|
|
|
import {
|
|
|
|
checkVoiceBroadcastPreConditions,
|
2022-11-30 18:16:22 +08:00
|
|
|
VoiceBroadcastInfoState,
|
|
|
|
VoiceBroadcastPlayback,
|
|
|
|
VoiceBroadcastPlaybacksStore,
|
2022-11-10 16:38:48 +08:00
|
|
|
VoiceBroadcastPreRecording,
|
|
|
|
VoiceBroadcastPreRecordingStore,
|
|
|
|
VoiceBroadcastRecordingsStore,
|
|
|
|
} from "../../../src/voice-broadcast";
|
|
|
|
import { setUpVoiceBroadcastPreRecording } from "../../../src/voice-broadcast/utils/setUpVoiceBroadcastPreRecording";
|
|
|
|
import { mkRoomMemberJoinEvent, stubClient } from "../../test-utils";
|
2022-11-30 18:16:22 +08:00
|
|
|
import { mkVoiceBroadcastInfoStateEvent } from "./test-utils";
|
2022-11-10 16:38:48 +08:00
|
|
|
|
|
|
|
jest.mock("../../../src/voice-broadcast/utils/checkVoiceBroadcastPreConditions");
|
|
|
|
|
|
|
|
describe("setUpVoiceBroadcastPreRecording", () => {
|
|
|
|
const roomId = "!room:example.com";
|
|
|
|
let client: MatrixClient;
|
|
|
|
let userId: string;
|
|
|
|
let room: Room;
|
|
|
|
let preRecordingStore: VoiceBroadcastPreRecordingStore;
|
2022-11-30 18:16:22 +08:00
|
|
|
let infoEvent: MatrixEvent;
|
|
|
|
let playback: VoiceBroadcastPlayback;
|
|
|
|
let playbacksStore: VoiceBroadcastPlaybacksStore;
|
2022-11-10 16:38:48 +08:00
|
|
|
let recordingsStore: VoiceBroadcastRecordingsStore;
|
2022-12-28 23:13:37 +08:00
|
|
|
let preRecording: VoiceBroadcastPreRecording | null;
|
2022-11-10 16:38:48 +08:00
|
|
|
|
2022-12-28 23:13:37 +08:00
|
|
|
const itShouldNotCreateAPreRecording = () => {
|
2022-11-10 16:38:48 +08:00
|
|
|
it("should return null", () => {
|
2022-12-28 23:13:37 +08:00
|
|
|
expect(preRecording).toBeNull();
|
2022-11-10 16:38:48 +08:00
|
|
|
});
|
2022-12-28 23:13:37 +08:00
|
|
|
|
|
|
|
it("should not create a broadcast pre recording", () => {
|
|
|
|
expect(preRecordingStore.getCurrent()).toBeNull();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const setUpPreRecording = async () => {
|
|
|
|
preRecording = await setUpVoiceBroadcastPreRecording(
|
|
|
|
room,
|
|
|
|
client,
|
|
|
|
playbacksStore,
|
|
|
|
recordingsStore,
|
|
|
|
preRecordingStore,
|
|
|
|
);
|
2022-11-10 16:38:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client = stubClient();
|
|
|
|
|
|
|
|
const clientUserId = client.getUserId();
|
|
|
|
if (!clientUserId) fail("empty userId");
|
|
|
|
userId = clientUserId;
|
|
|
|
|
|
|
|
room = new Room(roomId, client, userId);
|
2022-11-30 18:16:22 +08:00
|
|
|
infoEvent = mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoState.Started,
|
|
|
|
client.getUserId()!,
|
|
|
|
client.getDeviceId()!,
|
|
|
|
);
|
2022-12-28 23:13:37 +08:00
|
|
|
preRecording = null;
|
2022-11-10 16:38:48 +08:00
|
|
|
preRecordingStore = new VoiceBroadcastPreRecordingStore();
|
2022-11-30 18:16:22 +08:00
|
|
|
playback = new VoiceBroadcastPlayback(infoEvent, client);
|
|
|
|
jest.spyOn(playback, "pause");
|
|
|
|
playbacksStore = new VoiceBroadcastPlaybacksStore();
|
2022-11-10 16:38:48 +08:00
|
|
|
recordingsStore = new VoiceBroadcastRecordingsStore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when the preconditions fail", () => {
|
2022-12-28 23:13:37 +08:00
|
|
|
beforeEach(async () => {
|
2022-12-22 18:37:07 +08:00
|
|
|
mocked(checkVoiceBroadcastPreConditions).mockResolvedValue(false);
|
2022-12-28 23:13:37 +08:00
|
|
|
await setUpPreRecording();
|
2022-11-10 16:38:48 +08:00
|
|
|
});
|
|
|
|
|
2022-12-28 23:13:37 +08:00
|
|
|
itShouldNotCreateAPreRecording();
|
2022-11-10 16:38:48 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("when the preconditions pass", () => {
|
|
|
|
beforeEach(() => {
|
2022-12-22 18:37:07 +08:00
|
|
|
mocked(checkVoiceBroadcastPreConditions).mockResolvedValue(true);
|
2022-11-10 16:38:48 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("and there is no user id", () => {
|
2022-12-28 23:13:37 +08:00
|
|
|
beforeEach(async () => {
|
2022-11-10 16:38:48 +08:00
|
|
|
mocked(client.getUserId).mockReturnValue(null);
|
2022-12-28 23:13:37 +08:00
|
|
|
await setUpPreRecording();
|
2022-11-10 16:38:48 +08:00
|
|
|
});
|
|
|
|
|
2022-12-28 23:13:37 +08:00
|
|
|
itShouldNotCreateAPreRecording();
|
2022-11-10 16:38:48 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("and there is no room member", () => {
|
2022-12-28 23:13:37 +08:00
|
|
|
beforeEach(async () => {
|
2022-11-10 16:38:48 +08:00
|
|
|
// check test precondition
|
|
|
|
expect(room.getMember(userId)).toBeNull();
|
2022-12-28 23:13:37 +08:00
|
|
|
await setUpPreRecording();
|
2022-11-10 16:38:48 +08:00
|
|
|
});
|
|
|
|
|
2022-12-28 23:13:37 +08:00
|
|
|
itShouldNotCreateAPreRecording();
|
2022-11-10 16:38:48 +08:00
|
|
|
});
|
|
|
|
|
2022-11-30 18:16:22 +08:00
|
|
|
describe("and there is a room member and listening to another broadcast", () => {
|
2022-11-10 16:38:48 +08:00
|
|
|
beforeEach(() => {
|
2022-11-30 18:16:22 +08:00
|
|
|
playbacksStore.setCurrent(playback);
|
2022-12-12 19:24:14 +08:00
|
|
|
room.currentState.setStateEvents([mkRoomMemberJoinEvent(userId, roomId)]);
|
2022-12-28 23:13:37 +08:00
|
|
|
setUpPreRecording();
|
2022-11-10 16:38:48 +08:00
|
|
|
});
|
|
|
|
|
2022-11-30 18:16:22 +08:00
|
|
|
it("should pause the current playback and create a voice broadcast pre-recording", () => {
|
|
|
|
expect(playback.pause).toHaveBeenCalled();
|
|
|
|
expect(playbacksStore.getCurrent()).toBeNull();
|
|
|
|
|
2022-11-10 16:38:48 +08:00
|
|
|
expect(checkVoiceBroadcastPreConditions).toHaveBeenCalledWith(room, client, recordingsStore);
|
2022-12-28 23:13:37 +08:00
|
|
|
expect(preRecording).toBeInstanceOf(VoiceBroadcastPreRecording);
|
2022-11-10 16:38:48 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|