@libp2p/interface-compliance-tests 1.1.22 → 1.1.25

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 +18 -21
  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 +84 -162
  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 +18 -12
  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 -74
  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 +87 -167
  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 +51 -84
  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 +12 -2
  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 +20 -24
  49. package/src/pubsub/connection-handlers.ts +97 -180
  50. package/src/pubsub/emit-self.ts +23 -15
  51. package/src/pubsub/index.ts +2 -3
  52. package/src/pubsub/messages.ts +18 -99
  53. package/src/pubsub/multiple-nodes.ts +106 -184
  54. package/src/pubsub/two-nodes.ts +61 -98
  55. package/src/pubsub/utils.ts +16 -5
@@ -4,48 +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'
9
- import { CustomEvent } from '@libp2p/interfaces'
7
+ import { connectPeers } from '../mocks/registrar.js'
10
8
  import type { TestSetup } from '../index.js'
11
- import type { Message } from '@libp2p/interfaces/pubsub'
9
+ import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
12
10
  import type { PubSubArgs } from './index.js'
13
- import type { PeerId } from '@libp2p/interfaces/peer-id'
14
- import type { Registrar } from '@libp2p/interfaces/registrar'
15
- import type { PubSubBaseProtocol } from '@libp2p/pubsub'
16
- import { Components } from '@libp2p/interfaces/components'
11
+ import type { Components } from '@libp2p/interfaces/components'
17
12
  import { start, stop } from '../index.js'
13
+ import { createComponents } from './utils.js'
14
+ import { pEvent } from 'p-event'
18
15
 
