@libp2p/interface-compliance-tests 1.1.24 → 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 (45) 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.map +1 -1
  14. package/dist/src/pubsub/api.js +10 -15
  15. package/dist/src/pubsub/api.js.map +1 -1
  16. package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
  17. package/dist/src/pubsub/connection-handlers.js +53 -145
  18. package/dist/src/pubsub/connection-handlers.js.map +1 -1
  19. package/dist/src/pubsub/emit-self.d.ts.map +1 -1
  20. package/dist/src/pubsub/emit-self.js +8 -7
  21. package/dist/src/pubsub/emit-self.js.map +1 -1
  22. package/dist/src/pubsub/messages.d.ts.map +1 -1
  23. package/dist/src/pubsub/messages.js +9 -12
  24. package/dist/src/pubsub/messages.js.map +1 -1
  25. package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
  26. package/dist/src/pubsub/multiple-nodes.js +80 -216
  27. package/dist/src/pubsub/multiple-nodes.js.map +1 -1
  28. package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
  29. package/dist/src/pubsub/two-nodes.js +20 -38
  30. package/dist/src/pubsub/two-nodes.js.map +1 -1
  31. package/dist/src/pubsub/utils.d.ts +2 -0
  32. package/dist/src/pubsub/utils.d.ts.map +1 -1
  33. package/dist/src/pubsub/utils.js +10 -0
  34. package/dist/src/pubsub/utils.js.map +1 -1
  35. package/package.json +1 -1
  36. package/src/mocks/connection-manager.ts +4 -20
  37. package/src/mocks/connection.ts +5 -5
  38. package/src/mocks/registrar.ts +6 -10
  39. package/src/pubsub/api.ts +11 -16
  40. package/src/pubsub/connection-handlers.ts +54 -151
  41. package/src/pubsub/emit-self.ts +10 -7
  42. package/src/pubsub/messages.ts +11 -13
  43. package/src/pubsub/multiple-nodes.ts +88 -220
  44. package/src/pubsub/two-nodes.ts +23 -41
  45. package/src/pubsub/utils.ts +11 -0
@@ -5,15 +5,12 @@ import pDefer from 'p-defer'
5
5
  import pWaitFor from 'p-wait-for'
6
6
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
7
7
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
8
- import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
9
- import { createEd25519PeerId } from '@libp2p/peer-id-factory'
10
- import { waitForSubscriptionUpdate } from './utils.js'
8
+ import { connectPeers } from '../mocks/registrar.js'
9
+ import { createComponents, waitForSubscriptionUpdate } from './utils.js'
11
10
  import type { TestSetup } from '../index.js'
12
11
  import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
13
12
  import type { PubSubArgs } from './index.js'
14
- import type { PeerId } from '@libp2p/interfaces/peer-id'
15
- import type { Registrar } from '@libp2p/interfaces/registrar'
16
- import { Components } from '@libp2p/interfaces/components'
13
+ import type { Components } from '@libp2p/interfaces/components'
17
14
  import { start, stop } from '../index.js'
18
15
 
19
16
  const topic = 'foo'
