|
| 1 | +import { getIsRpcHealthy } from "../account"; |
| 2 | +import { NETWORK_ID } from "constants/localStorageTypes"; |
| 3 | +import { DEFAULT_NETWORKS } from "@shared/constants/stellar"; |
| 4 | +import type { DataStorageAccess } from "background/helpers/dataStorageAccess"; |
| 5 | + |
| 6 | +jest.mock("@shared/helpers/stellar", () => { |
| 7 | + const actual = jest.requireActual("@shared/helpers/stellar"); |
| 8 | + return { |
| 9 | + ...actual, |
| 10 | + isCustomNetwork: jest.fn(), |
| 11 | + }; |
| 12 | +}); |
| 13 | + |
| 14 | +jest.mock("@sentry/browser", () => ({ |
| 15 | + captureException: jest.fn(), |
| 16 | +})); |
| 17 | + |
| 18 | +describe("getIsRpcHealthy", () => { |
| 19 | + const mockFetch = jest.fn(); |
| 20 | + const mockIsCustomNetwork = jest.requireMock("@shared/helpers/stellar") |
| 21 | + .isCustomNetwork as jest.Mock; |
| 22 | + const mockCaptureException = jest.requireMock("@sentry/browser") |
| 23 | + .captureException as jest.Mock; |
| 24 | + |
| 25 | + const localStore: DataStorageAccess = { |
| 26 | + getItem: jest.fn(), |
| 27 | + setItem: jest.fn(), |
| 28 | + removeItem: jest.fn(), |
| 29 | + clear: jest.fn(), |
| 30 | + } as unknown as DataStorageAccess; |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + jest.resetAllMocks(); |
| 34 | + (global as any).fetch = mockFetch; |
| 35 | + (localStore.getItem as jest.Mock).mockImplementation(async (key) => { |
| 36 | + if (key === NETWORK_ID) { |
| 37 | + return { |
| 38 | + ...DEFAULT_NETWORKS[1], |
| 39 | + network: "testnet", |
| 40 | + networkName: "Testnet", |
| 41 | + }; |
| 42 | + } |
| 43 | + return undefined; |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it("returns true without calling fetch for custom networks", async () => { |
| 48 | + mockIsCustomNetwork.mockReturnValue(true); |
| 49 | + |
| 50 | + const result = await getIsRpcHealthy(localStore); |
| 51 | + |
| 52 | + expect(result).toBe(true); |
| 53 | + expect(mockFetch).not.toHaveBeenCalled(); |
| 54 | + }); |
| 55 | + |
| 56 | + it("returns true when rpc health is healthy", async () => { |
| 57 | + mockIsCustomNetwork.mockReturnValue(false); |
| 58 | + mockFetch.mockResolvedValue({ |
| 59 | + ok: true, |
| 60 | + json: async () => ({ status: "healthy" }), |
| 61 | + }); |
| 62 | + |
| 63 | + const result = await getIsRpcHealthy(localStore); |
| 64 | + |
| 65 | + expect(mockFetch).toHaveBeenCalledWith( |
| 66 | + "http://localhost:3003/api/v1/rpc-health?network=testnet", |
| 67 | + ); |
| 68 | + expect(result).toBe(true); |
| 69 | + expect(mockCaptureException).not.toHaveBeenCalled(); |
| 70 | + }); |
| 71 | + |
| 72 | + it("returns false and captures when rpc health is unhealthy", async () => { |
| 73 | + mockIsCustomNetwork.mockReturnValue(false); |
| 74 | + mockFetch.mockResolvedValue({ |
| 75 | + ok: true, |
| 76 | + json: async () => ({ status: "unhealthy" }), |
| 77 | + }); |
| 78 | + |
| 79 | + const result = await getIsRpcHealthy(localStore); |
| 80 | + |
| 81 | + expect(result).toBe(false); |
| 82 | + expect(mockCaptureException).toHaveBeenCalledWith( |
| 83 | + "Soroban RPC is not healthy - unhealthy", |
| 84 | + ); |
| 85 | + }); |
| 86 | +}); |
0 commit comments