sdk: more PrepareNote() tests.

This commit is contained in:
fiatjaf
2025-05-06 00:03:47 -03:00
parent b84d99ac06
commit 05bf7b9c4f
3 changed files with 70 additions and 16 deletions

View File

@@ -99,9 +99,8 @@ func TestStreamLiveFeed(t *testing.T) {
relay.Close() relay.Close()
// create a new system instance pointing only to relay1 as the "indexer" // create a new system instance pointing only to relay1 as the "indexer"
sys := NewSystem(WithRelayListRelays([]string{ sys := NewSystem()
"ws://localhost:48481", sys.RelayListRelays = NewRelayStream("ws://localhost:48481")
}))
defer sys.Close() defer sys.Close()
// prepublish some events // prepublish some events

View File

@@ -13,9 +13,9 @@ import (
) )
var ( var (
domainRe = regexp.MustCompile(`(?:https?:\/\/)?([a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*\.(?:[a-zA-Z]{2,})`) domainRe = regexp.MustCompile(`\b(?:https?:\/\/)?([a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*\.(?:[a-zA-Z]{2,}))`)
nostrRe = regexp.MustCompile(`(?:nostr:)?(npub|nprofile|naddr|nevent|note)1([a-zA-Z0-9]+`) nostrRe = regexp.MustCompile(`\b(?:nostr:)?(npub|nprofile|naddr|nevent|note)1([a-zA-Z0-9]+)`)
tagRe = regexp.MustCompile(`\b#+\w+`) tagRe = regexp.MustCompile(`^#+\w+\b|\s#+\w+\b`)
) )
// PrepareNoteEvent takes an event with content that may include any number of URLs, partial URLs without the scheme, npub references with or without the "nostr:" prefix, tags and whatnot, then edits the content so it contains properly formatted URLs and references and adds the required tags based on these. // PrepareNoteEvent takes an event with content that may include any number of URLs, partial URLs without the scheme, npub references with or without the "nostr:" prefix, tags and whatnot, then edits the content so it contains properly formatted URLs and references and adds the required tags based on these.
@@ -82,12 +82,17 @@ func (sys *System) PrepareNoteEvent(ctx context.Context, evt *nostr.Event) (targ
tag = append(tag, relay) // shove this relay hint here tag = append(tag, relay) // shove this relay hint here
} }
} else { } else {
evt.Tags = append(evt.Tags, append(b.AsTag(), relay)) b.Relays = append(b.Relays, relay)
tag = b.AsTag()
tag[0] = "q"
evt.Tags = append(evt.Tags, tag)
} }
case nostr.EntityPointer: case nostr.EntityPointer:
pk = b.PublicKey pk = b.PublicKey
if tag := evt.Tags.FindWithValue("q", b.AsTagReference()); tag == nil { if tag := evt.Tags.FindWithValue("q", b.AsTagReference()); tag == nil {
evt.Tags = append(evt.Tags, b.AsTag()) tag := b.AsTag()
tag[0] = "q"
evt.Tags = append(evt.Tags, tag)
} }
} }

View File

