Skip to content

Commit

Permalink
chore: up test
Browse files Browse the repository at this point in the history
  • Loading branch information
iosh committed Oct 10, 2024
1 parent 67401e2 commit bf3a58e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 14 deletions.
78 changes: 78 additions & 0 deletions __test__/customConfig.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { http, createPublicClient, webSocket } from "cive";
import { privateKeyToAccount } from "cive/accounts";
import { hexAddressToBase32 } from "cive/utils";
import { beforeAll, describe, expect, test } from "vitest";
import { createServer } from "../index";
import { localChain } from "./help";

const TEST_MINING_ADDRESS = "0x1b13CC31fC4Ceca3b72e3cc6048E7fabaefB3AC3";
const TEST_MINING_ADDRESS_PK =
"0xe590669d866ccd3baf9ec816e3b9f50a964b678e8752386c0d81c39d7e9c6930";
const TEST_NETWORK_ID = 1111;

const miningAccount = privateKeyToAccount(TEST_MINING_ADDRESS_PK, {
networkId: TEST_NETWORK_ID,
});

const TEST_PK = [
"5880f9c6c419f4369b53327bae24e62d805ab0ff825acf7e7945d1f9a0a17bd0",
"346328c0b7e2abf88fd9c5126192a85eb18b5309c3bd3cc7b61c8b403d1b0e68",
"bdc6d1c95aaaac8db4eaa5d3d097ba556cf2871149f31e695458d7813543c258",
"8d108e2aff19b1134c7cc29fbe4a721e599ef21d7d183f64dcd5e78e3a12e5e7",
"e9cfd4d1d29f7a67c970c1d5c145e958061cd54d05a83e29c7e39b7be894c9c6",
];

beforeAll(async () => {
const server = await createServer({
nodeType: "full",
devBlockIntervalMs: 200,
miningAuthor: TEST_MINING_ADDRESS,
jsonrpcHttpPort: 12555,
jsonrpcWsPort: 12556,
chainId: TEST_NETWORK_ID,
evmChainId: 2222,
genesisSecrets: TEST_PK,
// devPackTxImmediately: true,
posReferenceEnableHeight: 1,
defaultTransitionTime: 2,
});

await server.start();

await new Promise((resolve) => setTimeout(resolve, 15000));

return () => server.stop();
});

describe("customConfig", () => {
test("test http port", async () => {
const client = createPublicClient({
chain: localChain,
transport: http("http://127.0.0.1:12555"),
});

expect((await client.getStatus()).chainId).toBe(1111);
});

test("test ws port", async () => {
const client = createPublicClient({
chain: localChain,
transport: webSocket("ws://127.0.0.1:12556"),
});

expect((await client.getStatus()).chainId).toBe(1111);
});

test("test mining address", async () => {
const client = createPublicClient({
chain: localChain,
transport: http("http://127.0.0.1:12555"),
});

expect(
await client.getBalance({
address: miningAccount.address,
}),
).toMatchInlineSnapshot("0n");
});
});
12 changes: 1 addition & 11 deletions __test__/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,7 @@ describe("server", () => {

await server.stop();

await expect(async () =>
client.getStatus(),
).rejects.toThrowErrorMatchingInlineSnapshot(`
[TimeoutError: The request took too long to respond.
URL: http://127.0.0.1:12537
Request body: {"method":"cfx_getStatus"}
Details: The request timed out.
Version: 2.21.16]
`);
await expect(async () => client.getStatus()).rejects.toThrow();
await new Promise((resolve) => setTimeout(resolve, 3000));
});
});
4 changes: 1 addition & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import { type ConfluxConfig, ConfluxNode } from "./conflux";

export type { ConfluxConfig };


let isServiceCreated = false;
export async function createServer(userConfig: ConfluxConfig = {}) {

if (isServiceCreated) {
throw new Error("The server has already been created");
}
Expand All @@ -23,7 +21,7 @@ export async function createServer(userConfig: ConfluxConfig = {}) {
...userConfig,
};

const node = new ConfluxNode()
const node = new ConfluxNode();
return {
async start() {
node.startNode(config);
Expand Down

0 comments on commit bf3a58e

Please sign in to comment.