Add voice broadcast recording stores (#9319)

* Add voice broadcast recording stores

* Refactor startNewVoiceBroadcastRecording
This commit is contained in:
Michael Weimann 2022-09-28 10:22:50 +02:00 committed by GitHub
parent c14191bfb6
commit d775e403c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 769 additions and 119 deletions

View file

@ -0,0 +1,158 @@
/*
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 { EventTimelineSet, EventType, MatrixClient, MatrixEvent, RelationType, Room } from "matrix-js-sdk/src/matrix";
import { Relations } from "matrix-js-sdk/src/models/relations";
import {
VoiceBroadcastInfoEventContent,
VoiceBroadcastInfoEventType,
VoiceBroadcastInfoState,
VoiceBroadcastRecording,
VoiceBroadcastRecordingEvent,
} from "../../../src/voice-broadcast";
import { mkEvent, mkStubRoom, stubClient } from "../../test-utils";
describe("VoiceBroadcastRecording", () => {
const roomId = "!room:example.com";
let room: Room;
let client: MatrixClient;
let infoEvent: MatrixEvent;
let voiceBroadcastRecording: VoiceBroadcastRecording;
let onStateChanged: (state: VoiceBroadcastInfoState) => void;
const mkVoiceBroadcastInfoEvent = (content: VoiceBroadcastInfoEventContent) => {
return mkEvent({
event: true,
type: VoiceBroadcastInfoEventType,
user: client.getUserId(),
room: roomId,
content,
});
};
const setUpVoiceBroadcastRecording = () => {
voiceBroadcastRecording = new VoiceBroadcastRecording(infoEvent, client);
voiceBroadcastRecording.on(VoiceBroadcastRecordingEvent.StateChanged, onStateChanged);
};
beforeEach(() => {
client = stubClient();
room = mkStubRoom(roomId, "Test Room", client);
mocked(client.getRoom).mockImplementation((getRoomId: string) => {
if (getRoomId === roomId) {
return room;
}
});
onStateChanged = jest.fn();
});
afterEach(() => {
voiceBroadcastRecording.off(VoiceBroadcastRecordingEvent.StateChanged, onStateChanged);
});
describe("when created for a Voice Broadcast Info without relations", () => {
beforeEach(() => {
infoEvent = mkVoiceBroadcastInfoEvent({
state: VoiceBroadcastInfoState.Started,
});
setUpVoiceBroadcastRecording();
});
it("should be in Started state", () => {
expect(voiceBroadcastRecording.state).toBe(VoiceBroadcastInfoState.Started);
});
describe("and calling stop()", () => {
beforeEach(() => {
voiceBroadcastRecording.stop();
});
it("should send a stopped Voice Broadcast Info event", () => {
expect(mocked(client.sendStateEvent)).toHaveBeenCalledWith(
roomId,
VoiceBroadcastInfoEventType,
{
state: VoiceBroadcastInfoState.Stopped,
["m.relates_to"]: {
rel_type: RelationType.Reference,
event_id: infoEvent.getId(),
},
},
client.getUserId(),
);
});
it("should be in state stopped", () => {
expect(voiceBroadcastRecording.state).toBe(VoiceBroadcastInfoState.Stopped);
});
it("should emit a stopped state changed event", () => {
expect(onStateChanged).toHaveBeenCalledWith(VoiceBroadcastInfoState.Stopped);
});
});
});
describe("when created for a Voice Broadcast Info with a Stopped relation", () => {
beforeEach(() => {
infoEvent = mkVoiceBroadcastInfoEvent({
state: VoiceBroadcastInfoState.Started,
chunk_length: 300,
});
const relationsContainer = {
getRelations: jest.fn(),
} as unknown as Relations;
mocked(relationsContainer.getRelations).mockReturnValue([
mkVoiceBroadcastInfoEvent({
state: VoiceBroadcastInfoState.Stopped,
["m.relates_to"]: {
rel_type: RelationType.Reference,
event_id: infoEvent.getId(),
},
}),
]);
const timelineSet = {
relations: {
getChildEventsForEvent: jest.fn().mockImplementation(
(
eventId: string,
relationType: RelationType | string,
eventType: EventType | string,
) => {
if (
eventId === infoEvent.getId()
&& relationType === RelationType.Reference
&& eventType === VoiceBroadcastInfoEventType
) {
return relationsContainer;
}
},
),
},
} as unknown as EventTimelineSet;
mocked(room.getUnfilteredTimelineSet).mockReturnValue(timelineSet);
setUpVoiceBroadcastRecording();
});
it("should be in Stopped state", () => {
expect(voiceBroadcastRecording.state).toBe(VoiceBroadcastInfoState.Stopped);
});
});
});