@@ -1,6 +1,7 @@
package sdk package sdk
import ( import (
"context"
"testing" "testing"
"fiatjaf.com/nostr" "fiatjaf.com/nostr"
@@ -8,33 +9,82 @@ import (
) )
func TestPrepareNoteEvent(t *testing.T) { func TestPrepareNoteEvent(t *testing.T) {
// prepare a dummy system, it's needed for context but won't be used in these specific tests
sys := NewSystem()
defer sys.Close()
ctx := context.Background()
tests := []struct { tests := []struct {
name string name string
content string content string
wantTags nostr.Tags tags nostr.Tags // initial tags
want string wantTags nostr.Tags // expected tags after processing
want string // expected content after processing
}{ }{
{ {
name: "plain text", name: "plain text",
content: "hello world", content: "hello world",
tags: nostr.Tags{},
wantTags: nostr.Tags{}, wantTags: nostr.Tags{},
want: "hello world", want: "hello world",
}, },
{ {
name: "with nostr: prefix, url and hashtag", name: "with nostr: prefix, url and hashtag",
content: "hello nostr:npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6 please visit https://banana.com/ and get your free #banana", content: "hello nostr:npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6 please visit https://banana.com/ and get your free #banana",
tags: nostr.Tags{},
wantTags: nostr.Tags{ wantTags: nostr.Tags{
{"p", "82341f882b6eabcd2ba7f1ef90aad961cf074af15b9ef44a09f9d2a8fbfbe6a2"}, {"p", "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"},
{"t", "banana"},
}, },
want: "hello nostr:npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6 please visit https://banana.com/ and get your free #banana", want: "hello nostr:npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6 please visit https://banana.com/ and get your free #banana",
}, },
{ {
name: "with bare npub and bare url", name: "with bare npub and bare url",
content: "hello npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6 please visit banana.com", content: "hello npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6 please visit banana.com",
tags: nostr.Tags{},
wantTags: nostr.Tags{ wantTags: nostr.Tags{
{"p", "82341f882b6eabcd2ba7f1ef90aad961cf074af15b9ef44a09f9d2a8fbfbe6a2"}, {"p", "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"},
}, },
want: "hello nostr:npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6 please visit banana.com", want: "hello nostr:npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6 please visit https://banana.com",
},
{
name: "content with a single hashtag",
content: "#this is a test with #nostr",
tags: nostr.Tags{},
wantTags: nostr.Tags{
{"t", "nostr"},
{"t", "this"},
},
want: "#this is a test with #nostr",
},
{
name: "content with multiple hashtags",
content: "testing #multiple #hashtags here",
tags: nostr.Tags{},
wantTags: nostr.Tags{
{"t", "multiple"},
{"t", "hashtags"},
},
want: "testing #multiple #hashtags here",
},
{
name: "content with existing t tag",
content: "when adding #tags don't add duplicate #tags to banana.social",
tags: nostr.Tags{{"t", "tags"}},
wantTags: nostr.Tags{
{"t", "tags"},
},
want: "when adding #tags don't add duplicate #tags to https://banana.social",
},
{
name: "content with mixed tags and hashtag",
content: "a valid nevent1qqsr0f9w78uyy09qwmjt0kv63j4l7sxahq33725lqyyp79whlfjurwspz4mhxue69uhh56nzv34hxcfwv9ehw6nyddhqygpm7rrrljungc6q0tuh5hj7ue863q73qlheu4vywtzwhx42a7j9n5x0aedk and an invalid nevent1aaa",
tags: nostr.Tags{{"t", "unrelated"}},
wantTags: nostr.Tags{
{"t", "unrelated"},
{"q", "37a4aef1f8423ca076e4b7d99a8cabff40ddb8231f2a9f01081f15d7fa65c1ba", "wss://zjbdksa.aswjdkn", "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"},
},
want: "a valid nostr:nevent1qqsr0f9w78uyy09qwmjt0kv63j4l7sxahq33725lqyyp79whlfjurwspz4mhxue69uhh56nzv34hxcfwv9ehw6nyddhqygpm7rrrljungc6q0tuh5hj7ue863q73qlheu4vywtzwhx42a7j9n5x0aedk and an invalid nevent1aaa",
}, },
} }
@@ -42,12 +92,12 @@ func TestPrepareNoteEvent(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
evt := &nostr.Event{ evt := &nostr.Event{
Content: tt.content, Content: tt.content,
Tags: nostr.Tags{}, Tags: tt.tags.Clone(),
} }
PrepareNoteEvent(evt) sys.PrepareNoteEvent(ctx, evt)
require.Equal(t, tt.want, evt.Content) require.Equal(t, tt.want, evt.Content)
require.Equal(t, tt.wantTags, evt.Tags) require.ElementsMatch(t, tt.wantTags, evt.Tags, "tags mismatch")
}) })
} }
} }