Skip to content

Commit

Permalink
added missing tests in utils
Browse files Browse the repository at this point in the history
Signed-off-by: sivasathyaseeelan <[email protected]>
  • Loading branch information
sivasathyaseeelan committed Jan 28, 2025
1 parent b445e8b commit 19d9ccd
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion core/api/test/unit/utils/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { btc2sat, sat2btc } from "@/domain/bitcoin"
import {
btc2sat,
sat2btc,
toSats,
toMilliSatsFromNumber,
toMilliSatsFromString,
checkedToCurrencyBaseAmount,
checkedToSats,
isSha256Hash,
} from "@/domain/bitcoin"
import {
InvalidCurrencyBaseAmountError,
InvalidSatoshiAmountError,
} from "@/domain/errors";
import { elapsedSinceTimestamp } from "@/utils"

describe("utils", () => {
Expand All @@ -22,6 +35,52 @@ describe("utils", () => {
})
})

describe("toSats", () => {
it("toSats converts number or bigint to satoshis", () => {
expect(toSats(12345)).toBe(12345);
expect(toSats(BigInt(12345))).toBe(12345);
})
})

describe("toMilliSatsFromNumber", () => {
it("toMilliSatsFromNumber converts number to millisatoshis", () => {
expect(toMilliSatsFromNumber(12345)).toBe(12345);
})
})

describe("toMilliSatsFromString", () => {
it("toMilliSatsFromString converts string to millisatoshis", () => {
expect(toMilliSatsFromString("12345")).toBe(12345);
expect(toMilliSatsFromString("0")).toBe(0);
})
})

describe("checkedToCurrencyBaseAmount", () => {
it("checkedToCurrencyBaseAmount validates currency base amount", () => {
expect(checkedToCurrencyBaseAmount(12345)).toBe(12345);
expect(checkedToCurrencyBaseAmount(0)).toBeInstanceOf(
InvalidCurrencyBaseAmountError
);
})
})

describe("checkedToSats", () => {
it("checkedToSats validates satoshi amount", () => {
expect(checkedToSats(12345)).toBe(12345);
expect(checkedToSats(0)).toBeInstanceOf(InvalidSatoshiAmountError);
})
})

describe("isSha256Hash", () => {
it("isSha256Hash validates SHA-256 hash format", () => {
expect(isSha256Hash("a3c45e9f8b7d0f4c5a1234567890abcdef1234567890abcdef1234567890abcd")).toBe(
true
);
expect(isSha256Hash("not-a-hash")).toBe(false);
expect(isSha256Hash("123")).toBe(false);
})
})

describe("elapsedFromTimestamp", () => {
it("returns expected number of seconds for elapsed time", () => {
const timestamp = new Date()
Expand Down

0 comments on commit 19d9ccd

Please sign in to comment.