From 6f5cd4d3c102fc9f20179ca33f71b9c6859ae229 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Sat, 30 Sep 2023 13:08:33 -0600 Subject: [PATCH] add init test for recursive canisters --- examples/recursion/src/recursion/index.did | 12 ++++++------ .../recursion/src/recursive_canister/index.did | 9 +++++---- .../recursion/src/recursive_canister/index.ts | 10 ++++++++-- examples/recursion/test/pretest.ts | 2 +- examples/recursion/test/test.ts | 17 +++++++++++++++-- examples/recursion/test/tests.ts | 17 +++++++++++++++++ 6 files changed, 52 insertions(+), 15 deletions(-) diff --git a/examples/recursion/src/recursion/index.did b/examples/recursion/src/recursion/index.did index adf313f9ee..0b3b6b0e80 100644 --- a/examples/recursion/src/recursion/index.did +++ b/examples/recursion/src/recursion/index.did @@ -10,11 +10,11 @@ type rec_337 = record {myVar:variant {num:int8; varRec:rec_337}}; type rec_289 = record {myVecRecords:vec rec_289}; 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_385 = service {getMessage: () -> (text) query; myQuery: (rec_385) -> (rec_385) query;}; +type rec_388 = service {getMessage: () -> (text) query; myQuery: (rec_388) -> (rec_388) query;}; +type rec_397 = service {getMessage: () -> (text) query; myQuery: (rec_397) -> (rec_397) query;}; +type rec_400 = service {getMessage: () -> (text) query; myQuery: (rec_400) -> (rec_400) query;}; +type rec_393 = service {getMessage: () -> (text) query; 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;}; type rec_313 = record {opt rec_313; opt rec_313}; @@ -39,7 +39,7 @@ service: () -> { testRecRecordWithVec: (rec_289) -> (rec_292) query; testRecRecordWithVecReturn: () -> (rec_333) query; testRecService: (rec_385) -> (rec_388) query; - testRecServiceCall: (rec_397) -> (rec_400) ; + testRecServiceCall: (rec_397) -> (rec_400); testRecServiceReturn: () -> (rec_393) query; testRecServiceSimple: (rec_377) -> (rec_380) query; testRecTupleWithOpt: (rec_313) -> (rec_316) query; diff --git a/examples/recursion/src/recursive_canister/index.did b/examples/recursion/src/recursive_canister/index.did index fdb2aad9a1..c13cc9540d 100644 --- a/examples/recursion/src/recursive_canister/index.did +++ b/examples/recursion/src/recursive_canister/index.did @@ -1,5 +1,6 @@ -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; +type rec_18 = service {getMessage: () -> (text) query; myQuery: (rec_18) -> (rec_18) query;}; +type rec_21 = service {getMessage: () -> (text) query; myQuery: (rec_21) -> (rec_21) query;}; +service: (text) -> { + getMessage: () -> (text) query; + myQuery: (rec_18) -> (rec_21) query; } diff --git a/examples/recursion/src/recursive_canister/index.ts b/examples/recursion/src/recursive_canister/index.ts index 7ca4594c33..dc76437737 100644 --- a/examples/recursion/src/recursive_canister/index.ts +++ b/examples/recursion/src/recursive_canister/index.ts @@ -1,8 +1,14 @@ -import { query, Canister, Recursive } from 'azle'; +import { query, Canister, Recursive, text, init } from 'azle'; + +let myMessage = ''; const MyCanister = Recursive(() => Canister({ - myQuery: query([MyCanister], MyCanister, (param) => param) + init: init([text], (message) => { + myMessage = message; + }), + myQuery: query([MyCanister], MyCanister, (param) => param), + getMessage: query([], text, () => myMessage) }) ); diff --git a/examples/recursion/test/pretest.ts b/examples/recursion/test/pretest.ts index 5c1d628c8c..2f2023274f 100644 --- a/examples/recursion/test/pretest.ts +++ b/examples/recursion/test/pretest.ts @@ -8,7 +8,7 @@ async function pretest() { stdio: 'inherit' }); - execSync(`dfx deploy recursive_canister`, { + execSync(`dfx deploy recursive_canister --argument '("hello")'`, { stdio: 'inherit' }); diff --git a/examples/recursion/test/test.ts b/examples/recursion/test/test.ts index e3a18cf29f..34e97ee022 100644 --- a/examples/recursion/test/test.ts +++ b/examples/recursion/test/test.ts @@ -1,6 +1,7 @@ import { getCanisterId, runTests } from 'azle/test'; import { createActor } from './dfx_generated/recursion'; -import { getTests } from './tests'; +import { createActor as createRecursiveActor } from './dfx_generated/recursive_canister'; +import { getRecursiveCanisterTests, getTests } from './tests'; const recursionCanister = createActor(getCanisterId('recursion'), { agentOptions: { @@ -8,4 +9,16 @@ const recursionCanister = createActor(getCanisterId('recursion'), { } }); -runTests(getTests(recursionCanister)); +const recursiveCanister = createRecursiveActor( + getCanisterId('recursive_canister'), + { + agentOptions: { + host: 'http://127.0.0.1:8000' + } + } +); + +runTests([ + ...getTests(recursionCanister), + ...getRecursiveCanisterTests(recursiveCanister) +]); diff --git a/examples/recursion/test/tests.ts b/examples/recursion/test/tests.ts index 73eafbf247..f901177a10 100644 --- a/examples/recursion/test/tests.ts +++ b/examples/recursion/test/tests.ts @@ -4,12 +4,29 @@ import { rec_313, rec_321 } from './dfx_generated/recursion/recursion.did'; +import { _SERVICE as _REC_SERVICE } from './dfx_generated/recursive_canister/recursive_canister.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 getRecursiveCanisterTests( + recursive_canister: ActorSubclass<_REC_SERVICE> +): Test[] { + return [ + { + name: 'test recursive canister init method', + test: async () => { + const result = await recursive_canister.getMessage(); + + return { + Ok: result === 'hello' + }; + } + } + ]; +} export function getTests(recursion_canister: ActorSubclass<_SERVICE>): Test[] { return [ {