@libp2p/interface-compliance-tests 1.1.7 → 1.1.11

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 +12 -2
  2. package/dist/src/mocks/connection.d.ts.map +1 -1
  3. package/dist/src/mocks/connection.js +59 -33
  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 +11 -2
  10. package/dist/src/mocks/registrar.d.ts.map +1 -1
  11. package/dist/src/mocks/registrar.js +47 -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 +3 -2
  17. package/dist/src/pubsub/api.d.ts.map +1 -1
  18. package/dist/src/pubsub/api.js +23 -13
  19. package/dist/src/pubsub/api.js.map +1 -1
  20. package/dist/src/pubsub/connection-handlers.d.ts +3 -2
  21. package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
  22. package/dist/src/pubsub/connection-handlers.js +165 -69
  23. package/dist/src/pubsub/connection-handlers.js.map +1 -1
  24. package/dist/src/pubsub/emit-self.d.ts +3 -2
  25. package/dist/src/pubsub/emit-self.d.ts.map +1 -1
  26. package/dist/src/pubsub/emit-self.js +15 -4
  27. package/dist/src/pubsub/emit-self.js.map +1 -1
  28. package/dist/src/pubsub/index.d.ts +6 -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 +3 -2
  32. package/dist/src/pubsub/messages.d.ts.map +1 -1
  33. package/dist/src/pubsub/messages.js +42 -39
  34. package/dist/src/pubsub/messages.js.map +1 -1
  35. package/dist/src/pubsub/multiple-nodes.d.ts +3 -2
  36. package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
  37. package/dist/src/pubsub/multiple-nodes.js +172 -89
  38. package/dist/src/pubsub/multiple-nodes.js.map +1 -1
  39. package/dist/src/pubsub/two-nodes.d.ts +3 -2
  40. package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
  41. package/dist/src/pubsub/two-nodes.js +96 -40
  42. package/dist/src/pubsub/two-nodes.js.map +1 -1
  43. package/dist/src/pubsub/utils.d.ts +2 -2
  44. package/dist/src/pubsub/utils.d.ts.map +1 -1
  45. package/dist/src/pubsub/utils.js +7 -9
  46. package/dist/src/pubsub/utils.js.map +1 -1
  47. package/dist/src/stream-muxer/close-test.d.ts.map +1 -1
  48. package/dist/src/stream-muxer/close-test.js +5 -2
  49. package/dist/src/stream-muxer/close-test.js.map +1 -1
  50. package/package.json +8 -5
  51. package/src/mocks/connection.ts +83 -34
  52. package/src/mocks/multiaddr-connection.ts +3 -2
  53. package/src/mocks/registrar.ts +65 -7
  54. package/src/mocks/upgrader.ts +8 -2
  55. package/src/pubsub/api.ts +29 -15
  56. package/src/pubsub/connection-handlers.ts +186 -75
  57. package/src/pubsub/emit-self.ts +19 -7
  58. package/src/pubsub/index.ts +6 -3
  59. package/src/pubsub/messages.ts +59 -44
  60. package/src/pubsub/multiple-nodes.ts +197 -102
  61. package/src/pubsub/two-nodes.ts +111 -48
  62. package/src/pubsub/utils.ts +9 -10
  63. package/src/stream-muxer/close-test.ts +5 -2
@@ -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)
@@ -1,23 +1,33 @@
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
+ 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
+ import { CustomEvent } from '@libp2p/interfaces'
7
12
  import type { TestSetup } from '../index.js'
8
- import type { PubSub } from '@libp2p/interfaces/pubsub'
13
+ import type { PubSubOptions, RPC } from '@libp2p/interfaces/pubsub'
9
14
  import type { EventMap } from './index.js'
15
+ import type { PubsubBaseProtocol } from '@libp2p/pubsub'
10
16
 
11
17
  const topic = 'foo'
12
18
  const data = uint8ArrayFromString('bar')
13
19
 
