import { describe, expect, it, } from "vitest"; import { bytesToHex, bytesToString, compareBytes, concatBytes, copyBytes, equalBytes, hexToBytes, isEmpty, sliceBytes, stringToBytes, } from "./bytes.js"; describe("byte utilities", () => { describe("compareBytes", () => { it("should return 0 for equal arrays", () => { const a = new Uint8Array([ 1, 2, 3, ]); const b = new Uint8Array([ 1, 2, 3, ]); expect(compareBytes(a, b)).toBe(0); }); it("should return negative for lexicographically smaller array", () => { const a = new Uint8Array([ 1, 2, 3, ]); const b = new Uint8Array([ 1, 2, 4, ]); expect(compareBytes(a, b)).toBeLessThan(0); }); it("should return positive for lexicographically larger array", () => { const a = new Uint8Array([ 1, 2, 4, ]); const b = new Uint8Array([ 1, 2, 3, ]); expect(compareBytes(a, b)).toBeGreaterThan(0); }); it("should handle arrays of different lengths", () => { const a = new Uint8Array([1, 2]); const b = new Uint8Array([ 1, 2, 3, ]); expect(compareBytes(a, b)).toBeLessThan(0); expect(compareBytes(b, a)).toBeGreaterThan(0); }); it("should handle empty arrays", () => { const a = new Uint8Array([]); const b = new Uint8Array([1]); expect(compareBytes(a, b)).toBeLessThan(0); expect(compareBytes(a, a)).toBe(0); }); }); describe("equalBytes", () => { it("should return true for equal arrays", () => { const a = new Uint8Array([ 1, 2, 3, ]); const b = new Uint8Array([ 1, 2, 3, ]); expect(equalBytes(a, b)).toBe(true); }); it("should return false for different arrays", () => { const a = new Uint8Array([ 1, 2, 3, ]); const b = new Uint8Array([ 1, 2, 4, ]); expect(equalBytes(a, b)).toBe(false); }); it("should return false for arrays of different lengths", () => { const a = new Uint8Array([1, 2]); const b = new Uint8Array([ 1, 2, 3, ]); expect(equalBytes(a, b)).toBe(false); }); it("should return true for empty arrays", () => { const a = new Uint8Array([]); const b = new Uint8Array([]); expect(equalBytes(a, b)).toBe(true); }); }); describe("concatBytes", () => { it("should concatenate multiple arrays", () => { const a = new Uint8Array([1, 2]); const b = new Uint8Array([3, 4]); const c = new Uint8Array([5, 6]); const result = concatBytes(a, b, c); expect(result).toEqual(new Uint8Array([ 1, 2, 3, 4, 5, 6, ])); }); it("should handle empty arrays", () => { const a = new Uint8Array([1, 2]); const b = new Uint8Array([]); const c = new Uint8Array([3, 4]); const result = concatBytes(a, b, c); expect(result).toEqual(new Uint8Array([ 1, 2, 3, 4, ])); }); it("should handle single array", () => { const a = new Uint8Array([ 1, 2, 3, ]); const result = concatBytes(a); expect(result).toEqual(a); expect(result).not.toBe(a); // Should be a copy }); it("should handle no arrays", () => { const result = concatBytes(); expect(result).toEqual(new Uint8Array([])); }); }); describe("copyBytes", () => { it("should create a copy of the array", () => { const original = new Uint8Array([ 1, 2, 3, ]); const copy = copyBytes(original); expect(copy).toEqual(original); expect(copy).not.toBe(original); // Different object }); it("should not affect original when copy is modified", () => { const original = new Uint8Array([ 1, 2, 3, ]); const copy = copyBytes(original); copy[0] = 99; expect(original[0]).toBe(1); expect(copy[0]).toBe(99); }); }); describe("stringToBytes / bytesToString", () => { it("should convert string to bytes and back", () => { const str = "Hello, World!"; const bytes = stringToBytes(str); const result = bytesToString(bytes); expect(result).toBe(str); }); it("should handle UTF-8 characters", () => { const str = "Hello, δΈ–η•Œ! 🌍"; const bytes = stringToBytes(str); const result = bytesToString(bytes); expect(result).toBe(str); }); it("should handle empty string", () => { const str = ""; const bytes = stringToBytes(str); expect(bytes.length).toBe(0); expect(bytesToString(bytes)).toBe(str); }); }); describe("bytesToHex / hexToBytes", () => { it("should convert bytes to hex and back", () => { const bytes = new Uint8Array([ 0, 15, 16, 255, ]); const hex = bytesToHex(bytes); expect(hex).toBe("000f10ff"); const result = hexToBytes(hex); expect(result).toEqual(bytes); }); it("should handle hex with 0x prefix", () => { const hex = "0x0a0b0c"; const bytes = hexToBytes(hex); expect(bytes).toEqual(new Uint8Array([ 10, 11, 12, ])); }); it("should handle uppercase hex", () => { const hex = "0A0B0C"; const bytes = hexToBytes(hex); expect(bytes).toEqual(new Uint8Array([ 10, 11, 12, ])); }); it("should handle empty bytes", () => { const bytes = new Uint8Array([]); const hex = bytesToHex(bytes); expect(hex).toBe(""); expect(hexToBytes(hex)).toEqual(bytes); }); it("should throw on invalid hex string (odd length)", () => { expect(() => hexToBytes("abc")).toThrow("even length"); }); it("should throw on invalid hex characters", () => { expect(() => hexToBytes("0g0h")).toThrow("invalid hex"); }); }); describe("isEmpty", () => { it("should return true for empty array", () => { expect(isEmpty(new Uint8Array([]))).toBe(true); }); it("should return false for non-empty array", () => { expect(isEmpty(new Uint8Array([1]))).toBe(false); expect(isEmpty(new Uint8Array([0]))).toBe(false); }); }); describe("sliceBytes", () => { it("should slice array from start to end", () => { const bytes = new Uint8Array([ 1, 2, 3, 4, 5, ]); const slice = sliceBytes(bytes, 1, 4); expect(slice).toEqual(new Uint8Array([ 2, 3, 4, ])); }); it("should slice from start to end of array", () => { const bytes = new Uint8Array([ 1, 2, 3, 4, 5, ]); const slice = sliceBytes(bytes, 2); expect(slice).toEqual(new Uint8Array([ 3, 4, 5, ])); }); it("should create a copy, not a view", () => { const bytes = new Uint8Array([ 1, 2, 3, 4, 5, ]); const slice = sliceBytes(bytes, 1, 4); slice[0] = 99; expect(bytes[1]).toBe(2); // Original unchanged }); it("should handle empty slice", () => { const bytes = new Uint8Array([ 1, 2, 3, ]); const slice = sliceBytes(bytes, 1, 1); expect(slice).toEqual(new Uint8Array([])); }); }); });