'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 * as fs from 'fs-extra' ; import { PackageTreeItem } from '../../extension/explorer/model/PackageTreeItem' ; import { TestUtil } from '../TestUtil' ; import { BlockchainPackageExplorerProvider } from '../../extension/explorer/packageExplorer' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { VSCodeBlockchainOutputAdapter } from '../../extension/logging/VSCodeBlockchainOutputAdapter' ; import { LogType } from 'ibm-blockchain-platform-common' ; import { ExtensionUtil } from '../../extension/util/ExtensionUtil' ; import { PackageRegistry } from '../../extension/registries/PackageRegistry' ; import { PackageRegistryEntry } from '../../extension/registries/PackageRegistryEntry' ; chai . use ( sinonChai ) ; chai . should ( ) ; describe ( 'packageExplorer' , ( ) => { const mySandBox = sinon . createSandbox ( ) ; let logSpy ; let blockchainPackageExplorerProvider ; const rootPath = path . dirname ( __dirname ) ; const testDir = path . join ( rootPath , '../../test/data/packageDir/v2/packages' ) ; const packageDir = path . join ( TestUtil . EXTENSION_TEST_DIR , 'v2' , 'packages' ) ; before ( async ( ) => { await TestUtil . setupTests ( mySandBox ) ; } ) ; beforeEach ( async ( ) => { logSpy = mySandBox . spy ( VSCodeBlockchainOutputAdapter . instance ( ) , 'log' ) ; blockchainPackageExplorerProvider = ExtensionUtil . getBlockchainPackageExplorerProvider ( ) ; await fs . copy ( testDir , packageDir ) ; } ) ; afterEach ( async ( ) => { mySandBox . restore ( ) ; } ) ; it ( 'should show smart contract packages in the BlockchainPackageExplorer view' , async ( ) => { const testPackages = await blockchainPackageExplorerProvider . getChildren ( ) as Array < PackageTreeItem > ; testPackages . length . should . equal ( 6 ) ; testPackages [ 0 ] . label . should . equal ( 'fabcar-go' ) ; testPackages [ 1 ] . label . should . equal ( 'fabcar-java' ) ; testPackages [ 2 ] . label . should . equal ( 'fabcar-javascript@0.0.1' ) ; testPackages [ 3 ] . label . should . equal ( 'fabcar-typescript@1.0.0' ) ; testPackages [ 4 ] . label . should . equal ( 'fabcar-typescript@0.0.2' ) ; testPackages [ 5 ] . label . should . equal ( 'ayy@0.0.1' ) ; testPackages [ 0 ] . description . should . equal ( '(.tgz)' ) ; testPackages [ 1 ] . description . should . equal ( '(.tar.gz)' ) ; testPackages [ 2 ] . description . should . equal ( '(.tar.gz)' ) ; testPackages [ 3 ] . description . should . equal ( '(.cds)' ) ; testPackages [ 4 ] . description . should . equal ( '(.tgz)' ) ; testPackages [ 5 ] . description . should . equal ( '(.cds)' ) ; logSpy . should . not . have . been . calledWith ( LogType . ERROR ) ; } ) ; it ( 'should say that there are no packages' , async ( ) => { await PackageRegistry . instance ( ) . clear ( ) ; const packages = await blockchainPackageExplorerProvider . getChildren ( ) as Array < PackageTreeItem > ; packages . length . should . equal ( 1 ) ; packages [ 0 ] . label . should . equal ( `No packages found` ) ; } ) ; it ( 'should refresh the smart contract packages view when refresh is called' , async ( ) => { const onDidChangeTreeDataSpy = mySandBox . spy ( blockchainPackageExplorerProvider [ '_onDidChangeTreeData' ] , 'fire' ) ; await vscode . commands . executeCommand ( ExtensionCommands . REFRESH_PACKAGES ) ; onDidChangeTreeDataSpy . should . have . been . called ; logSpy . should . not . have . been . calledWith ( LogType . ERROR ) ; } ) ; it ( 'should get a tree item in BlockchainPackageExplorer' , async ( ) => { const testPackages = await blockchainPackageExplorerProvider . getChildren ( ) as Array < PackageTreeItem > ; const firstTestPackage = blockchainPackageExplorerProvider . getTreeItem ( testPackages [ 0 ] ) as PackageTreeItem ; firstTestPackage . label . should . equal ( 'fabcar-go' ) ; firstTestPackage . description . should . equal ( '(.tgz)' ) ; firstTestPackage . tooltip . should . equal ( 'fabcar-go (.tgz)\nFile size: 2359 KB' ) ; logSpy . should . not . have . been . calledWith ( LogType . ERROR ) ; } ) ; it ( 'should show Fabric V1 contracts (endsWith .cds)' , async ( ) => { const newPackage = new PackageRegistryEntry ( { name : 'smartContractPackageBlue' , version : '0.0.1' , path : 'smartContractPackageBlue@0.0.1.cds' , sizeKB : 12 } ) ; mySandBox . stub ( PackageRegistry . instance ( ) , 'getAll' ) . resolves ( [ newPackage ] ) ; const testPackages = await blockchainPackageExplorerProvider . getChildren ( ) as Array < PackageTreeItem > ; testPackages . length . should . equal ( 1 ) ; testPackages [ 0 ] . label . should . equal ( 'smartContractPackageBlue@0.0.1' ) ; testPackages [ 0 ] . description . should . equal ( '(.cds)' ) ; } ) ; } ) ;