import { VSCodeBlockchainDockerOutputAdapter } from '../../extension/logging/VSCodeBlockchainDockerOutputAdapter' ; import { LogType , FabricRuntimeUtil } from 'ibm-blockchain-platform-common' ; import * as chai from 'chai' ; import * as sinon from 'sinon' ; chai . should ( ) ; describe ( 'VSCodeBlockchainDockerOutputAdapter' , ( ) => { const outputAdapter = VSCodeBlockchainDockerOutputAdapter . instance ( FabricRuntimeUtil . LOCAL_FABRIC ) ; let sandbox ; beforeEach ( async ( ) => { sandbox = sinon . createSandbox ( ) ; outputAdapter . setConsole ( false ) ; } ) ; afterEach ( async ( ) => { sandbox . restore ( ) ; outputAdapter . setConsole ( false ) ; } ) ; describe ( '#instance' , ( ) => { it ( `should create new channel if it doesn't already exist` , ( ) => { VSCodeBlockchainDockerOutputAdapter . channels = [ ] ; VSCodeBlockchainDockerOutputAdapter . channels . length . should . equal ( 0 ) ; VSCodeBlockchainDockerOutputAdapter . instance ( 'newChannel' ) ; VSCodeBlockchainDockerOutputAdapter . channels . length . should . equal ( 1 ) ; VSCodeBlockchainDockerOutputAdapter . channels [ 0 ] . channelName . should . equal ( 'newChannel' ) ; } ) ; it ( `should get channel if it already exist` , ( ) => { VSCodeBlockchainDockerOutputAdapter . channels = [ { channelName : 'existingChannel' } as VSCodeBlockchainDockerOutputAdapter ] ; VSCodeBlockchainDockerOutputAdapter . channels . length . should . equal ( 1 ) ; VSCodeBlockchainDockerOutputAdapter . instance ( 'existingChannel' ) ; VSCodeBlockchainDockerOutputAdapter . channels . length . should . equal ( 1 ) ; VSCodeBlockchainDockerOutputAdapter . channels [ 0 ] . channelName . should . equal ( 'existingChannel' ) ; } ) ; } ) ; describe ( '#log' , ( ) => { it ( 'should log to the output channel' , ( ) => { const outputSpy = sandbox . spy ( outputAdapter , 'appendLine' ) ; outputAdapter . log ( LogType . INFO , 'hello world' ) ; outputSpy . should . have . been . calledOnceWithExactly ( LogType . INFO , 'hello world' , undefined ) ; } ) ; } ) ; describe ( '#error' , ( ) => { it ( 'should log to the output channel' , ( ) => { const outputSpy = sandbox . spy ( outputAdapter , 'appendLine' ) ; outputAdapter . log ( LogType . ERROR , 'hello world' ) ; outputSpy . should . have . been . calledOnceWithExactly ( LogType . ERROR , 'hello world' , undefined ) ; } ) ; } ) ; describe ( '#show' , ( ) => { it ( 'should show the output channel' , ( ) => { const outputSpy = sandbox . spy ( outputAdapter [ 'outputChannel' ] , 'show' ) ; outputAdapter . show ( ) ; outputSpy . should . have . been . calledOnce ; } ) ; } ) ; describe ( '#setConsole' , ( ) => { it ( 'should enable console logging' , ( ) => { outputAdapter . setConsole ( true ) ; const consoleLogSpy = sandbox . spy ( console , 'log' ) ; outputAdapter . log ( LogType . INFO , 'hello' , 'hello world' ) ; consoleLogSpy . should . have . been . calledTwice ; consoleLogSpy . firstCall . should . have . been . calledWithExactly ( 'hello' ) ; consoleLogSpy . secondCall . should . have . been . calledWithExactly ( 'hello world' ) ; } ) ; it ( 'should disable console logging' , ( ) => { outputAdapter . setConsole ( false ) ; const consoleLogSpy = sandbox . spy ( console , 'log' ) ; outputAdapter . log ( LogType . INFO , 'hello' , 'hello world' ) ; consoleLogSpy . should . have . not . been . called ; const consoleErrorSpy = sandbox . spy ( console , 'error' ) ; outputAdapter . log ( LogType . ERROR , 'hello' , 'hello world' ) ; consoleErrorSpy . should . have . not . been . called ; } ) ; } ) ; } ) ;