Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for call transfers #2677

Merged
merged 2 commits into from
Sep 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 89 additions & 4 deletions spec/unit/webrtc/call.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
supportsMatrixCall,
CallType,
CallState,
CallParty,
} from '../../../src/webrtc/call';
import { SDPStreamMetadata, SDPStreamMetadataKey, SDPStreamMetadataPurpose } from '../../../src/webrtc/callEventTypes';
import {
Expand All @@ -42,20 +43,20 @@ import { Callback, EventType, IContent, ISendEventResponse, MatrixEvent, Room }
const FAKE_ROOM_ID = "!foo:bar";
const CALL_LIFETIME = 60000;

const startVoiceCall = async (client: TestClient, call: MatrixCall): Promise<void> => {
const startVoiceCall = async (client: TestClient, call: MatrixCall, userId?: string): Promise<void> => {
const callPromise = call.placeVoiceCall();
await client.httpBackend.flush("");
await callPromise;

call.getOpponentMember = jest.fn().mockReturnValue({ userId: "@bob:bar.uk" });
call.getOpponentMember = jest.fn().mockReturnValue({ userId: userId ?? "@bob:bar.uk" });
};

const startVideoCall = async (client: TestClient, call: MatrixCall): Promise<void> => {
const startVideoCall = async (client: TestClient, call: MatrixCall, userId?: string): Promise<void> => {
const callPromise = call.placeVideoCall();
await client.httpBackend.flush("");
await callPromise;

call.getOpponentMember = jest.fn().mockReturnValue({ userId: "@bob:bar.uk" });
call.getOpponentMember = jest.fn().mockReturnValue({ userId: userId ?? "@bob:bar.uk" });
};

const fakeIncomingCall = async (client: TestClient, call: MatrixCall, version: string | number = "1") => {
Expand Down Expand Up @@ -127,6 +128,7 @@ describe('Call', function() {
},
} as unknown as Room;
};
client.client.getProfileInfo = jest.fn();

call = new MatrixCall({
client: client.client,
Expand Down Expand Up @@ -1191,4 +1193,87 @@ describe('Call', function() {
id: "usermedia_video_track",
}));
});

describe("call transfers", () => {
const ALICE_USER_ID = "@alice:foo";
const ALICE_DISPLAY_NAME = "Alice";
const ALICE_AVATAR_URL = "avatar.alice.foo";

const BOB_USER_ID = "@bob:foo";
const BOB_DISPLAY_NAME = "Bob";
const BOB_AVATAR_URL = "avatar.bob.foo";

beforeEach(() => {
mocked(client.client.getProfileInfo).mockImplementation(async (userId) => {
if (userId === ALICE_USER_ID) {
return {
displayname: ALICE_DISPLAY_NAME,
avatar_url: ALICE_AVATAR_URL,
};
} else if (userId === BOB_USER_ID) {
return {
displayname: BOB_DISPLAY_NAME,
avatar_url: BOB_AVATAR_URL,
};
} else {
return {};
}
});
});

it("transfers call to another call", async () => {
const newCall = new MatrixCall({
client: client.client,
roomId: FAKE_ROOM_ID,
});

const callHangupListener = jest.fn();
const newCallHangupListener = jest.fn();

call.on(CallEvent.Hangup, callHangupListener);
newCall.on(CallEvent.Error, () => { });
newCall.on(CallEvent.Hangup, newCallHangupListener);

await startVoiceCall(client, call, ALICE_USER_ID);
await startVoiceCall(client, newCall, BOB_USER_ID);

await call.transferToCall(newCall);

expect(mockSendEvent).toHaveBeenCalledWith(FAKE_ROOM_ID, EventType.CallReplaces, expect.objectContaining({
target_user: {
id: ALICE_USER_ID,
display_name: ALICE_DISPLAY_NAME,
avatar_url: ALICE_AVATAR_URL,
},
}));
expect(mockSendEvent).toHaveBeenCalledWith(FAKE_ROOM_ID, EventType.CallReplaces, expect.objectContaining({
target_user: {
id: BOB_USER_ID,
display_name: BOB_DISPLAY_NAME,
avatar_url: BOB_AVATAR_URL,
},
}));

expect(callHangupListener).toHaveBeenCalledWith(call);
expect(newCallHangupListener).toHaveBeenCalledWith(newCall);
});

it("transfers a call to another user", async () => {
// @ts-ignore Mock
jest.spyOn(call, "terminate");

await startVoiceCall(client, call, ALICE_USER_ID);
await call.transfer(BOB_USER_ID);

expect(mockSendEvent).toHaveBeenCalledWith(FAKE_ROOM_ID, EventType.CallReplaces, expect.objectContaining({
target_user: {
id: BOB_USER_ID,
display_name: BOB_DISPLAY_NAME,
avatar_url: BOB_AVATAR_URL,
},
}));
// @ts-ignore Mock
expect(call.terminate).toHaveBeenCalledWith(CallParty.Local, CallErrorCode.Transfered, true);
});
});
});