import { P2P , events , constants } from '../../../src/index' ; import { wait } from '../../utils/helpers' ; import { createNetwork , destroyNetwork } from '../../utils/network_setup' ; const SOCKET_HUNG_UP_CODE = 1006 ; const { EVENT_CLOSE_INBOUND , EVENT_CLOSE_OUTBOUND } = events ; const { INTENTIONAL_DISCONNECT_CODE } = constants ; describe ( 'Events on Connection Close' , ( ) => { let p2pNodeList = [ ] ; const collectedEvents = new Map ( ) ; beforeEach ( async ( ) => { p2pNodeList = await createNetwork ( { networkSize : 2 , networkDiscoveryWaitTime : 1 , } ) ; const firstNode = p2pNodeList [ 0 ] ; const secondNode = p2pNodeList [ 1 ] ; firstNode . on ( EVENT_CLOSE_INBOUND , msg => { collectedEvents . set ( 'EVENT_CLOSE_INBOUND' , msg ) ; } ) ; secondNode . on ( EVENT_CLOSE_OUTBOUND , msg => { collectedEvents . set ( 'EVENT_CLOSE_OUTBOUND' , msg ) ; } ) ; await wait ( 1000 ) ; } ) ; afterEach ( async ( ) => { await destroyNetwork ( p2pNodeList ) ; } ) ; it ( `should handle ${ EVENT_CLOSE_INBOUND } event and payload` , async ( ) => { const secondNode = p2pNodeList [ 1 ] ; await secondNode . stop ( ) ; await wait ( 300 ) ; const payload = collectedEvents . get ( 'EVENT_CLOSE_INBOUND' ) ; expect ( payload ) . toMatchObject ( { code : INTENTIONAL_DISCONNECT_CODE , peerInfo : { port : secondNode . config . port , sharedState : expect . any ( Object ) , } , } ) ; } ) ; it ( `should handle ${ EVENT_CLOSE_OUTBOUND } event and payload` , async ( ) => { const firstNode = p2pNodeList [ 0 ] ; await firstNode . stop ( ) ; await wait ( 300 ) ; const payload = collectedEvents . get ( 'EVENT_CLOSE_OUTBOUND' ) ; expect ( payload ) . toMatchObject ( { code : SOCKET_HUNG_UP_CODE , peerInfo : { port : firstNode . config . port , sharedState : expect . any ( Object ) , } , } ) ; } ) ; } ) ;