Files
suaveplan/packages/foundation/data/bytes/src/uint8/uint8.test.ts
2026-05-27 23:47:53 +01:00

239 lines
7.6 KiB
TypeScript

import { describe, expect, it } from "@suaveplan/testing/runner";
import fc from "fast-check";
import {
concatUint8Arrays,
convertDataFormat,
detectInputFormat,
formatOutput,
inputToUint8Array,
sliceUint8Array,
uint8ArraysEqual,
validateInputTypeFormat,
} from "./uint8.js";
describe("concatUint8Arrays", () => {
it("concatenates multiple arrays", { timeout: 5000 }, () => {
const a = new Uint8Array([1, 2]);
const b = new Uint8Array([3, 4]);
const c = new Uint8Array([5]);
expect(concatUint8Arrays(a, b, c)).toEqual(
new Uint8Array([1, 2, 3, 4, 5])
);
});
it("handles empty arrays", { timeout: 5000 }, () => {
expect(
concatUint8Arrays(new Uint8Array(), new Uint8Array([1]))
).toEqual(new Uint8Array([1]));
});
it("handles single array", { timeout: 5000 }, () => {
const a = new Uint8Array([1, 2, 3]);
expect(concatUint8Arrays(a)).toEqual(a);
});
it("handles no arguments", { timeout: 5000 }, () => {
expect(concatUint8Arrays()).toEqual(new Uint8Array());
});
it("concatUint8Arrays associativity", { timeout: 30000 }, () => {
fc.assert(
fc.property(
fc.uint8Array(),
fc.uint8Array(),
fc.uint8Array(),
(a, b, c) => {
const left = concatUint8Arrays(concatUint8Arrays(a, b), c);
const right = concatUint8Arrays(a, concatUint8Arrays(b, c));
expect(left).toEqual(right);
}
)
);
});
});
describe("sliceUint8Array", () => {
it("slices correctly", { timeout: 5000 }, () => {
const arr = new Uint8Array([1, 2, 3, 4, 5]);
expect(sliceUint8Array(arr, 1, 3)).toEqual(new Uint8Array([2, 3]));
});
it("slices from start only", { timeout: 5000 }, () => {
const arr = new Uint8Array([1, 2, 3, 4, 5]);
expect(sliceUint8Array(arr, 2)).toEqual(new Uint8Array([3, 4, 5]));
});
it("returns empty for zero-length slice", { timeout: 5000 }, () => {
const arr = new Uint8Array([1, 2, 3]);
expect(sliceUint8Array(arr, 1, 1)).toEqual(new Uint8Array());
});
});
describe("uint8ArraysEqual", () => {
it("returns true for equal arrays", { timeout: 5000 }, () => {
expect(
uint8ArraysEqual(
new Uint8Array([1, 2, 3]),
new Uint8Array([1, 2, 3])
)
).toBe(true);
});
it("returns false for different arrays", { timeout: 5000 }, () => {
expect(
uint8ArraysEqual(
new Uint8Array([1, 2, 3]),
new Uint8Array([1, 2, 4])
)
).toBe(false);
});
it("returns false for different lengths", { timeout: 5000 }, () => {
expect(
uint8ArraysEqual(new Uint8Array([1, 2]), new Uint8Array([1, 2, 3]))
).toBe(false);
});
it("returns true for two empty arrays", { timeout: 5000 }, () => {
expect(uint8ArraysEqual(new Uint8Array(), new Uint8Array())).toBe(true);
});
});
describe("detectInputFormat", () => {
it("detects Uint8Array", { timeout: 5000 }, () => {
expect(detectInputFormat(new Uint8Array())).toBe("uint8array");
});
it("detects hex string", { timeout: 5000 }, () => {
expect(detectInputFormat("48656c6c6f")).toBe("hex");
});
it("detects utf8 for general string", { timeout: 5000 }, () => {
expect(detectInputFormat("Hello, world!")).toBe("utf8");
});
it("detects utf8 for non-hex even string", { timeout: 5000 }, () => {
expect(detectInputFormat("not-hex-string")).toBe("utf8");
});
});
describe("inputToUint8Array", () => {
it("returns Uint8Array as-is", { timeout: 5000 }, () => {
const arr = new Uint8Array([1, 2, 3]);
expect(inputToUint8Array(arr)).toBe(arr);
});
it("converts hex string to Uint8Array", { timeout: 5000 }, () => {
expect(inputToUint8Array("48656c6c6f", "hex")).toEqual(
new Uint8Array([72, 101, 108, 108, 111])
);
});
it("converts utf8 string to Uint8Array", { timeout: 5000 }, () => {
expect(inputToUint8Array("Hello", "utf8")).toEqual(
new Uint8Array([72, 101, 108, 108, 111])
);
});
it("converts base64 string to Uint8Array", { timeout: 5000 }, () => {
expect(inputToUint8Array("SGVsbG8=", "base64")).toEqual(
new Uint8Array([72, 101, 108, 108, 111])
);
});
it("auto-detects format when not provided", { timeout: 5000 }, () => {
// "48656c6c6f" is valid hex
const result = inputToUint8Array("48656c6c6f");
expect(result).toEqual(new Uint8Array([72, 101, 108, 108, 111]));
});
it("throws when string given with uint8array format", {
timeout: 5000,
}, () => {
expect(() => inputToUint8Array("hello", "uint8array")).toThrow();
});
});
describe("validateInputTypeFormat", () => {
it("validates Uint8Array with uint8array format", { timeout: 5000 }, () => {
expect(() =>
validateInputTypeFormat(new Uint8Array(), "uint8array")
).not.toThrow();
});
it("validates string with hex format", { timeout: 5000 }, () => {
expect(() => validateInputTypeFormat("ff", "hex")).not.toThrow();
});
it("throws when Uint8Array given but string format requested", {
timeout: 5000,
}, () => {
expect(() =>
validateInputTypeFormat(new Uint8Array(), "hex")
).toThrow();
});
it("throws when string given but uint8array format requested", {
timeout: 5000,
}, () => {
expect(() => validateInputTypeFormat("hello", "uint8array")).toThrow();
});
});
describe("formatOutput", () => {
it("returns Uint8Array for uint8array format", { timeout: 5000 }, () => {
const arr = new Uint8Array([1, 2, 3]);
const result = formatOutput(arr, "uint8array");
expect(result).toBe(arr);
});
it("returns hex string for hex format", { timeout: 5000 }, () => {
expect(formatOutput(new Uint8Array([255]), "hex")).toBe("ff");
});
it("returns base64 string for base64 format", { timeout: 5000 }, () => {
expect(
formatOutput(new Uint8Array([72, 101, 108, 108, 111]), "base64")
).toBe("SGVsbG8=");
});
it("returns utf8 string for utf8 format", { timeout: 5000 }, () => {
expect(
formatOutput(new Uint8Array([72, 101, 108, 108, 111]), "utf8")
).toBe("Hello");
});
});
describe("convertDataFormat", () => {
it("converts hex to utf8", { timeout: 5000 }, () => {
expect(convertDataFormat("48656c6c6f", "utf8", "hex")).toBe("Hello");
});
it("converts utf8 to hex", { timeout: 5000 }, () => {
expect(convertDataFormat("Hello", "hex", "utf8")).toBe("48656c6c6f");
});
it("converts Uint8Array to hex", { timeout: 5000 }, () => {
expect(
convertDataFormat(new Uint8Array([72, 101, 108, 108, 111]), "hex")
).toBe("48656c6c6f");
});
it("converts hex to base64", { timeout: 5000 }, () => {
expect(convertDataFormat("48656c6c6f", "base64", "hex")).toBe(
"SGVsbG8="
);
});
it("converts Uint8Array to uint8array (identity)", {
timeout: 5000,
}, () => {
const arr = new Uint8Array([1, 2, 3]);
expect(convertDataFormat(arr, "uint8array")).toBe(arr);
});
it("auto-detects input format for hex strings", { timeout: 5000 }, () => {
expect(convertDataFormat("48656c6c6f", "utf8")).toBe("Hello");
});
});