From 4eabefffaa0023cce02a9366bb8326d310c0efcc Mon Sep 17 00:00:00 2001 From: Effect Bot Date: Sat, 7 Nov 2026 08:15:11 +0000 Subject: [PATCH] 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. --- packages/effect/test/encoding/Base64.test.ts | 3 +- .../effect/test/encoding/Base64Url.test.ts | 5 ++-- packages/effect/test/encoding/Hex.test.ts | 10 +++++-- packages/effect/test/utils/encoding.ts | 28 +++++++++++++++++++ 4 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 packages/effect/test/utils/encoding.ts diff --git a/packages/effect/test/encoding/Base64.test.ts b/packages/effect/test/encoding/Base64.test.ts index 3a7f2890ed..4f65fd5fb7 100644 --- a/packages/effect/test/encoding/Base64.test.ts +++ b/packages/effect/test/encoding/Base64.test.ts @@ -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"] diff --git a/packages/effect/test/encoding/Base64Url.test.ts b/packages/effect/test/encoding/Base64Url.test.ts index 1c501aaa66..2f0fc4b248 100644 --- a/packages/effect/test/encoding/Base64Url.test.ts +++ b/packages/effect/test/encoding/Base64Url.test.ts @@ -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") diff --git a/packages/effect/test/encoding/Hex.test.ts b/packages/effect/test/encoding/Hex.test.ts index 87de18cd60..c092c6016d 100644 --- a/packages/effect/test/encoding/Hex.test.ts +++ b/packages/effect/test/encoding/Hex.test.ts @@ -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> = [ + 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 diff --git a/packages/effect/test/utils/encoding.ts b/packages/effect/test/utils/encoding.ts new file mode 100644 index 0000000000..a8f8b81ba2 --- /dev/null +++ b/packages/effect/test/utils/encoding.ts @@ -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() + }) +}