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 tests for call answering / candidate sending #2666

Merged
merged 8 commits into from
Sep 14, 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
7 changes: 6 additions & 1 deletion spec/test-utils/webrtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class MockRTCPeerConnection {
private static instances: MockRTCPeerConnection[] = [];

private negotiationNeededListener: () => void;
public icecandidateListener: (e: RTCPeerConnectionIceEvent) => void;
dbkr marked this conversation as resolved.
Show resolved Hide resolved
private needsNegotiation = false;
public readyToNegotiate: Promise<void>;
private onReadyToNegotiate: () => void;
Expand Down Expand Up @@ -135,7 +136,11 @@ export class MockRTCPeerConnection {
}

addEventListener(type: string, listener: () => void) {
if (type === 'negotiationneeded') this.negotiationNeededListener = listener;
if (type === 'negotiationneeded') {
this.negotiationNeededListener = listener;
} else if (type == 'icecandidate') {
this.icecandidateListener = listener;
}
}
createDataChannel(label: string, opts: RTCDataChannelInit) { return { label, ...opts }; }
createOffer() {
Expand Down
97 changes: 97 additions & 0 deletions spec/unit/webrtc/call.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,103 @@ describe('Call', function() {
});
});

describe("answering calls", () => {
//let answerSentProm;
dbkr marked this conversation as resolved.
Show resolved Hide resolved

beforeEach(async () => {
await fakeIncomingCall(client, call, "1");

/*answerSentProm = new Promise<void>(resolve => {
mocked(client.client.sendEvent).mockImplementation(
(
roomId: string,
threadId: string | null,
eventType: string | IContent,
content: IContent | string,
txnId?: string | Callback,
callback?: Callback,
) => {
// annoyingly we have top do backwards compat logic here
if (!threadId?.startsWith("$") && threadId !== null) {
callback = txnId as Callback;
txnId = content as string;
content = eventType as IContent;
eventType = threadId;
threadId = null;
}
if (eventType === EventType.CallAnswer) resolve();
return Promise.resolve({ event_id: "foo" });
},
);
});*/
dbkr marked this conversation as resolved.
Show resolved Hide resolved
});

const untilEventSent = async (...args) => {
const maxTries = 20;

for (let tries = 0; tries < maxTries; ++tries) {
if (tries) {
await new Promise(resolve => {
setTimeout(resolve, 100);
});
}
try {
expect(client.client.sendEvent).toHaveBeenCalledWith(...args);
return;
} catch (e) {
if (tries == maxTries - 1) {
throw e;
}
}
}
};

it("sends an answer event", async () => {
await call.answer();
await untilEventSent(
FAKE_ROOM_ID,
EventType.CallAnswer,
expect.objectContaining({
call_id: call.callId,
answer: expect.objectContaining({
type: 'offer',
}),
}),
);
});

it("sends ICE candidates as separate events if they arrive after the answer", async () => {
const fakeCandidateString = "here is a fake candidate!";

await call.answer();
await untilEventSent(
FAKE_ROOM_ID,
EventType.CallAnswer,
expect.objectContaining({}),
);

const mockPeerConn = call.peerConn as unknown as MockRTCPeerConnection;
mockPeerConn.icecandidateListener({
candidate: {
candidate: fakeCandidateString,
sdpMLineIndex: 0,
sdpMid: '0',
toJSON: jest.fn().mockReturnValue(fakeCandidateString),
},
} as unknown as RTCPeerConnectionIceEvent);

await untilEventSent(
FAKE_ROOM_ID,
EventType.CallCandidates,
expect.objectContaining({
candidates: [
fakeCandidateString,
],
}),
);
});
});

it("times out an incoming call", async () => {
jest.useFakeTimers();
await fakeIncomingCall(client, call, "1");
Expand Down