19
- export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
16
+ export default (common: TestSetup<PubSub, PubSubArgs>) => {
20
17
  describe('pubsub connection handlers', () => {
21
- let psA: PubSubBaseProtocol
22
- let psB: PubSubBaseProtocol
23
- let peerA: PeerId
24
- let peerB: PeerId
25
- let registrarA: Registrar
26
- let registrarB: Registrar
18
+ let psA: PubSub
19
+ let psB: PubSub
20
+ let componentsA: Components
21
+ let componentsB: Components
27
22
 
28
23
  describe('nodes send state on connection', () => {
29
24
  // Create pubsub nodes and connect them
30
25
  before(async () => {
31
- peerA = await createEd25519PeerId()
32
- peerB = await createEd25519PeerId()
33
-
34
- registrarA = mockRegistrar()
35
- registrarB = mockRegistrar()
26
+ componentsA = await createComponents()
27
+ componentsB = await createComponents()
36
28
 
37
29
  psA = await common.setup({
38
- components: new Components({
39
- peerId: peerA,
40
- registrar: registrarA
41
- }),
30
+ components: componentsA,
42
31
  init: {}
43
32
  })
44
33
  psB = await common.setup({
45
- components: new Components({
46
- peerId: peerB,
47
- registrar: registrarB
48
- }),
34
+ components: componentsB,
49
35
  init: {}
50
36
  })
51
37
 
@@ -74,21 +60,11 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
74
60
 
75
61
  it('existing subscriptions are sent upon peer connection', async function () {
76
62
  const subscriptionsChanged = Promise.all([
77
- new Promise((resolve) => psA.addEventListener('pubsub:subscription-change', resolve, {
78
- once: true
79
- })),
80
- new Promise((resolve) => psB.addEventListener('pubsub:subscription-change', resolve, {
81
- once: true
82
- }))
63
+ pEvent(psA, 'subscription-change'),
64
+ pEvent(psB, 'subscription-change')
83
65
  ])
84
66
 
85
- await connectPeers(psA.multicodecs[0], {
86
- peerId: peerA,
87
- registrar: registrarA
88
- }, {
89
- peerId: peerB,
90
- registrar: registrarB
91
- })
67
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
92
68
 
93
69
  await subscriptionsChanged
94
70
 
@@ -98,39 +74,28 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
98
74
  expect(psA.getTopics()).to.deep.equal(['Za'])
99
75
  expect(psB.getTopics()).to.deep.equal(['Zb'])
100
76
 
101
- expect(psA.getSubscribers('Zb').map(p => p.toString())).to.deep.equal([peerB.toString()])
102
- expect(psB.getSubscribers('Za').map(p => p.toString())).to.deep.equal([peerA.toString()])
77
+ expect(psA.getSubscribers('Zb').map(p => p.toString())).to.deep.equal([componentsB.getPeerId().toString()])
78
+ expect(psB.getSubscribers('Za').map(p => p.toString())).to.deep.equal([componentsA.getPeerId().toString()])
103
79
  })
104
80
  })
105
81
 
106
82
  describe('pubsub started before connect', () => {
107
- let psA: PubSubBaseProtocol
108
- let psB: PubSubBaseProtocol
109
- let peerA: PeerId
110
- let peerB: PeerId
111
- let registrarA: Registrar
112
- let registrarB: Registrar
83
+ let psA: PubSub
84
+ let psB: PubSub
85
+ let componentsA: Components
86
+ let componentsB: Components
113
87
 
114
88
  // Create pubsub nodes and start them
115
89
  beforeEach(async () => {
116
- peerA = await createEd25519PeerId()
117
- peerB = await createEd25519PeerId()
118
-
119
- registrarA = mockRegistrar()
120
- registrarB = mockRegistrar()
90
+ componentsA = await createComponents()
91
+ componentsB = await createComponents()
121
92
 
122
93
  psA = await common.setup({
123
- components: new Components({
124
- peerId: peerA,
125
- registrar: registrarA
126
- }),
94
+ components: componentsA,
127
95
  init: {}
128
96
  })
129
97
  psB = await common.setup({
130
- components: new Components({
131
- peerId: peerB,
132
- registrar: registrarB
133
- }),
98
+ components: componentsB,
134
99
  init: {}
135
100
  })
136
101
 
@@ -144,13 +109,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
144
109
  })
145
110
 
146
111
  it('should get notified of connected peers on dial', async () => {
147
- await connectPeers(psA.multicodecs[0], {
148
- peerId: peerA,
149
- registrar: registrarA
150
- }, {
151
- peerId: peerB,
152
- registrar: registrarB
153
- })
112
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
154
113
 
155
114
  return await Promise.all([
156
115
  pWaitFor(() => psA.getPeers().length === 1),
@@ -163,21 +122,18 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
163
122
  const topic = 'test-topic'
164
123
  const data = uint8ArrayFromString('hey!')
165
124
 
166
- await connectPeers(psA.multicodecs[0], {
167
- peerId: peerA,
168
- registrar: registrarA
169
- }, {
170
- peerId: peerB,
171
- registrar: registrarB
172
- })
125
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
173
126
 
174
127
  let subscribedTopics = psA.getTopics()
175
128
  expect(subscribedTopics).to.not.include(topic)
176
129
 
177
- psA.addEventListener(topic, (evt) => {
178
- const msg = evt.detail
179
- expect(msg.data).to.equalBytes(data)
180
- defer.resolve()
130
+ psA.subscribe(topic)
131
+ psA.addEventListener('message', (evt) => {
132
+ if (evt.detail.topic === topic) {
133
+ const msg = evt.detail
134
+ expect(msg.data).to.equalBytes(data)
135
+ defer.resolve()
136
+ }
181
137
  })
182
138
  psA.subscribe(topic)
183
139
 
@@ -187,42 +143,31 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
187
143
  // wait for psB to know about psA subscription
188
144
  await pWaitFor(() => {
189
145
  const subscribedPeers = psB.getSubscribers(topic)
190
- return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
146
+ return subscribedPeers.map(p => p.toString()).includes(componentsA.getPeerId().toString()) // eslint-disable-line max-nested-callbacks
191
147
  })
192
- void psB.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: data }))
148
+ await psB.publish(topic, data)
193
149
 
194
150
  await defer.promise
195
151
  })
196
152
  })
197
153
 
198
154
  describe('pubsub started after connect', () => {
199
- let psA: PubSubBaseProtocol
200
- let psB: PubSubBaseProtocol
201
- let peerA: PeerId
202
- let peerB: PeerId
203
- let registrarA: Registrar
204
- let registrarB: Registrar
155
+ let psA: PubSub
156
+ let psB: PubSub
157
+ let componentsA: Components
158
+ let componentsB: Components
205
159
 
206
160
  // Create pubsub nodes
207
161
  beforeEach(async () => {
208
- peerA = await createEd25519PeerId()
209
- peerB = await createEd25519PeerId()
210
-
211
- registrarA = mockRegistrar()
212
- registrarB = mockRegistrar()
162
+ componentsA = await createComponents()
163
+ componentsB = await createComponents()
213
164
 
214
165
  psA = await common.setup({
215
- components: new Components({
216
- peerId: peerA,
217
- registrar: registrarA
218
- }),
166
+ components: componentsA,
219
167
  init: {}
220
168
  })
221
169
  psB = await common.setup({
222
- components: new Components({
223
- peerId: peerB,
224
- registrar: registrarB
225
- }),
170
+ components: componentsB,
226
171
  init: {}
227
172
  })
228
173
  })
@@ -238,13 +183,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
238
183
  it('should get notified of connected peers after starting', async () => {
239
184
  await start(psA, psB)
240
185
 
241
- await connectPeers(psA.multicodecs[0], {
242
- peerId: peerA,
243
- registrar: registrarA
244
- }, {
245
- peerId: peerB,
246
- registrar: registrarB
247
- })
186
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
248
187
 
249
188
  return await Promise.all([
250
189
  pWaitFor(() => psA.getPeers().length === 1),
@@ -259,13 +198,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
259
198
 
260
199
  await start(psA, psB)
261
200
 
262
- await connectPeers(psA.multicodecs[0], {
263
- peerId: peerA,
264
- registrar: registrarA
265
- }, {
266
- peerId: peerB,
267
- registrar: registrarB
268
- })
201
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
269
202
 
270
203
  await Promise.all([
271
204
  pWaitFor(() => psA.getPeers().length === 1),
@@ -275,10 +208,13 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
275
208
  let subscribedTopics = psA.getTopics()
276
209
  expect(subscribedTopics).to.not.include(topic)
277
210
 
278
- psA.addEventListener(topic, (evt) => {
279
- const msg = evt.detail
280
- expect(msg.data).to.equalBytes(data)
281
- defer.resolve()
211
+ psA.subscribe(topic)
212
+ psA.addEventListener('message', (evt) => {
213
+ if (evt.detail.topic === topic) {
214
+ const msg = evt.detail
215
+ expect(msg.data).to.equalBytes(data)
216
+ defer.resolve()
217
+ }
282
218
  })
283
219
  psA.subscribe(topic)
284
220
 
@@ -288,42 +224,31 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
288
224
  // wait for psB to know about psA subscription
289
225
  await pWaitFor(() => {
290
226
  const subscribedPeers = psB.getSubscribers(topic)
291
- return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
227
+ return subscribedPeers.map(p => p.toString()).includes(componentsA.getPeerId().toString()) // eslint-disable-line max-nested-callbacks
292
228
  })
293
- void psB.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: data }))
229
+ await psB.publish(topic, data)
294
230
 
295
231
  await defer.promise
296
232
  })
297
233
  })
298
234
 
299
235
  describe('pubsub with intermittent connections', () => {
300
- let psA: PubSubBaseProtocol
301
- let psB: PubSubBaseProtocol
302
- let peerA: PeerId
303
- let peerB: PeerId
304
- let registrarA: Registrar
305
- let registrarB: Registrar
236
+ let psA: PubSub
237
+ let psB: PubSub
238
+ let componentsA: Components
239
+ let componentsB: Components
306
240
 
307
241
  // Create pubsub nodes and start them
308
242
  beforeEach(async () => {
309
- peerA = await createEd25519PeerId()
310
- peerB = await createEd25519PeerId()
311
-
312
- registrarA = mockRegistrar()
313
- registrarB = mockRegistrar()
243
+ componentsA = await createComponents()
244
+ componentsB = await createComponents()
314
245
 
315
246
  psA = await common.setup({
316
- components: new Components({
317
- peerId: peerA,
318
- registrar: registrarA
319
- }),
247
+ components: componentsA,
320
248
  init: {}
321
249
  })
322
250
  psB = await common.setup({
323
- components: new Components({
324
- peerId: peerB,
325
- registrar: registrarB
326
- }),
251
+ components: componentsB,
327
252
  init: {}
328
253
  })
329
254
 
@@ -346,22 +271,19 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
346
271
  const defer1 = pDefer()
347
272
  const defer2 = pDefer()
348
273
 
349
- await connectPeers(psA.multicodecs[0], {
350
- peerId: peerA,
351
- registrar: registrarA
352
- }, {
353
- peerId: peerB,
354
- registrar: registrarB
355
- })
274
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
356
275
 
357
276
  let subscribedTopics = psA.getTopics()
358
277
  expect(subscribedTopics).to.not.include(topic)
359
278
 
360
- psA.addEventListener(topic, (evt) => {
361
- const msg = evt.detail
362
- expect(msg.data).to.equalBytes(data)
363
- counter++
364
- counter === 1 ? defer1.resolve() : defer2.resolve()
279
+ psA.subscribe(topic)
280
+ psA.addEventListener('message', (evt) => {
281
+ if (evt.detail.topic === topic) {
282
+ const msg = evt.detail
283
+ expect(msg.data).to.equalBytes(data)
284
+ counter++
285
+ counter === 1 ? defer1.resolve() : defer2.resolve()
286
+ }
365
287
  })
366
288
  psA.subscribe(topic)
367
289
 
@@ -371,9 +293,9 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
371
293
  // wait for psB to know about psA subscription
372
294
  await pWaitFor(() => {
373
295
  const subscribedPeers = psB.getSubscribers(topic)
374
- return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
296
+ return subscribedPeers.map(p => p.toString()).includes(componentsA.getPeerId().toString()) // eslint-disable-line max-nested-callbacks
375
297
  })
376
- void psB.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: data }))
298
+ await psB.publish(topic, data)
377
299
 
378
300
  await defer1.promise
379
301
 
@@ -392,21 +314,15 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
392
314
  await psB._libp2p.start()
393
315
  await psB.start()
394
316
 
395
- await connectPeers(psA.multicodecs[0], {
396
- peerId: peerA,
397
- registrar: registrarA
398
- }, {
399
- peerId: peerB,
400
- registrar: registrarB
401
- })
317
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
402
318
 
403
319
  // wait for remoteLibp2p to know about libp2p subscription
404
320
  await pWaitFor(() => {
405
321
  const subscribedPeers = psB.getSubscribers(topic)
406
- return subscribedPeers.toString().includes(peerA.toString())
322
+ return subscribedPeers.toString().includes(componentsA.getPeerId().toString())
407
323
  })
408
324
 
409
- void psB.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: data }))
325
+ await psB.publish(topic, data)
410
326
 
411
327
  await defer2.promise
412
328
  })
@@ -417,8 +333,13 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
417
333
  let aReceivedSecondMessageFromB = false
418
334
  let bReceivedFirstMessageFromA = false
419
335
  let bReceivedSecondMessageFromA = false
336
+ const topic = 'reconnect-channel'
420
337
 
421
338
  const handlerSpyA = (evt: CustomEvent<Message>) => {
339
+ if (evt.detail.topic !== topic) {
340
+ return
341
+ }
342
+
422
343
  const message = evt.detail
423
344
  const data = uint8ArrayToString(message.data)
424
345
 
@@ -431,6 +352,10 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
431
352
  }
432
353
  }
433
354
  const handlerSpyB = (evt: CustomEvent<Message>) => {
355
+ if (evt.detail.topic !== topic) {
356
+ return
357
+ }
358
+
434
359
  const message = evt.detail
435
360
  const data = uint8ArrayToString(message.data)
436
361
 
@@ -443,10 +368,8 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
443
368
  }
444
369
  }
445
370
 
446
- const topic = 'reconnect-channel'
447
-
448
- psA.addEventListener(topic, handlerSpyA)
449
- psB.addEventListener(topic, handlerSpyB)
371
+ psA.addEventListener('message', handlerSpyA)
372
+ psB.addEventListener('message', handlerSpyB)
450
373
  psA.subscribe(topic)
451
374
  psB.subscribe(topic)
452
375
 
@@ -455,23 +378,17 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
455
378
  const originalConnection = await psA._libp2p.dialer.connectToPeer(psB.peerId)
456
379
 
457
380
  // second connection
458
- await connectPeers(psA.multicodecs[0], {
459
- peerId: peerA,
460
- registrar: registrarA
461
- }, {
462
- peerId: peerB,
463
- registrar: registrarB
464
- })
381
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
465
382
 
466
383
  // Wait for subscriptions to occur
467
384
  await pWaitFor(() => {
468
- return psA.getSubscribers(topic).includes(peerB) &&
469
- psB.getSubscribers(topic).map(p => p.toString()).includes(peerA.toString())
385
+ return psA.getSubscribers(topic).map(p => p.toString()).includes(componentsB.getPeerId().toString()) &&
386
+ psB.getSubscribers(topic).map(p => p.toString()).includes(componentsA.getPeerId().toString())
470
387
  })
471
388
 
472
389
  // Verify messages go both ways
473
- void psA.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: uint8ArrayFromString('message-from-a-1') }))
474
- void psB.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: uint8ArrayFromString('message-from-b-1') }))
390
+ await psA.publish(topic, uint8ArrayFromString('message-from-a-1'))
391
+ await psB.publish(topic, uint8ArrayFromString('message-from-b-1'))
475
392
  await pWaitFor(() => {
476
393
  return aReceivedFirstMessageFromB && bReceivedFirstMessageFromA
477
394
  })
