'use strict' ; import * as chai from 'chai' ; import * as sinon from 'sinon' ; import * as sinonChai from 'sinon-chai' ; import * as chaiAsPromised from 'chai-as-promised' ; import { BlockchainWalletExplorerProvider } from '../../extension/explorer/walletExplorer' ; import { BlockchainGatewayExplorerProvider } from '../../extension/explorer/gatewayExplorer' ; import { BlockchainEnvironmentExplorerProvider } from '../../extension/explorer/environmentExplorer' ; import { BlockchainPackageExplorerProvider } from '../../extension/explorer/packageExplorer' ; import { ExtensionUtil } from '../../extension/util/ExtensionUtil' ; chai . use ( sinonChai ) ; chai . use ( chaiAsPromised ) ; const should = chai . should ( ) ; export enum LanguageType { CHAINCODE = 'chaincode' , CONTRACT = 'contract' } module . exports = function ( ) : any { this . Then ( /^there (should|shouldn't) be an? (environment connected |committed smart contract |channel |Node |Organizations |identity )?tree item with a label '(.*?)' in the '(Smart Contracts|Fabric Environments|Fabric Gateways|Fabric Wallets)' panel( for item| for the current tree item)?( .*)?$/ , this . timeout , async ( shouldOrshouldnt , child , label , panel , thing2 , thing ) => { let treeItems = [ ] ; if ( panel === 'Smart Contracts' ) { const blockchainPackageExplorerProvider = ExtensionUtil . getBlockchainPackageExplorerProvider ( ) ; treeItems = await blockchainPackageExplorerProvider . getChildren ( ) ; } else if ( panel === 'Fabric Environments' ) { const blockchainRuntimeExplorerProvider = ExtensionUtil . getBlockchainEnvironmentExplorerProvider ( ) ; if ( ! child ) { treeItems = await blockchainRuntimeExplorerProvider . getChildren ( ) ; } else if ( child . includes ( 'environment connected' ) ) { const allTreeItems = await blockchainRuntimeExplorerProvider . getChildren ( ) ; treeItems = [ allTreeItems [ 0 ] ] ; } else if ( child . includes ( 'channel' ) ) { const allTreeItems = await blockchainRuntimeExplorerProvider . getChildren ( ) ; for ( let i = 1 ; i < allTreeItems . length - 2 ; i ++ ) { treeItems . push ( allTreeItems [ i ] ) ; } } else if ( child . includes ( 'committed smart contract' ) ) { const allTreeItems = await blockchainRuntimeExplorerProvider . getChildren ( ) ; const channelItemIndex = allTreeItems . findIndex ( ( item ) => { return thing . trim ( ) === item . label ; } ) ; treeItems = await blockchainRuntimeExplorerProvider . getChildren ( allTreeItems [ channelItemIndex ] ) ; } else if ( child . includes ( 'Node' ) ) { const allTreeItems = await blockchainRuntimeExplorerProvider . getChildren ( ) ; treeItems = await blockchainRuntimeExplorerProvider . getChildren ( allTreeItems [ allTreeItems . length - 2 ] ) ; } else if ( child . includes ( 'Organizations' ) ) { const allTreeItems = await blockchainRuntimeExplorerProvider . getChildren ( ) ; treeItems = await blockchainRuntimeExplorerProvider . getChildren ( allTreeItems [ allTreeItems . length - 1 ] ) ; } else { treeItems = await blockchainRuntimeExplorerProvider . getChildren ( ) ; } } else if ( panel === 'Fabric Gateways' ) { const blockchainGatewayExplorerProvider = ExtensionUtil . getBlockchainGatewayExplorerProvider ( ) ; treeItems = await blockchainGatewayExplorerProvider . getChildren ( ) ; } else if ( panel === 'Fabric Wallets' ) { const blockchainWalletExplorerProvider = ExtensionUtil . getBlockchainWalletExplorerProvider ( ) ; treeItems = await blockchainWalletExplorerProvider . getChildren ( ) ; if ( thing2 && thing2 . includes ( 'for the current tree item' ) ) { treeItems = await blockchainWalletExplorerProvider . getChildren ( this . treeItem ) ; } else if ( child && child . includes ( 'identity' ) && thing && thing2 ) { const walletIndex = treeItems . findIndex ( ( item ) => { return item . label === thing . trim ( ) ; } ) ; if ( walletIndex < 0 ) { throw new Error ( 'Name of thing doesn\'t exist' ) ; } treeItems = await blockchainWalletExplorerProvider . getChildren ( treeItems [ walletIndex ] ) ; } } else { throw new Error ( 'Name of panel doesn\'t exist' ) ; } const treeItem = treeItems . find ( ( item ) => { return item . label === label ; } ) ; if ( shouldOrshouldnt === 'should' ) { should . exist ( treeItem ) ; this . treeItem = treeItem ; } else { should . not . exist ( treeItem ) ; } } ) ; this . Then ( /^the '(Smart Contracts|Fabric Environments|Fabric Gateways|Fabric Wallets)' tree item should have a child '(.*)'$/ , this . timeout , async ( panel , childName ) => { let children ; if ( panel === 'Fabric Wallets' ) { const blockchainWalletExplorerProvider = ExtensionUtil . getBlockchainWalletExplorerProvider ( ) ; const allTreeItems = await blockchainWalletExplorerProvider . getChildren ( ) ; const parent = allTreeItems . find ( ( item ) => { return item . label === this . treeItem . label ; } ) ; children = await blockchainWalletExplorerProvider . getChildren ( parent ) ; } else if ( panel === 'Fabric Gateways' ) { const blockchainGatewayExplorerProvider = ExtensionUtil . getBlockchainGatewayExplorerProvider ( ) ; const allTreeItems = await blockchainGatewayExplorerProvider . getChildren ( ) ; const parent = allTreeItems . find ( ( item ) => { return item . label === this . treeItem . label ; } ) ; children = await blockchainGatewayExplorerProvider . getChildren ( parent ) ; } else if ( panel === 'Fabric Environments' ) { const blockchainEnvironmentExplorerProvider = ExtensionUtil . getBlockchainEnvironmentExplorerProvider ( ) ; const allTreeItems = await blockchainEnvironmentExplorerProvider . getChildren ( ) ; const parent = allTreeItems . find ( ( item ) => { return item . label === this . treeItem . label ; } ) ; children = await blockchainEnvironmentExplorerProvider . getChildren ( parent ) ; } this . treeItem = children . find ( ( child ) => { return child . label === childName ; } ) ; } ) ; this . Then ( /the tree item should have a tooltip (equal|including) to '(.*)'/ , this . timeout , async ( operator , tooltipValue ) => { tooltipValue = tooltipValue . replace ( /\\n/g , `\n` ) ; if ( operator === 'equal' ) { this . treeItem . tooltip . should . equal ( tooltipValue ) ; } else if ( operator === 'including' ) { this . treeItem . tooltip . includes ( tooltipValue ) . should . equal ( true ) ; } else { throw new Error ( 'Unknown operator for step.' ) ; } } ) ; this . Then ( /the file size should be greater than '(.*)' KB/ , this . timeout , async ( value ) => { const regex = /File size: (.*?) KB/ ; const fileSize = this . treeItem . tooltip . match ( regex ) [ 1 ] ; ( fileSize >= value ) . should . equal ( true ) ; } ) ; this . Then ( "the logger should have been called with '{string}', '{string}' and '{string}'" , this . timeout , async ( type , popupMessage , outputMessage ) => { if ( popupMessage === 'undefined' ) { popupMessage = undefined ; } this . userInputUtilHelper . logSpy . should . have . been . calledWith ( type , popupMessage , outputMessage ) ; } ) ; this . Then ( "the log should have been called with '{string}' and '{string}'" , this . timeout , async ( type , popupMessage ) => { this . userInputUtilHelper . logSpy . should . have . been . calledWith ( type , sinon . match ( popupMessage ) ) ; } ) ; } ;