mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-05-16 19:31:18 +00:00
Refactor tests
This commit is contained in:
parent
927277330e
commit
3a039cd355
@ -9,13 +9,16 @@
|
||||
* If you find any bugs in generated JS/TS, please write an issue on GitHub: https://github.com/fluencelabs/js-client/issues
|
||||
*
|
||||
*/
|
||||
import type { IFluenceClient as IFluenceClient$$, ParticleContext as ParticleContext$$ } from '@fluencelabs/js-client';
|
||||
import type {
|
||||
IFluenceClient as IFluenceClient$$,
|
||||
ParticleContext as ParticleContext$$,
|
||||
} from "@fluencelabs/js-client";
|
||||
|
||||
import {
|
||||
v5_callFunction as callFunction$$,
|
||||
v5_registerService as registerService$$,
|
||||
FluencePeer as FluencePeer$$
|
||||
} from '@fluencelabs/js-client';
|
||||
v5_callFunction as callFunction$$,
|
||||
v5_registerService as registerService$$,
|
||||
FluencePeer as FluencePeer$$,
|
||||
} from "@fluencelabs/js-client";
|
||||
|
||||
/**
|
||||
* @typedef {import("@fluencelabs/js-client").NonArrowSimpleType} NonArrowSimpleType
|
||||
@ -28,53 +31,48 @@ import {
|
||||
* @returns {JSONValue} value represented in typescript
|
||||
*/
|
||||
export function aqua2ts(value, schema) {
|
||||
if (schema.tag === "nil") {
|
||||
return null;
|
||||
if (schema.tag === "nil") {
|
||||
return null;
|
||||
} else if (schema.tag === "option") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
else if (schema.tag === "option") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
if (value.length === 0) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return aqua2ts(value[0], schema.type);
|
||||
}
|
||||
if (value.length === 0) {
|
||||
return null;
|
||||
} else {
|
||||
return aqua2ts(value[0], schema.type);
|
||||
}
|
||||
else if (schema.tag === "scalar" ||
|
||||
schema.tag === "bottomType" ||
|
||||
schema.tag === "topType") {
|
||||
return value;
|
||||
} else if (
|
||||
schema.tag === "scalar" ||
|
||||
schema.tag === "bottomType" ||
|
||||
schema.tag === "topType"
|
||||
) {
|
||||
return value;
|
||||
} else if (schema.tag === "array") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
else if (schema.tag === "array") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
return value.map((y) => {
|
||||
return aqua2ts(y, schema.type);
|
||||
});
|
||||
return value.map((y) => {
|
||||
return aqua2ts(y, schema.type);
|
||||
});
|
||||
} else if (schema.tag === "unlabeledProduct") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
else if (schema.tag === "unlabeledProduct") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
return value.map((y, i) => {
|
||||
return aqua2ts(y, schema.items[i]);
|
||||
});
|
||||
}
|
||||
else if (schema.tag === "struct" || schema.tag === "labeledProduct") {
|
||||
if (typeof value !== "object" || value == null || Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
return Object.entries(schema.fields).reduce((agg, [key, type]) => {
|
||||
const val = aqua2ts(value[key], type);
|
||||
return { ...agg, [key]: val };
|
||||
}, {});
|
||||
}
|
||||
else {
|
||||
throw new Error("Unexpected tag: " + JSON.stringify(schema));
|
||||
return value.map((y, i) => {
|
||||
return aqua2ts(y, schema.items[i]);
|
||||
});
|
||||
} else if (schema.tag === "struct" || schema.tag === "labeledProduct") {
|
||||
if (typeof value !== "object" || value == null || Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
return Object.entries(schema.fields).reduce((agg, [key, type]) => {
|
||||
const val = aqua2ts(value[key], type);
|
||||
return { ...agg, [key]: val };
|
||||
}, {});
|
||||
} else {
|
||||
throw new Error("Unexpected tag: " + JSON.stringify(schema));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Convert value from its typescript representation to representation in aqua
|
||||
@ -83,52 +81,46 @@ export function aqua2ts(value, schema) {
|
||||
* @returns {JSONValue} represented in aqua
|
||||
*/
|
||||
export function ts2aqua(value, schema) {
|
||||
if (schema.tag === "nil") {
|
||||
return null;
|
||||
if (schema.tag === "nil") {
|
||||
return null;
|
||||
} else if (schema.tag === "option") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
else if (schema.tag === "option") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
return value === null ? [] : [ts2aqua(value, schema.type)];
|
||||
return value === null ? [] : [ts2aqua(value, schema.type)];
|
||||
} else if (
|
||||
schema.tag === "scalar" ||
|
||||
schema.tag === "bottomType" ||
|
||||
schema.tag === "topType"
|
||||
) {
|
||||
return value;
|
||||
} else if (schema.tag === "array") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
else if (schema.tag === "scalar" ||
|
||||
schema.tag === "bottomType" ||
|
||||
schema.tag === "topType") {
|
||||
return value;
|
||||
return value.map((y) => {
|
||||
return ts2aqua(y, schema.type);
|
||||
});
|
||||
} else if (schema.tag === "unlabeledProduct") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
else if (schema.tag === "array") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
return value.map((y) => {
|
||||
return ts2aqua(y, schema.type);
|
||||
});
|
||||
}
|
||||
else if (schema.tag === "unlabeledProduct") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
return value.map((y, i) => {
|
||||
return ts2aqua(y, schema.items[i]);
|
||||
});
|
||||
}
|
||||
else if (schema.tag === "struct" || schema.tag === "labeledProduct") {
|
||||
if (typeof value !== "object" || value == null || Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
return Object.entries(schema.fields).reduce((agg, [key, type]) => {
|
||||
const val = ts2aqua(value[key], type);
|
||||
return { ...agg, [key]: val };
|
||||
}, {});
|
||||
}
|
||||
else {
|
||||
throw new Error("Unexpected tag: " + JSON.stringify(schema));
|
||||
return value.map((y, i) => {
|
||||
return ts2aqua(y, schema.items[i]);
|
||||
});
|
||||
} else if (schema.tag === "struct" || schema.tag === "labeledProduct") {
|
||||
if (typeof value !== "object" || value == null || Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
return Object.entries(schema.fields).reduce((agg, [key, type]) => {
|
||||
const val = ts2aqua(value[key], type);
|
||||
return { ...agg, [key]: val };
|
||||
}, {});
|
||||
} else {
|
||||
throw new Error("Unexpected tag: " + JSON.stringify(schema));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Functions
|
||||
export const test_script = `
|
||||
(seq
|
||||
@ -143,41 +135,38 @@ export const test_script = `
|
||||
)
|
||||
`;
|
||||
|
||||
export function test(
|
||||
config?: {ttl?: number}
|
||||
): Promise<void>;
|
||||
export function test(config?: { ttl?: number }): Promise<void>;
|
||||
|
||||
export function test(
|
||||
peer: IFluenceClient$$,
|
||||
config?: {ttl?: number}
|
||||
peer: IFluenceClient$$,
|
||||
config?: { ttl?: number },
|
||||
): Promise<void>;
|
||||
|
||||
export async function test(...args: any[]) {
|
||||
const argNames = [];
|
||||
const argCount = argNames.length;
|
||||
let peer = undefined;
|
||||
if (args[0] instanceof FluencePeer$$) {
|
||||
peer = args[0];
|
||||
args = args.slice(1);
|
||||
}
|
||||
const argNames = [];
|
||||
const argCount = argNames.length;
|
||||
let peer = undefined;
|
||||
if (args[0] instanceof FluencePeer$$) {
|
||||
peer = args[0];
|
||||
args = args.slice(1);
|
||||
}
|
||||
|
||||
const callArgs = Object.fromEntries(
|
||||
args.slice(0, argCount).map((arg, i) => [argNames[i], arg]),
|
||||
);
|
||||
|
||||
const callArgs = Object.fromEntries(args.slice(0, argCount).map((arg, i) => [argNames[i], arg]));
|
||||
const params = {
|
||||
peer,
|
||||
args: callArgs,
|
||||
config: args[argCount],
|
||||
};
|
||||
|
||||
const params = ({
|
||||
peer,
|
||||
args: callArgs,
|
||||
config: args[argCount]
|
||||
});
|
||||
const result = await callFunction$$({
|
||||
script: test_script,
|
||||
...params,
|
||||
});
|
||||
|
||||
const result = await callFunction$$({
|
||||
script: test_script,
|
||||
...params,
|
||||
});
|
||||
|
||||
return aqua2ts(result,
|
||||
{
|
||||
"tag": "nil"
|
||||
}
|
||||
);
|
||||
return aqua2ts(result, {
|
||||
tag: "nil",
|
||||
});
|
||||
}
|
||||
|
@ -9,13 +9,16 @@
|
||||
* If you find any bugs in generated JS/TS, please write an issue on GitHub: https://github.com/fluencelabs/js-client/issues
|
||||
*
|
||||
*/
|
||||
import type { IFluenceClient as IFluenceClient$$, ParticleContext as ParticleContext$$ } from '@fluencelabs/js-client';
|
||||
import type {
|
||||
IFluenceClient as IFluenceClient$$,
|
||||
ParticleContext as ParticleContext$$,
|
||||
} from "@fluencelabs/js-client";
|
||||
|
||||
import {
|
||||
v5_callFunction as callFunction$$,
|
||||
v5_registerService as registerService$$,
|
||||
FluencePeer as FluencePeer$$
|
||||
} from '@fluencelabs/js-client';
|
||||
v5_callFunction as callFunction$$,
|
||||
v5_registerService as registerService$$,
|
||||
FluencePeer as FluencePeer$$,
|
||||
} from "@fluencelabs/js-client";
|
||||
|
||||
/**
|
||||
* @typedef {import("@fluencelabs/js-client").NonArrowSimpleType} NonArrowSimpleType
|
||||
@ -28,53 +31,48 @@ import {
|
||||
* @returns {JSONValue} value represented in typescript
|
||||
*/
|
||||
export function aqua2ts(value, schema) {
|
||||
if (schema.tag === "nil") {
|
||||
return null;
|
||||
if (schema.tag === "nil") {
|
||||
return null;
|
||||
} else if (schema.tag === "option") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
else if (schema.tag === "option") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
if (value.length === 0) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return aqua2ts(value[0], schema.type);
|
||||
}
|
||||
if (value.length === 0) {
|
||||
return null;
|
||||
} else {
|
||||
return aqua2ts(value[0], schema.type);
|
||||
}
|
||||
else if (schema.tag === "scalar" ||
|
||||
schema.tag === "bottomType" ||
|
||||
schema.tag === "topType") {
|
||||
return value;
|
||||
} else if (
|
||||
schema.tag === "scalar" ||
|
||||
schema.tag === "bottomType" ||
|
||||
schema.tag === "topType"
|
||||
) {
|
||||
return value;
|
||||
} else if (schema.tag === "array") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
else if (schema.tag === "array") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
return value.map((y) => {
|
||||
return aqua2ts(y, schema.type);
|
||||
});
|
||||
return value.map((y) => {
|
||||
return aqua2ts(y, schema.type);
|
||||
});
|
||||
} else if (schema.tag === "unlabeledProduct") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
else if (schema.tag === "unlabeledProduct") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
return value.map((y, i) => {
|
||||
return aqua2ts(y, schema.items[i]);
|
||||
});
|
||||
}
|
||||
else if (schema.tag === "struct" || schema.tag === "labeledProduct") {
|
||||
if (typeof value !== "object" || value == null || Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
return Object.entries(schema.fields).reduce((agg, [key, type]) => {
|
||||
const val = aqua2ts(value[key], type);
|
||||
return { ...agg, [key]: val };
|
||||
}, {});
|
||||
}
|
||||
else {
|
||||
throw new Error("Unexpected tag: " + JSON.stringify(schema));
|
||||
return value.map((y, i) => {
|
||||
return aqua2ts(y, schema.items[i]);
|
||||
});
|
||||
} else if (schema.tag === "struct" || schema.tag === "labeledProduct") {
|
||||
if (typeof value !== "object" || value == null || Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
return Object.entries(schema.fields).reduce((agg, [key, type]) => {
|
||||
const val = aqua2ts(value[key], type);
|
||||
return { ...agg, [key]: val };
|
||||
}, {});
|
||||
} else {
|
||||
throw new Error("Unexpected tag: " + JSON.stringify(schema));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Convert value from its typescript representation to representation in aqua
|
||||
@ -83,146 +81,196 @@ export function aqua2ts(value, schema) {
|
||||
* @returns {JSONValue} represented in aqua
|
||||
*/
|
||||
export function ts2aqua(value, schema) {
|
||||
if (schema.tag === "nil") {
|
||||
return null;
|
||||
if (schema.tag === "nil") {
|
||||
return null;
|
||||
} else if (schema.tag === "option") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
else if (schema.tag === "option") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
return value === null ? [] : [ts2aqua(value, schema.type)];
|
||||
return value === null ? [] : [ts2aqua(value, schema.type)];
|
||||
} else if (
|
||||
schema.tag === "scalar" ||
|
||||
schema.tag === "bottomType" ||
|
||||
schema.tag === "topType"
|
||||
) {
|
||||
return value;
|
||||
} else if (schema.tag === "array") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
else if (schema.tag === "scalar" ||
|
||||
schema.tag === "bottomType" ||
|
||||
schema.tag === "topType") {
|
||||
return value;
|
||||
return value.map((y) => {
|
||||
return ts2aqua(y, schema.type);
|
||||
});
|
||||
} else if (schema.tag === "unlabeledProduct") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
else if (schema.tag === "array") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
return value.map((y) => {
|
||||
return ts2aqua(y, schema.type);
|
||||
});
|
||||
}
|
||||
else if (schema.tag === "unlabeledProduct") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
return value.map((y, i) => {
|
||||
return ts2aqua(y, schema.items[i]);
|
||||
});
|
||||
}
|
||||
else if (schema.tag === "struct" || schema.tag === "labeledProduct") {
|
||||
if (typeof value !== "object" || value == null || Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
return Object.entries(schema.fields).reduce((agg, [key, type]) => {
|
||||
const val = ts2aqua(value[key], type);
|
||||
return { ...agg, [key]: val };
|
||||
}, {});
|
||||
}
|
||||
else {
|
||||
throw new Error("Unexpected tag: " + JSON.stringify(schema));
|
||||
return value.map((y, i) => {
|
||||
return ts2aqua(y, schema.items[i]);
|
||||
});
|
||||
} else if (schema.tag === "struct" || schema.tag === "labeledProduct") {
|
||||
if (typeof value !== "object" || value == null || Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
return Object.entries(schema.fields).reduce((agg, [key, type]) => {
|
||||
const val = ts2aqua(value[key], type);
|
||||
return { ...agg, [key]: val };
|
||||
}, {});
|
||||
} else {
|
||||
throw new Error("Unexpected tag: " + JSON.stringify(schema));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Services
|
||||
export interface SrvDef {
|
||||
create: (wasm_b64_content: string, callParams: ParticleContext$$) => { error: string | null; service_id: string | null; success: boolean; } | Promise<{ error: string | null; service_id: string | null; success: boolean; }>;
|
||||
list: (callParams: ParticleContext$$) => string[] | Promise<string[]>;
|
||||
remove: (service_id: string, callParams: ParticleContext$$) => { error: string | null; success: boolean; } | Promise<{ error: string | null; success: boolean; }>;
|
||||
create: (
|
||||
wasm_b64_content: string,
|
||||
callParams: ParticleContext$$,
|
||||
) =>
|
||||
| { error: string | null; service_id: string | null; success: boolean }
|
||||
| Promise<{
|
||||
error: string | null;
|
||||
service_id: string | null;
|
||||
success: boolean;
|
||||
}>;
|
||||
list: (callParams: ParticleContext$$) => string[] | Promise<string[]>;
|
||||
remove: (
|
||||
service_id: string,
|
||||
callParams: ParticleContext$$,
|
||||
) =>
|
||||
| { error: string | null; success: boolean }
|
||||
| Promise<{ error: string | null; success: boolean }>;
|
||||
}
|
||||
export function registerSrv(service: SrvDef): void;
|
||||
export function registerSrv(serviceId: string, service: SrvDef): void;
|
||||
export function registerSrv(peer: IFluenceClient$$, service: SrvDef): void;
|
||||
export function registerSrv(peer: IFluenceClient$$, serviceId: string, service: SrvDef): void;
|
||||
export function registerSrv(
|
||||
peer: IFluenceClient$$,
|
||||
serviceId: string,
|
||||
service: SrvDef,
|
||||
): void;
|
||||
export function registerSrv(...args: any[]) {
|
||||
const service = args.pop();
|
||||
const defaultServiceId = "single_module_srv";
|
||||
const service = args.pop();
|
||||
const defaultServiceId = "single_module_srv";
|
||||
|
||||
const params = args[0] instanceof FluencePeer$$ ? ({
|
||||
peer: args[0],
|
||||
serviceId: args[1] ?? defaultServiceId
|
||||
}) : ({
|
||||
peer: undefined,
|
||||
serviceId: args[0] ?? defaultServiceId
|
||||
});
|
||||
const params =
|
||||
args[0] instanceof FluencePeer$$
|
||||
? {
|
||||
peer: args[0],
|
||||
serviceId: args[1] ?? defaultServiceId,
|
||||
}
|
||||
: {
|
||||
peer: undefined,
|
||||
serviceId: args[0] ?? defaultServiceId,
|
||||
};
|
||||
|
||||
if (params.serviceId == null) {
|
||||
throw new Error("Service ID is not provided");
|
||||
}
|
||||
if (params.serviceId == null) {
|
||||
throw new Error("Service ID is not provided");
|
||||
}
|
||||
|
||||
registerService$$({
|
||||
service,
|
||||
...params
|
||||
});
|
||||
registerService$$({
|
||||
service,
|
||||
...params,
|
||||
});
|
||||
}
|
||||
|
||||
export interface CalcServiceDef {
|
||||
divide: (num: number, callParams: ParticleContext$$) => number | Promise<number>;
|
||||
clear_state: (callParams: ParticleContext$$) => void | Promise<void>;
|
||||
test_logs: (callParams: ParticleContext$$) => void | Promise<void>;
|
||||
multiply: (num: number, callParams: ParticleContext$$) => number | Promise<number>;
|
||||
add: (num: number, callParams: ParticleContext$$) => number | Promise<number>;
|
||||
state: (callParams: ParticleContext$$) => number | Promise<number>;
|
||||
subtract: (num: number, callParams: ParticleContext$$) => number | Promise<number>;
|
||||
divide: (
|
||||
num: number,
|
||||
callParams: ParticleContext$$,
|
||||
) => number | Promise<number>;
|
||||
clear_state: (callParams: ParticleContext$$) => void | Promise<void>;
|
||||
test_logs: (callParams: ParticleContext$$) => void | Promise<void>;
|
||||
multiply: (
|
||||
num: number,
|
||||
callParams: ParticleContext$$,
|
||||
) => number | Promise<number>;
|
||||
add: (num: number, callParams: ParticleContext$$) => number | Promise<number>;
|
||||
state: (callParams: ParticleContext$$) => number | Promise<number>;
|
||||
subtract: (
|
||||
num: number,
|
||||
callParams: ParticleContext$$,
|
||||
) => number | Promise<number>;
|
||||
}
|
||||
export function registerCalcService(serviceId: string, service: CalcServiceDef): void;
|
||||
export function registerCalcService(peer: IFluenceClient$$, serviceId: string, service: CalcServiceDef): void;
|
||||
export function registerCalcService(
|
||||
serviceId: string,
|
||||
service: CalcServiceDef,
|
||||
): void;
|
||||
export function registerCalcService(
|
||||
peer: IFluenceClient$$,
|
||||
serviceId: string,
|
||||
service: CalcServiceDef,
|
||||
): void;
|
||||
export function registerCalcService(...args: any[]) {
|
||||
const service = args.pop();
|
||||
const defaultServiceId = undefined;
|
||||
const service = args.pop();
|
||||
const defaultServiceId = undefined;
|
||||
|
||||
const params = args[0] instanceof FluencePeer$$ ? ({
|
||||
peer: args[0],
|
||||
serviceId: args[1] ?? defaultServiceId
|
||||
}) : ({
|
||||
peer: undefined,
|
||||
serviceId: args[0] ?? defaultServiceId
|
||||
});
|
||||
const params =
|
||||
args[0] instanceof FluencePeer$$
|
||||
? {
|
||||
peer: args[0],
|
||||
serviceId: args[1] ?? defaultServiceId,
|
||||
}
|
||||
: {
|
||||
peer: undefined,
|
||||
serviceId: args[0] ?? defaultServiceId,
|
||||
};
|
||||
|
||||
if (params.serviceId == null) {
|
||||
throw new Error("Service ID is not provided");
|
||||
}
|
||||
if (params.serviceId == null) {
|
||||
throw new Error("Service ID is not provided");
|
||||
}
|
||||
|
||||
registerService$$({
|
||||
service,
|
||||
...params
|
||||
});
|
||||
registerService$$({
|
||||
service,
|
||||
...params,
|
||||
});
|
||||
}
|
||||
|
||||
export interface HelloWorldDef {
|
||||
hello: (str: string, callParams: ParticleContext$$) => string | Promise<string>;
|
||||
hello: (
|
||||
str: string,
|
||||
callParams: ParticleContext$$,
|
||||
) => string | Promise<string>;
|
||||
}
|
||||
export function registerHelloWorld(service: HelloWorldDef): void;
|
||||
export function registerHelloWorld(serviceId: string, service: HelloWorldDef): void;
|
||||
export function registerHelloWorld(peer: IFluenceClient$$, service: HelloWorldDef): void;
|
||||
export function registerHelloWorld(peer: IFluenceClient$$, serviceId: string, service: HelloWorldDef): void;
|
||||
export function registerHelloWorld(
|
||||
serviceId: string,
|
||||
service: HelloWorldDef,
|
||||
): void;
|
||||
export function registerHelloWorld(
|
||||
peer: IFluenceClient$$,
|
||||
service: HelloWorldDef,
|
||||
): void;
|
||||
export function registerHelloWorld(
|
||||
peer: IFluenceClient$$,
|
||||
serviceId: string,
|
||||
service: HelloWorldDef,
|
||||
): void;
|
||||
export function registerHelloWorld(...args: any[]) {
|
||||
const service = args.pop();
|
||||
const defaultServiceId = "hello-world";
|
||||
const service = args.pop();
|
||||
const defaultServiceId = "hello-world";
|
||||
|
||||
const params = args[0] instanceof FluencePeer$$ ? ({
|
||||
peer: args[0],
|
||||
serviceId: args[1] ?? defaultServiceId
|
||||
}) : ({
|
||||
peer: undefined,
|
||||
serviceId: args[0] ?? defaultServiceId
|
||||
});
|
||||
const params =
|
||||
args[0] instanceof FluencePeer$$
|
||||
? {
|
||||
peer: args[0],
|
||||
serviceId: args[1] ?? defaultServiceId,
|
||||
}
|
||||
: {
|
||||
peer: undefined,
|
||||
serviceId: args[0] ?? defaultServiceId,
|
||||
};
|
||||
|
||||
if (params.serviceId == null) {
|
||||
throw new Error("Service ID is not provided");
|
||||
}
|
||||
if (params.serviceId == null) {
|
||||
throw new Error("Service ID is not provided");
|
||||
}
|
||||
|
||||
registerService$$({
|
||||
service,
|
||||
...params
|
||||
});
|
||||
registerService$$({
|
||||
service,
|
||||
...params,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Functions
|
||||
export const resourceTest_script = `
|
||||
(seq
|
||||
@ -447,63 +495,62 @@ export const resourceTest_script = `
|
||||
)
|
||||
`;
|
||||
|
||||
export type ResourceTestResult = [string | null, string[]]
|
||||
export type ResourceTestResult = [string | null, string[]];
|
||||
|
||||
export function resourceTest(
|
||||
label: string,
|
||||
config?: {ttl?: number}
|
||||
label: string,
|
||||
config?: { ttl?: number },
|
||||
): Promise<ResourceTestResult>;
|
||||
|
||||
export function resourceTest(
|
||||
peer: IFluenceClient$$,
|
||||
label: string,
|
||||
config?: {ttl?: number}
|
||||
peer: IFluenceClient$$,
|
||||
label: string,
|
||||
config?: { ttl?: number },
|
||||
): Promise<ResourceTestResult>;
|
||||
|
||||
export async function resourceTest(...args: any[]) {
|
||||
const argNames = ["label"];
|
||||
const argCount = argNames.length;
|
||||
let peer = undefined;
|
||||
if (args[0] instanceof FluencePeer$$) {
|
||||
peer = args[0];
|
||||
args = args.slice(1);
|
||||
}
|
||||
const argNames = ["label"];
|
||||
const argCount = argNames.length;
|
||||
let peer = undefined;
|
||||
if (args[0] instanceof FluencePeer$$) {
|
||||
peer = args[0];
|
||||
args = args.slice(1);
|
||||
}
|
||||
|
||||
const callArgs = Object.fromEntries(
|
||||
args.slice(0, argCount).map((arg, i) => [argNames[i], arg]),
|
||||
);
|
||||
|
||||
const callArgs = Object.fromEntries(args.slice(0, argCount).map((arg, i) => [argNames[i], arg]));
|
||||
const params = {
|
||||
peer,
|
||||
args: callArgs,
|
||||
config: args[argCount],
|
||||
};
|
||||
|
||||
const params = ({
|
||||
peer,
|
||||
args: callArgs,
|
||||
config: args[argCount]
|
||||
});
|
||||
const result = await callFunction$$({
|
||||
script: resourceTest_script,
|
||||
...params,
|
||||
});
|
||||
|
||||
const result = await callFunction$$({
|
||||
script: resourceTest_script,
|
||||
...params,
|
||||
});
|
||||
|
||||
return aqua2ts(result,
|
||||
{
|
||||
"items": [
|
||||
{
|
||||
"type": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
},
|
||||
"tag": "option"
|
||||
return aqua2ts(result, {
|
||||
items: [
|
||||
{
|
||||
type: {
|
||||
name: "string",
|
||||
tag: "scalar",
|
||||
},
|
||||
{
|
||||
"type": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
},
|
||||
"tag": "array"
|
||||
}
|
||||
tag: "option",
|
||||
},
|
||||
{
|
||||
type: {
|
||||
name: "string",
|
||||
tag: "scalar",
|
||||
},
|
||||
tag: "array",
|
||||
},
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
}
|
||||
);
|
||||
tag: "unlabeledProduct",
|
||||
});
|
||||
}
|
||||
|
||||
export const helloTest_script = `
|
||||
@ -519,44 +566,41 @@ export const helloTest_script = `
|
||||
)
|
||||
`;
|
||||
|
||||
export function helloTest(
|
||||
config?: {ttl?: number}
|
||||
): Promise<string>;
|
||||
export function helloTest(config?: { ttl?: number }): Promise<string>;
|
||||
|
||||
export function helloTest(
|
||||
peer: IFluenceClient$$,
|
||||
config?: {ttl?: number}
|
||||
peer: IFluenceClient$$,
|
||||
config?: { ttl?: number },
|
||||
): Promise<string>;
|
||||
|
||||
export async function helloTest(...args: any[]) {
|
||||
const argNames = [];
|
||||
const argCount = argNames.length;
|
||||
let peer = undefined;
|
||||
if (args[0] instanceof FluencePeer$$) {
|
||||
peer = args[0];
|
||||
args = args.slice(1);
|
||||
}
|
||||
const argNames = [];
|
||||
const argCount = argNames.length;
|
||||
let peer = undefined;
|
||||
if (args[0] instanceof FluencePeer$$) {
|
||||
peer = args[0];
|
||||
args = args.slice(1);
|
||||
}
|
||||
|
||||
const callArgs = Object.fromEntries(
|
||||
args.slice(0, argCount).map((arg, i) => [argNames[i], arg]),
|
||||
);
|
||||
|
||||
const callArgs = Object.fromEntries(args.slice(0, argCount).map((arg, i) => [argNames[i], arg]));
|
||||
const params = {
|
||||
peer,
|
||||
args: callArgs,
|
||||
config: args[argCount],
|
||||
};
|
||||
|
||||
const params = ({
|
||||
peer,
|
||||
args: callArgs,
|
||||
config: args[argCount]
|
||||
});
|
||||
const result = await callFunction$$({
|
||||
script: helloTest_script,
|
||||
...params,
|
||||
});
|
||||
|
||||
const result = await callFunction$$({
|
||||
script: helloTest_script,
|
||||
...params,
|
||||
});
|
||||
|
||||
return aqua2ts(result,
|
||||
{
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
}
|
||||
);
|
||||
return aqua2ts(result, {
|
||||
name: "string",
|
||||
tag: "scalar",
|
||||
});
|
||||
}
|
||||
|
||||
export const callHappy_script = `
|
||||
@ -585,51 +629,50 @@ export const callHappy_script = `
|
||||
`;
|
||||
|
||||
export function callHappy(
|
||||
a: string,
|
||||
b: number,
|
||||
c: number,
|
||||
d: (arg0: string) => number | Promise<number>,
|
||||
config?: {ttl?: number}
|
||||
a: string,
|
||||
b: number,
|
||||
c: number,
|
||||
d: (arg0: string) => number | Promise<number>,
|
||||
config?: { ttl?: number },
|
||||
): Promise<number>;
|
||||
|
||||
export function callHappy(
|
||||
peer: IFluenceClient$$,
|
||||
a: string,
|
||||
b: number,
|
||||
c: number,
|
||||
d: (arg0: string) => number | Promise<number>,
|
||||
config?: {ttl?: number}
|
||||
peer: IFluenceClient$$,
|
||||
a: string,
|
||||
b: number,
|
||||
c: number,
|
||||
d: (arg0: string) => number | Promise<number>,
|
||||
config?: { ttl?: number },
|
||||
): Promise<number>;
|
||||
|
||||
export async function callHappy(...args: any[]) {
|
||||
const argNames = ["a", "b", "c", "d"];
|
||||
const argCount = argNames.length;
|
||||
let peer = undefined;
|
||||
if (args[0] instanceof FluencePeer$$) {
|
||||
peer = args[0];
|
||||
args = args.slice(1);
|
||||
}
|
||||
const argNames = ["a", "b", "c", "d"];
|
||||
const argCount = argNames.length;
|
||||
let peer = undefined;
|
||||
if (args[0] instanceof FluencePeer$$) {
|
||||
peer = args[0];
|
||||
args = args.slice(1);
|
||||
}
|
||||
|
||||
const callArgs = Object.fromEntries(
|
||||
args.slice(0, argCount).map((arg, i) => [argNames[i], arg]),
|
||||
);
|
||||
|
||||
const callArgs = Object.fromEntries(args.slice(0, argCount).map((arg, i) => [argNames[i], arg]));
|
||||
const params = {
|
||||
peer,
|
||||
args: callArgs,
|
||||
config: args[argCount],
|
||||
};
|
||||
|
||||
const params = ({
|
||||
peer,
|
||||
args: callArgs,
|
||||
config: args[argCount]
|
||||
});
|
||||
const result = await callFunction$$({
|
||||
script: callHappy_script,
|
||||
...params,
|
||||
});
|
||||
|
||||
const result = await callFunction$$({
|
||||
script: callHappy_script,
|
||||
...params,
|
||||
});
|
||||
|
||||
return aqua2ts(result,
|
||||
{
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
);
|
||||
return aqua2ts(result, {
|
||||
name: "f64",
|
||||
tag: "scalar",
|
||||
});
|
||||
}
|
||||
|
||||
export const demo_calculation_script = `
|
||||
@ -664,45 +707,44 @@ export const demo_calculation_script = `
|
||||
`;
|
||||
|
||||
export function demo_calculation(
|
||||
service_id: string,
|
||||
config?: {ttl?: number}
|
||||
service_id: string,
|
||||
config?: { ttl?: number },
|
||||
): Promise<number>;
|
||||
|
||||
export function demo_calculation(
|
||||
peer: IFluenceClient$$,
|
||||
service_id: string,
|
||||
config?: {ttl?: number}
|
||||
peer: IFluenceClient$$,
|
||||
service_id: string,
|
||||
config?: { ttl?: number },
|
||||
): Promise<number>;
|
||||
|
||||
export async function demo_calculation(...args: any[]) {
|
||||
const argNames = ["service_id"];
|
||||
const argCount = argNames.length;
|
||||
let peer = undefined;
|
||||
if (args[0] instanceof FluencePeer$$) {
|
||||
peer = args[0];
|
||||
args = args.slice(1);
|
||||
}
|
||||
const argNames = ["service_id"];
|
||||
const argCount = argNames.length;
|
||||
let peer = undefined;
|
||||
if (args[0] instanceof FluencePeer$$) {
|
||||
peer = args[0];
|
||||
args = args.slice(1);
|
||||
}
|
||||
|
||||
const callArgs = Object.fromEntries(
|
||||
args.slice(0, argCount).map((arg, i) => [argNames[i], arg]),
|
||||
);
|
||||
|
||||
const callArgs = Object.fromEntries(args.slice(0, argCount).map((arg, i) => [argNames[i], arg]));
|
||||
const params = {
|
||||
peer,
|
||||
args: callArgs,
|
||||
config: args[argCount],
|
||||
};
|
||||
|
||||
const params = ({
|
||||
peer,
|
||||
args: callArgs,
|
||||
config: args[argCount]
|
||||
});
|
||||
const result = await callFunction$$({
|
||||
script: demo_calculation_script,
|
||||
...params,
|
||||
});
|
||||
|
||||
const result = await callFunction$$({
|
||||
script: demo_calculation_script,
|
||||
...params,
|
||||
});
|
||||
|
||||
return aqua2ts(result,
|
||||
{
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
);
|
||||
return aqua2ts(result, {
|
||||
name: "f64",
|
||||
tag: "scalar",
|
||||
});
|
||||
}
|
||||
|
||||
export const marineTest_script = `
|
||||
@ -740,43 +782,42 @@ export const marineTest_script = `
|
||||
`;
|
||||
|
||||
export function marineTest(
|
||||
wasm64: string,
|
||||
config?: {ttl?: number}
|
||||
wasm64: string,
|
||||
config?: { ttl?: number },
|
||||
): Promise<number>;
|
||||
|
||||
export function marineTest(
|
||||
peer: IFluenceClient$$,
|
||||
wasm64: string,
|
||||
config?: {ttl?: number}
|
||||
peer: IFluenceClient$$,
|
||||
wasm64: string,
|
||||
config?: { ttl?: number },
|
||||
): Promise<number>;
|
||||
|
||||
export async function marineTest(...args: any[]) {
|
||||
const argNames = ["wasm64"];
|
||||
const argCount = argNames.length;
|
||||
let peer = undefined;
|
||||
if (args[0] instanceof FluencePeer$$) {
|
||||
peer = args[0];
|
||||
args = args.slice(1);
|
||||
}
|
||||
const argNames = ["wasm64"];
|
||||
const argCount = argNames.length;
|
||||
let peer = undefined;
|
||||
if (args[0] instanceof FluencePeer$$) {
|
||||
peer = args[0];
|
||||
args = args.slice(1);
|
||||
}
|
||||
|
||||
const callArgs = Object.fromEntries(
|
||||
args.slice(0, argCount).map((arg, i) => [argNames[i], arg]),
|
||||
);
|
||||
|
||||
const callArgs = Object.fromEntries(args.slice(0, argCount).map((arg, i) => [argNames[i], arg]));
|
||||
const params = {
|
||||
peer,
|
||||
args: callArgs,
|
||||
config: args[argCount],
|
||||
};
|
||||
|
||||
const params = ({
|
||||
peer,
|
||||
args: callArgs,
|
||||
config: args[argCount]
|
||||
});
|
||||
const result = await callFunction$$({
|
||||
script: marineTest_script,
|
||||
...params,
|
||||
});
|
||||
|
||||
const result = await callFunction$$({
|
||||
script: marineTest_script,
|
||||
...params,
|
||||
});
|
||||
|
||||
return aqua2ts(result,
|
||||
{
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
);
|
||||
return aqua2ts(result, {
|
||||
name: "f64",
|
||||
tag: "scalar",
|
||||
});
|
||||
}
|
||||
|
@ -14,10 +14,10 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { JSONValue, NonArrowType } from "@fluencelabs/interfaces";
|
||||
import { JSONValue, NonArrowSimpleType } from "@fluencelabs/interfaces";
|
||||
import { it, describe, expect, test } from "vitest";
|
||||
|
||||
import { aqua2ts, ts2aqua } from "../conversions.js";
|
||||
import { aqua2ts, ts2aqua } from "../converters.js";
|
||||
|
||||
const i32 = { tag: "scalar", name: "i32" } as const;
|
||||
|
||||
@ -172,7 +172,7 @@ const nestedStructs = [
|
||||
interface ConversionTestArgs {
|
||||
aqua: JSONValue;
|
||||
ts: JSONValue;
|
||||
type: NonArrowType;
|
||||
type: NonArrowSimpleType;
|
||||
}
|
||||
|
||||
describe("Conversion from aqua to typescript", () => {
|
@ -68,11 +68,7 @@ export function ts2aqua(value, schema) {
|
||||
if (schema.tag === "nil") {
|
||||
return null;
|
||||
} else if (schema.tag === "option") {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error("Bad schema");
|
||||
}
|
||||
|
||||
return value === null ? [] : [ts2aqua(value, schema.type)];
|
||||
return value == null ? [] : [ts2aqua(value, schema.type)];
|
||||
} else if (
|
||||
schema.tag === "scalar" ||
|
||||
schema.tag === "bottomType" ||
|
||||
|
@ -55,7 +55,6 @@
|
||||
"libp2p": "0.46.6",
|
||||
"multiformats": "11.0.1",
|
||||
"rxjs": "7.5.5",
|
||||
"ts-pattern": "3.3.3",
|
||||
"uint8arrays": "4.0.3",
|
||||
"uuid": "8.3.2",
|
||||
"zod": "3.22.4"
|
||||
|
@ -15,17 +15,17 @@
|
||||
*/
|
||||
|
||||
import { CallResultsArray } from "@fluencelabs/avm";
|
||||
import { JSONValue } from "@fluencelabs/interfaces";
|
||||
import { fromUint8Array, toUint8Array } from "js-base64";
|
||||
import { concat } from "uint8arrays/concat";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { z } from "zod";
|
||||
|
||||
import { ExpirationError } from "../jsPeer/errors.js";
|
||||
import { KeyPair } from "../keypair/index.js";
|
||||
import { numberToLittleEndianBytes } from "../util/bytes.js";
|
||||
|
||||
import { IParticle } from "./interfaces.js";
|
||||
import { JSONValue } from "@fluencelabs/interfaces";
|
||||
import { ExpirationError } from "../jsPeer/errors.js";
|
||||
|
||||
const particleSchema = z.object({
|
||||
id: z.string(),
|
||||
|
@ -71,6 +71,7 @@ describe("Sig service test suite", () => {
|
||||
});
|
||||
|
||||
const p = await peer.internals.createNewParticle(script);
|
||||
|
||||
peer.internals.initiateParticle(
|
||||
p,
|
||||
() => {},
|
||||
|
7
pnpm-lock.yaml
generated
7
pnpm-lock.yaml
generated
@ -280,9 +280,6 @@ importers:
|
||||
rxjs:
|
||||
specifier: 7.5.5
|
||||
version: 7.5.5
|
||||
ts-pattern:
|
||||
specifier: 3.3.3
|
||||
version: 3.3.3
|
||||
uint8arrays:
|
||||
specifier: 4.0.3
|
||||
version: 4.0.3
|
||||
@ -14060,10 +14057,6 @@ packages:
|
||||
yn: 3.1.1
|
||||
dev: true
|
||||
|
||||
/ts-pattern@3.3.3:
|
||||
resolution: {integrity: sha512-Z5EFi6g6wyX3uDFHqxF5W5c5h663oZg9O6aOiAT7fqNu0HPSfCxtHzrQ7SblTy738Mrg2Ezorky8H5aUOm8Pvg==}
|
||||
dev: false
|
||||
|
||||
/ts-pattern@5.0.5:
|
||||
resolution: {integrity: sha512-tL0w8U/pgaacOmkb9fRlYzWEUDCfVjjv9dD4wHTgZ61MjhuMt46VNWTG747NqW6vRzoWIKABVhFSOJ82FvXrfA==}
|
||||
dev: true
|
||||
|
Loading…
x
Reference in New Issue
Block a user