import { describe, expect, test } from "bun:test" import { deepHexlify } from "./index" describe("deepHexlify", () => { // Test primitive values test("should handle null and undefined", () => { expect(deepHexlify(null)).toBeNull() expect(deepHexlify(undefined)).toBeUndefined() expect(deepHexlify(() => {})).toBeUndefined() }) test("should return string as is", () => { expect(deepHexlify("test")).toBe("test") expect(deepHexlify("0x123")).toBe("0x123") expect(deepHexlify("")).toBe("") }) test("should return boolean as is", () => { expect(deepHexlify(true)).toBe(true) expect(deepHexlify(false)).toBe(false) }) test("should return number as is", () => { expect(deepHexlify(123)).toBe(123) expect(deepHexlify(0)).toBe(0) expect(deepHexlify(-123)).toBe(-123) expect(deepHexlify(1.23)).toBe(1.23) }) // Test functions test("should return undefined for functions", () => { expect(deepHexlify(() => {})).toBeUndefined() expect(deepHexlify(async () => {})).toBeUndefined() }) // Test BigInt test("should convert BigInt to hex", () => { expect(deepHexlify(BigInt(123))).toBe("0x7b") expect(deepHexlify(BigInt(0))).toBe("0x0") expect(deepHexlify(BigInt("0x123"))).toBe("0x123") }) // Test Arrays test("should handle arrays recursively", () => { const input = [123, "test", BigInt(456), [BigInt(789), "nested"], true] const expected = [123, "test", "0x1c8", ["0x315", "nested"], true] expect(deepHexlify(input)).toEqual(expected) }) test("should handle empty arrays", () => { expect(deepHexlify([])).toEqual([]) }) test("should handle arrays with null and undefined", () => { expect(deepHexlify([null, undefined, 123])).toEqual([ null, undefined, 123 ]) }) // Test Objects test("should handle plain objects recursively", () => { const input = { number: 123, string: "test", bigint: BigInt(456), nested: { value: BigInt(789), text: "nested" }, bool: true } const expected = { number: 123, string: "test", bigint: "0x1c8", nested: { value: "0x315", text: "nested" }, bool: true } expect(deepHexlify(input)).toEqual(expected) }) test("should handle empty objects", () => { expect(deepHexlify({})).toEqual({}) }) // Test Mixed Complex Objects test("should handle complex nested structures", () => { const input = { array: [123, BigInt(456), { value: BigInt(789) }], object: { bigint: BigInt(101112), nested: [BigInt(131415), { deep: BigInt(161718) }] }, primitives: { string: "test", number: 123, boolean: true, null: null, undefined: undefined } } const expected = { array: [123, "0x1c8", { value: "0x315" }], object: { bigint: "0x18af8", nested: ["0x20157", { deep: "0x277b6" }] }, primitives: { string: "test", number: 123, boolean: true, null: null, undefined: undefined } } expect(deepHexlify(input)).toEqual(expected) }) // Test Edge Cases test("should handle objects with non-enumerable properties", () => { const obj = Object.create( {}, { visible: { value: BigInt(123), enumerable: true }, invisible: { value: BigInt(456), enumerable: false } } ) expect(deepHexlify(obj)).toEqual({ visible: "0x7b" }) }) // test("should handle circular references", () => { // // biome-ignore lint/suspicious/noExplicitAny: it's a recursive function, so it's hard to type // const circular: any = { prop: BigInt(123) }; // circular.self = circular; // // biome-ignore lint/suspicious/noExplicitAny: it's a recursive function, so it's hard to type // const result: any = deepHexlify(circular); // expect(result.prop).toBe("0x7b"); // expect(result.self.prop).toBe("0x7b"); // }); // test("should handle special numeric values", () => { // const input = { // zero: BigInt(0), // leading_zero: BigInt(0x01), // large_number: BigInt("0x1234567890abcdef"), // negative: BigInt(-123) // }; // const result = deepHexlify(input); // expect(result.zero).toBe("0x0"); // expect(result.leading_zero).toBe("0x1"); // expect(result.large_number).toBe("0x1234567890abcdef"); // expect(result.negative).toBe("-0x7b"); // }); // Test non-object, non-BigNumber values // test("should handle other types correctly", () => { // const date = new Date(); // expect(deepHexlify(date)).toMatch(/^0x/); // Date converted to hex // const symbol = Symbol('test'); // expect(deepHexlify(symbol)).toMatch(/^0x/); // Symbol converted to hex // }); })