@@ -9,8 +9,8 @@ import { DelegatedPeerRouting } from '../src/index.js'
9
9
import goIpfs from 'go-ipfs'
10
10
import { peerIdFromString } from '@libp2p/peer-id'
11
11
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
12
+ import all from 'it-all'
12
13
import type { IDResult } from 'ipfs-core-types/src/root'
13
- import type { PeerData } from 'ipfs-core-types/src/dht'
14
14
15
15
const factory = createFactory ( {
16
16
type : 'go' ,
@@ -107,22 +107,12 @@ describe('DelegatedPeerRouting', function () {
107
107
host : opts . host
108
108
} ) )
109
109
110
- let peer : PeerData | undefined
111
-
112
- for await ( const event of router . findPeer ( peerIdFromString ( peerIdToFind . id ) ) ) {
113
- if ( event . name === 'FINAL_PEER' ) {
114
- peer = event . peer
115
- }
116
- }
117
-
118
- if ( peer == null ) {
119
- throw new Error ( 'Did not find peer' )
120
- }
110
+ const peer = await router . findPeer ( peerIdFromString ( peerIdToFind . id ) )
121
111
122
112
const { id, multiaddrs } = peer
123
113
expect ( id ) . to . exist ( )
124
114
expect ( multiaddrs ) . to . exist ( )
125
- expect ( id ) . to . eql ( peerIdToFind . id )
115
+ expect ( id . equals ( peerIdToFind . id ) ) . to . be . true ( )
126
116
} )
127
117
128
118
it ( 'should be able to find peers via the delegate with a peerid' , async ( ) => {
@@ -133,17 +123,7 @@ describe('DelegatedPeerRouting', function () {
133
123
host : opts . host
134
124
} ) )
135
125
136
- let peer : PeerData | undefined
137
-
138
- for await ( const event of router . findPeer ( peerIdFromString ( peerIdToFind . id ) ) ) {
139
- if ( event . name === 'FINAL_PEER' ) {
140
- peer = event . peer
141
- }
142
- }
143
-
144
- if ( peer == null ) {
145
- throw new Error ( 'Did not find peer' )
146
- }
126
+ const peer = await router . findPeer ( peerIdFromString ( peerIdToFind . id ) )
147
127
148
128
const { id, multiaddrs } = peer
149
129
expect ( id ) . to . exist ( )
@@ -160,17 +140,7 @@ describe('DelegatedPeerRouting', function () {
160
140
host : opts . host
161
141
} ) )
162
142
163
- let peer : PeerData | undefined
164
-
165
- for await ( const event of router . findPeer ( peerIdFromString ( peerIdToFind . id ) , { timeout : 2000 } ) ) {
166
- if ( event . name === 'FINAL_PEER' ) {
167
- peer = event . peer
168
- }
169
- }
170
-
171
- if ( peer == null ) {
172
- throw new Error ( 'Did not find peer' )
173
- }
143
+ const peer = await router . findPeer ( peerIdFromString ( peerIdToFind . id ) , { timeout : 2000 } )
174
144
175
145
const { id, multiaddrs } = peer
176
146
expect ( id ) . to . exist ( )
@@ -189,11 +159,8 @@ describe('DelegatedPeerRouting', function () {
189
159
190
160
// This is one of the default Bootstrap nodes, but we're not connected to it
191
161
// so we'll test with it.
192
- for await ( const event of router . findPeer ( peerIdFromString ( 'QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64' ) ) ) {
193
- if ( event . name === 'FINAL_PEER' ) {
194
- throw new Error ( 'Should not have found peer' )
195
- }
196
- }
162
+ await expect ( router . findPeer ( peerIdFromString ( 'QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64' ) ) )
163
+ . to . eventually . be . rejected . with . property ( 'code' , 'ERR_NOT_FOUND' )
197
164
} )
198
165
} )
199
166
@@ -212,19 +179,13 @@ describe('DelegatedPeerRouting', function () {
212
179
213
180
const key = peerIdFromString ( peerIdToFind . id ) . toBytes ( )
214
181
215
- const closerPeers : PeerData [ ] = [ ]
216
-
217
- for await ( const event of router . getClosestPeers ( key ) ) {
218
- if ( event . name === 'PEER_RESPONSE' ) {
219
- closerPeers . push ( ...event . closer )
220
- }
221
- }
182
+ const closerPeers = await all ( router . getClosestPeers ( key ) )
222
183
223
184
// we should be closest to the 2 other peers
224
185
expect ( closerPeers . length ) . to . equal ( 2 )
225
186
closerPeers . forEach ( result => {
226
- // shouldnt be the delegate
227
- expect ( delegatePeerId . equals ( peerIdFromString ( result . id ) ) ) . to . equal ( false )
187
+ // shouldn't be the delegate
188
+ expect ( delegatePeerId . equals ( result . id ) ) . to . equal ( false )
228
189
expect ( result . multiaddrs ) . to . be . an ( 'array' )
229
190
} )
230
191
} )
@@ -242,19 +203,13 @@ describe('DelegatedPeerRouting', function () {
242
203
const delegatePeerId = peerIdFromString ( nodeId . id )
243
204
244
205
const peerId = await createEd25519PeerId ( )
245
- const closerPeers : PeerData [ ] = [ ]
246
-
247
- for await ( const event of router . getClosestPeers ( peerId . toBytes ( ) ) ) {
248
- if ( event . name === 'PEER_RESPONSE' ) {
249
- closerPeers . push ( ...event . closer )
250
- }
251
- }
206
+ const closerPeers = await all ( router . getClosestPeers ( peerId . toBytes ( ) ) )
252
207
253
208
// we should be closest to the 2 other peers
254
209
expect ( closerPeers . length ) . to . equal ( 2 )
255
210
closerPeers . forEach ( result => {
256
211
// shouldnt be the delegate
257
- expect ( delegatePeerId . equals ( peerIdFromString ( result . id ) ) ) . to . equal ( false )
212
+ expect ( delegatePeerId . equals ( result . id ) ) . to . equal ( false )
258
213
expect ( result . multiaddrs ) . to . be . an ( 'array' )
259
214
} )
260
215
} )
0 commit comments