Skip to content

Commit

Permalink
make sure tests can be run multiple times in a row
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Nov 15, 2024
1 parent cb6f776 commit f7edea4
Showing 1 changed file with 55 additions and 15 deletions.
70 changes: 55 additions & 15 deletions tests/property/ic_api/reply/test/tests.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -16,16 +11,16 @@ 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';
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<CandidValueConstraints> = {
constraints: {},
api: 'class'
Expand All @@ -37,7 +32,8 @@ export function getTests(): Test {
candidDefinitionArb(context, {}),
CandidValueArb
),
async (input) => {
fc.uuid(),
async (input, uuid) => {
const {
src: {
typeAnnotation: tsType,
Expand All @@ -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),
Expand All @@ -63,6 +60,8 @@ export function getTests(): Test {
agentArgumentValue
);
expect(updateResult).toEqual(agentArgumentValue);

cleanUpCanister(uuid);
}
),
defaultPropTestParams()
Expand All @@ -71,7 +70,15 @@ export function getTests(): Test {
};
}

async function setupCanisters(
async function cleanUpCanister(uuid: string): Promise<void> {
await rm(`test/dfx_generated/canister${uuid}`, {
recursive: true,
force: true
});
}

async function setupCanister(
uuid: string,
idlType: string,
tsType: string,
imports: string[],
Expand All @@ -90,5 +97,38 @@ async function setupCanisters(

pretest();

return await getCanisterActor<Actor>('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<Actor>('canister', uuid);
}

export async function getCanisterActor<T>(
canisterName: string,
uuid: string = ''
): Promise<ActorSubclass<T>> {
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;
}

0 comments on commit f7edea4

Please sign in to comment.