Skip to content

Commit

Permalink
e2e tests using vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
krpeacock committed Oct 23, 2023
1 parent cadc406 commit 95f8ad2
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 56 deletions.
2 changes: 1 addition & 1 deletion demos/sample-javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

1. Ensure all dependencies are installed: `npm install`
2. Run the development server `npm run develop`
3. Visit the running site at http://localhost:8080
3. Visit the running site at http://127.0.0.1:8080
2 changes: 1 addition & 1 deletion e2e/browser/.proxyrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/api": {
"target": "http://localhost:4943/",
"target": "http://127.0.0.1:4943/",
}
}
1 change: 1 addition & 0 deletions e2e/node/basic/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ActorMethod, Certificate, getManagementCanister } from '@dfinity/agent'
import { IDL } from '@dfinity/candid';
import { Principal } from '@dfinity/principal';
import agent from '../utils/agent';
import { test, expect } from 'vitest';

test('read_state', async () => {
const resolvedAgent = await agent;
Expand Down
4 changes: 2 additions & 2 deletions e2e/node/basic/canisterStatus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { CanisterStatus, HttpAgent } from '@dfinity/agent';
import { Principal } from '@dfinity/principal';
import counter from '../canisters/counter';
import { makeAgent } from '../utils/agent';
import { describe, it, afterEach, expect } from 'vitest';

jest.setTimeout(30_000);
afterEach(async () => {
await Promise.resolve();
});
Expand All @@ -23,7 +23,7 @@ describe('canister status', () => {
it('should throw an error if fetchRootKey has not been called', async () => {
const counterObj = await (await counter)();
const agent = new HttpAgent({
host: `http://localhost:${process.env.REPLICA_PORT ?? 4943}`,
host: `http://127.0.0.1:${process.env.REPLICA_PORT ?? 4943}`,
verifyQuerySignatures: false,
});
const shouldThrow = async () => {
Expand Down
12 changes: 7 additions & 5 deletions e2e/node/basic/mainnet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { IDL } from '@dfinity/candid';
import { Ed25519KeyIdentity } from '@dfinity/identity';
import { Principal } from '@dfinity/principal';
import { describe, it, expect, vi } from 'vitest';
import { makeAgent } from '../utils/agent';

const createWhoamiActor = (identity: Identity) => {
const createWhoamiActor = async (identity: Identity) => {
const canisterId = 'ivcos-eqaaa-aaaab-qablq-cai';
const idlFactory = () => {
return IDL.Service({
Expand All @@ -14,7 +15,7 @@ const createWhoamiActor = (identity: Identity) => {
vi.useFakeTimers();
new Date(Date.now());

const agent = new HttpAgent({ host: 'https://icp-api.io', fetch: fetch, identity });
const agent = await makeAgent({ host: 'https://icp-api.io', identity });

return Actor.createActor(idlFactory, {
agent,
Expand All @@ -24,7 +25,7 @@ const createWhoamiActor = (identity: Identity) => {

describe('certified query', () => {
it('should verify a query certificate', async () => {
const actor = createWhoamiActor(new AnonymousIdentity());
const actor = await createWhoamiActor(new AnonymousIdentity());

const result = await actor.whoami();
expect(Principal.from(result)).toBeInstanceOf(Principal);
Expand All @@ -36,8 +37,9 @@ describe('certified query', () => {
count++;
return newIdentity;
});
const actors = identities.map(createWhoamiActor);
const promises = actors.map(actor => actor.whoami());
const actors = await identities.map(async identity => await createWhoamiActor(identity));

const promises = actors.map(async actor => (await actor).whoami());

const results = await Promise.all(promises);
results.forEach(result => {
Expand Down
9 changes: 3 additions & 6 deletions e2e/node/utils/agent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HttpAgent, HttpAgentOptions } from '@dfinity/agent';
import { Ed25519KeyIdentity } from '@dfinity/identity';
import fetch from 'isomorphic-fetch';

export const identity = Ed25519KeyIdentity.generate();
export const principal = identity.getPrincipal();
Expand All @@ -10,13 +11,9 @@ if (Number.isNaN(port)) {
}

export const makeAgent = async (options?: HttpAgentOptions) => {
if (!global.fetch) {
await import('isomorphic-fetch').then(module => {
global.fetch = module.default;
});
}
const agent = new HttpAgent({
host: `http://localhost:${process.env.REPLICA_PORT ?? 4943}`,
host: `http://127.0.0.1:${process.env.REPLICA_PORT ?? 4943}`,
fetch: global.fetch ?? fetch,
verifyQuerySignatures: false,
...options,
});
Expand Down
8 changes: 8 additions & 0 deletions e2e/node/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
setupFiles: ['./test-setup.ts'],
testTimeout: 100_000,
},
});
4 changes: 2 additions & 2 deletions packages/agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ For example,
import fetch from 'isomorphic-fetch';
import { HttpAgent } from '@dfinity/agent';

const host = process.env.DFX_NETWORK === 'local' ? 'http://localhost:4943' : 'https://icp-api.io';
const host = process.env.DFX_NETWORK === 'local' ? 'http://127.0.0.1:4943' : 'https://icp-api.io';

const agent = new HttpAgent({ fetch, host });
```
Expand All @@ -99,7 +99,7 @@ For example,
import fetch from 'isomorphic-fetch';
import { HttpAgent } from '@dfinity/agent';

const host = process.env.DFX_NETWORK === 'local' ? 'http://localhost:4943' : 'https://ic0.app';
const host = process.env.DFX_NETWORK === 'local' ? 'http://127.0.0.1:4943' : 'https://ic0.app';

/**
* @type {RequestInit}
Expand Down
18 changes: 9 additions & 9 deletions packages/agent/src/actor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('makeActor', () => {

expect(calls.length).toBe(5);
expect(calls[0]).toEqual([
`http://localhost/api/v2/canister/${canisterId.toText()}/call`,
`http://127.0.0.1/api/v2/canister/${canisterId.toText()}/call`,
{
method: 'POST',
headers: {
Expand All @@ -160,7 +160,7 @@ describe('makeActor', () => {
]);

expect(calls[1]).toEqual([
`http://localhost/api/v2/canister/${canisterId.toText()}/read_state`,
`http://127.0.0.1/api/v2/canister/${canisterId.toText()}/read_state`,
{
method: 'POST',
headers: {
Expand All @@ -176,7 +176,7 @@ describe('makeActor', () => {
},
]);

expect(calls[2][0]).toBe('http://localhost/api/v1/read');
expect(calls[2][0]).toBe('http://127.0.0.1/api/v1/read');
expect(calls[2][1]).toEqual({
method: 'POST',
headers: {
Expand All @@ -191,7 +191,7 @@ describe('makeActor', () => {
}),
});

expect(calls[3][0]).toBe('http://localhost/api/v1/read');
expect(calls[3][0]).toBe('http://127.0.0.1/api/v1/read');
expect(calls[3][1]).toEqual({
method: 'POST',
headers: {
Expand All @@ -206,7 +206,7 @@ describe('makeActor', () => {
}),
});

expect(calls[4][0]).toBe('http://localhost/api/v1/read');
expect(calls[4][0]).toBe('http://127.0.0.1/api/v1/read');
expect(calls[4][1]).toEqual({
method: 'POST',
headers: {
Expand All @@ -221,7 +221,7 @@ describe('makeActor', () => {
}),
});

expect(calls[5][0]).toBe('http://localhost/api/v1/call');
expect(calls[5][0]).toBe('http://127.0.0.1/api/v1/call');
expect(calls[5][1]).toEqual({
method: 'POST',
headers: {
Expand Down Expand Up @@ -273,7 +273,7 @@ describe('makeActor', () => {
// todo: add method to test update call after Certificate changes have been adjusted
});
};
const httpAgent = new HttpAgent({ fetch: mockFetch, host: 'http://localhost' });
const httpAgent = new HttpAgent({ fetch: mockFetch, host: 'http://127.0.0.1' });
const canisterId = Principal.fromText('2chl6-4hpzw-vqaaa-aaaaa-c');
const actor = Actor.createActor(actorInterface, { canisterId, agent: httpAgent });
const actorWithHttpDetails = Actor.createActorWithHttpDetails(actorInterface, {
Expand Down Expand Up @@ -312,7 +312,7 @@ describe('makeActor', () => {
greet: IDL.Func([IDL.Text], [IDL.Text]),
});
};
const httpAgent = new HttpAgent({ fetch: mockFetch, host: 'http://localhost' });
const httpAgent = new HttpAgent({ fetch: mockFetch, host: 'http://127.0.0.1' });
const canisterId = Principal.fromText('2chl6-4hpzw-vqaaa-aaaaa-c');
const actor = Actor.createActor(actorInterface, { canisterId, agent: httpAgent });

Expand All @@ -327,7 +327,7 @@ describe('makeActor', () => {
}
});
it('should throw a helpful error if the canisterId is not set', async () => {
const httpAgent = new HttpAgent({ host: 'http://localhost' });
const httpAgent = new HttpAgent({ host: 'http://127.0.0.1' });
const actorInterface = () => {
return IDL.Service({
greet: IDL.Func([IDL.Text], [IDL.Text]),
Expand Down
32 changes: 16 additions & 16 deletions packages/agent/src/agent/http/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const { window } = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
window.fetch = global.fetch;
(global as any).window = window;

const HTTP_AGENT_HOST = 'http://localhost:4943';
const HTTP_AGENT_HOST = 'http://127.0.0.1:4943';

const DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS = 5 * 60 * 1000;
const REPLICA_PERMITTED_DRIFT_MILLISECONDS = 60 * 1000;
Expand Down Expand Up @@ -72,7 +72,7 @@ test('call', async () => {
const nonce = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]) as Nonce;
const principal = Principal.anonymous();

const httpAgent = new HttpAgent({ fetch: mockFetch, host: 'http://localhost' });
const httpAgent = new HttpAgent({ fetch: mockFetch, host: 'http://127.0.0.1' });

const methodName = 'greet';
const arg = new Uint8Array([]);
Expand Down Expand Up @@ -108,7 +108,7 @@ test('call', async () => {
expect(requestId).toEqual(expectedRequestId);
const call1 = calls[0][0];
const call2 = calls[0][1];
expect(call1).toBe(`http://localhost/api/v2/canister/${canisterId.toText()}/call`);
expect(call1).toBe(`http://127.0.0.1/api/v2/canister/${canisterId.toText()}/call`);
expect(call2.method).toEqual('POST');
expect(call2.body).toEqual(cbor.encode(expectedRequest));
expect(call2.headers['Content-Type']).toEqual('application/cbor');
Expand Down Expand Up @@ -138,7 +138,7 @@ test('queries with the same content should have the same signature', async () =>

const httpAgent = new HttpAgent({
fetch: mockFetch,
host: 'http://localhost',
host: 'http://127.0.0.1',
disableNonce: true,
});
httpAgent.addTransform(makeNonceTransform(() => nonce));
Expand Down Expand Up @@ -198,7 +198,7 @@ test('readState should not call transformers if request is passed', async () =>

const httpAgent = new HttpAgent({
fetch: mockFetch,
host: 'http://localhost',
host: 'http://127.0.0.1',
disableNonce: true,
});
httpAgent.addTransform(makeNonceTransform(() => nonce));
Expand Down Expand Up @@ -288,7 +288,7 @@ test('use anonymous principal if unspecified', async () => {

const httpAgent = new HttpAgent({
fetch: mockFetch,
host: 'http://localhost',
host: 'http://127.0.0.1',
disableNonce: true,
});
httpAgent.addTransform(makeNonceTransform(() => nonce));
Expand Down Expand Up @@ -326,7 +326,7 @@ test('use anonymous principal if unspecified', async () => {
expect(calls.length).toBe(1);
expect(requestId).toEqual(expectedRequestId);

expect(calls[0][0]).toBe(`http://localhost/api/v2/canister/${canisterId.toText()}/call`);
expect(calls[0][0]).toBe(`http://127.0.0.1/api/v2/canister/${canisterId.toText()}/call`);
const call2 = calls[0][1];
expect(call2.method).toEqual('POST');
expect(call2.body).toEqual(cbor.encode(expectedRequest));
Expand Down Expand Up @@ -376,14 +376,14 @@ describe('invalidate identity', () => {
const mockFetch: jest.Mock = jest.fn();
it('should allow its identity to be invalidated', () => {
const identity = new AnonymousIdentity();
const agent = new HttpAgent({ identity, fetch: mockFetch, host: 'http://localhost' });
const agent = new HttpAgent({ identity, fetch: mockFetch, host: 'http://127.0.0.1' });
const invalidate = () => agent.invalidateIdentity();
expect(invalidate).not.toThrowError();
});
it('should throw an error instead of making a call if its identity is invalidated', async () => {
const canisterId: Principal = Principal.fromText('2chl6-4hpzw-vqaaa-aaaaa-c');
const identity = new AnonymousIdentity();
const agent = new HttpAgent({ identity, fetch: mockFetch, host: 'http://localhost' });
const agent = new HttpAgent({ identity, fetch: mockFetch, host: 'http://127.0.0.1' });
agent.invalidateIdentity();

const expectedError =
Expand Down Expand Up @@ -421,7 +421,7 @@ describe('replace identity', () => {
const mockFetch: jest.Mock = jest.fn();
it('should allow an actor to replace its identity', () => {
const identity = new AnonymousIdentity();
const agent = new HttpAgent({ identity, fetch: mockFetch, host: 'http://localhost' });
const agent = new HttpAgent({ identity, fetch: mockFetch, host: 'http://127.0.0.1' });

const identity2 = new AnonymousIdentity();
const replace = () => agent.replaceIdentity(identity2);
Expand All @@ -440,7 +440,7 @@ describe('replace identity', () => {

const canisterId: Principal = Principal.fromText('2chl6-4hpzw-vqaaa-aaaaa-c');
const identity = new AnonymousIdentity();
const agent = new HttpAgent({ identity, fetch: mockFetch, host: 'http://localhost' });
const agent = new HttpAgent({ identity, fetch: mockFetch, host: 'http://127.0.0.1' });
// First invalidate identity
agent.invalidateIdentity();
await agent
Expand Down Expand Up @@ -734,7 +734,7 @@ test('should fetch with given call options and fetch options', async () => {
const canisterId: Principal = Principal.fromText('2chl6-4hpzw-vqaaa-aaaaa-c');
const httpAgent = new HttpAgent({
fetch: mockFetch,
host: 'http://localhost',
host: 'http://127.0.0.1',
callOptions: {
reactNative: { textStreaming: true },
},
Expand Down Expand Up @@ -772,7 +772,7 @@ describe('default host', () => {
expect((agent as any)._host.hostname).toBe('icp-api.io');
});
it('should use the existing host if the agent is used on a known hostname', () => {
const knownHosts = ['ic0.app', 'icp0.io', 'localhost', '127.0.0.1'];
const knownHosts = ['ic0.app', 'icp0.io', '127.0.0.1', '127.0.0.1'];
for (const host of knownHosts) {
delete window.location;
window.location = {
Expand All @@ -784,7 +784,7 @@ describe('default host', () => {
}
});
it('should correctly handle subdomains on known hosts', () => {
const knownHosts = ['ic0.app', 'icp0.io', 'localhost', '127.0.0.1'];
const knownHosts = ['ic0.app', 'icp0.io', '127.0.0.1', '127.0.0.1'];
for (const host of knownHosts) {
delete window.location;
window.location = {
Expand All @@ -796,8 +796,8 @@ describe('default host', () => {
expect((agent as any)._host.hostname).toBe(host);
}
});
it('should handle port numbers for localhost', () => {
const knownHosts = ['localhost', '127.0.0.1'];
it('should handle port numbers for 127.0.0.1', () => {
const knownHosts = ['127.0.0.1', '127.0.0.1'];
for (const host of knownHosts) {
delete window.location;
// hostname is different from host when port is specified
Expand Down
4 changes: 2 additions & 2 deletions packages/agent/src/agent/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export class HttpAgent implements Agent {
);
}
// Mainnet and local will have the api route available
const knownHosts = ['ic0.app', 'icp0.io', 'localhost', '127.0.0.1'];
const knownHosts = ['ic0.app', 'icp0.io', '127.0.0.1', '127.0.0.1'];
const hostname = location?.hostname;
let knownHost;
if (hostname && typeof hostname === 'string') {
Expand Down Expand Up @@ -270,7 +270,7 @@ export class HttpAgent implements Agent {

public isLocal(): boolean {
const hostname = this._host.hostname;
return hostname === '127.0.0.1' || hostname.endsWith('localhost');
return hostname === '127.0.0.1' || hostname.endsWith('127.0.0.1');
}

public addTransform(fn: HttpAgentRequestTransformFn, priority = fn.priority || 0): void {
Expand Down
2 changes: 1 addition & 1 deletion packages/agent/src/fetch_candid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('simulate fetching a Candid interface', async () => {
);
});

const agent = new HttpAgent({ fetch: mockFetch, host: 'http://localhost' });
const agent = new HttpAgent({ fetch: mockFetch, host: 'http://127.0.0.1' });

const candid = await fetchCandid('ryjl3-tyaaa-aaaaa-aaaba-cai', agent);

Expand Down
Loading

0 comments on commit 95f8ad2

Please sign in to comment.