2022-10-19 15:01:14 +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.
|
|
|
|
*/
|
|
|
|
|
2022-11-24 09:08:41 +01:00
|
|
|
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
2022-10-19 15:01:14 +02:00
|
|
|
|
|
|
|
import {
|
|
|
|
hasRoomLiveVoiceBroadcast,
|
|
|
|
VoiceBroadcastInfoEventType,
|
|
|
|
VoiceBroadcastInfoState,
|
|
|
|
} from "../../../src/voice-broadcast";
|
|
|
|
import { mkEvent, stubClient } from "../../test-utils";
|
|
|
|
import { mkVoiceBroadcastInfoStateEvent } from "./test-utils";
|
|
|
|
|
|
|
|
describe("hasRoomLiveVoiceBroadcast", () => {
|
|
|
|
const otherUserId = "@other:example.com";
|
|
|
|
const roomId = "!room:example.com";
|
|
|
|
let client: MatrixClient;
|
|
|
|
let room: Room;
|
2022-11-24 09:08:41 +01:00
|
|
|
let expectedEvent: MatrixEvent | null = null;
|
2022-10-19 15:01:14 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const addVoiceBroadcastInfoEvent = (state: VoiceBroadcastInfoState, sender: string): MatrixEvent => {
|
|
|
|
const infoEvent = mkVoiceBroadcastInfoStateEvent(room.roomId, state, sender, "ASD123");
|
2022-11-24 09:08:41 +01:00
|
|
|
room.currentState.setStateEvents([infoEvent]);
|
|
|
|
return infoEvent;
|
2022-10-19 15:01:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const itShouldReturnTrueTrue = () => {
|
|
|
|
it("should return true/true", () => {
|
|
|
|
expect(hasRoomLiveVoiceBroadcast(room, client.getUserId())).toEqual({
|
|
|
|
hasBroadcast: true,
|
2022-11-24 09:08:41 +01:00
|
|
|
infoEvent: expectedEvent,
|
2022-10-19 15:01:14 +02:00
|
|
|
startedByUser: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const itShouldReturnTrueFalse = () => {
|
|
|
|
it("should return true/false", () => {
|
|
|
|
expect(hasRoomLiveVoiceBroadcast(room, client.getUserId())).toEqual({
|
|
|
|
hasBroadcast: true,
|
2022-11-24 09:08:41 +01:00
|
|
|
infoEvent: expectedEvent,
|
2022-10-19 15:01:14 +02:00
|
|
|
startedByUser: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const itShouldReturnFalseFalse = () => {
|
|
|
|
it("should return false/false", () => {
|
|
|
|
expect(hasRoomLiveVoiceBroadcast(room, client.getUserId())).toEqual({
|
|
|
|
hasBroadcast: false,
|
2022-11-24 09:08:41 +01:00
|
|
|
infoEvent: null,
|
2022-10-19 15:01:14 +02:00
|
|
|
startedByUser: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
client = stubClient();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-11-24 09:08:41 +01:00
|
|
|
expectedEvent = null;
|
2022-10-19 15:01:14 +02:00
|
|
|
room = new Room(roomId, client, client.getUserId());
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is no voice broadcast info at all", () => {
|
|
|
|
itShouldReturnFalseFalse();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when the »state« prop is missing", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
room.currentState.setStateEvents([
|
|
|
|
mkEvent({
|
|
|
|
event: true,
|
|
|
|
room: room.roomId,
|
|
|
|
user: client.getUserId(),
|
|
|
|
type: VoiceBroadcastInfoEventType,
|
|
|
|
skey: client.getUserId(),
|
|
|
|
content: {},
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
itShouldReturnFalseFalse();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is a live broadcast from the current and another user", () => {
|
|
|
|
beforeEach(() => {
|
2022-11-24 09:08:41 +01:00
|
|
|
expectedEvent = addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Started, client.getUserId());
|
2022-10-19 15:01:14 +02:00
|
|
|
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Started, otherUserId);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnTrueTrue();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there are only stopped info events", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Stopped, client.getUserId());
|
|
|
|
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Stopped, otherUserId);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnFalseFalse();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe.each([
|
|
|
|
// all there are kind of live states
|
|
|
|
VoiceBroadcastInfoState.Started,
|
|
|
|
VoiceBroadcastInfoState.Paused,
|
2022-10-26 14:54:44 +02:00
|
|
|
VoiceBroadcastInfoState.Resumed,
|
2022-10-19 15:01:14 +02:00
|
|
|
])("when there is a live broadcast (%s) from the current user", (state: VoiceBroadcastInfoState) => {
|
|
|
|
beforeEach(() => {
|
2022-11-24 09:08:41 +01:00
|
|
|
expectedEvent = addVoiceBroadcastInfoEvent(state, client.getUserId());
|
2022-10-19 15:01:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnTrueTrue();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there was a live broadcast, that has been stopped", () => {
|
|
|
|
beforeEach(() => {
|
2022-10-26 14:54:44 +02:00
|
|
|
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Resumed, client.getUserId());
|
2022-10-19 15:01:14 +02:00
|
|
|
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Stopped, client.getUserId());
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnFalseFalse();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is a live broadcast from another user", () => {
|
|
|
|
beforeEach(() => {
|
2022-11-24 09:08:41 +01:00
|
|
|
expectedEvent = addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Resumed, otherUserId);
|
2022-10-19 15:01:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnTrueFalse();
|
|
|
|
});
|
|
|
|
});
|