import { teardownPeerSetup , PeerSetupWithWallets , setupPeerWallets , } from '../../../jest/with-peers-setup-teardown' ; import { LatencyOptions , TestMessageService } from '../../message-service/test-message-service' ; import { getWithPeersCreateChannelsArgs , waitForObjectiveProposals } from '../utils' ; jest . setTimeout ( 120000 ) ; let peerSetup ; beforeAll ( async ( ) => { peerSetup = await setupPeerWallets ( ) ; } ) ; afterAll ( async ( ) => { await teardownPeerSetup ( peerSetup ) ; } ) ; describe ( 'CloseChannels' , ( ) => { const testCases = [ { dropRate : 0 , meanDelay : undefined , closer : 'A' , } , { dropRate : 0 , meanDelay : undefined , closer : 'B' , } , { dropRate : 0.1 , meanDelay : 15 , closer : 'A' } , ] ; test . each ( testCases ) ( 'can successfully create and close channel with the latency options: %o' , async ( options ) => { const { peerWallets } = peerSetup ; TestMessageService . setLatencyOptions ( peerWallets , { dropRate : 0 , meanDelay : undefined } ) ; const closingWallet = options . closer === 'A' ? peerWallets . a : peerWallets . b ; const walletReceivingClose = options . closer === 'A' ? peerWallets . b : peerWallets . a ; const response = await closingWallet . createChannels ( Array ( 10 ) . fill ( getWithPeersCreateChannelsArgs ( peerSetup ) ) ) ; const createChannelObjectiveIds = response . map ( o => o . objectiveId ) ; await waitForObjectiveProposals ( createChannelObjectiveIds , walletReceivingClose ) ; const receivingCloseResponse = await walletReceivingClose . approveObjectives ( createChannelObjectiveIds ) ; await expect ( response ) . toBeObjectiveDoneType ( 'Success' ) ; await expect ( receivingCloseResponse ) . toBeObjectiveDoneType ( 'Success' ) ; TestMessageService . setLatencyOptions ( peerWallets , options ) ; TestMessageService . freeze ( peerWallets ) ; const channelIds = response . map ( o => o . channelId ) ; const closeResponse = await closingWallet . closeChannels ( channelIds ) ; const secondCloseResponse = await closingWallet . closeChannels ( channelIds ) ; expect ( closeResponse . every ( o => o . currentStatus === 'approved' ) ) . toBe ( true ) ; expect ( secondCloseResponse . every ( o => o . currentStatus === 'approved' ) ) . toBe ( true ) ; TestMessageService . unfreeze ( peerWallets ) ; await expect ( closeResponse ) . toBeObjectiveDoneType ( 'Success' ) ; await expect ( secondCloseResponse ) . toBeObjectiveDoneType ( 'Success' ) ; const aChannels = await peerWallets . a . getChannels ( ) ; for ( const a of aChannels ) { expect ( a . status ) . toEqual ( 'closed' ) ; } const bChannels = await peerWallets . b . getChannels ( ) ; for ( const b of bChannels ) { expect ( b . status ) . toEqual ( 'closed' ) ; } } ) ; } ) ;