Skip to content
This repository was archived by the owner on Oct 21, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading