@libp2p/interface-compliance-tests 1.1.7 → 1.1.8

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 (63) hide show
  1. package/dist/src/mocks/connection.d.ts +8 -2
  2. package/dist/src/mocks/connection.d.ts.map +1 -1
  3. package/dist/src/mocks/connection.js +47 -29
  4. package/dist/src/mocks/connection.js.map +1 -1
  5. package/dist/src/mocks/multiaddr-connection.d.ts +2 -1
  6. package/dist/src/mocks/multiaddr-connection.d.ts.map +1 -1
  7. package/dist/src/mocks/multiaddr-connection.js +2 -2
  8. package/dist/src/mocks/multiaddr-connection.js.map +1 -1
  9. package/dist/src/mocks/registrar.d.ts +7 -2
  10. package/dist/src/mocks/registrar.d.ts.map +1 -1
  11. package/dist/src/mocks/registrar.js +49 -5
  12. package/dist/src/mocks/registrar.js.map +1 -1
  13. package/dist/src/mocks/upgrader.d.ts.map +1 -1
  14. package/dist/src/mocks/upgrader.js +8 -2
  15. package/dist/src/mocks/upgrader.js.map +1 -1
  16. package/dist/src/pubsub/api.d.ts +2 -2
  17. package/dist/src/pubsub/api.d.ts.map +1 -1
  18. package/dist/src/pubsub/api.js +15 -7
  19. package/dist/src/pubsub/api.js.map +1 -1
  20. package/dist/src/pubsub/connection-handlers.d.ts +2 -2
  21. package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
  22. package/dist/src/pubsub/connection-handlers.js +53 -51
  23. package/dist/src/pubsub/connection-handlers.js.map +1 -1
  24. package/dist/src/pubsub/emit-self.d.ts +2 -2
  25. package/dist/src/pubsub/emit-self.d.ts.map +1 -1
  26. package/dist/src/pubsub/emit-self.js +12 -2
  27. package/dist/src/pubsub/emit-self.js.map +1 -1
  28. package/dist/src/pubsub/index.d.ts +3 -3
  29. package/dist/src/pubsub/index.d.ts.map +1 -1
  30. package/dist/src/pubsub/index.js.map +1 -1
  31. package/dist/src/pubsub/messages.d.ts +2 -2
  32. package/dist/src/pubsub/messages.d.ts.map +1 -1
  33. package/dist/src/pubsub/messages.js +36 -32
  34. package/dist/src/pubsub/messages.js.map +1 -1
  35. package/dist/src/pubsub/multiple-nodes.d.ts +2 -2
  36. package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
  37. package/dist/src/pubsub/multiple-nodes.js +105 -53
  38. package/dist/src/pubsub/multiple-nodes.js.map +1 -1
  39. package/dist/src/pubsub/two-nodes.d.ts +2 -2
  40. package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
  41. package/dist/src/pubsub/two-nodes.js +31 -17
  42. package/dist/src/pubsub/two-nodes.js.map +1 -1
  43. package/dist/src/stream-muxer/close-test.d.ts.map +1 -1
  44. package/dist/src/stream-muxer/close-test.js +5 -2
  45. package/dist/src/stream-muxer/close-test.js.map +1 -1
  46. package/package.json +7 -5
  47. package/src/mocks/connection.ts +65 -30
  48. package/src/mocks/multiaddr-connection.ts +3 -2
  49. package/src/mocks/registrar.ts +63 -7
  50. package/src/mocks/upgrader.ts +8 -2
  51. package/src/pubsub/api.ts +19 -9
  52. package/src/pubsub/connection-handlers.ts +61 -55
  53. package/src/pubsub/emit-self.ts +14 -4
  54. package/src/pubsub/index.ts +3 -3
  55. package/src/pubsub/messages.ts +50 -36
  56. package/src/pubsub/multiple-nodes.ts +120 -55
  57. package/src/pubsub/two-nodes.ts +37 -22
  58. package/src/stream-muxer/close-test.ts +5 -2
  59. package/dist/src/pubsub/utils.d.ts +0 -3
  60. package/dist/src/pubsub/utils.d.ts.map +0 -1
  61. package/dist/src/pubsub/utils.js +0 -11
  62. package/dist/src/pubsub/utils.js.map +0 -1
  63. package/src/pubsub/utils.ts +0 -13
@@ -1,23 +1,30 @@
1
1
  import { expect } from 'aegir/utils/chai.js'
2
2
  import sinon from 'sinon'
3
- import * as PeerIdFactory from '@libp2p/peer-id-factory'
3
+ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
4
4
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
5
- import * as utils from '@libp2p/pubsub/utils'
5
+ import { noSignMsgId } from '@libp2p/pubsub/utils'
6
6
  import { PeerStreams } from '@libp2p/pubsub/peer-streams'
7
7
  import type { TestSetup } from '../index.js'
8
- import type { PubSub } from '@libp2p/interfaces/pubsub'
8
+ import type { PubSub, PubSubOptions, RPC } from '@libp2p/interfaces/pubsub'
9
9
  import type { EventMap } from './index.js'
10
+ import { mockRegistrar } from '../mocks/registrar.js'
11
+ import pDefer from 'p-defer'
12
+ import delay from 'delay'
10
13
 
11
14
  const topic = 'foo'
12
15
  const data = uint8ArrayFromString('bar')
13
16
 
14
- export default (common: TestSetup<PubSub<EventMap>>) => {
17
+ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
15
18
  describe('messages', () => {
16
19
  let pubsub: PubSub<EventMap>
17
20
 
18
21
  // Create pubsub router
19
22
  beforeEach(async () => {
20
- pubsub = await common.setup()
23
+ pubsub = await common.setup({
24
+ peerId: await createEd25519PeerId(),
25
+ registrar: mockRegistrar(),
26
+ emitSelf: true
27
+ })
21
28
  await pubsub.start()
22
29
  })
23
30
 
@@ -29,14 +36,14 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
29
36
 
30
37
  it('should emit normalized signed messages on publish', async () => {
31
38
  pubsub.globalSignaturePolicy = 'StrictSign'
32
- // @ts-expect-error protected field
33
- sinon.spy(pubsub, '_emitMessage')
39
+
40
+ const spy = sinon.spy(pubsub, 'emitMessage')
34
41
 
35
42
  await pubsub.publish(topic, data)
36
- // @ts-expect-error protected field
37
- expect(pubsub._emitMessage.callCount).to.eql(1)
38
- // @ts-expect-error protected field
39
- const [messageToEmit] = pubsub._emitMessage.getCall(0).args
43
+
44
+ expect(spy).to.have.property('callCount', 1)
45
+
46
+ const [messageToEmit] = spy.getCall(0).args
40
47
 
41
48
  expect(messageToEmit.seqno).to.not.eql(undefined)
42
49
  expect(messageToEmit.key).to.not.eql(undefined)
@@ -44,68 +51,75 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
44
51
  })
45
52
 
46
53
  it('should drop unsigned messages', async () => {
47
- // @ts-expect-error protected field
48
- sinon.spy(pubsub, '_emitMessage')
49
- // @ts-expect-error protected field
50
- sinon.spy(pubsub, '_publish')
54
+ const emitMessageSpy = sinon.spy(pubsub, 'emitMessage')
55
+ // @ts-expect-error protected abstract field
56
+ const publishSpy = sinon.spy(pubsub, '_publish')
51
57
  sinon.spy(pubsub, 'validate')
52
58
 
53
59
  const peerStream = new PeerStreams({
54
- id: await PeerIdFactory.createEd25519PeerId(),
60
+ id: await createEd25519PeerId(),
55
61
  protocol: 'test'
56
62
  })
57
- const rpc = {
63
+ const rpc: RPC = {
58
64
  subscriptions: [],
59
65
  msgs: [{
60
- receivedFrom: peerStream.id.toString(),
61
66
  from: peerStream.id.toBytes(),
62
67
  data,
63
- seqno: utils.randomSeqno(),
68
+ seqno: await noSignMsgId(data),
64
69
  topicIDs: [topic]
65
70
  }]
66
71
  }
67
72
 
68
73
  pubsub.subscribe(topic)
69
- // @ts-expect-error protected field
70
- await pubsub._processRpc(peerStream.id.toString(), peerStream, rpc)
74
+
75
+ await pubsub.processRpc(peerStream.id, peerStream, rpc)
76
+
77
+ // message should not be delivered
78
+ await delay(1000)
71
79
 
72
80
  expect(pubsub.validate).to.have.property('callCount', 1)
73
- // @ts-expect-error protected field
74
- expect(pubsub._emitMessage).to.have.property('called', false)
75
- // @ts-expect-error protected field
76
- expect(pubsub._publish).to.have.property('called', false)
81
+ expect(emitMessageSpy).to.have.property('called', false)
82
+ expect(publishSpy).to.have.property('called', false)
77
83
  })
78
84
 
79
85
  it('should not drop unsigned messages if strict signing is disabled', async () => {
80
86
  pubsub.globalSignaturePolicy = 'StrictNoSign'
87
+
88
+ const emitMessageSpy = sinon.spy(pubsub, 'emitMessage')
81
89
  // @ts-expect-error protected field
82
- sinon.spy(pubsub, '_emitMessage')
83
- // @ts-expect-error protected field
84
- sinon.spy(pubsub, '_publish')
90
+ const publishSpy = sinon.spy(pubsub, '_publish')
85
91
  sinon.spy(pubsub, 'validate')
86
92
 
87
93
  const peerStream = new PeerStreams({
88
- id: await PeerIdFactory.createEd25519PeerId(),
94
+ id: await createEd25519PeerId(),
89
95
  protocol: 'test'
90
96
  })
91
97
 
92
- const rpc = {
98
+ const rpc: RPC = {
93
99
  subscriptions: [],
94
100
  msgs: [{
101
+ from: peerStream.id.toBytes(),
95
102
  data,
96
103
  topicIDs: [topic]
97
104
  }]
98
105
  }
99
106
 
100
107
  pubsub.subscribe(topic)
101
- // @ts-expect-error protected field
102
- await pubsub._processRpc(peerStream.id.toString(), peerStream, rpc)
108
+
109
+ const deferred = pDefer()
110
+
111
+ await pubsub.processRpc(peerStream.id, peerStream, rpc)
112
+
113
+ pubsub.addEventListener(topic, () => {
114
+ deferred.resolve()
115
+ })
116
+
117
+ // await message delivery
118
+ await deferred.promise
103
119
 
104
120
  expect(pubsub.validate).to.have.property('callCount', 1)
105
- // @ts-expect-error protected field
106
- expect(pubsub._emitMessage).to.have.property('called', 1)
107
- // @ts-expect-error protected field
108
- expect(pubsub._publish).to.have.property('called', 1)
121
+ expect(emitMessageSpy).to.have.property('callCount', 1)
122
+ expect(publishSpy).to.have.property('callCount', 1)
109
123
  })
110
124
  })
111
125
  }
@@ -6,12 +6,15 @@ import pDefer from 'p-defer'
6
6
  import pWaitFor from 'p-wait-for'
7
7
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
8
8
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
9
- import { expectSet } from './utils.js'
10
9
  import type { TestSetup } from '../index.js'
11
- import type { PubSub, Message } from '@libp2p/interfaces/pubsub'
10
+ import type { PubSub, Message, PubSubOptions } from '@libp2p/interfaces/pubsub'
12
11
  import type { EventMap } from './index.js'
12
+ import type { PeerId } from '@libp2p/interfaces/src/peer-id'
13
+ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
14
+ import type { Registrar } from '@libp2p/interfaces/src/registrar'
15
+ import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
13
16
 
14
- export default (common: TestSetup<PubSub<EventMap>>) => {
17
+ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
15
18
  describe('pubsub with multiple nodes', function () {
16
19
  describe('every peer subscribes to the topic', () => {
17
20
  describe('line', () => {
@@ -21,36 +24,62 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
21
24
  let psA: PubSub<EventMap>
22
25
  let psB: PubSub<EventMap>
23
26
  let psC: PubSub<EventMap>
27
+ let peerIdA: PeerId
28
+ let peerIdB: PeerId
29
+ let peerIdC: PeerId
30
+ let registrarA: Registrar
31
+ let registrarB: Registrar
32
+ let registrarC: Registrar
24
33
 
25
34
  // Create and start pubsub nodes
26
35
  beforeEach(async () => {
27
- psA = await common.setup()
28
- psB = await common.setup()
29
- psC = await common.setup()
36
+ peerIdA = await createEd25519PeerId()
37
+ peerIdB = await createEd25519PeerId()
38
+ peerIdC = await createEd25519PeerId()
39
+
40
+ registrarA = mockRegistrar()
41
+ registrarB = mockRegistrar()
42
+ registrarC = mockRegistrar()
43
+
44
+ psA = await common.setup({
45
+ peerId: peerIdA,
46
+ registrar: registrarA
47
+ })
48
+ psB = await common.setup({
49
+ peerId: peerIdB,
50
+ registrar: registrarB
51
+ })
52
+ psC = await common.setup({
53
+ peerId: peerIdC,
54
+ registrar: registrarC
55
+ })
30
56
 
31
57
  // Start pubsub modes
32
- ;[psA, psB, psC].map((p) => p.start())
58
+ await Promise.all(
59
+ [psA, psB, psC].map((p) => p.start())
60
+ )
33
61
  })
34
62
 
35
63
  // Connect nodes
36
64
  beforeEach(async () => {
37
- // @ts-expect-error protected field
38
- await psA._libp2p.dial(psB.peerId)
39
- // @ts-expect-error protected field
40
- await psB._libp2p.dial(psC.peerId)
65
+ await connectPeers(psA.multicodecs[0], registrarA, registrarB, peerIdA, peerIdB)
66
+ await connectPeers(psB.multicodecs[0], registrarB, registrarC, peerIdB, peerIdC)
41
67
 
42
68
  // Wait for peers to be ready in pubsub
43
69
  await pWaitFor(() =>
44
- psA.peers.size === 1 &&
45
- psC.peers.size === 1 &&
46
- psA.peers.size === 1
70
+ psA.getPeers().length === 1 &&
71
+ psC.getPeers().length === 1 &&
72
+ psA.getPeers().length === 1
47
73
  )
48
74
  })
49
75
 
50
76
  afterEach(async () => {
51
77
  sinon.restore()
52
78
 
53
- ;[psA, psB, psC].map((p) => p.stop())
79
+ await Promise.all(
80
+ [psA, psB, psC].map((p) => p.stop())
81
+ )
82
+
54
83
  await common.teardown()
55
84
  })
56
85
 
@@ -58,24 +87,23 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
58
87
  const topic = 'Z'
59
88
 
60
89
  psA.subscribe(topic)
61
- expectSet(psA.subscriptions, [topic])
90
+ expect(psA.getTopics()).to.deep.equal([topic])
62
91
 
63
92
  await new Promise((resolve) => psB.addEventListener('pubsub:subscription-change', resolve, {
64
93
  once: true
65
94
  }))
66
- expect(psB.peers.size).to.equal(2)
95
+ expect(psB.getPeers().length).to.equal(2)
67
96
 
68
- const aPeerId = psA.peerId.toString()
69
- expectSet(psB.topics.get(topic), [aPeerId])
97
+ expect(psB.getSubscribers(topic)).to.deep.equal([peerIdA])
70
98
 
71
- expect(psC.peers.size).to.equal(1)
72
- expect(psC.topics.get(topic)).to.eql(undefined)
99
+ expect(psC.getPeers().length).to.equal(1)
100
+ expect(psC.getSubscribers(topic)).to.be.empty()
73
101
  })
74
102
 
75
103
  it('subscribe to the topic on node b', async () => {
76
104
  const topic = 'Z'
77
105
  psB.subscribe(topic)
78
- expectSet(psB.subscriptions, [topic])
106
+ expect(psB.getTopics()).to.deep.equal([topic])
79
107
 
80
108
  await Promise.all([
81
109
  new Promise((resolve) => psA.addEventListener('pubsub:subscription-change', resolve, {
@@ -86,11 +114,11 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
86
114
  }))
87
115
  ])
88
116
 
89
- expect(psA.peers.size).to.equal(1)
90
- expectSet(psA.topics.get(topic), [psB.peerId.toString()])
117
+ expect(psA.getPeers().length).to.equal(1)
118
+ expect(psA.getSubscribers(topic)).to.deep.equal([peerIdB])
91
119
 
92
- expect(psC.peers.size).to.equal(1)
93
- expectSet(psC.topics.get(topic), [psB.peerId.toString()])
120
+ expect(psC.getPeers().length).to.equal(1)
121
+ expect(psC.getSubscribers(topic)).to.deep.equal([peerIdB])
94
122
  })
95
123
 
96
124
  it('subscribe to the topic on node c', async () => {
@@ -98,12 +126,12 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
98
126
  const defer = pDefer()
99
127
 
100
128
  psC.subscribe(topic)
101
- expectSet(psC.subscriptions, [topic])
129
+ expect(psC.getTopics()).to.deep.equal([topic])
102
130
 
103
131
  psB.addEventListener('pubsub:subscription-change', () => {
104
- expect(psA.peers.size).to.equal(1)
105
- expect(psB.peers.size).to.equal(2)
106
- expectSet(psB.topics.get(topic), [psC.peerId.toString()])
132
+ expect(psA.getPeers().length).to.equal(1)
133
+ expect(psB.getPeers().length).to.equal(2)
134
+ expect(psB.getSubscribers(topic)).to.deep.equal([peerIdC])
107
135
 
108
136
  defer.resolve()
109
137
  }, {
@@ -236,56 +264,93 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
236
264
  let psC: PubSub<EventMap>
237
265
  let psD: PubSub<EventMap>
238
266
  let psE: PubSub<EventMap>
267
+ let peerIdA: PeerId
268
+ let peerIdB: PeerId
269
+ let peerIdC: PeerId
270
+ let peerIdD: PeerId
271
+ let peerIdE: PeerId
272
+ let registrarA: Registrar
273
+ let registrarB: Registrar
274
+ let registrarC: Registrar
275
+ let registrarD: Registrar
276
+ let registrarE: Registrar
239
277
 
240
278
  // Create and start pubsub nodes
241
279
  beforeEach(async () => {
242
- psA = await common.setup()
243
- psB = await common.setup()
244
- psC = await common.setup()
245
- psD = await common.setup()
246
- psE = await common.setup()
280
+ peerIdA = await createEd25519PeerId()
281
+ peerIdB = await createEd25519PeerId()
282
+ peerIdC = await createEd25519PeerId()
283
+ peerIdD = await createEd25519PeerId()
284
+ peerIdE = await createEd25519PeerId()
285
+
286
+ registrarA = mockRegistrar()
287
+ registrarB = mockRegistrar()
288
+ registrarC = mockRegistrar()
289
+ registrarD = mockRegistrar()
290
+ registrarE = mockRegistrar()
291
+
292
+ psA = await common.setup({
293
+ peerId: peerIdA,
294
+ registrar: registrarA
295
+ })
296
+ psB = await common.setup({
297
+ peerId: peerIdB,
298
+ registrar: registrarB
299
+ })
300
+ psC = await common.setup({
301
+ peerId: peerIdC,
302
+ registrar: registrarC
303
+ })
304
+ psD = await common.setup({
305
+ peerId: peerIdD,
306
+ registrar: registrarD
307
+ })
308
+ psE = await common.setup({
309
+ peerId: peerIdE,
310
+ registrar: registrarE
311
+ })
247
312
 
248
313
  // Start pubsub nodes
249
- ;[psA, psB, psC, psD, psE].map((p) => p.start())
314
+ await Promise.all(
315
+ [psA, psB, psC, psD, psE].map((p) => p.start())
316
+ )
250
317
  })
251
318
 
252
319
  // connect nodes
253
320
  beforeEach(async () => {
254
- // @ts-expect-error protected field
255
- await psA._libp2p.dial(psB.peerId)
256
- // @ts-expect-error protected field
257
- await psB._libp2p.dial(psC.peerId)
258
- // @ts-expect-error protected field
259
- await psC._libp2p.dial(psD.peerId)
260
- // @ts-expect-error protected field
261
- await psD._libp2p.dial(psE.peerId)
321
+ await connectPeers(psA.multicodecs[0], registrarA, registrarB, peerIdA, peerIdB)
322
+ await connectPeers(psA.multicodecs[0], registrarB, registrarC, peerIdB, peerIdC)
323
+ await connectPeers(psA.multicodecs[0], registrarC, registrarD, peerIdC, peerIdD)
324
+ await connectPeers(psA.multicodecs[0], registrarD, registrarE, peerIdD, peerIdE)
262
325
 
263
326
  // Wait for peers to be ready in pubsub
264
327
  await pWaitFor(() =>
265
- psA.peers.size === 1 &&
266
- psB.peers.size === 2 &&
267
- psC.peers.size === 2 &&
268
- psD.peers.size === 2 &&
269
- psE.peers.size === 1
328
+ psA.getPeers().length === 1 &&
329
+ psB.getPeers().length === 2 &&
330
+ psC.getPeers().length === 2 &&
331
+ psD.getPeers().length === 2 &&
332
+ psE.getPeers().length === 1
270
333
  )
271
334
  })
272
335
 
273
336
  afterEach(async () => {
274
- [psA, psB, psC, psD, psE].map((p) => p.stop())
337
+ await Promise.all(
338
+ [psA, psB, psC, psD, psE].map((p) => p.stop())
339
+ )
275
340
  await common.teardown()
276
341
  })
277
342
 
278
343
  it('subscribes', () => {
279
344
  psA.subscribe('Z')
280
- expectSet(psA.subscriptions, ['Z'])
345
+ expect(psA.getTopics()).to.deep.equal(['Z'])
281
346
  psB.subscribe('Z')
282
- expectSet(psB.subscriptions, ['Z'])
347
+ expect(psB.getTopics()).to.deep.equal(['Z'])
283
348
  psC.subscribe('Z')
284
- expectSet(psC.subscriptions, ['Z'])
349
+ expect(psC.getTopics()).to.deep.equal(['Z'])
285
350
  psD.subscribe('Z')
286
- expectSet(psD.subscriptions, ['Z'])
351
+ expect(psD.getTopics()).to.deep.equal(['Z'])
287
352
  psE.subscribe('Z')
288
- expectSet(psE.subscriptions, ['Z'])
353
+ expect(psE.getTopics()).to.deep.equal(['Z'])
289
354
  })
290
355
 
291
356
  it('publishes from c', async function () {
@@ -6,12 +6,12 @@ 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
8
  import type { TestSetup } from '../index.js'
9
- import type { PubSub, Message } from '@libp2p/interfaces/pubsub'
10
- import {
11
- first,
12
- expectSet
13
- } from './utils.js'
9
+ import type { PubSub, Message, PubSubOptions } from '@libp2p/interfaces/pubsub'
14
10
  import type { EventMap } from './index.js'
11
+ import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
12
+ import type { PeerId } from '@libp2p/interfaces/src/peer-id'
13
+ import type { Registrar } from '@libp2p/interfaces/src/registrar'
14
+ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
15
15
 
16
16
  const topic = 'foo'
17
17
 
@@ -19,28 +19,43 @@ function shouldNotHappen () {
19
19
  expect.fail()
20
20
  }
21
21
 
22
- export default (common: TestSetup<PubSub<EventMap>>) => {
22
+ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
23
23
  describe('pubsub with two nodes', () => {
24
24
  let psA: PubSub<EventMap>
25
25
  let psB: PubSub<EventMap>
26
+ let peerIdA: PeerId
27
+ let peerIdB: PeerId
28
+ let registrarA: Registrar
29
+ let registrarB: Registrar
26
30
 
27
31
  // Create pubsub nodes and connect them
28
32
  before(async () => {
29
- psA = await common.setup()
30
- psB = await common.setup()
33
+ peerIdA = await createEd25519PeerId()
34
+ peerIdB = await createEd25519PeerId()
31
35
 
32
- expect(psA.peers.size).to.be.eql(0)
33
- expect(psB.peers.size).to.be.eql(0)
36
+ registrarA = mockRegistrar()
37
+ registrarB = mockRegistrar()
38
+
39
+ psA = await common.setup({
40
+ peerId: peerIdA,
41
+ registrar: registrarA
42
+ })
43
+ psB = await common.setup({
44
+ peerId: peerIdB,
45
+ registrar: registrarB
46
+ })
34
47
 
35
48
  // Start pubsub and connect nodes
36
49
  await psA.start()
37
50
  await psB.start()
38
51
 
39
- // @ts-expect-error protected property
40
- await psA._libp2p.dial(psB.peerId)
52
+ expect(psA.getPeers()).to.be.empty()
53
+ expect(psB.getPeers()).to.be.empty()
54
+
55
+ await connectPeers(psA.multicodecs[0], registrarA, registrarB, peerIdA, peerIdB)
41
56
 
42
57
  // Wait for peers to be ready in pubsub
43
- await pWaitFor(() => psA.peers.size === 1 && psB.peers.size === 1)
58
+ await pWaitFor(() => psA.getPeers().length === 1 && psB.getPeers().length === 1)
44
59
  })
45
60
 
46
61
  after(async () => {
@@ -57,10 +72,10 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
57
72
 
58
73
  psB.addEventListener('pubsub:subscription-change', (evt) => {
59
74
  const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
60
- expectSet(psA.subscriptions, [topic])
61
- expect(psB.peers.size).to.equal(1)
62
- expectSet(psB.topics.get(topic), [psA.peerId.toString()])
63
- expect(changedPeerId.toString()).to.equal(first(psB.peers).id.toString())
75
+ expect(psA.getTopics()).to.deep.equal([topic])
76
+ expect(psB.getPeers()).to.have.lengthOf(1)
77
+ expect(psB.getSubscribers(topic)).to.deep.equal([peerIdA])
78
+ expect(changedPeerId).to.deep.equal(psB.getPeers()[0])
64
79
  expect(changedSubs).to.have.lengthOf(1)
65
80
  expect(changedSubs[0].topicID).to.equal(topic)
66
81
  expect(changedSubs[0].subscribe).to.equal(true)
@@ -135,7 +150,7 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
135
150
  function receivedMsg (evt: CustomEvent<Message>) {
136
151
  const msg = evt.detail
137
152
  expect(uint8ArrayToString(msg.data)).to.equal('banana')
138
- expect(msg.from).to.be.eql(psB.peerId.toString())
153
+ expect(msg.from).to.deep.equal(peerIdB)
139
154
  expect(msg.seqno).to.be.a('Uint8Array')
140
155
  expect(msg.topicIDs).to.be.eql([topic])
141
156
 
@@ -156,13 +171,13 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
156
171
  const defer = pDefer()
157
172
 
158
173
  psA.unsubscribe(topic)
159
- expect(psA.subscriptions.size).to.equal(0)
174
+ expect(psA.getTopics()).to.be.empty()
160
175
 
161
176
  psB.addEventListener('pubsub:subscription-change', (evt) => {
162
177
  const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
163
- expect(psB.peers.size).to.equal(1)
164
- expectSet(psB.topics.get(topic), [])
165
- expect(changedPeerId.toString()).to.equal(first(psB.peers).id.toString())
178
+ expect(psB.getPeers()).to.have.lengthOf(1)
179
+ expect(psB.getTopics()).to.be.empty()
180
+ expect(changedPeerId).to.deep.equal(psB.getPeers()[0])
166
181
  expect(changedSubs).to.have.lengthOf(1)
167
182
  expect(changedSubs[0].topicID).to.equal(topic)
168
183
  expect(changedSubs[0].subscribe).to.equal(false)
@@ -10,6 +10,7 @@ import { expect } from 'aegir/utils/chai.js'
10
10
  import delay from 'delay'
11
11
  import type { TestSetup } from '../index.js'
12
12
  import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer'
13
+ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
13
14
 
14
15
  function randomBuffer () {
15
16
  return uint8ArrayFromString(Math.random().toString())
@@ -27,6 +28,8 @@ const infiniteRandom = {
27
28
  export default (common: TestSetup<Muxer, MuxerOptions>) => {
28
29
  describe('close', () => {
29
30
  it('closing underlying socket closes streams', async () => {
31
+ const localPeer = await createEd25519PeerId()
32
+ const remotePeer = await createEd25519PeerId()
30
33
  const muxer = await common.setup({
31
34
  onStream: (stream) => {
32
35
  void pipe(stream, drain)
@@ -40,8 +43,8 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
40
43
  returnOnAbort: true
41
44
  })
42
45
 
43
- await upgrader.upgradeInbound(mockMultiaddrConnection(abortableRemote))
44
- const dialerConn = await upgrader.upgradeOutbound(mockMultiaddrConnection(local))
46
+ await upgrader.upgradeInbound(mockMultiaddrConnection(abortableRemote, localPeer))
47
+ const dialerConn = await upgrader.upgradeOutbound(mockMultiaddrConnection(local, remotePeer))
45
48
 
46
49
  const s1 = await dialerConn.newStream([''])
47
50
  const s2 = await dialerConn.newStream([''])
@@ -1,3 +0,0 @@
1
- export declare function first<V>(map: Map<any, V>): V;
2
- export declare function expectSet<T>(set?: Set<T>, subs?: T[]): void;
3
- //# sourceMappingURL=utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/pubsub/utils.ts"],"names":[],"mappings":"AAEA,wBAAgB,KAAK,CAAE,CAAC,EAAG,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,CAAC,CAE9C;AAED,wBAAgB,SAAS,CAAE,CAAC,EAAG,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,CAAC,EAAE,QAMtD"}
@@ -1,11 +0,0 @@
1
- import { expect } from 'aegir/utils/chai.js';
2
- export function first(map) {
3
- return map.values().next().value;
4
- }
5
- export function expectSet(set, subs) {
6
- if ((set == null) || (subs == null)) {
7
- throw new Error('No set or subs passed');
8
- }
9
- expect(Array.from(set.values())).to.eql(subs);
10
- }
11
- //# sourceMappingURL=utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/pubsub/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAE5C,MAAM,UAAU,KAAK,CAAM,GAAgB;IACzC,OAAO,GAAG,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAA;AAClC,CAAC;AAED,MAAM,UAAU,SAAS,CAAM,GAAY,EAAE,IAAU;IACrD,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,EAAE;QACnC,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAA;KACzC;IAED,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;AAC/C,CAAC"}
@@ -1,13 +0,0 @@
1
- import { expect } from 'aegir/utils/chai.js'
2
-
3
- export function first <V> (map: Map<any, V>): V {
4
- return map.values().next().value
5
- }
6
-
7
- export function expectSet <T> (set?: Set<T>, subs?: T[]) {
8
- if ((set == null) || (subs == null)) {
9
- throw new Error('No set or subs passed')
10
- }
11
-
12
- expect(Array.from(set.values())).to.eql(subs)
13
- }