From 01f9f6101037dc1c5bc95e8dda94303d695e9837 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 8 Feb 2026 04:20:48 +0000 Subject: [PATCH] test: add unit tests for hex utils - Added `tests/utils/hex.test.mjs` - Covered `normalizeHexString` with various input types - Covered `HEX64_REGEX` with valid and invalid cases - Verified aliases `normalizeHexId` and `normalizeHexPubkey` Co-authored-by: PR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> --- tests/utils/hex.test.mjs | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/utils/hex.test.mjs 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); + }); + }); +});