|
| 1 | +import { beforeEach, describe, expect, test, mock, spyOn } from "bun:test"; |
| 2 | +import { existsSync, readFileSync } from "fs"; |
| 3 | +import { RequestPrefix } from "./types"; |
| 4 | +import { chainToChainId } from "@wormhole-foundation/sdk-base"; |
| 5 | + |
| 6 | +const mockExistsSync = mock(existsSync); |
| 7 | +const mockReadFileSync = mock(readFileSync); |
| 8 | + |
| 9 | +mock.module("fs", () => ({ |
| 10 | + existsSync: mockExistsSync, |
| 11 | + readFileSync: mockReadFileSync, |
| 12 | +})); |
| 13 | + |
| 14 | +describe("Custom Chain Loading", () => { |
| 15 | + beforeEach(() => { |
| 16 | + mockExistsSync.mockClear(); |
| 17 | + mockReadFileSync.mockClear(); |
| 18 | + |
| 19 | + delete process.env.CHAIN_CONFIG_PATH; |
| 20 | + delete process.env.CUSTOM_CHAINS; |
| 21 | + }); |
| 22 | + |
| 23 | + test("loads custom chains from file when file exists", async () => { |
| 24 | + const mockCustomChainData = { |
| 25 | + "10003": { |
| 26 | + wormholeChainId: 10003, |
| 27 | + evmChainId: 421614, |
| 28 | + rpc: "https://sepolia-rollup.arbitrum.io/rpc", |
| 29 | + name: "Arbitrum Sepolia", |
| 30 | + gasPriceDecimals: 18, |
| 31 | + nativeDecimals: 18, |
| 32 | + executorAddress: "0x1234567890123456789012345678901234567890", |
| 33 | + coreContractAddress: "0x0987654321098765432109876543210987654321", |
| 34 | + viemChainName: "arbitrumSepolia", |
| 35 | + viemTokenName: "Ether", |
| 36 | + viemTokenSymbol: "ETH", |
| 37 | + capabilities: { |
| 38 | + requestPrefixes: ["ERV1"], |
| 39 | + gasDropOffLimit: "100000000000", |
| 40 | + maxGasLimit: "1000000", |
| 41 | + maxMsgValue: "200000000000", |
| 42 | + }, |
| 43 | + }, |
| 44 | + }; |
| 45 | + |
| 46 | + mockExistsSync.mockReturnValue(true); |
| 47 | + mockReadFileSync.mockReturnValue(JSON.stringify(mockCustomChainData)); |
| 48 | + |
| 49 | + delete require.cache[require.resolve("./chains")]; |
| 50 | + const { enabledChains } = await import("./chains"); |
| 51 | + |
| 52 | + expect(mockExistsSync).toHaveBeenCalledWith("/config/chains.json"); |
| 53 | + expect(mockReadFileSync).toHaveBeenCalledWith( |
| 54 | + "/config/chains.json", |
| 55 | + "utf-8", |
| 56 | + ); |
| 57 | + |
| 58 | + expect(enabledChains[10003]).toBeDefined(); |
| 59 | + expect(enabledChains[10003]!.name).toBe("Arbitrum Sepolia"); |
| 60 | + expect(enabledChains[10003]!.evmChainId).toBe(421614); |
| 61 | + |
| 62 | + expect(enabledChains[10003]!.capabilities.gasDropOffLimit).toBe( |
| 63 | + 100000000000n, |
| 64 | + ); |
| 65 | + expect(enabledChains[10003]!.capabilities.maxGasLimit).toBe(1000000n); |
| 66 | + expect(enabledChains[10003]!.capabilities.maxMsgValue).toBe(200000000000n); |
| 67 | + expect(enabledChains[10003]!.capabilities.requestPrefixes).toEqual([ |
| 68 | + RequestPrefix.ERV1, |
| 69 | + ]); |
| 70 | + |
| 71 | + expect(enabledChains[10003]!.viemChain).toBeDefined(); |
| 72 | + expect(enabledChains[10003]!.viemChain?.name).toBe("arbitrumSepolia"); |
| 73 | + expect(enabledChains[10003]!.viemChain?.id).toBe(421614); |
| 74 | + expect(enabledChains[10003]!.viemChain?.nativeCurrency.name).toBe("Ether"); |
| 75 | + expect(enabledChains[10003]!.viemChain?.nativeCurrency.symbol).toBe("ETH"); |
| 76 | + }); |
| 77 | + |
| 78 | + test("loads custom chains from custom path when CHAIN_CONFIG_PATH is set", async () => { |
| 79 | + const customPath = "/custom/path/chains.json"; |
| 80 | + process.env.CHAIN_CONFIG_PATH = customPath; |
| 81 | + |
| 82 | + chainToChainId("ArbitrumSepolia"); |
| 83 | + const mockCustomChainData = { |
| 84 | + "10003": { |
| 85 | + wormholeChainId: 10003, |
| 86 | + evmChainId: 1, |
| 87 | + rpc: "https://eth-mainnet.example.com", |
| 88 | + name: "Ethereum Mainnet", |
| 89 | + gasPriceDecimals: 18, |
| 90 | + nativeDecimals: 18, |
| 91 | + executorAddress: "0x1111111111111111111111111111111111111111", |
| 92 | + coreContractAddress: "0x2222222222222222222222222222222222222222", |
| 93 | + viemChainName: "mainnet", |
| 94 | + viemTokenName: "Ether", |
| 95 | + viemTokenSymbol: "ETH", |
| 96 | + capabilities: { |
| 97 | + requestPrefixes: ["ERV1"], |
| 98 | + gasDropOffLimit: "50000000000", |
| 99 | + maxGasLimit: "2000000", |
| 100 | + maxMsgValue: "100000000000", |
| 101 | + }, |
| 102 | + }, |
| 103 | + }; |
| 104 | + |
| 105 | + mockExistsSync.mockReturnValue(true); |
| 106 | + mockReadFileSync.mockReturnValue(JSON.stringify(mockCustomChainData)); |
| 107 | + |
| 108 | + delete require.cache[require.resolve("./chains")]; |
| 109 | + const { enabledChains } = await import("./chains"); |
| 110 | + |
| 111 | + expect(mockExistsSync).toHaveBeenCalledWith(customPath); |
| 112 | + expect(mockReadFileSync).toHaveBeenCalledWith(customPath, "utf-8"); |
| 113 | + expect(enabledChains[10003]).toBeDefined(); |
| 114 | + expect(enabledChains[10003]!.name).toBe("Ethereum Mainnet"); |
| 115 | + }); |
| 116 | + |
| 117 | + test("handles file reading errors gracefully", async () => { |
| 118 | + const consoleSpy = spyOn(console, "error").mockImplementation(() => {}); |
| 119 | + |
| 120 | + mockExistsSync.mockReturnValue(true); |
| 121 | + mockReadFileSync.mockImplementation(() => { |
| 122 | + throw new Error("File read error"); |
| 123 | + }); |
| 124 | + |
| 125 | + delete require.cache[require.resolve("./chains")]; |
| 126 | + const { enabledChains } = await import("./chains"); |
| 127 | + |
| 128 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 129 | + "Failed to load custom chains from", |
| 130 | + "/config/chains.json", |
| 131 | + expect.any(Error), |
| 132 | + ); |
| 133 | + }); |
| 134 | + |
| 135 | + test("handles invalid JSON in file gracefully", async () => { |
| 136 | + const consoleSpy = spyOn(console, "error").mockImplementation(() => {}); |
| 137 | + |
| 138 | + mockExistsSync.mockReturnValue(true); |
| 139 | + mockReadFileSync.mockReturnValue("invalid json content"); |
| 140 | + |
| 141 | + delete require.cache[require.resolve("./chains")]; |
| 142 | + |
| 143 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 144 | + "Failed to load custom chains from", |
| 145 | + "/config/chains.json", |
| 146 | + expect.any(Error), |
| 147 | + ); |
| 148 | + }); |
| 149 | +}); |
0 commit comments