From 3aee75c371c4c3bae727899317b51982a0bb60ae Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Sat, 30 Sep 2023 12:15:01 -0600 Subject: [PATCH] finish recursive test example --- examples/recursion/dfx.json | 3 +- examples/recursion/src/recursion/index.did | 4 +- examples/recursion/src/recursion/index.ts | 8 +- .../src/recursive_canister/index.did | 12 +-- examples/recursion/test/pretest.ts | 22 +++++- examples/recursion/test/tests.ts | 74 +++++++++++++++++-- 6 files changed, 102 insertions(+), 21 deletions(-) diff --git a/examples/recursion/dfx.json b/examples/recursion/dfx.json index 62f13159eb..05f3c8b1e0 100644 --- a/examples/recursion/dfx.json +++ b/examples/recursion/dfx.json @@ -10,7 +10,8 @@ "declarations": { "output": "test/dfx_generated/recursion", "node_compatibility": true - } + }, + "env": ["MY_CANISTER_PRINCIPAL"] }, "recursive_canister": { "type": "custom", diff --git a/examples/recursion/src/recursion/index.did b/examples/recursion/src/recursion/index.did index cb764937a7..adf313f9ee 100644 --- a/examples/recursion/src/recursion/index.did +++ b/examples/recursion/src/recursion/index.did @@ -1,5 +1,3 @@ -type rec_397 = service {myQuery: (rec_397) -> (rec_397) query;}; -type rec_400 = service {myQuery: (rec_400) -> (rec_400) query;}; type rec_365 = func (rec_365) -> (rec_365) query; type rec_368 = func (rec_368) -> (rec_368) query; type rec_373 = func (rec_373) -> (rec_373) query; @@ -14,6 +12,8 @@ type rec_292 = record {myVecRecords:vec rec_292}; type rec_333 = record {myVecRecords:vec rec_333}; type rec_385 = service {myQuery: (rec_385) -> (rec_385) query;}; type rec_388 = service {myQuery: (rec_388) -> (rec_388) query;}; +type rec_397 = service {myQuery: (rec_397) -> (rec_397) query;}; +type rec_400 = service {myQuery: (rec_400) -> (rec_400) query;}; type rec_393 = service {myQuery: (rec_393) -> (rec_393) query;}; type rec_377 = service {myQuery: (rec_377) -> (rec_377) query;}; type rec_380 = service {myQuery: (rec_380) -> (rec_380) query;}; diff --git a/examples/recursion/src/recursion/index.ts b/examples/recursion/src/recursion/index.ts index 221f39079d..4959221a93 100644 --- a/examples/recursion/src/recursion/index.ts +++ b/examples/recursion/src/recursion/index.ts @@ -117,9 +117,11 @@ export default Canister({ testRecService: query([MyFullCanister], MyFullCanister, (param) => param), testRecServiceReturn: query([], MyFullCanister, () => { return MyFullCanister( - // Principal.fromText(process.env.MY_CANISTER_PRINCIPAL) ?? - Principal.fromText('asrmz-lmaaa-aaaaa-qaaeq-cai') ?? - ic.trap('process.env.MY_CANISTER_PRINCIPAL is undefined') + Principal.fromText( + process.env.MY_CANISTER_PRINCIPAL ?? + // Principal.fromText('asrmz-lmaaa-aaaaa-qaaeq-cai') ?? + ic.trap('process.env.MY_CANISTER_PRINCIPAL is undefined') + ) ); }), testRecServiceCall: update( diff --git a/examples/recursion/src/recursive_canister/index.did b/examples/recursion/src/recursive_canister/index.did index b2f5f7c53e..fdb2aad9a1 100644 --- a/examples/recursion/src/recursive_canister/index.did +++ b/examples/recursion/src/recursive_canister/index.did @@ -1,9 +1,5 @@ -type rec_20 = service { - myQuery: (rec_20) -> (rec_20) query; -}; -type rec_23 = service { - myQuery: (rec_23) -> (rec_23) query; -}; +type rec_20 = service {myQuery: (rec_20) -> (rec_20) query;}; +type rec_23 = service {myQuery: (rec_23) -> (rec_23) query;}; service: () -> { - myQuery: (rec_20) -> (rec_23) query; -} \ No newline at end of file + myQuery: (rec_20) -> (rec_23) query; +} diff --git a/examples/recursion/test/pretest.ts b/examples/recursion/test/pretest.ts index fd6c38f77a..5c1d628c8c 100644 --- a/examples/recursion/test/pretest.ts +++ b/examples/recursion/test/pretest.ts @@ -1,16 +1,34 @@ import { execSync } from 'child_process'; +import { getCanisterId } from 'azle/test'; async function pretest() { await new Promise((resolve) => setTimeout(resolve, 5000)); - execSync(`dfx canister uninstall-code recursion || true`, { + execSync(`dfx canister uninstall-code recursive_canister || true`, { stdio: 'inherit' }); - execSync(`dfx deploy recursion`, { + execSync(`dfx deploy recursive_canister`, { stdio: 'inherit' }); + execSync(`dfx generate recursive_canister`, { + stdio: 'inherit' + }); + + execSync(`dfx canister uninstall-code recursion || true`, { + stdio: 'inherit' + }); + + execSync( + `MY_CANISTER_PRINCIPAL=${getCanisterId( + 'recursive_canister' + )} dfx deploy recursion`, + { + stdio: 'inherit' + } + ); + execSync(`dfx generate recursion`, { stdio: 'inherit' }); diff --git a/examples/recursion/test/tests.ts b/examples/recursion/test/tests.ts index f6bc1e3fff..73eafbf247 100644 --- a/examples/recursion/test/tests.ts +++ b/examples/recursion/test/tests.ts @@ -1,12 +1,15 @@ -import { Test } from 'azle/test'; +import { Test, getCanisterId } from 'azle/test'; import { _SERVICE, - rec_10, - rec_8 + rec_313, + rec_321 } from './dfx_generated/recursion/recursion.did'; import { ActorSubclass } from '@dfinity/agent'; import { Principal } from '@dfinity/principal'; +import { execSync } from 'child_process'; +// TODO these tests should be rewritten to use @dfinity/agent once this issue is resolved: https://github.com/dfinity/agent-js/issues/702 +// TODO this issue also needs to be resolved: https://forum.dfinity.org/t/services-wont-deserialize-properly-if-functions-arent-in-alphabetical-order/20885 export function getTests(recursion_canister: ActorSubclass<_SERVICE>): Test[] { return [ { @@ -179,7 +182,7 @@ export function getTests(recursion_canister: ActorSubclass<_SERVICE>): Test[] { { name: 'recursive tuples with vec', test: async () => { - const input: rec_10 = [[[[], [[[], []]]]], []]; + const input: rec_321 = [[[[], [[[], []]]]], []]; const result = await recursion_canister.testRecTupleWithVec(input); @@ -211,7 +214,7 @@ export function getTests(recursion_canister: ActorSubclass<_SERVICE>): Test[] { { name: 'recursive tuples with opt', test: async () => { - const input: rec_8 = [[[[], [[[], []]]]], []]; + const input: rec_313 = [[[[], [[[], []]]]], []]; const result = await recursion_canister.testRecTupleWithOpt(input); @@ -309,6 +312,67 @@ export function getTests(recursion_canister: ActorSubclass<_SERVICE>): Test[] { result[1].varTuple[1].varTuple[1].num === 10 }; } + }, + { + name: 'test rec service simple', + test: async () => { + const principalId = getCanisterId('recursive_canister'); + const result = execSync( + `dfx canister call recursion testRecServiceSimple '(service "${principalId}")'` + ) + .toString() + .trim(); + + return { + Ok: result === `(service "${principalId}")` + }; + } + }, + { + name: 'test rec service', + test: async () => { + const principalId = getCanisterId('recursive_canister'); + const result = execSync( + `dfx canister call recursion testRecService '(service "${principalId}")'` + ) + .toString() + .trim(); + + return { + Ok: result === `(service "${principalId}")` + }; + } + }, + { + name: 'test rec service return', + test: async () => { + const principalId = getCanisterId('recursive_canister'); + const result = execSync( + `dfx canister call recursion testRecServiceReturn` + ) + .toString() + .trim(); + + return { + Ok: result === `(service "${principalId}")` + }; + } + }, + { + name: 'test rec service call', + skip: true, // TODO waiting for azle.encode and azle.decode to be implemented + test: async () => { + const principalId = getCanisterId('recursive_canister'); + const result = execSync( + `dfx canister call recursion testRecServiceCall '(service "${principalId}")'` + ) + .toString() + .trim(); + + return { + Ok: result === `(service "${principalId}")` + }; + } } ]; }