-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |