@libp2p/interface-compliance-tests 1.1.6 → 1.1.10

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 (92) hide show
  1. package/dist/src/mocks/connection.d.ts +13 -2
  2. package/dist/src/mocks/connection.d.ts.map +1 -1
  3. package/dist/src/mocks/connection.js +56 -24
  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/muxer.d.ts +2 -2
  10. package/dist/src/mocks/muxer.d.ts.map +1 -1
  11. package/dist/src/mocks/muxer.js +175 -26
  12. package/dist/src/mocks/muxer.js.map +1 -1
  13. package/dist/src/mocks/registrar.d.ts +11 -2
  14. package/dist/src/mocks/registrar.d.ts.map +1 -1
  15. package/dist/src/mocks/registrar.js +47 -5
  16. package/dist/src/mocks/registrar.js.map +1 -1
  17. package/dist/src/mocks/upgrader.d.ts.map +1 -1
  18. package/dist/src/mocks/upgrader.js +8 -4
  19. package/dist/src/mocks/upgrader.js.map +1 -1
  20. package/dist/src/pubsub/api.d.ts +3 -2
  21. package/dist/src/pubsub/api.d.ts.map +1 -1
  22. package/dist/src/pubsub/api.js +23 -13
  23. package/dist/src/pubsub/api.js.map +1 -1
  24. package/dist/src/pubsub/connection-handlers.d.ts +3 -2
  25. package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
  26. package/dist/src/pubsub/connection-handlers.js +165 -69
  27. package/dist/src/pubsub/connection-handlers.js.map +1 -1
  28. package/dist/src/pubsub/emit-self.d.ts +3 -2
  29. package/dist/src/pubsub/emit-self.d.ts.map +1 -1
  30. package/dist/src/pubsub/emit-self.js +15 -4
  31. package/dist/src/pubsub/emit-self.js.map +1 -1
  32. package/dist/src/pubsub/index.d.ts +6 -3
  33. package/dist/src/pubsub/index.d.ts.map +1 -1
  34. package/dist/src/pubsub/index.js.map +1 -1
  35. package/dist/src/pubsub/messages.d.ts +3 -2
  36. package/dist/src/pubsub/messages.d.ts.map +1 -1
  37. package/dist/src/pubsub/messages.js +42 -39
  38. package/dist/src/pubsub/messages.js.map +1 -1
  39. package/dist/src/pubsub/multiple-nodes.d.ts +3 -2
  40. package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
  41. package/dist/src/pubsub/multiple-nodes.js +163 -65
  42. package/dist/src/pubsub/multiple-nodes.js.map +1 -1
  43. package/dist/src/pubsub/two-nodes.d.ts +3 -2
  44. package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
  45. package/dist/src/pubsub/two-nodes.js +80 -38
  46. package/dist/src/pubsub/two-nodes.js.map +1 -1
  47. package/dist/src/stream-muxer/base-test.js +1 -1
  48. package/dist/src/stream-muxer/base-test.js.map +1 -1
  49. package/dist/src/stream-muxer/close-test.d.ts.map +1 -1
  50. package/dist/src/stream-muxer/close-test.js +12 -10
  51. package/dist/src/stream-muxer/close-test.js.map +1 -1
  52. package/dist/src/transport/dial-test.d.ts.map +1 -1
  53. package/dist/src/transport/dial-test.js +2 -1
  54. package/dist/src/transport/dial-test.js.map +1 -1
  55. package/dist/src/transport/filter-test.js +1 -1
  56. package/dist/src/transport/filter-test.js.map +1 -1
  57. package/dist/src/transport/listen-test.d.ts.map +1 -1
  58. package/dist/src/transport/listen-test.js +2 -1
  59. package/dist/src/transport/listen-test.js.map +1 -1
  60. package/dist/src/utils/is-valid-tick.d.ts +6 -0
  61. package/dist/src/utils/is-valid-tick.d.ts.map +1 -0
  62. package/dist/src/utils/is-valid-tick.js +15 -0
  63. package/dist/src/utils/is-valid-tick.js.map +1 -0
  64. package/package.json +7 -5
  65. package/src/mocks/connection.ts +80 -24
  66. package/src/mocks/multiaddr-connection.ts +3 -2
  67. package/src/mocks/muxer.ts +230 -28
  68. package/src/mocks/registrar.ts +65 -7
  69. package/src/mocks/upgrader.ts +8 -5
  70. package/src/pubsub/api.ts +29 -15
  71. package/src/pubsub/connection-handlers.ts +186 -75
  72. package/src/pubsub/emit-self.ts +19 -7
  73. package/src/pubsub/index.ts +6 -3
  74. package/src/pubsub/messages.ts +59 -44
  75. package/src/pubsub/multiple-nodes.ts +189 -76
  76. package/src/pubsub/two-nodes.ts +94 -46
  77. package/src/stream-muxer/base-test.ts +1 -1
  78. package/src/stream-muxer/close-test.ts +13 -12
  79. package/src/transport/dial-test.ts +2 -1
  80. package/src/transport/filter-test.ts +1 -1
  81. package/src/transport/listen-test.ts +2 -1
  82. package/src/utils/is-valid-tick.ts +18 -0
  83. package/dist/src/pubsub/utils.d.ts +0 -3
  84. package/dist/src/pubsub/utils.d.ts.map +0 -1
  85. package/dist/src/pubsub/utils.js +0 -11
  86. package/dist/src/pubsub/utils.js.map +0 -1
  87. package/dist/src/transport/utils/index.d.ts +0 -15
  88. package/dist/src/transport/utils/index.d.ts.map +0 -1
  89. package/dist/src/transport/utils/index.js +0 -137
  90. package/dist/src/transport/utils/index.js.map +0 -1
  91. package/src/pubsub/utils.ts +0 -13
  92. package/src/transport/utils/index.ts +0 -172
