import { expect, test, vi } from "vitest" import { getRandomIndex, getRandomUint32, getRandomUnitValue } from "#Source/random/index.ts" test("getRandomUint32 returns an integer within uint32 range across available random sources", () => { const cryptoBackedValue = getRandomUint32() expect(Number.isInteger(cryptoBackedValue)).toBe(true) expect(cryptoBackedValue).toBeGreaterThanOrEqual(0) expect(cryptoBackedValue).toBeLessThan(0x1_0000_0000) vi.stubGlobal("crypto", undefined) try { const fallbackValue = getRandomUint32() expect(Number.isInteger(fallbackValue)).toBe(true) expect(fallbackValue).toBeGreaterThanOrEqual(0) expect(fallbackValue).toBeLessThan(0x1_0000_0000) } finally { vi.unstubAllGlobals() } }) test("getRandomUnitValue returns a unit-interval float across available random sources", () => { const cryptoBackedValue = getRandomUnitValue() expect(cryptoBackedValue).toBeGreaterThanOrEqual(0) expect(cryptoBackedValue).toBeLessThan(1) vi.stubGlobal("crypto", undefined) try { const fallbackValue = getRandomUnitValue() expect(fallbackValue).toBeGreaterThanOrEqual(0) expect(fallbackValue).toBeLessThan(1) } finally { vi.unstubAllGlobals() } }) test("getRandomIndex returns an integer index within alphabet bounds across available random sources", () => { const cryptoBackedValue = getRandomIndex(3) expect(Number.isInteger(cryptoBackedValue)).toBe(true) expect(cryptoBackedValue).toBeGreaterThanOrEqual(0) expect(cryptoBackedValue).toBeLessThan(3) vi.stubGlobal("crypto", undefined) try { const fallbackValue = getRandomIndex(3) expect(Number.isInteger(fallbackValue)).toBe(true) expect(fallbackValue).toBeGreaterThanOrEqual(0) expect(fallbackValue).toBeLessThan(3) } finally { vi.unstubAllGlobals() } })