@@ -9,13 +9,15 @@ import { multiaddr } from '@multiformats/multiaddr'
99import { WebSockets , WebSocketsSecure } from '@multiformats/multiaddr-matcher'
1010import { expect } from 'aegir/chai'
1111import { isLoopbackAddr } from 'is-loopback-addr'
12+ import { createLibp2p } from 'libp2p'
1213import { pEvent } from 'p-event'
1314import pWaitFor from 'p-wait-for'
1415import Sinon from 'sinon'
1516import { stubInterface } from 'sinon-ts'
1617import * as filters from '../src/filters.js'
1718import { webSockets } from '../src/index.js'
18- import type { Connection , Libp2pEvents , Listener , Transport , Upgrader , TLSCertificate } from '@libp2p/interface'
19+ import type { Connection , Libp2pEvents , Listener , Transport , Upgrader , TLSCertificate , PeerDiscoveryEvents , PeerDiscovery , Startable } from '@libp2p/interface'
20+ import type { AddressManager } from '@libp2p/interface-internal'
1921import type { StubbedInstance } from 'sinon-ts'
2022
2123describe ( 'instantiate the transport' , ( ) => {
@@ -746,3 +748,45 @@ describe('auto-tls (IPv6)', () => {
746748 expect ( wsOptions . port ) . to . equal ( wssOptions . port )
747749 } )
748750} )
751+
752+ class TestPeerDiscovery extends TypedEventEmitter < PeerDiscoveryEvents > implements PeerDiscovery , Startable {
753+ private readonly addressManager : AddressManager
754+
755+ constructor ( components : {
756+ addressManager : AddressManager
757+ } ) {
758+ super ( )
759+ this . addressManager = components . addressManager
760+ }
761+
762+ async start ( ) : Promise < void > {
763+ }
764+
765+ async stop ( ) : Promise < void > { }
766+
767+ afterStart ( ) : void {
768+ this . addressManager . getAddresses ( )
769+ }
770+
771+ readonly [ Symbol . toStringTag ] = '@libp2p/test-peer-discovery'
772+ }
773+
774+ describe ( 'discovery-websockets' , ( ) => {
775+ it ( 'should discover peers over websockets' , async ( ) => {
776+ const libp2p = await createLibp2p ( {
777+ addresses : {
778+ listen : [
779+ '/ip4/0.0.0.0/tcp/0/ws'
780+ ]
781+ } ,
782+ peerDiscovery : [ ( component : {
783+ addressManager : AddressManager
784+ } ) => new TestPeerDiscovery ( component ) ] ,
785+ transports : [
786+ webSockets ( )
787+ ]
788+ } )
789+
790+ await libp2p . start ( )
791+ } )
792+ } )
0 commit comments