121 lines
3.1 KiB
TypeScript
Raw Normal View History

/**
* Copyright 2023 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Fluence, type ClientConfig } from "@fluencelabs/js-client";
import { fromByteArray } from "base64-js";
import { test as particleTest } from "./_aqua/finalize_particle.js";
import {
registerHelloWorld,
helloTest,
marineTest,
} from "./_aqua/smoke_test.js";
import { wasm } from "./wasmb64.js";
const relay = {
multiaddr:
"/ip4/127.0.0.1/tcp/9991/ws/p2p/12D3KooWBM3SdXWqGaawQDGQ6JprtwswEg3FWGvGhmgmMez1vRbR",
peerId: "12D3KooWBM3SdXWqGaawQDGQ6JprtwswEg3FWGvGhmgmMez1vRbR",
};
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 = (): ClientConfig => {
return {
keyPair: {
type: "Ed25519",
source: generateRandomUint8Array(),
},
} as const;
};
export type TestResult =
| { type: "success"; data: string }
| { type: "failure"; error: string };
export const runTest = async (): Promise<TestResult> => {
try {
Fluence.onConnectionStateChange((state) => {
console.info("connection state changed: ", state);
});
console.log("connecting to Fluence Network...");
console.log("multiaddr: ", relay.multiaddr);
2023-10-18 11:54:27 +07:00
await Fluence.connect(relay, {
...optsWithRandomKeyPair(),
CDNUrl: "http://localhost:3001",
});
console.log("connected");
const relayPeerId = Fluence.getClient().getRelayPeerId();
console.log("relay:", relayPeerId);
registerHelloWorld({
hello(str) {
return "Hello, " + str + "!";
},
});
const client = Fluence.getClient();
console.log("my peer id: ", client.getPeerId());
console.log("my sk id: ", fromByteArray(client.getPeerSecretKey()));
console.log("running hello test...");
const hello = await helloTest();
console.log("hello test finished, result: ", hello);
console.log("running marine test...");
const marine = await marineTest(wasm);
console.log("running particle test...");
await particleTest();
console.log("marine test finished, result: ", marine);
const returnVal = {
hello,
marine,
};
return { type: "success", data: JSON.stringify(returnVal) };
} finally {
console.log("disconnecting from Fluence Network...");
await Fluence.disconnect();
console.log("disconnected");
}
};
export const runMain = () => {
runTest()
.then(() => {
console.log("done!");
})
.catch((err) => {
console.error("error: ", err);
});
};