import { expect } from 'chai' ; import { RuntimeConfigurationBuilder } from '../../lib/skill/RuntimeConfigurationBuilder' ; import { MockAlwaysFalseErrorHandler } from '../mocks/error/MockAlwaysFalseErrorHandler' ; import { MockAlwaysTrueErrorHandler } from '../mocks/error/MockAlwaysTrueErrorHandler' ; import { MockAlwaysFalseRequestHandler } from '../mocks/request/MockAlwaysFalseRequestHandler' ; import { MockAlwaysTrueRequestHandler } from '../mocks/request/MockAlwaysTrueRequestHandler' ; describe ( 'RuntimeConfigurationBuilder' , ( ) => { it ( 'should be able to add single request handler using matcher and executor' , async ( ) => { const runtimeConfiguration = new RuntimeConfigurationBuilder < string , string > ( ) . addRequestHandler ( ( input ) => input === 'Test Request' , ( input ) => 'Event received' ) . getRuntimeConfiguration ( ) ; const requestMapper = runtimeConfiguration . requestMappers [ 0 ] ; const requestHandlerChain = await requestMapper . getRequestHandlerChain ( 'Test Request' ) ; const requestHandler = requestHandlerChain . getRequestHandler ( ) ; expect ( requestHandler . handle ( ) ) . eq ( 'Event received' ) ; } ) ; it ( 'should throw an error if request handler matcher is invalid' , ( ) => { try { new RuntimeConfigurationBuilder < string , string > ( ) . addRequestHandler ( true as any , false as any ) . getRuntimeConfiguration ( ) ; } catch ( error ) { expect ( error . message ) . eq ( 'Incompatible matcher type: boolean' ) ; return ; } throw new Error ( 'should have thrown an error!' ) ; } ) ; it ( 'should be able to add multiple request handlers' , async ( ) => { const runtimeConfiguration = new RuntimeConfigurationBuilder < string , string > ( ) . addRequestHandlers ( new MockAlwaysTrueRequestHandler ( ) , new MockAlwaysFalseRequestHandler ( ) ) . getRuntimeConfiguration ( ) ; const requestMapper = runtimeConfiguration . requestMappers [ 0 ] ; const requestHandlerChain = await requestMapper . getRequestHandlerChain ( 'Test Request' ) ; const requestHandler = requestHandlerChain . getRequestHandler ( ) ; expect ( requestHandler . handle ( 'Test request' ) ) . eq ( 'Input(Test request) received at MockAlwaysTrueRequestHandler' ) ; } ) ; it ( 'should be able to add multiple global request interceptors with either object or function' , async ( ) => { const runtimeConfiguration = new RuntimeConfigurationBuilder < string , string > ( ) . addRequestHandlers ( new MockAlwaysTrueRequestHandler ( ) , new MockAlwaysFalseRequestHandler ( ) ) . addRequestInterceptors ( { process ( input ) { expect ( input ) . eq ( 'Test Request?' ) ; } , } , ( input ) : void => { expect ( input ) . eq ( 'Test Request' ) ; } ) . getRuntimeConfiguration ( ) ; const requestMapper = runtimeConfiguration . requestMappers [ 0 ] ; const requestHandlerChain = await requestMapper . getRequestHandlerChain ( 'Test Request' ) ; const requestHandler = requestHandlerChain . getRequestHandler ( ) ; expect ( requestHandler . handle ( 'Test request' ) ) . eq ( 'Input(Test request) received at MockAlwaysTrueRequestHandler' ) ; expect ( runtimeConfiguration . requestInterceptors . length ) . eq ( 2 ) ; } ) ; it ( 'should thrown an error if request interceptor is invalid' , ( ) => { try { new RuntimeConfigurationBuilder < string , string > ( ) . addRequestHandlers ( new MockAlwaysTrueRequestHandler ( ) ) . addRequestInterceptors ( true as any ) . getRuntimeConfiguration ( ) ; } catch ( error ) { expect ( error . message ) . eq ( 'Incompatible executor type: boolean' ) ; return ; } throw new Error ( 'should have thrown an error!' ) ; } ) ; it ( 'should be able to add multiple global response interceptors with either object or function' , async ( ) => { const runtimeConfiguration = new RuntimeConfigurationBuilder < string , string > ( ) . addRequestHandlers ( new MockAlwaysTrueRequestHandler ( ) , new MockAlwaysFalseRequestHandler ( ) ) . addResponseInterceptors ( { process ( input , output ) { expect ( input ) . eq ( 'Test Request?' ) ; } , } , ( input , output ) : void => { expect ( input ) . eq ( 'Test Request' ) ; } ) . getRuntimeConfiguration ( ) ; const requestMapper = runtimeConfiguration . requestMappers [ 0 ] ; const requestHandlerChain = await requestMapper . getRequestHandlerChain ( 'Test Request' ) ; const requestHandler = requestHandlerChain . getRequestHandler ( ) ; expect ( requestHandler . handle ( 'Test request' ) ) . eq ( 'Input(Test request) received at MockAlwaysTrueRequestHandler' ) ; expect ( runtimeConfiguration . responseInterceptors . length ) . eq ( 2 ) ; } ) ; it ( 'should thrown an error if response interceptor is invalid' , ( ) => { try { new RuntimeConfigurationBuilder < string , string > ( ) . addRequestHandlers ( new MockAlwaysTrueRequestHandler ( ) ) . addResponseInterceptors ( true as any ) . getRuntimeConfiguration ( ) ; } catch ( error ) { expect ( error . message ) . eq ( 'Incompatible executor type: boolean' ) ; return ; } throw new Error ( 'should have thrown an error!' ) ; } ) ; it ( 'should be able to add single error handler using matcher and executor' , async ( ) => { const runtimeConfiguration = new RuntimeConfigurationBuilder < string , string > ( ) . addErrorHandler ( ( input , error ) => input === 'Test Request' && error . message === 'Test Error' , ( input , error ) => 'Error received' ) . getRuntimeConfiguration ( ) ; const errorHandler = await runtimeConfiguration . errorMapper . getErrorHandler ( 'Test Request' , new Error ( 'Test Error' ) ) ; expect ( errorHandler . handle ( null , null ) ) . eq ( 'Error received' ) ; } ) ; it ( 'should be able to add multiple error handlers' , async ( ) => { const runtimeConfiguration = new RuntimeConfigurationBuilder < string , string > ( ) . addErrorHandlers ( new MockAlwaysTrueErrorHandler ( ) , new MockAlwaysFalseErrorHandler ( ) ) . getRuntimeConfiguration ( ) ; const errorHandler = await runtimeConfiguration . errorMapper . getErrorHandler ( 'Test Request' , new Error ( 'Test Error' ) ) ; expect ( errorHandler . handle ( 'Test Request' , new Error ( 'Test Error' ) ) ) . eq ( 'Error received at MockAlwaysTrueErrorHandler' ) ; } ) ; } ) ;