test(encoding): run Base64, Base64Url and Hex suites without native Uint8Array methods

CI's Node, Bun and Deno runtimes all ship Uint8Array.fromBase64/toBase64/
fromHex/toHex, so the JS fallbacks used on Node 22/24 and older browsers
were never exercised. Each suite now also runs with those methods removed.

Also types the Hex failure vectors so pnpm check passes.
This commit is contained in:
Effect Bot
2026-11-07 08:15:11 +00:00
parent fc9bee986f
commit 4eabefffaa
4 changed files with 41 additions and 5 deletions
+2 -1
View File
@@ -2,8 +2,9 @@ import { assert, describe, it } from "@effect/vitest"
import { Result } from "effect"
import * as Base64 from "effect/encoding/Base64"
import type * as EncodingError from "effect/encoding/EncodingError"
import { describeWithFallback } from "../utils/encoding.ts"
describe("Base64", () => {
describeWithFallback("Base64", () => {
it("encodes and decodes binary subarrays with and without padding", () => {
const bytes = new Uint8Array([42, 251, 255, 128, 42])
const vectors = ["", "+w==", "+/8=", "+/+A"]
@@ -1,9 +1,10 @@
import { assert, describe, it } from "@effect/vitest"
import { assert, it } from "@effect/vitest"
import { Result } from "effect"
import * as Base64Url from "effect/encoding/Base64Url"
import type * as EncodingError from "effect/encoding/EncodingError"
import { describeWithFallback } from "../utils/encoding.ts"
describe("Base64Url", () => {
describeWithFallback("Base64Url", () => {
it("encodes strings and bytes without padding", () => {
assert.strictEqual(Base64Url.encode("hello?"), "aGVsbG8_")
assert.strictEqual(Base64Url.encode(new Uint8Array([251, 255])), "-_8")
+8 -2
View File
@@ -1,6 +1,8 @@
import { assert, describe, it } from "@effect/vitest"
import { Result } from "effect"
import type * as EncodingError from "effect/encoding/EncodingError"
import * as Hex from "effect/encoding/Hex"
import { describeWithFallback } from "../utils/encoding.ts"
const assertRandom = (length: number, expectedLength: number): void => {
const value = Hex.random(length)
@@ -8,7 +10,7 @@ const assertRandom = (length: number, expectedLength: number): void => {
assert.match(value, /^[0-9a-f]*$/)
}
describe("Hex", () => {
describeWithFallback("Hex", () => {
it("encodes strings and bytes as lowercase hexadecimal", () => {
assert.strictEqual(Hex.encode("hello"), "68656c6c6f")
assert.strictEqual(Hex.encode(new Uint8Array([72, 101, 108, 108, 111])), "48656c6c6f")
@@ -58,7 +60,11 @@ describe("Hex", () => {
[`${long}0g`, "Invalid input"]
]
for (const [input, message] of vectors) {
for (const result of [Hex.decode(input), Hex.decodeString(input)]) {
const results: ReadonlyArray<Result.Result<unknown, EncodingError.EncodingError>> = [
Hex.decode(input),
Hex.decodeString(input)
]
for (const result of results) {
assert.isTrue(Result.isFailure(result), input)
if (Result.isFailure(result)) {
const { kind, module } = result.failure
+28
View File
@@ -0,0 +1,28 @@
import { afterAll, beforeAll, describe } from "@effect/vitest"
// The native `Uint8Array` Base64 and hex methods the encoding modules may use.
const natives = [
[Uint8Array, "fromBase64"],
[Uint8Array, "fromHex"],
[Uint8Array.prototype, "toBase64"],
[Uint8Array.prototype, "toHex"]
] as const
// Registers `suite` twice: once as is, and once with the native methods
// removed, so the fallbacks are covered on runtimes that ship them.
export const describeWithFallback = (name: string, suite: () => void): void => {
describe(name, suite)
describe(`${name} without native methods`, () => {
const descriptors = natives.map(([target, key]) => Object.getOwnPropertyDescriptor(target, key))
beforeAll(() => {
for (const [target, key] of natives) Reflect.deleteProperty(target, key)
})
afterAll(() => {
natives.forEach(([target, key], i) => {
const descriptor = descriptors[i]
if (descriptor !== undefined) Reflect.defineProperty(target, key, descriptor)
})
})
suite()
})
}