'use strict' ; import * as vscode from 'vscode' ; import * as path from 'path' ; import * as chai from 'chai' ; import * as sinon from 'sinon' ; import * as sinonChai from 'sinon-chai' ; import { TestUtil } from '../TestUtil' ; import { PackageTreeItem } from '../../extension/explorer/model/PackageTreeItem' ; import { PackageRegistry } from '../../extension/registries/PackageRegistry' ; import { PackageRegistryEntry } from '../../extension/registries/PackageRegistryEntry' ; import { BlockchainTreeItem } from '../../extension/explorer/model/BlockchainTreeItem' ; import { BlockchainPackageExplorerProvider } from '../../extension/explorer/packageExplorer' ; import { UserInputUtil } from '../../extension/commands/UserInputUtil' ; import { VSCodeBlockchainOutputAdapter } from '../../extension/logging/VSCodeBlockchainOutputAdapter' ; import { LogType } from 'ibm-blockchain-platform-common' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { SettingConfigurations } from '../../extension/configurations' ; import { ExtensionUtil } from '../../extension/util/ExtensionUtil' ; import { DeployView } from '../../extension/webview/DeployView' ; chai . should ( ) ; chai . use ( sinonChai ) ; describe ( 'DeleteSmartContractPackageCommand' , ( ) => { let showConfirmationWarningMessageStub ; const TEST_EXTENSION_DIRECTORY = path . join ( path . dirname ( __dirname ) , '../../test/data' ) ; let mySandBox ; before ( async ( ) => { mySandBox = sinon . createSandbox ( ) ; await TestUtil . setupTests ( mySandBox ) ; await vscode . workspace . getConfiguration ( ) . update ( SettingConfigurations . EXTENSION_DIRECTORY , TEST_EXTENSION_DIRECTORY , vscode . ConfigurationTarget . Global ) ; } ) ; describe ( 'deleteSmartContractPackage' , ( ) => { let _package ; let logStub ; let packagesStub ; beforeEach ( async ( ) => { DeployView . panel = undefined ; mySandBox . restore ( ) ; showConfirmationWarningMessageStub = mySandBox . stub ( UserInputUtil , 'showConfirmationWarningMessage' ) . resolves ( true ) ; packagesStub = mySandBox . stub ( PackageRegistry . instance ( ) , 'getAll' ) ; _package = new PackageRegistryEntry ( ) ; _package . name = 'myPackage' ; _package . path = 'myPath' ; _package . version = '0.0.1' ; packagesStub . resolves ( [ _package ] ) ; logStub = mySandBox . stub ( VSCodeBlockchainOutputAdapter . instance ( ) , 'log' ) . resolves ( ) ; } ) ; afterEach ( async ( ) => { mySandBox . restore ( ) ; } ) ; it ( "should test a 'smart contract package' can be deleted from the command" , async ( ) => { const blockchainPackageExplorerProvider = ExtensionUtil . getBlockchainPackageExplorerProvider ( ) ; const onDidChangeTreeDataSpy = mySandBox . spy ( blockchainPackageExplorerProvider [ '_onDidChangeTreeData' ] , 'fire' ) ; mySandBox . stub ( UserInputUtil , 'showSmartContractPackagesQuickPickBox' ) . resolves ( [ { label : 'vscode-pkg-1@0.0.1' , data : _package } ] ) ; const deleteStub = mySandBox . stub ( PackageRegistry . instance ( ) , 'delete' ) . resolves ( ) ; await vscode . commands . executeCommand ( ExtensionCommands . DELETE_SMART_CONTRACT ) ; onDidChangeTreeDataSpy . should . have . been . called ; deleteStub . should . have . been . calledOnceWithExactly ( _package ) ; logStub . getCall ( 0 ) . should . have . been . calledWithExactly ( LogType . INFO , undefined , `deleteSmartContractPackage` ) ; logStub . getCall ( 1 ) . should . have . been . calledWithExactly ( LogType . SUCCESS , `Successfully deleted ${ _package . name } package` ) ; } ) ; it ( `should test multiple 'smart contract packages' can be deleted from the command` , async ( ) => { const blockchainPackageExplorerProvider = ExtensionUtil . getBlockchainPackageExplorerProvider ( ) ; const onDidChangeTreeDataSpy = mySandBox . spy ( blockchainPackageExplorerProvider [ '_onDidChangeTreeData' ] , 'fire' ) ; const packages = await PackageRegistry . instance ( ) . getAll ( ) ; const dataOne = packages [ 0 ] ; const dataTwo = packages [ 1 ] ; mySandBox . stub ( UserInputUtil , 'showSmartContractPackagesQuickPickBox' ) . resolves ( [ { label : 'vscode-pkg-1@0.0.1' , data : dataOne } , { label : 'vscode-pkg-1@0.0.2' , data : dataTwo } ] ) ; const deleteStub = mySandBox . stub ( PackageRegistry . instance ( ) , 'delete' ) . resolves ( ) ; await vscode . commands . executeCommand ( ExtensionCommands . DELETE_SMART_CONTRACT ) ; onDidChangeTreeDataSpy . should . have . been . called ; deleteStub . should . have . been . calledTwice ; deleteStub . should . have . been . calledWithExactly ( dataOne ) ; deleteStub . should . have . been . calledWithExactly ( dataTwo ) ; logStub . getCall ( 0 ) . should . have . been . calledWithExactly ( LogType . INFO , undefined , `deleteSmartContractPackage` ) ; logStub . getCall ( 1 ) . should . have . been . calledWithExactly ( LogType . SUCCESS , `Successfully deleted packages` ) ; } ) ; it ( "should test a 'smart contract package' can be deleted from tree" , async ( ) => { const blockchainPackageExplorerProvider = ExtensionUtil . getBlockchainPackageExplorerProvider ( ) ; const initialPackages = await blockchainPackageExplorerProvider . getChildren ( ) ; const onDidChangeTreeDataSpy = mySandBox . spy ( blockchainPackageExplorerProvider [ '_onDidChangeTreeData' ] , 'fire' ) ; const packageToDelete = initialPackages [ 0 ] as PackageTreeItem ; const deleteStub = mySandBox . stub ( PackageRegistry . instance ( ) , 'delete' ) . resolves ( ) ; await vscode . commands . executeCommand ( ExtensionCommands . DELETE_SMART_CONTRACT , packageToDelete ) ; onDidChangeTreeDataSpy . should . have . been . called ; deleteStub . should . have . been . calledOnceWithExactly ( packageToDelete . packageEntry ) ; logStub . getCall ( 0 ) . should . have . been . calledWithExactly ( LogType . INFO , undefined , `deleteSmartContractPackage` ) ; logStub . getCall ( 1 ) . should . have . been . calledWithExactly ( LogType . SUCCESS , `Successfully deleted ${ _package . name } package` ) ; } ) ; it ( `should test deleting smart contract package can be cancelled` , async ( ) => { const blockchainPackageExplorerProvider = ExtensionUtil . getBlockchainPackageExplorerProvider ( ) ; const initialPackages = await blockchainPackageExplorerProvider . getChildren ( ) ; const initialLength = initialPackages . length ; mySandBox . stub ( UserInputUtil , 'showSmartContractPackagesQuickPickBox' ) . resolves ( undefined ) ; await vscode . commands . executeCommand ( ExtensionCommands . DELETE_SMART_CONTRACT ) ; const newPackageList = await blockchainPackageExplorerProvider . getChildren ( ) ; newPackageList . length . should . equal ( initialLength ) ; newPackageList . should . deep . equal ( initialPackages ) ; logStub . should . have . been . calledOnceWithExactly ( LogType . INFO , undefined , `deleteSmartContractPackage` ) ; } ) ; it ( 'should stop if user doesn\'t select to continue' , async ( ) => { mySandBox . stub ( UserInputUtil , 'showSmartContractPackagesQuickPickBox' ) . resolves ( [ { label : 'vscode-pkg-1@0.0.1' , data : _package } ] ) ; showConfirmationWarningMessageStub . resolves ( false ) ; const deleteSpy = mySandBox . spy ( PackageRegistry . instance ( ) , 'delete' ) ; await vscode . commands . executeCommand ( ExtensionCommands . DELETE_SMART_CONTRACT ) ; deleteSpy . should . not . have . been . called ; logStub . should . have . been . calledOnceWithExactly ( LogType . INFO , undefined , `deleteSmartContractPackage` ) ; } ) ; it ( 'should stop if user doesn\'t pass any smart contract packages to delete' , async ( ) => { mySandBox . stub ( UserInputUtil , 'showSmartContractPackagesQuickPickBox' ) . resolves ( [ ] ) ; const deleteSpy = mySandBox . spy ( PackageRegistry . instance ( ) , 'delete' ) ; await vscode . commands . executeCommand ( ExtensionCommands . DELETE_SMART_CONTRACT ) ; deleteSpy . should . not . have . been . called ; logStub . should . have . been . calledOnceWithExactly ( LogType . INFO , undefined , `deleteSmartContractPackage` ) ; } ) ; it ( `should test that the deploy view packages update (if open)` , async ( ) => { const blockchainPackageExplorerProvider = ExtensionUtil . getBlockchainPackageExplorerProvider ( ) ; const onDidChangeTreeDataSpy = mySandBox . spy ( blockchainPackageExplorerProvider [ '_onDidChangeTreeData' ] , 'fire' ) ; const _packageOne = new PackageRegistryEntry ( ) ; _packageOne . name = 'vscode-pkg-1' ; _packageOne . path = 'myPath' ; _packageOne . version = '0.0.1' ; const _packageTwo = new PackageRegistryEntry ( ) ; _packageTwo . name = 'vscode-pkg-1' ; _packageTwo . path = 'myPath' ; _packageTwo . version = '0.0.2' ; const _packageThree = new PackageRegistryEntry ( ) ; _packageThree . name = 'vscode-pkg-2' ; _packageThree . path = 'myPath' ; _packageThree . version = '0.0.1' ; packagesStub . resolves ( [ _packageOne , _packageTwo , _packageThree ] ) ; mySandBox . stub ( UserInputUtil , 'showSmartContractPackagesQuickPickBox' ) . resolves ( [ { label : 'vscode-pkg-1@0.0.1' , data : _packageOne } , { label : 'vscode-pkg-1@0.0.2' , data : _packageTwo } ] ) ; DeployView . panel = { webview : { } } as unknown as vscode . WebviewPanel ; const updatePackagesStub = mySandBox . stub ( DeployView , 'updatePackages' ) . resolves ( ) ; const deleteStub = mySandBox . stub ( PackageRegistry . instance ( ) , 'delete' ) . resolves ( ) ; await vscode . commands . executeCommand ( ExtensionCommands . DELETE_SMART_CONTRACT ) ; onDidChangeTreeDataSpy . should . have . been . called ; deleteStub . should . have . been . calledTwice ; deleteStub . should . have . been . calledWithExactly ( _packageOne ) ; deleteStub . should . have . been . calledWithExactly ( _packageTwo ) ; logStub . getCall ( 0 ) . should . have . been . calledWithExactly ( LogType . INFO , undefined , `deleteSmartContractPackage` ) ; logStub . getCall ( 1 ) . should . have . been . calledWithExactly ( LogType . SUCCESS , `Successfully deleted packages` ) ; updatePackagesStub . should . have . been . calledOnce ; } ) ; } ) ; } ) ;