import { NetworkContextForTestSuite } from "@counterfactual/local-ganache-server" ; import { parseEther } from "ethers/utils" ; import { Node } from "../../src" ; import { NODE_EVENTS } from "../../src/types" ; import { toBeLt } from "../engine/integration/bignumber-jest-matcher" ; import { setup , SetupContext } from "./setup" ; import { collateralizeChannel , constructUninstallVirtualRpc , createChannel , installVirtualApp } from "./utils" ; expect . extend ( { toBeLt } ) ; jest . setTimeout ( 15000 ) ; const { TicTacToeApp } = global [ "networkContext" ] as NetworkContextForTestSuite ; describe ( "Concurrently uninstalling virtual and installing virtual applications without issue" , ( ) => { let multisigAddressAB ; let multisigAddressBC ; let nodeA ; let nodeB ; let nodeC ; let installedAppInstanceId ; beforeEach ( async ( ) => { const context = await setup ( global , true ) ; nodeA = context [ "A" ] . node ; nodeB = context [ "B" ] . node ; nodeC = context [ "C" ] . node ; multisigAddressAB = await createChannel ( nodeA , nodeB ) ; multisigAddressBC = await createChannel ( nodeB , nodeC ) ; await collateralizeChannel ( multisigAddressAB , nodeA , nodeB , parseEther ( "2" ) ) ; await collateralizeChannel ( multisigAddressBC , nodeB , nodeC , parseEther ( "2" ) ) ; installedAppInstanceId = await installVirtualApp ( nodeA , nodeB , nodeC , TicTacToeApp ) ; } ) ; it ( "will uninstall virtual and install virtual successfully when called by the same node" , async ( done ) => { let completedEvents = 0 ; const registerEvent = ( ) => { completedEvents += 1 ; if ( completedEvents === 2 ) done ( ) ; } ; nodeA . once ( NODE_EVENTS . INSTALL_VIRTUAL , registerEvent ) ; nodeC . once ( NODE_EVENTS . UNINSTALL_VIRTUAL , registerEvent ) ; nodeA . rpcRouter . dispatch ( constructUninstallVirtualRpc ( installedAppInstanceId , nodeB . publicIdentifier ) ) ; installVirtualApp ( nodeA , nodeB , nodeC , TicTacToeApp ) ; } ) ; it ( "will uninstall virtual and install virtual successfully when called by different nodes" , async ( done ) => { let completedEvents = 0 ; const registerEvent = ( ) => { completedEvents += 1 ; if ( completedEvents === 2 ) done ( ) ; } ; nodeA . once ( NODE_EVENTS . INSTALL_VIRTUAL , registerEvent ) ; nodeA . once ( NODE_EVENTS . UNINSTALL_VIRTUAL , registerEvent ) ; nodeC . rpcRouter . dispatch ( constructUninstallVirtualRpc ( installedAppInstanceId , nodeB . publicIdentifier ) ) ; installVirtualApp ( nodeA , nodeB , nodeC , TicTacToeApp ) ; } ) ; } ) ;