2021-04-23 16:39:39 +03:00
|
|
|
/*
|
2021-04-27 20:56:22 +03:00
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
2021-04-23 16:39:39 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import './skinned-sdk';
|
|
|
|
|
2021-12-09 12:10:23 +03:00
|
|
|
import { CallEvent, CallState, CallType } from 'matrix-js-sdk/src/webrtc/call';
|
|
|
|
import EventEmitter from 'events';
|
|
|
|
|
2021-11-30 21:09:13 +03:00
|
|
|
import CallHandler, { CallHandlerEvent } from '../src/CallHandler';
|
2021-04-23 16:39:39 +03:00
|
|
|
import { stubClient, mkStubRoom } from './test-utils';
|
|
|
|
import { MatrixClientPeg } from '../src/MatrixClientPeg';
|
|
|
|
import dis from '../src/dispatcher/dispatcher';
|
|
|
|
import DMRoomMap from '../src/utils/DMRoomMap';
|
|
|
|
import SdkConfig from '../src/SdkConfig';
|
2021-06-02 19:39:13 +03:00
|
|
|
import { ActionPayload } from '../src/dispatcher/payloads';
|
2021-11-30 21:09:13 +03:00
|
|
|
import { Action } from "../src/dispatcher/actions";
|
2021-04-23 16:39:39 +03:00
|
|
|
|
|
|
|
const REAL_ROOM_ID = '$room1:example.org';
|
|
|
|
const MAPPED_ROOM_ID = '$room2:example.org';
|
|
|
|
const MAPPED_ROOM_ID_2 = '$room3:example.org';
|
|
|
|
|
|
|
|
function mkStubDM(roomId, userId) {
|
|
|
|
const room = mkStubRoom(roomId);
|
|
|
|
room.getJoinedMembers = jest.fn().mockReturnValue([
|
|
|
|
{
|
|
|
|
userId: '@me:example.org',
|
|
|
|
name: 'Member',
|
|
|
|
rawDisplayName: 'Member',
|
|
|
|
roomId: roomId,
|
|
|
|
membership: 'join',
|
|
|
|
getAvatarUrl: () => 'mxc://avatar.url/image.png',
|
|
|
|
getMxcAvatarUrl: () => 'mxc://avatar.url/image.png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: userId,
|
|
|
|
name: 'Member',
|
|
|
|
rawDisplayName: 'Member',
|
|
|
|
roomId: roomId,
|
|
|
|
membership: 'join',
|
|
|
|
getAvatarUrl: () => 'mxc://avatar.url/image.png',
|
|
|
|
getMxcAvatarUrl: () => 'mxc://avatar.url/image.png',
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
room.currentState.getMembers = room.getJoinedMembers;
|
|
|
|
|
|
|
|
return room;
|
|
|
|
}
|
|
|
|
|
|
|
|
class FakeCall extends EventEmitter {
|
|
|
|
roomId: string;
|
|
|
|
callId = "fake call id";
|
|
|
|
|
|
|
|
constructor(roomId) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.roomId = roomId;
|
|
|
|
}
|
|
|
|
|
|
|
|
setRemoteOnHold() {}
|
|
|
|
setRemoteAudioElement() {}
|
|
|
|
|
|
|
|
placeVoiceCall() {
|
|
|
|
this.emit(CallEvent.State, CallState.Connected, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 19:39:13 +03:00
|
|
|
function untilDispatch(waitForAction: string): Promise<ActionPayload> {
|
|
|
|
let dispatchHandle;
|
|
|
|
return new Promise<ActionPayload>(resolve => {
|
|
|
|
dispatchHandle = dis.register(payload => {
|
|
|
|
if (payload.action === waitForAction) {
|
|
|
|
dis.unregister(dispatchHandle);
|
|
|
|
resolve(payload);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-30 21:09:13 +03:00
|
|
|
function untilCallHandlerEvent(callHandler: CallHandler, event: CallHandlerEvent): Promise<void> {
|
|
|
|
return new Promise<void>((resolve) => {
|
|
|
|
callHandler.addListener(event, () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-23 16:39:39 +03:00
|
|
|
describe('CallHandler', () => {
|
|
|
|
let dmRoomMap;
|
|
|
|
let callHandler;
|
|
|
|
let audioElement;
|
|
|
|
let fakeCall;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
stubClient();
|
|
|
|
MatrixClientPeg.get().createCall = roomId => {
|
|
|
|
if (fakeCall && fakeCall.roomId !== roomId) {
|
|
|
|
throw new Error("Only one call is supported!");
|
|
|
|
}
|
|
|
|
fakeCall = new FakeCall(roomId);
|
|
|
|
return fakeCall;
|
|
|
|
};
|
|
|
|
|
|
|
|
callHandler = new CallHandler();
|
|
|
|
callHandler.start();
|
|
|
|
|
2021-06-02 19:39:13 +03:00
|
|
|
const realRoom = mkStubDM(REAL_ROOM_ID, '@user1:example.org');
|
|
|
|
const mappedRoom = mkStubDM(MAPPED_ROOM_ID, '@user2:example.org');
|
|
|
|
const mappedRoom2 = mkStubDM(MAPPED_ROOM_ID_2, '@user3:example.org');
|
|
|
|
|
|
|
|
MatrixClientPeg.get().getRoom = roomId => {
|
|
|
|
switch (roomId) {
|
|
|
|
case REAL_ROOM_ID:
|
|
|
|
return realRoom;
|
|
|
|
case MAPPED_ROOM_ID:
|
|
|
|
return mappedRoom;
|
|
|
|
case MAPPED_ROOM_ID_2:
|
|
|
|
return mappedRoom2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-23 16:39:39 +03:00
|
|
|
dmRoomMap = {
|
|
|
|
getUserIdForRoomId: roomId => {
|
|
|
|
if (roomId === REAL_ROOM_ID) {
|
|
|
|
return '@user1:example.org';
|
|
|
|
} else if (roomId === MAPPED_ROOM_ID) {
|
|
|
|
return '@user2:example.org';
|
|
|
|
} else if (roomId === MAPPED_ROOM_ID_2) {
|
|
|
|
return '@user3:example.org';
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getDMRoomsForUserId: userId => {
|
|
|
|
if (userId === '@user2:example.org') {
|
|
|
|
return [MAPPED_ROOM_ID];
|
|
|
|
} else if (userId === '@user3:example.org') {
|
|
|
|
return [MAPPED_ROOM_ID_2];
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
DMRoomMap.setShared(dmRoomMap);
|
|
|
|
|
|
|
|
audioElement = document.createElement('audio');
|
|
|
|
audioElement.id = "remoteAudio";
|
|
|
|
document.body.appendChild(audioElement);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
callHandler.stop();
|
|
|
|
DMRoomMap.setShared(null);
|
|
|
|
// @ts-ignore
|
|
|
|
window.mxCallHandler = null;
|
2021-08-04 11:46:39 +03:00
|
|
|
fakeCall = null;
|
2021-04-23 16:39:39 +03:00
|
|
|
MatrixClientPeg.unset();
|
|
|
|
|
|
|
|
document.body.removeChild(audioElement);
|
|
|
|
SdkConfig.unset();
|
|
|
|
});
|
|
|
|
|
2021-08-04 11:46:39 +03:00
|
|
|
it('should look up the correct user and start a call in the room when a phone number is dialled', async () => {
|
2021-06-02 19:39:13 +03:00
|
|
|
MatrixClientPeg.get().getThirdpartyUser = jest.fn().mockResolvedValue([{
|
|
|
|
userid: '@user2:example.org',
|
|
|
|
protocol: "im.vector.protocol.sip_native",
|
|
|
|
fields: {
|
|
|
|
is_native: true,
|
|
|
|
lookup_success: true,
|
|
|
|
},
|
|
|
|
}]);
|
2021-04-23 16:39:39 +03:00
|
|
|
|
2021-11-30 21:09:13 +03:00
|
|
|
await callHandler.dialNumber('01818118181');
|
2021-06-02 19:39:13 +03:00
|
|
|
|
2021-11-25 23:49:43 +03:00
|
|
|
const viewRoomPayload = await untilDispatch(Action.ViewRoom);
|
2021-06-02 19:39:13 +03:00
|
|
|
expect(viewRoomPayload.room_id).toEqual(MAPPED_ROOM_ID);
|
2021-08-04 11:46:39 +03:00
|
|
|
|
|
|
|
// Check that a call was started
|
|
|
|
expect(fakeCall.roomId).toEqual(MAPPED_ROOM_ID);
|
2021-06-02 19:39:13 +03:00
|
|
|
});
|
2021-04-23 16:39:39 +03:00
|
|
|
|
2021-06-02 19:39:13 +03:00
|
|
|
it('should move calls between rooms when remote asserted identity changes', async () => {
|
2021-11-30 21:09:13 +03:00
|
|
|
callHandler.placeCall(REAL_ROOM_ID, CallType.Voice);
|
|
|
|
|
|
|
|
await untilCallHandlerEvent(callHandler, CallHandlerEvent.CallState);
|
2021-04-23 16:39:39 +03:00
|
|
|
|
|
|
|
// should start off in the actual room ID it's in at the protocol level
|
|
|
|
expect(callHandler.getCallForRoom(REAL_ROOM_ID)).toBe(fakeCall);
|
|
|
|
|
|
|
|
let callRoomChangeEventCount = 0;
|
|
|
|
const roomChangePromise = new Promise<void>(resolve => {
|
2021-04-28 14:33:15 +03:00
|
|
|
callHandler.addListener(CallHandlerEvent.CallChangeRoom, () => {
|
2021-04-28 12:49:07 +03:00
|
|
|
++callRoomChangeEventCount;
|
|
|
|
resolve();
|
2021-04-23 16:39:39 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Now emit an asserted identity for user2: this should be ignored
|
|
|
|
// because we haven't set the config option to obey asserted identity
|
|
|
|
fakeCall.getRemoteAssertedIdentity = jest.fn().mockReturnValue({
|
|
|
|
id: "@user2:example.org",
|
|
|
|
});
|
|
|
|
fakeCall.emit(CallEvent.AssertedIdentityChanged);
|
|
|
|
|
|
|
|
// Now set the config option
|
|
|
|
SdkConfig.put({
|
2021-04-27 21:33:53 +03:00
|
|
|
voip: {
|
|
|
|
obeyAssertedIdentity: true,
|
|
|
|
},
|
2021-04-23 16:39:39 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// ...and send another asserted identity event for a different user
|
|
|
|
fakeCall.getRemoteAssertedIdentity = jest.fn().mockReturnValue({
|
|
|
|
id: "@user3:example.org",
|
|
|
|
});
|
|
|
|
fakeCall.emit(CallEvent.AssertedIdentityChanged);
|
|
|
|
|
|
|
|
await roomChangePromise;
|
2021-04-28 14:33:15 +03:00
|
|
|
callHandler.removeAllListeners();
|
2021-04-23 16:39:39 +03:00
|
|
|
|
|
|
|
// If everything's gone well, we should have seen only one room change
|
|
|
|
// event and the call should now be in user 3's room.
|
|
|
|
// If it's not obeying any, the call will still be in REAL_ROOM_ID.
|
|
|
|
// If it incorrectly obeyed both asserted identity changes, either it will
|
|
|
|
// have just processed one and the call will be in the wrong room, or we'll
|
|
|
|
// have seen two room change dispatches.
|
|
|
|
expect(callRoomChangeEventCount).toEqual(1);
|
|
|
|
expect(callHandler.getCallForRoom(REAL_ROOM_ID)).toBeNull();
|
|
|
|
expect(callHandler.getCallForRoom(MAPPED_ROOM_ID_2)).toBe(fakeCall);
|
|
|
|
});
|
|
|
|
});
|