From cec97d446c4dc9357c791fcd359ed0884485e978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Sun, 18 Sep 2022 09:18:07 +0200 Subject: [PATCH 1/2] Add test for call transfers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- spec/unit/webrtc/call.spec.ts | 95 +++++++++++++++++++++++++++++++++-- 1 file changed, 91 insertions(+), 4 deletions(-) diff --git a/spec/unit/webrtc/call.spec.ts b/spec/unit/webrtc/call.spec.ts index 5504e3ae307..60ac33f429c 100644 --- a/spec/unit/webrtc/call.spec.ts +++ b/spec/unit/webrtc/call.spec.ts @@ -24,6 +24,7 @@ import { supportsMatrixCall, CallType, CallState, + CallParty, } from '../../../src/webrtc/call'; import { SDPStreamMetadata, SDPStreamMetadataKey, SDPStreamMetadataPurpose } from '../../../src/webrtc/callEventTypes'; import { @@ -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 => { +const startVoiceCall = async (client: TestClient, call: MatrixCall, userId?: string): Promise => { 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 => { +const startVideoCall = async (client: TestClient, call: MatrixCall, userId?: string): Promise => { 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") => { @@ -127,6 +128,7 @@ describe('Call', function() { }, } as unknown as Room; }; + client.client.getProfileInfo = jest.fn(); call = new MatrixCall({ client: client.client, @@ -1191,4 +1193,89 @@ 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, + }); + // call checks one of these is wired up + newCall.on(CallEvent.Error, () => { }); + + // @ts-ignore Mock + jest.spyOn(call, "terminate"); + // @ts-ignore Mock + jest.spyOn(newCall, "terminate"); + + 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, + }, + })); + + // @ts-ignore Mock + expect(call.terminate).toHaveBeenCalledWith(CallParty.Local, CallErrorCode.Transfered, true); + // @ts-ignore Mock + expect(newCall.terminate).toHaveBeenCalledWith(CallParty.Local, CallErrorCode.Transfered, true); + }); + + 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); + }); + }); }); From 619ab012d4454b1cfb6a5b2492431e7539abc1fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Tue, 20 Sep 2022 16:53:19 +0200 Subject: [PATCH 2/2] Check if event gets emitted MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- spec/unit/webrtc/call.spec.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/spec/unit/webrtc/call.spec.ts b/spec/unit/webrtc/call.spec.ts index 60ac33f429c..a470fcdd748 100644 --- a/spec/unit/webrtc/call.spec.ts +++ b/spec/unit/webrtc/call.spec.ts @@ -1226,13 +1226,13 @@ describe('Call', function() { client: client.client, roomId: FAKE_ROOM_ID, }); - // call checks one of these is wired up - newCall.on(CallEvent.Error, () => { }); - // @ts-ignore Mock - jest.spyOn(call, "terminate"); - // @ts-ignore Mock - jest.spyOn(newCall, "terminate"); + 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); @@ -1254,10 +1254,8 @@ describe('Call', function() { }, })); - // @ts-ignore Mock - expect(call.terminate).toHaveBeenCalledWith(CallParty.Local, CallErrorCode.Transfered, true); - // @ts-ignore Mock - expect(newCall.terminate).toHaveBeenCalledWith(CallParty.Local, CallErrorCode.Transfered, true); + expect(callHangupListener).toHaveBeenCalledWith(call); + expect(newCallHangupListener).toHaveBeenCalledWith(newCall); }); it("transfers a call to another user", async () => {