package/src/pubsub/api.ts CHANGED
@@ -3,20 +3,33 @@ import sinon from 'sinon'
3
3
  import pDefer from 'p-defer'
4
4
  import pWaitFor from 'p-wait-for'
5
5
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
6
+ import { mockRegistrar } from '../mocks/registrar.js'
7
+ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
8
+ import delay from 'delay'
9
+ import { CustomEvent } from '@libp2p/interfaces'
6
10
  import type { TestSetup } from '../index.js'
7
- import type { PubSub } from '@libp2p/interfaces/pubsub'
11
+ import type { PubSub, PubSubOptions } from '@libp2p/interfaces/pubsub'
8
12
  import type { EventMap } from './index.js'
13
+ import type { Registrar } from '@libp2p/interfaces/src/registrar'
14
+ import type { PubsubBaseProtocol } from '@libp2p/pubsub'
9
15
 
10
16
  const topic = 'foo'
11
17
  const data = uint8ArrayFromString('bar')
12
18
 
13
- export default (common: TestSetup<PubSub<EventMap>>) => {
19
+ export default (common: TestSetup<PubsubBaseProtocol<EventMap>, PubSubOptions>) => {
14
20
  describe('pubsub api', () => {
15
21
  let pubsub: PubSub<EventMap>
22
+ let registrar: Registrar
16
23
 
17
24
  // Create pubsub router
18
25
  beforeEach(async () => {
19
- pubsub = await common.setup()
26
+ registrar = mockRegistrar()
27
+
28
+ pubsub = await common.setup({
29
+ peerId: await createEd25519PeerId(),
30
+ registrar,
31
+ emitSelf: true
32
+ })
20
33
  })
21
34
 
22
35
  afterEach(async () => {
@@ -26,22 +39,22 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
26
39
  })
27
40
 
28
41
  it('can start correctly', async () => {
29
- sinon.spy(pubsub.registrar, 'register')
42
+ sinon.spy(registrar, 'register')
30
43
 
31
44
  await pubsub.start()
32
45
 
33
- expect(pubsub.started).to.eql(true)
34
- expect(pubsub.registrar.register).to.have.property('callCount', 1)
46
+ expect(pubsub.isStarted()).to.equal(true)
47
+ expect(registrar.register).to.have.property('callCount', 1)
35
48
  })
36
49
 
37
50
  it('can stop correctly', async () => {
38
- sinon.spy(pubsub.registrar, 'unregister')
51
+ sinon.spy(registrar, 'unregister')
39
52
 
40
53
  await pubsub.start()
41
54
  await pubsub.stop()
42
55
 
43
- expect(pubsub.started).to.eql(false)
44
- expect(pubsub.registrar.unregister).to.have.property('callCount', 1)
56
+ expect(pubsub.isStarted()).to.equal(false)
57
+ expect(registrar.unregister).to.have.property('callCount', 1)
45
58
  })
46
59
 
47
60
  it('can subscribe and unsubscribe correctly', async () => {
@@ -50,20 +63,22 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
50
63
  }
51
64
 
52
65
  await pubsub.start()
53
- pubsub.subscribe(topic)
54
- pubsub.addEventListener('topic', handler)
66
+ pubsub.addEventListener(topic, handler)
55
67
 
56
68
  await pWaitFor(() => {
57
69
  const topics = pubsub.getTopics()
58
70
  return topics.length === 1 && topics[0] === topic
59
71
  })
60
72
 
61
- pubsub.unsubscribe(topic)
73
+ pubsub.removeEventListener(topic, handler)
62
74
 
63
75
  await pWaitFor(() => pubsub.getTopics().length === 0)
64
76
 
65
77
  // Publish to guarantee the handler is not called
66
- await pubsub.publish(topic, data)
78
+ pubsub.dispatchEvent(new CustomEvent(topic, { detail: data }))
79
+
80
+ // handlers are called async
81
+ await delay(100)
67
82
 
68
83
  await pubsub.stop()
69
84
  })
@@ -73,13 +88,12 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
73
88
 
74
89
  await pubsub.start()
75
90
 
76
- pubsub.subscribe(topic)
77
91
  pubsub.addEventListener(topic, (evt) => {
78
92
  const msg = evt.detail
79
93
  expect(msg).to.not.eql(undefined)
80
94
  defer.resolve()
81
95
  })
82
- await pubsub.publish(topic, data)
96
+ pubsub.dispatchEvent(new CustomEvent(topic, { detail: data }))
83
97
  await defer.promise
84
98
 
85
99
  await pubsub.stop()
@@ -4,28 +4,49 @@ 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 { expectSet } from './utils.js'
7
+ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
8
+ import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
9
+ import { CustomEvent } from '@libp2p/interfaces'
8
10
  import type { TestSetup } from '../index.js'
9
- import type { PubSub, Message } from '@libp2p/interfaces/pubsub'
11
+ import type { Message, PubSubOptions } from '@libp2p/interfaces/pubsub'
10
12
  import type { EventMap } from './index.js'
13
+ import type { PeerId } from '@libp2p/interfaces/src/peer-id'
14
+ import type { Registrar } from '@libp2p/interfaces/src/registrar'
15
+ import type { PubsubBaseProtocol } from '@libp2p/pubsub'
11
16
 
12
- export default (common: TestSetup<PubSub<EventMap>>) => {
17
+ export default (common: TestSetup<PubsubBaseProtocol<EventMap>, PubSubOptions>) => {
13
18
  describe('pubsub connection handlers', () => {
14
- let psA: PubSub<EventMap>
15
- let psB: PubSub<EventMap>
19
+ let psA: PubsubBaseProtocol<EventMap>
20
+ let psB: PubsubBaseProtocol<EventMap>
21
+ let peerA: PeerId
22
+ let peerB: PeerId
23
+ let registrarA: Registrar
24
+ let registrarB: Registrar
16
25
 
17
26
  describe('nodes send state on connection', () => {
18
27
  // Create pubsub nodes and connect them
19
28
  before(async () => {
20
- psA = await common.setup()
21
- psB = await common.setup()
29
+ peerA = await createEd25519PeerId()
30
+ peerB = await createEd25519PeerId()
22
31
 
23
- expect(psA.peers.size).to.be.eql(0)
24
- expect(psB.peers.size).to.be.eql(0)
32
+ registrarA = mockRegistrar()
33
+ registrarB = mockRegistrar()
34
+
35
+ psA = await common.setup({
36
+ peerId: peerA,
37
+ registrar: registrarA
38
+ })
39
+ psB = await common.setup({
40
+ peerId: peerB,
41
+ registrar: registrarB
42
+ })
25
43
 
26
44
  // Start pubsub
27
45
  await psA.start()
28
46
  await psB.start()
47
+
48
+ expect(psA.getPeers()).to.be.empty()
49
+ expect(psB.getPeers()).to.be.empty()
29
50
  })
30
51
 
31
52
  // Make subscriptions prior to nodes connected
@@ -33,10 +54,10 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
33
54
  psA.subscribe('Za')
34
55
  psB.subscribe('Zb')
35
56
 
36
- expect(psA.peers.size).to.equal(0)
37
- expectSet(psA.subscriptions, ['Za'])
38
- expect(psB.peers.size).to.equal(0)
39
- expectSet(psB.subscriptions, ['Zb'])
57
+ expect(psA.getPeers()).to.be.empty()
58
+ expect(psA.getTopics()).to.deep.equal(['Za'])
59
+ expect(psB.getPeers()).to.be.empty()
60
+ expect(psB.getTopics()).to.deep.equal(['Zb'])
40
61
  })
41
62
 
42
63
  after(async () => {
@@ -45,9 +66,7 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
45
66
  })
46
67
 
47
68
  it('existing subscriptions are sent upon peer connection', async function () {
48
- await Promise.all([
49
- // @ts-expect-error protected fields
50
- psA._libp2p.dial(psB.peerId),
69
+ const subscriptionsChanged = Promise.all([
51
70
  new Promise((resolve) => psA.addEventListener('pubsub:subscription-change', resolve, {
52
71
  once: true
53
72
  })),
@@ -56,24 +75,51 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
56
75
  }))
57
76
  ])
58
77
 
59
- expect(psA.peers.size).to.equal(1)
60
- expect(psB.peers.size).to.equal(1)
78
+ await connectPeers(psA.multicodecs[0], {
79
+ peerId: peerA,
80
+ registrar: registrarA
81
+ }, {
82
+ peerId: peerB,
83
+ registrar: registrarB
84
+ })
61
85
 
62
- expectSet(psA.subscriptions, ['Za'])
86
+ await subscriptionsChanged
63
87
 
64
- expectSet(psB.topics.get('Za'), [psA.peerId.toString()])
88
+ expect(psA.getPeers()).to.have.lengthOf(1)
89
+ expect(psB.getPeers()).to.have.lengthOf(1)
65
90
 
66
- expectSet(psB.subscriptions, ['Zb'])
91
+ expect(psA.getTopics()).to.deep.equal(['Za'])
92
+ expect(psB.getTopics()).to.deep.equal(['Zb'])
67
93
 
68
- expectSet(psA.topics.get('Zb'), [psB.peerId.toString()])
94
+ expect(psA.getSubscribers('Zb').map(p => p.toString())).to.deep.equal([peerB.toString()])
95
+ expect(psB.getSubscribers('Za').map(p => p.toString())).to.deep.equal([peerA.toString()])
69
96
  })
70
97
  })
71
98
 
72
99
  describe('pubsub started before connect', () => {
100
+ let psA: PubsubBaseProtocol<EventMap>
101
+ let psB: PubsubBaseProtocol<EventMap>
102
+ let peerA: PeerId
103
+ let peerB: PeerId
104
+ let registrarA: Registrar
105
+ let registrarB: Registrar
106
+
73
107
  // Create pubsub nodes and start them
74
108
  beforeEach(async () => {
75
- psA = await common.setup()
76
- psB = await common.setup()
109
+ peerA = await createEd25519PeerId()
110
+ peerB = await createEd25519PeerId()
111
+
112
+ registrarA = mockRegistrar()
113
+ registrarB = mockRegistrar()
114
+
115
+ psA = await common.setup({
116
+ peerId: peerA,
117
+ registrar: registrarA
118
+ })
119
+ psB = await common.setup({
120
+ peerId: peerB,
121
+ registrar: registrarB
122
+ })
77
123
 
78
124
  await psA.start()
79
125
  await psB.start()
@@ -86,13 +132,17 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
86
132
  })
87
133
 
88
134
  it('should get notified of connected peers on dial', async () => {
89
- // @ts-expect-error protected fields
90
- const connection = await psA._libp2p.dial(psB.peerId)
91
- expect(connection).to.exist()
135
+ await connectPeers(psA.multicodecs[0], {
136
+ peerId: peerA,
137
+ registrar: registrarA
138
+ }, {
139
+ peerId: peerB,
140
+ registrar: registrarB
141
+ })
92
142
 
93
143
  return await Promise.all([
94
- pWaitFor(() => psA.peers.size === 1),
95
- pWaitFor(() => psB.peers.size === 1)
144
+ pWaitFor(() => psA.getPeers().length === 1),
145
+ pWaitFor(() => psB.getPeers().length === 1)
96
146
  ])
97
147
  })
98
148
 
@@ -101,8 +151,13 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
101
151
  const topic = 'test-topic'
102
152
  const data = uint8ArrayFromString('hey!')
103
153
 
104
- // @ts-expect-error protected fields
105
- await psA._libp2p.dial(psB.peerId)
154
+ await connectPeers(psA.multicodecs[0], {
155
+ peerId: peerA,
156
+ registrar: registrarA
157
+ }, {
158
+ peerId: peerB,
159
+ registrar: registrarB
160
+ })
106
161
 
107
162
  let subscribedTopics = psA.getTopics()
108
163
  expect(subscribedTopics).to.not.include(topic)
@@ -120,19 +175,38 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
120
175
  // wait for psB to know about psA subscription
121
176
  await pWaitFor(() => {
122
177
  const subscribedPeers = psB.getSubscribers(topic)
123
- return subscribedPeers.includes(psA.peerId.toString())
178
+ return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
124
179
  })
125
- void psB.publish(topic, data)
180
+ void psB.dispatchEvent(new CustomEvent(topic, { detail: data }))
126
181
 
127
182
  await defer.promise
128
183
  })
129
184
  })
130
185
 
131
186
  describe('pubsub started after connect', () => {
187
+ let psA: PubsubBaseProtocol<EventMap>
188
+ let psB: PubsubBaseProtocol<EventMap>
189
+ let peerA: PeerId
190
+ let peerB: PeerId
191
+ let registrarA: Registrar
192
+ let registrarB: Registrar
193
+
132
194
  // Create pubsub nodes
133
195
  beforeEach(async () => {
134
- psA = await common.setup()
135
- psB = await common.setup()
196
+ peerA = await createEd25519PeerId()
197
+ peerB = await createEd25519PeerId()
198
+
199
+ registrarA = mockRegistrar()
200
+ registrarB = mockRegistrar()
201
+
202
+ psA = await common.setup({
203
+ peerId: peerA,
204
+ registrar: registrarA
205
+ })
206
+ psB = await common.setup({
207
+ peerId: peerB,
208
+ registrar: registrarB
209
+ })
136
210
  })
137
211
 
138
212
  afterEach(async () => {
@@ -145,18 +219,20 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
145
219
  })
146
220
 
147
221
  it('should get notified of connected peers after starting', async () => {
148
- // @ts-expect-error protected fields
149
- const connection = await psA._libp2p.dial(psB.peerId)
150
- expect(connection).to.exist()
151
- expect(psA.peers.size).to.be.eql(0)
152
- expect(psB.peers.size).to.be.eql(0)
153
-
154
222
  await psA.start()
155
223
  await psB.start()
156
224
 
225
+ await connectPeers(psA.multicodecs[0], {
226
+ peerId: peerA,
227
+ registrar: registrarA
228
+ }, {
229
+ peerId: peerB,
230
+ registrar: registrarB
231
+ })
232
+
157
233
  return await Promise.all([
158
- pWaitFor(() => psA.peers.size === 1),
159
- pWaitFor(() => psB.peers.size === 1)
234
+ pWaitFor(() => psA.getPeers().length === 1),
235
+ pWaitFor(() => psB.getPeers().length === 1)
160
236
  ])
161
237
  })
162
238
 
@@ -165,15 +241,20 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
165
241
  const topic = 'test-topic'
166
242
  const data = uint8ArrayFromString('hey!')
167
243
 
168
- // @ts-expect-error protected fields
169
- await psA._libp2p.dial(psB.peerId)
170
-
171
244
  await psA.start()
172
245
  await psB.start()
173
246
 
247
+ await connectPeers(psA.multicodecs[0], {
248
+ peerId: peerA,
249
+ registrar: registrarA
250
+ }, {
251
+ peerId: peerB,
252
+ registrar: registrarB
253
+ })
254
+
174
255
  await Promise.all([
175
- pWaitFor(() => psA.peers.size === 1),
176
- pWaitFor(() => psB.peers.size === 1)
256
+ pWaitFor(() => psA.getPeers().length === 1),
257
+ pWaitFor(() => psB.getPeers().length === 1)
177
258
  ])
178
259
 
179
260
  let subscribedTopics = psA.getTopics()
@@ -192,19 +273,38 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
192
273
  // wait for psB to know about psA subscription
193
274
  await pWaitFor(() => {
194
275
  const subscribedPeers = psB.getSubscribers(topic)
195
- return subscribedPeers.includes(psA.peerId.toString())
276
+ return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
196
277
  })
197
- void psB.publish(topic, data)
278
+ void psB.dispatchEvent(new CustomEvent(topic, { detail: data }))
198
279
 
199
280
  await defer.promise
200
281
  })
201
282
  })
