Skip to content

Commit

Permalink
Merge pull request #1520 from demergent-labs/1517_inspect_message
Browse files Browse the repository at this point in the history
Test: Added InspectMessageMethodArb
  • Loading branch information
lastmjs authored Apr 17, 2024
2 parents e22dabf + 569f341 commit 4621524
Show file tree
Hide file tree
Showing 13 changed files with 1,340 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ jobs:
"property_tests/tests/canister_methods/http_request",
"property_tests/tests/canister_methods/http_request_update",
"property_tests/tests/canister_methods/init",
"property_tests/tests/canister_methods/inspect_message",
"property_tests/tests/canister_methods/post_upgrade",
"property_tests/tests/canister_methods/pre_upgrade",
"property_tests/tests/canister_methods/query",
Expand Down
44 changes: 43 additions & 1 deletion dfx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,23 @@ export async function createAnonymousAgent() {
}
}

/**
* Returns an agent authenticated with the identity with the given name.
*
* Generates a new identity with the given name if shouldGenerateIdentity and
* the identity doesn't already exist.
*
* @param identityName
* @returns
*/
export async function createAuthenticatedAgent(
identityName: string
identityName: string,
shouldGenerateIdentity: boolean = false
): Promise<HttpAgent> {
if (shouldGenerateIdentity && !identityExists(identityName)) {
generateIdentity(identityName);
}

const agent = new HttpAgent({
host: getAgentHost(),
identity: getSecp256k1KeyIdentity(identityName)
Expand All @@ -53,6 +67,34 @@ export async function createAuthenticatedAgent(
return agent;
}

/**
* Returns an agent authenticated with the identity with the given name.
*
* Generates a new identity with the given name if shouldGenerateIdentity and
* the identity doesn't already exist.
*
* IMPORTANT: In order to be synchronous this call will not fetch the root key.
* If you are not on mainnet you will need to fetch the root key separately.
*
* @param identityName
* @returns
*/
export function createAuthenticatedAgentSync(
identityName: string,
shouldGenerateIdentity: boolean = false
): HttpAgent {
if (shouldGenerateIdentity && !identityExists(identityName)) {
generateIdentity(identityName);
}

const agent = new HttpAgent({
host: getAgentHost(),
identity: getSecp256k1KeyIdentity(identityName)
});

return agent;
}

export function whoami(): string {
return execSync(`dfx identity whoami`).toString().trim();
}
Expand Down
8 changes: 7 additions & 1 deletion property_tests/arbitraries/canister_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Test } from '../../test';
import { CliStringVisitor } from '../visitors/cli-string-visitor';
import { CorrespondingJSType } from './candid/corresponding_js_type';
import { InitMethod } from './canister_methods/init_method_arb';
import { InspectMessageMethod } from './canister_methods/inspect_message_method_arb';
import { PostUpgradeMethod } from './canister_methods/post_upgrade_arb';
import { PreUpgradeMethod } from './canister_methods/pre_upgrade_method_arb';
import { QueryMethod } from './canister_methods/query_method_arb';
Expand All @@ -24,14 +25,16 @@ export type CanisterMethod<
| UpdateMethod
| InitMethod<ParamAgentArgumentValue, ParamAgentResponseValue>
| PostUpgradeMethod<ParamAgentArgumentValue, ParamAgentResponseValue>
| PreUpgradeMethod;
| PreUpgradeMethod
| InspectMessageMethod;

export type CanisterConfig<
ParamAgentArgumentValue extends CorrespondingJSType = undefined,
ParamAgentResponseValue = undefined
> = {
globalDeclarations?: string[];
initMethod?: InitMethod<ParamAgentArgumentValue, ParamAgentResponseValue>;
inspectMessageMethod?: InspectMessageMethod;
postUpgradeMethod?: PostUpgradeMethod<
ParamAgentArgumentValue,
ParamAgentResponseValue
Expand All @@ -56,6 +59,9 @@ export function CanisterArb<
ParamAgentResponseValue
>[] = [
...(config.initMethod ? [config.initMethod] : []),
...(config.inspectMessageMethod
? [config.inspectMessageMethod]
: []),
...(config.postUpgradeMethod ? [config.postUpgradeMethod] : []),
...(config.preUpgradeMethod ? [config.preUpgradeMethod] : []),
...(config.queryMethods ?? []),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import fc from 'fast-check';

import { Test } from '../../../test';
import { VoidArb } from '../candid/primitive/void';
import { UniqueIdentifierArb } from '../unique_identifier_arb';
import {
BodyGenerator,
CallbackLocation,
CallbackLocationArb,
generateCallback,
TestsGenerator
} from '.';

export type InspectMessageMethod = {
imports: Set<string>;
globalDeclarations: string[];
sourceCode: string;
tests: Test[][];
};

export function InspectMessageMethodArb(constraints: {
generateBody: BodyGenerator;
generateTests: TestsGenerator;
callbackLocation?: CallbackLocation;
}) {
return fc
.tuple(
UniqueIdentifierArb('canisterProperties'),
VoidArb(),
CallbackLocationArb,
UniqueIdentifierArb('globalNames')
)
.map(
([
functionName,
returnType,
defaultCallbackLocation,
callbackName
]): InspectMessageMethod => {
const callbackLocation =
constraints.callbackLocation ?? defaultCallbackLocation;

const imports = new Set(['inspectMessage', 'ic']);

const callback = generateCallback(
[],
returnType,
constraints.generateBody,
callbackLocation,
callbackName
);

const globalDeclarations =
callbackLocation === 'STANDALONE' ? [callback] : [];

const sourceCode = `${functionName}: inspectMessage(${
callbackLocation === 'STANDALONE' ? callbackName : callback
})`;

const tests = constraints.generateTests(
functionName,
[],
returnType
);

return {
imports,
globalDeclarations,
sourceCode,
tests
};
}
);
}
15 changes: 10 additions & 5 deletions property_tests/get_actor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Agent, HttpAgent } from '@dfinity/agent';

import { getCanisterId } from '../dfx';

export function getActor(parentDir: string) {
export function getActor(parentDir: string, agent?: Agent) {
const resolvedPathIndex = require.resolve(
`${parentDir}/dfx_generated/canister/index.js`
);
Expand All @@ -13,10 +15,13 @@ export function getActor(parentDir: string) {

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { createActor } = require(`${parentDir}/dfx_generated/canister`);

return createActor(getCanisterId('canister'), {
agentOptions: {
host: 'http://127.0.0.1:8000',
verifyQuerySignatures: false // TODO Major issue: https://forum.dfinity.org/t/agent-js-0-20-0-is-released-replica-signed-query-edition/24743/16?u=lastmjs
}
agent:
agent ??
new HttpAgent({
host: 'http://127.0.0.1:8000',
verifyQuerySignatures: false // TODO Major issue: https://forum.dfinity.org/t/agent-js-0-20-0-is-released-replica-signed-query-edition/24743/16?u=lastmjs
})
});
}
16 changes: 16 additions & 0 deletions property_tests/tests/canister_methods/inspect_message/dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"canisters": {
"canister": {
"type": "custom",
"main": "src/index.ts",
"candid": "src/index.did",
"build": "npx azle canister",
"wasm": ".azle/canister/canister.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/canister",
"node_compatibility": true
}
}
}
}
Loading

0 comments on commit 4621524

Please sign in to comment.