2021-02-19 16:32:02 +03:00
|
|
|
import {encode} from 'bs58';
|
|
|
|
import {generatePeerId, peerIdToSeed, seedToPeerId} from '../../internal/peerIdUtils';
|
|
|
|
import {FluenceClientImpl} from '../../internal/FluenceClientImpl';
|
|
|
|
import {createConnectedClient} from '../util';
|
2021-01-19 15:47:49 +03:00
|
|
|
import log from 'loglevel';
|
2021-02-19 16:32:02 +03:00
|
|
|
import {createClient} from '../../api';
|
2021-01-19 15:47:49 +03:00
|
|
|
import Multiaddr from 'multiaddr';
|
2021-01-29 16:48:27 +03:00
|
|
|
|
|
|
|
const devNodeAddress = '/dns4/dev.fluence.dev/tcp/19001/wss/p2p/12D3KooWEXNUbCXooUwHrHBbrmjsrpHXoEphPwbjQXEGyzbqKnE9';
|
|
|
|
const devNodePeerId = '12D3KooWEXNUbCXooUwHrHBbrmjsrpHXoEphPwbjQXEGyzbqKnE9';
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
describe('Typescript usage suite', () => {
|
|
|
|
it('should create private key from seed and back', async function () {
|
|
|
|
// prettier-ignore
|
|
|
|
let seed = [46, 188, 245, 171, 145, 73, 40, 24, 52, 233, 215, 163, 54, 26, 31, 221, 159, 179, 126, 106, 27, 199, 189, 194, 80, 133, 235, 42, 42, 247, 80, 201];
|
|
|
|
let seedStr = encode(seed);
|
|
|
|
log.trace('SEED STR: ' + seedStr);
|
|
|
|
let pid = await seedToPeerId(seedStr);
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(peerIdToSeed(pid)).toEqual(seedStr);
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
|
2021-02-14 00:35:02 +03:00
|
|
|
describe('should make connection to network', function () {
|
2021-01-29 16:48:27 +03:00
|
|
|
const testProcedure = async (client: FluenceClientImpl) => {
|
2021-01-19 15:47:49 +03:00
|
|
|
let resMakingPromise = new Promise((resolve) => {
|
|
|
|
client.registerCallback('test', 'test', (args, _) => {
|
|
|
|
resolve(args);
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
let script = `
|
|
|
|
(seq
|
|
|
|
(call "${client.relayPeerId}" ("op" "identity") [])
|
|
|
|
(call "${client.selfPeerId}" ("test" "test") [hello])
|
|
|
|
)
|
|
|
|
`;
|
|
|
|
|
|
|
|
let data: Map<string, any> = new Map();
|
|
|
|
data.set('hello', 'world');
|
|
|
|
|
|
|
|
await client.sendScript(script, data);
|
|
|
|
|
2021-02-19 16:32:02 +03:00
|
|
|
return await resMakingPromise;
|
2021-01-19 15:47:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
it('address as string', async function () {
|
|
|
|
// arrange
|
2021-01-29 16:48:27 +03:00
|
|
|
const addr = devNodeAddress;
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
// act
|
2021-01-29 16:48:27 +03:00
|
|
|
const client = (await createClient(addr)) as FluenceClientImpl;
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
// assert
|
|
|
|
const res = await testProcedure(client);
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res).toEqual(['world']);
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('address as multiaddr', async function () {
|
|
|
|
// arrange
|
2021-01-29 16:48:27 +03:00
|
|
|
const addr = new Multiaddr(devNodeAddress);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
// act
|
2021-01-29 16:48:27 +03:00
|
|
|
const client = (await createClient(addr)) as FluenceClientImpl;
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
// assert
|
|
|
|
const res = await testProcedure(client);
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res).toEqual(['world']);
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('address as node', async function () {
|
|
|
|
// arrange
|
|
|
|
const addr = {
|
2021-01-29 16:48:27 +03:00
|
|
|
multiaddr: devNodeAddress,
|
|
|
|
peerId: devNodePeerId,
|
2021-01-19 15:47:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// act
|
2021-01-29 16:48:27 +03:00
|
|
|
const client = (await createClient(addr)) as FluenceClientImpl;
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
// assert
|
|
|
|
const res = await testProcedure(client);
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res).toEqual(['world']);
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('peerid as peer id', async function () {
|
|
|
|
// arrange
|
2021-01-29 16:48:27 +03:00
|
|
|
const addr = devNodeAddress;
|
2021-01-19 15:47:49 +03:00
|
|
|
const pid = await generatePeerId();
|
|
|
|
|
|
|
|
// act
|
2021-01-29 16:48:27 +03:00
|
|
|
const client = (await createClient(addr, pid)) as FluenceClientImpl;
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
// assert
|
|
|
|
const res = await testProcedure(client);
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res).toEqual(['world']);
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
|
2021-01-29 16:48:27 +03:00
|
|
|
it('peerid as seed', async function () {
|
2021-01-19 15:47:49 +03:00
|
|
|
// arrange
|
2021-01-29 16:48:27 +03:00
|
|
|
const addr = devNodeAddress;
|
2021-01-19 15:47:49 +03:00
|
|
|
const pid = peerIdToSeed(await generatePeerId());
|
|
|
|
|
|
|
|
// act
|
2021-01-29 16:48:27 +03:00
|
|
|
const client = (await createClient(addr, pid)) as FluenceClientImpl;
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
// assert
|
|
|
|
const res = await testProcedure(client);
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res).toEqual(['world']);
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-02-14 00:35:02 +03:00
|
|
|
it('should make a call through the network', async function () {
|
2021-01-19 15:47:49 +03:00
|
|
|
// arrange
|
2021-02-14 00:35:02 +03:00
|
|
|
const client = await createConnectedClient(devNodeAddress);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
client.registerCallback('test', 'test', (args, _) => {
|
|
|
|
log.trace('should make a call through the network, called "test" "test" with args', args);
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
|
|
|
|
let resMakingPromise = new Promise((resolve) => {
|
|
|
|
client.registerCallback('test', 'reverse_args', (args, _) => {
|
|
|
|
resolve([...args].reverse());
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// act
|
|
|
|
let script = `
|
|
|
|
(seq
|
|
|
|
(call "${client.relayPeerId}" ("op" "identity") [])
|
|
|
|
(seq
|
|
|
|
(call "${client.selfPeerId}" ("test" "test") [a b c d] result)
|
|
|
|
(call "${client.selfPeerId}" ("test" "reverse_args") [a b c d])
|
|
|
|
)
|
|
|
|
)
|
|
|
|
`;
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
await client.sendScript(script, data);
|
|
|
|
|
|
|
|
// assert
|
|
|
|
const res = await resMakingPromise;
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res).toEqual(['some d', 'some c', 'some b', 'some a']);
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
|
2021-02-14 00:35:02 +03:00
|
|
|
it('fireAndForget should work', async function () {
|
2021-01-19 15:47:49 +03:00
|
|
|
// arrange
|
2021-01-29 16:48:27 +03:00
|
|
|
const client = await createConnectedClient(devNodeAddress);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
let resMakingPromise = new Promise((resolve) => {
|
|
|
|
client.registerCallback('test', 'reverse_args', (args, _) => {
|
|
|
|
resolve([...args].reverse());
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// act
|
|
|
|
let script = `
|
|
|
|
(call "${client.selfPeerId}" ("test" "reverse_args") [a b c d])
|
|
|
|
`;
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
await client.fireAndForget(script, data);
|
|
|
|
|
|
|
|
// assert
|
|
|
|
const res = await resMakingPromise;
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res).toEqual(['some d', 'some c', 'some b', 'some a']);
|
2021-01-29 16:48:27 +03:00
|
|
|
});
|
|
|
|
|
2021-02-14 00:35:02 +03:00
|
|
|
it('fetch should work', async function () {
|
2021-01-29 16:48:27 +03:00
|
|
|
// arrange
|
|
|
|
const client = await createConnectedClient(devNodeAddress);
|
|
|
|
|
2021-01-19 15:47:49 +03:00
|
|
|
// act
|
|
|
|
let script = `
|
|
|
|
(call "${client.relayPeerId}" ("op" "identify") [] result)
|
|
|
|
`;
|
|
|
|
const data = new Map();
|
|
|
|
data.set('__relay', client.relayPeerId);
|
|
|
|
|
|
|
|
const [res] = await client.fetch(script, ['result'], data);
|
|
|
|
|
|
|
|
// assert
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res.external_addresses).not.toBeUndefined;
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
|
2021-02-14 00:35:02 +03:00
|
|
|
it('two clients should work inside the same time browser', async function () {
|
2021-01-19 15:47:49 +03:00
|
|
|
// arrange
|
2021-02-19 16:32:02 +03:00
|
|
|
const client1 = await createConnectedClient(devNodeAddress);
|
|
|
|
const client2 = await createConnectedClient(devNodeAddress);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
let resMakingPromise = new Promise((resolve) => {
|
|
|
|
client2.registerCallback('test', 'test', (args, _) => {
|
|
|
|
resolve([...args]);
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
let script = `
|
|
|
|
(seq
|
|
|
|
(call "${client1.relayPeerId}" ("op" "identity") [])
|
2021-02-19 16:32:02 +03:00
|
|
|
(call "${client2.selfPeerId}" ("test" "test") [a b c d])
|
2021-01-19 15:47:49 +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');
|
|
|
|
|
|
|
|
await client1.sendScript(script, data);
|
|
|
|
|
|
|
|
let res = await resMakingPromise;
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res).toEqual(['some a', 'some b', 'some c', 'some d']);
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
|
2021-02-14 00:35:02 +03:00
|
|
|
it('event registration should work', async function () {
|
2021-01-19 15:47:49 +03:00
|
|
|
// arrange
|
2021-02-19 16:32:02 +03:00
|
|
|
const client1 = await createConnectedClient(devNodeAddress);
|
|
|
|
const client2 = await createConnectedClient(devNodeAddress);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
client2.registerEvent('event_stream', 'test');
|
|
|
|
const resMakingPromise = new Promise((resolve) => {
|
|
|
|
client2.subscribe('event_stream', resolve);
|
|
|
|
});
|
|
|
|
|
|
|
|
// act
|
|
|
|
let script = `
|
2021-02-19 16:32:02 +03:00
|
|
|
(call "${client2.selfPeerId}" ("event_stream" "test") [hello])
|
2021-01-19 15:47:49 +03:00
|
|
|
`;
|
|
|
|
|
|
|
|
let data: Map<string, any> = new Map();
|
|
|
|
data.set('hello', 'world');
|
|
|
|
|
|
|
|
await client1.fireAndForget(script, data);
|
|
|
|
|
|
|
|
// assert
|
|
|
|
let res = await resMakingPromise;
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res).toEqual({
|
2021-01-19 15:47:49 +03:00
|
|
|
type: 'test',
|
|
|
|
args: ['world'],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|