Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest"
2import { Encoding } from "effect"
3
4const assertRandomHex = (length: number, expectedLength: number): void => {
5 const value = Encoding.randomHex(length)
6 assert.strictEqual(value.length, expectedLength)
7 assert.match(value, /^[0-9a-f]*$/)
8}
9
10describe("Encoding", () => {
11 describe("randomHex", () => {
12 it("generates lowercase hexadecimal words", () => {
13 assertRandomHex(0, 0)
14 assertRandomHex(8, 8)
15 assertRandomHex(16, 16)
16 assertRandomHex(32, 32)
17 })
18
19 it("rounds non-negative lengths down to multiples of 8", () => {
20 assertRandomHex(7, 0)
21 assertRandomHex(15.9, 8)
22 assertRandomHex(23, 16)
23 })
24
25 it("uses unsigned 32-bit coercion without validation", () => {
26 assertRandomHex(Number.NaN, 0)
27 assertRandomHex(Number.POSITIVE_INFINITY, 0)
28 assertRandomHex(2 ** 32, 0)
29 assertRandomHex(2 ** 32 + 8, 8)
30 })
31 })
32})