202
283
 
203
284
  describe('pubsub with intermittent connections', () => {
285
+ let psA: PubsubBaseProtocol<EventMap>
286
+ let psB: PubsubBaseProtocol<EventMap>
287
+ let peerA: PeerId
288
+ let peerB: PeerId
289
+ let registrarA: Registrar
290
+ let registrarB: Registrar
291
+
204
292
  // Create pubsub nodes and start them
205
293
  beforeEach(async () => {
206
- psA = await common.setup()
207
- psB = await common.setup()
294
+ peerA = await createEd25519PeerId()
295
+ peerB = await createEd25519PeerId()
296
+
297
+ registrarA = mockRegistrar()
298
+ registrarB = mockRegistrar()
299
+
300
+ psA = await common.setup({
301
+ peerId: peerA,
302
+ registrar: registrarA
303
+ })
304
+ psB = await common.setup({
305
+ peerId: peerB,
306
+ registrar: registrarB
307
+ })
208
308
 
209
309
  await psA.start()
210
310
  await psB.start()
@@ -219,17 +319,21 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
219
319
  await common.teardown()
220
320
  })
221
321
 
222
- it('should receive pubsub messages after a node restart', async function () {
322
+ it.skip('should receive pubsub messages after a node restart', async function () {
223
323
  const topic = 'test-topic'
224
324
  const data = uint8ArrayFromString('hey!')
225
- const psAid = psA.peerId.toString()
226
325
 
227
326
  let counter = 0
228
327
  const defer1 = pDefer()
229
328
  const defer2 = pDefer()
230
329
 
231
- // @ts-expect-error protected fields
232
- await psA._libp2p.dial(psB.peerId)
330
+ await connectPeers(psA.multicodecs[0], {
331
+ peerId: peerA,
332
+ registrar: registrarA
333
+ }, {
334
+ peerId: peerB,
335
+ registrar: registrarB
336
+ })
233
337
 
234
338
  let subscribedTopics = psA.getTopics()
235
339
  expect(subscribedTopics).to.not.include(topic)
@@ -248,9 +352,9 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
248
352
  // wait for psB to know about psA subscription
249
353
  await pWaitFor(() => {
250
354
  const subscribedPeers = psB.getSubscribers(topic)
251
- return subscribedPeers.includes(psAid)
355
+ return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
252
356
  })
253
- void psB.publish(topic, data)
357
+ void psB.dispatchEvent(new CustomEvent(topic, { detail: data }))
254
358
 
255
359
  await defer1.promise
256
360
 
@@ -269,23 +373,26 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
269
373
  await psB._libp2p.start()
270
374
  await psB.start()
271
375
 
272
- // @ts-expect-error protected fields
273
- psA._libp2p.peerStore.addressBook.set(psB.peerId, psB._libp2p.multiaddrs)
274
- // @ts-expect-error protected fields
275
- await psA._libp2p.dial(psB.peerId)
376
+ await connectPeers(psA.multicodecs[0], {
377
+ peerId: peerA,
378
+ registrar: registrarA
379
+ }, {
380
+ peerId: peerB,
381
+ registrar: registrarB
382
+ })
276
383
 
277
384
  // wait for remoteLibp2p to know about libp2p subscription
278
385
  await pWaitFor(() => {
279
386
  const subscribedPeers = psB.getSubscribers(topic)
280
- return subscribedPeers.includes(psAid)
387
+ return subscribedPeers.toString().includes(peerA.toString())
281
388
  })
282
389
 
283
- void psB.publish(topic, data)
390
+ void psB.dispatchEvent(new CustomEvent(topic, { detail: data }))
284
391
 
285
392
  await defer2.promise
286
393
  })
287
394
 
288
- it('should handle quick reconnects with a delayed disconnect', async () => {
395
+ it.skip('should handle quick reconnects with a delayed disconnect', async () => {
289
396
  // Subscribe on both
290
397
  let aReceivedFirstMessageFromB = false
291
398
  let aReceivedSecondMessageFromB = false
@@ -327,21 +434,25 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
327
434
  // Create two connections to the remote peer
328
435
  // @ts-expect-error protected fields
329
436
  const originalConnection = await psA._libp2p.dialer.connectToPeer(psB.peerId)
437
+
330
438
  // second connection
331
- // @ts-expect-error protected fields
332
- await psA._libp2p.dialer.connectToPeer(psB.peerId)
333
- // @ts-expect-error protected fields
334
- expect(psA._libp2p.connections.get(psB.peerId.toString())).to.have.length(2)
439
+ await connectPeers(psA.multicodecs[0], {
440
+ peerId: peerA,
441
+ registrar: registrarA
442
+ }, {
443
+ peerId: peerB,
444
+ registrar: registrarB
445
+ })
335
446
 
336
447
  // Wait for subscriptions to occur
337
448
  await pWaitFor(() => {
338
- return psA.getSubscribers(topic).includes(psB.peerId.toString()) &&
339
- psB.getSubscribers(topic).includes(psA.peerId.toString())
449
+ return psA.getSubscribers(topic).includes(peerB) &&
450
+ psB.getSubscribers(topic).map(p => p.toString()).includes(peerA.toString())
340
451
  })
341
452
 
342
453
  // Verify messages go both ways
343
- void psA.publish(topic, uint8ArrayFromString('message-from-a-1'))
344
- void psB.publish(topic, uint8ArrayFromString('message-from-b-1'))
454
+ void psA.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('message-from-a-1') }))
455
+ void psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('message-from-b-1') }))
345
456
  await pWaitFor(() => {
346
457
  return aReceivedFirstMessageFromB && bReceivedFirstMessageFromA
347
458
  })
@@ -354,8 +465,8 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
354
465
  await pWaitFor(() => psAConnUpdateSpy.callCount === 1)
355
466
 
356
467
  // Verify messages go both ways after the disconnect
357
- void psA.publish(topic, uint8ArrayFromString('message-from-a-2'))
358
- void psB.publish(topic, uint8ArrayFromString('message-from-b-2'))
468
+ void psA.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('message-from-a-2') }))
469
+ void psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('message-from-b-2') }))
359
470
  await pWaitFor(() => {
360
471
  return aReceivedSecondMessageFromB && bReceivedSecondMessageFromA
361
472
  })
