2023-02-13 17:41:35 +03:00
|
|
|
import { fromByteArray } from 'base64-js';
|
2023-02-15 03:00:42 +03:00
|
|
|
import { Fluence } from '@fluencelabs/js-client.api';
|
2023-02-16 14:38:48 +03:00
|
|
|
import { kras, randomKras } from '@fluencelabs/fluence-network-environment';
|
|
|
|
import { registerHelloWorld, smokeTest } from './_aqua/smoke_test.js';
|
2023-02-13 17:41:35 +03:00
|
|
|
|
2023-03-07 20:07:52 +04:00
|
|
|
// Relay running on local machine
|
2023-02-13 17:41:35 +03:00
|
|
|
// const relay = {
|
|
|
|
// multiaddr: '/ip4/127.0.0.1/tcp/4310/ws/p2p/12D3KooWKEprYXUXqoV5xSBeyqrWLpQLLH4PXfvVkDJtmcqmh5V3',
|
|
|
|
// peerId: '12D3KooWKEprYXUXqoV5xSBeyqrWLpQLLH4PXfvVkDJtmcqmh5V3',
|
|
|
|
// };
|
|
|
|
|
2023-03-07 20:07:52 +04:00
|
|
|
// Currently the tests executes some calls to registry. And they fail for a single local node setup. So we use kras instead.
|
2023-02-16 14:38:48 +03:00
|
|
|
const relay = randomKras();
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const optsWithRandomKeyPair = () => {
|
|
|
|
return {
|
|
|
|
keyPair: {
|
|
|
|
type: 'Ed25519',
|
|
|
|
source: generateRandomUint8Array(),
|
|
|
|
},
|
|
|
|
} as const;
|
2023-02-13 17:41:35 +03:00
|
|
|
};
|
|
|
|
|
2023-03-07 20:07:52 +04:00
|
|
|
export type TestResult = { res: string | null; errors: string[]; hello: string };
|
|
|
|
|
|
|
|
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...');
|
|
|
|
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()));
|
|
|
|
|
|
|
|
console.log('running some aqua...');
|
|
|
|
const [res, errors, hello] = await smokeTest('my_resource');
|
|
|
|
console.log(hello);
|
|
|
|
if (res === null) {
|
|
|
|
console.log('aqua failed, errors', errors);
|
|
|
|
} else {
|
|
|
|
console.log('aqua finished, result', res);
|
|
|
|
}
|
2023-03-07 20:07:52 +04:00
|
|
|
|
|
|
|
return { res, errors, hello };
|
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));
|
|
|
|
};
|