@@ -26,33 +23,22 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
26
23
  describe('pubsub with two nodes', () => {
27
24
  let psA: PubSub
28
25
  let psB: PubSub
29
- let peerIdA: PeerId
30
- let peerIdB: PeerId
31
- let registrarA: Registrar
32
- let registrarB: Registrar
26
+ let componentsA: Components
27
+ let componentsB: Components
33
28
 
34
29
  // Create pubsub nodes and connect them
35
30
  beforeEach(async () => {
36
- peerIdA = await createEd25519PeerId()
37
- peerIdB = await createEd25519PeerId()
38
-
39
- registrarA = mockRegistrar()
40
- registrarB = mockRegistrar()
31
+ componentsA = await createComponents()
32
+ componentsB = await createComponents()
41
33
 
42
34
  psA = await common.setup({
43
- components: new Components({
44
- peerId: peerIdA,
45
- registrar: registrarA
46
- }),
35
+ components: componentsA,
47
36
  init: {
48
37
  emitSelf: true
49
38
  }
50
39
  })
51
40
  psB = await common.setup({
52
- components: new Components({
53
- peerId: peerIdB,
54
- registrar: registrarB
55
- }),
41
+ components: componentsB,
56
42
  init: {
57
43
  emitSelf: false
58
44
  }
@@ -64,13 +50,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
64
50
  expect(psA.getPeers()).to.be.empty()
65
51
  expect(psB.getPeers()).to.be.empty()
66
52
 
67
- await connectPeers(psA.multicodecs[0], {
68
- peerId: peerIdA,
69
- registrar: registrarA
70
- }, {
71
- peerId: peerIdB,
72
- registrar: registrarB
73
- })
53
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
74
54
 
75
55
  // Wait for peers to be ready in pubsub
76
56
  await pWaitFor(() => psA.getPeers().length === 1 && psB.getPeers().length === 1)
@@ -91,7 +71,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
91
71
  const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
92
72
  expect(psA.getTopics()).to.deep.equal([topic])
93
73
  expect(psB.getPeers()).to.have.lengthOf(1)
94
- expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdA.toString()])
74
+ expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsA.getPeerId().toString()])
95
75
  expect(changedPeerId).to.deep.equal(psB.getPeers()[0])
96
76
  expect(changedSubs).to.have.lengthOf(1)
97
77
  expect(changedSubs[0].topic).to.equal(topic)
@@ -123,11 +103,11 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
123
103
  psB.subscribe(topic)
124
104
 
125
105
  await Promise.all([
126
- waitForSubscriptionUpdate(psA, peerIdB),
127
- waitForSubscriptionUpdate(psB, peerIdA)
106
+ waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
107
+ waitForSubscriptionUpdate(psB, componentsA.getPeerId())
128
108
  ])
129
109
 
130
- psA.publish(topic, uint8ArrayFromString('hey'))
110
+ await psA.publish(topic, uint8ArrayFromString('hey'))
131
111
 
132
112
  return await defer.promise
133
113
  })
@@ -166,11 +146,11 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
166
146
  psB.subscribe(topic)
167
147
 
168
148
  await Promise.all([
169
- waitForSubscriptionUpdate(psA, peerIdB),
170
- waitForSubscriptionUpdate(psB, peerIdA)
149
+ waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
150
+ waitForSubscriptionUpdate(psB, componentsA.getPeerId())
171
151
  ])
172
152
 
173
- psB.publish(topic, uint8ArrayFromString('banana'))
153
+ await psB.publish(topic, uint8ArrayFromString('banana'))
174
154
 
175
155
  return await defer.promise
176
156
  })
@@ -185,7 +165,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
185
165
  function receivedMsg (evt: CustomEvent<Message>) {
186
166
  const msg = evt.detail
187
167
  expect(uint8ArrayToString(msg.data)).to.equal('banana')
188
- expect(msg.from.toString()).to.equal(peerIdB.toString())
168
+ expect(msg.from.toString()).to.equal(componentsB.getPeerId().toString())
189
169
  expect(msg.sequenceNumber).to.be.a('BigInt')
190
170
  expect(msg.topic).to.be.equal(topic)
191
171
 
@@ -201,11 +181,13 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
201
181
  psB.subscribe(topic)
202
182
 
203
183
  await Promise.all([
204
- waitForSubscriptionUpdate(psA, peerIdB),
205
- waitForSubscriptionUpdate(psB, peerIdA)
184
+ waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
185
+ waitForSubscriptionUpdate(psB, componentsA.getPeerId())
206
186
  ])
207
187
 
208
- Array.from({ length: 10 }, (_, i) => psB.publish(topic, uint8ArrayFromString('banana')))
188
+ await Promise.all(
189
+ Array.from({ length: 10 }, async (_, i) => await psB.publish(topic, uint8ArrayFromString('banana')))
190
+ )
209
191
 
210
192
  return await defer.promise
211
193
  })
@@ -1,7 +1,10 @@
1
1
  import { pEvent } from 'p-event'
2
2
  import pWaitFor from 'p-wait-for'
3
+ import { Components } from '@libp2p/interfaces/components'
3
4
  import type { PubSub, SubscriptionChangeData } from '@libp2p/interfaces/pubsub'
4
5
  import type { PeerId } from '@libp2p/interfaces/peer-id'
6
+ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
7
+ import { mockConnectionManager, mockRegistrar } from '../mocks/index.js'
5
8
 
6
9
  export async function waitForSubscriptionUpdate (a: PubSub, b: PeerId) {
7
10
  await pWaitFor(async () => {
@@ -10,3 +13,11 @@ export async function waitForSubscriptionUpdate (a: PubSub, b: PeerId) {
10
13
  return event.detail.peerId.equals(b)
11
14
  })
12
15
  }
16
+
17
+ export async function createComponents (): Promise<Components> {
18
+ return new Components({
19
+ peerId: await createEd25519PeerId(),
20
+ registrar: mockRegistrar(),
21
+ connectionManager: mockConnectionManager()
22
+ })
23
+ }