mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-10-27 07:52:24 +00:00
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 <[email protected]>
61 lines
1.8 KiB
JavaScript
61 lines
1.8 KiB
JavaScript
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");
|
|
});
|