Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
krpeacock committed Oct 30, 2023
1 parent 95e9d02 commit 92f5e58
Show file tree
Hide file tree
Showing 23 changed files with 503 additions and 259 deletions.
3 changes: 0 additions & 3 deletions canister_ids.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"counter": {
"ic": "teog5-mqaaa-aaaab-qaija-cai"
},
"docs": {
"ic": "erxue-5aaaa-aaaab-qaagq-cai"
}
Expand Down
4 changes: 0 additions & 4 deletions dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
"docs": {
"type": "assets",
"source": ["docs/generated"]
},
"counter": {
"type": "motoko",
"main": "e2e/node/canisters/counter.mo"
}
}
}
1 change: 1 addition & 0 deletions docs/generated/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ <h1>Agent-JS Changelog</h1>
<section>
<h2>Version x.x.x</h2>
<ul>
<li>feat: adds subnet metrics decoding to canisterStatus for `/subnet` path</li>
<li>
chore: replaces use of localhost with 127.0.0.1 for better node 18 support. Also swaps
Jest for vitest, runs mitm against mainnet, and updates some packages
Expand Down
1 change: 1 addition & 0 deletions e2e/node/basic/canisterStatus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('canister status', () => {
const counterObj = await (await counter)();
const agent = new HttpAgent({
host: `http://127.0.0.1:${process.env.REPLICA_PORT ?? 4943}`,
verifyQuerySignatures: false,
});
const shouldThrow = async () => {
// eslint-disable-next-line no-useless-catch
Expand Down
11 changes: 6 additions & 5 deletions e2e/node/basic/counter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('counter', () => {
} catch (error) {
console.error(error);
}
});
}, 40000);
it('should submit distinct requests with nonce by default', async () => {
const { actor: counter } = await counterCanister();
const values = await Promise.all(new Array(4).fill(undefined).map(() => counter.inc_read()));
Expand All @@ -20,7 +20,7 @@ describe('counter', () => {
// Sets of unique results should be the same length
expect(set1.size).toBe(values.length);
expect(set2.size).toEqual(values2.length);
});
}, 40000);
it('should submit duplicate requests if nonce is disabled', async () => {
const { actor: counter } = await noncelessCanister();
const values = await Promise.all(new Array(4).fill(undefined).map(() => counter.inc_read()));
Expand All @@ -29,15 +29,16 @@ describe('counter', () => {
const set2 = new Set(values2);

expect(set1.size < values.length || set2.size < values2.length).toBe(true);
});
}, 40000);
it('should increment', async () => {
const { actor: counter } = await noncelessCanister();

expect(Number(await counter.read())).toEqual(0);
await counter.inc();
expect(Number(await counter.read())).toEqual(1);
await counter.inc();
expect(Number(await counter.read())).toEqual(2);
});
}, 40000);
});
describe('retrytimes', () => {
it('should retry after a failure', async () => {
Expand All @@ -63,5 +64,5 @@ describe('retrytimes', () => {
} catch (error) {
console.error(error);
}
});
}, 40000);
});
10 changes: 0 additions & 10 deletions e2e/node/basic/mainnet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Ed25519KeyIdentity } from '@dfinity/identity';
import { Principal } from '@dfinity/principal';
import { describe, it, expect, vi } from 'vitest';
import { makeAgent } from '../utils/agent';
import { createActor, canisterId } from '../canisters/declarations/counter';

