@@ -155,6 +155,174 @@ describe("ConnectionManager", function () {
155
155
156
156
expect ( await peerConnectedPeerExchange ) . to . eq ( true ) ;
157
157
} ) ;
158
+ it ( "should emit `waku:online` event only when first peer is connected" , async function ( ) {
159
+ const peerIdPx = await createSecp256k1PeerId ( ) ;
160
+
161
+ await waku . libp2p . peerStore . save ( peerIdPx , {
162
+ tags : {
163
+ [ Tags . PEER_EXCHANGE ] : {
164
+ value : 50 ,
165
+ ttl : 1200000
166
+ }
167
+ }
168
+ } ) ;
169
+
170
+ let eventCount = 0 ;
171
+ const wakuOnline = new Promise < boolean > ( ( resolve ) => {
172
+ waku . connectionManager . addEventListener (
173
+ EPeersByDiscoveryEvents . NODE_ONLINE ,
174
+ ( ) => {
175
+ eventCount ++ ;
176
+ resolve ( true ) ;
177
+ }
178
+ ) ;
179
+ } ) ;
180
+
181
+ expect ( waku . isConnected ( ) ) . to . be . false ;
182
+
183
+ waku . libp2p . dispatchEvent (
184
+ new CustomEvent < PeerId > ( "peer:connect" , { detail : peerIdPx } )
185
+ ) ;
186
+ waku . libp2p . dispatchEvent (
187
+ new CustomEvent < PeerId > ( "peer:connect" , {
188
+ detail : await createSecp256k1PeerId ( )
189
+ } )
190
+ ) ;
191
+ waku . libp2p . dispatchEvent (
192
+ new CustomEvent < PeerId > ( "peer:connect" , {
193
+ detail : await createSecp256k1PeerId ( )
194
+ } )
195
+ ) ;
196
+
197
+ expect ( await wakuOnline ) . to . eq ( true ) ;
198
+ expect ( eventCount ) . to . be . eq ( 1 ) ;
199
+ } ) ;
200
+ it ( "isConnected should return true after first peer connects" , async function ( ) {
201
+ const peerIdPx = await createSecp256k1PeerId ( ) ;
202
+
203
+ await waku . libp2p . peerStore . save ( peerIdPx , {
204
+ tags : {
205
+ [ Tags . PEER_EXCHANGE ] : {
206
+ value : 50 ,
207
+ ttl : 1200000
208
+ }
209
+ }
210
+ } ) ;
211
+
212
+ expect ( waku . isConnected ( ) ) . to . be . false ;
213
+
214
+ waku . libp2p . dispatchEvent (
215
+ new CustomEvent < PeerId > ( "peer:connect" , { detail : peerIdPx } )
216
+ ) ;
217
+ waku . libp2p . dispatchEvent (
218
+ new CustomEvent < PeerId > ( "peer:connect" , {
219
+ detail : await createSecp256k1PeerId ( )
220
+ } )
221
+ ) ;
222
+
223
+ await delay ( 100 ) ;
224
+
225
+ expect ( waku . isConnected ( ) ) . to . be . true ;
226
+ } ) ;
227
+ } ) ;
228
+
229
+ describe ( "peer:disconnect" , ( ) => {
230
+ it ( "should emit `waku:offline` event when all peers disconnect" , async function ( ) {
231
+ const peerIdPx = await createSecp256k1PeerId ( ) ;
232
+ const peerIdPx2 = await createSecp256k1PeerId ( ) ;
233
+
234
+ await waku . libp2p . peerStore . save ( peerIdPx , {
235
+ tags : {
236
+ [ Tags . PEER_EXCHANGE ] : {
237
+ value : 50 ,
238
+ ttl : 1200000
239
+ }
240
+ }
241
+ } ) ;
242
+
243
+ await waku . libp2p . peerStore . save ( peerIdPx2 , {
244
+ tags : {
245
+ [ Tags . PEER_EXCHANGE ] : {
246
+ value : 50 ,
247
+ ttl : 1200000
248
+ }
249
+ }
250
+ } ) ;
251
+
252
+ let eventCount = 0 ;
253
+ const wakuOffline = new Promise < boolean > ( ( resolve ) => {
254
+ waku . connectionManager . addEventListener (
255
+ EPeersByDiscoveryEvents . NODE_OFFLINE ,
256
+ ( ) => {
257
+ eventCount ++ ;
258
+ resolve ( true ) ;
259
+ }
260
+ ) ;
261
+ } ) ;
262
+
263
+ waku . libp2p . dispatchEvent (
264
+ new CustomEvent < PeerId > ( "peer:connect" , { detail : peerIdPx } )
265
+ ) ;
266
+ waku . libp2p . dispatchEvent (
267
+ new CustomEvent < PeerId > ( "peer:connect" , { detail : peerIdPx2 } )
268
+ ) ;
269
+
270
+ await delay ( 100 ) ;
271
+
272
+ expect ( waku . isConnected ( ) ) . to . be . true ;
273
+
274
+ waku . libp2p . dispatchEvent (
275
+ new CustomEvent < PeerId > ( "peer:disconnect" , { detail : peerIdPx } )
276
+ ) ;
277
+ waku . libp2p . dispatchEvent (
278
+ new CustomEvent < PeerId > ( "peer:disconnect" , { detail : peerIdPx2 } )
279
+ ) ;
280
+
281
+ expect ( await wakuOffline ) . to . eq ( true ) ;
282
+ expect ( eventCount ) . to . be . eq ( 1 ) ;
283
+ } ) ;
284
+ it ( "isConnected should return false after all peers disconnect" , async function ( ) {
285
+ const peerIdPx = await createSecp256k1PeerId ( ) ;
286
+ const peerIdPx2 = await createSecp256k1PeerId ( ) ;
287
+
288
+ await waku . libp2p . peerStore . save ( peerIdPx , {
289
+ tags : {
290
+ [ Tags . PEER_EXCHANGE ] : {
291
+ value : 50 ,
292
+ ttl : 1200000
293
+ }
294
+ }
295
+ } ) ;
296
+
297
+ await waku . libp2p . peerStore . save ( peerIdPx2 , {
298
+ tags : {
299
+ [ Tags . PEER_EXCHANGE ] : {
300
+ value : 50 ,
301
+ ttl : 1200000
302
+ }
303
+ }
304
+ } ) ;
305
+
306
+ waku . libp2p . dispatchEvent (
307
+ new CustomEvent < PeerId > ( "peer:connect" , { detail : peerIdPx } )
308
+ ) ;
309
+ waku . libp2p . dispatchEvent (
310
+ new CustomEvent < PeerId > ( "peer:connect" , { detail : peerIdPx2 } )
311
+ ) ;
312
+
313
+ await delay ( 100 ) ;
314
+
315
+ expect ( waku . isConnected ( ) ) . to . be . true ;
316
+
317
+ waku . libp2p . dispatchEvent (
318
+ new CustomEvent < PeerId > ( "peer:disconnect" , { detail : peerIdPx } )
319
+ ) ;
320
+ waku . libp2p . dispatchEvent (
321
+ new CustomEvent < PeerId > ( "peer:disconnect" , { detail : peerIdPx2 } )
322
+ ) ;
323
+
324
+ expect ( waku . isConnected ( ) ) . to . be . false ;
325
+ } ) ;
158
326
} ) ;
159
327
} ) ;
160
328
0 commit comments