@@ -484,8 +401,8 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
484
401
  await pWaitFor(() => psAConnUpdateSpy.callCount === 1)
485
402
 
486
403
  // Verify messages go both ways after the disconnect
487
- void psA.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: uint8ArrayFromString('message-from-a-2') }))
488
- void psB.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: uint8ArrayFromString('message-from-b-2') }))
404
+ await psA.publish(topic, uint8ArrayFromString('message-from-a-2'))
405
+ await psB.publish(topic, uint8ArrayFromString('message-from-b-2'))
489
406
  await pWaitFor(() => {
490
407
  return aReceivedSecondMessageFromB && bReceivedSecondMessageFromA
491
408
  })
@@ -3,28 +3,28 @@ import sinon from 'sinon'
3
3
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
4
4
  import { createEd25519PeerId } from '@libp2p/peer-id-factory'
5
5
  import { mockRegistrar } from '../mocks/registrar.js'
6
- import { CustomEvent } from '@libp2p/interfaces'
7
6
  import type { TestSetup } from '../index.js'
8
7
  import type { PubSubArgs } from './index.js'
9
- import type { PubSubBaseProtocol } from '@libp2p/pubsub'
10
8
  import { Components } from '@libp2p/interfaces/components'
11
9
  import { start, stop } from '../index.js'
