2021-03-18 19:23:46 +01:00
import { obj as createThoughStream } from 'through2' ;
import metaRPCClientFactory from './metaRPCClientFactory' ;
2021-12-06 17:40:39 +01:00
describe ( 'metaRPCClientFactory' , ( ) => {
it ( 'should be able to make an rpc request with the method' , ( ) => {
2021-03-18 19:23:46 +01:00
const streamTest = createThoughStream ( ( chunk ) => {
2021-12-06 17:40:39 +01:00
expect ( chunk . method ) . toStrictEqual ( 'foo' ) ;
2021-03-18 19:23:46 +01:00
} ) ;
const metaRPCClient = metaRPCClientFactory ( streamTest ) ;
metaRPCClient . foo ( ) ;
} ) ;
2021-12-06 17:40:39 +01:00
it ( 'should be able to make an rpc request/response with the method and params and node-style callback' , ( ) => {
2021-03-18 19:23:46 +01:00
const streamTest = createThoughStream ( ) ;
const metaRPCClient = metaRPCClientFactory ( streamTest ) ;
// make a "foo" method call
metaRPCClient . foo ( 'bar' , ( _ , result ) => {
2021-12-06 17:40:39 +01:00
expect ( result ) . toStrictEqual ( 'foobarbaz' ) ;
2021-03-18 19:23:46 +01:00
} ) ;
// fake a response
metaRPCClient . requests . forEach ( ( _ , key ) => {
streamTest . write ( {
jsonrpc : '2.0' ,
id : key ,
result : 'foobarbaz' ,
} ) ;
} ) ;
} ) ;
2021-12-06 17:40:39 +01:00
it ( 'should be able to make an rpc request/error with the method and params and node-style callback' , ( ) => {
2021-03-18 19:23:46 +01:00
const streamTest = createThoughStream ( ) ;
const metaRPCClient = metaRPCClientFactory ( streamTest ) ;
// make a "foo" method call
metaRPCClient . foo ( 'bar' , ( err ) => {
2021-12-06 17:40:39 +01:00
expect ( err . message ) . toStrictEqual ( 'foo-message' ) ;
expect ( err . code ) . toStrictEqual ( 1 ) ;
2021-03-18 19:23:46 +01:00
} ) ;
metaRPCClient . requests . forEach ( ( _ , key ) => {
streamTest . write ( {
jsonrpc : '2.0' ,
id : key ,
error : {
code : 1 ,
message : 'foo-message' ,
} ,
} ) ;
} ) ;
} ) ;
2021-12-06 17:40:39 +01:00
it ( 'should be able to make an rpc request/response with the method and params and node-style callback with multiple instances of metaRPCClientFactory and the same connectionStream' , ( ) => {
2021-03-18 19:23:46 +01:00
const streamTest = createThoughStream ( ) ;
const metaRPCClient = metaRPCClientFactory ( streamTest ) ;
const metaRPCClient2 = metaRPCClientFactory ( streamTest ) ;
// make a "foo" method call, followed by "baz" call on metaRPCClient2
metaRPCClient . foo ( 'bar' , ( _ , result ) => {
2021-12-06 17:40:39 +01:00
expect ( result ) . toStrictEqual ( 'foobarbaz' ) ;
2021-03-18 19:23:46 +01:00
metaRPCClient2 . baz ( 'bar' , ( err ) => {
2021-12-06 17:40:39 +01:00
expect ( err ) . toBeNull ( ) ;
2021-03-18 19:23:46 +01:00
} ) ;
} ) ;
// fake a response
metaRPCClient . requests . forEach ( ( _ , key ) => {
streamTest . write ( {
jsonrpc : '2.0' ,
id : key ,
result : 'foobarbaz' ,
} ) ;
} ) ;
// fake client2's response
metaRPCClient2 . requests . forEach ( ( _ , key ) => {
streamTest . write ( {
jsonrpc : '2.0' ,
id : key ,
result : 'foobarbaz' ,
} ) ;
} ) ;
} ) ;
2021-05-17 23:46:00 +02:00
2021-12-06 17:40:39 +01:00
it ( 'should be able to handle notifications' , ( ) => {
2021-05-17 23:46:00 +02:00
const streamTest = createThoughStream ( ) ;
const metaRPCClient = metaRPCClientFactory ( streamTest ) ;
metaRPCClient . onNotification ( ( notification ) => {
2021-12-06 17:40:39 +01:00
expect ( notification . method ) . toStrictEqual ( 'foobarbaz' ) ;
2021-05-17 23:46:00 +02:00
} ) ;
// send a notification
streamTest . write ( {
jsonrpc : '2.0' ,
method : 'foobarbaz' ,
params : [ 'bar' ] ,
} ) ;
} ) ;
2021-12-06 17:40:39 +01:00
it ( 'should be able to handle errors with no id' , ( ) => {
2021-05-17 23:46:00 +02:00
const streamTest = createThoughStream ( ) ;
const metaRPCClient = metaRPCClientFactory ( streamTest ) ;
metaRPCClient . onUncaughtError ( ( error ) => {
2021-12-06 17:40:39 +01:00
expect ( error . code ) . toStrictEqual ( 1 ) ;
2021-05-17 23:46:00 +02:00
} ) ;
streamTest . write ( {
jsonrpc : '2.0' ,
error : {
code : 1 ,
message : 'error msg' ,
} ,
} ) ;
} ) ;
2021-12-06 17:40:39 +01:00
it ( 'should be able to handle errors with null id' , ( ) => {
2021-05-17 23:46:00 +02:00
const streamTest = createThoughStream ( ) ;
const metaRPCClient = metaRPCClientFactory ( streamTest ) ;
metaRPCClient . onUncaughtError ( ( error ) => {
2021-12-06 17:40:39 +01:00
expect ( error . code ) . toStrictEqual ( 1 ) ;
2021-05-17 23:46:00 +02:00
} ) ;
streamTest . write ( {
jsonrpc : '2.0' ,
id : null ,
error : {
code : 1 ,
message : 'error msg' ,
} ,
} ) ;
} ) ;
2022-06-07 22:37:15 +02:00
it ( 'should be able to handle no message within TIMEOUT secs' , async ( ) => {
jest . useFakeTimers ( ) ;
const streamTest = createThoughStream ( ) ;
const metaRPCClient = metaRPCClientFactory ( streamTest ) ;
const errorPromise = new Promise ( ( _resolve , reject ) =>
2022-06-17 13:04:44 +02:00
metaRPCClient . getState ( 'bad' , ( error , _ ) => {
2022-06-07 22:37:15 +02:00
reject ( error ) ;
} ) ,
) ;
jest . runOnlyPendingTimers ( ) ;
await expect ( errorPromise ) . rejects . toThrow ( 'No response from RPC' ) ;
jest . useRealTimers ( ) ;
} ) ;
2021-03-18 19:23:46 +01:00
} ) ;