2021-04-23 15:39:39 +02:00
|
|
|
/*
|
2021-04-27 19:56:22 +02:00
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
2021-04-23 15:39:39 +02: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-04-28 11:49:07 +02:00
|
|
|
import CallHandler, { PlaceCallType, CallHandlerEvent } from '../src/CallHandler';
|
2021-04-23 15:39:39 +02:00
|
|
|
import { stubClient, mkStubRoom } from './test-utils';
|
|
|
|
import { MatrixClientPeg } from '../src/MatrixClientPeg';
|
|
|
|
import dis from '../src/dispatcher/dispatcher';
|
|
|
|
import { CallEvent, CallState } from 'matrix-js-sdk/src/webrtc/call';
|
|
|
|
import DMRoomMap from '../src/utils/DMRoomMap';
|
|
|
|
import EventEmitter from 'events';
|
|
|
|
import SdkConfig from '../src/SdkConfig';
|
2021-06-02 18:39:13 +02:00
|
|
|
import { ActionPayload } from '../src/dispatcher/payloads';
|
2021-06-03 15:38:13 +02:00
|
|
|
import { Action } from '../src/dispatcher/actions';
|
2021-04-23 15:39:39 +02: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 18:39:13 +02: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-04-23 15:39:39 +02: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 18:39:13 +02: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 15:39:39 +02: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;
|
|
|
|
MatrixClientPeg.unset();
|
|
|
|
|
|
|
|
document.body.removeChild(audioElement);
|
|
|
|
SdkConfig.unset();
|
|
|
|
});
|
|
|
|
|
2021-06-02 18:39:13 +02:00
|
|
|
it('should look up the correct user and open the room when a phone number is dialled', async () => {
|
|
|
|
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 15:39:39 +02:00
|
|
|
|
2021-06-02 18:39:13 +02:00
|
|
|
dis.dispatch({
|
2021-06-03 15:38:13 +02:00
|
|
|
action: Action.DialNumber,
|
2021-06-02 18:39:13 +02:00
|
|
|
number: '01818118181',
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
const viewRoomPayload = await untilDispatch('view_room');
|
|
|
|
expect(viewRoomPayload.room_id).toEqual(MAPPED_ROOM_ID);
|
|
|
|
});
|
2021-04-23 15:39:39 +02:00
|
|
|
|
2021-06-02 18:39:13 +02:00
|
|
|
it('should move calls between rooms when remote asserted identity changes', async () => {
|
2021-04-23 15:39:39 +02:00
|
|
|
dis.dispatch({
|
|
|
|
action: 'place_call',
|
|
|
|
type: PlaceCallType.Voice,
|
|
|
|
room_id: REAL_ROOM_ID,
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
// wait for the call to be set up
|
2021-06-02 18:39:13 +02:00
|
|
|
await untilDispatch('call_state');
|
2021-04-23 15:39:39 +02: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 13:33:15 +02:00
|
|
|
callHandler.addListener(CallHandlerEvent.CallChangeRoom, () => {
|
2021-04-28 11:49:07 +02:00
|
|
|
++callRoomChangeEventCount;
|
|
|
|
resolve();
|
2021-04-23 15:39:39 +02: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 20:33:53 +02:00
|
|
|
voip: {
|
|
|
|
obeyAssertedIdentity: true,
|
|
|
|
},
|
2021-04-23 15:39:39 +02: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 13:33:15 +02:00
|
|
|
callHandler.removeAllListeners();
|
2021-04-23 15:39:39 +02: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);
|
|
|
|
});
|
|
|
|
});
|