@@ -1,21 +1,29 @@
1
1
  import { expect } from 'aegir/utils/chai.js'
2
2
  import sinon from 'sinon'
3
3
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
4
+ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
5
+ import { mockRegistrar } from '../mocks/registrar.js'
6
+ import { CustomEvent } from '@libp2p/interfaces'
4
7
  import type { TestSetup } from '../index.js'
5
- import type { PubSub, PubsubOptions } from '@libp2p/interfaces/pubsub'
8
+ import type { PubSubOptions } from '@libp2p/interfaces/pubsub'
6
9
  import type { EventMap } from './index.js'
10
+ import type { PubsubBaseProtocol } from '@libp2p/pubsub'
7
11
 
8
12
  const topic = 'foo'
9
13
  const data = uint8ArrayFromString('bar')
10
14
  const shouldNotHappen = () => expect.fail()
11
15
 
12
- export default (common: TestSetup<PubSub<EventMap>, Partial<PubsubOptions>>) => {
16
+ export default (common: TestSetup<PubsubBaseProtocol<EventMap>, PubSubOptions>) => {
13
17
  describe('emit self', () => {
14
- let pubsub: PubSub<EventMap>
18
+ let pubsub: PubsubBaseProtocol<EventMap>
15
19
 
16
20
  describe('enabled', () => {
17
21
  before(async () => {
18
- pubsub = await common.setup({ emitSelf: true })
22
+ pubsub = await common.setup({
23
+ peerId: await createEd25519PeerId(),
24
+ registrar: mockRegistrar(),
25
+ emitSelf: true
26
+ })
19
27
  })
20
28
 
21
29
  before(async () => {
@@ -34,7 +42,7 @@ export default (common: TestSetup<PubSub<EventMap>, Partial<PubsubOptions>>) =>
34
42
  once: true
35
43
  }))
36
44
 
37
- void pubsub.publish(topic, data)
45
+ void pubsub.dispatchEvent(new CustomEvent(topic, { detail: data }))
38
46
 
39
47
  return await promise
40
48
  })
@@ -42,7 +50,11 @@ export default (common: TestSetup<PubSub<EventMap>, Partial<PubsubOptions>>) =>
42
50
 
43
51
  describe('disabled', () => {
44
52
  before(async () => {
45
- pubsub = await common.setup({ emitSelf: false })
53
+ pubsub = await common.setup({
54
+ peerId: await createEd25519PeerId(),
55
+ registrar: mockRegistrar(),
56
+ emitSelf: false
57
+ })
46
58
  })
47
59
 
48
60
  before(async () => {
@@ -61,7 +73,7 @@ export default (common: TestSetup<PubSub<EventMap>, Partial<PubsubOptions>>) =>
61
73
  once: true
62
74
  })
63
75
 
64
- void pubsub.publish(topic, data)
76
+ void pubsub.dispatchEvent(new CustomEvent(topic, { detail: data }))
65
77
 
66
78
  // Wait 1 second to guarantee that self is not noticed
67
79
  return await new Promise((resolve) => setTimeout(resolve, 1000))
@@ -5,17 +5,20 @@ 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 { PubSub, Message, PubsubEvents } from '@libp2p/interfaces/pubsub'
8
+ import type { Message, PubSubEvents, PubSubOptions } from '@libp2p/interfaces/pubsub'
9
+ import type { PubsubBaseProtocol } from '@libp2p/pubsub'
9
10
 
10
- export interface EventMap extends PubsubEvents {
11
+ export interface EventMap extends PubSubEvents {
11
12
  'topic': CustomEvent<Message>
12
13
  'foo': CustomEvent<Message>
13
14
  'test-topic': CustomEvent<Message>
14
15
  'reconnect-channel': CustomEvent<Message>
15
16
  'Z': CustomEvent<Message>
17
+ 'Za': CustomEvent<Message>
18
+ 'Zb': CustomEvent<Message>
16
19
  }
17
20
 
18
- export default (common: TestSetup<PubSub<EventMap>>) => {
21
+ export default (common: TestSetup<PubsubBaseProtocol<EventMap>, PubSubOptions>) => {
19
22
  describe('interface-pubsub compliance tests', () => {
20
23
  apiTest(common)
21
24
  emitSelfTest(common)