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 { validAction } from "./tic-tac-toe" ; import { collateralizeChannel , constructTakeActionRpc , constructUninstallVirtualRpc , createChannel , installApp , installVirtualApp } from "./utils" ; expect . extend ( { toBeLt } ) ; jest . setTimeout ( 15000 ) ; const { TicTacToeApp } = global [ "networkContext" ] as NetworkContextForTestSuite ; describe ( "Concurrently taking action on regular app and uninstallling virtual app without issue" , ( ) => { let multisigAddressAB ; let multisigAddressBC ; let nodeA ; let nodeB ; let nodeC ; let virtualId ; let appId ; 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" ) ) ; virtualId = await installVirtualApp ( nodeA , nodeB , nodeC , TicTacToeApp ) ; [ appId ] = await installApp ( nodeA , nodeB , TicTacToeApp ) ; } ) ; it ( "can handle concurrent uninstall virtual and take action by the same node" , async ( done ) => { let executedActions = 0 ; const incrementAndEnd = ( ) => { executedActions += 1 ; if ( executedActions === 2 ) done ( ) ; } ; nodeC . on ( NODE_EVENTS . UNINSTALL_VIRTUAL , ( ) => { incrementAndEnd ( ) ; } ) ; nodeB . on ( NODE_EVENTS . UPDATE_STATE , async ( ) => { incrementAndEnd ( ) ; } ) ; const takeActionReq = ( appId ) => constructTakeActionRpc ( appId , validAction ) ; const uninstallReq = ( appId ) => constructUninstallVirtualRpc ( appId , nodeB . publicIdentifier ) ; nodeB . rpcRouter . dispatch ( takeActionReq ( appId ) ) ; nodeA . rpcRouter . dispatch ( uninstallReq ( virtualId ) ) ; } ) ; } ) ;