From ed828aec62a161d6add5c673bb382df842f281d9 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 26 Feb 2026 17:25:21 +0000 Subject: [PATCH] test: add unit tests for pointerArrayToKey utility Introduces comprehensive unit tests for `pointerArrayToKey` in `tests/utils/pointer.test.mjs`. The tests cover: - Invalid inputs (non-arrays, short arrays) - Type validation ('a' and 'e' support) - Value normalization (trimming and lowercasing) - Optional relay handling (trimming and type checking) - Error conditions (empty values, non-string inputs) These tests use the native Node.js test runner and follow existing project patterns. Co-authored-by: PR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> --- tests/utils/pointer.test.mjs | 59 ++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 tests/utils/pointer.test.mjs diff --git a/tests/utils/pointer.test.mjs b/tests/utils/pointer.test.mjs new file mode 100644 index 00000000..46bd72b9 --- /dev/null +++ b/tests/utils/pointer.test.mjs @@ -0,0 +1,59 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { pointerArrayToKey } from "../../js/utils/pointer.js"; + +test("pointerArrayToKey: returns empty string for non-array input", () => { + assert.equal(pointerArrayToKey(null), ""); + assert.equal(pointerArrayToKey(undefined), ""); + assert.equal(pointerArrayToKey("not an array"), ""); + assert.equal(pointerArrayToKey(123), ""); + assert.equal(pointerArrayToKey({}), ""); +}); + +test("pointerArrayToKey: returns empty string for array with length < 2", () => { + assert.equal(pointerArrayToKey([]), ""); + assert.equal(pointerArrayToKey(["a"]), ""); +}); + +test("pointerArrayToKey: returns empty string if type is not 'a' or 'e'", () => { + assert.equal(pointerArrayToKey(["x", "value"]), ""); + assert.equal(pointerArrayToKey(["", "value"]), ""); + assert.equal(pointerArrayToKey([1, "value"]), ""); +}); + +test("pointerArrayToKey: returns empty string if value is not a string", () => { + assert.equal(pointerArrayToKey(["a", null]), ""); + assert.equal(pointerArrayToKey(["a", 123]), ""); + assert.equal(pointerArrayToKey(["a", {}]), ""); +}); + +test("pointerArrayToKey: returns empty string if value is empty or whitespace", () => { + assert.equal(pointerArrayToKey(["a", ""]), ""); + assert.equal(pointerArrayToKey(["a", " "]), ""); +}); + +test("pointerArrayToKey: returns 'type:value' for valid 'a' type without relay", () => { + assert.equal(pointerArrayToKey(["a", "my-id"]), "a:my-id"); +}); + +test("pointerArrayToKey: returns 'type:value' for valid 'e' type without relay", () => { + assert.equal(pointerArrayToKey(["e", "event-id"]), "e:event-id"); +}); + +test("pointerArrayToKey: trims and lowercases value", () => { + assert.equal(pointerArrayToKey(["a", " My-ID "]), "a:my-id"); +}); + +test("pointerArrayToKey: returns 'type:value:relay' when relay is provided", () => { + assert.equal(pointerArrayToKey(["a", "id", "wss://relay.example.com"]), "a:id:wss://relay.example.com"); +}); + +test("pointerArrayToKey: trims relay", () => { + assert.equal(pointerArrayToKey(["a", "id", " wss://relay.example.com "]), "a:id:wss://relay.example.com"); +}); + +test("pointerArrayToKey: ignores relay if it is not a string", () => { + assert.equal(pointerArrayToKey(["a", "id", null]), "a:id"); + assert.equal(pointerArrayToKey(["a", "id", 123]), "a:id"); + assert.equal(pointerArrayToKey(["a", "id", {}]), "a:id"); +});