2022-10-21 15:30:02 +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";
|
|
|
|
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
|
|
|
|
|
|
|
import {
|
|
|
|
findRoomLiveVoiceBroadcastFromUserAndDevice,
|
|
|
|
VoiceBroadcastInfoEventType,
|
|
|
|
VoiceBroadcastInfoState,
|
|
|
|
} from "../../../src/voice-broadcast";
|
|
|
|
import { mkEvent, stubClient } from "../../test-utils";
|
|
|
|
import { mkVoiceBroadcastInfoStateEvent } from "./test-utils";
|
|
|
|
|
|
|
|
describe("findRoomLiveVoiceBroadcastFromUserAndDevice", () => {
|
|
|
|
const roomId = "!room:example.com";
|
|
|
|
let client: MatrixClient;
|
|
|
|
let room: Room;
|
|
|
|
|
|
|
|
const itShouldReturnNull = () => {
|
|
|
|
it("should return null", () => {
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(
|
2023-02-14 01:01:43 +08:00
|
|
|
findRoomLiveVoiceBroadcastFromUserAndDevice(room, client.getUserId()!, client.getDeviceId()!),
|
2022-12-12 19:24:14 +08:00
|
|
|
).toBeNull();
|
2022-10-21 15:30:02 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
client = stubClient();
|
2023-02-14 01:01:43 +08:00
|
|
|
room = new Room(roomId, client, client.getUserId()!);
|
2022-10-21 15:30:02 +08:00
|
|
|
jest.spyOn(room.currentState, "getStateEvents");
|
|
|
|
mocked(client.getRoom).mockImplementation((getRoomId: string) => {
|
|
|
|
if (getRoomId === roomId) return room;
|
2023-02-14 01:01:43 +08:00
|
|
|
return null;
|
2022-10-21 15:30:02 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is no info event", () => {
|
|
|
|
itShouldReturnNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is an info event without content", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
room.currentState.setStateEvents([
|
|
|
|
mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: VoiceBroadcastInfoEventType,
|
|
|
|
room: roomId,
|
2023-02-14 01:01:43 +08:00
|
|
|
user: client.getUserId()!,
|
2022-10-21 15:30:02 +08:00
|
|
|
content: {},
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is a stopped info event", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
room.currentState.setStateEvents([
|
|
|
|
mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoState.Stopped,
|
2023-02-14 01:01:43 +08:00
|
|
|
client.getUserId()!,
|
2022-10-21 15:30:02 +08:00
|
|
|
client.getDeviceId(),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is a started info event from another device", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
const event = mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoState.Stopped,
|
2023-02-14 01:01:43 +08:00
|
|
|
client.getUserId()!,
|
2022-10-21 15:30:02 +08:00
|
|
|
"JKL123",
|
|
|
|
);
|
|
|
|
room.currentState.setStateEvents([event]);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldReturnNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is a started info event", () => {
|
|
|
|
let event: MatrixEvent;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
event = mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoState.Started,
|
2023-02-14 01:01:43 +08:00
|
|
|
client.getUserId()!,
|
2022-10-21 15:30:02 +08:00
|
|
|
client.getDeviceId(),
|
|
|
|
);
|
|
|
|
room.currentState.setStateEvents([event]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return this event", () => {
|
|
|
|
expect(room.currentState.getStateEvents).toHaveBeenCalledWith(
|
|
|
|
VoiceBroadcastInfoEventType,
|
2023-02-14 01:01:43 +08:00
|
|
|
client.getUserId()!,
|
2022-10-21 15:30:02 +08:00
|
|
|
);
|
|
|
|
|
2023-02-14 01:01:43 +08:00
|
|
|
expect(findRoomLiveVoiceBroadcastFromUserAndDevice(room, client.getUserId()!, client.getDeviceId()!)).toBe(
|
2022-12-12 19:24:14 +08:00
|
|
|
event,
|
|
|
|
);
|
2022-10-21 15:30:02 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|