Add tags test

This commit is contained in:
Alex Gleason 2024-05-21 13:32:27 -05:00
parent 9839b8138f
commit 5e607f664e
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 74 additions and 7 deletions

45
src/utils/tags.test.ts Normal file
View File

@ -0,0 +1,45 @@
import { assertEquals } from '@std/assert';
import { addTag, deleteTag, findQuoteTag, findReplyTag, getTagSet, hasTag } from './tags.ts';
Deno.test('addTag', () => {
const tags = [['p', 'alex']];
assertEquals(addTag(tags, ['p', 'alex']), [['p', 'alex']]);
assertEquals(addTag(tags, ['p', 'fiatjaf']), [['p', 'alex'], ['p', 'fiatjaf']]);
});
Deno.test('deleteTag', () => {
const tags = [['p', 'alex'], ['p', 'fiatjaf']];
assertEquals(deleteTag(tags, ['p', 'alex']), [['p', 'fiatjaf']]);
assertEquals(deleteTag(tags, ['p', 'fiatjaf']), [['p', 'alex']]);
});
Deno.test('findQuoteTag', () => {
assertEquals(findQuoteTag([['q', '123']]), ['q', '123']);
assertEquals(findQuoteTag([['e', '', '', 'mention', '456']]), ['e', '', '', 'mention', '456']);
assertEquals(findQuoteTag([['e', '', '', 'mention', '456'], ['q', '123']]), ['q', '123']);
assertEquals(findQuoteTag([['q', '123'], ['e', '', '', 'mention', '456']]), ['q', '123']);
});
Deno.test('findReplyTag', () => {
const root = ['e', '123', '', 'root'];
const reply = ['e', '456', '', 'reply'];
assertEquals(findReplyTag([root]), root);
assertEquals(findReplyTag([reply]), reply);
assertEquals(findReplyTag([root, reply]), reply);
assertEquals(findReplyTag([reply, root]), reply);
assertEquals(findReplyTag([['e', '321'], ['e', '789']]), ['e', '789']);
assertEquals(findReplyTag([reply, ['e', '789']]), reply);
});
Deno.test('getTagSet', () => {
const tags = [['p', 'alex'], ['p', 'fiatjaf'], ['p', 'alex']];
assertEquals(getTagSet(tags, 'p'), new Set(['alex', 'fiatjaf']));
});
Deno.test('hasTag', () => {
const tags = [['p', 'alex']];
assertEquals(hasTag(tags, ['p', 'alex']), true);
assertEquals(hasTag(tags, ['p', 'fiatjaf']), false);
});

View File

@ -31,18 +31,40 @@ function addTag(tags: readonly string[][], tag: string[]): string[][] {
} }
} }
const isReplyTag = (tag: string[]) => tag[0] === 'e' && tag[3] === 'reply'; /** Tag is a NIP-10 root tag. */
const isRootTag = (tag: string[]) => tag[0] === 'e' && tag[3] === 'root'; function isRootTag(tag: string[]): tag is ['e', string, string, 'root', ...string[]] {
const isLegacyReplyTag = (tag: string[]) => tag[0] === 'e' && !tag[3]; return tag[0] === 'e' && tag[3] === 'root';
}
const isQuoteTag = (tag: string[]) => tag[0] === 'q'; /** Tag is a NIP-10 reply tag. */
const isLegacyQuoteTag = (tag: string[]) => tag[0] === 'e' && tag[3] === 'mention'; function isReplyTag(tag: string[]): tag is ['e', string, string, 'reply', ...string[]] {
return tag[0] === 'e' && tag[3] === 'reply';
}
function findReplyTag(tags: string[][]): string[] | undefined { /** Tag is a legacy "e" tag with a "mention" marker. */
function isLegacyQuoteTag(tag: string[]): tag is ['e', string, string, 'mention', ...string[]] {
return tag[0] === 'e' && tag[3] === 'mention';
}
/** Tag is an "e" tag without a NIP-10 marker. */
function isLegacyReplyTag(tag: string[]): tag is ['e', string, string] {
return tag[0] === 'e' && !tag[3];
}
/** Tag is a "q" tag. */
function isQuoteTag(tag: string[]): tag is ['q', ...string[]] {
return tag[0] === 'q';
}
/** Get the "e" tag for the event being replied to, first according to the NIPs then falling back to the legacy way. */
function findReplyTag(tags: string[][]): ['e', ...string[]] | undefined {
return tags.find(isReplyTag) || tags.find(isRootTag) || tags.findLast(isLegacyReplyTag); return tags.find(isReplyTag) || tags.find(isRootTag) || tags.findLast(isLegacyReplyTag);
} }
function findQuoteTag(tags: string[][]): string[] | undefined { /** Get the "q" tag, falling back to the legacy "e" tag with a "mention" marker. */
function findQuoteTag(
tags: string[][],
): ['q', ...string[]] | ['e', string, string, 'mention', ...string[]] | undefined {
return tags.find(isQuoteTag) || tags.find(isLegacyQuoteTag); return tags.find(isQuoteTag) || tags.find(isLegacyQuoteTag);
} }