2023-02-13 17:41:35 +03:00
|
|
|
import { fromByteArray } from 'base64-js';
|
2023-08-25 00:15:49 +07:00
|
|
|
import { Fluence } from '@fluencelabs/js-client';
|
|
|
|
import type { ClientConfig } from '@fluencelabs/js-client';
|
2023-03-11 00:03:34 +04:00
|
|
|
import { registerHelloWorld, helloTest, marineTest, resourceTest } from './_aqua/smoke_test.js';
|
2023-09-14 10:22:59 +07:00
|
|
|
import { test as particleTest } from './_aqua/finalize_particle.js';
|
2023-03-11 00:03:34 +04:00
|
|
|
import { wasm } from './wasmb64.js';
|
2023-02-13 17:41:35 +03:00
|
|
|
|
2023-06-20 21:52:41 +02:00
|
|
|
const relay = {
|
|
|
|
multiaddr: '/ip4/127.0.0.1/tcp/9991/ws/p2p/12D3KooWBM3SdXWqGaawQDGQ6JprtwswEg3FWGvGhmgmMez1vRbR',
|
|
|
|
peerId: '12D3KooWBM3SdXWqGaawQDGQ6JprtwswEg3FWGvGhmgmMez1vRbR',
|
|
|
|
};
|
2023-02-13 17:41:35 +03:00
|
|
|
|
2023-02-16 14:38:48 +03:00
|
|
|
function generateRandomUint8Array() {
|
|
|
|
const uint8Array = new Uint8Array(32);
|
|
|
|
for (let i = 0; i < uint8Array.length; i++) {
|
|
|
|
uint8Array[i] = Math.floor(Math.random() * 256);
|
|
|
|
}
|
|
|
|
return uint8Array;
|
|
|
|
}
|
|
|
|
|
2023-04-03 21:52:40 +04:00
|
|
|
const optsWithRandomKeyPair = (): ClientConfig => {
|
2023-02-16 14:38:48 +03:00
|
|
|
return {
|
|
|
|
keyPair: {
|
|
|
|
type: 'Ed25519',
|
|
|
|
source: generateRandomUint8Array(),
|
|
|
|
},
|
|
|
|
} as const;
|
2023-02-13 17:41:35 +03:00
|
|
|
};
|
|
|
|
|
2023-03-11 00:03:34 +04:00
|
|
|
export type TestResult = { type: 'success'; data: string } | { type: 'failure'; error: string };
|
2023-03-07 20:07:52 +04:00
|
|
|
|
|
|
|
export const runTest = async (): Promise<TestResult> => {
|
2023-02-16 14:38:48 +03:00
|
|
|
try {
|
|
|
|
Fluence.onConnectionStateChange((state) => console.info('connection state changed: ', state));
|
|
|
|
|
|
|
|
console.log('connecting to Fluence Network...');
|
2023-04-03 21:52:40 +04:00
|
|
|
console.log('multiaddr: ', relay.multiaddr);
|
2023-02-16 14:38:48 +03:00
|
|
|
await Fluence.connect(relay, optsWithRandomKeyPair());
|
|
|
|
|
|
|
|
console.log('connected');
|
2023-02-13 17:41:35 +03:00
|
|
|
|
2023-02-16 15:33:03 +03:00
|
|
|
const relayPeerId = (await Fluence.getClient()).getRelayPeerId();
|
|
|
|
console.log('relay:', relayPeerId);
|
|
|
|
|
2023-02-16 14:38:48 +03:00
|
|
|
await registerHelloWorld({
|
|
|
|
hello(str) {
|
|
|
|
return 'Hello, ' + str + '!';
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const client = await Fluence.getClient();
|
|
|
|
|
|
|
|
console.log('my peer id: ', client.getPeerId());
|
|
|
|
console.log('my sk id: ', fromByteArray(client.getPeerSecretKey()));
|
|
|
|
|
2023-03-11 00:03:34 +04:00
|
|
|
console.log('running hello test...');
|
|
|
|
const hello = await helloTest();
|
|
|
|
console.log('hello test finished, result: ', hello);
|
|
|
|
|
2023-04-03 21:52:40 +04:00
|
|
|
console.log('running marine test...');
|
|
|
|
const marine = await marineTest(wasm);
|
2023-09-14 10:22:59 +07:00
|
|
|
|
|
|
|
console.log('running particle test...');
|
|
|
|
await particleTest();
|
|
|
|
|
2023-04-03 21:52:40 +04:00
|
|
|
console.log('marine test finished, result: ', marine);
|
2023-03-11 00:03:34 +04:00
|
|
|
|
|
|
|
const returnVal = {
|
|
|
|
hello,
|
2023-09-29 16:23:06 +07:00
|
|
|
marine,
|
2023-03-11 00:03:34 +04:00
|
|
|
};
|
|
|
|
return { type: 'success', data: JSON.stringify(returnVal) };
|
2023-02-16 14:38:48 +03:00
|
|
|
} finally {
|
|
|
|
console.log('disconnecting from Fluence Network...');
|
|
|
|
await Fluence.disconnect();
|
|
|
|
console.log('disconnected');
|
|
|
|
}
|
2023-02-13 17:41:35 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export const runMain = () => {
|
2023-03-07 20:07:52 +04:00
|
|
|
runTest()
|
2023-02-13 17:41:35 +03:00
|
|
|
.then(() => console.log('done!'))
|
|
|
|
.catch((err) => console.error('error: ', err));
|
|
|
|
};
|