Skip to content

Commit

Permalink
more review + more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
toger5 committed May 27, 2024
1 parent f5bca40 commit 52ee79d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 23 deletions.
88 changes: 73 additions & 15 deletions spec/unit/matrixrtc/MatrixRTCSession.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const membershipTemplate: CallMembershipData = {
device_id: "AAAAAAA",
expires: 60 * 60 * 1000,
membershipID: "bloop",
foci_active: [{ type: "livekit" }],
foci_active: [{ type: "livekit", livekit_service_url: "https://lk.url" }],
};

const mockFocus = { type: "mock" };
Expand Down Expand Up @@ -199,6 +199,64 @@ describe("MatrixRTCSession", () => {
});
});

describe("getsActiveFocus", () => {
const activeFociConfig = { type: "livekit", livekit_service_url: "https://active.url" };
it("gets the correct active focus with oldest_membership", () => {
const mockRoom = makeMockRoom([
Object.assign({}, membershipTemplate, {
device_id: "foo",
created_ts: 500,
foci_active: [activeFociConfig],
}),
Object.assign({}, membershipTemplate, { device_id: "old", created_ts: 1000 }),
Object.assign({}, membershipTemplate, { device_id: "bar", created_ts: 2000 }),
]);

sess = MatrixRTCSession.roomSessionForRoom(client, mockRoom);

sess.joinRoomSession([{ type: "livekit", livekit_service_url: "htts://test.org" }], {
type: "livekit",
focus_selection: "oldest_membership",
});
expect(sess.getActiveFocus()).toBe(activeFociConfig);
});
it("does not provide focus if the selction method is unknown", () => {
const mockRoom = makeMockRoom([
Object.assign({}, membershipTemplate, {
device_id: "foo",
created_ts: 500,
foci_active: [activeFociConfig],
}),
Object.assign({}, membershipTemplate, { device_id: "old", created_ts: 1000 }),
Object.assign({}, membershipTemplate, { device_id: "bar", created_ts: 2000 }),
]);

sess = MatrixRTCSession.roomSessionForRoom(client, mockRoom);

sess.joinRoomSession([{ type: "livekit", livekit_service_url: "htts://test.org" }], {
type: "livekit",
focus_selection: "unknown",
});
expect(sess.getActiveFocus()).toBe(undefined);
});
it("gets the correct active focus legacy", () => {
const mockRoom = makeMockRoom([
Object.assign({}, membershipTemplate, {
device_id: "foo",
created_ts: 500,
foci_active: [activeFociConfig],
}),
Object.assign({}, membershipTemplate, { device_id: "old", created_ts: 1000 }),
Object.assign({}, membershipTemplate, { device_id: "bar", created_ts: 2000 }),
]);

sess = MatrixRTCSession.roomSessionForRoom(client, mockRoom);

sess.joinRoomSession([{ type: "livekit", livekit_service_url: "htts://test.org" }]);
expect(sess.getActiveFocus()).toBe(activeFociConfig);
});
});

