2021-08-18 20:13:40 +03:00
import { Multiaddr } from 'multiaddr' ;
2021-03-03 22:01:05 +03:00
import { nodes } from '../connection' ;
import { RequestFlowBuilder } from '../../internal/RequestFlowBuilder' ;
2021-06-09 20:39:04 +03:00
import log from 'loglevel' ;
2021-09-08 12:42:30 +03:00
import { FluencePeer } from '../../index' ;
import { checkConnection } from '../../internal/utils' ;
2021-03-03 22:01:05 +03:00
2021-09-08 12:42:30 +03:00
const peer = new FluencePeer ( ) ;
2021-01-19 15:47:49 +03:00
describe ( 'Typescript usage suite' , ( ) = > {
2021-03-03 22:01:05 +03:00
afterEach ( async ( ) = > {
2021-09-08 12:42:30 +03:00
if ( peer ) {
await peer . uninit ( ) ;
2021-03-03 22:01:05 +03:00
}
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
2021-09-08 12:42:30 +03:00
await peer . init ( { connectTo : nodes [ 0 ] } ) ;
2021-03-03 22:01:05 +03:00
// act
const [ request , promise ] = new RequestFlowBuilder ( )
. withRawScript (
` (seq
( call init_relay ( "op" "identity" ) [ "hello world!" ] result )
( call % init_peer_id % ( "callback" "callback" ) [ result ] )
) ` ,
)
2021-06-03 20:11:09 +03:00
. buildAsFetch < [ string ] > ( 'callback' , 'callback' ) ;
2021-09-08 12:42:30 +03:00
await peer . internals . initiateFlow ( request ) ;
console . log ( request . getParticle ( ) . script ) ;
2021-03-03 22:01:05 +03:00
// assert
2021-06-03 20:11:09 +03:00
const [ result ] = await promise ;
2021-03-03 22:01:05 +03:00
expect ( result ) . toBe ( 'hello world!' ) ;
} ) ;
it ( 'check connection should work' , async function ( ) {
2021-09-08 12:42:30 +03:00
await peer . init ( { connectTo : nodes [ 0 ] } ) ;
2021-03-03 22:01:05 +03:00
2021-09-08 12:42:30 +03:00
let isConnected = await checkConnection ( peer ) ;
2021-03-03 22:01:05 +03:00
expect ( isConnected ) . toEqual ( true ) ;
} ) ;
2021-03-25 21:33:27 +03:00
it ( 'check connection should work with ttl' , async function ( ) {
2021-09-08 12:42:30 +03:00
await peer . init ( { connectTo : nodes [ 0 ] } ) ;
2021-03-25 21:33:27 +03:00
2021-09-08 12:42:30 +03:00
let isConnected = await checkConnection ( peer , 10000 ) ;
2021-03-25 21:33:27 +03:00
expect ( isConnected ) . toEqual ( true ) ;
} ) ;
2021-03-03 22:01:05 +03:00
it ( 'two clients should work inside the same time browser' , async ( ) = > {
// arrange
2021-09-08 12:42:30 +03:00
const peer1 = new FluencePeer ( ) ;
await peer1 . init ( { connectTo : nodes [ 0 ] } ) ;
const peer2 = new FluencePeer ( ) ;
await peer2 . init ( { connectTo : nodes [ 0 ] } ) ;
2021-03-03 22:01:05 +03:00
let resMakingPromise = new Promise ( ( resolve ) = > {
2021-09-08 12:42:30 +03:00
peer2 . internals . callServiceHandler . onEvent ( 'test' , 'test' , ( args , _ ) = > {
2021-03-03 22:01:05 +03:00
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
2021-09-08 12:42:30 +03:00
( call "${peer1.connectionInfo.connectedRelay}" ( "op" "identity" ) [ ] )
( call "${peer2.connectionInfo.selfPeerId}" ( "test" "test" ) [ a b c d ] )
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 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-09-08 12:42:30 +03:00
await peer1 . internals . initiateFlow ( new RequestFlowBuilder ( ) . 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-09-08 12:42:30 +03:00
await peer1 . uninit ( ) ;
await peer2 . uninit ( ) ;
2021-03-03 22:01:05 +03:00
} ) ;
describe ( 'should make connection to network' , ( ) = > {
it ( 'address as string' , async ( ) = > {
2021-01-19 15:47:49 +03:00
// arrange
2021-09-08 12:42:30 +03:00
const addr = nodes [ 0 ] ;
2021-01-19 15:47:49 +03:00
// act
2021-09-08 12:42:30 +03:00
await peer . init ( { connectTo : addr } ) ;
const isConnected = await checkConnection ( peer ) ;
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-09-08 12:42:30 +03:00
await peer . init ( { connectTo : addr } ) ;
const isConnected = await checkConnection ( peer ) ;
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-09-08 12:42:30 +03:00
await peer . init ( { connectTo : addr } ) ;
const isConnected = await checkConnection ( peer ) ;
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-09-08 12:42:30 +03:00
const addr = nodes [ 0 ] ;
2021-01-19 15:47:49 +03:00
// act
2021-09-08 12:42:30 +03:00
await peer . init ( { connectTo : addr } ) ;
const isConnected = await checkConnection ( peer ) ;
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-09-08 12:42:30 +03:00
const addr = nodes [ 0 ] ;
2021-01-19 15:47:49 +03:00
// act
2021-09-08 12:42:30 +03:00
await peer . init ( { connectTo : addr } ) ;
const isConnected = await checkConnection ( peer ) ;
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
2021-09-08 12:42:30 +03:00
const addr = nodes [ 0 ] ;
2021-03-25 21:33:27 +03:00
// act
2021-09-08 12:42:30 +03:00
await peer . init ( { connectTo : addr , dialTimeoutMs : 100000 } ) ;
const isConnected = await checkConnection ( peer ) ;
2021-03-25 21:33:27 +03:00
// assert
expect ( isConnected ) . toBeTruthy ;
} ) ;
it ( 'With connection options: skipCheckConnection' , async ( ) = > {
// arrange
2021-09-08 12:42:30 +03:00
const addr = nodes [ 0 ] ;
2021-03-25 21:33:27 +03:00
// act
2021-09-08 12:42:30 +03:00
await peer . init ( { connectTo : addr , skipCheckConnection : true } ) ;
const isConnected = await checkConnection ( peer ) ;
2021-03-25 21:33:27 +03:00
// assert
expect ( isConnected ) . toBeTruthy ;
} ) ;
it ( 'With connection options: checkConnectionTTL' , async ( ) = > {
// arrange
2021-09-08 12:42:30 +03:00
const addr = nodes [ 0 ] ;
2021-03-25 21:33:27 +03:00
// act
2021-09-08 12:42:30 +03:00
await peer . init ( { connectTo : addr , checkConnectionTimeoutMs : 1000 } ) ;
const isConnected = await checkConnection ( peer ) ;
2021-03-25 21:33:27 +03:00
// 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 ( )
. 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
2021-09-08 12:42:30 +03:00
await peer . init ( { connectTo : nodes [ 0 ] } ) ;
await peer . internals . initiateFlow ( request ) ;
2021-02-25 18:36:10 +03:00
// assert
await expect ( promise ) . rejects . toMatchObject ( {
2021-06-08 11:08:07 +03:00
msg : expect.stringContaining ( "Service with id 'incorrect' not found" ) ,
2021-02-25 18:36:10 +03:00
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 ( )
. 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-09-08 12:42:30 +03:00
await peer . init ( ) ;
await peer . internals . 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
2021-09-08 12:42:30 +03:00
it . skip ( 'Should throw correct message when calling non existing local service' , async function ( ) {
2021-04-13 17:04:36 +03:00
// arrange
2021-09-08 12:42:30 +03:00
await peer . init ( ) ;
2021-04-13 17:04:36 +03:00
// act
2021-09-08 12:42:30 +03:00
const res = callIdentifyOnInitPeerId ( peer ) ;
2021-04-13 17:04:36 +03:00
// assert
2021-04-21 19:56:19 +03:00
await expect ( res ) . rejects . toMatchObject ( {
2021-06-10 17:32:15 +03:00
msg : expect.stringContaining (
` 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-04-21 19:56:19 +03:00
instruction : 'call %init_peer_id% ("peer" "identify") [] res' ,
} ) ;
2021-04-13 17:04:36 +03:00
} ) ;
2021-04-27 17:08:18 +03:00
2021-06-09 20:39:04 +03:00
it ( 'Should not crash if undefined is passed as a variable' , async ( ) = > {
// arrange
2021-09-08 12:42:30 +03:00
await peer . init ( ) ;
2021-06-09 20:39:04 +03:00
const [ request , promise ] = new RequestFlowBuilder ( )
. withRawScript (
`
( seq
( call % init_peer_id % ( "op" "identity" ) [ arg ] res )
( call % init_peer_id % ( "return" "return" ) [ res ] )
)
` ,
)
. withVariable ( 'arg' , undefined as any )
. buildAsFetch < any [ ] > ( 'return' , 'return' ) ;
// act
2021-09-08 12:42:30 +03:00
await peer . internals . initiateFlow ( request ) ;
2021-06-09 20:39:04 +03:00
const [ res ] = await promise ;
// assert
expect ( res ) . toBe ( null ) ;
} ) ;
2021-04-27 17:08:18 +03:00
it ( 'Should throw correct error when the client tries to send a particle not to the relay' , async ( ) = > {
// arrange
2021-09-08 12:42:30 +03:00
await peer . init ( ) ;
2021-04-27 17:08:18 +03:00
// act
const [ req , promise ] = new RequestFlowBuilder ( )
. withRawScript ( '(call "incorrect_peer_id" ("any" "service") [])' )
. buildWithErrorHandling ( ) ;
2021-09-08 12:42:30 +03:00
await peer . internals . initiateFlow ( req ) ;
2021-04-27 17:08:18 +03:00
// assert
await expect ( promise ) . rejects . toMatch (
'Particle is expected to be sent to only the single peer (relay which client is connected to)' ,
) ;
} ) ;
2021-01-19 15:47:49 +03:00
} ) ;
2021-04-13 17:04:36 +03:00
2021-09-08 12:42:30 +03:00
async function callIdentifyOnInitPeerId ( peer : FluencePeer ) : 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 ( ) ;
} ) ;
2021-09-08 12:42:30 +03:00
await peer . internals . initiateFlow ( request ) ;
2021-04-13 17:04:36 +03:00
return promise ;
}