From 30fbb8d81a64fc1dd8edc03470bb316149a09e83 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:36:43 +0000 Subject: [PATCH] Refactor RevertModal to use shared nip71.getDTagValueFromTags Replaced the duplicated `extractDTagValue` method in `RevertModal.js` with the standard `getDTagValueFromTags` utility from `js/nostr/nip71.js`. Verified by adding a new unit test suite `tests/nostr/nip71-d-tag.test.mjs` ensuring the utility covers all edge cases. This improves code health by reducing duplication and centralizing NIP-71 logic. Co-authored-by: PR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> --- js/ui/components/RevertModal.js | 21 ++--------- tests/nostr/nip71-d-tag.test.mjs | 60 ++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 18 deletions(-) create mode 100644 tests/nostr/nip71-d-tag.test.mjs diff --git a/js/ui/components/RevertModal.js b/js/ui/components/RevertModal.js index 3e2d4ab0..13d19ac7 100644 --- a/js/ui/components/RevertModal.js +++ b/js/ui/components/RevertModal.js @@ -1,3 +1,4 @@ +import { getDTagValueFromTags } from "../../nostr/nip71.js"; import { Nip71FormManager } from "./nip71FormManager.js"; import { createModalAccessibility } from "./modalAccessibility.js"; import { @@ -354,7 +355,7 @@ export class RevertModal { if (this.subtitle) { const subtitleParts = []; - const dTagValue = this.extractDTagValue(video.tags); + const dTagValue = getDTagValueFromTags(video.tags); if (dTagValue) { subtitleParts.push(`d=${dTagValue}`); } @@ -604,7 +605,7 @@ export class RevertModal { typeof version.rawMagnet === "string" ? version.rawMagnet.trim() : ""; const displayMagnet = magnet || rawMagnet; const isPrivate = version.isPrivate === true; - const dTagValue = this.extractDTagValue(version.tags); + const dTagValue = getDTagValueFromTags(version.tags); const nip71Metadata = this.buildNip71DisplayMetadata(version); @@ -1488,21 +1489,5 @@ export class RevertModal { references, }; } - - - extractDTagValue(tags) { - if (!Array.isArray(tags)) { - return ""; - } - for (const tag of tags) { - if (!Array.isArray(tag) || tag.length < 2) { - continue; - } - if (tag[0] === "d" && typeof tag[1] === "string") { - return tag[1]; - } - } - return ""; - } } diff --git a/tests/nostr/nip71-d-tag.test.mjs b/tests/nostr/nip71-d-tag.test.mjs new file mode 100644 index 00000000..08ee87a9 --- /dev/null +++ b/tests/nostr/nip71-d-tag.test.mjs @@ -0,0 +1,60 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { getDTagValueFromTags } from "../../js/nostr/nip71.js"; + +test("getDTagValueFromTags handles invalid inputs", (t) => { + assert.equal(getDTagValueFromTags(null), ""); + assert.equal(getDTagValueFromTags(undefined), ""); + assert.equal(getDTagValueFromTags("not an array"), ""); + assert.equal(getDTagValueFromTags({}), ""); +}); + +test("getDTagValueFromTags handles empty tags array", (t) => { + assert.equal(getDTagValueFromTags([]), ""); +}); + +test("getDTagValueFromTags returns empty string if no d-tag present", (t) => { + const tags = [ + ["e", "eventId"], + ["p", "pubkey"], + ["t", "hashtag"], + ]; + assert.equal(getDTagValueFromTags(tags), ""); +}); + +test("getDTagValueFromTags returns correct value for single d-tag", (t) => { + const tags = [ + ["e", "eventId"], + ["d", "my-identifier"], + ["p", "pubkey"], + ]; + assert.equal(getDTagValueFromTags(tags), "my-identifier"); +}); + +test("getDTagValueFromTags returns first value for multiple d-tags", (t) => { + const tags = [ + ["d", "first-identifier"], + ["d", "second-identifier"], + ]; + assert.equal(getDTagValueFromTags(tags), "first-identifier"); +}); + +test("getDTagValueFromTags skips malformed d-tags", (t) => { + const tags = [ + ["d"], // too short + ["d", null], // invalid value type + ["d", 123], // invalid value type + "not an array tag", + ["d", "valid-identifier"], + ]; + assert.equal(getDTagValueFromTags(tags), "valid-identifier"); +}); + +test("getDTagValueFromTags skips empty string d-tag value", (t) => { + // nip71 implementation: if (typeof tag[1] === "string" && tag[1]) { ... } + const tags = [ + ["d", ""], + ["d", "next-valid"], + ]; + assert.equal(getDTagValueFromTags(tags), "next-valid"); +});