describe("joining", () => {
let mockRoom: Room;
let sendStateEventMock: jest.Mock;
Expand All @@ -224,13 +282,13 @@ describe("MatrixRTCSession", () => {
});

it("shows joined once join is called", () => {
sess!.joinRoomSession(mockFocus, [mockFocus]);
sess!.joinRoomSession([mockFocus], mockFocus);
expect(sess!.isJoined()).toEqual(true);
});

it("sends a membership event when joining a call", () => {
jest.useFakeTimers();
sess!.joinRoomSession(mockFocus, [mockFocus]);
sess!.joinRoomSession([mockFocus], mockFocus);
expect(client.sendStateEvent).toHaveBeenCalledWith(
mockRoom!.roomId,
EventType.GroupCallMemberPrefix,
Expand All @@ -255,11 +313,11 @@ describe("MatrixRTCSession", () => {
});

it("does nothing if join called when already joined", () => {
sess!.joinRoomSession(mockFocus, [mockFocus]);
sess!.joinRoomSession([mockFocus], mockFocus);

expect(client.sendStateEvent).toHaveBeenCalledTimes(1);

sess!.joinRoomSession(mockFocus, [mockFocus]);
sess!.joinRoomSession([mockFocus], mockFocus);
expect(client.sendStateEvent).toHaveBeenCalledTimes(1);
});

Expand All @@ -276,7 +334,7 @@ describe("MatrixRTCSession", () => {
const sendStateEventMock = jest.fn().mockImplementation(resolveFn);
client.sendStateEvent = sendStateEventMock;

sess!.joinRoomSession(mockFocus, [mockFocus]);
sess!.joinRoomSession([mockFocus], mockFocus);

const eventContent = await eventSentPromise;

Expand Down Expand Up @@ -324,7 +382,7 @@ describe("MatrixRTCSession", () => {
});

it("creates a key when joining", () => {
sess!.joinRoomSession(mockFocus, [mockFocus], { manageMediaKeys: true });
sess!.joinRoomSession([mockFocus], mockFocus, { manageMediaKeys: true });
const keys = sess?.getKeysForParticipant("@alice:example.org", "AAAAAAA");
expect(keys).toHaveLength(1);

Expand All @@ -338,7 +396,7 @@ describe("MatrixRTCSession", () => {
sendEventMock.mockImplementation(resolve);
});

sess!.joinRoomSession(mockFocus, [mockFocus], { manageMediaKeys: true });
sess!.joinRoomSession([mockFocus], mockFocus, { manageMediaKeys: true });

await eventSentPromise;

Expand Down Expand Up @@ -374,7 +432,7 @@ describe("MatrixRTCSession", () => {
});
});

sess!.joinRoomSession(mockFocus, [mockFocus], { manageMediaKeys: true });
sess!.joinRoomSession([mockFocus], mockFocus, { manageMediaKeys: true });
jest.advanceTimersByTime(10000);

await eventSentPromise;
Expand All @@ -396,7 +454,7 @@ describe("MatrixRTCSession", () => {
throw e;
});

sess!.joinRoomSession(mockFocus, [mockFocus], { manageMediaKeys: true });
sess!.joinRoomSession([mockFocus], mockFocus, { manageMediaKeys: true });

expect(client.cancelPendingEvent).toHaveBeenCalledWith(eventSentinel);
});
Expand All @@ -411,7 +469,7 @@ describe("MatrixRTCSession", () => {
sendEventMock.mockImplementation(resolve);
});

sess.joinRoomSession(mockFocus, [mockFocus], { manageMediaKeys: true });
sess.joinRoomSession([mockFocus], mockFocus, { manageMediaKeys: true });
await keysSentPromise1;

sendEventMock.mockClear();
Expand Down Expand Up @@ -464,7 +522,7 @@ describe("MatrixRTCSession", () => {
sendEventMock.mockImplementation((_roomId, _evType, payload) => resolve(payload));
});

sess.joinRoomSession(mockFocus, [mockFocus], { manageMediaKeys: true });
sess.joinRoomSession([mockFocus], mockFocus, { manageMediaKeys: true });
const firstKeysPayload = await keysSentPromise1;
expect(firstKeysPayload.keys).toHaveLength(1);

Expand Down Expand Up @@ -501,7 +559,7 @@ describe("MatrixRTCSession", () => {
sendEventMock.mockImplementation(resolve);
});

sess.joinRoomSession(mockFocus, [mockFocus], { manageMediaKeys: true });
sess.joinRoomSession([mockFocus], mockFocus, { manageMediaKeys: true });
await keysSentPromise1;

sendEventMock.mockClear();
Expand Down Expand Up @@ -597,7 +655,7 @@ describe("MatrixRTCSession", () => {

jest.advanceTimersByTime(10000);

sess.joinRoomSession(mockFocus, [mockFocus]);
sess.joinRoomSession([mockFocus], mockFocus);

expect(client.sendStateEvent).toHaveBeenCalledWith(
mockRoomNoExpired!.roomId,
Expand Down Expand Up @@ -633,7 +691,7 @@ describe("MatrixRTCSession", () => {
]);
sess = MatrixRTCSession.roomSessionForRoom(client, mockRoom);

sess.joinRoomSession(mockFocus, [mockFocus]);
sess.joinRoomSession([mockFocus], mockFocus);

expect(client.sendStateEvent).toHaveBeenCalledWith(
mockRoom!.roomId,
Expand Down
8 changes: 4 additions & 4 deletions src/matrixrtc/CallMembership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

import { EitherAnd } from "matrix-events-sdk/lib/types";

import { IContent, MatrixEvent } from "../matrix";
import { MatrixEvent } from "../matrix";
import { deepCompare } from "../utils";
import { Focus } from "./focus";

Expand All @@ -27,7 +27,7 @@ type CallScope = "m.room" | "m.user";

// MSC4143 (MatrixRTC) session membership data

export interface SessionMembershipData {
export type SessionMembershipData = {
application: string;
call_id: string;
device_id: string;
Expand All @@ -38,7 +38,7 @@ export interface SessionMembershipData {

// Application specific data
scope?: CallScope;
}
};

export const isSessionMembershipData = (data: any): data is SessionMembershipData =>
"foci_active" in data &&
Expand Down Expand Up @@ -109,7 +109,7 @@ export class CallMembership {

public constructor(
private parentEvent: MatrixEvent,
private data: IContent,
private data: any,
) {
if (isLegacyCallMembershipData(data)) checkCallMembershipDataLegacy(data);
else if (isSessionMembershipData(data)) checkSessionsMembershipData(data);
Expand Down
8 changes: 4 additions & 4 deletions src/matrixrtc/MatrixRTCSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
const callMemberships: CallMembership[] = [];
for (const memberEvent of callMemberEvents) {
const content = memberEvent.getContent();
let membershipContents: CallMembershipData[] = [];
let membershipContents: any[] = [];
// We first decide if its a MSC4143 event (per device state key)
if ("memberships" in content) {
// we have a legacy (one event for all devices) event
Expand All @@ -173,7 +173,7 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
// We have a MSC4143 event membership event
if (Object.keys(content).length !== 0) {
// We checked for empty content to not try to construct CallMembership's with {}.
membershipContents.push(content as CallMembershipData);
membershipContents.push(content);
}
}
if (membershipContents.length === 0) {
Expand Down Expand Up @@ -277,8 +277,8 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
* @param joinConfig - Additional configuration for the joined session.
*/
public async joinRoomSession(
fociActive: Focus,
fociPreferred: Focus[],
fociActive?: Focus,
joinConfig?: JoinSessionConfig,
): Promise<void> {
if (this.isJoined()) {
Expand Down Expand Up @@ -709,7 +709,7 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
scope: "m.room",
application: "m.call",
device_id: this.client.getDeviceId()!,
foci_active: { type: "livekit", focus_selection: "oldest_membership" } as LivekitFocusActive as Focus,
foci_active: { type: "livekit", focus_selection: "oldest_membership" },
foci_preferred: this.ownFociPreferred ?? [],
};
}
Expand Down
1 change: 1 addition & 0 deletions src/matrixrtc/focus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ limitations under the License.
*/
export interface Focus {
type: string;
[key: string]: unknown;
}

0 comments on commit 52ee79d

Please sign in to comment.