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 unit tests for hangup / reject #2606

Merged
merged 2 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions spec/test-utils/webrtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ export class MockRTCPeerConnection {
sdp: DUMMY_SDP,
});
}
createAnswer() {
return Promise.resolve({
type: 'answer',
sdp: DUMMY_SDP,
});
}
setRemoteDescription() {
return Promise.resolve();
}
Expand Down
122 changes: 97 additions & 25 deletions spec/unit/webrtc/call.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import {
installWebRTCMocks,
} from "../../test-utils/webrtc";
import { CallFeed } from "../../../src/webrtc/callFeed";
import { EventType } from "../../../src";
import { EventType, MatrixEvent } from "../../../src";

const FAKE_ROOM_ID = "!foo:bar";

const startVoiceCall = async (client: TestClient, call: MatrixCall): Promise<void> => {
const callPromise = call.placeVoiceCall();
Expand All @@ -43,6 +45,31 @@ const startVideoCall = async (client: TestClient, call: MatrixCall): Promise<voi
call.getOpponentMember = jest.fn().mockReturnValue({ userId: "@bob:bar.uk" });
};

const fakeIncomingCall = async (client: TestClient, call: MatrixCall, version: string | number = "1") => {
const callPromise = call.initWithInvite({
getContent: jest.fn().mockReturnValue({
version,
call_id: "call_id",
party_id: "remote_party_id",
offer: {
sdp: DUMMY_SDP,
},
}),
getSender: () => "@test:foo",
getLocalAge: () => null,
} as unknown as MatrixEvent);
call.getFeeds().push(new CallFeed({
client: client.client,
userId: "remote_user_id",
// @ts-ignore Mock
stream: new MockMediaStream("remote_stream_id", [new MockMediaStreamTrack("remote_tack_id")]),
id: "remote_feed_id",
purpose: SDPStreamMetadataPurpose.Usermedia,
}));
await client.httpBackend.flush("");
await callPromise;
};

describe('Call', function() {
let client;
let call;
Expand All @@ -60,7 +87,7 @@ describe('Call', function() {
client = new TestClient("@alice:foo", "somedevice", "token", undefined, {});
// We just stub out sendEvent: we're not interested in testing the client's
// event sending code here
client.client.sendEvent = () => {};
client.client.sendEvent = jest.fn();
client.client.mediaHandler = new MockMediaHandler;
client.client.getMediaHandler = () => client.client.mediaHandler;
client.httpBackend.when("GET", "/voip/turnServer").respond(200, {});
Expand All @@ -74,7 +101,7 @@ describe('Call', function() {

call = new MatrixCall({
client: client.client,
roomId: '!foo:bar',
roomId: FAKE_ROOM_ID,
});
// call checks one of these is wired up
call.on('error', () => {});
Expand Down Expand Up @@ -594,28 +621,7 @@ describe('Call', function() {
});

it("should end call after receiving a select event with a different party id", async () => {
const callPromise = call.initWithInvite({
getContent: () => ({
version: 1,
call_id: "call_id",
party_id: "remote_party_id",
offer: {
sdp: DUMMY_SDP,
},
}),
getSender: () => "@test:foo",
getLocalAge: () => null,
});
call.feeds.push(new CallFeed({
client,
userId: "remote_user_id",
// @ts-ignore Mock
stream: new MockMediaStream("remote_stream_id", [new MockMediaStreamTrack("remote_tack_id")]),
id: "remote_feed_id",
purpose: SDPStreamMetadataPurpose.Usermedia,
}));
await client.httpBackend.flush();
await callPromise;
await fakeIncomingCall(client, call);

const callHangupCallback = jest.fn();
call.on(CallEvent.Hangup, callHangupCallback);
Expand Down Expand Up @@ -845,4 +851,70 @@ describe('Call', function() {
});
});
});

describe("rejecting calls", () => {
it("sends hangup event when rejecting v0 calls", async () => {
await fakeIncomingCall(client, call, 0);

call.reject();

expect(client.client.sendEvent).toHaveBeenCalledWith(
FAKE_ROOM_ID,
EventType.CallHangup,
expect.objectContaining({
call_id: call.callId,
}),
);
});

it("sends reject event when rejecting v1 calls", async () => {
await fakeIncomingCall(client, call, "1");

call.reject();

expect(client.client.sendEvent).toHaveBeenCalledWith(
FAKE_ROOM_ID,
EventType.CallReject,
expect.objectContaining({
call_id: call.callId,
}),
);
});

it("does not reject a call that has already been answered", async () => {
await fakeIncomingCall(client, call, "1");

await call.answer();

client.client.sendEvent.mockReset();

let caught = false;
try {
call.reject();
} catch (e) {
caught = true;
}

expect(caught).toEqual(true);
expect(client.client.sendEvent).not.toHaveBeenCalled();
});

it("hangs up a call", async () => {
await fakeIncomingCall(client, call, "1");

await call.answer();

client.client.sendEvent.mockReset();

call.hangup();

expect(client.client.sendEvent).toHaveBeenCalledWith(
FAKE_ROOM_ID,
EventType.CallHangup,
expect.objectContaining({
call_id: call.callId,
}),
);
});
});
});