getMaxPriorityFeePerGas.test.ts•2.96 kB
import { getMaxPriorityFeePerGas } from "../../../tools/ethereum/getMaxPriorityFeePerGas";
import { INFURA_CHAIN_URLS } from "../../../config/chains";
global.fetch = jest.fn();
describe("eth_maxPriorityFeePerGas", () => {
afterEach(() => {
jest.clearAllMocks();
});
it("fetches max priority fee successfully", async () => {
const mockResponse = { json: async () => ({ result: "0x55d4a80" }) } as Response;
(fetch as jest.MockedFunction<typeof fetch>).mockResolvedValueOnce(mockResponse);
const result = await getMaxPriorityFeePerGas.handler({});
expect(result.content?.[0]?.text).toContain("Max priority fee per gas on chainid 1");
expect(result.content?.[0]?.text).toContain("0x55d4a80");
expect(fetch).toHaveBeenCalledWith(
INFURA_CHAIN_URLS[1],
expect.objectContaining({ body: expect.stringContaining('"eth_maxPriorityFeePerGas"') })
);
});
it("handles unsupported chain ID", async () => {
const result = await getMaxPriorityFeePerGas.handler({ chainid: 9999 });
expect(result.content?.[0]?.text).toContain("Unsupported chain ID: 9999");
});
it("handles RPC error response", async () => {
const mockResponse = { json: async () => ({ error: { message: "RPC failed" } }) } as Response;
(fetch as jest.MockedFunction<typeof fetch>).mockResolvedValueOnce(mockResponse);
const result = await getMaxPriorityFeePerGas.handler({});
expect(result.content?.[0]?.text).toContain(
"Error fetching max priority fee per gas: RPC failed"
);
});
it("handles unknown RPC response (no result, no error)", async () => {
const mockResponse = { json: async () => ({}) } as Response;
(fetch as jest.MockedFunction<typeof fetch>).mockResolvedValueOnce(mockResponse);
const result = await getMaxPriorityFeePerGas.handler({});
expect(result.content?.[0]?.text).toContain(
"Error fetching max priority fee per gas: Unknown error"
);
});
it("handles network errors", async () => {
(fetch as jest.MockedFunction<typeof fetch>).mockRejectedValueOnce(new Error("Network down"));
const result = await getMaxPriorityFeePerGas.handler({});
expect(result.content?.[0]?.text).toContain(
"Error fetching max priority fee per gas: Network down"
);
});
it("handles non-Error thrown object", async () => {
(fetch as jest.MockedFunction<typeof fetch>).mockRejectedValueOnce("Unexpected failure");
const result = await getMaxPriorityFeePerGas.handler({});
expect(result.content?.[0]?.text).toContain(
"Error fetching max priority fee per gas: Unexpected failure"
);
});
it("handles unknown object errors correctly", async () => {
(fetch as jest.MockedFunction<typeof fetch>).mockRejectedValueOnce({
code: 123,
info: "bad",
});
const result = await getMaxPriorityFeePerGas.handler({});
expect(result.content?.[0]?.text).toContain('{"code":123,"info":"bad"}');
});
});