10
+ import type { PubSub } from '@libp2p/interfaces/pubsub'
11
+ import { createComponents } from './utils.js'
12
12
 
13
13
  const topic = 'foo'
14
14
  const data = uint8ArrayFromString('bar')
15
15
  const shouldNotHappen = () => expect.fail()
16
16
 
17
- export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
17
+ export default (common: TestSetup<PubSub, PubSubArgs>) => {
18
18
  describe('emit self', () => {
19
- let pubsub: PubSubBaseProtocol
19
+ let pubsub: PubSub
20
+ let components: Components
20
21
 
21
22
  describe('enabled', () => {
22
23
  before(async () => {
24
+ components = await createComponents()
25
+
23
26
  pubsub = await common.setup({
24
- components: new Components({
25
- peerId: await createEd25519PeerId(),
26
- registrar: mockRegistrar()
27
- }),
27
+ components,
28
28
  init: {
29
29
  emitSelf: true
30
30
  }
@@ -43,13 +43,21 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
43
43
  })
44
44
 
45
45
  it('should emit to self on publish', async () => {
46
- const promise = new Promise((resolve) => pubsub.addEventListener(topic, resolve, {
47
- once: true
48
- }))
46
+ const promise = new Promise<void>((resolve) => {
47
+ pubsub.addEventListener('message', (evt) => {
48
+ if (evt.detail.topic === topic) {
49
+ resolve()
50
+ }
51
+ }, {
52
+ once: true
53
+ })
54
+ })
55
+
56
+ const result = await pubsub.publish(topic, data)
49
57
 
50
- void pubsub.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: data }))
58
+ await promise
51
59
 
52
- return await promise
60
+ expect(result).to.have.property('recipients').with.lengthOf(1)
53
61
  })
54
62
  })
55
63
 
@@ -78,11 +86,11 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
78
86
  })
79
87
 
80
88
  it('should not emit to self on publish', async () => {
81
- pubsub.addEventListener(topic, () => shouldNotHappen, {
89
+ pubsub.addEventListener('message', shouldNotHappen, {
82
90
  once: true
83
91
  })
84
92
 
85
- void pubsub.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: data }))
93
+ await pubsub.publish(topic, data)
86
94
 
87
95
  // Wait 1 second to guarantee that self is not noticed
88
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)