Skip to content

Commit

Permalink
Test the network hooks and NetworkConnection initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
schaable committed Sep 18, 2024
1 parent f55e534 commit bb29501
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import type { EthereumProvider } from "../../../../src/types/providers.js";
import type { NetworkConfig } from "@ignored/hardhat-vnext/types/config";

import assert from "node:assert/strict";
import { describe, it } from "node:test";

import { HttpProvider } from "../../../../src/internal/builtin-plugins/network-manager/http-provider.js";
import { NetworkConnectionImplementation } from "../../../../src/internal/builtin-plugins/network-manager/network-connection.js";

describe("NetworkConnectionImplementation", () => {
const localhostNetworkConfig: NetworkConfig = {
type: "http",
chainId: undefined,
chainType: undefined,
from: undefined,
gas: "auto",
gasMultiplier: 1,
gasPrice: "auto",
url: "http://localhost:8545",
timeout: 20_000,
httpHeaders: {},
};

describe("NetworkConnectionImplementation.create", () => {
it("should create a new network connection", async () => {
let expectedProvider: EthereumProvider | undefined;

const createProvider = async (): Promise<EthereumProvider> => {
expectedProvider = await HttpProvider.create({
url: localhostNetworkConfig.url,
networkName: "localhost",
extraHeaders: localhostNetworkConfig.httpHeaders,
timeout: localhostNetworkConfig.timeout,
});

return expectedProvider;
};

const closeConnection = async () => {};

const networkConnection = await NetworkConnectionImplementation.create(
1,
"localhost",
"unknown",
localhostNetworkConfig,
closeConnection,
createProvider,
);

assert.equal(networkConnection.id, 1);
assert.equal(networkConnection.networkName, "localhost");
assert.equal(networkConnection.chainType, "unknown");
assert.deepEqual(networkConnection.networkConfig, localhostNetworkConfig);
assert.deepEqual(networkConnection.provider, expectedProvider);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,57 @@ describe("NetworkManagerImplementation", () => {
});
});
});

describe("network -> closeConnection hook", () => {
it("should call the closeConnection hook when closing a connection", async () => {
let hookCalled = false;
const networkHooks: Partial<NetworkHooks> = {
closeConnection: async () => {
hookCalled = true;
},
};

hre.hooks.registerHandlers("network", networkHooks);

const networkConnection = await networkManager.connect();
await networkConnection.close();

hre.hooks.unregisterHandlers("network", networkHooks);

assert.ok(hookCalled, "The closeConnection hook was not called");
});
});

describe("network -> onRequest hook", async () => {
it("should call the onRequest hook when making a request", async () => {
let hookCalled = false;
const onRequest: NetworkHooks["onRequest"] = async (
context,
networkConnection,
jsonRpcRequest,
next,
) => {
hookCalled = true;
return next(context, networkConnection, jsonRpcRequest);
};
const networkHooks: Partial<NetworkHooks> = {
onRequest,
};

hre.hooks.registerHandlers("network", networkHooks);

const connection = await networkManager.connect();
// This will fail because we don't have a local node running
// but we don't care about the result, we just want to trigger the hook
try {
await connection.provider.request({
method: "eth_chainId",
});
} catch (error) {}

hre.hooks.unregisterHandlers("network", networkHooks);

assert.ok(hookCalled, "The onRequest hook was not called");
});
});
});

0 comments on commit bb29501

Please sign in to comment.