Improve some voice broadcast tests (#9786)

This commit is contained in:
Michael Weimann 2022-12-16 19:23:29 +01:00 committed by GitHub
parent 6ec6d44c96
commit 9584388171
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 83 deletions

View file

@ -15,24 +15,13 @@ limitations under the License.
*/
import { mocked } from "jest-mock";
import {
EventTimelineSet,
EventType,
MatrixClient,
MatrixEvent,
MatrixEventEvent,
RelationType,
Room,
} from "matrix-js-sdk/src/matrix";
import { Relations } from "matrix-js-sdk/src/models/relations";
import { RelationsContainer } from "matrix-js-sdk/src/models/relations-container";
import { EventType, MatrixClient, MatrixEvent, MatrixEventEvent, RelationType, Room } from "matrix-js-sdk/src/matrix";
import { RelationsHelper, RelationsHelperEvent } from "../../src/events/RelationsHelper";
import { mkEvent, mkRelationsContainer, mkStubRoom, stubClient } from "../test-utils";
import { mkEvent, stubClient } from "../test-utils";
describe("RelationsHelper", () => {
const roomId = "!room:example.com";
let userId: string;
let event: MatrixEvent;
let relatedEvent1: MatrixEvent;
let relatedEvent2: MatrixEvent;
@ -41,62 +30,59 @@ describe("RelationsHelper", () => {
let client: MatrixClient;
let relationsHelper: RelationsHelper;
let onAdd: (event: MatrixEvent) => void;
let timelineSet: EventTimelineSet;
let relationsContainer: RelationsContainer;
let relations: Relations;
let relationsOnAdd: (event: MatrixEvent) => void;
beforeEach(() => {
client = stubClient();
userId = client.getUserId() || "";
mocked(client.relations).mockClear();
room = mkStubRoom(roomId, "test room", client);
mocked(client.getRoom).mockImplementation((getRoomId?: string) => {
if (getRoomId === roomId) {
return room;
}
room = new Room(roomId, client, client.getSafeUserId());
mocked(client.getRoom).mockImplementation((getRoomId?: string): Room | null => {
if (getRoomId === roomId) return room;
return null;
});
event = mkEvent({
event: true,
type: EventType.RoomMessage,
room: roomId,
user: userId,
user: client.getSafeUserId(),
content: {},
});
relatedEvent1 = mkEvent({
event: true,
type: EventType.RoomMessage,
room: roomId,
user: userId,
content: { relatedEvent: 1 },
user: client.getSafeUserId(),
content: {
["m.relates_to"]: {
rel_type: RelationType.Reference,
event_id: event.getId(),
},
},
});
relatedEvent2 = mkEvent({
event: true,
type: EventType.RoomMessage,
room: roomId,
user: userId,
content: { relatedEvent: 2 },
user: client.getSafeUserId(),
content: {
["m.relates_to"]: {
rel_type: RelationType.Reference,
event_id: event.getId(),
},
},
});
relatedEvent3 = mkEvent({
event: true,
type: EventType.RoomMessage,
room: roomId,
user: userId,
content: { relatedEvent: 3 },
user: client.getSafeUserId(),
content: {
["m.relates_to"]: {
rel_type: RelationType.Reference,
event_id: event.getId(),
},
},
});
onAdd = jest.fn();
relationsContainer = mkRelationsContainer();
// TODO Michael W: create test utils, remove casts
relations = {
getRelations: jest.fn(),
on: jest.fn().mockImplementation((type, l) => (relationsOnAdd = l)),
off: jest.fn(),
} as unknown as Relations;
timelineSet = {
relations: relationsContainer,
} as unknown as EventTimelineSet;
});
afterEach(() => {
@ -139,13 +125,10 @@ describe("RelationsHelper", () => {
});
});
describe("and relations are created and a new event appears", () => {
describe("and a new event appears", () => {
beforeEach(() => {
mocked(room.getUnfilteredTimelineSet).mockReturnValue(timelineSet);
mocked(relationsContainer.getChildEventsForEvent).mockReturnValue(relations);
mocked(relations.getRelations).mockReturnValue([relatedEvent1]);
room.relations.aggregateChildEvent(relatedEvent2);
event.emit(MatrixEventEvent.RelationsCreated, RelationType.Reference, EventType.RoomMessage);
relationsOnAdd(relatedEvent2);
});
it("should emit the new event", () => {
@ -184,9 +167,7 @@ describe("RelationsHelper", () => {
describe("when there is an event with relations", () => {
beforeEach(() => {
mocked(room.getUnfilteredTimelineSet).mockReturnValue(timelineSet);
mocked(relationsContainer.getChildEventsForEvent).mockReturnValue(relations);
mocked(relations.getRelations).mockReturnValue([relatedEvent1]);
room.relations.aggregateChildEvent(relatedEvent1);
relationsHelper = new RelationsHelper(event, RelationType.Reference, EventType.RoomMessage, client);
relationsHelper.on(RelationsHelperEvent.Add, onAdd);
});
@ -203,7 +184,8 @@ describe("RelationsHelper", () => {
describe("and a new event appears", () => {
beforeEach(() => {
relationsOnAdd(relatedEvent2);
room.relations.aggregateChildEvent(relatedEvent2);
event.emit(MatrixEventEvent.RelationsCreated, RelationType.Reference, EventType.RoomMessage);
});
it("should emit the new event", () => {

View file

@ -15,11 +15,10 @@ limitations under the License.
*/
import { mocked } from "jest-mock";
import { MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import { Playback, PlaybackState } from "../../../src/audio/Playback";
import { PlaybackManager } from "../../../src/audio/PlaybackManager";
import { RelationsHelperEvent } from "../../../src/events/RelationsHelper";
import { MediaEventHelper } from "../../../src/utils/MediaEventHelper";
import {
VoiceBroadcastInfoState,
@ -44,6 +43,7 @@ describe("VoiceBroadcastPlayback", () => {
const userId = "@user:example.com";
let deviceId: string;
const roomId = "!room:example.com";
let room: Room;
let client: MatrixClient;
let infoEvent: MatrixEvent;
let playback: VoiceBroadcastPlayback;
@ -133,16 +133,13 @@ describe("VoiceBroadcastPlayback", () => {
});
};
beforeAll(() => {
client = stubClient();
deviceId = client.getDeviceId() || "";
chunk1Event = mkVoiceBroadcastChunkEvent(userId, roomId, chunk1Length, 1);
chunk2Event = mkVoiceBroadcastChunkEvent(userId, roomId, chunk2Length, 2);
const createChunkEvents = () => {
chunk1Event = mkVoiceBroadcastChunkEvent(infoEvent.getId()!, userId, roomId, chunk1Length, 1);
chunk2Event = mkVoiceBroadcastChunkEvent(infoEvent.getId()!, userId, roomId, chunk2Length, 2);
chunk2Event.setTxnId("tx-id-1");
chunk2BEvent = mkVoiceBroadcastChunkEvent(userId, roomId, chunk2Length, 2);
chunk2BEvent = mkVoiceBroadcastChunkEvent(infoEvent.getId()!, userId, roomId, chunk2Length, 2);
chunk2BEvent.setTxnId("tx-id-1");
chunk3Event = mkVoiceBroadcastChunkEvent(userId, roomId, chunk3Length, 3);
chunk3Event = mkVoiceBroadcastChunkEvent(infoEvent.getId()!, userId, roomId, chunk3Length, 3);
chunk1Helper = mkChunkHelper(chunk1Data);
chunk2Helper = mkChunkHelper(chunk2Data);
@ -167,10 +164,17 @@ describe("VoiceBroadcastPlayback", () => {
if (event === chunk2Event) return chunk2Helper;
if (event === chunk3Event) return chunk3Helper;
});
});
};
beforeEach(() => {
client = stubClient();
deviceId = client.getDeviceId() || "";
jest.clearAllMocks();
room = new Room(roomId, client, client.getSafeUserId());
mocked(client.getRoom).mockImplementation((roomId: string): Room | null => {
if (roomId === room.roomId) return room;
return null;
});
onStateChanged = jest.fn();
});
@ -180,10 +184,9 @@ describe("VoiceBroadcastPlayback", () => {
describe(`when there is a ${VoiceBroadcastInfoState.Resumed} broadcast without chunks yet`, () => {
beforeEach(async () => {
// info relation
mocked(client.relations).mockResolvedValueOnce({ events: [] });
setUpChunkEvents([]);
infoEvent = mkInfoEvent(VoiceBroadcastInfoState.Resumed);
createChunkEvents();
room.addLiveEvents([infoEvent]);
playback = await mkPlayback();
});
@ -222,9 +225,7 @@ describe("VoiceBroadcastPlayback", () => {
describe("and receiving the first chunk", () => {
beforeEach(() => {
// TODO Michael W: Use RelationsHelper
// @ts-ignore
playback.chunkRelationHelper.emit(RelationsHelperEvent.Add, chunk1Event);
room.relations.aggregateChildEvent(chunk1Event);
});
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
@ -243,10 +244,13 @@ describe("VoiceBroadcastPlayback", () => {
describe(`when there is a ${VoiceBroadcastInfoState.Resumed} voice broadcast with some chunks`, () => {
beforeEach(async () => {
// info relation
mocked(client.relations).mockResolvedValueOnce({ events: [] });
setUpChunkEvents([chunk2Event, chunk1Event]);
infoEvent = mkInfoEvent(VoiceBroadcastInfoState.Resumed);
createChunkEvents();
setUpChunkEvents([chunk2Event, chunk1Event]);
room.addLiveEvents([infoEvent, chunk1Event, chunk2Event]);
room.relations.aggregateChildEvent(chunk2Event);
room.relations.aggregateChildEvent(chunk1Event);
playback = await mkPlayback();
});
@ -256,8 +260,8 @@ describe("VoiceBroadcastPlayback", () => {
describe("and an event with the same transaction Id occurs", () => {
beforeEach(() => {
// @ts-ignore
playback.chunkRelationHelper.emit(RelationsHelperEvent.Add, chunk2BEvent);
room.addLiveEvents([chunk2BEvent]);
room.relations.aggregateChildEvent(chunk2BEvent);
});
it("durationSeconds should not change", () => {
@ -284,9 +288,8 @@ describe("VoiceBroadcastPlayback", () => {
describe("and the next chunk arrived", () => {
beforeEach(() => {
// TODO Michael W: Use RelationsHelper
// @ts-ignore
playback.chunkRelationHelper.emit(RelationsHelperEvent.Add, chunk3Event);
room.addLiveEvents([chunk3Event]);
room.relations.aggregateChildEvent(chunk3Event);
});
itShouldSetTheStateTo(VoiceBroadcastPlaybackState.Playing);
@ -312,8 +315,10 @@ describe("VoiceBroadcastPlayback", () => {
describe("when there is a stopped voice broadcast", () => {
beforeEach(async () => {
setUpChunkEvents([chunk2Event, chunk1Event]);
infoEvent = mkInfoEvent(VoiceBroadcastInfoState.Stopped);
createChunkEvents();
setUpChunkEvents([chunk2Event, chunk1Event]);
room.addLiveEvents([infoEvent, chunk1Event, chunk2Event]);
playback = await mkPlayback();
});

View file

@ -33,16 +33,16 @@ describe("VoiceBroadcastChunkEvents", () => {
let chunkEvents: VoiceBroadcastChunkEvents;
beforeEach(() => {
eventSeq1Time1 = mkVoiceBroadcastChunkEvent(userId, roomId, 7, 1, 1);
eventSeq2Time4 = mkVoiceBroadcastChunkEvent(userId, roomId, 23, 2, 4);
eventSeq2Time4Dup = mkVoiceBroadcastChunkEvent(userId, roomId, 3141, 2, 4);
eventSeq1Time1 = mkVoiceBroadcastChunkEvent("info1", userId, roomId, 7, 1, 1);
eventSeq2Time4 = mkVoiceBroadcastChunkEvent("info1", userId, roomId, 23, 2, 4);
eventSeq2Time4Dup = mkVoiceBroadcastChunkEvent("info1", userId, roomId, 3141, 2, 4);
jest.spyOn(eventSeq2Time4Dup, "getId").mockReturnValue(eventSeq2Time4.getId());
eventSeq3Time2 = mkVoiceBroadcastChunkEvent(userId, roomId, 42, 3, 2);
eventSeq3Time2 = mkVoiceBroadcastChunkEvent("info1", userId, roomId, 42, 3, 2);
eventSeq3Time2.setTxnId(txnId);
eventSeq3Time2T = mkVoiceBroadcastChunkEvent(userId, roomId, 42, 3, 2);
eventSeq3Time2T = mkVoiceBroadcastChunkEvent("info1", userId, roomId, 42, 3, 2);
eventSeq3Time2T.setTxnId(txnId);
eventSeq4Time1 = mkVoiceBroadcastChunkEvent(userId, roomId, 69, 4, 1);
eventSeqUTime3 = mkVoiceBroadcastChunkEvent(userId, roomId, 314, undefined, 3);
eventSeq4Time1 = mkVoiceBroadcastChunkEvent("info1", userId, roomId, 69, 4, 1);
eventSeqUTime3 = mkVoiceBroadcastChunkEvent("info1", userId, roomId, 314, undefined, 3);
chunkEvents = new VoiceBroadcastChunkEvents();
});

View file

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { EventType, MatrixEvent, MsgType } from "matrix-js-sdk/src/matrix";
import { EventType, MatrixEvent, MsgType, RelationType } from "matrix-js-sdk/src/matrix";
import {
VoiceBroadcastChunkEventType,
@ -54,6 +54,7 @@ export const mkVoiceBroadcastInfoStateEvent = (
};
export const mkVoiceBroadcastChunkEvent = (
infoEventId: string,
userId: string,
roomId: string,
duration: number,
@ -76,6 +77,10 @@ export const mkVoiceBroadcastChunkEvent = (
[VoiceBroadcastChunkEventType]: {
...(sequence ? { sequence } : {}),
},
["m.relates_to"]: {
rel_type: RelationType.Reference,
event_id: infoEventId,
},
},
ts: timestamp,
});