Refactor RevertModal to use shared d-tag extraction utility

- Removed duplicated `extractDTagValue` method from `js/ui/components/RevertModal.js`
- Imported and used `getDTagValueFromTags` from `js/nostr/nip71.js`
- Added unit tests for `getDTagValueFromTags` in `tests/nostr/nip71-d-tag.test.mjs`

This reduces code duplication and ensures consistent d-tag extraction logic.

Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
google-labs-jules[bot]andPR0M3TH3AN committed 2026-02-26 17:37:26 +00:00
1 parent 9913e484ab
commit 931f57eebe
2 files changed
+50 -18

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { getDTagValueFromTags } from '../../js/nostr/nip71.js';
test('getDTagValueFromTags utility', async (t) => {
await t.test('returns empty string for invalid inputs', () => {
assert.equal(getDTagValueFromTags(null), '');
assert.equal(getDTagValueFromTags(undefined), '');
assert.equal(getDTagValueFromTags({}), '');
assert.equal(getDTagValueFromTags(''), '');
assert.equal(getDTagValueFromTags(123), '');
});
await t.test('returns empty string if no d-tag present', () => {
const tags = [['p', 'pubkey'], ['e', 'event-id']];
assert.equal(getDTagValueFromTags(tags), '');
});
await t.test('returns correct value for single d-tag', () => {
const tags = [['d', 'test-value']];
assert.equal(getDTagValueFromTags(tags), 'test-value');
});
await t.test('returns first value for multiple d-tags', () => {
const tags = [['d', 'first'], ['d', 'second']];
assert.equal(getDTagValueFromTags(tags), 'first');
});
await t.test('skips invalid d-tags (malformed)', () => {
const tags = [
['d'], // missing value
['d', 123], // value not string
['d', null], // value not string
'invalid', // tag not array
['d', 'valid'],
];
assert.equal(getDTagValueFromTags(tags), 'valid');
});
await t.test('handles empty d-tag value correctly', () => {
// nip71 implementation returns empty string if value is empty string, OR skips it?
// Implementation: if (typeof tag[1] === "string" && tag[1]) { return tag[1]; }
// So it skips empty strings.
const tags = [['d', ''], ['d', 'fallback']];
assert.equal(getDTagValueFromTags(tags), 'fallback');
});
});