import { teardownPeerSetup , setupPeerWallets , PeerSetupWithWallets , } from '../../../jest/with-peers-setup-teardown' ; import { LatencyOptions , TestMessageService } from '../../message-service/test-message-service' ; import { getWithPeersCreateChannelsArgs , waitForObjectiveProposals } from '../utils' ; jest . setTimeout ( 60000 ) ; let peerSetup ; beforeAll ( async ( ) => { peerSetup = await setupPeerWallets ( ) ; } ) ; afterAll ( async ( ) => { await teardownPeerSetup ( peerSetup ) ; } ) ; describe ( 'EnsureObjectives' , ( ) => { const testCases = [ { dropRate : 0 , meanDelay : undefined , } , { dropRate : 0.3 , meanDelay : undefined } , { dropRate : 0 , meanDelay : 50 } , { dropRate : 0.2 , meanDelay : 25 } , ] ; test . each ( testCases ) ( 'can successfully create a channel with the latency options: %o' , async ( options ) => { const { peerWallets , peerEngines } = peerSetup ; TestMessageService . setLatencyOptions ( peerWallets , options ) ; const response = await peerWallets . a . createChannels ( Array ( 1 ) . fill ( getWithPeersCreateChannelsArgs ( peerSetup ) ) ) ; const objectiveIds = response . map ( o => o . objectiveId ) ; await waitForObjectiveProposals ( objectiveIds , peerWallets . b ) ; const bResponse = await peerWallets . b . approveObjectives ( objectiveIds ) ; await expect ( response ) . toBeObjectiveDoneType ( 'Success' ) ; await expect ( bResponse ) . toBeObjectiveDoneType ( 'Success' ) ; const { channelResults : aChannels } = await peerEngines . a . getChannels ( ) ; for ( const a of aChannels ) { expect ( a . status ) . toEqual ( 'running' ) ; } const { channelResults : bChannels } = await peerEngines . a . getChannels ( ) ; for ( const b of bChannels ) { expect ( b . status ) . toEqual ( 'running' ) ; } } ) ; test ( 'fails when all messages are dropped' , async ( ) => { const { peerWallets } = peerSetup ; TestMessageService . setLatencyOptions ( peerWallets , { dropRate : 1 } ) ; peerWallets . b . on ( 'ObjectiveProposed' , o => peerWallets . b . approveObjectives ( [ o . objectiveId ] ) ) ; const result = await peerWallets . a . createChannels ( [ getWithPeersCreateChannelsArgs ( peerSetup ) ] ) ; await expect ( result ) . toBeObjectiveDoneType ( 'ObjectiveTimedOutError' ) ; } ) ; } ) ;