@@ -20,24 +20,28 @@ import type { ConnectionManager, TransportManager } from '@libp2p/interface-inte
20
20
21
21
const browser = detect ( )
22
22
23
+ interface Initiator {
24
+ multiaddr : Multiaddr
25
+ peerConnection : RTCPeerConnection
26
+ connectionManager : StubbedInstance < ConnectionManager >
27
+ transportManager : StubbedInstance < TransportManager >
28
+ connection : StubbedInstance < Connection >
29
+ stream : Stream
30
+ log : Logger
31
+ }
32
+
33
+ interface Recipient {
34
+ peerConnection : RTCPeerConnection
35
+ connection : StubbedInstance < Connection >
36
+ abortController : AbortController
37
+ signal : AbortSignal
38
+ stream : Stream
39
+ log : Logger
40
+ }
41
+
23
42
interface PrivateToPrivateComponents {
24
- initiator : {
25
- multiaddr : Multiaddr
26
- peerConnection : RTCPeerConnection
27
- connectionManager : StubbedInstance < ConnectionManager >
28
- transportManager : StubbedInstance < TransportManager >
29
- connection : StubbedInstance < Connection >
30
- stream : Stream
31
- log : Logger
32
- }
33
- recipient : {
34
- peerConnection : RTCPeerConnection
35
- connection : StubbedInstance < Connection >
36
- abortController : AbortController
37
- signal : AbortSignal
38
- stream : Stream
39
- log : Logger
40
- }
43
+ initiator : Initiator
44
+ recipient : Recipient
41
45
}
42
46
43
47
async function getComponents ( ) : Promise < PrivateToPrivateComponents > {
@@ -85,9 +89,16 @@ async function getComponents (): Promise<PrivateToPrivateComponents> {
85
89
86
90
describe ( 'webrtc basic' , ( ) => {
87
91
const isFirefox = ( ( browser != null ) && browser . name === 'firefox' )
92
+ let initiator : Initiator
93
+ let recipient : Recipient
94
+
95
+ afterEach ( ( ) => {
96
+ initiator ?. peerConnection ?. close ( )
97
+ recipient ?. peerConnection ?. close ( )
98
+ } )
88
99
89
100
it ( 'should connect' , async ( ) => {
90
- const { initiator, recipient } = await getComponents ( )
101
+ ( { initiator, recipient } = await getComponents ( ) )
91
102
92
103
// no existing connection
93
104
initiator . connectionManager . getConnections . returns ( [ ] )
@@ -114,14 +125,11 @@ describe('webrtc basic', () => {
114
125
expect ( initiator . peerConnection . connectionState ) . eq ( 'connected' )
115
126
expect ( recipient . peerConnection . connectionState ) . eq ( 'connected' )
116
127
} )
117
-
118
- initiator . peerConnection . close ( )
119
- recipient . peerConnection . close ( )
120
128
} )
121
129
122
130
it ( 'should survive aborting during connection' , async ( ) => {
131
+ ( { initiator, recipient } = await getComponents ( ) )
123
132
const abortController = new AbortController ( )
124
- const { initiator, recipient } = await getComponents ( )
125
133
126
134
// no existing connection
127
135
initiator . connectionManager . getConnections . returns ( [ ] )
@@ -150,15 +158,20 @@ describe('webrtc basic', () => {
150
158
handleIncomingStream ( recipient )
151
159
] ) )
152
160
. to . eventually . be . rejected . with . property ( 'message' , 'Oh noes!' )
153
-
154
- initiator . peerConnection . close ( )
155
- recipient . peerConnection . close ( )
156
161
} )
157
162
} )
158
163
159
164
describe ( 'webrtc receiver' , ( ) => {
165
+ let initiator : Initiator
166
+ let recipient : Recipient
167
+
168
+ afterEach ( ( ) => {
169
+ initiator ?. peerConnection ?. close ( )
170
+ recipient ?. peerConnection ?. close ( )
171
+ } )
172
+
160
173
it ( 'should fail receiving on invalid sdp offer' , async ( ) => {
161
- const { initiator, recipient } = await getComponents ( )
174
+ ( { initiator, recipient } = await getComponents ( ) )
162
175
const receiverPeerConnectionPromise = handleIncomingStream ( recipient )
163
176
const stream = pbStream ( initiator . stream ) . pb ( Message )
164
177
@@ -171,8 +184,16 @@ describe('webrtc receiver', () => {
171
184
} )
172
185
173
186
describe ( 'webrtc dialer' , ( ) => {
187
+ let initiator : Initiator
188
+ let recipient : Recipient
189
+
190
+ afterEach ( ( ) => {
191
+ initiator ?. peerConnection ?. close ( )
192
+ recipient ?. peerConnection ?. close ( )
193
+ } )
194
+
174
195
it ( 'should fail receiving on invalid sdp answer' , async ( ) => {
175
- const { initiator, recipient } = await getComponents ( )
196
+ ( { initiator, recipient } = await getComponents ( ) )
176
197
177
198
// existing connection already exists
178
199
initiator . connectionManager . getConnections . returns ( [
@@ -190,13 +211,10 @@ describe('webrtc dialer', () => {
190
211
191
212
await stream . write ( { type : Message . Type . SDP_ANSWER , data : 'bad' } )
192
213
await expect ( initiatorPeerConnectionPromise ) . to . be . rejectedWith ( / F a i l e d t o s e t r e m o t e D e s c r i p t i o n / )
193
-
194
- initiator . peerConnection . close ( )
195
- recipient . peerConnection . close ( )
196
214
} )
197
215
198
216
it ( 'should fail on receiving a candidate before an answer' , async ( ) => {
199
- const { initiator, recipient } = await getComponents ( )
217
+ ( { initiator, recipient } = await getComponents ( ) )
200
218
201
219
// existing connection already exists
202
220
initiator . connectionManager . getConnections . returns ( [
@@ -226,9 +244,6 @@ describe('webrtc dialer', () => {
226
244
await expect ( initiatorPeerConnectionPromise ) . to . be . rejectedWith ( / R e m o t e s h o u l d s e n d a n S D P a n s w e r / )
227
245
228
246
pc . close ( )
229
-
230
- initiator . peerConnection . close ( )
231
- recipient . peerConnection . close ( )
232
247
} )
233
248
} )
234
249
0 commit comments