@@ -5,15 +5,13 @@ import pDefer from 'p-defer'
5
5
import { createLibp2p } from '../../src/index.js'
6
6
import type { Components } from '../../src/components.js'
7
7
import type { Libp2p } from '@libp2p/interface'
8
+ import type { Registrar } from '@libp2p/interface-internal'
8
9
9
10
describe ( 'registrar protocols' , ( ) => {
10
11
let libp2p : Libp2p
12
+ let registrar : Registrar
11
13
12
- afterEach ( async ( ) => {
13
- await libp2p ?. stop ( )
14
- } )
15
-
16
- it ( 'should be able to register and unregister a handler' , async ( ) => {
14
+ beforeEach ( async ( ) => {
17
15
const deferred = pDefer < Components > ( )
18
16
19
17
libp2p = await createLibp2p ( {
@@ -25,9 +23,14 @@ describe('registrar protocols', () => {
25
23
} )
26
24
27
25
const components = await deferred . promise
26
+ registrar = components . registrar
27
+ } )
28
28
29
- const registrar = components . registrar
29
+ afterEach ( async ( ) => {
30
+ await libp2p ?. stop ( )
31
+ } )
30
32
33
+ it ( 'should be able to register and unregister a handler' , async ( ) => {
31
34
expect ( registrar . getProtocols ( ) ) . to . not . have . any . keys ( [ '/echo/1.0.0' , '/echo/1.0.1' ] )
32
35
33
36
const echoHandler = ( ) : void => { }
@@ -43,4 +46,38 @@ describe('registrar protocols', () => {
43
46
'/echo/1.0.1'
44
47
] )
45
48
} )
49
+
50
+ it ( 'should error if registering two handlers for the same protocol' , async ( ) => {
51
+ const echoHandler = ( ) : void => { }
52
+ await libp2p . handle ( '/echo/1.0.0' , echoHandler )
53
+
54
+ await expect ( libp2p . handle ( '/echo/1.0.0' , echoHandler ) ) . to . eventually . be . rejected
55
+ . with . property ( 'name' , 'DuplicateProtocolHandlerError' )
56
+ } )
57
+
58
+ it ( 'should error if registering two handlers for the same protocols' , async ( ) => {
59
+ const echoHandler = ( ) : void => { }
60
+ await libp2p . handle ( '/echo/1.0.0' , echoHandler )
61
+
62
+ await expect ( libp2p . handle ( [ '/echo/2.0.0' , '/echo/1.0.0' ] , echoHandler ) ) . to . eventually . be . rejected
63
+ . with . property ( 'name' , 'DuplicateProtocolHandlerError' )
64
+ } )
65
+
66
+ it ( 'should not error if force-registering two handlers for the same protocol' , async ( ) => {
67
+ const echoHandler = ( ) : void => { }
68
+ await libp2p . handle ( '/echo/1.0.0' , echoHandler )
69
+
70
+ await expect ( libp2p . handle ( '/echo/1.0.0' , echoHandler , {
71
+ force : true
72
+ } ) ) . to . eventually . be . ok
73
+ } )
74
+
75
+ it ( 'should not error if force-registering two handlers for the same protocols' , async ( ) => {
76
+ const echoHandler = ( ) : void => { }
77
+ await libp2p . handle ( '/echo/1.0.0' , echoHandler )
78
+
79
+ await expect ( libp2p . handle ( [ '/echo/2.0.0' , '/echo/1.0.0' ] , echoHandler , {
80
+ force : true
81
+ } ) ) . to . eventually . be . ok
82
+ } )
46
83
} )
0 commit comments