|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { EventType, GroupCallIntent, GroupCallType, Room, RoomMember } from '../../../src'; |
| 18 | +import { GroupCall } from "../../../src/webrtc/groupCall"; |
| 19 | +import { MatrixClient } from "../../../src/client"; |
| 20 | +import { MockAudioContext, MockMediaHandler } from '../../test-utils/webrtc'; |
| 21 | + |
| 22 | +const FAKE_SELF_USER_ID = "@me:test.dummy"; |
| 23 | +const FAKE_SELF_DEVICE_ID = "AAAAAA"; |
| 24 | +const FAKE_SELF_SESSION_ID = "1"; |
| 25 | +const FAKE_ROOM_ID = "!fake:test.dummy"; |
| 26 | + |
| 27 | +describe('Group Call', function() { |
| 28 | + beforeEach(function() { |
| 29 | + // @ts-ignore Mock |
| 30 | + global.AudioContext = MockAudioContext; |
| 31 | + }); |
| 32 | + |
| 33 | + it("sends state event to room when creating", async () => { |
| 34 | + const mockSendState = jest.fn(); |
| 35 | + |
| 36 | + const mockClient = { |
| 37 | + sendStateEvent: mockSendState, |
| 38 | + groupCallEventHandler: { |
| 39 | + groupCalls: new Map(), |
| 40 | + }, |
| 41 | + } as unknown as MatrixClient; |
| 42 | + |
| 43 | + const room = new Room(FAKE_ROOM_ID, mockClient, FAKE_SELF_USER_ID); |
| 44 | + const groupCall = new GroupCall(mockClient, room, GroupCallType.Video, false, GroupCallIntent.Prompt); |
| 45 | + |
| 46 | + await groupCall.create(); |
| 47 | + |
| 48 | + expect(mockSendState.mock.calls[0][0]).toEqual(FAKE_ROOM_ID); |
| 49 | + expect(mockSendState.mock.calls[0][1]).toEqual(EventType.GroupCallPrefix); |
| 50 | + expect(mockSendState.mock.calls[0][2]["m.type"]).toEqual(GroupCallType.Video); |
| 51 | + expect(mockSendState.mock.calls[0][2]["m.intent"]).toEqual(GroupCallIntent.Prompt); |
| 52 | + }); |
| 53 | + |
| 54 | + it("sends member state event to room on enter", async () => { |
| 55 | + const mockSendState = jest.fn(); |
| 56 | + const mockMediaHandler = new MockMediaHandler(); |
| 57 | + |
| 58 | + const mockClient = { |
| 59 | + sendStateEvent: mockSendState, |
| 60 | + groupCallEventHandler: { |
| 61 | + groupCalls: new Map(), |
| 62 | + }, |
| 63 | + callEventHandler: { |
| 64 | + calls: new Map(), |
| 65 | + }, |
| 66 | + mediaHandler: mockMediaHandler, |
| 67 | + getMediaHandler: () => mockMediaHandler, |
| 68 | + getUserId: () => FAKE_SELF_USER_ID, |
| 69 | + getDeviceId: () => FAKE_SELF_DEVICE_ID, |
| 70 | + getSessionId: () => FAKE_SELF_SESSION_ID, |
| 71 | + emit: jest.fn(), |
| 72 | + on: jest.fn(), |
| 73 | + removeListener: jest.fn(), |
| 74 | + } as unknown as MatrixClient; |
| 75 | + |
| 76 | + const room = new Room(FAKE_ROOM_ID, mockClient, FAKE_SELF_USER_ID); |
| 77 | + const groupCall = new GroupCall(mockClient, room, GroupCallType.Video, false, GroupCallIntent.Prompt); |
| 78 | + |
| 79 | + room.currentState.members[FAKE_SELF_USER_ID] = { |
| 80 | + userId: FAKE_SELF_USER_ID, |
| 81 | + } as unknown as RoomMember; |
| 82 | + |
| 83 | + await groupCall.create(); |
| 84 | + |
| 85 | + try { |
| 86 | + await groupCall.enter(); |
| 87 | + |
| 88 | + expect(mockSendState.mock.lastCall[0]).toEqual(FAKE_ROOM_ID); |
| 89 | + expect(mockSendState.mock.lastCall[1]).toEqual(EventType.GroupCallMemberPrefix); |
| 90 | + expect(mockSendState.mock.lastCall[2]['m.calls'].length).toEqual(1); |
| 91 | + expect(mockSendState.mock.lastCall[2]['m.calls'][0]["m.call_id"]).toEqual(groupCall.groupCallId); |
| 92 | + expect(mockSendState.mock.lastCall[2]['m.calls'][0]['m.devices'].length).toEqual(1); |
| 93 | + expect(mockSendState.mock.lastCall[2]['m.calls'][0]['m.devices'][0].device_id).toEqual(FAKE_SELF_DEVICE_ID); |
| 94 | + } finally { |
| 95 | + groupCall.leave(); |
| 96 | + } |
| 97 | + }); |
| 98 | +}); |
0 commit comments