Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: v1 agent compatibility with v2 actor tests #902

Merged
merged 6 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- ci: removing headless browser tests pending a rewrite

### Added

- test: adding test for backwards compatibility with actor for v1 agents

## [1.4.0] - 2024-06-17

### Added
Expand Down
20 changes: 11 additions & 9 deletions e2e/node/canisters/counter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Actor, ActorSubclass, HttpAgentOptions } from '@dfinity/agent';
import { Actor, ActorSubclass, HttpAgentOptions, Agent } from '@dfinity/agent';
import { IDL } from '@dfinity/candid';
import { Principal } from '@dfinity/principal';
import { readFileSync } from 'fs';
Expand Down Expand Up @@ -47,20 +47,22 @@ export default async function (): Promise<{
return cache;
}

export const createActor = async (options?: HttpAgentOptions) => {
export const createActor = async (options?: HttpAgentOptions, agent?: Agent) => {
const module = readFileSync(path.join(__dirname, 'counter.wasm'));
const agent = await makeAgent({
...options,
});
const effectiveAgent = agent
? agent
: await makeAgent({
...options,
});
try {
if (!options?.host?.includes('icp-api')) {
await agent.fetchRootKey();
await effectiveAgent.fetchRootKey();
}
} catch (_) {
//
}

const canisterId = await Actor.createCanister({ agent });
await Actor.install({ module }, { canisterId, agent });
return Actor.createActor(idl, { canisterId, agent }) as ActorSubclass<_SERVICE>;
const canisterId = await Actor.createCanister({ agent: effectiveAgent });
await Actor.install({ module }, { canisterId, agent: effectiveAgent });
return Actor.createActor(idl, { canisterId, agent: effectiveAgent }) as ActorSubclass<_SERVICE>;
};
22 changes: 22 additions & 0 deletions e2e/node/integration/actor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { HttpAgent } from '@dfinity/agent';
import { HttpAgent as HttpAgentv1 } from 'agent1';
import { createActor } from '../canisters/counter';
import { vi, test, expect } from 'vitest';

vi.useRealTimers();
test("Legacy Agent interface should be accepted by Actor's createActor", async () => {
// Use the v1.4.0 agent to create an actor
const actor = await createActor(
{},
new HttpAgentv1({
host: `http://127.0.0.1:${process.env.REPLICA_PORT ?? 4943}`,
}) as unknown as HttpAgent,
);

// Verify that update calls work
await actor.write(8n); //?
// Verify that query calls work
const count = await actor.read(); //?
expect(count).toBe(8n);
}, 15_000);
// TODO: tests for rejected, unknown time out
1 change: 1 addition & 0 deletions e2e/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@types/text-encoding": "^0.0.36",
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
"agent1": "npm:@dfinity/agent@^1.4.0",
"esbuild": "^0.15.16",
"eslint": "^8.19.0",
"eslint-plugin-jsdoc": "^39.3.3",
Expand Down
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/agent/src/actor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,5 @@ describe('makeActor', () => {
);
});
});
// TODO: tests for rejected, unknown time out

jest.setTimeout(20000);
5 changes: 5 additions & 0 deletions packages/agent/src/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,10 @@ function _createActorMethod(
effectiveCanisterId: ecid,
});

requestId;
response;
requestDetails;

if (!response.ok || response.body /* IC-1462 */) {
throw new UpdateCallRejectedError(cid, methodName, requestId, response);
}
Expand All @@ -544,6 +548,7 @@ function _createActorMethod(
pollStrategy,
blsVerify,
);
reply;
const shouldIncludeHttpDetails = func.annotations.includes(ACTOR_METHOD_WITH_HTTP_DETAILS);
const shouldIncludeCertificate = func.annotations.includes(ACTOR_METHOD_WITH_CERTIFICATE);

Expand Down
Loading