@@ -7,6 +7,7 @@ const { expect } = chai
7
7
const pDefer = require ( 'p-defer' )
8
8
9
9
const PeerInfo = require ( 'peer-info' )
10
+ const Topology = require ( 'libp2p-interfaces/src/topology' )
10
11
const PeerStore = require ( '../../src/peer-store' )
11
12
const Registrar = require ( '../../src/registrar' )
12
13
const { createMockConnection } = require ( './utils' )
@@ -32,53 +33,7 @@ describe('registrar', () => {
32
33
throw new Error ( 'should fail to register a protocol if no multicodec is provided' )
33
34
} )
34
35
35
- it ( 'should fail to register a protocol if no handlers are provided' , ( ) => {
36
- const topologyProps = {
37
- multicodecs : multicodec
38
- }
39
-
40
- try {
41
- registrar . register ( topologyProps )
42
- } catch ( err ) {
43
- expect ( err ) . to . exist ( )
44
- return
45
- }
46
- throw new Error ( 'should fail to register a protocol if no handlers are provided' )
47
- } )
48
-
49
- it ( 'should fail to register a protocol if the onConnect handler is not provided' , ( ) => {
50
- const topologyProps = {
51
- multicodecs : multicodec ,
52
- handlers : {
53
- onDisconnect : ( ) => { }
54
- }
55
- }
56
-
57
- try {
58
- registrar . register ( topologyProps )
59
- } catch ( err ) {
60
- expect ( err ) . to . exist ( )
61
- return
62
- }
63
- throw new Error ( 'should fail to register a protocol if the onConnect handler is not provided' )
64
- } )
65
-
66
- it ( 'should fail to register a protocol if the onDisconnect handler is not provided' , ( ) => {
67
- const topologyProps = {
68
- multicodecs : multicodec ,
69
- handlers : {
70
- onConnect : ( ) => { }
71
- }
72
- }
73
-
74
- try {
75
- registrar . register ( topologyProps )
76
- } catch ( err ) {
77
- expect ( err ) . to . exist ( )
78
- return
79
- }
80
- throw new Error ( 'should fail to register a protocol if the onDisconnect handler is not provided' )
81
- } )
36
+ // TODO: not valid topology
82
37
} )
83
38
84
39
describe ( 'registration' , ( ) => {
@@ -88,27 +43,27 @@ describe('registrar', () => {
88
43
} )
89
44
90
45
it ( 'should be able to register a protocol' , ( ) => {
91
- const topologyProps = {
46
+ const topologyProps = new Topology ( {
47
+ multicodecs : multicodec ,
92
48
handlers : {
93
49
onConnect : ( ) => { } ,
94
50
onDisconnect : ( ) => { }
95
- } ,
96
- multicodecs : multicodec
97
- }
51
+ }
52
+ } )
98
53
99
54
const identifier = registrar . register ( topologyProps )
100
55
101
56
expect ( identifier ) . to . exist ( )
102
57
} )
103
58
104
59
it ( 'should be able to unregister a protocol' , ( ) => {
105
- const topologyProps = {
60
+ const topologyProps = new Topology ( {
61
+ multicodecs : multicodec ,
106
62
handlers : {
107
63
onConnect : ( ) => { } ,
108
64
onDisconnect : ( ) => { }
109
- } ,
110
- multicodecs : multicodec
111
- }
65
+ }
66
+ } )
112
67
113
68
const identifier = registrar . register ( topologyProps )
114
69
const success = registrar . unregister ( identifier )
@@ -138,7 +93,7 @@ describe('registrar', () => {
138
93
registrar . onConnect ( remotePeerInfo , conn )
139
94
expect ( registrar . connections . size ) . to . eql ( 1 )
140
95
141
- const topologyProps = {
96
+ const topologyProps = new Topology ( {
142
97
multicodecs : multicodec ,
143
98
handlers : {
144
99
onConnect : ( peerInfo , connection ) => {
@@ -153,19 +108,17 @@ describe('registrar', () => {
153
108
onDisconnectDefer . resolve ( )
154
109
}
155
110
}
156
- }
111
+ } )
157
112
158
113
// Register protocol
159
114
const identifier = registrar . register ( topologyProps )
160
115
const topology = registrar . topologies . get ( identifier )
161
116
162
117
// Topology created
163
118
expect ( topology ) . to . exist ( )
164
- expect ( topology . peers . size ) . to . eql ( 1 )
165
119
166
120
registrar . onDisconnect ( remotePeerInfo )
167
121
expect ( registrar . connections . size ) . to . eql ( 0 )
168
- expect ( topology . peers . size ) . to . eql ( 1 ) // topology should keep the peer
169
122
170
123
// Wait for handlers to be called
171
124
return Promise . all ( [
@@ -178,7 +131,7 @@ describe('registrar', () => {
178
131
const onConnectDefer = pDefer ( )
179
132
const onDisconnectDefer = pDefer ( )
180
133
181
- const topologyProps = {
134
+ const topologyProps = new Topology ( {
182
135
multicodecs : multicodec ,
183
136
handlers : {
184
137
onConnect : ( ) => {
@@ -188,15 +141,14 @@ describe('registrar', () => {
188
141
onDisconnectDefer . resolve ( )
189
142
}
190
143
}
191
- }
144
+ } )
192
145
193
146
// Register protocol
194
147
const identifier = registrar . register ( topologyProps )
195
148
const topology = registrar . topologies . get ( identifier )
196
149
197
150
// Topology created
198
151
expect ( topology ) . to . exist ( )
199
- expect ( topology . peers . size ) . to . eql ( 0 )
200
152
expect ( registrar . connections . size ) . to . eql ( 0 )
201
153
202
154
// Setup connections before registrar
@@ -212,7 +164,6 @@ describe('registrar', () => {
212
164
peerStore . put ( peerInfo )
213
165
214
166
await onConnectDefer . promise
215
- expect ( topology . peers . size ) . to . eql ( 1 )
216
167
217
168
// Remove protocol to peer and update it
218
169
peerInfo . protocols . delete ( multicodec )
0 commit comments