import * as chai from 'chai' ; import * as sinon from 'sinon' ; import { TestUtil } from '../../TestUtil' ; import { EnvironmentFactory } from '../../../extension/fabric/environments/EnvironmentFactory' ; import { FabricRuntimeUtil , FabricEnvironmentRegistryEntry , EnvironmentType , FabricEnvironment , MicrofabEnvironment } from 'ibm-blockchain-platform-common' ; import { LocalMicroEnvironment } from '../../../extension/fabric/environments/LocalMicroEnvironment' ; import { LocalMicroEnvironmentManager } from '../../../extension/fabric/environments/LocalMicroEnvironmentManager' ; import { LocalEnvironment } from '../../../extension/fabric/environments/LocalEnvironment' ; import { UserInputUtil } from '../../../extension/commands/UserInputUtil' ; chai . should ( ) ; describe ( 'EnvironmentFactory' , ( ) => { let sandbox ; before ( async ( ) => { await TestUtil . setupTests ( sandbox ) ; } ) ; beforeEach ( async ( ) => { sandbox = sinon . createSandbox ( ) ; } ) ; afterEach ( async ( ) => { sandbox . restore ( ) ; } ) ; it ( 'should throw an error if no name is provided' , async ( ) => { const registryEntry = new FabricEnvironmentRegistryEntry ( ) ; try { EnvironmentFactory . getEnvironment ( registryEntry ) ; } catch ( error ) { error . message . should . equal ( 'Unable to get environment, a name must be provided' ) ; } } ) ; it ( `should return a local environment` , async ( ) => { const registryEntry = new FabricEnvironmentRegistryEntry ( ) ; registryEntry . name = 'oldlocal' ; registryEntry . managedRuntime = true ; registryEntry . environmentType = EnvironmentType . LOCAL_ENVIRONMENT ; registryEntry . environmentDirectory = '/some/path' ; const environment = EnvironmentFactory . getEnvironment ( registryEntry ) ; environment . should . be . an . instanceOf ( LocalEnvironment ) ; } ) ; it ( `should return a local microfab environment` , async ( ) => { await TestUtil . startLocalFabric ( ) ; const registryEntry = new FabricEnvironmentRegistryEntry ( ) ; registryEntry . name = FabricRuntimeUtil . LOCAL_FABRIC ; registryEntry . managedRuntime = true ; registryEntry . environmentType = EnvironmentType . LOCAL_MICROFAB_ENVIRONMENT ; registryEntry . environmentDirectory = '/some/path' ; const localEnvironment = new LocalMicroEnvironment ( registryEntry . name , 8080 , 1 , UserInputUtil . V2_0 ) ; const getRuntimeStub = sandbox . stub ( LocalMicroEnvironmentManager . instance ( ) , 'getRuntime' ) . returns ( localEnvironment ) ; const environment = EnvironmentFactory . getEnvironment ( registryEntry ) ; environment . should . be . an . instanceOf ( LocalMicroEnvironment ) ; getRuntimeStub . should . have . been . calledOnce ; } ) ; it ( 'should return a remote fabric environment' , async ( ) => { const registryEntry = new FabricEnvironmentRegistryEntry ( ) ; registryEntry . name = 'fabricEnvironment' ; registryEntry . managedRuntime = false ; registryEntry . environmentType = EnvironmentType . ENVIRONMENT ; const environment = EnvironmentFactory . getEnvironment ( registryEntry ) ; environment . should . be . an . instanceOf ( FabricEnvironment ) ; } ) ; it ( 'should return a remote fabric environment if only the name is passed' , async ( ) => { const registryEntry = new FabricEnvironmentRegistryEntry ( ) ; registryEntry . name = 'fabricEnvironment' ; const environment = EnvironmentFactory . getEnvironment ( registryEntry ) ; environment . should . be . an . instanceOf ( FabricEnvironment ) ; } ) ; it ( 'should return a Microfab environment' , async ( ) => { const registryEntry = new FabricEnvironmentRegistryEntry ( ) ; registryEntry . name = 'microfabEnvironment' ; registryEntry . managedRuntime = false ; registryEntry . environmentType = EnvironmentType . MICROFAB_ENVIRONMENT ; registryEntry . url = 'http://someurl:9001' ; const environment = EnvironmentFactory . getEnvironment ( registryEntry ) ; environment . should . be . an . instanceOf ( MicrofabEnvironment ) ; } ) ; it ( `should return a local environment when old v1 ansible environment` , async ( ) => { const registryEntry = new FabricEnvironmentRegistryEntry ( ) ; registryEntry . name = 'oldlocal' ; registryEntry . managedRuntime = true ; registryEntry . environmentType = EnvironmentType . MANAGED ; registryEntry . environmentDirectory = '/some/path' ; const environment = EnvironmentFactory . getEnvironment ( registryEntry ) ; environment . should . be . an . instanceOf ( LocalEnvironment ) ; } ) ; } ) ;