diff --git a/tests/property/ic_api/reply/test/tests.ts b/tests/property/ic_api/reply/test/tests.ts index 8aa670f4cc..acca2e99cd 100644 --- a/tests/property/ic_api/reply/test/tests.ts +++ b/tests/property/ic_api/reply/test/tests.ts @@ -1,13 +1,8 @@ globalThis._azleExperimental = true; -import { ActorSubclass } from '@dfinity/agent'; -import { - defaultPropTestParams, - expect, - getCanisterActor, - it, - Test -} from 'azle/test'; +import { ActorSubclass, HttpAgent } from '@dfinity/agent'; +import { getCanisterId } from 'azle/dfx'; +import { defaultPropTestParams, expect, it, Test } from 'azle/test'; import { candidDefinitionArb } from 'azle/test/property/arbitraries/candid/candid_definition_arb'; import { CandidValueAndMetaArbGenerator } from 'azle/test/property/arbitraries/candid/candid_value_and_meta_arb_generator'; import { @@ -16,8 +11,8 @@ import { } from 'azle/test/property/arbitraries/candid/candid_values_arb'; import { Context } from 'azle/test/property/arbitraries/types'; import fc from 'fast-check'; -import { mkdir, writeFile } from 'fs/promises'; -import { dirname } from 'path'; +import { cp, mkdir, rm, writeFile } from 'fs/promises'; +import { dirname, join } from 'path'; import { _SERVICE as Actor } from './dfx_generated/canister/canister.did'; import { generateCanister } from './generate_canister'; @@ -25,7 +20,7 @@ import { pretest } from './pretest'; export function getTests(): Test { return () => { - it('should always reply with the input in alwaysReplyQuery', async () => { + it('should always reply with the input in alwaysReplyQuery and alwaysReplyUpdate', async () => { const context: Context = { constraints: {}, api: 'class' @@ -37,7 +32,8 @@ export function getTests(): Test { candidDefinitionArb(context, {}), CandidValueArb ), - async (input) => { + fc.uuid(), + async (input, uuid) => { const { src: { typeAnnotation: tsType, @@ -47,7 +43,8 @@ export function getTests(): Test { }, value: { agentArgumentValue } } = input; - const canister = await setupCanisters( + const canister = await setupCanister( + uuid, idlType, tsType, Array.from(imports), @@ -63,6 +60,8 @@ export function getTests(): Test { agentArgumentValue ); expect(updateResult).toEqual(agentArgumentValue); + + cleanUpCanister(uuid); } ), defaultPropTestParams() @@ -71,7 +70,15 @@ export function getTests(): Test { }; } -async function setupCanisters( +async function cleanUpCanister(uuid: string): Promise { + await rm(`test/dfx_generated/canister${uuid}`, { + recursive: true, + force: true + }); +} + +async function setupCanister( + uuid: string, idlType: string, tsType: string, imports: string[], @@ -90,5 +97,38 @@ async function setupCanisters( pretest(); - return await getCanisterActor('canister'); + await mkdir(`test/dfx_generated/canister${uuid}`, { recursive: true }); + await cp( + 'test/dfx_generated/canister', + `test/dfx_generated/canister${uuid}`, + { recursive: true } + ); + + return await getCanisterActor('canister', uuid); +} + +export async function getCanisterActor( + canisterName: string, + uuid: string = '' +): Promise> { + const importPath = join( + process.cwd(), + 'test', + 'dfx_generated', + `${canisterName}${uuid}` + ); + + const { createActor } = await import(importPath); + + const agent = new HttpAgent({ + host: 'http://127.0.0.1:8000' + }); + + await agent.fetchRootKey(); + + const actor = createActor(getCanisterId(canisterName), { + agent + }); + + return actor; }