14
- export default (common: TestSetup<PubSub<EventMap>>) => {
20
+ export default (common: TestSetup<PubsubBaseProtocol<EventMap>, PubSubOptions>) => {
15
21
  describe('messages', () => {
16
- let pubsub: PubSub<EventMap>
22
+ let pubsub: PubsubBaseProtocol<EventMap>
17
23
 
18
24
  // Create pubsub router
19
25
  beforeEach(async () => {
20
- pubsub = await common.setup()
26
+ pubsub = await common.setup({
27
+ peerId: await createEd25519PeerId(),
28
+ registrar: mockRegistrar(),
29
+ emitSelf: true
30
+ })
21
31
  await pubsub.start()
22
32
  })
23
33
 
@@ -29,83 +39,88 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
29
39
 
30
40
  it('should emit normalized signed messages on publish', async () => {
31
41
  pubsub.globalSignaturePolicy = 'StrictSign'
32
- // @ts-expect-error protected field
33
- sinon.spy(pubsub, '_emitMessage')
34
42
 
35
- 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
+ const spy = sinon.spy(pubsub, 'publishMessage')
44
+
45
+ await pubsub.dispatchEvent(new CustomEvent(topic, { detail: data }))
46
+
47
+ await pWaitFor(async () => {
48
+ return spy.callCount === 1
49
+ })
50
+
51
+ expect(spy).to.have.property('callCount', 1)
52
+
53
+ const [from, messageToEmit] = spy.getCall(0).args
40
54
 
55
+ expect(from.toString()).to.equal(pubsub.peerId.toString())
41
56
  expect(messageToEmit.seqno).to.not.eql(undefined)
42
57
  expect(messageToEmit.key).to.not.eql(undefined)
43
58
  expect(messageToEmit.signature).to.not.eql(undefined)
44
59
  })
45
60
 
46
61
  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')
62
+ const publishSpy = sinon.spy(pubsub, 'publishMessage')
51
63
  sinon.spy(pubsub, 'validate')
52
64
 
53
65
  const peerStream = new PeerStreams({
54
- id: await PeerIdFactory.createEd25519PeerId(),
66
+ id: await createEd25519PeerId(),
55
67
  protocol: 'test'
56
68
  })
57
- const rpc = {
69
+ const rpc: RPC = {
58
70
  subscriptions: [],
59
- msgs: [{
60
- receivedFrom: peerStream.id.toString(),
71
+ messages: [{
61
72
  from: peerStream.id.toBytes(),
62
73
  data,
63
- seqno: utils.randomSeqno(),
64
- topicIDs: [topic]
74
+ seqno: await noSignMsgId(data),
75
+ topic: topic
65
76
  }]
66
77
  }
67
78
 
68
79
  pubsub.subscribe(topic)
69
- // @ts-expect-error protected field
70
- await pubsub._processRpc(peerStream.id.toString(), peerStream, rpc)
71
80
 
72
- 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
+ await pubsub.processRpc(peerStream.id, peerStream, rpc)
82
+
83
+ // message should not be delivered
84
+ await delay(1000)
85
+
86
+ expect(publishSpy).to.have.property('called', false)
77
87
  })
78
88
 
79
89
  it('should not drop unsigned messages if strict signing is disabled', async () => {
80
90
  pubsub.globalSignaturePolicy = 'StrictNoSign'
81
- // @ts-expect-error protected field
82
- sinon.spy(pubsub, '_emitMessage')
83
- // @ts-expect-error protected field
84
- sinon.spy(pubsub, '_publish')
91
+
92
+ const publishSpy = sinon.spy(pubsub, 'publishMessage')
85
93
  sinon.spy(pubsub, 'validate')
86
94
 
87
95
  const peerStream = new PeerStreams({
88
- id: await PeerIdFactory.createEd25519PeerId(),
96
+ id: await createEd25519PeerId(),
89
97
  protocol: 'test'
90
98
  })
91
99
 
92
- const rpc = {
100
+ const rpc: RPC = {
93
101
  subscriptions: [],
94
- msgs: [{
102
+ messages: [{
103
+ from: peerStream.id.toBytes(),
95
104
  data,
96
- topicIDs: [topic]
105
+ topic
97
106
  }]
98
107
  }
99
108
 
100
109
  pubsub.subscribe(topic)
101
- // @ts-expect-error protected field
102
- await pubsub._processRpc(peerStream.id.toString(), peerStream, rpc)
110
+
111
+ const deferred = pDefer()
112
+
113
+ pubsub.addEventListener(topic, () => {
114
+ deferred.resolve()
115
+ })
116
+
117
+ await pubsub.processRpc(peerStream.id, peerStream, rpc)
118
+
119
+ // await message delivery
120
+ await deferred.promise
103
121
 
104
122
  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)
123
+ expect(publishSpy).to.have.property('callCount', 1)
109
124
  })
110
125
  })
111
126
  }