Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Improve some voice broadcast tests (#9786)
Browse files Browse the repository at this point in the history
  • Loading branch information
weeman1337 authored Dec 16, 2022
1 parent 6ec6d44 commit 9584388
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 83 deletions.
82 changes: 32 additions & 50 deletions test/events/RelationsHelper-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(() => {
Expand Down Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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);
});
Expand All @@ -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", () => {
Expand Down
55 changes: 30 additions & 25 deletions test/voice-broadcast/models/VoiceBroadcastPlayback-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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();
});

Expand All @@ -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();
});

Expand Down Expand Up @@ -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);
Expand All @@ -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();
});

Expand All @@ -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", () => {
Expand All @@ -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);
Expand All @@ -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();
});

Expand Down
14 changes: 7 additions & 7 deletions test/voice-broadcast/utils/VoiceBroadcastChunkEvents-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down
7 changes: 6 additions & 1 deletion test/voice-broadcast/utils/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -54,6 +54,7 @@ export const mkVoiceBroadcastInfoStateEvent = (
};

export const mkVoiceBroadcastChunkEvent = (
infoEventId: string,
userId: string,
roomId: string,
duration: number,
Expand All @@ -76,6 +77,10 @@ export const mkVoiceBroadcastChunkEvent = (
[VoiceBroadcastChunkEventType]: {
...(sequence ? { sequence } : {}),
},
["m.relates_to"]: {
rel_type: RelationType.Reference,
event_id: infoEventId,
},
},
ts: timestamp,
});
Expand Down

0 comments on commit 9584388

Please sign in to comment.