|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { getErrorDetails, isInsufficientFundsError } from "./helpers.js"; |
| 3 | + |
| 4 | +describe("isInsufficientFundsError", () => { |
| 5 | + it("should detect basic insufficient funds error message", () => { |
| 6 | + const error = new Error("insufficient funds"); |
| 7 | + expect(isInsufficientFundsError(error)).toBe(true); |
| 8 | + }); |
| 9 | + |
| 10 | + it("should detect insufficient funds for gas error", () => { |
| 11 | + const error = new Error("Insufficient funds for gas * price + value"); |
| 12 | + expect(isInsufficientFundsError(error)).toBe(true); |
| 13 | + }); |
| 14 | + |
| 15 | + it("should detect insufficient funds for intrinsic transaction cost", () => { |
| 16 | + const error = new Error( |
| 17 | + "insufficient funds for intrinsic transaction cost", |
| 18 | + ); |
| 19 | + expect(isInsufficientFundsError(error)).toBe(true); |
| 20 | + }); |
| 21 | + |
| 22 | + it("should detect insufficient balance error", () => { |
| 23 | + const error = new Error("insufficient balance"); |
| 24 | + expect(isInsufficientFundsError(error)).toBe(true); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should detect insufficient native funds error", () => { |
| 28 | + const error = new Error("Insufficient Native Funds"); |
| 29 | + expect(isInsufficientFundsError(error)).toBe(true); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should detect INSUFFICIENT_FUNDS error code", () => { |
| 33 | + const error = { code: "INSUFFICIENT_FUNDS", message: "Transaction failed" }; |
| 34 | + expect(isInsufficientFundsError(error)).toBe(true); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should detect reason field", () => { |
| 38 | + const error = { |
| 39 | + reason: "insufficient funds", |
| 40 | + message: "Transaction failed", |
| 41 | + }; |
| 42 | + expect(isInsufficientFundsError(error)).toBe(true); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should detect error in nested data.message", () => { |
| 46 | + const error = { data: { message: "insufficient funds for gas" } }; |
| 47 | + expect(isInsufficientFundsError(error)).toBe(true); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should handle string errors", () => { |
| 51 | + expect(isInsufficientFundsError("insufficient funds")).toBe(true); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should return false for non-insufficient funds errors", () => { |
| 55 | + const error = new Error("User rejected transaction"); |
| 56 | + expect(isInsufficientFundsError(error)).toBe(false); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should return false for null/undefined", () => { |
| 60 | + expect(isInsufficientFundsError(null)).toBe(false); |
| 61 | + expect(isInsufficientFundsError(undefined)).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should be case insensitive", () => { |
| 65 | + const error = new Error("INSUFFICIENT FUNDS FOR GAS"); |
| 66 | + expect(isInsufficientFundsError(error)).toBe(true); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe("getErrorDetails", () => { |
| 71 | + it("should extract message and code from Error object", () => { |
| 72 | + const error = new Error("Test error message"); |
| 73 | + const details = getErrorDetails(error); |
| 74 | + expect(details.message).toBe("Test error message"); |
| 75 | + expect(details.code).toBeUndefined(); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should extract message and code from error object", () => { |
| 79 | + const error = { message: "Test message", code: "TEST_CODE" }; |
| 80 | + const details = getErrorDetails(error); |
| 81 | + expect(details.message).toBe("Test message"); |
| 82 | + expect(details.code).toBe("TEST_CODE"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should extract message from nested data", () => { |
| 86 | + const error = { data: { message: "Nested error message" } }; |
| 87 | + const details = getErrorDetails(error); |
| 88 | + expect(details.message).toBe("Nested error message"); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should handle string errors", () => { |
| 92 | + const details = getErrorDetails("String error"); |
| 93 | + expect(details.message).toBe("String error"); |
| 94 | + expect(details.code).toBeUndefined(); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should handle null/undefined", () => { |
| 98 | + const details = getErrorDetails(null); |
| 99 | + expect(details.message).toBe("Unknown error"); |
| 100 | + expect(details.code).toBeUndefined(); |
| 101 | + }); |
| 102 | + |
| 103 | + it("should extract reason as code", () => { |
| 104 | + const error = { message: "Test message", reason: "test_reason" }; |
| 105 | + const details = getErrorDetails(error); |
| 106 | + expect(details.message).toBe("Test message"); |
| 107 | + expect(details.code).toBe("test_reason"); |
| 108 | + }); |
| 109 | +}); |
0 commit comments