Skip to content

Commit 9b46702

Browse files
committed
Test the network hooks and NetworkConnection initialization
1 parent 87be7cf commit 9b46702

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type { EthereumProvider } from "../../../../src/types/providers.js";
2+
import type { NetworkConfig } from "@ignored/hardhat-vnext/types/config";
3+
4+
import assert from "node:assert/strict";
5+
import { describe, it } from "node:test";
6+
7+
import { HttpProvider } from "../../../../src/internal/builtin-plugins/network-manager/http-provider.js";
8+
import { NetworkConnectionImplementation } from "../../../../src/internal/builtin-plugins/network-manager/network-connection.js";
9+
10+
describe("NetworkConnectionImplementation", () => {
11+
const localhostNetworkConfig: NetworkConfig = {
12+
type: "http",
13+
chainId: undefined,
14+
chainType: undefined,
15+
from: undefined,
16+
gas: "auto",
17+
gasMultiplier: 1,
18+
gasPrice: "auto",
19+
url: "http://localhost:8545",
20+
timeout: 20_000,
21+
httpHeaders: {},
22+
};
23+
24+
describe("NetworkConnectionImplementation.create", () => {
25+
it("should create a new network connection", async () => {
26+
let expectedProvider: EthereumProvider | undefined;
27+
28+
const createProvider = async (): Promise<EthereumProvider> => {
29+
expectedProvider = await HttpProvider.create({
30+
url: localhostNetworkConfig.url,
31+
networkName: "localhost",
32+
extraHeaders: localhostNetworkConfig.httpHeaders,
33+
timeout: localhostNetworkConfig.timeout,
34+
});
35+
36+
return expectedProvider;
37+
};
38+
39+
const closeConnection = async () => {};
40+
41+
const networkConnection = await NetworkConnectionImplementation.create(
42+
1,
43+
"localhost",
44+
"unknown",
45+
localhostNetworkConfig,
46+
closeConnection,
47+
createProvider,
48+
);
49+
50+
assert.equal(networkConnection.id, 1);
51+
assert.equal(networkConnection.networkName, "localhost");
52+
assert.equal(networkConnection.chainType, "unknown");
53+
assert.deepEqual(networkConnection.networkConfig, localhostNetworkConfig);
54+
assert.deepEqual(networkConnection.provider, expectedProvider);
55+
});
56+
});
57+
});

v-next/hardhat/test/internal/builtin-plugins/network-manager/network-manager.ts

+53
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,57 @@ describe("NetworkManagerImplementation", () => {
209209
});
210210
});
211211
});
212+
213+
describe("network -> closeConnection hook", () => {
214+
it("should call the closeConnection hook when closing a connection", async () => {
215+
let hookCalled = false;
216+
const networkHooks: Partial<NetworkHooks> = {
217+
closeConnection: async () => {
218+
hookCalled = true;
219+
},
220+
};
221+
222+
hre.hooks.registerHandlers("network", networkHooks);
223+
224+
const networkConnection = await networkManager.connect();
225+
await networkConnection.close();
226+
227+
hre.hooks.unregisterHandlers("network", networkHooks);
228+
229+
assert.ok(hookCalled, "The closeConnection hook was not called");
230+
});
231+
});
232+
233+
describe("network -> onRequest hook", async () => {
234+
it("should call the onRequest hook when making a request", async () => {
235+
let hookCalled = false;
236+
const onRequest: NetworkHooks["onRequest"] = async (
237+
context,
238+
networkConnection,
239+
jsonRpcRequest,
240+
next,
241+
) => {
242+
hookCalled = true;
243+
return next(context, networkConnection, jsonRpcRequest);
244+
};
245+
const networkHooks: Partial<NetworkHooks> = {
246+
onRequest,
247+
};
248+
249+
hre.hooks.registerHandlers("network", networkHooks);
250+
251+
const connection = await networkManager.connect();
252+
// This will fail because we don't have a local node running
253+
// but we don't care about the result, we just want to trigger the hook
254+
try {
255+
await connection.provider.request({
256+
method: "eth_chainId",
257+
});
258+
} catch (error) {}
259+
260+
hre.hooks.unregisterHandlers("network", networkHooks);
261+
262+
assert.ok(hookCalled, "The onRequest hook was not called");
263+
});
264+
});
212265
});

0 commit comments

Comments
 (0)