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

View file

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

View file

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

View file

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. 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 { import {
VoiceBroadcastChunkEventType, VoiceBroadcastChunkEventType,
@ -54,6 +54,7 @@ export const mkVoiceBroadcastInfoStateEvent = (
}; };
export const mkVoiceBroadcastChunkEvent = ( export const mkVoiceBroadcastChunkEvent = (
infoEventId: string,
userId: string, userId: string,
roomId: string, roomId: string,
duration: number, duration: number,
@ -76,6 +77,10 @@ export const mkVoiceBroadcastChunkEvent = (
[VoiceBroadcastChunkEventType]: { [VoiceBroadcastChunkEventType]: {
...(sequence ? { sequence } : {}), ...(sequence ? { sequence } : {}),
}, },
["m.relates_to"]: {
rel_type: RelationType.Reference,
event_id: infoEventId,
},
}, },
ts: timestamp, ts: timestamp,
}); });