2021-03-05 17:50:52 +03:00
import { checkConnection , createClient , FluenceClient } from '../../FluenceClient' ;
2021-01-19 15:47:49 +03:00
import Multiaddr from 'multiaddr' ;
2021-03-03 22:01:05 +03:00
import { nodes } from '../connection' ;
import { RequestFlowBuilder } from '../../internal/RequestFlowBuilder' ;
let client : FluenceClient ;
2021-01-19 15:47:49 +03:00
describe ( 'Typescript usage suite' , ( ) = > {
2021-03-03 22:01:05 +03:00
afterEach ( async ( ) = > {
if ( client ) {
await client . disconnect ( ) ;
}
2021-01-19 15:47:49 +03:00
} ) ;
2021-03-03 22:01:05 +03:00
it ( 'should make a call through network' , async ( ) = > {
// arrange
client = await createClient ( ) ;
await client . connect ( nodes [ 0 ] . multiaddr ) ;
// act
const [ request , promise ] = new RequestFlowBuilder ( )
2021-03-29 23:52:46 +03:00
. withDefaults ( )
2021-03-03 22:01:05 +03:00
. withRawScript (
` (seq
( call init_relay ( "op" "identity" ) [ "hello world!" ] result )
( call % init_peer_id % ( "callback" "callback" ) [ result ] )
) ` ,
)
. buildAsFetch < [ [ string ] ] > ( 'callback' , 'callback' ) ;
await client . initiateFlow ( request ) ;
// assert
const [ [ result ] ] = await promise ;
expect ( result ) . toBe ( 'hello world!' ) ;
} ) ;
it ( 'check connection should work' , async function ( ) {
client = await createClient ( ) ;
await client . connect ( nodes [ 0 ] . multiaddr ) ;
let isConnected = await checkConnection ( client ) ;
expect ( isConnected ) . toEqual ( true ) ;
} ) ;
2021-03-25 21:33:27 +03:00
it ( 'check connection should work with ttl' , async function ( ) {
client = await createClient ( ) ;
await client . connect ( nodes [ 0 ] . multiaddr ) ;
let isConnected = await checkConnection ( client , 10000 ) ;
expect ( isConnected ) . toEqual ( true ) ;
} ) ;
2021-03-03 22:01:05 +03:00
it ( 'two clients should work inside the same time browser' , async ( ) = > {
// arrange
const client1 = await createClient ( nodes [ 0 ] . multiaddr ) ;
const client2 = await createClient ( nodes [ 0 ] . multiaddr ) ;
let resMakingPromise = new Promise ( ( resolve ) = > {
client2 . aquaCallHandler . onEvent ( 'test' , 'test' , ( args , _ ) = > {
resolve ( [ . . . args ] ) ;
return { } ;
2021-01-19 15:47:49 +03:00
} ) ;
2021-03-03 22:01:05 +03:00
} ) ;
2021-01-19 15:47:49 +03:00
2021-03-03 22:01:05 +03:00
let script = `
( seq
( call "${client1.relayPeerId}" ( "op" "identity" ) [ ] )
( call "${client2.selfPeerId}" ( "test" "test" ) [ a b c d ] )
)
` ;
2021-01-19 15:47:49 +03:00
2021-03-03 22:01:05 +03:00
let data : Map < string , any > = new Map ( ) ;
data . set ( 'a' , 'some a' ) ;
data . set ( 'b' , 'some b' ) ;
data . set ( 'c' , 'some c' ) ;
data . set ( 'd' , 'some d' ) ;
2021-01-19 15:47:49 +03:00
2021-03-29 23:52:46 +03:00
await client1 . initiateFlow (
new RequestFlowBuilder ( ) . withDefaults ( ) . withRawScript ( script ) . withVariables ( data ) . build ( ) ,
) ;
2021-01-19 15:47:49 +03:00
2021-03-03 22:01:05 +03:00
let res = await resMakingPromise ;
expect ( res ) . toEqual ( [ 'some a' , 'some b' , 'some c' , 'some d' ] ) ;
2021-01-19 15:47:49 +03:00
2021-03-03 22:01:05 +03:00
await client1 . disconnect ( ) ;
await client2 . disconnect ( ) ;
} ) ;
describe ( 'should make connection to network' , ( ) = > {
it ( 'address as string' , async ( ) = > {
2021-01-19 15:47:49 +03:00
// arrange
2021-02-25 15:33:37 +03:00
const addr = nodes [ 0 ] . multiaddr ;
2021-01-19 15:47:49 +03:00
// act
2021-03-03 22:01:05 +03:00
client = await createClient ( addr ) ;
const isConnected = await checkConnection ( client ) ;
2021-01-19 15:47:49 +03:00
// assert
2021-03-03 22:01:05 +03:00
expect ( isConnected ) . toBeTruthy ;
2021-01-19 15:47:49 +03:00
} ) ;
2021-03-03 22:01:05 +03:00
it ( 'address as multiaddr' , async ( ) = > {
2021-01-19 15:47:49 +03:00
// arrange
2021-02-25 15:33:37 +03:00
const addr = new Multiaddr ( nodes [ 0 ] . multiaddr ) ;
2021-01-19 15:47:49 +03:00
// act
2021-03-03 22:01:05 +03:00
client = await createClient ( addr ) ;
const isConnected = await checkConnection ( client ) ;
2021-01-19 15:47:49 +03:00
// assert
2021-03-03 22:01:05 +03:00
expect ( isConnected ) . toBeTruthy ;
2021-01-19 15:47:49 +03:00
} ) ;
2021-03-03 22:01:05 +03:00
it ( 'address as node' , async ( ) = > {
2021-01-19 15:47:49 +03:00
// arrange
2021-02-25 15:33:37 +03:00
const addr = nodes [ 0 ] ;
2021-01-19 15:47:49 +03:00
// act
2021-03-03 22:01:05 +03:00
client = await createClient ( addr ) ;
const isConnected = await checkConnection ( client ) ;
2021-01-19 15:47:49 +03:00
// assert
2021-03-03 22:01:05 +03:00
expect ( isConnected ) . toBeTruthy ;
2021-01-19 15:47:49 +03:00
} ) ;
2021-03-03 22:01:05 +03:00
it ( 'peerid as peer id' , async ( ) = > {
2021-01-19 15:47:49 +03:00
// arrange
2021-02-25 15:33:37 +03:00
const addr = nodes [ 0 ] . multiaddr ;
2021-01-19 15:47:49 +03:00
// act
2021-03-03 22:01:05 +03:00
client = await createClient ( addr ) ;
const isConnected = await checkConnection ( client ) ;
2021-01-19 15:47:49 +03:00
// assert
2021-03-03 22:01:05 +03:00
expect ( isConnected ) . toBeTruthy ;
2021-01-19 15:47:49 +03:00
} ) ;
2021-03-03 22:01:05 +03:00
it ( 'peerid as seed' , async ( ) = > {
2021-01-19 15:47:49 +03:00
// arrange
2021-02-25 15:33:37 +03:00
const addr = nodes [ 0 ] . multiaddr ;
2021-01-19 15:47:49 +03:00
// act
2021-03-03 22:01:05 +03:00
client = await createClient ( addr ) ;
const isConnected = await checkConnection ( client ) ;
2021-01-19 15:47:49 +03:00
// assert
2021-03-03 22:01:05 +03:00
expect ( isConnected ) . toBeTruthy ;
2021-01-19 15:47:49 +03:00
} ) ;
2021-03-25 21:33:27 +03:00
it ( 'With connection options: dialTimeout' , async ( ) = > {
// arrange
const addr = nodes [ 0 ] . multiaddr ;
// act
client = await createClient ( addr , undefined , { dialTimeout : 100000 } ) ;
const isConnected = await checkConnection ( client ) ;
// assert
expect ( isConnected ) . toBeTruthy ;
} ) ;
it ( 'With connection options: skipCheckConnection' , async ( ) = > {
// arrange
const addr = nodes [ 0 ] . multiaddr ;
// act
client = await createClient ( addr , undefined , { skipCheckConnection : true } ) ;
const isConnected = await checkConnection ( client ) ;
// assert
expect ( isConnected ) . toBeTruthy ;
} ) ;
it ( 'With connection options: checkConnectionTTL' , async ( ) = > {
// arrange
const addr = nodes [ 0 ] . multiaddr ;
// act
client = await createClient ( addr , undefined , { checkConnectionTTL : 1000 } ) ;
const isConnected = await checkConnection ( client ) ;
// assert
expect ( isConnected ) . toBeTruthy ;
} ) ;
2021-01-19 15:47:49 +03:00
} ) ;
2021-02-25 18:36:10 +03:00
it ( 'xor handling should work with connected client' , async function ( ) {
// arrange
2021-03-03 22:01:05 +03:00
const [ request , promise ] = new RequestFlowBuilder ( )
2021-03-29 23:52:46 +03:00
. withDefaults ( )
2021-03-03 22:01:05 +03:00
. withRawScript (
`
2021-02-25 18:36:10 +03:00
( seq
2021-03-03 22:01:05 +03:00
( call init_relay ( "op" "identity" ) [ ] )
( call init_relay ( "incorrect" "service" ) [ "incorrect_arg" ] )
2021-02-25 18:36:10 +03:00
)
2021-03-03 22:01:05 +03:00
` ,
)
. buildWithErrorHandling ( ) ;
2021-02-25 18:36:10 +03:00
2021-03-03 22:01:05 +03:00
// act
client = await createClient ( nodes [ 0 ] . multiaddr ) ;
await client . initiateFlow ( request ) ;
2021-02-25 18:36:10 +03:00
// assert
await expect ( promise ) . rejects . toMatchObject ( {
error : expect.stringContaining ( "Service with id 'incorrect' not found" ) ,
instruction : expect.stringContaining ( 'incorrect' ) ,
} ) ;
} ) ;
it ( 'xor handling should work with local client' , async function ( ) {
// arrange
2021-03-03 22:01:05 +03:00
const [ request , promise ] = new RequestFlowBuilder ( )
2021-03-29 23:52:46 +03:00
. withDefaults ( )
2021-03-03 22:01:05 +03:00
. withRawScript (
`
( call % init_peer_id % ( "service" "fails" ) [ ] )
` ,
)
. configHandler ( ( h ) = > {
h . use ( ( req , res , _ ) = > {
res . retCode = 1 ;
res . result = 'service failed internally' ;
} ) ;
} )
. buildWithErrorHandling ( ) ;
2021-02-25 18:36:10 +03:00
// act
2021-03-03 22:01:05 +03:00
client = await createClient ( ) ;
await client . initiateFlow ( request ) ;
2021-02-25 18:36:10 +03:00
// assert
2021-03-03 22:01:05 +03:00
await expect ( promise ) . rejects . toMatch ( 'service failed internally' ) ;
2021-02-25 18:36:10 +03:00
} ) ;
2021-04-13 17:04:36 +03:00
it ( 'Should throw correct message when calling non existing local service' , async function ( ) {
// arrange
client = await createClient ( ) ;
// act
2021-04-20 14:19:08 +03:00
const res = callIdentifyOnInitPeerId ( client ) ;
2021-04-13 17:04:36 +03:00
// assert
await expect ( res ) . rejects . toMatch (
"The handler did not set any result. Make sure you are calling the right peer and the handler has been registered. Original request data was: serviceId='peer' fnName='identify' args=''" ,
) ;
} ) ;
2021-01-19 15:47:49 +03:00
} ) ;
2021-04-13 17:04:36 +03:00
2021-04-20 14:19:08 +03:00
async function callIdentifyOnInitPeerId ( client : FluenceClient ) : Promise < string [ ] > {
2021-04-13 17:04:36 +03:00
let request ;
const promise = new Promise < string [ ] > ( ( resolve , reject ) = > {
request = new RequestFlowBuilder ( )
. withRawScript (
`
( call % init_peer_id % ( "peer" "identify" ) [ ] res )
` ,
)
. handleScriptError ( reject )
. build ( ) ;
} ) ;
await client . initiateFlow ( request ) ;
return promise ;
}