@libp2p/interface-compliance-tests 1.1.23 → 1.1.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/dist/src/mocks/connection-manager.d.ts +0 -8
  2. package/dist/src/mocks/connection-manager.d.ts.map +1 -1
  3. package/dist/src/mocks/connection-manager.js +4 -16
  4. package/dist/src/mocks/connection-manager.js.map +1 -1
  5. package/dist/src/mocks/connection.d.ts +2 -1
  6. package/dist/src/mocks/connection.d.ts.map +1 -1
  7. package/dist/src/mocks/connection.js +4 -4
  8. package/dist/src/mocks/connection.js.map +1 -1
  9. package/dist/src/mocks/registrar.d.ts +2 -5
  10. package/dist/src/mocks/registrar.d.ts.map +1 -1
  11. package/dist/src/mocks/registrar.js +4 -4
  12. package/dist/src/mocks/registrar.js.map +1 -1
  13. package/dist/src/pubsub/api.d.ts +2 -2
  14. package/dist/src/pubsub/api.d.ts.map +1 -1
  15. package/dist/src/pubsub/api.js +10 -15
  16. package/dist/src/pubsub/api.js.map +1 -1
  17. package/dist/src/pubsub/connection-handlers.d.ts +2 -2
  18. package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
  19. package/dist/src/pubsub/connection-handlers.js +58 -148
  20. package/dist/src/pubsub/connection-handlers.js.map +1 -1
  21. package/dist/src/pubsub/emit-self.d.ts +2 -2
  22. package/dist/src/pubsub/emit-self.d.ts.map +1 -1
  23. package/dist/src/pubsub/emit-self.js +8 -7
  24. package/dist/src/pubsub/emit-self.js.map +1 -1
  25. package/dist/src/pubsub/index.d.ts +2 -3
  26. package/dist/src/pubsub/index.d.ts.map +1 -1
  27. package/dist/src/pubsub/index.js.map +1 -1
  28. package/dist/src/pubsub/messages.d.ts +2 -2
  29. package/dist/src/pubsub/messages.d.ts.map +1 -1
  30. package/dist/src/pubsub/messages.js +13 -75
  31. package/dist/src/pubsub/messages.js.map +1 -1
  32. package/dist/src/pubsub/multiple-nodes.d.ts +2 -2
  33. package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
  34. package/dist/src/pubsub/multiple-nodes.js +80 -216
  35. package/dist/src/pubsub/multiple-nodes.js.map +1 -1
  36. package/dist/src/pubsub/two-nodes.d.ts +2 -2
  37. package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
  38. package/dist/src/pubsub/two-nodes.js +20 -38
  39. package/dist/src/pubsub/two-nodes.js.map +1 -1
  40. package/dist/src/pubsub/utils.d.ts +5 -2
  41. package/dist/src/pubsub/utils.d.ts.map +1 -1
  42. package/dist/src/pubsub/utils.js +11 -1
  43. package/dist/src/pubsub/utils.js.map +1 -1
  44. package/package.json +1 -1
  45. package/src/mocks/connection-manager.ts +4 -20
  46. package/src/mocks/connection.ts +5 -5
  47. package/src/mocks/registrar.ts +6 -10
  48. package/src/pubsub/api.ts +12 -18
  49. package/src/pubsub/connection-handlers.ts +69 -170
  50. package/src/pubsub/emit-self.ts +13 -10
  51. package/src/pubsub/index.ts +2 -3
  52. package/src/pubsub/messages.ts +18 -100
  53. package/src/pubsub/multiple-nodes.ts +98 -231
  54. package/src/pubsub/two-nodes.ts +27 -46
  55. package/src/pubsub/utils.ts +15 -4
@@ -5,68 +5,48 @@ import pDefer from 'p-defer'
5
5
  import pWaitFor from 'p-wait-for'
6
6
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
7
7
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
8
- import { createEd25519PeerId } from '@libp2p/peer-id-factory'
9
- import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
10
- import { waitForSubscriptionUpdate } from './utils.js'
8
+ import { connectPeers } from '../mocks/registrar.js'
9
+ import { createComponents, waitForSubscriptionUpdate } from './utils.js'
11
10
  import type { TestSetup } from '../index.js'
