import { RequestConfig , CallType , BaseConfig , ClientStreamCbs , BidiAndServerStreamCbs , } from "../lib/local/grpcHandlerFactory" ; import { getServer } from "./grpc-server/grpc-server" ; import * as protoLoader from "@grpc/proto-loader" ; import * as path from "path" ; import { GrpcHandlerFactory } from "../lib/local/grpcHandlerFactory" ; import * as grpc from "grpc" ; import * as _ from "lodash" ; describe ( "test gRPC unary call" , ( ) => { let routeServer ; beforeAll ( ( ) => { routeServer = getServer ( ) ; routeServer . bind ( "0.0.0.0:50052" , grpc . ServerCredentials . createInsecure ( ) ) ; routeServer . start ( ) ; console . log ( "Starting gRPC server" ) ; } ) ; afterAll ( ( ) => { routeServer . tryShutdown ( ( ) => { } ) ; } ) ; const packageDefinition = protoLoader . loadSync ( path . resolve ( __dirname , "./grpc-server/protos/todo.proto" ) ) ; const baseConfig = { grpcServerURI : "localhost:50052" , packageDefinition : packageDefinition , packageName : "todo" , serviceName : "ListActions" , } ; it ( "should make a unary call with no arguments" , done => { const expected = { items : [ { item : "clean the house" } , { item : "do laundry" } ] , } ; const unaryCallConfig = { requestName : "GetList" , callType : CallType . UNARY_CALL , argument : { } , callbacks : null , } ; const mergedConfig = { ... baseConfig , ... unaryCallConfig } ; const unaryHandler = GrpcHandlerFactory . createHandler ( mergedConfig ) ; unaryHandler . initiateRequest ( ) . then ( res => { expect ( res [ 0 ] . payload ) . toEqual ( expected ) ; done ( ) ; } ) ; } ) ; it ( "should make a unary call with arguments" , done => { const expected = { items : [ { item : "clean the house" } , { item : "do laundry" } , { item : "more to do" } ] , } ; const unaryCallConfig = { callType : CallType . UNARY_CALL , requestName : "AddItem" , argument : { item : "more to do" } , callbacks : null , } ; const mergedConfig = { ... baseConfig , ... unaryCallConfig } ; const unaryHandler = GrpcHandlerFactory . createHandler ( mergedConfig ) ; unaryHandler . initiateRequest ( ) . then ( res => { expect ( res [ 0 ] . payload ) . toEqual ( expected ) ; done ( ) ; } ) ; } ) ; it ( "should make a client side streaming request to calculate average" , done => { const clientStreamConfig = { callType : CallType . CLIENT_STREAM , requestName : "CalculateAverage" , argument : { } , callbacks : { onEndReadCb : function ( data ) { const result = data . average . low ; expect ( result ) . toEqual ( 15 ) ; done ( ) ; } , } , } ; const mergedConfig = { ... baseConfig , ... clientStreamConfig } ; const clientStreamHandler = GrpcHandlerFactory . createHandler ( mergedConfig ) ; clientStreamHandler . initiateRequest ( ) ; const { writableStream } = clientStreamHandler . getEmitters ( ) ; writableStream . write ( { numb : 10 } ) ; writableStream . write ( { numb : 15 } ) ; writableStream . write ( { numb : 20 } ) ; writableStream . end ( ) ; } ) ; it ( "should respond to a server side streaming request" , done => { const testArr = [ ] ; const onDataReadCb = ( data ) => { testArr . push ( data [ data . length - 1 ] ) ; } ; const onEndReadCb = ( ) => { const extracted = [ ] ; Object . values ( testArr ) . forEach ( obj => { extracted . push ( obj . payload ) ; } ) ; expect ( extracted ) . toEqual ( [ { numb : 3 } , { numb : 3 } , { numb : 3 } , { numb : 3 } , { numb : 3 } ] ) ; done ( ) ; } ; const serverStreamConfig = { callType : CallType . SERVER_STREAM , requestName : "TestServerStream" , argument : { } , callbacks : { onDataReadCb , onEndReadCb , } , } ; const mergedConfig = { ... baseConfig , ... serverStreamConfig , } ; const serverStreamHandler = GrpcHandlerFactory . createHandler ( mergedConfig ) ; serverStreamHandler . initiateRequest ( ) ; } ) ; it ( "should test bidirectional streaming" , done => { const testArr = [ ] ; const bidiStreamConfig = { callType : CallType . BIDI_STREAM , requestName : "ItemStreamer" , argument : { } , callbacks : { onDataReadCb : data => { data . forEach ( entry => { if ( entry . type === "read" ) { testArr . push ( entry . payload ) ; } } ) ; } , onEndReadCb : ( ) => { expect ( testArr ) . toEqual ( [ { msg : "spoon - count: 0" } , { msg : "fork - count: 1" } , { msg : "spoon - count: 0" } , ] ) ; done ( ) ; } , } , } ; const mergedConfig = { ... baseConfig , ... bidiStreamConfig , } ; const bidiStreamHandler = GrpcHandlerFactory . createHandler ( mergedConfig ) as BidiStreamHandler ; bidiStreamHandler . initiateRequest ( ) ; const { writableStream } = bidiStreamHandler . getEmitters ( ) ; const messagesToSend = [ { item : "spoon" } , { item : "fork" } ] ; messagesToSend . forEach ( msg => { writableStream . write ( msg ) ; } ) ; writableStream . end ( ) ; } ) ; } ) ;