2022-11-10 08:38:48 +00: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 10:16:22 +00:00
|
|
|
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
2023-01-09 15:08:30 +00:00
|
|
|
import { SyncState } from "matrix-js-sdk/src/sync";
|
2022-11-10 08:38:48 +00:00
|
|
|
|
2023-01-09 15:08:30 +00:00
|
|
|
import Modal from "../../../src/Modal";
|
2022-11-10 08:38:48 +00:00
|
|
|
import {
|
2022-11-30 10:16:22 +00:00
|
|
|
VoiceBroadcastInfoState,
|
|
|
|
VoiceBroadcastPlayback,
|
|
|
|
VoiceBroadcastPlaybacksStore,
|
2022-11-10 08:38:48 +00: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 10:16:22 +00:00
|
|
|
import { mkVoiceBroadcastInfoStateEvent } from "./test-utils";
|
2022-11-10 08:38:48 +00:00
|
|
|
|
2023-01-09 15:08:30 +00:00
|
|
|
jest.mock("../../../src/Modal");
|
2022-11-10 08:38:48 +00:00
|
|
|
|
|
|
|
describe("setUpVoiceBroadcastPreRecording", () => {
|
|
|
|
const roomId = "!room:example.com";
|
|
|
|
let client: MatrixClient;
|
|
|
|
let userId: string;
|
|
|
|
let room: Room;
|
|
|
|
let preRecordingStore: VoiceBroadcastPreRecordingStore;
|
2022-11-30 10:16:22 +00:00
|
|
|
let infoEvent: MatrixEvent;
|
|
|
|
let playback: VoiceBroadcastPlayback;
|
|
|
|
let playbacksStore: VoiceBroadcastPlaybacksStore;
|
2022-11-10 08:38:48 +00:00
|
|
|
let recordingsStore: VoiceBroadcastRecordingsStore;
|
2022-12-28 15:13:37 +00:00
|
|
|
let preRecording: VoiceBroadcastPreRecording | null;
|
2022-11-10 08:38:48 +00:00
|
|
|
|
2022-12-28 15:13:37 +00:00
|
|
|
const itShouldNotCreateAPreRecording = () => {
|
2022-11-10 08:38:48 +00:00
|
|
|
it("should return null", () => {
|
2022-12-28 15:13:37 +00:00
|
|
|
expect(preRecording).toBeNull();
|
2022-11-10 08:38:48 +00:00
|
|
|
});
|
2022-12-28 15:13:37 +00: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 08:38:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client = stubClient();
|
|
|
|
|
|
|
|
const clientUserId = client.getUserId();
|
|
|
|
if (!clientUserId) fail("empty userId");
|
|
|
|
userId = clientUserId;
|
|
|
|
|
|
|
|
room = new Room(roomId, client, userId);
|
2022-11-30 10:16:22 +00:00
|
|
|
infoEvent = mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoState.Started,
|
|
|
|
client.getUserId()!,
|
|
|
|
client.getDeviceId()!,
|
|
|
|
);
|
2022-12-28 15:13:37 +00:00
|
|
|
preRecording = null;
|
2022-11-10 08:38:48 +00:00
|
|
|
preRecordingStore = new VoiceBroadcastPreRecordingStore();
|
|
|
|
recordingsStore = new VoiceBroadcastRecordingsStore();
|
2023-01-02 12:21:33 +00:00
|
|
|
playback = new VoiceBroadcastPlayback(infoEvent, client, recordingsStore);
|
|
|
|
jest.spyOn(playback, "pause");
|
|
|
|
playbacksStore = new VoiceBroadcastPlaybacksStore(recordingsStore);
|
2022-11-10 08:38:48 +00:00
|
|
|
});
|
|
|
|
|
2023-01-09 15:08:30 +00:00
|
|
|
describe("when trying to start a broadcast if there is no connection", () => {
|
2022-12-28 15:13:37 +00:00
|
|
|
beforeEach(async () => {
|
2023-01-09 15:08:30 +00:00
|
|
|
mocked(client.getSyncState).mockReturnValue(SyncState.Error);
|
2022-12-28 15:13:37 +00:00
|
|
|
await setUpPreRecording();
|
2022-11-10 08:38:48 +00:00
|
|
|
});
|
|
|
|
|
2023-01-09 15:08:30 +00:00
|
|
|
it("should show an info dialog and not set up a pre-recording", () => {
|
|
|
|
expect(preRecordingStore.getCurrent()).toBeNull();
|
|
|
|
expect(Modal.createDialog).toMatchSnapshot();
|
2022-11-10 08:38:48 +00:00
|
|
|
});
|
2023-01-09 15:08:30 +00:00
|
|
|
});
|
2022-11-10 08:38:48 +00:00
|
|
|
|
2023-01-09 15:08:30 +00:00
|
|
|
describe("when setting up a pre-recording", () => {
|
2022-11-10 08:38:48 +00:00
|
|
|
describe("and there is no user id", () => {
|
2022-12-28 15:13:37 +00:00
|
|
|
beforeEach(async () => {
|
2022-11-10 08:38:48 +00:00
|
|
|
mocked(client.getUserId).mockReturnValue(null);
|
2022-12-28 15:13:37 +00:00
|
|
|
await setUpPreRecording();
|
2022-11-10 08:38:48 +00:00
|
|
|
});
|
|
|
|
|
2022-12-28 15:13:37 +00:00
|
|
|
itShouldNotCreateAPreRecording();
|
2022-11-10 08:38:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("and there is no room member", () => {
|
2022-12-28 15:13:37 +00:00
|
|
|
beforeEach(async () => {
|
2022-11-10 08:38:48 +00:00
|
|
|
// check test precondition
|
|
|
|
expect(room.getMember(userId)).toBeNull();
|
2022-12-28 15:13:37 +00:00
|
|
|
await setUpPreRecording();
|
2022-11-10 08:38:48 +00:00
|
|
|
});
|
|
|
|
|
2022-12-28 15:13:37 +00:00
|
|
|
itShouldNotCreateAPreRecording();
|
2022-11-10 08:38:48 +00:00
|
|
|
});
|
|
|
|
|
2022-11-30 10:16:22 +00:00
|
|
|
describe("and there is a room member and listening to another broadcast", () => {
|
2023-01-09 15:08:30 +00:00
|
|
|
beforeEach(async () => {
|
2022-11-30 10:16:22 +00:00
|
|
|
playbacksStore.setCurrent(playback);
|
2022-11-10 08:38:48 +00:00
|
|
|
room.currentState.setStateEvents([mkRoomMemberJoinEvent(userId, roomId)]);
|
2023-01-09 15:08:30 +00:00
|
|
|
await setUpPreRecording();
|
2022-11-10 08:38:48 +00:00
|
|
|
});
|
|
|
|
|
2022-11-30 10:16:22 +00:00
|
|
|
it("should pause the current playback and create a voice broadcast pre-recording", () => {
|
|
|
|
expect(playback.pause).toHaveBeenCalled();
|
|
|
|
expect(playbacksStore.getCurrent()).toBeNull();
|
2022-12-28 15:13:37 +00:00
|
|
|
expect(preRecording).toBeInstanceOf(VoiceBroadcastPreRecording);
|
2022-11-10 08:38:48 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|