diff --git a/tests/utils/hex.test.mjs b/tests/utils/hex.test.mjs new file mode 100644 index 00000000..0d6fe98f --- /dev/null +++ b/tests/utils/hex.test.mjs @@ -0,0 +1,63 @@ +import { describe, it } from "node:test"; +import assert from "node:assert/strict"; +import { + normalizeHexString, + normalizeHexId, + normalizeHexPubkey, + HEX64_REGEX, +} from "../../js/utils/hex.js"; + +describe("hex utils", () => { + describe("normalizeHexString", () => { + it("should return empty string for non-string inputs", () => { + assert.equal(normalizeHexString(undefined), ""); + assert.equal(normalizeHexString(null), ""); + assert.equal(normalizeHexString(123), ""); + assert.equal(normalizeHexString({}), ""); + }); + + it("should return empty string for empty or whitespace-only strings", () => { + assert.equal(normalizeHexString(""), ""); + assert.equal(normalizeHexString(" "), ""); + }); + + it("should trim and lowercase valid hex strings", () => { + assert.equal(normalizeHexString("ABC"), "abc"); + assert.equal(normalizeHexString(" DEF "), "def"); + assert.equal(normalizeHexString("AbCdEf"), "abcdef"); + }); + }); + + describe("aliases", () => { + it("should export normalizeHexId as an alias", () => { + assert.equal(normalizeHexId, normalizeHexString); + }); + + it("should export normalizeHexPubkey as an alias", () => { + assert.equal(normalizeHexPubkey, normalizeHexString); + }); + }); + + describe("HEX64_REGEX", () => { + it("should match valid 64-character hex strings", () => { + const validHex = "a".repeat(64); + assert.match(validHex, HEX64_REGEX); + const mixedCase = "A".repeat(32) + "b".repeat(32); + assert.match(mixedCase, HEX64_REGEX); + }); + + it("should not match strings with incorrect length", () => { + assert.doesNotMatch("a".repeat(63), HEX64_REGEX); + assert.doesNotMatch("a".repeat(65), HEX64_REGEX); + }); + + it("should not match strings with non-hex characters", () => { + const invalidChar = "a".repeat(63) + "g"; + assert.doesNotMatch(invalidChar, HEX64_REGEX); + }); + + it("should not match empty strings", () => { + assert.doesNotMatch("", HEX64_REGEX); + }); + }); +});