@libp2p/interface-compliance-tests 1.1.28 → 1.1.31

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 (53) hide show
  1. package/dist/src/index.d.ts +2 -3
  2. package/dist/src/index.d.ts.map +1 -1
  3. package/dist/src/index.js +15 -2
  4. package/dist/src/index.js.map +1 -1
  5. package/dist/src/mocks/connection-manager.d.ts +21 -7
  6. package/dist/src/mocks/connection-manager.d.ts.map +1 -1
  7. package/dist/src/mocks/connection-manager.js +97 -9
  8. package/dist/src/mocks/connection-manager.js.map +1 -1
  9. package/dist/src/mocks/index.d.ts +1 -1
  10. package/dist/src/mocks/index.d.ts.map +1 -1
  11. package/dist/src/mocks/index.js +1 -1
  12. package/dist/src/mocks/index.js.map +1 -1
  13. package/dist/src/mocks/muxer.d.ts.map +1 -1
  14. package/dist/src/mocks/muxer.js +5 -1
  15. package/dist/src/mocks/muxer.js.map +1 -1
  16. package/dist/src/mocks/registrar.d.ts +0 -2
  17. package/dist/src/mocks/registrar.d.ts.map +1 -1
  18. package/dist/src/mocks/registrar.js +2 -20
  19. package/dist/src/mocks/registrar.js.map +1 -1
  20. package/dist/src/pubsub/api.d.ts.map +1 -1
  21. package/dist/src/pubsub/api.js +21 -10
  22. package/dist/src/pubsub/api.js.map +1 -1
  23. package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
  24. package/dist/src/pubsub/connection-handlers.js +45 -43
  25. package/dist/src/pubsub/connection-handlers.js.map +1 -1
  26. package/dist/src/pubsub/emit-self.d.ts.map +1 -1
  27. package/dist/src/pubsub/emit-self.js +22 -18
  28. package/dist/src/pubsub/emit-self.js.map +1 -1
  29. package/dist/src/pubsub/messages.d.ts.map +1 -1
  30. package/dist/src/pubsub/messages.js +7 -4
  31. package/dist/src/pubsub/messages.js.map +1 -1
  32. package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
  33. package/dist/src/pubsub/multiple-nodes.js +33 -33
  34. package/dist/src/pubsub/multiple-nodes.js.map +1 -1
  35. package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
  36. package/dist/src/pubsub/two-nodes.js +10 -8
  37. package/dist/src/pubsub/two-nodes.js.map +1 -1
  38. package/dist/src/pubsub/utils.d.ts.map +1 -1
  39. package/dist/src/pubsub/utils.js +4 -1
  40. package/dist/src/pubsub/utils.js.map +1 -1
  41. package/package.json +1 -1
  42. package/src/index.ts +19 -3
  43. package/src/mocks/connection-manager.ts +125 -11
  44. package/src/mocks/index.ts +1 -1
  45. package/src/mocks/muxer.ts +7 -1
  46. package/src/mocks/registrar.ts +2 -27
  47. package/src/pubsub/api.ts +23 -10
  48. package/src/pubsub/connection-handlers.ts +47 -43
  49. package/src/pubsub/emit-self.ts +24 -19
  50. package/src/pubsub/messages.ts +7 -4
  51. package/src/pubsub/multiple-nodes.ts +35 -35
  52. package/src/pubsub/two-nodes.ts +11 -10
  53. package/src/pubsub/utils.ts +6 -1
@@ -4,7 +4,6 @@ import pDefer from 'p-defer'
4
4
  import pWaitFor from 'p-wait-for'
5
5
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
6
6
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
7
- import { connectPeers } from '../mocks/registrar.js'
8
7
  import type { TestSetup } from '../index.js'
9
8
  import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
10
9
  import type { PubSubArgs } from './index.js'
@@ -12,6 +11,7 @@ import type { Components } from '@libp2p/interfaces/components'
12
11
  import { start, stop } from '../index.js'
13
12
  import { createComponents } from './utils.js'
14
13
  import { pEvent } from 'p-event'
14
+ import { mockNetwork } from '../mocks/connection-manager.js'
15
15
 
