From cc582a4a7d40f1a5a30697d9a63ba3194b96bc54 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Tue, 21 Jun 2022 17:50:05 +0300 Subject: [PATCH] Add test for FluenceConnection --- src/__test__/connection.ts | 10 ++-- .../integration/FluenceConnection.spec.ts | 40 +++++++++++++++ src/__test__/integration/peer.spec.ts | 49 +++++++++++++++++++ 3 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 src/__test__/integration/FluenceConnection.spec.ts diff --git a/src/__test__/connection.ts b/src/__test__/connection.ts index daea1feb..63fe7b7c 100644 --- a/src/__test__/connection.ts +++ b/src/__test__/connection.ts @@ -1,12 +1,12 @@ -// Uncomment to test on dev nodes +// Uncomment to test on krasnodar nodes // export const nodes = [ // { -// multiaddr: '/dns4/dev.fluence.dev/tcp/19003/wss/p2p/12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb', -// peerId: '12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb', +// multiaddr: '/dns4/kras-00.fluence.dev/tcp/19990/wss/p2p/12D3KooWSD5PToNiLQwKDXsu8JSysCwUt8BVUJEqCHcDe7P5h45e', +// peerId: '12D3KooWSD5PToNiLQwKDXsu8JSysCwUt8BVUJEqCHcDe7P5h45e', // }, // { -// multiaddr: '/dns4/dev.fluence.dev/tcp/19004/wss/p2p/12D3KooWJbJFaZ3k5sNd8DjQgg3aERoKtBAnirEvPV8yp76kEXHB', -// peerId: '12D3KooWJbJFaZ3k5sNd8DjQgg3aERoKtBAnirEvPV8yp76kEXHB', +// multiaddr: '/dns4/kras-00.fluence.dev/tcp/19001/wss/p2p/12D3KooWR4cv1a8tv7pps4HH6wePNaK6gf1Hww5wcCMzeWxyNw51', +// peerId: '12D3KooWR4cv1a8tv7pps4HH6wePNaK6gf1Hww5wcCMzeWxyNw51', // }, // ]; diff --git a/src/__test__/integration/FluenceConnection.spec.ts b/src/__test__/integration/FluenceConnection.spec.ts new file mode 100644 index 00000000..d2028e17 --- /dev/null +++ b/src/__test__/integration/FluenceConnection.spec.ts @@ -0,0 +1,40 @@ +import { Multiaddr } from 'multiaddr'; +import PeerId from 'peer-id'; +import { FluenceConnection } from '../../internal/FluenceConnection'; +import { Particle } from '../../internal/Particle'; +import { nodes } from '../connection'; + +const relay = nodes[0]; + +describe('Fluence connection integration tests', () => { + it('should work, bitch!', async () => { + const pidStructure = await PeerId.create(); + const pid = pidStructure.toB58String(); + + const script = ` + (seq + (call "${relay.peerId}" ("op" "noop") []) + (call "${pid}" ("op" "noop") []) + )`; + + const promise = new Promise(async (resolve) => { + const conn = await FluenceConnection.createConnection({ + peerId: pidStructure, + relayAddress: new Multiaddr(relay.multiaddr), + onIncomingParticle: (p) => { + resolve(p); + }, + }); + + conn.connect(); + + const p = Particle.createNew(script, 5000, pid); + + await conn.sendParticle(p); + }); + + const res: any = await promise; + const json = JSON.parse(res); + expect(json.script).toBe(script); + }); +}); diff --git a/src/__test__/integration/peer.spec.ts b/src/__test__/integration/peer.spec.ts index d1f45dc2..18e8a100 100644 --- a/src/__test__/integration/peer.spec.ts +++ b/src/__test__/integration/peer.spec.ts @@ -76,6 +76,55 @@ describe('Typescript usage suite', () => { }); }); + it('should make a call through network1', async () => { + // arrange + await peer.start({ connectTo: nodes[0] }); + + const result = await new Promise((resolve, reject) => { + const script = ` + (xor + (seq + (call %init_peer_id% ("load" "relay") [] init_relay) + (seq + (call init_relay ("op" "identity") ["hello world!"] result) + (call %init_peer_id% ("callback" "callback") [result]) + ) + ) + (seq + (call init_relay ("op" "identity") []) + (call %init_peer_id% ("callback" "error") [%last_error%]) + ) + )`; + const particle = peer.internals.createNewParticle(script); + + if (particle instanceof Error) { + return reject(particle.message); + } + + registerHandlersHelper(peer, particle, { + load: { + relay: () => { + return peer.getStatus().relayPeerId; + }, + }, + callback: { + callback: (args: any) => { + const [val] = args; + resolve(val); + }, + error: (args: any) => { + const [error] = args; + reject(error); + }, + }, + }); + + peer.internals.initiateParticle(particle, handleTimeout(reject)); + }); + + expect(result).toBe('hello world!'); + }); + it('should make a call through network', async () => { // arrange await peer.start({ connectTo: nodes[0] });