From 5ff035bc1dcb44a18060861fd95acd974b97bcab Mon Sep 17 00:00:00 2001 From: Mustafa BOLEKEN Date: Mon, 11 Nov 2024 12:40:43 +0300 Subject: [PATCH] Add unit tests --- src/test/js/webrtc_adaptor.test.js | 81 ++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/src/test/js/webrtc_adaptor.test.js b/src/test/js/webrtc_adaptor.test.js index 0bae8b1a..7892150e 100644 --- a/src/test/js/webrtc_adaptor.test.js +++ b/src/test/js/webrtc_adaptor.test.js @@ -1945,6 +1945,87 @@ describe("WebRTCAdaptor", function() { }); }); + it("should handle negotiation when iceRestart is true", async function() { + let adaptor; + let mockPeerConnection; + + beforeEach(function () { + adaptor = new WebRTCAdaptor({ + websocketURL: "ws://example.com", + initializeComponents: false, + }); + mockPeerConnection = { + setLocalDescription: sinon.fake.resolves(), + createOffer: sinon.fake.resolves({ sdp: "mockSdp" }), + localDescription: { sdp: "mockSdp" } + }; + adaptor.remotePeerConnection["stream1"] = mockPeerConnection; + }); + + const streamId = "stream1"; + adaptor.remotePeerConnection[streamId] = mockPeerConnection; + adaptor.iceRestart = true; + + await adaptor.remotePeerConnection[streamId].onnegotiationneeded(); + + expect(mockPeerConnection.createOffer.calledWith({ iceRestart: true })).to.be.true; + expect(mockPeerConnection.setLocalDescription.calledWith({ sdp: "mockSdp" })).to.be.true; + expect(adaptor.webSocketAdaptor.send.calledWith({ desc: { sdp: "mockSdp" } })).to.be.true; + }); + + it("should not handle negotiation when iceRestart is false", async function() { + let adaptor; + let mockPeerConnection; + + beforeEach(function () { + adaptor = new WebRTCAdaptor({ + websocketURL: "ws://example.com", + initializeComponents: false, + }); + mockPeerConnection = { + setLocalDescription: sinon.fake(), + createOffer: sinon.fake() + }; + adaptor.remotePeerConnection["stream1"] = mockPeerConnection; + }); + + const streamId = "stream1"; + adaptor.remotePeerConnection[streamId] = mockPeerConnection; + adaptor.iceRestart = false; + + await adaptor.remotePeerConnection[streamId].onnegotiationneeded(); + + expect(mockPeerConnection.createOffer.called).to.be.false; + expect(mockPeerConnection.setLocalDescription.called).to.be.false; + expect(adaptor.webSocketAdaptor.send.called).to.be.false; + }); + + it("should log error during negotiation", async function() { + let adaptor; + let mockPeerConnection; + + beforeEach(function () { + adaptor = new WebRTCAdaptor({ + websocketURL: "ws://example.com", + initializeComponents: false, + }); + mockPeerConnection = { + setLocalDescription: sinon.fake.rejects(new Error("negotiation error")), + createOffer: sinon.fake.resolves({ sdp: "mockSdp" }) + }; + adaptor.remotePeerConnection["stream1"] = mockPeerConnection; + }); + + const streamId = "stream1"; + adaptor.remotePeerConnection[streamId] = mockPeerConnection; + adaptor.iceRestart = true; + const loggerSpy = sinon.spy(Logger, "error"); + + await adaptor.remotePeerConnection[streamId].onnegotiationneeded(); + + expect(loggerSpy.calledWith('Error during negotiation', sinon.match.instanceOf(Error))).to.be.true; + loggerSpy.restore(); + }); });