mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-25 13:51:34 +00:00
feat: Cleaning up technical debts (#295)
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
import { fromByteArray } from 'base64-js';
|
||||
import { Fluence } from '@fluencelabs/js-client.api';
|
||||
import type { ClientConfig } from '@fluencelabs/js-client.api';
|
||||
import { kras, randomKras } from '@fluencelabs/fluence-network-environment';
|
||||
import { registerHelloWorld, helloTest, marineTest, resourceTest } from './_aqua/smoke_test.js';
|
||||
import { wasm } from './wasmb64.js';
|
||||
@ -11,7 +12,9 @@ import { wasm } from './wasmb64.js';
|
||||
// };
|
||||
|
||||
// Currently the tests executes some calls to registry. And they fail for a single local node setup. So we use kras instead.
|
||||
const relay = randomKras();
|
||||
// TODO DXJ-356: use local peers instead of kras
|
||||
// const relay = randomKras();
|
||||
const relay = kras[4];
|
||||
|
||||
function generateRandomUint8Array() {
|
||||
const uint8Array = new Uint8Array(32);
|
||||
@ -21,7 +24,7 @@ function generateRandomUint8Array() {
|
||||
return uint8Array;
|
||||
}
|
||||
|
||||
const optsWithRandomKeyPair = () => {
|
||||
const optsWithRandomKeyPair = (): ClientConfig => {
|
||||
return {
|
||||
keyPair: {
|
||||
type: 'Ed25519',
|
||||
@ -37,6 +40,7 @@ export const runTest = async (): Promise<TestResult> => {
|
||||
Fluence.onConnectionStateChange((state) => console.info('connection state changed: ', state));
|
||||
|
||||
console.log('connecting to Fluence Network...');
|
||||
console.log('multiaddr: ', relay.multiaddr);
|
||||
await Fluence.connect(relay, optsWithRandomKeyPair());
|
||||
|
||||
console.log('connected');
|
||||
@ -68,10 +72,9 @@ export const runTest = async (): Promise<TestResult> => {
|
||||
const hello = await helloTest();
|
||||
console.log('hello test finished, result: ', hello);
|
||||
|
||||
// TODO: some wired error shit about SharedArrayBuffer
|
||||
// console.log('running marine test...');
|
||||
// const marine = await marineTest(wasm);
|
||||
// console.log('marine test finished, result: ', marine);
|
||||
console.log('running marine test...');
|
||||
const marine = await marineTest(wasm);
|
||||
console.log('marine test finished, result: ', marine);
|
||||
|
||||
const returnVal = {
|
||||
res,
|
||||
@ -79,8 +82,6 @@ export const runTest = async (): Promise<TestResult> => {
|
||||
// marine,
|
||||
};
|
||||
return { type: 'success', data: JSON.stringify(returnVal) };
|
||||
} catch (err: any) {
|
||||
return { type: 'failure', error: err.toString() };
|
||||
} finally {
|
||||
console.log('disconnecting from Fluence Network...');
|
||||
await Fluence.disconnect();
|
||||
|
19
packages/@tests/marine/node/.gitignore
vendored
19
packages/@tests/marine/node/.gitignore
vendored
@ -1,19 +0,0 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
bundle/
|
||||
/dist/
|
||||
/worker/dist/
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
.idea
|
@ -1,5 +0,0 @@
|
||||
module.exports = {
|
||||
preset: 'ts-jest',
|
||||
testEnvironment: 'node',
|
||||
testPathIgnorePatterns: ['dist'],
|
||||
};
|
@ -1,19 +0,0 @@
|
||||
{
|
||||
"name": "@test/marine_node",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"test": "jest"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "16.11.59",
|
||||
"typescript": "^4.0.0",
|
||||
"@types/jest": "28.1.0",
|
||||
"jest": "28.1.0",
|
||||
"ts-jest": "28.0.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@fluencelabs/avm": "0.32.1",
|
||||
"@fluencelabs/marine.background-runner": "0.1.0",
|
||||
"@fluencelabs/marine.deps-loader.node": "0.1.0"
|
||||
}
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
import { MarineBackgroundRunner } from '@fluencelabs/marine.background-runner';
|
||||
import { InlinedWorkerLoader, WasmNpmLoader } from '@fluencelabs/marine.deps-loader.node';
|
||||
import { callAvm, JSONArray, JSONObject } from '@fluencelabs/avm';
|
||||
|
||||
const vmPeerId = '12D3KooWNzutuy8WHXDKFqFsATvCR6j9cj2FijYbnd47geRKaQZS';
|
||||
|
||||
describe('Nodejs integration tests', () => {
|
||||
it('Smoke test', async () => {
|
||||
let runner: MarineBackgroundRunner | undefined = undefined;
|
||||
try {
|
||||
// arrange
|
||||
const avm = new WasmNpmLoader('@fluencelabs/avm', 'avm.wasm');
|
||||
const control = new WasmNpmLoader('@fluencelabs/marine-js', 'marine-js.wasm');
|
||||
const worker = new InlinedWorkerLoader();
|
||||
runner = new MarineBackgroundRunner(worker, control, () => {});
|
||||
|
||||
await avm.start();
|
||||
|
||||
await runner.start();
|
||||
await runner.createService(avm.getValue(), 'avm');
|
||||
|
||||
const s = `(seq
|
||||
(par
|
||||
(call "${vmPeerId}" ("local_service_id" "local_fn_name") [] result_1)
|
||||
(call "remote_peer_id" ("service_id" "fn_name") [] g)
|
||||
)
|
||||
(call "${vmPeerId}" ("local_service_id" "local_fn_name") [] result_2)
|
||||
)`;
|
||||
|
||||
// act
|
||||
const res = await callAvm(
|
||||
(args: JSONArray | JSONObject) => runner!.callService('avm', 'invoke', args, undefined),
|
||||
{
|
||||
currentPeerId: vmPeerId,
|
||||
initPeerId: vmPeerId,
|
||||
timestamp: Date.now(),
|
||||
ttl: 10000,
|
||||
},
|
||||
s,
|
||||
Buffer.from(''),
|
||||
Buffer.from(''),
|
||||
[],
|
||||
);
|
||||
|
||||
// assert
|
||||
expect(res).toMatchObject({
|
||||
retCode: 0,
|
||||
errorMessage: '',
|
||||
});
|
||||
} finally {
|
||||
runner?.stop();
|
||||
}
|
||||
});
|
||||
});
|
@ -1,7 +0,0 @@
|
||||
{
|
||||
"extends": "../../../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist"
|
||||
},
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
22
packages/@tests/marine/web/.gitignore
vendored
22
packages/@tests/marine/web/.gitignore
vendored
@ -1,22 +0,0 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
|
||||
build/
|
||||
public/*.*
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
bundle/
|
||||
/dist/
|
||||
/worker/dist/
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
.idea
|
@ -1,12 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Webpack App</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hello world!</h1>
|
||||
<h2>Tip: Check your console</h2>
|
||||
</body>
|
||||
|
||||
</html>
|
@ -1,9 +0,0 @@
|
||||
module.exports = {
|
||||
preset: 'jest-puppeteer',
|
||||
testMatch: ['**/?(*.)+(spec|test).[t]s'],
|
||||
testPathIgnorePatterns: ['/node_modules/', 'dist'],
|
||||
testMatch: ['**/test/*.spec.ts'],
|
||||
transform: {
|
||||
'^.+\\.ts?$': 'ts-jest',
|
||||
},
|
||||
};
|
@ -1,37 +0,0 @@
|
||||
{
|
||||
"name": "@test/marine_web",
|
||||
"version": "0.1.0",
|
||||
"scripts": {
|
||||
"start": "webpack serve",
|
||||
"test": "jest",
|
||||
"build": "webpack --mode=production --node-env=production",
|
||||
"build:dev": "webpack --mode=development",
|
||||
"build:prod": "webpack --mode=production --node-env=production",
|
||||
"watch": "webpack --watch",
|
||||
"serve": "webpack serve"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@webpack-cli/generators": "^2.4.1",
|
||||
"css-loader": "^6.5.1",
|
||||
"html-webpack-plugin": "^5.5.0",
|
||||
"install-local": "^3.0.1",
|
||||
"style-loader": "^3.3.1",
|
||||
"ts-loader": "^8.3.0",
|
||||
"typescript": "^4.5.4",
|
||||
"util": "^0.12.4",
|
||||
"webpack": "^5.65.0",
|
||||
"webpack-cli": "^4.9.1",
|
||||
"webpack-dev-server": "^4.6.0",
|
||||
"@types/jest": "^27.0.3",
|
||||
"@types/jest-environment-puppeteer": "^4.4.1",
|
||||
"@types/puppeteer": "^5.4.4",
|
||||
"jest": "28.1.0",
|
||||
"jest-puppeteer": "^6.0.2",
|
||||
"ts-jest": "28.0.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@fluencelabs/avm": "0.34.4",
|
||||
"js-base64": "^3.7.2",
|
||||
"buffer": "6.0.3"
|
||||
}
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
import { Buffer } from 'buffer';
|
||||
|
||||
// @ts-ignore
|
||||
window.Buffer = Buffer;
|
||||
|
||||
import { MarineBackgroundRunner } from '@fluencelabs/marine.background-runner';
|
||||
import { InlinedWorkerLoader, WasmWebLoader } from '@fluencelabs/marine.deps-loader.web';
|
||||
import { callAvm, JSONArray, JSONObject } from '@fluencelabs/avm';
|
||||
import { toUint8Array } from 'js-base64';
|
||||
|
||||
const vmPeerId = '12D3KooWNzutuy8WHXDKFqFsATvCR6j9cj2FijYbnd47geRKaQZS';
|
||||
|
||||
const b = (s: string) => {
|
||||
return toUint8Array(s);
|
||||
};
|
||||
|
||||
const main = async () => {
|
||||
const avm = new WasmWebLoader('avm.wasm');
|
||||
const control = new WasmWebLoader('marine-js.wasm');
|
||||
const worker = new InlinedWorkerLoader();
|
||||
const runner = new MarineBackgroundRunner(worker, control, () => {});
|
||||
|
||||
await runner.start();
|
||||
await avm.start();
|
||||
const avmVal = await avm.getValue();
|
||||
await runner.createService(avmVal, 'avm');
|
||||
|
||||
const s = `(seq
|
||||
(par
|
||||
(call "${vmPeerId}" ("local_service_id" "local_fn_name") [] result_1)
|
||||
(call "remote_peer_id" ("service_id" "fn_name") [] g)
|
||||
)
|
||||
(call "${vmPeerId}" ("local_service_id" "local_fn_name") [] result_2)
|
||||
)`;
|
||||
|
||||
// act
|
||||
const res = await callAvm(
|
||||
(args: JSONArray | JSONObject) => runner.callService('avm', 'invoke', args, undefined),
|
||||
{
|
||||
currentPeerId: vmPeerId,
|
||||
initPeerId: vmPeerId,
|
||||
timestamp: Date.now(),
|
||||
ttl: 10000,
|
||||
},
|
||||
s,
|
||||
b(''),
|
||||
b(''),
|
||||
[],
|
||||
);
|
||||
await runner.stop();
|
||||
|
||||
return res;
|
||||
};
|
||||
|
||||
// @ts-ignore
|
||||
window.MAIN = main;
|
@ -1,115 +0,0 @@
|
||||
import Webpack from 'webpack';
|
||||
import WebpackDevServer from 'webpack-dev-server';
|
||||
import webpackConfig from '../webpack.config.js';
|
||||
import process from 'process';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
|
||||
// change directory to the location to the test-project.
|
||||
// run all the subsequent Webpack scripts in that directory
|
||||
process.chdir(path.join(__dirname, '..'));
|
||||
|
||||
let server;
|
||||
const port = 8080;
|
||||
|
||||
jest.setTimeout(10000);
|
||||
|
||||
const startServer = async (modifyConfig?) => {
|
||||
const loadInBrowserToDebug = false;
|
||||
// const loadInBrowserToDebug = true; // use this line to debug
|
||||
|
||||
modifyConfig = modifyConfig || ((_) => {});
|
||||
|
||||
const config: any = webpackConfig();
|
||||
modifyConfig(config);
|
||||
config.devServer.open = loadInBrowserToDebug;
|
||||
server = await makeServer(config);
|
||||
};
|
||||
|
||||
// https://stackoverflow.com/questions/42940550/wait-until-webpack-dev-server-is-ready
|
||||
function makeServer(config) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const compiler = Webpack(config);
|
||||
|
||||
let compiled = false;
|
||||
let listening = false;
|
||||
|
||||
compiler.hooks.done.tap('tap_name', () => {
|
||||
// console.log('compiled');
|
||||
|
||||
if (listening) resolve(server);
|
||||
else compiled = true;
|
||||
});
|
||||
|
||||
const server = new WebpackDevServer(compiler, config.devServer);
|
||||
|
||||
server.listen(port, '0.0.0.0', (err) => {
|
||||
if (err) return reject(err);
|
||||
|
||||
// console.log('listening');
|
||||
|
||||
if (compiled) {
|
||||
resolve(server);
|
||||
} else {
|
||||
listening = true;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const stopServer = async () => {
|
||||
console.log('test: stopping server');
|
||||
await server.stop();
|
||||
};
|
||||
|
||||
const publicDir = 'public';
|
||||
|
||||
const copyFile = async (packageName: string, fileName: string) => {
|
||||
const modulePath = require.resolve(packageName);
|
||||
const source = path.join(path.dirname(modulePath), fileName);
|
||||
const dest = path.join(publicDir, fileName);
|
||||
|
||||
return fs.promises.copyFile(source, dest);
|
||||
};
|
||||
|
||||
const copyPublicDeps = async () => {
|
||||
await fs.promises.mkdir(publicDir, { recursive: true });
|
||||
return Promise.all([
|
||||
copyFile('@fluencelabs/marine-js', 'marine-js.wasm'),
|
||||
copyFile('@fluencelabs/avm', 'avm.wasm'),
|
||||
]);
|
||||
};
|
||||
|
||||
const cleanPublicDeps = () => {
|
||||
return fs.promises.rm(publicDir, { recursive: true, force: true });
|
||||
};
|
||||
|
||||
describe('Browser integration tests', () => {
|
||||
beforeEach(async () => {
|
||||
await copyPublicDeps();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await stopServer();
|
||||
await cleanPublicDeps();
|
||||
});
|
||||
|
||||
it('Some test', async () => {
|
||||
console.log('test: starting server...');
|
||||
await startServer();
|
||||
console.log('test: navigating to page...');
|
||||
await page.goto('http://localhost:8080/');
|
||||
|
||||
console.log('test: running script in browser...');
|
||||
const res = await page.evaluate(() => {
|
||||
// @ts-ignore
|
||||
return window.MAIN();
|
||||
});
|
||||
|
||||
console.log('test: checking expectations...');
|
||||
await expect(res).toMatchObject({
|
||||
retCode: 0,
|
||||
errorMessage: '',
|
||||
});
|
||||
});
|
||||
});
|
@ -1,7 +0,0 @@
|
||||
{
|
||||
"extends": "../../../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist"
|
||||
},
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
@ -1,65 +0,0 @@
|
||||
// Generated using webpack-cli https://github.com/webpack/webpack-cli
|
||||
|
||||
const path = require('path');
|
||||
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
||||
|
||||
const isProduction = process.env.NODE_ENV == 'production';
|
||||
|
||||
const stylesHandler = 'style-loader';
|
||||
|
||||
const config = {
|
||||
entry: './src/index.ts',
|
||||
output: {
|
||||
path: path.resolve(__dirname, 'dist'),
|
||||
},
|
||||
devServer: {
|
||||
open: true,
|
||||
host: 'localhost',
|
||||
static: {
|
||||
directory: path.join(__dirname, 'public'),
|
||||
},
|
||||
},
|
||||
plugins: [
|
||||
new HtmlWebpackPlugin({
|
||||
template: 'index.html',
|
||||
}),
|
||||
|
||||
// Add your plugins here
|
||||
// Learn more about plugins from https://webpack.js.org/configuration/plugins/
|
||||
],
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.(ts|tsx)$/i,
|
||||
loader: 'ts-loader',
|
||||
exclude: ['/node_modules/'],
|
||||
},
|
||||
{
|
||||
test: /\.css$/i,
|
||||
use: [stylesHandler, 'css-loader'],
|
||||
},
|
||||
{
|
||||
test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
|
||||
type: 'asset',
|
||||
},
|
||||
|
||||
// Add your rules for custom modules here
|
||||
// Learn more about loaders from https://webpack.js.org/loaders/
|
||||
],
|
||||
},
|
||||
resolve: {
|
||||
extensions: ['.tsx', '.ts', '.js'],
|
||||
fallback: {
|
||||
buffer: require.resolve('buffer/'),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = () => {
|
||||
if (isProduction) {
|
||||
config.mode = 'production';
|
||||
} else {
|
||||
config.mode = 'development';
|
||||
}
|
||||
return config;
|
||||
};
|
@ -11,8 +11,7 @@
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"test": "node --loader ts-node/esm ./src/index.ts",
|
||||
"test_logs": "DEBUG=fluence:particle:* node --loader ts-node/esm ./src/index.ts"
|
||||
"test_commented_out": "node --loader ts-node/esm ./src/index.ts"
|
||||
},
|
||||
"repository": "https://github.com/fluencelabs/fluence-js",
|
||||
"author": "Fluence Labs",
|
||||
|
@ -24,6 +24,7 @@
|
||||
},
|
||||
"scripts": {
|
||||
"test_commented_out": "node --loader ts-node/esm ./test/index.ts",
|
||||
"simulate-cdn": "http-server -p 8766 ../../../client/js-client.web.standalone/dist",
|
||||
"start": "react-scripts start",
|
||||
"build": "react-scripts build",
|
||||
"_test": "react-scripts test",
|
||||
|
@ -11,6 +11,7 @@
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"simulate-cdn": "http-server -p 8765 ../../../client/js-client.web.standalone/dist",
|
||||
"test_commented_out": "node --loader ts-node/esm ./src/index.ts",
|
||||
"serve": "http-server public"
|
||||
},
|
||||
|
Reference in New Issue
Block a user