'use strict' ; import * as vscode from 'vscode' ; import * as chai from 'chai' ; import * as sinon from 'sinon' ; import * as sinonChai from 'sinon-chai' ; import { TestUtil } from '../TestUtil' ; import { UserInputUtil } from '../../extension/commands/UserInputUtil' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { FabricEnvironmentConnection } from 'ibm-blockchain-platform-environment-v1' ; import { LogType , FabricGatewayRegistry } from 'ibm-blockchain-platform-common' ; import { FabricEnvironmentManager } from '../../extension/fabric/environments/FabricEnvironmentManager' ; import { VSCodeBlockchainOutputAdapter } from '../../extension/logging/VSCodeBlockchainOutputAdapter' ; import { FabricDebugConfigurationProvider } from '../../extension/debug/FabricDebugConfigurationProvider' ; chai . should ( ) ; chai . use ( sinonChai ) ; describe ( 'DebugCommandListCommand' , ( ) => { let mySandBox ; let showDebugCommandListStub ; let executeCommandStub ; let runtimeStub ; let connectToGatewayStub ; let environmentConnectionStub ; let activeDebugSession ; let activeDebugSessionStub ; before ( async ( ) => { mySandBox = sinon . createSandbox ( ) ; await TestUtil . setupTests ( mySandBox ) ; await TestUtil . setupLocalFabric ( ) ; } ) ; beforeEach ( async ( ) => { await FabricGatewayRegistry . instance ( ) . clear ( ) ; runtimeStub = mySandBox . createStubInstance ( FabricEnvironmentConnection ) ; showDebugCommandListStub = mySandBox . stub ( UserInputUtil , 'showDebugCommandList' ) . resolves ( { label : 'Instantiate smart contract' , data : ExtensionCommands . SUBMIT_TRANSACTION } ) ; executeCommandStub = mySandBox . stub ( vscode . commands , 'executeCommand' ) ; executeCommandStub . callThrough ( ) ; executeCommandStub . withArgs ( ExtensionCommands . INSTANTIATE_SMART_CONTRACT ) . resolves ( ) ; executeCommandStub . withArgs ( ExtensionCommands . SUBMIT_TRANSACTION ) . resolves ( ) ; executeCommandStub . withArgs ( ExtensionCommands . EVALUATE_TRANSACTION ) . resolves ( ) ; executeCommandStub . withArgs ( ExtensionCommands . UPGRADE_SMART_CONTRACT ) . resolves ( ) ; activeDebugSession = { configuration : { env : { CORE_CHAINCODE_ID_NAME : 'mySmartContract:vscode-debug-123456' } , debugEvent : FabricDebugConfigurationProvider . debugEvent } } ; activeDebugSessionStub = mySandBox . stub ( vscode . debug , 'activeDebugSession' ) . value ( activeDebugSession ) ; const channelMap = new Map < string , string [ ] > ( ) ; channelMap . set ( 'mychannel' , [ 'peerOne' ] ) ; runtimeStub . createChannelMap . resolves ( channelMap ) ; runtimeStub . getCommittedSmartContractDefinitions . resolves ( [ ] ) ; environmentConnectionStub = mySandBox . stub ( FabricEnvironmentManager . instance ( ) , 'getConnection' ) . returns ( runtimeStub ) ; connectToGatewayStub = mySandBox . stub ( FabricDebugConfigurationProvider , 'connectToGateway' ) ; connectToGatewayStub . resolves ( true ) ; } ) ; afterEach ( async ( ) => { mySandBox . restore ( ) ; } ) ; it ( 'should show the submit and evaluate' , async ( ) => { await vscode . commands . executeCommand ( ExtensionCommands . DEBUG_COMMAND_LIST ) ; connectToGatewayStub . should . have . been . calledOnce ; executeCommandStub . should . have . been . calledWith ( ExtensionCommands . SUBMIT_TRANSACTION , undefined , 'mychannel' , 'mySmartContract' ) ; } ) ; it ( 'should return if not debugging a smart contract' , async ( ) => { activeDebugSession . configuration . debugEvent = undefined ; activeDebugSessionStub . value = activeDebugSession ; const logSpy = mySandBox . spy ( VSCodeBlockchainOutputAdapter . instance ( ) , 'log' ) ; await vscode . commands . executeCommand ( ExtensionCommands . DEBUG_COMMAND_LIST ) ; connectToGatewayStub . should . not . have . been . called ; logSpy . should . have . been . calledWith ( LogType . ERROR , undefined , 'The current debug session is not debugging a smart contract, this command can only be run when debugging a smart contract' ) ; } ) ; it ( 'should return if no environment connection' , async ( ) => { const logSpy = mySandBox . spy ( VSCodeBlockchainOutputAdapter . instance ( ) , 'log' ) ; environmentConnectionStub . returns ( undefined ) ; await vscode . commands . executeCommand ( ExtensionCommands . DEBUG_COMMAND_LIST ) ; connectToGatewayStub . should . not . have . been . called ; logSpy . should . have . been . calledWith ( LogType . ERROR , undefined , 'No connection to a blockchain found' ) ; executeCommandStub . should . not . have . been . calledWith ( ExtensionCommands . INSTANTIATE_SMART_CONTRACT ) ; } ) ; it ( 'should handle cancel' , async ( ) => { showDebugCommandListStub . resolves ( ) ; await vscode . commands . executeCommand ( ExtensionCommands . DEBUG_COMMAND_LIST ) ; showDebugCommandListStub . should . have . been . called ; executeCommandStub . should . have . been . calledOnceWithExactly ( ExtensionCommands . DEBUG_COMMAND_LIST ) ; connectToGatewayStub . should . not . have . been . called ; } ) ; it ( 'should submit transaction with channel name and contract name' , async ( ) => { showDebugCommandListStub . resolves ( { label : 'Submit transaction' , data : ExtensionCommands . SUBMIT_TRANSACTION } ) ; await vscode . commands . executeCommand ( ExtensionCommands . DEBUG_COMMAND_LIST ) ; connectToGatewayStub . should . have . been . calledOnce ; executeCommandStub . should . have . been . calledWithExactly ( ExtensionCommands . SUBMIT_TRANSACTION , undefined , 'mychannel' , 'mySmartContract' ) ; } ) ; it ( 'should evaluate transaction with channel name and contract name' , async ( ) => { showDebugCommandListStub . resolves ( { label : 'Evaluate transaction' , data : ExtensionCommands . EVALUATE_TRANSACTION } ) ; await vscode . commands . executeCommand ( ExtensionCommands . DEBUG_COMMAND_LIST ) ; connectToGatewayStub . should . have . been . calledOnce ; executeCommandStub . should . have . been . calledWithExactly ( ExtensionCommands . EVALUATE_TRANSACTION , undefined , 'mychannel' , 'mySmartContract' ) ; } ) ; it ( 'should connect to Org1 gateway before running the submit or evaluate commands' , async ( ) => { showDebugCommandListStub . resolves ( { label : 'Evaluate transaction' , data : ExtensionCommands . EVALUATE_TRANSACTION } ) ; await vscode . commands . executeCommand ( ExtensionCommands . DEBUG_COMMAND_LIST ) ; connectToGatewayStub . should . have . been . calledOnce ; executeCommandStub . should . have . been . calledWithExactly ( ExtensionCommands . EVALUATE_TRANSACTION , undefined , 'mychannel' , 'mySmartContract' ) ; } ) ; it ( 'should return if connecting to gateway fails' , async ( ) => { showDebugCommandListStub . resolves ( { label : 'Evaluate transaction' , data : ExtensionCommands . EVALUATE_TRANSACTION } ) ; connectToGatewayStub . resolves ( false ) ; await vscode . commands . executeCommand ( ExtensionCommands . DEBUG_COMMAND_LIST ) ; connectToGatewayStub . should . have . been . calledOnce ; executeCommandStub . should . not . have . been . calledWith ( ExtensionCommands . EVALUATE_TRANSACTION ) ; } ) ; it ( 'should run the command passed in' , async ( ) => { runtimeStub . getCommittedSmartContractDefinitions . resolves ( [ { name : 'mySmartContract' , version : 'vscode-debug-13232112018' } , { name : 'cake-network' , version : '0.0.2' } ] ) ; await vscode . commands . executeCommand ( ExtensionCommands . DEBUG_COMMAND_LIST , ExtensionCommands . UPGRADE_SMART_CONTRACT ) ; showDebugCommandListStub . should . not . have . been . called ; connectToGatewayStub . should . not . have . been . called ; executeCommandStub . should . have . been . calledWith ( ExtensionCommands . UPGRADE_SMART_CONTRACT , undefined , 'mychannel' , [ 'peerOne' ] ) ; } ) ; } ) ;