2022-09-28 11:22:50 +03: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-10-19 16:01:14 +03:00
|
|
|
import { EventType, MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
2022-09-28 11:22:50 +03:00
|
|
|
|
2022-10-19 16:01:14 +03:00
|
|
|
import Modal from "../../../src/Modal";
|
2022-09-28 11:22:50 +03:00
|
|
|
import {
|
|
|
|
startNewVoiceBroadcastRecording,
|
|
|
|
VoiceBroadcastInfoEventType,
|
|
|
|
VoiceBroadcastInfoState,
|
|
|
|
VoiceBroadcastRecordingsStore,
|
|
|
|
VoiceBroadcastRecording,
|
|
|
|
} from "../../../src/voice-broadcast";
|
|
|
|
import { mkEvent, stubClient } from "../../test-utils";
|
2022-10-19 16:01:14 +03:00
|
|
|
import { mkVoiceBroadcastInfoStateEvent } from "./test-utils";
|
2022-09-28 11:22:50 +03:00
|
|
|
|
|
|
|
jest.mock("../../../src/voice-broadcast/models/VoiceBroadcastRecording", () => ({
|
|
|
|
VoiceBroadcastRecording: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-10-19 16:01:14 +03:00
|
|
|
jest.mock("../../../src/Modal");
|
|
|
|
|
2022-09-28 11:22:50 +03:00
|
|
|
describe("startNewVoiceBroadcastRecording", () => {
|
|
|
|
const roomId = "!room:example.com";
|
2022-10-19 16:01:14 +03:00
|
|
|
const otherUserId = "@other:example.com";
|
2022-09-28 11:22:50 +03:00
|
|
|
let client: MatrixClient;
|
|
|
|
let recordingsStore: VoiceBroadcastRecordingsStore;
|
|
|
|
let room: Room;
|
|
|
|
let infoEvent: MatrixEvent;
|
|
|
|
let otherEvent: MatrixEvent;
|
2022-10-19 16:01:14 +03:00
|
|
|
let result: VoiceBroadcastRecording | null;
|
2022-09-28 11:22:50 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client = stubClient();
|
2022-10-19 16:01:14 +03:00
|
|
|
room = new Room(roomId, client, client.getUserId());
|
|
|
|
jest.spyOn(room.currentState, "maySendStateEvent");
|
|
|
|
|
2022-09-28 11:22:50 +03:00
|
|
|
mocked(client.getRoom).mockImplementation((getRoomId: string) => {
|
|
|
|
if (getRoomId === roomId) {
|
|
|
|
return room;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
mocked(client.sendStateEvent).mockImplementation((
|
|
|
|
sendRoomId: string,
|
|
|
|
eventType: string,
|
|
|
|
_content: any,
|
|
|
|
_stateKey: string,
|
|
|
|
) => {
|
|
|
|
if (sendRoomId === roomId && eventType === VoiceBroadcastInfoEventType) {
|
|
|
|
return Promise.resolve({ event_id: infoEvent.getId() });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
recordingsStore = {
|
|
|
|
setCurrent: jest.fn(),
|
2022-10-19 17:22:07 +03:00
|
|
|
getCurrent: jest.fn(),
|
2022-09-28 11:22:50 +03:00
|
|
|
} as unknown as VoiceBroadcastRecordingsStore;
|
|
|
|
|
2022-10-21 10:30:02 +03:00
|
|
|
infoEvent = mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoState.Started,
|
|
|
|
client.getUserId(),
|
|
|
|
client.getDeviceId(),
|
|
|
|
);
|
2022-09-28 11:22:50 +03:00
|
|
|
otherEvent = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMember,
|
|
|
|
content: {},
|
|
|
|
user: client.getUserId(),
|
|
|
|
room: roomId,
|
2022-10-19 16:01:14 +03:00
|
|
|
skey: "",
|
2022-09-28 11:22:50 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
mocked(VoiceBroadcastRecording).mockImplementation((
|
|
|
|
infoEvent: MatrixEvent,
|
|
|
|
client: MatrixClient,
|
|
|
|
) => {
|
|
|
|
return {
|
|
|
|
infoEvent,
|
|
|
|
client,
|
2022-10-12 01:31:28 +03:00
|
|
|
start: jest.fn(),
|
2022-09-28 11:22:50 +03:00
|
|
|
} as unknown as VoiceBroadcastRecording;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-19 16:01:14 +03: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 11:22:50 +03:00
|
|
|
|
2022-10-19 16:01:14 +03:00
|
|
|
describe("when there currently is no other broadcast", () => {
|
|
|
|
it("should create a new Voice Broadcast", async () => {
|
|
|
|
mocked(client.sendStateEvent).mockImplementation(async (
|
|
|
|
_roomId: string,
|
|
|
|
_eventType: string,
|
|
|
|
_content: any,
|
|
|
|
_stateKey = "",
|
|
|
|
) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
// emit state events after resolving the promise
|
|
|
|
room.currentState.setStateEvents([otherEvent]);
|
|
|
|
room.currentState.setStateEvents([infoEvent]);
|
|
|
|
}, 0);
|
|
|
|
return { event_id: infoEvent.getId() };
|
|
|
|
});
|
|
|
|
const recording = await startNewVoiceBroadcastRecording(room, client, recordingsStore);
|
|
|
|
|
|
|
|
expect(client.sendStateEvent).toHaveBeenCalledWith(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoEventType,
|
|
|
|
{
|
2022-10-20 16:41:10 +03:00
|
|
|
chunk_length: 120,
|
2022-10-19 16:01:14 +03:00
|
|
|
device_id: client.getDeviceId(),
|
|
|
|
state: VoiceBroadcastInfoState.Started,
|
|
|
|
},
|
|
|
|
client.getUserId(),
|
|
|
|
);
|
|
|
|
expect(recording.infoEvent).toBe(infoEvent);
|
|
|
|
expect(recording.start).toHaveBeenCalled();
|
|
|
|
});
|
2022-09-28 11:22:50 +03:00
|
|
|
});
|
|
|
|
|
2022-10-19 17:22:07 +03:00
|
|
|
describe("when there is already a current voice broadcast", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mocked(recordingsStore.getCurrent).mockReturnValue(
|
|
|
|
new VoiceBroadcastRecording(infoEvent, client),
|
|
|
|
);
|
|
|
|
|
|
|
|
result = await startNewVoiceBroadcastRecording(room, client, recordingsStore);
|
|
|
|
});
|
|
|
|
|
|
|
|
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 16:01:14 +03:00
|
|
|
beforeEach(async () => {
|
|
|
|
room.currentState.setStateEvents([
|
2022-10-21 10:30:02 +03:00
|
|
|
mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoState.Running,
|
|
|
|
client.getUserId(),
|
|
|
|
client.getDeviceId(),
|
|
|
|
),
|
2022-10-19 16:01:14 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
result = await startNewVoiceBroadcastRecording(room, client, recordingsStore);
|
|
|
|
});
|
2022-09-28 11:22:50 +03:00
|
|
|
|
2022-10-19 16:01:14 +03: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 10:30:02 +03:00
|
|
|
mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoState.Running,
|
|
|
|
otherUserId,
|
|
|
|
"ASD123",
|
|
|
|
),
|
2022-10-19 16:01:14 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
result = await startNewVoiceBroadcastRecording(room, client, recordingsStore);
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
result = await startNewVoiceBroadcastRecording(room, client, recordingsStore);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not start a voice broadcast", () => {
|
|
|
|
expect(result).toBeNull();
|
|
|
|
});
|
2022-09-28 11:22:50 +03:00
|
|
|
|
2022-10-19 16:01:14 +03:00
|
|
|
it("should show an info dialog", () => {
|
|
|
|
expect(Modal.createDialog).toMatchSnapshot();
|
2022-09-28 11:22:50 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|