Initial Commit
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import { describe, expect, it } from "@suaveplan/testing/runner";
|
||||
import { LicenseError } from "../../error-codes.js";
|
||||
import { base32Decode, base32Encode } from "./base32.js";
|
||||
|
||||
describe("base32Encode / base32Decode", () => {
|
||||
it("round-trips arbitrary bytes", { timeout: 5000 }, () => {
|
||||
const input = new Uint8Array([0, 1, 2, 64, 128, 200, 255, 42, 17]);
|
||||
const encoded = base32Encode(input);
|
||||
expect(encoded).toContain("-");
|
||||
const decoded = base32Decode(encoded);
|
||||
expect(Array.from(decoded)).toEqual(Array.from(input));
|
||||
});
|
||||
|
||||
it("encodes a single byte to two Base32 chars", { timeout: 5000 }, () => {
|
||||
const encoded = base32Encode(new Uint8Array([0xff]));
|
||||
// Two 5-bit groups: 11111 11100 ⇒ alphabet[31] + alphabet[28]
|
||||
expect(encoded.replace(/-/g, "")).toBe("ZW");
|
||||
});
|
||||
|
||||
it("ignores dashes during decode", { timeout: 5000 }, () => {
|
||||
const bytes = new Uint8Array([1, 2, 3, 4, 5]);
|
||||
const encoded = base32Encode(bytes);
|
||||
const decodedNoDashes = base32Decode(encoded.replace(/-/g, ""));
|
||||
const decodedWithDashes = base32Decode(encoded);
|
||||
expect(Array.from(decodedNoDashes)).toEqual(
|
||||
Array.from(decodedWithDashes)
|
||||
);
|
||||
});
|
||||
|
||||
it("is case-insensitive during decode", { timeout: 5000 }, () => {
|
||||
const bytes = new Uint8Array([0xab, 0xcd, 0xef]);
|
||||
const encoded = base32Encode(bytes);
|
||||
const lower = base32Decode(encoded.toLowerCase());
|
||||
const upper = base32Decode(encoded.toUpperCase());
|
||||
expect(Array.from(lower)).toEqual(Array.from(upper));
|
||||
});
|
||||
|
||||
it("throws LICENSE_INVALID_ENCODING on non-alphabet character", {
|
||||
timeout: 5000,
|
||||
}, () => {
|
||||
let caught: unknown;
|
||||
try {
|
||||
base32Decode("XXXXX!");
|
||||
} catch (e) {
|
||||
caught = e;
|
||||
}
|
||||
expect(caught).toBeInstanceOf(LicenseError);
|
||||
expect((caught as LicenseError).code).toBe("LICENSE.INVALID_ENCODING");
|
||||
});
|
||||
|
||||
it("round-trips every byte value 0..255", { timeout: 5000 }, () => {
|
||||
const all = new Uint8Array(256);
|
||||
for (let i = 0; i < 256; i++) all[i] = i;
|
||||
const decoded = base32Decode(base32Encode(all));
|
||||
expect(Array.from(decoded)).toEqual(Array.from(all));
|
||||
});
|
||||
|
||||
it("encodes an empty buffer to an empty string", { timeout: 5000 }, () => {
|
||||
expect(base32Encode(new Uint8Array(0))).toBe("");
|
||||
expect(base32Decode("")).toEqual(new Uint8Array(0));
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user