aqua-playground/src/__test__/examples.spec.ts
2023-01-19 12:09:57 +04:00

554 lines
18 KiB
TypeScript

import {getObjAssignCall, getObjCall, getObjRelayCall} from "../examples/objectCall";
jest.retryTimes(1)
import { Fluence, FluencePeer, KeyPair, setLogLevel } from '@fluencelabs/fluence';
import { EphemeralNetwork, defaultConfig } from '@fluencelabs/fluence/dist/internal/ephemeral';
import { registerPrintln } from '../compiled/examples/println';
import { callArrowCall, reproArgsBug426Call } from '../examples/callArrowCall';
import { dataAliasCall } from '../examples/dataAliasCall';
import { onCall } from '../examples/onCall';
import { funcCall } from '../examples/funcCall';
import { helloWorldCall } from '../examples/helloWorldCall';
import { foldBug499Call, foldCall } from '../examples/foldCall';
import { bugNG69Call, ifCall, ifWrapCall } from '../examples/ifCall';
import { parCall, testTimeoutCall } from '../examples/parCall';
import { complexCall } from '../examples/complex';
import { constantsCall, particleTtlAndTimestampCall } from '../examples/constantsCall';
import {
nilLengthCall,
nilLiteralCall,
returnNilCall,
returnNoneCall, streamAssignmentCall,
streamCall,
streamFunctorCall, streamIntFunctorCall, streamJoinCall,
streamReturnFromInnerFunc
} from '../examples/streamCall';
import { topologyBug205Call, topologyBug394Call, topologyBug427Call, topologyCall } from '../examples/topologyCall';
import { foldJoinCall } from '../examples/foldJoinCall';
import { registerHandlers, returnNull, returnOptionalCall, useOptionalCall } from '../examples/useOptionalCall';
import { viaArrCall, viaOptCall, viaOptNullCall, viaStreamCall } from '../examples/viaCall';
import { nestedFuncsCall } from '../examples/nestedFuncsCall';
import { assignmentCall } from '../examples/assignment';
import { tryCatchCall } from '../examples/tryCatchCall';
import { tryOtherwiseCall } from '../examples/tryOtherwiseCall';
import { coCall } from '../examples/coCall';
import { bugLNG60Call, passArgsCall } from '../examples/passArgsCall';
import { streamArgsCall } from '../examples/streamArgsCall';
import { streamResultsCall } from '../examples/streamResultsCall';
import { pushToStreamCall } from '../examples/pushToStreamCall';
import { literalCall } from '../examples/returnLiteralCall';
import { multiReturnCall } from '../examples/multiReturnCall';
import { declareCall } from '../examples/declareCall';
import {genOptions, genOptionsEmptyString} from '../examples/optionsCall';
import { closuresCall } from '../examples/closures';
import { bugLNG63_2Call, bugLNG63_3Call, bugLNG63Call, streamCanCall } from '../examples/streamCanCall';
import { streamCallbackCall } from '../examples/streamCallback';
import { streamResCall } from '../examples/streamRestrictionsCall';
import { joinIdxCall, joinIdxLocalCall, joinIdxRelayCall } from '../examples/joinCall';
import { recursiveStreamsCall } from '../examples/recursiveStreamsCall';
import {
arraySugarCall,
bugLNG59Call,
optionSugarCall,
streamSugarCall,
} from '../examples/collectionSugarCall';
import { funcsCall } from '../examples/funcsCall';
import { nestedDataCall } from '../examples/nestedDataCall';
import { mathTest1Call, mathTest2Call } from '../examples/mathCall';
import { lng58Bug } from '../compiled/examples/closures';
import { config, isEphemeral } from '../config';
import {bugLng79Call} from "../examples/canonCall";
var selfPeerId: string;
var peer2: FluencePeer;
var ephemeralNetwork: EphemeralNetwork;
// setLogLevel('debug');
async function startEphemeral() {
ephemeralNetwork = new EphemeralNetwork(defaultConfig);
await ephemeralNetwork.up();
const defaultPeer = Fluence.getPeer();
await defaultPeer.init({
KeyPair: await KeyPair.randomEd25519(),
});
const conn = ephemeralNetwork.getRelayConnection(config.relays[0].peerId, defaultPeer);
await defaultPeer.connect(conn);
selfPeerId = defaultPeer.getStatus().peerId;
peer2 = new FluencePeer();
await peer2.init({
KeyPair: await KeyPair.randomEd25519(),
});
const conn2 = ephemeralNetwork.getRelayConnection(config.relays[1].peerId, peer2);
await peer2.connect(conn2);
}
async function stopEphemeral() {
if (ephemeralNetwork) {
await ephemeralNetwork.down();
}
await Fluence.stop();
if (peer2) {
await peer2.stop();
}
}
async function start() {
// await Fluence.start({ connectTo: config.relays[0]});
await Fluence.start({ connectTo: config.relays[0], debug: {marineLogLevel: "debug"} });
selfPeerId = Fluence.getStatus().peerId;
peer2 = new FluencePeer();
await peer2.start({ connectTo: config.relays[1] });
}
async function stop() {
if (peer2) {
Fluence.stop();
await peer2.stop();
}
}
describe('Testing examples', () => {
beforeAll(async () => {
if (isEphemeral) {
await startEphemeral();
} else {
await start();
}
// this could be called from `println.aqua`
registerPrintln({
print: (arg0) => {
console.dir(arg0);
},
});
});
afterAll(async () => {
if (isEphemeral) {
await stopEphemeral();
} else {
await stop();
}
});
it('callArrow.aqua', async () => {
let callArrowResult = await callArrowCall();
expect(callArrowResult).toBe('Hello, callArrow call!');
});
it('callArrow.aqua args bug 426', async () => {
let argResult = await reproArgsBug426Call();
expect(argResult).toBe('privet');
});
it('streamRestrictions.aqua', async () => {
let streamResResult = await streamResCall();
expect(streamResResult).toEqual([[], ['a', 'b', 'c']]);
});
it('fold.aqua', async () => {
let foldCallResult = await foldCall();
expect(foldCallResult).toEqual(config.externalAddressesRelay1);
});
it('fold.aqua bug #499', async () => {
let foldCallResult = await foldBug499Call();
expect(foldCallResult).toEqual([5]);
});
it('if.aqua', async () => {
await ifCall();
});
it('if.aqua xor wrap', async () => {
let res = await ifWrapCall(peer2.getStatus().relayPeerId);
expect(res).toBe('1x');
});
it('if.aqua bug LNG-69', async () => {
let res = await bugNG69Call(peer2.getStatus().relayPeerId);
expect(res).toBe(true);
});
it(' par.aqua', async () => {
let parCallResult = await parCall();
expect(parCallResult).toBe('hello');
});
it(' par.aqua testTimeout', async () => {
let testTimeoutResult = await testTimeoutCall();
expect(testTimeoutResult).toBe('timeout');
});
it('helloWorld.aqua', async () => {
let helloWorldResult = await helloWorldCall();
expect(helloWorldResult).toBe('Hello, NAME!');
});
it('canon bug LNG-79', async () => {
let result = await bugLng79Call(selfPeerId, config.relays[0].peerId);
expect(result).toBe(2);
});
it('func.aqua', async () => {
let funcCallResult = await funcCall();
expect(funcCallResult).toBe('some str');
});
it('on.aqua', async () => {
let onCallResult = await onCall();
expect(onCallResult).toEqual(config.externalAddressesRelay1);
});
it('dataAlias.aqua', async () => {
let dataAliasResult = await dataAliasCall();
expect(dataAliasResult).toBe('peer id str');
});
it('complex.aqua', async () => {
let complexCallResult = await complexCall();
expect(complexCallResult).toEqual(['some str', '3', '1', '4', '1', '1', '3', '2', '4', '2', '2', selfPeerId]);
});
it('constants.aqua', async () => {
let constantCallResult = await constantsCall();
expect(constantCallResult).toEqual(['1', 'ab']);
});
it('PARTICLE_TTL and PARTICLE_TIMESTAMP', async () => {
const ttl = 1234;
let result = await particleTtlAndTimestampCall(ttl);
expect(result[1]).toBeDefined();
expect(result[0]).toEqual(ttl);
});
it('stream.aqua', async () => {
let streamResult = await streamCall();
expect(streamResult).toEqual(['first updated', 'second updated', 'third updated', 'fourth updated']);
// bug LNG-84
let returnNilResult = await returnNilCall();
expect(returnNilResult).toEqual([]);
let returnNoneResult = await returnNoneCall();
expect(returnNoneResult).toBe(null);
});
it('stream.aqua return stream from inner func', async () => {
let streamResult = await streamReturnFromInnerFunc()
expect(streamResult).toEqual([1, 2, 3, 4]);
})
it('stream.aqua functor', async () => {
let streamResult = await streamFunctorCall()
expect(streamResult).toEqual("123");
})
it('stream.aqua join', async () => {
let streamResult = await streamJoinCall()
expect(streamResult).toEqual("444");
})
it('stream.aqua assignment', async () => {
let streamResult = await streamAssignmentCall()
expect(streamResult).toEqual("333");
})
it('stream.aqua nil literal', async () => {
let result = await nilLiteralCall()
expect(result).toEqual([]);
})
it('stream.aqua nil length', async () => {
let result = await nilLengthCall()
expect(result).toEqual(0);
})
it('stream.aqua int functor', async () => {
let streamResult = await streamIntFunctorCall()
expect(streamResult).toEqual("123");
})
it('streamCan.aqua', async () => {
let streamCanResult = await streamCanCall();
expect(streamCanResult).toEqual(['a', 'b', null]);
});
it('streamCan.aqua LNG-63', async () => {
let result = await bugLNG63Call();
expect(result).toEqual('ok');
});
it('streamCan.aqua LNG-63 2', async () => {
let result = await bugLNG63_2Call();
expect(result).toEqual(['ok', ['ok'], ['ok', 'no', 'ok']]);
});
it('streamCan.aqua LNG-63 3', async () => {
let result = await bugLNG63_3Call();
expect(result).toEqual(['ok', 1, [1, 3, 2]]);
});
it('collectionSugar array', async () => {
let result = await arraySugarCall();
expect(result).toEqual([
[1, 2, 3],
[4, 5, 6],
]);
});
it('object creation getObj', async () => {
let result = await getObjCall()
expect(result).toEqual({
str: "some str",
num: 5,
inner: {
arr: ["a", "b", "c"],
num: 6
}
});
});
it('object creation getObjRelay', async () => {
let result = await getObjRelayCall()
expect(result).toEqual({
str: "some str",
num: 5,
inner: {
arr: ["a", "b", "c"],
num: 6
}
});
});
it('object creation getObjAssign', async () => {
let result = await getObjAssignCall()
expect(result).toEqual({
str: "some str",
num: 5,
inner: {
arr: ["a", "b", "c"],
num: 6
}
});
});
it('collectionSugar stream', async () => {
let result = await streamSugarCall();
expect(result).toEqual([
[1, 2, 3],
[4, 5, 6],
]);
});
it('update bug collectionSugar option', async () => {
let result = await optionSugarCall();
expect(result).toEqual([[1], ['some'], []]);
});
it('collectionSugar bug LNG-59', async () => {
let result = await bugLNG59Call([config.relays[2].peerId, config.relays[3].peerId]);
expect(result).toEqual('some str');
});
// TODO: uncomment
// it('recursiveStreams.aqua', async () => {
// let [sucList, loopList] = await recursiveStreamsCall();
// console.log(sucList);
// console.log(loopList);
// expect(loopList).toEqual(['yes', 'yes', 'yes', 'yes', 'no']);
// expect(sucList.length).toEqual(5);
// });
it('streamCallback.aqua', async () => {
let streamCallResult = await streamCallbackCall();
expect(streamCallResult).toEqual([]);
});
it('topology.aqua', async () => {
let topologyResult = await topologyCall(peer2);
expect(topologyResult).toBe('finish');
});
it('topology.aqua bug 205', async () => {
let topologyResult = await topologyBug205Call(peer2);
const peerId2 = peer2.getStatus().relayPeerId;
const res: string[] = [peerId2];
expect(topologyResult).toEqual(res);
});
it('topology.aqua bug 427', async () => {
let topologyResult = await topologyBug427Call(peer2);
expect(topologyResult).toEqual(['some string', 'some string']);
});
it('topology.aqua bug 394', async () => {
let topologyResult = await topologyBug394Call(peer2);
expect(topologyResult).toEqual(selfPeerId);
});
it('math.aqua test 1', async () => {
let res = await mathTest1Call();
expect(res).toEqual(-10);
});
it('math.aqua test 2', async () => {
let res = await mathTest2Call();
expect(res).toEqual(3);
});
it('foldJoin.aqua', async () => {
let foldJoinResult = await foldJoinCall();
expect(foldJoinResult.length).toBeGreaterThanOrEqual(3);
}, 16000);
it('funcs.aqua', async () => {
let result = await funcsCall();
expect(result).toEqual([13, 6, 3, 1]);
}, 7000);
it('option.aqua', async () => {
registerHandlers();
let optionResult = await useOptionalCall();
let optionalResult = await returnOptionalCall();
let noneResult = await returnNull();
expect(optionResult).toBe('hello');
expect(optionalResult).toBe('optional');
expect(noneResult).toBe(null);
});
it('via.aqua', async () => {
let res1 = await viaArrCall();
let res2 = await viaOptCall();
let res3 = await viaOptNullCall();
let res4 = await viaStreamCall();
expect(res1).toEqual(res2);
expect(res2).toEqual(res3);
expect(res3).toEqual(res4);
}, 16000);
it('nestedFuncs.aqua', async () => {
let nestedFuncsResult = await nestedFuncsCall();
expect(nestedFuncsResult).toBe('some-str');
});
it('nestedData.aqua', async () => {
let nestedDataResult = await nestedDataCall();
expect(nestedDataResult).toEqual({
one: {
val: 'hellohello',
},
});
});
it('closures.aqua', async () => {
let closuresResult = await closuresCall();
let res1 = config.externalAddressesRelay2;
let res2 = ['in', config.externalAddressesRelay2[0]];
expect(closuresResult).toEqual(['in', res1, res1, res2]);
});
// it('closures.aqua LNG-58 bug', async () => {
// let res = await lng58Bug()
// expect(res).toEqual("ok")
// });
it('assignment.aqua', async () => {
let assignmentResult = await assignmentCall();
expect(assignmentResult).toEqual(['abc', 'hello']);
});
it('tryOtherwise.aqua', async () => {
let tryOtherwiseResult = await tryOtherwiseCall();
expect(tryOtherwiseResult).toBe('error');
});
it('tryCatch.aqua', async () => {
let tryCatchResult = await tryCatchCall();
expect(tryCatchResult).toHaveLength(2);
expect(tryCatchResult[0]).toMatch(config.tryCatchError);
expect(tryCatchResult[1]).toBe(config.externalAddressesRelay1[0]);
});
it('coCall.aqua', async () => {
let coCallResult = await coCall();
expect(coCallResult).toEqual(config.externalAddressesRelay1);
});
it('passArgsCall.aqua', async () => {
let passArgsResult = await passArgsCall();
expect(passArgsResult).toBe('client-utilsid');
});
it('passArgsCall.aqua bugLNG60', async () => {
let result = await bugLNG60Call();
expect(result).toBe(true);
});
it('streamArgs.aqua', async () => {
let streamArgsResult = await streamArgsCall();
expect(streamArgsResult).toEqual([['peer_id', 'peer_id']]);
});
it('streamResults.aqua', async () => {
let streamResultsResult = await streamResultsCall();
expect(streamResultsResult).toEqual(['new_name', 'new_name', 'new_name']);
});
it('pushToStream.aqua', async () => {
let pushToStreamResult = await pushToStreamCall();
expect(pushToStreamResult).toEqual(['hello', 'get_string']);
});
it('literalCall.aqua', async () => {
let literalCallResult = await literalCall();
expect(literalCallResult).toBe('some literal');
});
it('join.aqua local', async () => {
let joinLocalCallResult = await joinIdxLocalCall();
expect(joinLocalCallResult.length).toBeGreaterThanOrEqual(2);
});
it('join.aqua relay', async () => {
let joinRelayCallResult = await joinIdxRelayCall();
expect(joinRelayCallResult.length).toBeGreaterThanOrEqual(2);
});
it('join.aqua network', async () => {
let joinCallResult = await joinIdxCall();
expect(joinCallResult.length).toBeGreaterThanOrEqual(2);
}, 16000);
it('multiReturn.aqua', async () => {
let multiReturnResult = await multiReturnCall();
expect(multiReturnResult).toEqual([['some-str', 'random-str', 'some-str'], 5, 'some-str', [1, 2], null, 10]);
});
it('declare.aqua', async () => {
let declareResult = await declareCall();
expect(declareResult).toBe('small_foodeclare all barsmall_fooexport_constdeclare_constdeclare_const2');
});
it('option_gen.aqua', async () => {
let optionGenResult = await genOptions();
expect(optionGenResult).toEqual(['none', 'some']);
});
it('option_gen.aqua emptyString', async () => {
let optionGenResult = await genOptionsEmptyString();
expect(optionGenResult).toEqual(null);
});
});