2022-09-28 10:22:50 +02: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 11:16:22 +01:00
|
|
|
import { EventType, ISendEventResponse, MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
2022-09-28 10:22:50 +02:00
|
|
|
|
2022-10-19 15:01:14 +02:00
|
|
|
import Modal from "../../../src/Modal";
|
2022-09-28 10:22:50 +02:00
|
|
|
import {
|
|
|
|
startNewVoiceBroadcastRecording,
|
|
|
|
VoiceBroadcastInfoEventType,
|
|
|
|
VoiceBroadcastInfoState,
|
|
|
|
VoiceBroadcastRecordingsStore,
|
|
|
|
VoiceBroadcastRecording,
|
2022-11-30 11:16:22 +01:00
|
|
|
VoiceBroadcastPlaybacksStore,
|
|
|
|
VoiceBroadcastPlayback,
|
2022-09-28 10:22:50 +02:00
|
|
|
} from "../../../src/voice-broadcast";
|
|
|
|
import { mkEvent, stubClient } from "../../test-utils";
|
2022-10-19 15:01:14 +02:00
|
|
|
import { mkVoiceBroadcastInfoStateEvent } from "./test-utils";
|
2022-09-28 10:22:50 +02:00
|
|
|
|
|
|
|
jest.mock("../../../src/voice-broadcast/models/VoiceBroadcastRecording", () => ({
|
|
|
|
VoiceBroadcastRecording: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-10-19 15:01:14 +02:00
|
|
|
jest.mock("../../../src/Modal");
|
|
|
|
|
2022-09-28 10:22:50 +02:00
|
|
|
describe("startNewVoiceBroadcastRecording", () => {
|
|
|
|
const roomId = "!room:example.com";
|
2022-10-19 15:01:14 +02:00
|
|
|
const otherUserId = "@other:example.com";
|
2022-09-28 10:22:50 +02:00
|
|
|
let client: MatrixClient;
|
2022-11-30 11:16:22 +01:00
|
|
|
let playbacksStore: VoiceBroadcastPlaybacksStore;
|
2022-09-28 10:22:50 +02:00
|
|
|
let recordingsStore: VoiceBroadcastRecordingsStore;
|
|
|
|
let room: Room;
|
|
|
|
let infoEvent: MatrixEvent;
|
|
|
|
let otherEvent: MatrixEvent;
|
2022-10-19 15:01:14 +02:00
|
|
|
let result: VoiceBroadcastRecording | null;
|
2022-09-28 10:22:50 +02:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client = stubClient();
|
2022-11-30 11:16:22 +01:00
|
|
|
room = new Room(roomId, client, client.getUserId()!);
|
2022-10-19 15:01:14 +02:00
|
|
|
jest.spyOn(room.currentState, "maySendStateEvent");
|
|
|
|
|
2022-09-28 10:22:50 +02:00
|
|
|
mocked(client.getRoom).mockImplementation((getRoomId: string) => {
|
|
|
|
if (getRoomId === roomId) {
|
|
|
|
return room;
|
|
|
|
}
|
2022-11-30 11:16:22 +01:00
|
|
|
|
|
|
|
return null;
|
2022-09-28 10:22:50 +02:00
|
|
|
});
|
|
|
|
mocked(client.sendStateEvent).mockImplementation((
|
|
|
|
sendRoomId: string,
|
|
|
|
eventType: string,
|
2022-11-30 11:16:22 +01:00
|
|
|
content: any,
|
|
|
|
stateKey: string,
|
|
|
|
): Promise<ISendEventResponse> => {
|
2022-09-28 10:22:50 +02:00
|
|
|
if (sendRoomId === roomId && eventType === VoiceBroadcastInfoEventType) {
|
2022-11-30 11:16:22 +01:00
|
|
|
return Promise.resolve({ event_id: infoEvent.getId()! });
|
2022-09-28 10:22:50 +02:00
|
|
|
}
|
|
|
|
|
2022-11-30 11:16:22 +01:00
|
|
|
throw new Error("Unexpected sendStateEvent call");
|
|
|
|
});
|
2022-09-28 10:22:50 +02:00
|
|
|
|
2022-10-21 09:30:02 +02:00
|
|
|
infoEvent = mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoState.Started,
|
2022-11-30 11:16:22 +01:00
|
|
|
client.getUserId()!,
|
|
|
|
client.getDeviceId()!,
|
2022-10-21 09:30:02 +02:00
|
|
|
);
|
2022-09-28 10:22:50 +02:00
|
|
|
otherEvent = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMember,
|
|
|
|
content: {},
|
2022-11-30 11:16:22 +01:00
|
|
|
user: client.getUserId()!,
|
2022-09-28 10:22:50 +02:00
|
|
|
room: roomId,
|
2022-10-19 15:01:14 +02:00
|
|
|
skey: "",
|
2022-09-28 10:22:50 +02:00
|
|
|
});
|
|
|
|
|
2022-11-30 11:16:22 +01:00
|
|
|
playbacksStore = new VoiceBroadcastPlaybacksStore();
|
|
|
|
recordingsStore = {
|
|
|
|
setCurrent: jest.fn(),
|
|
|
|
getCurrent: jest.fn(),
|
|
|
|
} as unknown as VoiceBroadcastRecordingsStore;
|
|
|
|
|
2022-09-28 10:22:50 +02:00
|
|
|
mocked(VoiceBroadcastRecording).mockImplementation((
|
|
|
|
infoEvent: MatrixEvent,
|
|
|
|
client: MatrixClient,
|
2022-11-04 11:48:08 +01:00
|
|
|
): any => {
|
2022-09-28 10:22:50 +02:00
|
|
|
return {
|
|
|
|
infoEvent,
|
|
|
|
client,
|
2022-10-12 00:31:28 +02:00
|
|
|
start: jest.fn(),
|
2022-09-28 10:22:50 +02:00
|
|
|
} as unknown as VoiceBroadcastRecording;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-19 15:01:14 +02:00
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when the current user is allowed to send voice broadcast info state events", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(room.currentState.maySendStateEvent).mockReturnValue(true);
|
|
|
|
});
|
2022-09-28 10:22:50 +02:00
|
|
|
|
2022-11-30 11:16:22 +01:00
|
|
|
describe("when currently listening to a broadcast and there is no recording", () => {
|
|
|
|
let playback: VoiceBroadcastPlayback;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
playback = new VoiceBroadcastPlayback(infoEvent, client);
|
|
|
|
jest.spyOn(playback, "pause");
|
|
|
|
playbacksStore.setCurrent(playback);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should stop listen to the current broadcast and create a new recording", async () => {
|
2022-10-19 15:01:14 +02:00
|
|
|
mocked(client.sendStateEvent).mockImplementation(async (
|
|
|
|
_roomId: string,
|
|
|
|
_eventType: string,
|
|
|
|
_content: any,
|
|
|
|
_stateKey = "",
|
2022-11-30 11:16:22 +01:00
|
|
|
): Promise<ISendEventResponse> => {
|
2022-11-30 12:32:56 +01:00
|
|
|
window.setTimeout(() => {
|
2022-10-19 15:01:14 +02:00
|
|
|
// emit state events after resolving the promise
|
|
|
|
room.currentState.setStateEvents([otherEvent]);
|
|
|
|
room.currentState.setStateEvents([infoEvent]);
|
|
|
|
}, 0);
|
2022-11-30 11:16:22 +01:00
|
|
|
return { event_id: infoEvent.getId()! };
|
2022-10-19 15:01:14 +02:00
|
|
|
});
|
2022-11-30 11:16:22 +01:00
|
|
|
const recording = await startNewVoiceBroadcastRecording(room, client, playbacksStore, recordingsStore);
|
|
|
|
expect(recording).not.toBeNull();
|
|
|
|
|
|
|
|
// expect to stop and clear the current playback
|
|
|
|
expect(playback.pause).toHaveBeenCalled();
|
|
|
|
expect(playbacksStore.getCurrent()).toBeNull();
|
2022-10-19 15:01:14 +02:00
|
|
|
|
|
|
|
expect(client.sendStateEvent).toHaveBeenCalledWith(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoEventType,
|
|
|
|
{
|
2022-10-20 15:41:10 +02:00
|
|
|
chunk_length: 120,
|
2022-10-19 15:01:14 +02:00
|
|
|
device_id: client.getDeviceId(),
|
|
|
|
state: VoiceBroadcastInfoState.Started,
|
|
|
|
},
|
|
|
|
client.getUserId(),
|
|
|
|
);
|
2022-11-30 11:16:22 +01:00
|
|
|
expect(recording!.infoEvent).toBe(infoEvent);
|
|
|
|
expect(recording!.start).toHaveBeenCalled();
|
2022-10-19 15:01:14 +02:00
|
|
|
});
|
2022-09-28 10:22:50 +02:00
|
|
|
});
|
|
|
|
|
2022-10-19 16:22:07 +02:00
|
|
|
describe("when there is already a current voice broadcast", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mocked(recordingsStore.getCurrent).mockReturnValue(
|
|
|
|
new VoiceBroadcastRecording(infoEvent, client),
|
|
|
|
);
|
|
|
|
|
2022-11-30 11:16:22 +01:00
|
|
|
result = await startNewVoiceBroadcastRecording(room, client, playbacksStore, recordingsStore);
|
2022-10-19 16:22:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not start a voice broadcast", () => {
|
|
|
|
expect(result).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show an info dialog", () => {
|
|
|
|
expect(Modal.createDialog).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there already is a live broadcast of the current user in the room", () => {
|
2022-10-19 15:01:14 +02:00
|
|
|
beforeEach(async () => {
|
|
|
|
room.currentState.setStateEvents([
|
2022-10-21 09:30:02 +02:00
|
|
|
mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
2022-10-26 14:54:44 +02:00
|
|
|
VoiceBroadcastInfoState.Resumed,
|
2022-11-30 11:16:22 +01:00
|
|
|
client.getUserId()!,
|
|
|
|
client.getDeviceId()!,
|
2022-10-21 09:30:02 +02:00
|
|
|
),
|
2022-10-19 15:01:14 +02:00
|
|
|
]);
|
|
|
|
|
2022-11-30 11:16:22 +01:00
|
|
|
result = await startNewVoiceBroadcastRecording(room, client, playbacksStore, recordingsStore);
|
2022-10-19 15:01:14 +02:00
|
|
|
});
|
2022-09-28 10:22:50 +02:00
|
|
|
|
2022-10-19 15:01:14 +02:00
|
|
|
it("should not start a voice broadcast", () => {
|
|
|
|
expect(result).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show an info dialog", () => {
|
|
|
|
expect(Modal.createDialog).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there already is a live broadcast of another user", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
room.currentState.setStateEvents([
|
2022-10-21 09:30:02 +02:00
|
|
|
mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
2022-10-26 14:54:44 +02:00
|
|
|
VoiceBroadcastInfoState.Resumed,
|
2022-10-21 09:30:02 +02:00
|
|
|
otherUserId,
|
|
|
|
"ASD123",
|
|
|
|
),
|
2022-10-19 15:01:14 +02:00
|
|
|
]);
|
|
|
|
|
2022-11-30 11:16:22 +01:00
|
|
|
result = await startNewVoiceBroadcastRecording(room, client, playbacksStore, recordingsStore);
|
2022-10-19 15:01:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not start a voice broadcast", () => {
|
|
|
|
expect(result).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show an info dialog", () => {
|
|
|
|
expect(Modal.createDialog).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when the current user is not allowed to send voice broadcast info state events", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mocked(room.currentState.maySendStateEvent).mockReturnValue(false);
|
2022-11-30 11:16:22 +01:00
|
|
|
result = await startNewVoiceBroadcastRecording(room, client, playbacksStore, recordingsStore);
|
2022-10-19 15:01:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not start a voice broadcast", () => {
|
|
|
|
expect(result).toBeNull();
|
|
|
|
});
|
2022-09-28 10:22:50 +02:00
|
|
|
|
2022-10-19 15:01:14 +02:00
|
|
|
it("should show an info dialog", () => {
|
|
|
|
expect(Modal.createDialog).toMatchSnapshot();
|
2022-09-28 10:22:50 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|