16
16
  export default (common: TestSetup<PubSub, PubSubArgs>) => {
17
17
  describe('pubsub connection handlers', () => {
@@ -23,27 +23,28 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
23
23
  describe('nodes send state on connection', () => {
24
24
  // Create pubsub nodes and connect them
25
25
  beforeEach(async () => {
26
+ mockNetwork.reset()
27
+
26
28
  componentsA = await createComponents()
27
29
  componentsB = await createComponents()
28
30
 
29
- psA = await common.setup({
31
+ psA = componentsA.setPubSub(await common.setup({
30
32
  components: componentsA,
31
33
  init: {}
32
- })
33
- psB = await common.setup({
34
+ }))
35
+
36
+ psB = componentsB.setPubSub(await common.setup({
34
37
  components: componentsB,
35
38
  init: {}
36
- })
39
+ }))
37
40
 
38
41
  // Start pubsub
39
- await start(psA, psB)
42
+ await start(componentsA, componentsB)
40
43
 
41
44
  expect(psA.getPeers()).to.be.empty()
42
45
  expect(psB.getPeers()).to.be.empty()
43
- })
44
46
 
45
- // Make subscriptions prior to nodes connected
46
- beforeEach(() => {
47
+ // Make subscriptions prior to nodes connected
47
48
  psA.subscribe('Za')
48
49
  psB.subscribe('Zb')
49
50
 
@@ -55,8 +56,9 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
55
56
 
56
57
  afterEach(async () => {
57
58
  sinon.restore()
58
- await stop(psA, psB)
59
+ await stop(componentsA, componentsB)
59
60
  await common.teardown()
61
+ mockNetwork.reset()
60
62
  })
61
63
 
62
64
  it('existing subscriptions are sent upon peer connection', async function () {
@@ -65,7 +67,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
65
67
  pEvent(psB, 'subscription-change')
66
68
  ])
67
69
 
68
- await connectPeers(psA.multicodecs[0], componentsA, componentsB)
70
+ await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
69
71
 
70
72
  await subscriptionsChanged
71
73
 
@@ -88,29 +90,31 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
88
90
 
89
91
  // Create pubsub nodes and start them
90
92
  beforeEach(async () => {
93
+ mockNetwork.reset()
91
94
  componentsA = await createComponents()
92
95
  componentsB = await createComponents()
93
96
 
94
- psA = await common.setup({
97
+ psA = componentsA.setPubSub(await common.setup({
95
98
  components: componentsA,
96
99
  init: {}
97
- })
98
- psB = await common.setup({
100
+ }))
101
+ psB = componentsB.setPubSub(await common.setup({
99
102
  components: componentsB,
100
103
  init: {}
101
- })
104
+ }))
102
105
 
103
- await start(psA, psB)
106
+ await start(componentsA, componentsB)
104
107
  })
105
108
 
106
109
  afterEach(async () => {
107
110
  sinon.restore()
108
- await stop(psA, psB)
111
+ await stop(componentsA, componentsB)
109
112
  await common.teardown()
113
+ mockNetwork.reset()
110
114
  })
111
115
 
112
116
  it('should get notified of connected peers on dial', async () => {
113
- await connectPeers(psA.multicodecs[0], componentsA, componentsB)
117
+ await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
114
118
 
115
119
  return await Promise.all([
116
120
  pWaitFor(() => psA.getPeers().length === 1),
@@ -123,7 +127,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
123
127
  const topic = 'test-topic'
124
128
  const data = uint8ArrayFromString('hey!')
125
129
 
126
- await connectPeers(psA.multicodecs[0], componentsA, componentsB)
130
+ await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
127
131
 
128
132
  let subscribedTopics = psA.getTopics()
129
133
  expect(subscribedTopics).to.not.include(topic)
@@ -160,29 +164,31 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
160
164
 
161
165
  // Create pubsub nodes
162
166
  beforeEach(async () => {
167
+ mockNetwork.reset()
163
168
  componentsA = await createComponents()
164
169
  componentsB = await createComponents()
165
170
 
166
- psA = await common.setup({
171
+ psA = componentsA.setPubSub(await common.setup({
167
172
  components: componentsA,
168
173
  init: {}
169
- })
170
- psB = await common.setup({
174
+ }))
175
+ psB = componentsB.setPubSub(await common.setup({
171
176
  components: componentsB,
172
177
  init: {}
173
- })
178
+ }))
174
179
  })
175
180
 
176
181
  afterEach(async () => {
177
182
  sinon.restore()
178
- await stop(psA, psB)
183
+ await stop(componentsA, componentsB)
179
184
  await common.teardown()
185
+ mockNetwork.reset()
180
186
  })
181
187
 
182
188
  it('should get notified of connected peers after starting', async () => {
183
- await start(psA, psB)
189
+ await start(componentsA, componentsB)
184
190
 
185
- await connectPeers(psA.multicodecs[0], componentsA, componentsB)
191
+ await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
186
192
 
187
193
  return await Promise.all([
188
194
  pWaitFor(() => psA.getPeers().length === 1),
@@ -195,9 +201,9 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
195
201
  const topic = 'test-topic'
196
202
  const data = uint8ArrayFromString('hey!')
197
203
 
198
- await start(psA, psB)
204
+ await start(componentsA, componentsB)
199
205
 
200
- await connectPeers(psA.multicodecs[0], componentsA, componentsB)
206
+ await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
201
207
 
202
208
  await Promise.all([
203
209
  pWaitFor(() => psA.getPeers().length === 1),
@@ -239,25 +245,27 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
239
245
 
240
246
  // Create pubsub nodes and start them
241
247
  beforeEach(async () => {
248
+ mockNetwork.reset()
242
249
  componentsA = await createComponents()
243
250
  componentsB = await createComponents()
244
251
 
245
- psA = await common.setup({
252
+ psA = componentsA.setPubSub(await common.setup({
246
253
  components: componentsA,
247
254
  init: {}
248
- })
249
- psB = await common.setup({
255
+ }))
256
+ psB = componentsB.setPubSub(await common.setup({
250
257
  components: componentsB,
251
258
  init: {}
252
- })
259
+ }))
253
260
 
254
- await start(psA, psB)
261
+ await start(componentsA, componentsB)
255
262
  })
256
263
 
257
264
  afterEach(async () => {
258
265
  sinon.restore()
259
- await stop(psA, psB)
266
+ await stop(componentsA, componentsB)
260
267
  await common.teardown()
268
+ mockNetwork.reset()
261
269
  })
262
270
 
263
271
  it.skip('should receive pubsub messages after a node restart', async function () {
@@ -268,7 +276,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
268
276
  const defer1 = pDefer()
269
277
  const defer2 = pDefer()
270
278
 
271
- await connectPeers(psA.multicodecs[0], componentsA, componentsB)
279
+ await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
272
280
 
273
281
  let subscribedTopics = psA.getTopics()
274
282
  expect(subscribedTopics).to.not.include(topic)
@@ -296,9 +304,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
296
304
 
297
305
  await defer1.promise
298
306
 
299
- await psB.stop()
300
- // @ts-expect-error protected fields
301
- await psB._libp2p.stop()
307
+ await stop(psB)
302
308
  await pWaitFor(() => {
303
309
  // @ts-expect-error protected fields
304
310
  const aHasConnectionToB = psA._libp2p.connectionManager.get(psB.peerId)
@@ -307,11 +313,9 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
307
313
 
308
314
  return aHasConnectionToB != null && bHasConnectionToA != null
309
315
  })
310
- // @ts-expect-error protected fields
311
- await psB._libp2p.start()
312
- await psB.start()
316
+ await start(psB)
313
317
 
314
- await connectPeers(psA.multicodecs[0], componentsA, componentsB)
318
+ await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
315
319
 
316
320
  // wait for remoteLibp2p to know about libp2p subscription
317
321
  await pWaitFor(() => {
@@ -375,7 +379,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
375
379
  const originalConnection = await psA._libp2p.dialer.connectToPeer(psB.peerId)
376
380
 
377
381
  // second connection
378
- await connectPeers(psA.multicodecs[0], componentsA, componentsB)
382
+ await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
379
383
 
380
384
  // Wait for subscriptions to occur
381
385
  await pWaitFor(() => {
@@ -9,6 +9,7 @@ import { Components } from '@libp2p/interfaces/components'
9
9
  import { start, stop } from '../index.js'
10
10
  import type { PubSub } from '@libp2p/interfaces/pubsub'
11
11
  import { createComponents } from './utils.js'
12
+ import { mockNetwork } from '../mocks/connection-manager.js'
12
13
 
13
14
  const topic = 'foo'
14
15
  const data = uint8ArrayFromString('bar')
@@ -16,30 +17,30 @@ const shouldNotHappen = () => expect.fail()
16
17
 
17
18
  export default (common: TestSetup<PubSub, PubSubArgs>) => {
18
19
  describe('emit self', () => {
19
- let pubsub: PubSub
20
- let components: Components
21
-
22
20
  describe('enabled', () => {
21
+ let pubsub: PubSub
22
+ let components: Components
23
+
23
24
  before(async () => {
25
+ mockNetwork.reset()
24
26
  components = await createComponents()
25
27
 
26
- pubsub = await common.setup({
28
+ pubsub = components.setPubSub(await common.setup({
27
29
  components,
28
30
  init: {
29
31
  emitSelf: true
30
32
  }
31
- })
32
- })
33
+ }))
33
34
 
34
- before(async () => {
35
- await start(pubsub)
35
+ await start(components)
36
36
  pubsub.subscribe(topic)
37
37
  })
38
38
 
39
39
  after(async () => {
40
40
  sinon.restore()
41
- await stop(pubsub)
41
+ await stop(components)
42
42
  await common.teardown()
43
+ mockNetwork.reset()
43
44
  })
44
45
 
45
46
  it('should emit to self on publish', async () => {
@@ -62,27 +63,31 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
62
63
  })
63
64
 
64
65
  describe('disabled', () => {
66
+ let pubsub: PubSub
67
+ let components: Components
68
+
65
69
  before(async () => {
66
- pubsub = await common.setup({
67
- components: new Components({
68
- peerId: await createEd25519PeerId(),
69
- registrar: mockRegistrar()
70
- }),
70
+ mockNetwork.reset()
71
+ components = new Components({
72
+ peerId: await createEd25519PeerId(),
73
+ registrar: mockRegistrar()
74
+ })
75
+ pubsub = components.setPubSub(await common.setup({
76
+ components,
71
77
  init: {
72
78
  emitSelf: false
73
79
  }
74
- })
75
- })
80
+ }))
76
81
 
77
- before(async () => {
78
- await start(pubsub)
82
+ await start(components)
79
83
  pubsub.subscribe(topic)
80
84
  })
81
85
 
82
86
  after(async () => {
83
87
  sinon.restore()
84
- await stop(pubsub)
88
+ await stop(components)
85
89
  await common.teardown()
90
+ mockNetwork.reset()
86
91
  })
87
92
 
88
93
  it('should not emit to self on publish', async () => {
@@ -8,6 +8,7 @@ import type { Components } from '@libp2p/interfaces/components'
8
8
  import { start, stop } from '../index.js'
9
9
  import { pEvent } from 'p-event'
10
10
  import { createComponents } from './utils.js'
11
+ import { mockNetwork } from '../mocks/connection-manager.js'
11
12
 
12
13
  const topic = 'foo'
13
14
  const data = uint8ArrayFromString('bar')
@@ -19,21 +20,23 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
19
20
 
20
21
  // Create pubsub router
21
22
  beforeEach(async () => {
23
+ mockNetwork.reset()
22
24
  components = await createComponents()
23
25
 
24
- pubsub = await common.setup({
26
+ pubsub = components.setPubSub(await common.setup({
25
27
  components,
26
28
  init: {
27
29
  emitSelf: true
28
30
  }
29
- })
30
- await start(pubsub)
31
+ }))
32
+ await start(components)
31
33
  })
32
34
 
33
35
  afterEach(async () => {
34
36
  sinon.restore()
35
- await stop(pubsub)
37
+ await stop(components)
36
38
  await common.teardown()
39
+ mockNetwork.reset()
37
40
  })
38
41
 
39
42
  it('should emit normalized signed messages on publish', async () => {
@@ -5,7 +5,6 @@ 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 } from '../mocks/registrar.js'
9
8
  import { createComponents, waitForSubscriptionUpdate } from './utils.js'
10
9
  import type { TestSetup } from '../index.js'
11
10
  import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
@@ -13,6 +12,7 @@ import type { PubSubArgs } from './index.js'
13
12
  import type { Components } from '@libp2p/interfaces/components'
14
13
  import { start, stop } from '../index.js'
15
14
  import delay from 'delay'
15
+ import { mockNetwork } from '../mocks/connection-manager.js'
16
16
 
17
17
  export default (common: TestSetup<PubSub, PubSubArgs>) => {
18
18
  describe('pubsub with multiple nodes', function () {
@@ -30,37 +30,37 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
30
30
 
31
31
  // Create and start pubsub nodes
32
32
  beforeEach(async () => {
33
+ mockNetwork.reset()
34
+
33
35
  componentsA = await createComponents()
34
36
  componentsB = await createComponents()
35
37
  componentsC = await createComponents()
36
38
 
37
- psA = await common.setup({
39
+ psA = componentsA.setPubSub(await common.setup({
38
40
  components: componentsA,
39
41
  init: {
40
42
  emitSelf: true
41
43
  }
42
- })
43
- psB = await common.setup({
44
+ }))
45
+ psB = componentsB.setPubSub(await common.setup({
44
46
  components: componentsB,
45
47
  init: {
46
48
  emitSelf: true
47
49
  }
48
- })
49
- psC = await common.setup({
50
+ }))
51
+ psC = componentsC.setPubSub(await common.setup({
50
52
  components: componentsC,
51
53
  init: {
52
54
  emitSelf: true
53
55
  }
54
- })
56
+ }))
55
57
 
56
58
  // Start pubsub modes
57
- await start(psA, psB, psC)
58
- })
59
+ await start(componentsA, componentsB, componentsC)
59
60
 
60
- // Connect nodes
61
- beforeEach(async () => {
62
- await connectPeers(psA.multicodecs[0], componentsA, componentsB)
63
- await connectPeers(psB.multicodecs[0], componentsB, componentsC)
61
+ // Connect nodes
62
+ await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
63
+ await componentsB.getConnectionManager().openConnection(componentsC.getPeerId())
64
64
 
65
65
  // Wait for peers to be ready in pubsub
66
66
  await pWaitFor(() =>
@@ -72,10 +72,9 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
72
72
 
73
73
  afterEach(async () => {
74
74
  sinon.restore()
75
-
76
- await stop(psA, psB, psC)
77
-
75
+ await stop(componentsA, componentsB, componentsC)
78
76
  await common.teardown()
77
+ mockNetwork.reset()
79
78
  })
80
79
 
81
80
  it('subscribe to the topic on node a', async () => {
@@ -259,53 +258,53 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
259
258
 
260
259
  // Create and start pubsub nodes
261
260
  beforeEach(async () => {
261
+ mockNetwork.reset()
262
+
262
263
  componentsA = await createComponents()
263
264
  componentsB = await createComponents()
264
265
  componentsC = await createComponents()
265
266
  componentsD = await createComponents()
266
267
  componentsE = await createComponents()
267
268
 
268
- psA = await common.setup({
269
+ psA = componentsA.setPubSub(await common.setup({
269
270
  components: componentsA,
270
271
  init: {
271
272
  emitSelf: true
272
273
  }
273
- })
274
- psB = await common.setup({
274
+ }))
275
+ psB = componentsB.setPubSub(await common.setup({
275
276
  components: componentsB,
276
277
  init: {
277
278
  emitSelf: true
278
279
  }
279
- })
280
- psC = await common.setup({
280
+ }))
281
+ psC = componentsC.setPubSub(await common.setup({
281
282
  components: componentsC,
282
283
  init: {
283
284
  emitSelf: true
284
285
  }
285
- })
286
- psD = await common.setup({
286
+ }))
287
+ psD = componentsD.setPubSub(await common.setup({
287
288
  components: componentsD,
288
289
  init: {
289
290
  emitSelf: true
290
291
  }
291
- })
292
- psE = await common.setup({
292
+ }))
293
+ psE = componentsE.setPubSub(await common.setup({
293
294
  components: componentsE,
294
295
  init: {
295
296
  emitSelf: true
296
297
  }
297
- })
298
+ }))
298
299
 
299
300
  // Start pubsub nodes
300
- await start(psA, psB, psC, psD, psE)
301
- })
301
+ await start(componentsA, componentsB, componentsC, componentsD, componentsE)
302
302
 
303
- // connect nodes
304
- beforeEach(async () => {
305
- await connectPeers(psA.multicodecs[0], componentsA, componentsB)
306
- await connectPeers(psA.multicodecs[0], componentsB, componentsC)
307
- await connectPeers(psA.multicodecs[0], componentsC, componentsD)
308
- await connectPeers(psA.multicodecs[0], componentsD, componentsE)
303
+ // connect nodes
304
+ await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
305
+ await componentsB.getConnectionManager().openConnection(componentsC.getPeerId())
306
+ await componentsC.getConnectionManager().openConnection(componentsD.getPeerId())
307
+ await componentsD.getConnectionManager().openConnection(componentsE.getPeerId())
309
308
 
310
309
  // Wait for peers to be ready in pubsub
311
310
  await pWaitFor(() =>
@@ -318,8 +317,9 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
318
317
  })
319
318
 
320
319
  afterEach(async () => {
321
- await stop(psA, psB, psC, psD, psE)
320
+ await stop(componentsA, componentsB, componentsC, componentsD, componentsE)
322
321
  await common.teardown()
322
+ mockNetwork.reset()
323
323
  })
324
324
 
325
325
  it('subscribes', () => {
@@ -5,13 +5,13 @@ 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 } from '../mocks/registrar.js'
9
8
  import { createComponents, waitForSubscriptionUpdate } from './utils.js'
10
9
  import type { TestSetup } from '../index.js'
11
10
  import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
12
11
  import type { PubSubArgs } from './index.js'
13
12
  import type { Components } from '@libp2p/interfaces/components'
14
13
  import { start, stop } from '../index.js'
14
+ import { mockNetwork } from '../mocks/connection-manager.js'
15
15
 
16
16
  const topic = 'foo'
17
17
 
@@ -28,29 +28,31 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
28
28
 
29
29
  // Create pubsub nodes and connect them
30
30
  beforeEach(async () => {
31
+ mockNetwork.reset()
32
+
31
33
  componentsA = await createComponents()
32
34
  componentsB = await createComponents()
33
35
 
34
- psA = await common.setup({
36
+ psA = componentsA.setPubSub(await common.setup({
35
37
  components: componentsA,
36
38
  init: {
37
39
  emitSelf: true
38
40
  }
39
- })
40
- psB = await common.setup({
41
+ }))
42
+ psB = componentsB.setPubSub(await common.setup({
41
43
  components: componentsB,
42
44
  init: {
43
45
  emitSelf: false
44
46
  }
45
- })
47
+ }))
46
48
 
47
49
  // Start pubsub and connect nodes
48
- await start(psA, psB)
50
+ await start(componentsA, componentsB)
49
51
 
50
52
  expect(psA.getPeers()).to.be.empty()
51
53
  expect(psB.getPeers()).to.be.empty()
52
54
 
53
- await connectPeers(psA.multicodecs[0], componentsA, componentsB)
55
+ await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
54
56
 
55
57
  // Wait for peers to be ready in pubsub
56
58
  await pWaitFor(() => psA.getPeers().length === 1 && psB.getPeers().length === 1)
@@ -58,10 +60,9 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
58
60
 
59
61
  afterEach(async () => {
60
62
  sinon.restore()
61
-
62
- await stop(psA, psB)
63
-
63
+ await stop(componentsA, componentsB)
64
64
  await common.teardown()
65
+ mockNetwork.reset()
65
66
  })
66
67
 
67
68
  it('Subscribe to a topic in nodeA', async () => {
@@ -5,6 +5,7 @@ import type { PubSub, SubscriptionChangeData } from '@libp2p/interfaces/pubsub'
5
5
  import type { PeerId } from '@libp2p/interfaces/peer-id'
6
6
  import { createEd25519PeerId } from '@libp2p/peer-id-factory'
7
7
  import { mockConnectionManager, mockRegistrar } from '../mocks/index.js'
8
+ import { mockNetwork } from '../mocks/connection-manager.js'
8
9
 
9
10
  export async function waitForSubscriptionUpdate (a: PubSub, b: PeerId) {
10
11
  await pWaitFor(async () => {
@@ -15,9 +16,13 @@ export async function waitForSubscriptionUpdate (a: PubSub, b: PeerId) {
15
16
  }
16
17
 
17
18
  export async function createComponents (): Promise<Components> {
18
- return new Components({
19
+ const components = new Components({
19
20
  peerId: await createEd25519PeerId(),
20
21
  registrar: mockRegistrar(),
21
22
  connectionManager: mockConnectionManager()
22
23
  })
24
+
25
+ mockNetwork.addNode(components)
26
+
27
+ return components
23
28
  }