2021-02-24 14:51:24 +03:00
|
|
|
import { encode } from 'bs58';
|
|
|
|
import { generatePeerId, peerIdToSeed, seedToPeerId } from '../../internal/peerIdUtils';
|
|
|
|
import { FluenceClientImpl } from '../../internal/FluenceClientImpl';
|
2021-01-19 15:47:49 +03:00
|
|
|
import log from 'loglevel';
|
2021-02-25 18:36:10 +03:00
|
|
|
import { createClient, subscribeForErrors } from '../../api';
|
2021-01-19 15:47:49 +03:00
|
|
|
import Multiaddr from 'multiaddr';
|
2021-02-25 18:36:10 +03:00
|
|
|
import { createConnectedClient, createLocalClient, nodes } from '../connection';
|
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-02-25 15:33:37 +03:00
|
|
|
const addr = nodes[0].multiaddr;
|
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-02-25 15:33:37 +03:00
|
|
|
const addr = new Multiaddr(nodes[0].multiaddr);
|
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
|
2021-02-25 15:33:37 +03:00
|
|
|
const addr = nodes[0];
|
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-02-25 15:33:37 +03:00
|
|
|
const addr = nodes[0].multiaddr;
|
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-02-25 15:33:37 +03:00
|
|
|
const addr = nodes[0].multiaddr;
|
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-25 15:33:37 +03:00
|
|
|
const client = await createConnectedClient(nodes[0].multiaddr);
|
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-02-25 15:33:37 +03:00
|
|
|
const client = await createConnectedClient(nodes[0].multiaddr);
|
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
|
2021-02-25 15:33:37 +03:00
|
|
|
const client = await createConnectedClient(nodes[0].multiaddr);
|
2021-01-29 16:48:27 +03:00
|
|
|
|
2021-01-19 15:47:49 +03:00
|
|
|
// act
|
|
|
|
let script = `
|
2021-02-25 15:33:37 +03:00
|
|
|
(call "${client.relayPeerId}" ("peer" "identify") [] result)
|
2021-01-19 15:47:49 +03:00
|
|
|
`;
|
|
|
|
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-25 15:33:37 +03:00
|
|
|
const client1 = await createConnectedClient(nodes[0].multiaddr);
|
|
|
|
const client2 = await createConnectedClient(nodes[0].multiaddr);
|
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-25 18:36:10 +03:00
|
|
|
|
|
|
|
it('xor handling should work with connected client', async function () {
|
|
|
|
// arrange
|
|
|
|
const client = await createConnectedClient(nodes[0].multiaddr);
|
|
|
|
log.setLevel('info');
|
|
|
|
|
|
|
|
// act
|
|
|
|
let script = `
|
|
|
|
(seq
|
|
|
|
(call relay ("op" "identity") [])
|
|
|
|
(call relay ("incorrect" "service") ["incorrect_arg"])
|
|
|
|
)
|
|
|
|
`;
|
|
|
|
const data = new Map();
|
|
|
|
data.set('relay', client.relayPeerId);
|
|
|
|
|
|
|
|
const promise = subscribeForErrors(client, 7000);
|
|
|
|
await client.sendScript(script, data);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
const client = await createLocalClient();
|
|
|
|
|
|
|
|
// act
|
|
|
|
let script = `(call %init_peer_id% ("incorrect" "service") ["incorrect_arg"])`;
|
|
|
|
|
|
|
|
const promise = subscribeForErrors(client, 7000);
|
|
|
|
await client.sendScript(script);
|
|
|
|
|
|
|
|
// assert
|
|
|
|
await expect(promise).rejects.toMatchObject({
|
|
|
|
error: expect.stringContaining('There is no service: incorrect'),
|
|
|
|
instruction: expect.stringContaining('incorrect'),
|
|
|
|
});
|
|
|
|
});
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|