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