'use strict' ; import * as vscode from 'vscode' ; import { BlockchainPackageExplorerProvider } from '../../extension/explorer/packageExplorer' ; import { PackageRegistry } from '../../extension/registries/PackageRegistry' ; import { PackageRegistryEntry } from '../../extension/registries/PackageRegistryEntry' ; import { TestUtil } from '../TestUtil' ; import * as chai from 'chai' ; import * as sinon from 'sinon' ; import * as sinonChai from 'sinon-chai' ; import * as path from 'path' ; import { PackageTreeItem } from '../../extension/explorer/model/PackageTreeItem' ; import { BlockchainTreeItem } from '../../extension/explorer/model/BlockchainTreeItem' ; import { VSCodeBlockchainOutputAdapter } from '../../extension/logging/VSCodeBlockchainOutputAdapter' ; import { LogType } from 'ibm-blockchain-platform-common' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { Reporter } from '../../extension/util/Reporter' ; import { SettingConfigurations } from '../../extension/configurations' ; import { ExtensionUtil } from '../../extension/util/ExtensionUtil' ; import { ListFilesInPackage } from 'ibm-blockchain-platform-environment-v1' ; import { UserInputUtil } from '../../extension/commands/UserInputUtil' ; chai . should ( ) ; chai . use ( sinonChai ) ; describe ( 'viewPackageInformationCommand' , ( ) => { const sandbox = sinon . createSandbox ( ) ; let blockchainPackageExplorerProvider ; let showSpy ; let logSpy ; let sendTelemetryEventStub ; let showSmartContractPackagesQuickPickBoxStub ; let listFilesStub ; let packagetreeItem ; let packages ; const TEST_PACKAGE_DIRECTORY = path . join ( path . dirname ( __dirname ) , '../../test/data/packageDir' ) ; before ( async ( ) => { await TestUtil . setupTests ( sandbox ) ; await vscode . workspace . getConfiguration ( ) . update ( SettingConfigurations . EXTENSION_DIRECTORY , TEST_PACKAGE_DIRECTORY , vscode . ConfigurationTarget . Global ) ; } ) ; beforeEach ( async ( ) => { logSpy = sandbox . stub ( VSCodeBlockchainOutputAdapter . instance ( ) , 'log' ) ; showSpy = sandbox . stub ( VSCodeBlockchainOutputAdapter . instance ( ) , 'show' ) ; sendTelemetryEventStub = sandbox . stub ( Reporter . instance ( ) , 'sendTelemetryEvent' ) ; blockchainPackageExplorerProvider = ExtensionUtil . getBlockchainPackageExplorerProvider ( ) ; packages = await blockchainPackageExplorerProvider . getChildren ( ) ; packagetreeItem = packages [ 2 ] as PackageTreeItem ; const allPackages = await PackageRegistry . instance ( ) . getAll ( ) ; showSmartContractPackagesQuickPickBoxStub = sandbox . stub ( UserInputUtil , 'showSmartContractPackagesQuickPickBox' ) . resolves ( { label : allPackages [ 1 ] . name , data : allPackages [ 1 ] } ) ; listFilesStub = sandbox . stub ( ListFilesInPackage , 'listFiles' ) ; listFilesStub . callThrough ( ) ; } ) ; afterEach ( async ( ) => { sandbox . restore ( ) ; } ) ; after ( async ( ) => { await vscode . workspace . getConfiguration ( ) . update ( SettingConfigurations . EXTENSION_DIRECTORY , TestUtil . EXTENSION_TEST_DIR , vscode . ConfigurationTarget . Global ) ; } ) ; it ( 'should display package information when user issues command from command palette' , async ( ) => { await vscode . commands . executeCommand ( ExtensionCommands . VIEW_PACKAGE_INFORMATION ) ; showSmartContractPackagesQuickPickBoxStub . should . have . been . called ; listFilesStub . should . have . been . called ; logSpy . getCall ( 0 ) . should . have . been . calledWith ( LogType . INFO , undefined , 'viewPackageInformation' ) ; logSpy . getCall ( 1 ) . should . have . been . calledWith ( LogType . INFO , undefined , `Found 3 file(s) in smart contract package fabcar-java:` ) ; logSpy . getCall ( 2 ) . should . have . been . calledWith ( LogType . INFO , undefined , '- metadata.json' ) ; logSpy . getCall ( 3 ) . should . have . been . calledWith ( LogType . INFO , undefined , '- src/fabcar-1.0-SNAPSHOT.jar' ) ; logSpy . getCall ( 4 ) . should . have . been . calledWith ( LogType . INFO , undefined , '- src/lib/genson-1.5.jar' ) ; logSpy . getCall ( 5 ) . should . have . been . calledWith ( LogType . SUCCESS , `Displayed information for smart contract package fabcar-java.` ) ; showSpy . should . have . been . calledOnce ; sendTelemetryEventStub . should . have . been . calledOnceWithExactly ( 'viewPackageInformationCommand' ) ; } ) ; it ( 'should display package information from treeItem' , async ( ) => { await vscode . commands . executeCommand ( ExtensionCommands . VIEW_PACKAGE_INFORMATION , packagetreeItem ) ; showSmartContractPackagesQuickPickBoxStub . should . not . have . been . called ; listFilesStub . should . have . been . called ; logSpy . getCall ( 0 ) . should . have . been . calledWith ( LogType . INFO , undefined , 'viewPackageInformation' ) ; logSpy . getCall ( 1 ) . should . have . been . calledWith ( LogType . INFO , undefined , `Found 8 file(s) in smart contract package fabcar-javascript@0.0.1:` ) ; logSpy . getCall ( 2 ) . should . have . been . calledWith ( LogType . INFO , undefined , '- metadata.json' ) ; logSpy . getCall ( 3 ) . should . have . been . calledWith ( LogType . INFO , undefined , '- src/.editorconfig' ) ; logSpy . getCall ( 4 ) . should . have . been . calledWith ( LogType . INFO , undefined , '- src/.eslintignore' ) ; logSpy . getCall ( 5 ) . should . have . been . calledWith ( LogType . INFO , undefined , '- src/.eslintrc.js' ) ; logSpy . getCall ( 6 ) . should . have . been . calledWith ( LogType . INFO , undefined , '- src/.gitignore' ) ; logSpy . getCall ( 7 ) . should . have . been . calledWith ( LogType . INFO , undefined , '- src/index.js' ) ; logSpy . getCall ( 8 ) . should . have . been . calledWith ( LogType . INFO , undefined , '- src/lib/fabcar.js' ) ; logSpy . getCall ( 9 ) . should . have . been . calledWith ( LogType . INFO , undefined , '- src/package.json' ) ; logSpy . getCall ( 10 ) . should . have . been . calledWith ( LogType . SUCCESS , `Displayed information for smart contract package fabcar-javascript@0.0.1.` ) ; showSpy . should . have . been . calledOnce ; sendTelemetryEventStub . should . have . been . calledOnceWithExactly ( 'viewPackageInformationCommand' ) ; } ) ; it ( 'should handle user canceling when chosing a contract' , async ( ) => { showSmartContractPackagesQuickPickBoxStub . resolves ( ) ; await vscode . commands . executeCommand ( ExtensionCommands . VIEW_PACKAGE_INFORMATION ) ; showSmartContractPackagesQuickPickBoxStub . should . have . been . called ; listFilesStub . should . have . not . been . called ; logSpy . getCall ( 0 ) . should . have . been . calledWith ( LogType . INFO , undefined , 'viewPackageInformation' ) ; showSpy . should . not . have . been . called ; } ) ; it ( 'should handle error displaying package information' , async ( ) => { const error = new Error ( 'some error' ) ; const caughtError = new Error ( `Unable to extract file list from fabcar-javascript@0.0.1: ${ error . message }` ) ; listFilesStub . reset ( ) ; listFilesStub . rejects ( error ) ; await vscode . commands . executeCommand ( ExtensionCommands . VIEW_PACKAGE_INFORMATION , packagetreeItem ) ; showSmartContractPackagesQuickPickBoxStub . should . have . not . been . called ; listFilesStub . should . have . been . called ; logSpy . getCall ( 0 ) . should . have . been . calledWith ( LogType . INFO , undefined , 'viewPackageInformation' ) ; logSpy . should . have . been . calledWith ( LogType . ERROR , caughtError . message , caughtError . toString ( ) ) ; showSpy . should . not . have . been . called ; } ) ; } ) ;