12
- import type { Message } from '@libp2p/interfaces/pubsub'
11
+ import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
13
12
  import type { PubSubArgs } from './index.js'
14
- import type { PeerId } from '@libp2p/interfaces/peer-id'
15
- import type { Registrar } from '@libp2p/interfaces/registrar'
16
- import type { PubSubBaseProtocol } from '@libp2p/pubsub'
17
- import { Components } from '@libp2p/interfaces/components'
13
+ import type { Components } from '@libp2p/interfaces/components'
18
14
  import { start, stop } from '../index.js'
19
15
 
20
- export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
16
+ export default (common: TestSetup<PubSub, PubSubArgs>) => {
21
17
  describe('pubsub with multiple nodes', function () {
22
18
  describe('every peer subscribes to the topic', () => {
23
19
  describe('line', () => {
24
20
  // line
25
21
  // ◉────◉────◉
26
22
  // a b c
27
- let psA: PubSubBaseProtocol
28
- let psB: PubSubBaseProtocol
29
- let psC: PubSubBaseProtocol
30
- let peerIdA: PeerId
31
- let peerIdB: PeerId
32
- let peerIdC: PeerId
33
- let registrarA: Registrar
34
- let registrarB: Registrar
35
- let registrarC: Registrar
23
+ let psA: PubSub
24
+ let psB: PubSub
25
+ let psC: PubSub
26
+ let componentsA: Components
27
+ let componentsB: Components
28
+ let componentsC: Components
36
29
 
37
30
  // Create and start pubsub nodes
38
31
  beforeEach(async () => {
39
- peerIdA = await createEd25519PeerId()
40
- peerIdB = await createEd25519PeerId()
41
- peerIdC = await createEd25519PeerId()
42
-
43
- registrarA = mockRegistrar()
44
- registrarB = mockRegistrar()
45
- registrarC = mockRegistrar()
32
+ componentsA = await createComponents()
33
+ componentsB = await createComponents()
34
+ componentsC = await createComponents()
46
35
 
47
36
  psA = await common.setup({
48
- components: new Components({
49
- peerId: peerIdA,
50
- registrar: registrarA
51
- }),
37
+ components: componentsA,
52
38
  init: {
53
39
  emitSelf: true
54
40
  }
55
41
  })
56
42
  psB = await common.setup({
57
- components: new Components({
58
- peerId: peerIdB,
59
- registrar: registrarB
60
- }),
43
+ components: componentsB,
61
44
  init: {
62
45
  emitSelf: true
63
46
  }
64
47
  })
65
48
  psC = await common.setup({
66
- components: new Components({
67
- peerId: peerIdC,
68
- registrar: registrarC
69
- }),
49
+ components: componentsC,
70
50
  init: {
71
51
  emitSelf: true
72
52
  }
@@ -78,20 +58,8 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
78
58
 
79
59
  // Connect nodes
80
60
  beforeEach(async () => {
81
- await connectPeers(psA.multicodecs[0], {
82
- peerId: peerIdA,
83
- registrar: registrarA
84
- }, {
85
- peerId: peerIdB,
86
- registrar: registrarB
87
- })
88
- await connectPeers(psA.multicodecs[0], {
89
- peerId: peerIdB,
90
- registrar: registrarB
91
- }, {
92
- peerId: peerIdC,
93
- registrar: registrarC
94
- })
61
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
62
+ await connectPeers(psB.multicodecs[0], componentsB, componentsC)
95
63
 
96
64
  // Wait for peers to be ready in pubsub
97
65
  await pWaitFor(() =>
@@ -115,10 +83,10 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
115
83
  psA.subscribe(topic)
116
84
  expect(psA.getTopics()).to.deep.equal([topic])
117
85
 
118
- await waitForSubscriptionUpdate(psB, psA)
86
+ await waitForSubscriptionUpdate(psB, componentsA.getPeerId())
119
87
 
120
88
  expect(psB.getPeers().length).to.equal(2)
121
- expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdA.toString()])
89
+ expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsA.getPeerId().toString()])
122
90
 
123
91
  expect(psC.getPeers().length).to.equal(1)
124
92
  expect(psC.getSubscribers(topic)).to.be.empty()
@@ -130,15 +98,15 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
130
98
  expect(psB.getTopics()).to.deep.equal([topic])
131
99
 
132
100
  await Promise.all([
133
- waitForSubscriptionUpdate(psA, psB),
134
- waitForSubscriptionUpdate(psC, psB)
101
+ waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
102
+ waitForSubscriptionUpdate(psC, componentsB.getPeerId())
135
103
  ])
136
104
 
137
105
  expect(psA.getPeers().length).to.equal(1)
138
- expect(psA.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdB.toString()])
106
+ expect(psA.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsB.getPeerId().toString()])
139
107
 
140
108
  expect(psC.getPeers().length).to.equal(1)
141
- expect(psC.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdB.toString()])
109
+ expect(psC.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsB.getPeerId().toString()])
142
110
  })
143
111
 
144
112
  it('subscribe to the topic on node c', async () => {
@@ -151,7 +119,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
151
119
  psB.addEventListener('subscription-change', () => {
152
120
  expect(psA.getPeers().length).to.equal(1)
153
121
  expect(psB.getPeers().length).to.equal(2)
154
- expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdC.toString()])
122
+ expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsC.getPeerId().toString()])
155
123
 
156
124
  defer.resolve()
157
125
  }, {
@@ -171,53 +139,36 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
171
139
 
172
140
  let counter = 0
173
141
 
174
- psA.addEventListener('message', (evt) => {
175
- if (evt.detail.topic === topic) {
176
- incMsg(evt)
177
- }
178
- })
179
- psB.addEventListener('message', (evt) => {
180
- if (evt.detail.topic === topic) {
181
- incMsg(evt)
182
- }
183
- })
184
- psC.addEventListener('message', (evt) => {
185
- if (evt.detail.topic === topic) {
186
- incMsg(evt)
187
- }
188
- })
142
+ psA.addEventListener('message', incMsg)
143
+ psB.addEventListener('message', incMsg)
144
+ psC.addEventListener('message', incMsg)
189
145
 
190
146
  await Promise.all([
191
- waitForSubscriptionUpdate(psA, psB),
192
- waitForSubscriptionUpdate(psB, psA),
193
- waitForSubscriptionUpdate(psC, psB)
147
+ waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
148
+ waitForSubscriptionUpdate(psB, componentsA.getPeerId()),
149
+ waitForSubscriptionUpdate(psC, componentsB.getPeerId())
194
150
  ])
195
151
 
196
- psA.publish(topic, uint8ArrayFromString('hey'))
152
+ const result = await psA.publish(topic, uint8ArrayFromString('hey'))
153
+
154
+ expect(result).to.have.property('recipients').with.property('length').greaterThanOrEqual(1)
197
155
 
198
156
  function incMsg (evt: CustomEvent<Message>) {
199
157
  const msg = evt.detail
158
+
159
+ if (msg.topic !== topic) {
160
+ return
161
+ }
162
+
200
163
  expect(uint8ArrayToString(msg.data)).to.equal('hey')
201
164
  check()
202
165
  }
203
166
 
204
167
  function check () {
205
168
  if (++counter === 3) {
206
- psA.removeEventListener('message', (evt) => {
207
- if (evt.detail.topic === topic) {
208
- incMsg(evt)
209
- }
210
- })
211
- psB.removeEventListener('message', (evt) => {
212
- if (evt.detail.topic === topic) {
213
- incMsg(evt)
214
- }
215
- })
216
- psC.removeEventListener('message', (evt) => {
217
- if (evt.detail.topic === topic) {
218
- incMsg(evt)
219
- }
220
- })
169
+ psA.removeEventListener('message', incMsg)
170
+ psB.removeEventListener('message', incMsg)
171
+ psC.removeEventListener('message', incMsg)
221
172
  defer.resolve()
222
173
  }
223
174
  }
@@ -239,57 +190,38 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
239
190
  const defer = pDefer()
240
191
  let counter = 0
241
192
 
242
- psA.addEventListener('message', (evt) => {
243
- if (evt.detail.topic === topic) {
244
- incMsg(evt)
245
- }
246
- })
247
- psB.addEventListener('message', (evt) => {
248
- if (evt.detail.topic === topic) {
249
- incMsg(evt)
250
- }
251
- })
252
- psC.addEventListener('message', (evt) => {
253
- if (evt.detail.topic === topic) {
254
- incMsg(evt)
255
- }
256
- })
193
+ psA.addEventListener('message', incMsg)
194
+ psB.addEventListener('message', incMsg)
195
+ psC.addEventListener('message', incMsg)
257
196
 
258
197
  psA.subscribe(topic)
259
198
  psB.subscribe(topic)
260
199
  psC.subscribe(topic)
261
200
 
262
201
  await Promise.all([
263
- waitForSubscriptionUpdate(psA, psB),
264
- waitForSubscriptionUpdate(psB, psA),
265
- waitForSubscriptionUpdate(psC, psB)
202
+ waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
203
+ waitForSubscriptionUpdate(psB, componentsA.getPeerId()),
204
+ waitForSubscriptionUpdate(psC, componentsB.getPeerId())
266
205
  ])
267
206
 
268
- psB.publish(topic, uint8ArrayFromString('hey'))
207
+ await psB.publish(topic, uint8ArrayFromString('hey'))
269
208
 
270
209
  function incMsg (evt: CustomEvent<Message>) {
271
210
  const msg = evt.detail
211
+
212
+ if (msg.topic !== topic) {
213
+ return
214
+ }
215
+
272
216
  expect(uint8ArrayToString(msg.data)).to.equal('hey')
273
217
  check()
274
218
  }
275
219
 
276
220
  function check () {
277
221
  if (++counter === 3) {
278
- psA.removeEventListener('message', (evt) => {
279
- if (evt.detail.topic === topic) {
280
- incMsg(evt)
281
- }
282
- })
283
- psB.removeEventListener('message', (evt) => {
284
- if (evt.detail.topic === topic) {
285
- incMsg(evt)
286
- }
287
- })
288
- psC.removeEventListener('message', (evt) => {
289
- if (evt.detail.topic === topic) {
290
- incMsg(evt)
291
- }
292
- })
222
+ psA.removeEventListener('message', incMsg)
223
+ psB.removeEventListener('message', incMsg)
224
+ psC.removeEventListener('message', incMsg)
293
225
  defer.resolve()
294
226
  }
295
227
  }
@@ -307,77 +239,51 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
307
239
  // │b d│
308
240
  // ◉─┘ └─◉
309
241
  // a
310
- let psA: PubSubBaseProtocol
311
- let psB: PubSubBaseProtocol
312
- let psC: PubSubBaseProtocol
313
- let psD: PubSubBaseProtocol
314
- let psE: PubSubBaseProtocol
315
- let peerIdA: PeerId
316
- let peerIdB: PeerId
317
- let peerIdC: PeerId
318
- let peerIdD: PeerId
319
- let peerIdE: PeerId
320
- let registrarA: Registrar
321
- let registrarB: Registrar
322
- let registrarC: Registrar
323
- let registrarD: Registrar
324
- let registrarE: Registrar
242
+ let psA: PubSub
243
+ let psB: PubSub
244
+ let psC: PubSub
245
+ let psD: PubSub
246
+ let psE: PubSub
247
+ let componentsA: Components
248
+ let componentsB: Components
249
+ let componentsC: Components
250
+ let componentsD: Components
251
+ let componentsE: Components
325
252
 
326
253
  // Create and start pubsub nodes
327
254
  beforeEach(async () => {
328
- peerIdA = await createEd25519PeerId()
329
- peerIdB = await createEd25519PeerId()
330
- peerIdC = await createEd25519PeerId()
331
- peerIdD = await createEd25519PeerId()
332
- peerIdE = await createEd25519PeerId()
333
-
334
- registrarA = mockRegistrar()
335
- registrarB = mockRegistrar()
336
- registrarC = mockRegistrar()
337
- registrarD = mockRegistrar()
338
- registrarE = mockRegistrar()
255
+ componentsA = await createComponents()
256
+ componentsB = await createComponents()
257
+ componentsC = await createComponents()
258
+ componentsD = await createComponents()
259
+ componentsE = await createComponents()
339
260
 
340
261
  psA = await common.setup({
341
- components: new Components({
342
- peerId: peerIdA,
343
- registrar: registrarA
344
- }),
262
+ components: componentsA,
345
263
  init: {
346
264
  emitSelf: true
347
265
  }
348
266
  })
349
267
  psB = await common.setup({
350
- components: new Components({
351
- peerId: peerIdB,
352
- registrar: registrarB
353
- }),
268
+ components: componentsB,
354
269
  init: {
355
270
  emitSelf: true
356
271
  }
357
272
  })
358
273
  psC = await common.setup({
359
- components: new Components({
360
- peerId: peerIdC,
361
- registrar: registrarC
362
- }),
274
+ components: componentsC,
363
275
  init: {
364
276
  emitSelf: true
365
277
  }
366
278
  })
367
279
  psD = await common.setup({
368
- components: new Components({
369
- peerId: peerIdD,
370
- registrar: registrarD
371
- }),
280
+ components: componentsD,
372
281
  init: {
373
282
  emitSelf: true
374
283
  }
375
284
  })
376
285
  psE = await common.setup({
377
- components: new Components({
378
- peerId: peerIdE,
379
- registrar: registrarE
380
- }),
286
+ components: componentsE,
381
287
  init: {
382
288
  emitSelf: true
383
289
  }
@@ -389,34 +295,10 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
389
295
 
390
296
  // connect nodes
391
297
  beforeEach(async () => {
392
- await connectPeers(psA.multicodecs[0], {
393
- peerId: peerIdA,
394
- registrar: registrarA
395
- }, {
396
- peerId: peerIdB,
397
- registrar: registrarB
398
- })
399
- await connectPeers(psA.multicodecs[0], {
400
- peerId: peerIdB,
401
- registrar: registrarB
402
- }, {
403
- peerId: peerIdC,
404
- registrar: registrarC
405
- })
406
- await connectPeers(psA.multicodecs[0], {
407
- peerId: peerIdC,
408
- registrar: registrarC
409
- }, {
410
- peerId: peerIdD,
411
- registrar: registrarD
412
- })
413
- await connectPeers(psA.multicodecs[0], {
414
- peerId: peerIdD,
415
- registrar: registrarD
416
- }, {
417
- peerId: peerIdE,
418
- registrar: registrarE
419
- })
298
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
299
+ await connectPeers(psA.multicodecs[0], componentsB, componentsC)
300
+ await connectPeers(psA.multicodecs[0], componentsC, componentsD)
301
+ await connectPeers(psA.multicodecs[0], componentsD, componentsE)
420
302
 
421
303
  // Wait for peers to be ready in pubsub
422
304
  await pWaitFor(() =>
@@ -452,48 +334,33 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
452
334
  const topic = 'Z'
453
335
 
454
336
  psA.subscribe(topic)
455
- psA.addEventListener('message', (evt) => {
456
- if (evt.detail.topic === topic) {
457
- incMsg(evt)
458
- }
459
- })
337
+ psA.addEventListener('message', incMsg)
460
338
  psB.subscribe(topic)
461
- psB.addEventListener('message', (evt) => {
462
- if (evt.detail.topic === topic) {
463
- incMsg(evt)
464
- }
465
- })
339
+ psB.addEventListener('message', incMsg)
466
340
  psC.subscribe(topic)
467
- psC.addEventListener('message', (evt) => {
468
- if (evt.detail.topic === topic) {
469
- incMsg(evt)
470
- }
471
- })
341
+ psC.addEventListener('message', incMsg)
472
342
  psD.subscribe(topic)
473
- psD.addEventListener('message', (evt) => {
474
- if (evt.detail.topic === topic) {
475
- incMsg(evt)
476
- }
477
- })
343
+ psD.addEventListener('message', incMsg)
478
344
  psE.subscribe(topic)
479
- psE.addEventListener('message', (evt) => {
480
- if (evt.detail.topic === topic) {
481
- incMsg(evt)
482
- }
483
- })
345
+ psE.addEventListener('message', incMsg)
484
346
 
485
347
  await Promise.all([
486
- waitForSubscriptionUpdate(psA, psB),
487
- waitForSubscriptionUpdate(psB, psA),
488
- waitForSubscriptionUpdate(psC, psB),
489
- waitForSubscriptionUpdate(psD, psC),
490
- waitForSubscriptionUpdate(psE, psD)
348
+ waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
349
+ waitForSubscriptionUpdate(psB, componentsA.getPeerId()),
350
+ waitForSubscriptionUpdate(psC, componentsB.getPeerId()),
351
+ waitForSubscriptionUpdate(psD, componentsC.getPeerId()),
352
+ waitForSubscriptionUpdate(psE, componentsD.getPeerId())
491
353
  ])
492
354
 
493
- psC.publish('Z', uint8ArrayFromString('hey from c'))
355
+ await psC.publish('Z', uint8ArrayFromString('hey from c'))
494
356
 
495
357
  function incMsg (evt: CustomEvent<Message>) {
496
358
  const msg = evt.detail
359
+
360
+ if (msg.topic !== topic) {
361
+ return
362
+ }
363
+
497
364
  expect(uint8ArrayToString(msg.data)).to.equal('hey from c')
498
365
  check()
499
366
  }
@@ -5,16 +5,12 @@ import pDefer from 'p-defer'
5
5
  import pWaitFor from 'p-wait-for'
6
6
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
7
7
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
8
- import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
9
- import { createEd25519PeerId } from '@libp2p/peer-id-factory'
10
- import { waitForSubscriptionUpdate } from './utils.js'
8
+ import { connectPeers } from '../mocks/registrar.js'
9
+ import { createComponents, waitForSubscriptionUpdate } from './utils.js'
11
10
  import type { TestSetup } from '../index.js'
12
- import type { Message } from '@libp2p/interfaces/pubsub'
11
+ import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
13
12
  import type { PubSubArgs } from './index.js'
14
- import type { PeerId } from '@libp2p/interfaces/peer-id'
15
- import type { Registrar } from '@libp2p/interfaces/registrar'
16
- import type { PubSubBaseProtocol } from '@libp2p/pubsub'
17
- import { Components } from '@libp2p/interfaces/components'
13
+ import type { Components } from '@libp2p/interfaces/components'
18
14
  import { start, stop } from '../index.js'
19
15
 
20
16
  const topic = 'foo'
@@ -23,37 +19,26 @@ function shouldNotHappen () {
23
19
  expect.fail()
24
20
  }
25
21
 
26
- export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
22
+ export default (common: TestSetup<PubSub, PubSubArgs>) => {
27
23
  describe('pubsub with two nodes', () => {
28
- let psA: PubSubBaseProtocol
29
- let psB: PubSubBaseProtocol
30
- let peerIdA: PeerId
31
- let peerIdB: PeerId
32
- let registrarA: Registrar
33
- let registrarB: Registrar
24
+ let psA: PubSub
25
+ let psB: PubSub
26
+ let componentsA: Components
27
+ let componentsB: Components
34
28
 
35
29
  // Create pubsub nodes and connect them
36
30
  beforeEach(async () => {
37
- peerIdA = await createEd25519PeerId()
38
- peerIdB = await createEd25519PeerId()
39
-
40
- registrarA = mockRegistrar()
41
- registrarB = mockRegistrar()
31
+ componentsA = await createComponents()
32
+ componentsB = await createComponents()
42
33
 
43
34
  psA = await common.setup({
44
- components: new Components({
45
- peerId: peerIdA,
46
- registrar: registrarA
47
- }),
35
+ components: componentsA,
48
36
  init: {
49
37
  emitSelf: true
50
38
  }
51
39
  })
52
40
  psB = await common.setup({
53
- components: new Components({
54
- peerId: peerIdB,
55
- registrar: registrarB
56
- }),
41
+ components: componentsB,
57
42
  init: {
58
43
  emitSelf: false
59
44
  }
@@ -65,13 +50,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
65
50
  expect(psA.getPeers()).to.be.empty()
66
51
  expect(psB.getPeers()).to.be.empty()
67
52
 
68
- await connectPeers(psA.multicodecs[0], {
69
- peerId: peerIdA,
70
- registrar: registrarA
71
- }, {
72
- peerId: peerIdB,
73
- registrar: registrarB
74
- })
53
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
75
54
 
76
55
  // Wait for peers to be ready in pubsub
77
56
  await pWaitFor(() => psA.getPeers().length === 1 && psB.getPeers().length === 1)
@@ -92,7 +71,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
92
71
  const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
93
72
  expect(psA.getTopics()).to.deep.equal([topic])
94
73
  expect(psB.getPeers()).to.have.lengthOf(1)
95
- expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdA.toString()])
74
+ expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsA.getPeerId().toString()])
96
75
  expect(changedPeerId).to.deep.equal(psB.getPeers()[0])
97
76
  expect(changedSubs).to.have.lengthOf(1)
98
77
  expect(changedSubs[0].topic).to.equal(topic)
@@ -124,11 +103,11 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
124
103
  psB.subscribe(topic)
125
104
 
126
105
  await Promise.all([
127
- waitForSubscriptionUpdate(psA, psB),
128
- waitForSubscriptionUpdate(psB, psA)
106
+ waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
107
+ waitForSubscriptionUpdate(psB, componentsA.getPeerId())
129
108
  ])
130
109
 
131
- psA.publish(topic, uint8ArrayFromString('hey'))
110
+ await psA.publish(topic, uint8ArrayFromString('hey'))
132
111
 
133
112
  return await defer.promise
134
113
  })
@@ -167,11 +146,11 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
167
146
  psB.subscribe(topic)
168
147
 
169
148
  await Promise.all([
170
- waitForSubscriptionUpdate(psA, psB),
171
- waitForSubscriptionUpdate(psB, psA)
149
+ waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
150
+ waitForSubscriptionUpdate(psB, componentsA.getPeerId())
172
151
  ])
173
152
 
174
- psB.publish(topic, uint8ArrayFromString('banana'))
153
+ await psB.publish(topic, uint8ArrayFromString('banana'))
175
154
 
176
155
  return await defer.promise
177
156
  })
@@ -186,7 +165,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
186
165
  function receivedMsg (evt: CustomEvent<Message>) {
187
166
  const msg = evt.detail
188
167
  expect(uint8ArrayToString(msg.data)).to.equal('banana')
189
- expect(msg.from.toString()).to.equal(peerIdB.toString())
168
+ expect(msg.from.toString()).to.equal(componentsB.getPeerId().toString())
190
169
  expect(msg.sequenceNumber).to.be.a('BigInt')
191
170
  expect(msg.topic).to.be.equal(topic)
192
171
 
@@ -202,11 +181,13 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
202
181
  psB.subscribe(topic)
203
182
 
204
183
  await Promise.all([
205
- waitForSubscriptionUpdate(psA, psB),
206
- waitForSubscriptionUpdate(psB, psA)
184
+ waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
185
+ waitForSubscriptionUpdate(psB, componentsA.getPeerId())
207
186
  ])
208
187
 
209
- Array.from({ length: 10 }, (_, i) => psB.publish(topic, uint8ArrayFromString('banana')))
188
+ await Promise.all(
189
+ Array.from({ length: 10 }, async (_, i) => await psB.publish(topic, uint8ArrayFromString('banana')))
190
+ )
210
191
 
211
192
  return await defer.promise
212
193
  })
@@ -1,12 +1,23 @@
1
1
  import { pEvent } from 'p-event'
2
2
  import pWaitFor from 'p-wait-for'
3
- import type { SubscriptionChangeData } from '@libp2p/interfaces/pubsub'
4
- import type { PubSubBaseProtocol } from '@libp2p/pubsub'
3
+ import { Components } from '@libp2p/interfaces/components'
4
+ import type { PubSub, SubscriptionChangeData } from '@libp2p/interfaces/pubsub'
5
+ import type { PeerId } from '@libp2p/interfaces/peer-id'
6
+ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
7
+ import { mockConnectionManager, mockRegistrar } from '../mocks/index.js'
5
8
 
6
- export async function waitForSubscriptionUpdate (a: PubSubBaseProtocol, b: PubSubBaseProtocol) {
9
+ export async function waitForSubscriptionUpdate (a: PubSub, b: PeerId) {
7
10
  await pWaitFor(async () => {
8
11
  const event = await pEvent<'subscription-change', CustomEvent<SubscriptionChangeData>>(a, 'subscription-change')
9
12
 
10
- return event.detail.peerId.equals(b.components.getPeerId())
13
+ return event.detail.peerId.equals(b)
14
+ })
15
+ }
16
+
17
+ export async function createComponents (): Promise<Components> {
18
+ return new Components({
19
+ peerId: await createEd25519PeerId(),
20
+ registrar: mockRegistrar(),
21
+ connectionManager: mockConnectionManager()
11
22
  })
12
23
  }