const createWhoamiActor = async (identity: Identity) => {
const canisterId = 'ivcos-eqaaa-aaaab-qablq-cai';
Expand Down Expand Up @@ -114,12 +113,3 @@ describe('certified query', () => {
`);
});
});

describe('call', async () => {
it('should make update calls against mainnet', async () => {
const counter = createActor(canisterId, { agentOptions: { host: 'https://ic0.app' } });
await counter.reset();
await counter.inc();
expect(await counter.read()).toEqual(1n);
}, 100_000);
});
59 changes: 27 additions & 32 deletions e2e/node/canisters/counter.mo
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
actor Counter {
var cell : Nat = 0;

public func greet(name : Text) : async Text {
return "Hello, " # name # "!";
};

public query func queryGreet(name : Text) : async Text {
return "Hello, " # name # "!";
};

public func inc() : async () {
cell += 1;
};

public query func read() : async Nat {
cell;
};

public func inc_read() : async Nat {
cell += 1;
cell;
};

public func write(n : Nat) : async () {
cell := n;
};

public func reset() : async () {
cell := 0;
};

};
var cell : Nat = 0;

public func greet(name : Text) : async Text {
return "Hello, " # name # "!";
};

public query func queryGreet(name : Text) : async Text {
return "Hello, " # name # "!";
};

public func inc() : async () {
cell += 1;
};

public query func read() : async Nat {
cell
};

public func inc_read() : async Nat {
cell += 1;
cell
};

public func write(n: Nat) : async () {
cell := n;
};
}
48 changes: 30 additions & 18 deletions e2e/node/canisters/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,6 @@ let cache: {
actor: any;
} | null = null;

const idl: IDL.InterfaceFactory = ({ IDL }) => {
return IDL.Service({
inc: IDL.Func([], [], []),
inc_read: IDL.Func([], [IDL.Nat], []),
read: IDL.Func([], [IDL.Nat], ['query']),
greet: IDL.Func([IDL.Text], [IDL.Text], []),
reset: IDL.Func([], [], []),
queryGreet: IDL.Func([IDL.Text], [IDL.Text], ['query']),
});
};

/**
* Create a counter Actor + canisterId
*/
Expand All @@ -35,14 +24,23 @@ export default async function (): Promise<{

const canisterId = await Actor.createCanister({ agent: await agent });
await Actor.install({ module }, { canisterId, agent: await agent });
const idl: IDL.InterfaceFactory = ({ IDL }) => {
return IDL.Service({
inc: IDL.Func([], [], []),
inc_read: IDL.Func([], [IDL.Nat], []),
read: IDL.Func([], [IDL.Nat], ['query']),
greet: IDL.Func([IDL.Text], [IDL.Text], []),
queryGreet: IDL.Func([IDL.Text], [IDL.Text], ['query']),
});
};

cache = {
canisterId,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
actor: Actor.createActor(idl, { canisterId, agent: await agent }) as any,
};
}
await cache.actor.reset();

return cache;
}
/**
Expand All @@ -61,13 +59,20 @@ export async function noncelessCanister(): Promise<{

const canisterId = await Actor.createCanister({ agent: disableNonceAgent });
await Actor.install({ module }, { canisterId, agent: disableNonceAgent });
const actor = Actor.createActor(idl, { canisterId, agent: disableNonceAgent }) as any;
const idl: IDL.InterfaceFactory = ({ IDL }) => {
return IDL.Service({
inc: IDL.Func([], [], []),
inc_read: IDL.Func([], [IDL.Nat], []),
read: IDL.Func([], [IDL.Nat], ['query']),
greet: IDL.Func([IDL.Text], [IDL.Text], []),
queryGreet: IDL.Func([IDL.Text], [IDL.Text], ['query']),
});
};

await actor.reset();
return {
canisterId,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
actor: actor,
actor: Actor.createActor(idl, { canisterId, agent: await disableNonceAgent }) as any,
};
}

Expand All @@ -86,7 +91,14 @@ export const createActor = async (options?: HttpAgentOptions) => {

const canisterId = await Actor.createCanister({ agent });
await Actor.install({ module }, { canisterId, agent });
const actor = Actor.createActor(idl, { canisterId, agent }) as any;
await actor.reset();
return actor;
const idl: IDL.InterfaceFactory = ({ IDL }) => {
return IDL.Service({
inc: IDL.Func([], [], []),
inc_read: IDL.Func([], [IDL.Nat], []),
read: IDL.Func([], [IDL.Nat], ['query']),
greet: IDL.Func([IDL.Text], [IDL.Text], []),
queryGreet: IDL.Func([IDL.Text], [IDL.Text], ['query']),
});
};
return Actor.createActor(idl, { canisterId, agent }) as any;
};
Binary file modified e2e/node/canisters/counter.wasm
Binary file not shown.
1 change: 0 additions & 1 deletion e2e/node/canisters/declarations/counter/counter.did
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ service : {
inc_read: () -> (nat);
queryGreet: (text) -> (text) query;
read: () -> (nat) query;
reset: () -> ();
write: (nat) -> ();
}
1 change: 0 additions & 1 deletion e2e/node/canisters/declarations/counter/counter.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ export interface _SERVICE {
inc_read: ActorMethod<[], bigint>;
queryGreet: ActorMethod<[string], string>;
read: ActorMethod<[], bigint>;
reset: ActorMethod<[], undefined>;
write: ActorMethod<[bigint], undefined>;
}
1 change: 0 additions & 1 deletion e2e/node/canisters/declarations/counter/counter.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export const idlFactory = ({ IDL }) => {
inc_read: IDL.Func([], [IDL.Nat], []),
queryGreet: IDL.Func([IDL.Text], [IDL.Text], ['query']),
read: IDL.Func([], [IDL.Nat], ['query']),
reset: IDL.Func([], [], []),
write: IDL.Func([IDL.Nat], [], []),
});
};
Expand Down
9 changes: 3 additions & 6 deletions e2e/node/canisters/declarations/counter/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Actor, ActorConfig, HttpAgent, HttpAgentOptions } from '@dfinity/agent';
import { Actor, HttpAgent } from '@dfinity/agent';

// Imports and re-exports candid interface
import { idlFactory } from './counter.did.js';
Expand All @@ -9,12 +9,9 @@ export { idlFactory } from './counter.did.js';
* process.env.CANISTER_ID_<CANISTER_NAME_UPPERCASE>
* beginning in dfx 0.15.0
*/
export const canisterId = 'teog5-mqaaa-aaaab-qaija-cai';
export const canisterId = process.env.CANISTER_ID_COUNTER || process.env.COUNTER_CANISTER_ID;

export const createActor = (
canisterId,
options: { agentOptions?: HttpAgentOptions; actorOptions?: ActorConfig; agent?: HttpAgent } = {},
) => {
export const createActor = (canisterId, options = {}) => {
const agent = options.agent || new HttpAgent({ ...options.agentOptions });

if (options.agent && options.agentOptions) {
Expand Down
1 change: 0 additions & 1 deletion e2e/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"private": true,
"name": "@do-not-publish/ic-node-e2e-tests",
"version": "0.19.3",
"type": "module",
"scripts": {
"ci": "npm run e2e",
"e2e": "vitest",
Expand Down
2 changes: 1 addition & 1 deletion e2e/node/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"incremental": true,
"target": "ESNext",
"module": "ESNext",
"lib": ["ESNext"],
"lib": ["ES2023"],
"declaration": true,
"sourceMap": true,
"tsBuildInfoFile": "./build_info.json",
Expand Down
2 changes: 1 addition & 1 deletion e2e/node/utils/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export const makeAgent = async (options?: HttpAgentOptions) => {
const agent = new HttpAgent({
host: `http://127.0.0.1:${process.env.REPLICA_PORT ?? 4943}`,
fetch: global.fetch ?? fetch,
verifyQuerySignatures: false,
...options,
});
try {
await agent.fetchRootKey();
await agent.syncTime();
} catch (_) {
//
}
Expand Down
1 change: 0 additions & 1 deletion e2e/node/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ export default defineConfig({
test: {
setupFiles: ['./test-setup.ts'],
testTimeout: 100_000,
threads: false,
},
});
Loading

0 comments on commit 92f5e58

Please sign in to comment.