diff --git a/packages/berajs/src/utils/encoder.test.ts b/packages/berajs/src/utils/encoder.test.ts new file mode 100644 index 000000000..8b6dbaa9b --- /dev/null +++ b/packages/berajs/src/utils/encoder.test.ts @@ -0,0 +1,25 @@ +import { describe, expect, test } from "@jest/globals"; +import { encrypt, decrypt } from "./encoder"; + +describe("Encoder Utils", () => { + const message = "Hello, World!"; + const key = "secretkey123"; + + test("encrypt should return a non-empty string", () => { + const cipherText = encrypt(message, key); + expect(typeof cipherText).toBe("string"); + expect(cipherText.length).toBeGreaterThan(0); + }); + + test("decrypt should return the original message with 0x prefix", () => { + const cipherText = encrypt(message, key); + const decryptedMessage = decrypt(cipherText, key); + expect(decryptedMessage).toBe(`0x${message}`); + }); + + test('decrypt should return "0x" for invalid cipher', () => { + const invalidCipher = "invalidciphertext"; + const result = decrypt(invalidCipher, key); + expect(result).toBe("0x"); + }); +}); diff --git a/packages/berajs/src/utils/errorMessages.test.ts b/packages/berajs/src/utils/errorMessages.test.ts new file mode 100644 index 000000000..5b6d75262 --- /dev/null +++ b/packages/berajs/src/utils/errorMessages.test.ts @@ -0,0 +1,17 @@ +import { describe, expect, test } from "@jest/globals"; + +import { getCustomAppErrorMessages, getErrorMessage } from "./errorMessages"; + +describe("Error Messages Utils", () => { + test("getCustomAppErrorMessages should return correct error message for PERPS", () => { + const error = { metaMessages: ["PriceImpactTooHigh"] }; + const message = getCustomAppErrorMessages(error, "PERPS"); + expect(message).toBe("This position causes too much price impact."); + }); + + test("getErrorMessage should return GENERAL_ERROR for unknown error", () => { + const error = { unknownKey: "unknownValue" }; + const message = getErrorMessage(error); + expect(message).toBe("Something went wrong. Please try again later."); + }); +}); diff --git a/packages/berajs/src/utils/fixedPointEthers.test.ts b/packages/berajs/src/utils/fixedPointEthers.test.ts new file mode 100644 index 000000000..f68c9e97a --- /dev/null +++ b/packages/berajs/src/utils/fixedPointEthers.test.ts @@ -0,0 +1,33 @@ +import { describe, expect, test } from "@jest/globals"; +import { + toQ64, + fromQ64, + toQ48, + fromQ48, + toSqrtPrice, + fromSqrtPrice, +} from "./fixedPointEthers"; +import { BigNumber } from "ethers"; + +describe("Fixed Point Ethers Utils", () => { + test("toQ64 and fromQ64 should correctly convert values", () => { + const value = 123.456; + const q64Value = toQ64(value); + const result = fromQ64(q64Value); + expect(result).toBeCloseTo(value, 5); + }); + + test("toQ48 and fromQ48 should correctly convert values", () => { + const value = 789.012; + const q48Value = toQ48(value); + const result = fromQ48(q48Value); + expect(result).toBeCloseTo(value, 5); + }); + + test("toSqrtPrice and fromSqrtPrice should correctly convert values", () => { + const price = 10000; + const sqrtPrice = toSqrtPrice(price); + const result = fromSqrtPrice(BigNumber.from(sqrtPrice)); + expect(result).toBeCloseTo(price, 5); + }); +}); diff --git a/packages/berajs/src/utils/formatAmountSmall.test.ts b/packages/berajs/src/utils/formatAmountSmall.test.ts new file mode 100644 index 000000000..34a276545 --- /dev/null +++ b/packages/berajs/src/utils/formatAmountSmall.test.ts @@ -0,0 +1,27 @@ +import { describe, expect, test } from "@jest/globals"; + +import { formatAmountSmall } from "./formatAmountSmall"; + +describe("formatAmountSmall", () => { + test("should return isSmall as false and numericValue as 0 for NaN or zero", () => { + expect(formatAmountSmall("abc")).toEqual({ + isSmall: false, + numericValue: 0, + }); + expect(formatAmountSmall(0)).toEqual({ isSmall: false, numericValue: 0 }); + }); + + test("should return isSmall as true for values less than 0.01", () => { + expect(formatAmountSmall(0.005)).toEqual({ + isSmall: true, + numericValue: 0.01, + }); + }); + + test("should return isSmall as false for values greater than or equal to 0.01", () => { + expect(formatAmountSmall(0.02)).toEqual({ + isSmall: false, + numericValue: 0.02, + }); + }); +}); diff --git a/packages/berajs/src/utils/formatNumber.test.ts b/packages/berajs/src/utils/formatNumber.test.ts new file mode 100644 index 000000000..462195ba0 --- /dev/null +++ b/packages/berajs/src/utils/formatNumber.test.ts @@ -0,0 +1,18 @@ +import { describe, expect, test } from "@jest/globals"; + +import { formatNumber } from "./formatNumber"; + +describe("formatNumber", () => { + test("should format numbers with default 8 decimals", () => { + expect(formatNumber(1234.56789)).toBe("1234.56789"); + }); + + test("should format numbers with specified decimals", () => { + expect(formatNumber(1234.56789, 2)).toBe("1234.57"); + }); + + test("should remove trailing zeros", () => { + expect(formatNumber(1234.5)).toBe("1234.5"); + expect(formatNumber(1234.0)).toBe("1234"); + }); +}); diff --git a/packages/berajs/src/utils/formatUsd.test.ts b/packages/berajs/src/utils/formatUsd.test.ts new file mode 100644 index 000000000..d79b533fb --- /dev/null +++ b/packages/berajs/src/utils/formatUsd.test.ts @@ -0,0 +1,18 @@ +import { describe, expect, test } from "@jest/globals"; + +import { formatUsd } from "./formatUsd"; + +describe("formatUsd with en-US locale", () => { + test("should format numbers as USD currency", () => { + expect(formatUsd(1234.56)).toBe("$1,234.56"); + }); + + test("should format strings as USD currency", () => { + expect(formatUsd("1234.56")).toBe("$1,234.56"); + }); + + test("should return $0.00 for invalid inputs", () => { + expect(formatUsd("abc")).toBe("$0.00"); + expect(formatUsd({} as any)).toBe("$0.00"); + }); +}); diff --git a/packages/berajs/src/utils/handle-native-bera.test.ts b/packages/berajs/src/utils/handle-native-bera.test.ts new file mode 100755 index 000000000..238f30b5f --- /dev/null +++ b/packages/berajs/src/utils/handle-native-bera.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, test, jest } from "@jest/globals"; +import { handleNativeBera } from './handle-native-bera'; +import { beraTokenAddress, nativeTokenAddress } from '@bera/config'; +import { getAddress } from 'viem'; + +// From .env +jest.mock('@bera/config', () => ({ + beraTokenAddress: '0x7507c1dc16935B82698e4C63f2746A2fCf994dF8', + nativeTokenAddress: '0x0000000000000000000000000000000000000000', +})); + +describe('handleNativeBera', () => { + test('should return beraTokenAddress if tokenAddress is nativeTokenAddress', () => { + const result = handleNativeBera(nativeTokenAddress); + expect(result).toBe(getAddress(beraTokenAddress)); + }); + + test('should return the same address if tokenAddress is not nativeTokenAddress', () => { + const someOtherAddress = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'; + const result = handleNativeBera(someOtherAddress); + expect(result).toBe(getAddress(someOtherAddress)); + }); + + test('should return the input if it is not a valid address', () => { + const invalidAddress = 'invalidAddress'; + const result = handleNativeBera(invalidAddress); + expect(result).toBe(invalidAddress); + }); +}); \ No newline at end of file diff --git a/packages/berajs/src/utils/timeFrame.test.ts b/packages/berajs/src/utils/timeFrame.test.ts new file mode 100755 index 000000000..2a4024cd6 --- /dev/null +++ b/packages/berajs/src/utils/timeFrame.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, test } from "@jest/globals"; + +import { TimeFrame, getTime, timeFrameToNumber } from "./timeFrame"; + +describe("TimeFrame Utils", () => { + test("should calculate correct time for HOURLY", () => { + const currentTime = Math.floor(Date.now() / 1000); + const expectedTime = currentTime - timeFrameToNumber[TimeFrame.HOURLY]; + expect(getTime(TimeFrame.HOURLY)).toBeCloseTo(expectedTime, 1); + }); + + test("should calculate correct time for WEEKLY", () => { + const currentTime = Math.floor(Date.now() / 1000); + const expectedTime = currentTime - timeFrameToNumber[TimeFrame.WEEKLY]; + expect(getTime(TimeFrame.WEEKLY)).toBeCloseTo(expectedTime, 1); + }); + + test("should calculate correct time for MONTHLY", () => { + const currentTime = Math.floor(Date.now() / 1000); + const expectedTime = currentTime - timeFrameToNumber[TimeFrame.MONTHLY]; + expect(getTime(TimeFrame.MONTHLY)).toBeCloseTo(expectedTime, 1); + }); + + test("should calculate correct time for QUARTERLY", () => { + const currentTime = Math.floor(Date.now() / 1000); + const expectedTime = currentTime - timeFrameToNumber[TimeFrame.QUARTERLY]; + expect(getTime(TimeFrame.QUARTERLY)).toBeCloseTo(expectedTime, 1); + }); +});