@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
@@ -4,47 +4,34 @@ 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 { createEd25519PeerId } from '@libp2p/peer-id-factory'
8
- import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
7
+ import { connectPeers } from '../mocks/registrar.js'
9
8
  import type { TestSetup } from '../index.js'
10
- import type { Message } from '@libp2p/interfaces/pubsub'
9
+ import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
11
10
  import type { PubSubArgs } from './index.js'
12
- import type { PeerId } from '@libp2p/interfaces/peer-id'
13
- import type { Registrar } from '@libp2p/interfaces/registrar'
14
- import type { PubSubBaseProtocol } from '@libp2p/pubsub'
15
- import { Components } from '@libp2p/interfaces/components'
11
+ import type { Components } from '@libp2p/interfaces/components'
16
12
  import { start, stop } from '../index.js'
13
+ import { createComponents } from './utils.js'
14
+ import { pEvent } from 'p-event'
17
15
 
18
- export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
16
+ export default (common: TestSetup<PubSub, PubSubArgs>) => {
19
17
  describe('pubsub connection handlers', () => {
20
- let psA: PubSubBaseProtocol
21
- let psB: PubSubBaseProtocol
22
- let peerA: PeerId
23
- let peerB: PeerId
24
- let registrarA: Registrar
25
- let registrarB: Registrar
18
+ let psA: PubSub
19
+ let psB: PubSub
20
+ let componentsA: Components
21
+ let componentsB: Components
26
22
 
27
23
  describe('nodes send state on connection', () => {
28
24
  // Create pubsub nodes and connect them
29
- before(async () => {
30
- peerA = await createEd25519PeerId()
31
- peerB = await createEd25519PeerId()
32
-
33
- registrarA = mockRegistrar()
34
- registrarB = mockRegistrar()
25
+ beforeEach(async () => {
26
+ componentsA = await createComponents()
27
+ componentsB = await createComponents()
35
28
 
36
29
  psA = await common.setup({
37
- components: new Components({
38
- peerId: peerA,
39
- registrar: registrarA
40
- }),
30
+ components: componentsA,
41
31
  init: {}
42
32
  })
43
33
  psB = await common.setup({
44
- components: new Components({
45
- peerId: peerB,
46
- registrar: registrarB
47
- }),
34
+ components: componentsB,
48
35
  init: {}
49
36
  })
50
37
 
@@ -56,7 +43,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
56
43
  })
57
44
 
58
45
  // Make subscriptions prior to nodes connected
59
- before(() => {
46
+ beforeEach(() => {
60
47
  psA.subscribe('Za')
61
48
  psB.subscribe('Zb')
62
49
 
@@ -66,28 +53,19 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
66
53
  expect(psB.getTopics()).to.deep.equal(['Zb'])
67
54
  })
68
55
 
69
- after(async () => {
56
+ afterEach(async () => {
70
57
  sinon.restore()
58
+ await stop(psA, psB)
71
59
  await common.teardown()
72
60
  })
73
61
 
74
62
  it('existing subscriptions are sent upon peer connection', async function () {
75
63
  const subscriptionsChanged = Promise.all([
76
- new Promise((resolve) => psA.addEventListener('subscription-change', resolve, {
77
- once: true
78
- })),
79
- new Promise((resolve) => psB.addEventListener('subscription-change', resolve, {
80
- once: true
81
- }))
64
+ pEvent(psA, 'subscription-change'),
65
+ pEvent(psB, 'subscription-change')
82
66
  ])
83
67
 
84
- await connectPeers(psA.multicodecs[0], {
85
- peerId: peerA,
86
- registrar: registrarA
87
- }, {
88
- peerId: peerB,
89
- registrar: registrarB
90
- })
68
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
91
69
 
92
70
  await subscriptionsChanged
93
71
 
@@ -97,39 +75,28 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
97
75
  expect(psA.getTopics()).to.deep.equal(['Za'])
98
76
  expect(psB.getTopics()).to.deep.equal(['Zb'])
99
77
 
100
- expect(psA.getSubscribers('Zb').map(p => p.toString())).to.deep.equal([peerB.toString()])
101
- expect(psB.getSubscribers('Za').map(p => p.toString())).to.deep.equal([peerA.toString()])
78
+ expect(psA.getSubscribers('Zb').map(p => p.toString())).to.deep.equal([componentsB.getPeerId().toString()])
79
+ expect(psB.getSubscribers('Za').map(p => p.toString())).to.deep.equal([componentsA.getPeerId().toString()])
102
80
  })
103
81
  })
104
82
 
105
83
  describe('pubsub started before connect', () => {
106
- let psA: PubSubBaseProtocol
107
- let psB: PubSubBaseProtocol
108
- let peerA: PeerId
109
- let peerB: PeerId
110
- let registrarA: Registrar
111
- let registrarB: Registrar
84
+ let psA: PubSub
85
+ let psB: PubSub
86
+ let componentsA: Components
87
+ let componentsB: Components
112
88
 
113
89
  // Create pubsub nodes and start them
114
90
  beforeEach(async () => {
115
- peerA = await createEd25519PeerId()
116
- peerB = await createEd25519PeerId()
117
-
118
- registrarA = mockRegistrar()
119
- registrarB = mockRegistrar()
91
+ componentsA = await createComponents()
92
+ componentsB = await createComponents()
120
93
 
121
94
  psA = await common.setup({
122
- components: new Components({
123
- peerId: peerA,
124
- registrar: registrarA
125
- }),
95
+ components: componentsA,
126
96
  init: {}
127
97
  })
128
98
  psB = await common.setup({
129
- components: new Components({
130
- peerId: peerB,
131
- registrar: registrarB
132
- }),
99
+ components: componentsB,
133
100
  init: {}
134
101
  })
135
102
 
@@ -138,18 +105,12 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
138
105
 
139
106
  afterEach(async () => {
140
107
  sinon.restore()
141
-
108
+ await stop(psA, psB)
142
109
  await common.teardown()
143
110
  })
144
111
 
145
112
  it('should get notified of connected peers on dial', async () => {
146
- await connectPeers(psA.multicodecs[0], {
147
- peerId: peerA,
148
- registrar: registrarA
149
- }, {
150
- peerId: peerB,
151
- registrar: registrarB
152
- })
113
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
153
114
 
154
115
  return await Promise.all([
155
116
  pWaitFor(() => psA.getPeers().length === 1),
@@ -162,13 +123,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
162
123
  const topic = 'test-topic'
163
124
  const data = uint8ArrayFromString('hey!')
164
125
 
165
- await connectPeers(psA.multicodecs[0], {
166
- peerId: peerA,
167
- registrar: registrarA
168
- }, {
169
- peerId: peerB,
170
- registrar: registrarB
171
- })
126
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
172
127
 
173
128
  let subscribedTopics = psA.getTopics()
174
129
  expect(subscribedTopics).to.not.include(topic)
@@ -189,64 +144,45 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
189
144
  // wait for psB to know about psA subscription
190
145
  await pWaitFor(() => {
191
146
  const subscribedPeers = psB.getSubscribers(topic)
192
- return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
147
+ return subscribedPeers.map(p => p.toString()).includes(componentsA.getPeerId().toString()) // eslint-disable-line max-nested-callbacks
193
148
  })
194
- psB.publish(topic, data)
149
+ await psB.publish(topic, data)
195
150
 
196
151
  await defer.promise
197
152
  })
198
153
  })
199
154
 
200
155
  describe('pubsub started after connect', () => {
201
- let psA: PubSubBaseProtocol
202
- let psB: PubSubBaseProtocol
203
- let peerA: PeerId
204
- let peerB: PeerId
205
- let registrarA: Registrar
206
- let registrarB: Registrar
156
+ let psA: PubSub
157
+ let psB: PubSub
158
+ let componentsA: Components
159
+ let componentsB: Components
207
160
 
208
161
  // Create pubsub nodes
209
162
  beforeEach(async () => {
210
- peerA = await createEd25519PeerId()
211
- peerB = await createEd25519PeerId()
212
-
213
- registrarA = mockRegistrar()
214
- registrarB = mockRegistrar()
163
+ componentsA = await createComponents()
164
+ componentsB = await createComponents()
215
165
 
216
166
  psA = await common.setup({
217
- components: new Components({
218
- peerId: peerA,
219
- registrar: registrarA
220
- }),
167
+ components: componentsA,
221
168
  init: {}
222
169
  })
223
170
  psB = await common.setup({
224
- components: new Components({
225
- peerId: peerB,
226
- registrar: registrarB
227
- }),
171
+ components: componentsB,
228
172
  init: {}
229
173
  })
230
174
  })
231
175
 
232
176
  afterEach(async () => {
233
177
  sinon.restore()
234
-
235
178
  await stop(psA, psB)
236
-
237
179
  await common.teardown()
238
180
  })
239
181
 
240
182
  it('should get notified of connected peers after starting', async () => {
241
183
  await start(psA, psB)
242
184
 
243
- await connectPeers(psA.multicodecs[0], {
244
- peerId: peerA,
245
- registrar: registrarA
246
- }, {
247
- peerId: peerB,
248
- registrar: registrarB
249
- })
185
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
250
186
 
251
187
  return await Promise.all([
252
188
  pWaitFor(() => psA.getPeers().length === 1),
@@ -261,13 +197,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
261
197
 
262
198
  await start(psA, psB)
263
199
 
264
- await connectPeers(psA.multicodecs[0], {
265
- peerId: peerA,
266
- registrar: registrarA
267
- }, {
268
- peerId: peerB,
269
- registrar: registrarB
270
- })
200
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
271
201
 
272
202
  await Promise.all([
273
203
  pWaitFor(() => psA.getPeers().length === 1),
@@ -293,42 +223,31 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
293
223
  // wait for psB to know about psA subscription
294
224
  await pWaitFor(() => {
295
225
  const subscribedPeers = psB.getSubscribers(topic)
296
- return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
226
+ return subscribedPeers.map(p => p.toString()).includes(componentsA.getPeerId().toString()) // eslint-disable-line max-nested-callbacks
297
227
  })
298
- psB.publish(topic, data)
228
+ await psB.publish(topic, data)
299
229
 
300
230
  await defer.promise
301
231
  })
302
232
  })
303
233
 
304
234
  describe('pubsub with intermittent connections', () => {
305
- let psA: PubSubBaseProtocol
306
- let psB: PubSubBaseProtocol
307
- let peerA: PeerId
308
- let peerB: PeerId
309
- let registrarA: Registrar
310
- let registrarB: Registrar
235
+ let psA: PubSub
236
+ let psB: PubSub
237
+ let componentsA: Components
238
+ let componentsB: Components
311
239
 
312
240
  // Create pubsub nodes and start them
313
241
  beforeEach(async () => {
314
- peerA = await createEd25519PeerId()
315
- peerB = await createEd25519PeerId()
316
-
317
- registrarA = mockRegistrar()
318
- registrarB = mockRegistrar()
242
+ componentsA = await createComponents()
243
+ componentsB = await createComponents()
319
244
 
320
245
  psA = await common.setup({
321
- components: new Components({
322
- peerId: peerA,
323
- registrar: registrarA
324
- }),
246
+ components: componentsA,
325
247
  init: {}
326
248
  })
327
249
  psB = await common.setup({
328
- components: new Components({
329
- peerId: peerB,
330
- registrar: registrarB
331
- }),
250
+ components: componentsB,
332
251
  init: {}
333
252
  })
334
253
 
@@ -337,9 +256,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
337
256
 
338
257
  afterEach(async () => {
339
258
  sinon.restore()
340
-
341
259
  await stop(psA, psB)
342
-
343
260
  await common.teardown()
344
261
  })
345
262
 
@@ -351,13 +268,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
351
268
  const defer1 = pDefer()
352
269
  const defer2 = pDefer()
353
270
 
354
- await connectPeers(psA.multicodecs[0], {
355
- peerId: peerA,
356
- registrar: registrarA
357
- }, {
358
- peerId: peerB,
359
- registrar: registrarB
360
- })
271
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
361
272
 
362
273
  let subscribedTopics = psA.getTopics()
363
274
  expect(subscribedTopics).to.not.include(topic)
@@ -379,9 +290,9 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
379
290
  // wait for psB to know about psA subscription
380
291
  await pWaitFor(() => {
381
292
  const subscribedPeers = psB.getSubscribers(topic)
382
- return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
293
+ return subscribedPeers.map(p => p.toString()).includes(componentsA.getPeerId().toString()) // eslint-disable-line max-nested-callbacks
383
294
  })
384
- psB.publish(topic, data)
295
+ await psB.publish(topic, data)
385
296
 
386
297
  await defer1.promise
387
298
 
@@ -400,21 +311,15 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
400
311
  await psB._libp2p.start()
401
312
  await psB.start()
402
313
 
403
- await connectPeers(psA.multicodecs[0], {
404
- peerId: peerA,
405
- registrar: registrarA
406
- }, {
407
- peerId: peerB,
408
- registrar: registrarB
409
- })
314
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
410
315
 
411
316
  // wait for remoteLibp2p to know about libp2p subscription
412
317
  await pWaitFor(() => {
413
318
  const subscribedPeers = psB.getSubscribers(topic)
414
- return subscribedPeers.toString().includes(peerA.toString())
319
+ return subscribedPeers.toString().includes(componentsA.getPeerId().toString())
415
320
  })
416
321
 
417
- psB.publish(topic, data)
322
+ await psB.publish(topic, data)
418
323
 
419
324
  await defer2.promise
420
325
  })
@@ -470,23 +375,17 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
470
375
  const originalConnection = await psA._libp2p.dialer.connectToPeer(psB.peerId)
471
376
 
472
377
  // second connection
473
- await connectPeers(psA.multicodecs[0], {
474
- peerId: peerA,
475
- registrar: registrarA
476
- }, {
477
- peerId: peerB,
478
- registrar: registrarB
479
- })
378
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
480
379
 
481
380
  // Wait for subscriptions to occur
482
381
  await pWaitFor(() => {
483
- return psA.getSubscribers(topic).includes(peerB) &&
484
- psB.getSubscribers(topic).map(p => p.toString()).includes(peerA.toString())
382
+ return psA.getSubscribers(topic).map(p => p.toString()).includes(componentsB.getPeerId().toString()) &&
383
+ psB.getSubscribers(topic).map(p => p.toString()).includes(componentsA.getPeerId().toString())
485
384
  })
486
385
 
487
386
  // Verify messages go both ways
488
- psA.publish(topic, uint8ArrayFromString('message-from-a-1'))
489
- psB.publish(topic, uint8ArrayFromString('message-from-b-1'))
387
+ await psA.publish(topic, uint8ArrayFromString('message-from-a-1'))
388
+ await psB.publish(topic, uint8ArrayFromString('message-from-b-1'))
490
389
  await pWaitFor(() => {
491
390
  return aReceivedFirstMessageFromB && bReceivedFirstMessageFromA
492
391
  })
@@ -499,8 +398,8 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
499
398
  await pWaitFor(() => psAConnUpdateSpy.callCount === 1)
500
399
 
501
400
  // Verify messages go both ways after the disconnect
502
- psA.publish(topic, uint8ArrayFromString('message-from-a-2'))
503
- psB.publish(topic, uint8ArrayFromString('message-from-b-2'))
401
+ await psA.publish(topic, uint8ArrayFromString('message-from-a-2'))
402
+ await psB.publish(topic, uint8ArrayFromString('message-from-b-2'))
504
403
  await pWaitFor(() => {
505
404
  return aReceivedSecondMessageFromB && bReceivedSecondMessageFromA
506
405
  })
@@ -5,25 +5,26 @@ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
5
5
  import { mockRegistrar } from '../mocks/registrar.js'
6
6
  import type { TestSetup } from '../index.js'
7
7
  import type { PubSubArgs } from './index.js'
8
- import type { PubSubBaseProtocol } from '@libp2p/pubsub'
9
8
  import { Components } from '@libp2p/interfaces/components'
10
9
  import { start, stop } from '../index.js'
10
+ import type { PubSub } from '@libp2p/interfaces/pubsub'
11
+ import { createComponents } from './utils.js'
11
12
 
12
13
  const topic = 'foo'
13
14
  const data = uint8ArrayFromString('bar')
14
15
  const shouldNotHappen = () => expect.fail()
15
16
 
16
- export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
17
+ export default (common: TestSetup<PubSub, PubSubArgs>) => {
17
18
  describe('emit self', () => {
18
- let pubsub: PubSubBaseProtocol
19
+ let pubsub: PubSub
20
+ let components: Components
19
21
 
20
22
  describe('enabled', () => {
21
23
  before(async () => {
24
+ components = await createComponents()
25
+
22
26
  pubsub = await common.setup({
23
- components: new Components({
24
- peerId: await createEd25519PeerId(),
25
- registrar: mockRegistrar()
26
- }),
27
+ components,
27
28
  init: {
28
29
  emitSelf: true
29
30
  }
@@ -52,9 +53,11 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
52
53
  })
53
54
  })
54
55
 
55
- pubsub.publish(topic, data)
56
+ const result = await pubsub.publish(topic, data)
57
+
58
+ await promise
56
59
 
57
- return await promise
60
+ expect(result).to.have.property('recipients').with.lengthOf(1)
58
61
  })
59
62
  })
60
63
 
@@ -87,7 +90,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
87
90
  once: true
88
91
  })
89
92
 
90
- pubsub.publish(topic, data)
93
+ await pubsub.publish(topic, data)
91
94
 
92
95
  // Wait 1 second to guarantee that self is not noticed
93
96
  return await new Promise((resolve) => setTimeout(resolve, 1000))
@@ -5,8 +5,7 @@ import connectionHandlersTest from './connection-handlers.js'
5
5
  import twoNodesTest from './two-nodes.js'
6
6
  import multipleNodesTest from './multiple-nodes.js'
7
7
  import type { TestSetup } from '../index.js'
8
- import type { PubSubInit } from '@libp2p/interfaces/pubsub'
9
- import type { PubSubBaseProtocol } from '@libp2p/pubsub'
8
+ import type { PubSub, PubSubInit } from '@libp2p/interfaces/pubsub'
10
9
  import type { Components } from '@libp2p/interfaces/components'
11
10
 
12
11
  export interface PubSubArgs {
@@ -14,7 +13,7 @@ export interface PubSubArgs {
14
13
  init: PubSubInit
15
14
  }
16
15
 
17
- export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
16
+ export default (common: TestSetup<PubSub, PubSubArgs>) => {
18
17
  describe('interface-pubsub compliance tests', () => {
19
18
  apiTest(common)
20
19
  emitSelfTest(common)
@@ -1,38 +1,28 @@
1
1
  import { expect } from 'aegir/chai'
2
2
  import sinon from 'sinon'
3
- import { createEd25519PeerId } from '@libp2p/peer-id-factory'
4
3
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
5
- import { noSignMsgId } from '@libp2p/pubsub/utils'
6
- import { PeerStreams } from '@libp2p/pubsub/peer-streams'
7
- import { mockRegistrar } from '../mocks/registrar.js'
8
- import pDefer from 'p-defer'
9
- import delay from 'delay'
10
- import pWaitFor from 'p-wait-for'
11
4
  import type { TestSetup } from '../index.js'
12
- import type { PubSubRPC } from '@libp2p/interfaces/pubsub'
5
+ import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
13
6
  import type { PubSubArgs } from './index.js'
14
- import type { PubSubBaseProtocol } from '@libp2p/pubsub'
15
- import { Components } from '@libp2p/interfaces/components'
16
- import type { PeerId } from '@libp2p/interfaces/peer-id'
7
+ import type { Components } from '@libp2p/interfaces/components'
17
8
  import { start, stop } from '../index.js'
9
+ import { pEvent } from 'p-event'
10
+ import { createComponents } from './utils.js'
18
11
 
19
12
  const topic = 'foo'
20
13
  const data = uint8ArrayFromString('bar')
21
14
 
22
- export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
15
+ export default (common: TestSetup<PubSub, PubSubArgs>) => {
23
16
  describe('messages', () => {
24
- let peerId: PeerId
25
- let pubsub: PubSubBaseProtocol
17
+ let pubsub: PubSub
18
+ let components: Components
26
19
 
27
20
  // Create pubsub router
28
21
  beforeEach(async () => {
29
- peerId = await createEd25519PeerId()
22
+ components = await createComponents()
30
23
 
31
24
  pubsub = await common.setup({
32
- components: new Components({
33
- peerId,
34
- registrar: mockRegistrar()
35
- }),
25
+ components,
36
26
  init: {
37
27
  emitSelf: true
38
28
  }
@@ -47,91 +37,19 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
47
37
  })
48
38
 
49
39
  it('should emit normalized signed messages on publish', async () => {
50
- pubsub.globalSignaturePolicy = 'StrictSign'
51
-
52
- const spy = sinon.spy(pubsub, 'publishMessage')
53
-
54
- await pubsub.publish(topic, data)
55
-
56
- await pWaitFor(async () => {
57
- return spy.callCount === 1
58
- })
59
-
60
- expect(spy).to.have.property('callCount', 1)
61
-
62
- const [from, messageToEmit] = spy.getCall(0).args
63
-
64
- expect(from.toString()).to.equal(peerId.toString())
65
- expect(messageToEmit.sequenceNumber).to.not.eql(undefined)
66
- expect(messageToEmit.key).to.not.eql(undefined)
67
- expect(messageToEmit.signature).to.not.eql(undefined)
68
- })
69
-
70
- it('should drop unsigned messages', async () => {
71
- const publishSpy = sinon.spy(pubsub, 'publishMessage')
72
- sinon.spy(pubsub, 'validate')
73
-
74
- const peerStream = new PeerStreams({
75
- id: await createEd25519PeerId(),
76
- protocol: 'test'
77
- })
78
- const rpc: PubSubRPC = {
79
- subscriptions: [],
80
- messages: [{
81
- from: peerStream.id.toBytes(),
82
- data,
83
- sequenceNumber: await noSignMsgId(data),
84
- topic: topic
85
- }]
86
- }
87
-
88
- pubsub.subscribe(topic)
89
-
90
- await pubsub.processRpc(peerStream.id, peerStream, rpc)
91
-
92
- // message should not be delivered
93
- await delay(1000)
94
-
95
- expect(publishSpy).to.have.property('called', false)
96
- })
97
-
98
- it('should not drop unsigned messages if strict signing is disabled', async () => {
99
- pubsub.globalSignaturePolicy = 'StrictNoSign'
100
-
101
- const publishSpy = sinon.spy(pubsub, 'publishMessage')
102
- sinon.spy(pubsub, 'validate')
103
-
104
- const peerStream = new PeerStreams({
105
- id: await createEd25519PeerId(),
106
- protocol: 'test'
107
- })
108
-
109
- const rpc: PubSubRPC = {
110
- subscriptions: [],
111
- messages: [{
112
- from: peerStream.id.toBytes(),
113
- data,
114
- topic
115
- }]
116
- }
40
+ const eventPromise = pEvent<'message', CustomEvent<Message>>(pubsub, 'message')
117
41
 
42
+ pubsub.globalSignaturePolicy = 'StrictSign'
118
43
  pubsub.subscribe(topic)
44
+ await pubsub.publish(topic, data)
119
45
 
120
- const deferred = pDefer()
121
-
122
- pubsub.addEventListener('message', (evt) => {
123
- if (evt.detail.topic === topic) {
124
- deferred.resolve()
125
- }
126
- })
127
-
128
- await pubsub.processRpc(peerStream.id, peerStream, rpc)
129
-
130
- // await message delivery
131
- await deferred.promise
46
+ const event = await eventPromise
47
+ const message = event.detail
132
48
 
133
- expect(pubsub.validate).to.have.property('callCount', 1)
134
- expect(publishSpy).to.have.property('callCount', 1)
49
+ expect(message.from.toString()).to.equal(components.getPeerId().toString())
50
+ expect(message.sequenceNumber).to.not.eql(undefined)
51
+ expect(message.key).to.not.eql(undefined)
52
+ expect(message.signature).to.not.eql(undefined)
135
53
  })
136
54
  })
137
55
  }