aqua-playground/src/__test__/examples.spec.ts

374 lines
12 KiB
TypeScript
Raw Normal View History

2022-01-26 20:32:57 +03:00
import {Fluence, FluencePeer, setLogLevel} from '@fluencelabs/fluence';
2021-10-07 13:55:10 +03:00
import { registerPrintln } from '../compiled/examples/println';
2022-02-09 12:53:30 +03:00
import {callArrowCall, reproArgsBug426Call} from '../examples/callArrowCall';
2021-10-07 13:55:10 +03:00
import { dataAliasCall } from '../examples/dataAliasCall';
import { onCall } from '../examples/onCall';
import { funcCall } from '../examples/funcCall';
import { helloWorldCall } from '../examples/helloWorldCall';
import { foldCall } from '../examples/foldCall';
2022-02-03 12:44:11 +03:00
import {ifCall, ifWrapCall} from '../examples/if';
2021-10-07 13:55:10 +03:00
import { parCall } from '../examples/parCall';
import { complexCall } from '../examples/complex';
import { constantsCall } from '../examples/constantsCall';
import { returnNilCall, returnNoneCall, streamCall } from '../examples/streamCall';
2022-02-09 11:22:22 +03:00
import {topologyBug205Call, topologyBug394Call, topologyBug427Call, topologyCall} from '../examples/topologyCall';
2021-10-07 13:55:10 +03:00
import { foldJoinCall } from '../examples/foldJoinCall';
import { registerHandlers, returnNull, returnOptionalCall, useOptionalCall } from '../examples/useOptionalCall';
import {viaArrCall, viaOptCall, viaOptNullCall, viaStreamCall} from '../examples/viaCall';
2021-10-07 13:55:10 +03:00
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 { 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 } from '../examples/optionsCall';
import { config } from '../config';
2021-10-25 18:40:02 +03:00
import {closuresCall} from "../examples/closures";
2021-10-26 19:10:39 +03:00
import {streamCanCall} from "../examples/streamCan";
2021-11-01 12:20:08 +03:00
import {streamCallbackCall} from "../examples/streamCallback";
2021-11-15 16:15:29 +03:00
import {streamResCall} from "../examples/streamRestrictionsCall";
2022-01-25 14:34:35 +03:00
import {joinIdxCall, joinIdxLocalCall, joinIdxRelayCall} from "../examples/joinCall";
2022-02-09 11:22:22 +03:00
import {topologyBug427} from "../compiled/examples/topology";
import {recursiveStreamsCall} from "../examples/recursiveStreamsCall";
2022-03-04 12:20:55 +03:00
import {allEmptySugarCall, arraySugarCall, optionSugarCall, streamSugarCall} from "../examples/collectionSugarCall";
2022-03-25 14:44:48 +03:00
import {funcsCall} from "../examples/funcsCall";
2022-04-03 20:43:04 +03:00
import {nestedDataCall} from "../examples/nestedDataCall";
2022-04-08 13:58:22 +03:00
import {mathTest1Call, mathTest2Call} from "../examples/mathCall";
2021-10-07 13:55:10 +03:00
2021-10-25 18:40:02 +03:00
var selfPeerId: string;
var peer2: FluencePeer;
2021-10-07 13:55:10 +03:00
const relays = config.relays
2022-01-26 20:32:57 +03:00
// setLogLevel('debug');
2021-10-07 13:55:10 +03:00
describe('Testing examples', () => {
beforeAll(async () => {
2022-01-26 13:17:02 +03:00
await Fluence.start({ connectTo: relays[0] });
2021-10-07 13:55:10 +03:00
selfPeerId = Fluence.getStatus().peerId;
peer2 = new FluencePeer();
2022-01-26 13:17:02 +03:00
await peer2.start({ connectTo: relays[1] });
2021-10-07 13:55:10 +03:00
// this could be called from `println.aqua`
registerPrintln({
print: (arg0) => {
console.log('println: ' + arg0);
},
});
});
afterAll(async () => {
if (peer2) {
Fluence.stop();
await peer2.stop();
}
});
it('callArrow.aqua', async () => {
let callArrowResult = await callArrowCall();
expect(callArrowResult).toBe('Hello, callArrow call!');
});
2022-02-09 12:53:30 +03:00
it('callArrow.aqua args bug 426', async () => {
let argResult = await reproArgsBug426Call();
expect(argResult).toBe("privet");
});
2021-11-15 16:15:29 +03:00
it('streamRestrictions.aqua', async () => {
let streamResResult = await streamResCall();
expect(streamResResult).toEqual([[], ["a", "b", "c"]]);
2021-11-15 16:15:29 +03:00
});
2021-10-07 13:55:10 +03:00
it('fold.aqua', async () => {
let foldCallResult = await foldCall();
expect(foldCallResult).toEqual(config.externalAddressesRelay1);
2021-10-07 13:55:10 +03:00
});
it('if.aqua', async () => {
await ifCall();
});
2022-02-03 12:44:11 +03:00
it('if.aqua xor wrap', async () => {
2022-02-03 15:14:24 +03:00
let res = await ifWrapCall(peer2.getStatus().relayPeerId);
expect(res).toBe('1x');
2022-02-03 12:44:11 +03:00
});
2021-10-07 13:55:10 +03:00
it(' par.aqua', async () => {
let parCallResult = await parCall();
expect(parCallResult).toBe('hello');
});
it('helloWorld.aqua', async () => {
let helloWorldResult = await helloWorldCall();
expect(helloWorldResult).toBe('Hello, NAME!');
});
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);
2021-10-07 13:55:10 +03:00
});
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([
2021-10-07 13:55:10 +03:00
'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']);
2021-10-07 13:55:10 +03:00
});
it('stream.aqua', async () => {
let streamResult = await streamCall();
expect(streamResult).toEqual(['first updated', 'second updated', 'third updated', 'fourth updated']);
2021-10-07 13:55:10 +03:00
let returnNilResult = await returnNilCall();
expect(returnNilResult).toEqual([]);
2021-10-07 13:55:10 +03:00
let returnNoneResult = await returnNoneCall();
expect(returnNoneResult).toBe(null);
});
2021-10-26 19:10:39 +03:00
it('streamCan.aqua', async () => {
let streamCanResult = await streamCanCall();
2022-03-17 07:23:05 +03:00
expect(streamCanResult).toEqual(["a", "b", null]);
2021-10-26 19:10:39 +03:00
});
it('collectionSugar array', async () => {
2022-03-02 18:54:58 +03:00
let result = await arraySugarCall();
expect(result).toEqual([[1,2,3], [4,5,6]]);
});
it('collectionSugar stream', async () => {
2022-03-02 18:54:58 +03:00
let result = await streamSugarCall();
expect(result).toEqual([[1,2,3], [4,5,6]]);
});
it('collectionSugar option', async () => {
2022-03-02 18:54:58 +03:00
let result = await optionSugarCall()
expect(result).toEqual([[1], ["some"], []]);
});
2022-03-04 12:20:55 +03:00
it('collectionSugar empty', async () => {
let result = await allEmptySugarCall()
expect(result).toEqual([[], [], [], [], null, [], null]);
});
2022-03-10 12:03:16 +03:00
it('recursiveStreams.aqua', async () => {
2022-03-10 12:44:09 +03:00
let [sucList, loopList] = await recursiveStreamsCall();
console.log(sucList)
console.log(loopList)
expect(loopList).toEqual(["yes","yes","yes","yes","no"]);
expect(sucList.length).toEqual(5);
});
2021-11-01 12:20:08 +03:00
it('streamCallback.aqua', async () => {
let streamCallResult = await streamCallbackCall();
expect(streamCallResult).toEqual([]);
2021-11-01 12:20:08 +03:00
});
2021-10-07 13:55:10 +03:00
it('topology.aqua', async () => {
let topologyResult = await topologyCall(peer2);
expect(topologyResult).toBe('finish');
});
2021-10-25 18:40:02 +03:00
it('topology.aqua bug 205', async () => {
let topologyResult = await topologyBug205Call(peer2);
const peerId2 = peer2.getStatus().relayPeerId
const res: string[] = [peerId2]
expect(topologyResult).toEqual(res);
2021-10-25 18:40:02 +03:00
});
2022-03-10 12:03:16 +03:00
it('topology.aqua bug 427', async () => {
2022-02-09 11:22:22 +03:00
let topologyResult = await topologyBug427Call(peer2);
expect(topologyResult).toEqual(["some string", "some string"]);
});
2021-12-29 12:27:06 +03:00
it('topology.aqua bug 394', async () => {
let topologyResult = await topologyBug394Call(peer2);
expect(topologyResult).toEqual(selfPeerId);
2021-12-29 12:27:06 +03:00
});
2022-04-08 13:58:22 +03:00
it('math.aqua test 1', async () => {
let res = await mathTest1Call();
expect(res).toEqual(-10);
});
it('math.aqua test 2', async () => {
let res = await mathTest2Call();
2022-04-11 10:51:59 +03:00
expect(res).toEqual(3);
2022-04-08 13:58:22 +03:00
});
2021-10-07 13:55:10 +03:00
it('foldJoin.aqua', async () => {
let foldJoinResult = await foldJoinCall();
2021-11-09 17:46:23 +03:00
expect(foldJoinResult.length).toBeGreaterThanOrEqual(3)
2022-01-28 11:54:57 +03:00
}, 16000);
2021-10-07 13:55:10 +03:00
2022-03-25 14:44:48 +03:00
it('funcs.aqua', async () => {
let result = await funcsCall();
2022-04-11 10:51:59 +03:00
expect(result).toEqual([13, 6, 3]);
2022-03-25 14:44:48 +03:00
}, 7000);
2021-10-07 13:55:10 +03:00
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);
2022-01-28 11:54:57 +03:00
}, 16000);
2021-10-07 13:55:10 +03:00
it('nestedFuncs.aqua', async () => {
let nestedFuncsResult = await nestedFuncsCall();
expect(nestedFuncsResult).toBe('some-str');
});
2022-04-04 10:39:49 +03:00
it('nestedData.aqua', async () => {
2022-04-03 20:43:04 +03:00
let nestedDataResult = await nestedDataCall();
2022-04-04 10:39:49 +03:00
expect(nestedDataResult).toEqual({
2022-04-03 20:43:04 +03:00
one: {
val: "hellohello"
}
});
});
2021-11-04 13:13:26 +03:00
it('closures.aqua', async () => {
let closuresResult = await closuresCall();
let res1 = config.externalAddressesRelay2
let res2 = ["in", config.externalAddressesRelay2[0]]
2022-03-14 12:14:06 +03:00
expect(closuresResult).toEqual(["in", res1, res1, res2]);
2021-11-04 13:13:26 +03:00
});
2021-10-07 13:55:10 +03:00
it('assignment.aqua', async () => {
let assignmentResult = await assignmentCall();
expect(assignmentResult).toEqual(['abc', 'hello']);
2021-10-07 13:55:10 +03:00
});
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]);
2021-10-07 13:55:10 +03:00
});
it('coCall.aqua', async () => {
let coCallResult = await coCall();
expect(coCallResult).toEqual(config.externalAddressesRelay1);
2021-10-07 13:55:10 +03:00
});
it('passArgsCall.aqua', async () => {
let passArgsResult = await passArgsCall();
expect(passArgsResult).toBe('client-utilsid');
});
it('streamArgs.aqua', async () => {
let streamArgsResult = await streamArgsCall();
expect(streamArgsResult).toEqual([['peer_id', 'peer_id']]);
2021-10-07 13:55:10 +03:00
});
it('streamResults.aqua', async () => {
let streamResultsResult = await streamResultsCall();
expect(streamResultsResult).toEqual(['new_name', 'new_name', 'new_name']);
2021-10-07 13:55:10 +03:00
});
it('pushToStream.aqua', async () => {
let pushToStreamResult = await pushToStreamCall();
expect(pushToStreamResult).toEqual(['hello', 'get_string']);
2021-10-07 13:55:10 +03:00
});
it('literalCall.aqua', async () => {
let literalCallResult = await literalCall();
expect(literalCallResult).toBe('some literal');
});
2022-01-25 14:34:35 +03:00
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);
});
2022-03-10 12:03:16 +03:00
it('join.aqua network', async () => {
2022-01-25 14:34:35 +03:00
let joinCallResult = await joinIdxCall();
expect(joinCallResult.length).toBeGreaterThanOrEqual(2);
2022-01-27 17:01:18 +03:00
}, 16000);
2022-01-25 14:34:35 +03:00
2021-10-07 13:55:10 +03:00
it('multiReturn.aqua', async () => {
let multiReturnResult = await multiReturnCall();
expect(multiReturnResult).toEqual([
2021-10-07 13:55:10 +03:00
['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']);
2021-10-07 13:55:10 +03:00
});
});