Merge pull request #2245 from PR0M3TH3AN/test/hex-utils-coverage-3841884006452707287

🧪 Add tests for hex utils
This commit is contained in:
thePR0M3TH3AN authored and GitHub committed 2026-02-08 10:55:13 -05:00
commit 6fcb18349b
1 file changed
+63
+63
View File
@@ -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);
});
});
});