diff --git a/count_test.go b/count_test.go index 6784e60..6f829d6 100644 --- a/count_test.go +++ b/count_test.go @@ -3,24 +3,19 @@ package nostr import ( "context" "testing" + + "github.com/stretchr/testify/assert" ) func TestCount(t *testing.T) { const RELAY = "wss://relay.nostr.band" - rl := mustRelayConnect(RELAY) + rl := mustRelayConnect(t, RELAY) defer rl.Close() count, err := rl.Count(context.Background(), Filters{ {Kinds: []int{KindContactList}, Tags: TagMap{"p": []string{"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"}}}, }) - if err != nil { - t.Errorf("count request failed: %v", err) - return - } - - if count <= 0 { - t.Errorf("count result wrong: %v", count) - return - } + assert.NoError(t, err) + assert.Greater(t, count, int64(0)) } diff --git a/envelopes_test.go b/envelopes_test.go index 0f01065..c800634 100644 --- a/envelopes_test.go +++ b/envelopes_test.go @@ -3,6 +3,8 @@ package nostr import ( "encoding/json" "testing" + + "github.com/stretchr/testify/assert" ) func TestEventEnvelopeEncodingAndDecoding(t *testing.T) { @@ -13,67 +15,53 @@ func TestEventEnvelopeEncodingAndDecoding(t *testing.T) { for _, raw := range eventEnvelopes { var env EventEnvelope - if err := json.Unmarshal([]byte(raw), &env); err != nil { - t.Errorf("failed to parse event envelope json: %v", err) - } - - if env.GetID() != env.ID { - t.Errorf("error serializing event id: %s != %s", env.GetID(), env.ID) - } - - if ok, _ := env.CheckSignature(); !ok { - t.Error("signature verification failed when it should have succeeded") - } - - asjson, err := json.Marshal(env) - if err != nil { - t.Errorf("failed to re marshal event as json: %v", err) - } - - if string(asjson) != raw { - t.Log(string(asjson)) - t.Error("json serialization broken") - } + err := json.Unmarshal([]byte(raw), &env) + assert.NoError(t, err) + assert.Equal(t, env.GetID(), env.ID) + + ok, _ := env.CheckSignature() + assert.True(t, ok) + + asJSON, err := json.Marshal(env) + assert.NoError(t, err) + assert.Equal(t, raw, string(asJSON)) } } func TestNoticeEnvelopeEncodingAndDecoding(t *testing.T) { - src := `["NOTICE","kjasbdlasvdluiasvd\"kjasbdksab\\d"]` + noticeEnv := `["NOTICE","kjasbdlasvdluiasvd\"kjasbdksab\\d"]` var env NoticeEnvelope - json.Unmarshal([]byte(src), &env) - if env != "kjasbdlasvdluiasvd\"kjasbdksab\\d" { - t.Error("failed to decode NOTICE") - } + err := json.Unmarshal([]byte(noticeEnv), &env) + assert.NoError(t, err) + assert.Equal(t, "kjasbdlasvdluiasvd\"kjasbdksab\\d", env) - if res, _ := json.Marshal(env); string(res) != src { - t.Errorf("failed to encode NOTICE: expected '%s', got '%s'", src, string(res)) - } + res, err := json.Marshal(env) + assert.NoError(t, err) + assert.Equal(t, noticeEnv, string(res)) } func TestEoseEnvelopeEncodingAndDecoding(t *testing.T) { - src := `["EOSE","kjasbdlasvdluiasvd\"kjasbdksab\\d"]` + eoseEnv := `["EOSE","kjasbdlasvdluiasvd\"kjasbdksab\\d"]` var env EOSEEnvelope - json.Unmarshal([]byte(src), &env) - if env != "kjasbdlasvdluiasvd\"kjasbdksab\\d" { - t.Error("failed to decode EOSE") - } + err := json.Unmarshal([]byte(eoseEnv), &env) + assert.NoError(t, err) + assert.Equal(t, "kjasbdlasvdluiasvd\"kjasbdksab\\d", env) - if res, _ := json.Marshal(env); string(res) != src { - t.Errorf("failed to encode EOSE: expected '%s', got '%s'", src, string(res)) - } + res, err := json.Marshal(env) + assert.NoError(t, err) + assert.Equal(t, eoseEnv, string(res)) } func TestCountEnvelopeEncodingAndDecoding(t *testing.T) { - src := `["COUNT","z",{"count":12}]` + countEnv := `["COUNT","z",{"count":12}]` var env CountEnvelope - json.Unmarshal([]byte(src), &env) - if *env.Count != 12 { - t.Error("failed to decode COUNT") - } + err := json.Unmarshal([]byte(countEnv), &env) + assert.NoError(t, err) + assert.Equal(t, 12, *env.Count) - if res, _ := json.Marshal(env); string(res) != src { - t.Errorf("failed to encode COUNT: expected '%s', got '%s'", src, string(res)) - } + res, err := json.Marshal(env) + assert.NoError(t, err) + assert.Equal(t, countEnv, string(res)) } func TestOKEnvelopeEncodingAndDecoding(t *testing.T) { @@ -84,35 +72,35 @@ func TestOKEnvelopeEncodingAndDecoding(t *testing.T) { for _, raw := range okEnvelopes { var env OKEnvelope - if err := json.Unmarshal([]byte(raw), &env); err != nil { - t.Errorf("failed to parse ok envelope json: %v", err) - } - - asjson, err := json.Marshal(env) - if err != nil { - t.Errorf("failed to re marshal ok as json: %v", err) - } - - if string(asjson) != raw { - t.Log(string(asjson)) - t.Error("json serialization broken") - } + err := json.Unmarshal([]byte(raw), &env) + assert.NoError(t, err) + + asJSON, err := json.Marshal(env) + assert.NoError(t, err) + assert.Equal(t, raw, string(asJSON)) } } func TestClosedEnvelopeEncodingAndDecoding(t *testing.T) { - for _, src := range []string{ + closeEnvelopes := []string{ `["CLOSED","_","error: something went wrong"]`, `["CLOSED",":1","auth-required: take a selfie and send it to the CIA"]`, - } { + } + + for _, raw := range closeEnvelopes { var env ClosedEnvelope - json.Unmarshal([]byte(src), &env) - if env.SubscriptionID != "_" && env.SubscriptionID != ":1" { - t.Error("failed to decode CLOSED") - } - if res, _ := json.Marshal(env); string(res) != src { - t.Errorf("failed to encode CLOSED: expected '%s', got '%s'", src, string(res)) - } + err := json.Unmarshal([]byte(raw), &env) + assert.NoError(t, err) + assert.Condition(t, func() (success bool) { + if env.SubscriptionID != "_" && env.SubscriptionID != ":1" { + return false + } + return true + }) + + res, err := json.Marshal(env) + assert.NoError(t, err) + assert.Equal(t, raw, string(res)) } } @@ -124,19 +112,12 @@ func TestAuthEnvelopeEncodingAndDecoding(t *testing.T) { for _, raw := range authEnvelopes { var env AuthEnvelope - if err := json.Unmarshal([]byte(raw), &env); err != nil { - t.Errorf("failed to parse auth envelope json: %v", err) - } - - asjson, err := json.Marshal(env) - if err != nil { - t.Errorf("failed to re marshal auth as json: %v", err) - } - - if string(asjson) != raw { - t.Log(string(asjson)) - t.Error("json serialization broken") - } + err := json.Unmarshal([]byte(raw), &env) + assert.NoError(t, err) + + asJSON, err := json.Marshal(env) + assert.NoError(t, err) + assert.Equal(t, raw, string(asJSON)) } } @@ -184,12 +165,12 @@ func TestParseMessage(t *testing.T) { if testCase.ExpectedEnvelope == nil && envelope == nil { return } - if testCase.ExpectedEnvelope == nil && envelope != nil { - t.Fatalf("expected nil but got %v\n", envelope) - } - if testCase.ExpectedEnvelope.String() != envelope.String() { - t.Fatalf("unexpected output:\n %s\n != %s", testCase.ExpectedEnvelope, envelope) + + if testCase.ExpectedEnvelope == nil { + assert.NotNil(t, envelope, "expected nil but got %v\n", envelope) } + + assert.Equal(t, testCase.ExpectedEnvelope.String(), envelope.String()) }) } } diff --git a/eose_test.go b/eose_test.go index 2815a8e..d073bc7 100644 --- a/eose_test.go +++ b/eose_test.go @@ -4,19 +4,18 @@ import ( "context" "testing" "time" + + "github.com/stretchr/testify/assert" ) func TestEOSEMadness(t *testing.T) { - rl := mustRelayConnect(RELAY) + rl := mustRelayConnect(t, RELAY) defer rl.Close() sub, err := rl.Subscribe(context.Background(), Filters{ {Kinds: []int{KindTextNote}, Limit: 2}, }) - if err != nil { - t.Errorf("subscription failed: %v", err) - return - } + assert.NoError(t, err) timeout := time.After(3 * time.Second) n := 0 @@ -25,15 +24,11 @@ func TestEOSEMadness(t *testing.T) { for { select { case event := <-sub.Events: - if event == nil { - t.Fatalf("event is nil: %v", event) - } + assert.NotNil(t, event) n++ case <-sub.EndOfStoredEvents: e++ - if e > 1 { - t.Fatalf("eose infinite loop") - } + assert.Less(t, e, 1, "eose infinite loop") continue case <-rl.Context().Done(): t.Fatalf("connection closed: %v", rl.Context().Err()) @@ -43,10 +38,6 @@ func TestEOSEMadness(t *testing.T) { } end: - if e != 1 { - t.Fatalf("didn't get an eose") - } - if n < 2 { - t.Fatalf("didn't get events") - } + assert.Equal(t, 1, e) + assert.Greater(t, n, 2) } diff --git a/event_test.go b/event_test.go index f631b4a..58a0379 100644 --- a/event_test.go +++ b/event_test.go @@ -3,6 +3,8 @@ package nostr import ( "encoding/json" "testing" + + "github.com/stretchr/testify/assert" ) func TestEventParsingAndVerifying(t *testing.T) { @@ -15,27 +17,17 @@ func TestEventParsingAndVerifying(t *testing.T) { for _, raw := range rawEvents { var ev Event - if err := json.Unmarshal([]byte(raw), &ev); err != nil { - t.Errorf("failed to parse event json: %v", err) - } - - if ev.GetID() != ev.ID { - t.Errorf("error serializing event id: %s != %s", ev.GetID(), ev.ID) - } + err := json.Unmarshal([]byte(raw), &ev) + assert.NoError(t, err) - if ok, _ := ev.CheckSignature(); !ok { - t.Error("signature verification failed when it should have succeeded") - } + assert.Equal(t, ev.ID, ev.GetID()) - asjson, err := json.Marshal(ev) - if err != nil { - t.Errorf("failed to re marshal event as json: %v", err) - } + ok, _ := ev.CheckSignature() + assert.True(t, ok, "signature verification failed when it should have succeeded") - if string(asjson) != raw { - t.Log(string(asjson)) - t.Error("json serialization broken") - } + asJSON, err := json.Marshal(ev) + assert.NoError(t, err) + assert.Equal(t, raw, string(asJSON)) } } @@ -54,33 +46,26 @@ func TestEventSerialization(t *testing.T) { for _, evt := range events { b, err := json.Marshal(evt) - if err != nil { - t.Log(evt) - t.Error("failed to serialize this event") - } + assert.NoError(t, err) var re Event - if err := json.Unmarshal(b, &re); err != nil { - t.Log(string(b)) - t.Error("failed to re parse event just serialized") - } - - if evt.ID != re.ID || evt.PubKey != re.PubKey || evt.Content != re.Content || - evt.CreatedAt != re.CreatedAt || evt.Sig != re.Sig || - len(evt.Tags) != len(re.Tags) { - t.Error("reparsed event differs from original") - } + err = json.Unmarshal(b, &re) + assert.NoError(t, err) + + assert.Condition(t, func() (success bool) { + if evt.ID != re.ID || evt.PubKey != re.PubKey || evt.Content != re.Content || + evt.CreatedAt != re.CreatedAt || evt.Sig != re.Sig || + len(evt.Tags) != len(re.Tags) { + return false + } + return true + }, "re-parsed event differs from original") for i := range evt.Tags { - if len(evt.Tags[i]) != len(re.Tags[i]) { - t.Errorf("reparsed tags %d length differ from original", i) - continue - } + assert.Equal(t, len(evt.Tags[i]), len(re.Tags[i]), "re-parsed tags %d length differ from original", i) for j := range evt.Tags[i] { - if evt.Tags[i][j] != re.Tags[i][j] { - t.Errorf("reparsed tag content %d %d length differ from original", i, j) - } + assert.Equal(t, re.Tags[i][j], evt.Tags[i][j], "re-parsed tag content %d %d length differ from original", i, j) } } } @@ -101,39 +86,44 @@ func TestEventSerializationWithExtraFields(t *testing.T) { evt.SetExtra("malf", "hello") b, err := json.Marshal(evt) - if err != nil { - t.Log(evt) - t.Error("failed to serialize this event") - } + assert.NoError(t, err) var re Event - if err := json.Unmarshal(b, &re); err != nil { - t.Log(string(b)) - t.Error("failed to re parse event just serialized") - } + err = json.Unmarshal(b, &re) + assert.NoError(t, err) - if evt.ID != re.ID || evt.PubKey != re.PubKey || evt.Content != re.Content || - evt.CreatedAt != re.CreatedAt || evt.Sig != re.Sig || - len(evt.Tags) != len(re.Tags) { - t.Error("reparsed event differs from original") - } + assert.Condition(t, func() (success bool) { + if evt.ID != re.ID || evt.PubKey != re.PubKey || evt.Content != re.Content || + evt.CreatedAt != re.CreatedAt || evt.Sig != re.Sig || + len(evt.Tags) != len(re.Tags) { + return false + } + return true + }, "reparsed event differs from original") - if evt.GetExtra("malf").(string) != evt.GetExtraString("malf") || evt.GetExtraString("malf") != "hello" { - t.Errorf("failed to parse extra string") - } + assert.Condition(t, func() (success bool) { + if evt.GetExtra("malf").(string) != evt.GetExtraString("malf") || evt.GetExtraString("malf") != "hello" { + return false + } + return true + }, "failed to parse extra string") - if float64(evt.GetExtra("elet").(int)) != evt.GetExtraNumber("elet") || evt.GetExtraNumber("elet") != 77 { - t.Logf("number: %v == %v", evt.GetExtra("elet"), evt.GetExtraNumber("elet")) - t.Errorf("failed to parse extra number") - } + assert.Condition(t, func() (success bool) { + if float64(evt.GetExtra("elet").(int)) != evt.GetExtraNumber("elet") || evt.GetExtraNumber("elet") != 77 { + return false + } + return true + }, "failed to parse extra number") - if evt.GetExtra("glub").(bool) != evt.GetExtraBoolean("glub") || evt.GetExtraBoolean("glub") != true { - t.Errorf("failed to parse extra boolean") - } + assert.Condition(t, func() (success bool) { + if evt.GetExtra("glub").(bool) != evt.GetExtraBoolean("glub") || evt.GetExtraBoolean("glub") != true { - if evt.GetExtra("plik") != nil { - t.Errorf("failed to parse extra null") - } + return false + } + return true + }, "failed to parse extra boolean") + + assert.Nil(t, evt.GetExtra("plik")) } func mustSignEvent(t *testing.T, privkey string, event *Event) { diff --git a/filter_test.go b/filter_test.go index 9422ede..0ee21e6 100644 --- a/filter_test.go +++ b/filter_test.go @@ -4,21 +4,25 @@ import ( "encoding/json" "slices" "testing" + + "github.com/stretchr/testify/assert" ) func TestFilterUnmarshal(t *testing.T) { raw := `{"ids": ["abc"],"#e":["zzz"],"#something":["nothing","bab"],"since":1644254609,"search":"test"}` var f Filter - if err := json.Unmarshal([]byte(raw), &f); err != nil { - t.Errorf("failed to parse filter json: %v", err) - } - - if f.Since == nil || f.Since.Time().UTC().Format("2006-01-02") != "2022-02-07" || - f.Until != nil || - f.Tags == nil || len(f.Tags) != 2 || !slices.Contains(f.Tags["something"], "bab") || - f.Search != "test" { - t.Error("failed to parse filter correctly") - } + err := json.Unmarshal([]byte(raw), &f) + assert.NoError(t, err) + + assert.Condition(t, func() (success bool) { + if f.Since == nil || f.Since.Time().UTC().Format("2006-01-02") != "2022-02-07" || + f.Until != nil || + f.Tags == nil || len(f.Tags) != 2 || !slices.Contains(f.Tags["something"], "bab") || + f.Search != "test" { + return false + } + return true + }, "failed to parse filter correctly") } func TestFilterMarshal(t *testing.T) { @@ -28,31 +32,29 @@ func TestFilterMarshal(t *testing.T) { Tags: TagMap{"fruit": {"banana", "mango"}}, Until: &until, }) - if err != nil { - t.Errorf("failed to marshal filter json: %v", err) - } + assert.NoError(t, err) expected := `{"kinds":[1,2,4],"until":12345678,"#fruit":["banana","mango"]}` - if string(filterj) != expected { - t.Errorf("filter json was wrong: %s != %s", string(filterj), expected) - } + assert.Equal(t, expected, string(filterj)) } func TestFilterUnmarshalWithLimitZero(t *testing.T) { raw := `{"ids": ["abc"],"#e":["zzz"],"limit":0,"#something":["nothing","bab"],"since":1644254609,"search":"test"}` var f Filter - if err := json.Unmarshal([]byte(raw), &f); err != nil { - t.Errorf("failed to parse filter json: %v", err) - } - - if f.Since == nil || - f.Since.Time().UTC().Format("2006-01-02") != "2022-02-07" || - f.Until != nil || - f.Tags == nil || len(f.Tags) != 2 || !slices.Contains(f.Tags["something"], "bab") || - f.Search != "test" || - f.LimitZero == false { - t.Error("failed to parse filter correctly") - } + err := json.Unmarshal([]byte(raw), &f) + assert.NoError(t, err) + + assert.Condition(t, func() (success bool) { + if f.Since == nil || + f.Since.Time().UTC().Format("2006-01-02") != "2022-02-07" || + f.Until != nil || + f.Tags == nil || len(f.Tags) != 2 || !slices.Contains(f.Tags["something"], "bab") || + f.Search != "test" || + f.LimitZero == false { + return false + } + return true + }, "failed to parse filter correctly") } func TestFilterMarshalWithLimitZero(t *testing.T) { @@ -63,14 +65,10 @@ func TestFilterMarshalWithLimitZero(t *testing.T) { Until: &until, LimitZero: true, }) - if err != nil { - t.Errorf("failed to marshal filter json: %v", err) - } + assert.NoError(t, err) expected := `{"kinds":[1,2,4],"until":12345678,"limit":0,"#fruit":["banana","mango"]}` - if string(filterj) != expected { - t.Errorf("filter json was wrong: %s != %s", string(filterj), expected) - } + assert.Equal(t, expected, string(filterj)) } func TestFilterMatchingLive(t *testing.T) { @@ -80,28 +78,22 @@ func TestFilterMatchingLive(t *testing.T) { json.Unmarshal([]byte(`{"kinds":[1],"authors":["a8171781fd9e90ede3ea44ddca5d3abf828fe8eedeb0f3abb0dd3e563562e1fc","1d80e5588de010d137a67c42b03717595f5f510e73e42cfc48f31bae91844d59","ed4ca520e9929dfe9efdadf4011b53d30afd0678a09aa026927e60e7a45d9244"],"since":1677033299}`), &filter) json.Unmarshal([]byte(`{"id":"5a127c9c931f392f6afc7fdb74e8be01c34035314735a6b97d2cf360d13cfb94","pubkey":"1d80e5588de010d137a67c42b03717595f5f510e73e42cfc48f31bae91844d59","created_at":1677033299,"kind":1,"tags":[["t","japan"]],"content":"If you like my art,I'd appreciate a coin or two!!\nZap is welcome!! Thanks.\n\n\n#japan #bitcoin #art #bananaart\nhttps://void.cat/d/CgM1bzDgHUCtiNNwfX9ajY.webp","sig":"828497508487ca1e374f6b4f2bba7487bc09fccd5cc0d1baa82846a944f8c5766918abf5878a580f1e6615de91f5b57a32e34c42ee2747c983aaf47dbf2a0255"}`), &event) - if !filter.Matches(&event) { - t.Error("live filter should match") - } + assert.True(t, filter.Matches(&event), "live filter should match") } func TestFilterEquality(t *testing.T) { - if !FilterEqual( + assert.True(t, FilterEqual( Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion}}, Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion}}, - ) { - t.Error("kinds filters should be equal") - } + ), "kinds filters should be equal") - if !FilterEqual( + assert.True(t, FilterEqual( Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion}, Tags: TagMap{"letter": {"a", "b"}}}, Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion}, Tags: TagMap{"letter": {"b", "a"}}}, - ) { - t.Error("kind+tags filters should be equal") - } + ), "kind+tags filters should be equal") tm := Now() - if !FilterEqual( + assert.True(t, FilterEqual( Filter{ Kinds: []int{KindEncryptedDirectMessage, KindDeletion}, Tags: TagMap{"letter": {"a", "b"}, "fruit": {"banana"}}, @@ -114,16 +106,12 @@ func TestFilterEquality(t *testing.T) { Since: &tm, IDs: []string{"aaaa", "bbbb"}, }, - ) { - t.Error("kind+2tags+since+ids filters should be equal") - } + ), "kind+2tags+since+ids filters should be equal") - if FilterEqual( + assert.False(t, FilterEqual( Filter{Kinds: []int{KindTextNote, KindEncryptedDirectMessage, KindDeletion}}, Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion, KindRepost}}, - ) { - t.Error("kinds filters shouldn't be equal") - } + ), "kinds filters shouldn't be equal") } func TestFilterClone(t *testing.T) { @@ -135,31 +123,21 @@ func TestFilterClone(t *testing.T) { IDs: []string{"9894b4b5cb5166d23ee8899a4151cf0c66aec00bde101982a13b8e8ceb972df9"}, } clone := flt.Clone() - if !FilterEqual(flt, clone) { - t.Errorf("clone is not equal:\n %v !=\n %v", flt, clone) - } + assert.True(t, FilterEqual(flt, clone), "clone is not equal:\n %v !=\n %v", flt, clone) clone1 := flt.Clone() clone1.IDs = append(clone1.IDs, "88f0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d") - if FilterEqual(flt, clone1) { - t.Error("modifying the clone ids should cause it to not be equal anymore") - } + assert.False(t, FilterEqual(flt, clone1), "modifying the clone ids should cause it to not be equal anymore") clone2 := flt.Clone() clone2.Tags["letter"] = append(clone2.Tags["letter"], "c") - if FilterEqual(flt, clone2) { - t.Error("modifying the clone tag items should cause it to not be equal anymore") - } + assert.False(t, FilterEqual(flt, clone2), "modifying the clone tag items should cause it to not be equal anymore") clone3 := flt.Clone() clone3.Tags["g"] = []string{"drt"} - if FilterEqual(flt, clone3) { - t.Error("modifying the clone tag map should cause it to not be equal anymore") - } + assert.False(t, FilterEqual(flt, clone3), "modifying the clone tag map should cause it to not be equal anymore") clone4 := flt.Clone() *clone4.Since++ - if FilterEqual(flt, clone4) { - t.Error("modifying the clone since should cause it to not be equal anymore") - } + assert.False(t, FilterEqual(flt, clone4), "modifying the clone since should cause it to not be equal anymore") } diff --git a/go.mod b/go.mod index d02161f..5a4e81c 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/nbd-wtf/go-nostr -go 1.21 +go 1.22.5 require ( github.com/bluekeyes/go-gitdiff v0.7.1 @@ -10,7 +10,7 @@ require ( github.com/gobwas/ws v1.2.0 github.com/mailru/easyjson v0.7.7 github.com/puzpuzpuz/xsync/v3 v3.0.2 - github.com/stretchr/testify v1.8.2 + github.com/stretchr/testify v1.9.0 github.com/tidwall/gjson v1.14.4 github.com/tyler-smith/go-bip32 v1.0.0 github.com/tyler-smith/go-bip39 v1.1.0 diff --git a/go.sum b/go.sum index 3ab0a69..a5fd901 100644 --- a/go.sum +++ b/go.sum @@ -92,6 +92,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc= github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= diff --git a/nip05/nip05_test.go b/nip05/nip05_test.go index 6d524fe..4d755d0 100644 --- a/nip05/nip05_test.go +++ b/nip05/nip05_test.go @@ -3,48 +3,54 @@ package nip05 import ( "context" "testing" + + "github.com/stretchr/testify/assert" ) func TestParse(t *testing.T) { - name, domain, _ := ParseIdentifier("saknd@yyq.com") - if name != "saknd" || domain != "yyq.com" { - t.Fatalf("wrong parsing") - } - - name, domain, _ = ParseIdentifier("287354gkj+asbdfo8gw3rlicbsopifbcp3iougb5piseubfdikswub5ks@yyq.com") - if name != "287354gkj+asbdfo8gw3rlicbsopifbcp3iougb5piseubfdikswub5ks" || domain != "yyq.com" { - t.Fatalf("wrong parsing") - } - - name, domain, _ = ParseIdentifier("asdn.com") - if name != "_" || domain != "asdn.com" { - t.Fatalf("wrong parsing") - } - - name, domain, _ = ParseIdentifier("_@uxux.com.br") - if name != "_" || domain != "uxux.com.br" { - t.Fatalf("wrong parsing") - } - - _, _, err := ParseIdentifier("821yh498ig21") - if err == nil { - t.Fatalf("should have errored") - } - - _, _, err = ParseIdentifier("////") - if err == nil { - t.Fatalf("should have errored") + tests := []struct { + input string + expectedName string + expectedDomain string + expectError bool + }{ + {"saknd@yyq.com", "saknd", "yyq.com", false}, + {"287354gkj+asbdfo8gw3rlicbsopifbcp3iougb5piseubfdikswub5ks@yyq.com", "287354gkj+asbdfo8gw3rlicbsopifbcp3iougb5piseubfdikswub5ks", "yyq.com", false}, + {"asdn.com", "_", "asdn.com", false}, + {"_@uxux.com.br", "_", "uxux.com.br", false}, + {"821yh498ig21", "", "", true}, + {"////", "", "", true}, + } + + for _, test := range tests { + name, domain, err := ParseIdentifier(test.input) + if test.expectError { + assert.Error(t, err, "expected error for input: %s", test.input) + } else { + assert.NoError(t, err, "not expect error for input: %s", test.input) + assert.Equal(t, test.expectedName, name) + assert.Equal(t, test.expectedDomain, domain) + } } } func TestQuery(t *testing.T) { - pp, err := QueryIdentifier(context.Background(), "fiatjaf.com") - if err != nil || pp.PublicKey != "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d" { - t.Fatalf("invalid query for fiatjaf.com") - } - - pp, err = QueryIdentifier(context.Background(), "htlc@fiatjaf.com") - if err != nil || pp.PublicKey != "f9dd6a762506260b38a2d3e5b464213c2e47fa3877429fe9ee60e071a31a07d7" { - t.Fatalf("invalid query for htlc@fiatjaf.com") + tests := []struct { + input string + expectedKey string + expectError bool + }{ + {"fiatjaf.com", "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d", false}, + {"htlc@fiatjaf.com", "f9dd6a762506260b38a2d3e5b464213c2e47fa3877429fe9ee60e071a31a07d7", false}, + } + + for _, test := range tests { + pp, err := QueryIdentifier(context.Background(), test.input) + if test.expectError { + assert.Error(t, err, "Expected error for input: %s", test.input) + } else { + assert.NoError(t, err, "Did not expect error for input: %s", test.input) + assert.Equal(t, test.expectedKey, pp.PublicKey, "For input: %s", test.input) + } } } diff --git a/nip11/nip11_test.go b/nip11/nip11_test.go index cda76de..90dadca 100644 --- a/nip11/nip11_test.go +++ b/nip11/nip11_test.go @@ -26,30 +26,35 @@ func TestAddSupportedNIP(t *testing.T) { info.AddSupportedNIP(18) for i, v := range []int{0, 1, 2, 12, 13, 17, 18, 19, 44} { - if info.SupportedNIPs[i] != v { - t.Errorf("expected info.SupportedNIPs[%d] to equal %v, got %v", - i, v, info.SupportedNIPs) - return - } + assert.Equal(t, v, info.SupportedNIPs[i], "expected info.SupportedNIPs[%d] to equal %v, got %v", + i, v, info.SupportedNIPs) } } func TestFetch(t *testing.T) { - res, err := Fetch(context.Background(), "wss://relay.nostr.bg") - assert.Equal(t, res.URL, "wss://relay.nostr.bg") - assert.Nil(t, err, "failed to fetch from wss") - assert.NotEmpty(t, res.Name) - - res, err = Fetch(context.Background(), "https://relay.nostr.bg") - assert.Nil(t, err, "failed to fetch from https") - assert.NotEmpty(t, res.Name) - - res, err = Fetch(context.Background(), "relay.nostr.bg") - assert.Nil(t, err, "failed to fetch without protocol") - assert.NotEmpty(t, res.Name) + tests := []struct { + inputURL string + expectError bool + expectedName string + expectedURL string + }{ + {"wss://relay.nostr.bg", false, "", "wss://relay.nostr.bg"}, + {"https://relay.nostr.bg", false, "", "wss://relay.nostr.bg"}, + {"relay.nostr.bg", false, "", "wss://relay.nostr.bg"}, + {"wlenwqkeqwe.asjdaskd", true, "", "wss://wlenwqkeqwe.asjdaskd"}, + } - res, err = Fetch(context.Background(), "wlenwqkeqwe.asjdaskd") - assert.Error(t, err) - assert.NotNil(t, res) - assert.Equal(t, res.URL, "wss://wlenwqkeqwe.asjdaskd") + for _, test := range tests { + res, err := Fetch(context.Background(), test.inputURL) + + if test.expectError { + assert.Error(t, err, "expected error for URL: %s", test.inputURL) + assert.NotNil(t, res, "expected result not to be nil for URL: %s", test.inputURL) + assert.Equal(t, test.expectedURL, res.URL, "expected URL to be %s for input: %s", test.expectedURL, test.inputURL) + } else { + assert.Nil(t, err, "unexpect error for URL: %s", test.inputURL) + assert.NotEmpty(t, res.Name, "expected non-empty name for URL: %s", test.inputURL) + assert.Equal(t, test.expectedURL, res.URL, "expected URL to be %s for input: %s", test.expectedURL, test.inputURL) + } + } } diff --git a/nip13/deprecated.go b/nip13/deprecated.go index c83b5b1..8d41900 100644 --- a/nip13/deprecated.go +++ b/nip13/deprecated.go @@ -7,7 +7,7 @@ import ( "github.com/nbd-wtf/go-nostr" ) -// Deprecated: use DoWork() +// Deprecated: use DoWork() instead. func Generate(event *nostr.Event, targetDifficulty int, timeout time.Duration) (*nostr.Event, error) { if event.PubKey == "" { return nil, ErrMissingPubKey diff --git a/nip19/nip19_test.go b/nip19/nip19_test.go index 6e21360..8fd1640 100644 --- a/nip19/nip19_test.go +++ b/nip19/nip19_test.go @@ -4,96 +4,61 @@ import ( "testing" "github.com/nbd-wtf/go-nostr" + "github.com/stretchr/testify/assert" ) func TestEncodeNpub(t *testing.T) { npub, err := EncodePublicKey("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d") - if err != nil { - t.Errorf("shouldn't error: %s", err) - } - if npub != "npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6" { - t.Error("produced an unexpected npub string") - } + assert.NoError(t, err) + assert.Equal(t, "npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6", npub, "produced an unexpected npub string") } func TestEncodeNsec(t *testing.T) { nsec, err := EncodePrivateKey("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d") - if err != nil { - t.Errorf("shouldn't error: %s", err) - } - if nsec != "nsec180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsgyumg0" { - t.Error("produced an unexpected nsec string") - } + assert.NoError(t, err) + assert.Equal(t, "nsec180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsgyumg0", nsec, "produced an unexpected nsec string") } func TestDecodeNpub(t *testing.T) { prefix, pubkey, err := Decode("npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6") - if err != nil { - t.Errorf("shouldn't error: %s", err) - } - if prefix != "npub" { - t.Error("returned invalid prefix") - } - if pubkey.(string) != "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d" { - t.Error("returned wrong pubkey") - } + assert.NoError(t, err) + assert.Equal(t, "npub", prefix, "returned invalid prefix") + assert.Equal(t, "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d", pubkey.(string), "returned wrong pubkey") } func TestFailDecodeBadChecksumNpub(t *testing.T) { _, _, err := Decode("npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w4") - if err == nil { - t.Errorf("should have errored: %s", err) - } + assert.Error(t, err) } func TestDecodeNprofile(t *testing.T) { - prefix, data, err := Decode("nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p") - if err != nil { - t.Error("failed to decode nprofile") - } - if prefix != "nprofile" { - t.Error("what") - } - pp, ok := data.(nostr.ProfilePointer) - if !ok { - t.Error("value returned of wrong type") - } - - if pp.PublicKey != "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d" { - t.Error("decoded invalid public key") - } - - if len(pp.Relays) != 2 { - t.Error("decoded wrong number of relays") - } - if pp.Relays[0] != "wss://r.x.com" || pp.Relays[1] != "wss://djbas.sadkb.com" { - t.Error("decoded relay URLs wrongly") - } -} + t.Run("first", func(t *testing.T) { + prefix, data, err := Decode("nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p") + assert.NoError(t, err) + assert.Equal(t, "nprofile", prefix) + + pp, ok := data.(nostr.ProfilePointer) + assert.True(t, ok, "value returned of wrong type") + assert.Equal(t, "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d", pp.PublicKey) + assert.Equal(t, 2, len(pp.Relays), "decoded wrong number of relays") + + assert.Equal(t, "wss://r.x.com", pp.Relays[0], "decoded relay URLs wrongly") + assert.Equal(t, "wss://djbas.sadkb.com", pp.Relays[1], "decoded relay URLs wrongly") + }) + + t.Run("second", func(t *testing.T) { + prefix, data, err := Decode("nprofile1qqsw3dy8cpumpanud9dwd3xz254y0uu2m739x0x9jf4a9sgzjshaedcpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5qyw8wumn8ghj7mn0wd68yttjv4kxz7fww4h8get5dpezumt9qyvhwumn8ghj7un9d3shjetj9enxjct5dfskvtnrdakstl69hg") + assert.NoError(t, err) + assert.Equal(t, "nprofile", prefix) + + pp, ok := data.(nostr.ProfilePointer) + assert.True(t, ok, "value returned of wrong type") + assert.Equal(t, "e8b487c079b0f67c695ae6c4c2552a47f38adfa2533cc5926bd2c102942fdcb7", pp.PublicKey) + assert.Equal(t, 3, len(pp.Relays), "decoded wrong number of relays") -func TestDecodeOtherNprofile(t *testing.T) { - prefix, data, err := Decode("nprofile1qqsw3dy8cpumpanud9dwd3xz254y0uu2m739x0x9jf4a9sgzjshaedcpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5qyw8wumn8ghj7mn0wd68yttjv4kxz7fww4h8get5dpezumt9qyvhwumn8ghj7un9d3shjetj9enxjct5dfskvtnrdakstl69hg") - if err != nil { - t.Error("failed to decode nprofile") - } - if prefix != "nprofile" { - t.Error("what") - } - pp, ok := data.(nostr.ProfilePointer) - if !ok { - t.Error("value returned of wrong type") - } - - if pp.PublicKey != "e8b487c079b0f67c695ae6c4c2552a47f38adfa2533cc5926bd2c102942fdcb7" { - t.Error("decoded invalid public key") - } - - if len(pp.Relays) != 3 { - t.Error("decoded wrong number of relays") - } - if pp.Relays[0] != "wss://nostr-pub.wellorder.net" || pp.Relays[1] != "wss://nostr-relay.untethr.me" { - t.Error("decoded relay URLs wrongly") - } + assert.Equal(t, "wss://nostr-pub.wellorder.net", pp.Relays[0], "decoded relay URLs wrongly") + assert.Equal(t, "wss://nostr-relay.untethr.me", pp.Relays[1], "decoded relay URLs wrongly") + }) } func TestEncodeNprofile(t *testing.T) { @@ -101,12 +66,11 @@ func TestEncodeNprofile(t *testing.T) { "wss://r.x.com", "wss://djbas.sadkb.com", }) - if err != nil { - t.Errorf("shouldn't error: %s", err) - } - if nprofile != "nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p" { - t.Error("produced an unexpected nprofile string") - } + + assert.NoError(t, err) + assert.Equal(t, + "nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p", + nprofile, "produced an unexpected nprofile string: %s", nprofile) } func TestEncodeDecodeNaddr(t *testing.T) { @@ -118,91 +82,60 @@ func TestEncodeDecodeNaddr(t *testing.T) { "wss://relay.nostr.example.mydomain.example.com", "wss://nostr.banana.com", }) - if err != nil { - t.Errorf("shouldn't error: %s", err) - } - if naddr != "naddr1qqrxyctwv9hxzqfwwaehxw309aex2mrp0yhxummnw3ezuetcv9khqmr99ekhjer0d4skjm3wv4uxzmtsd3jjucm0d5q3vamnwvaz7tmwdaehgu3wvfskuctwvyhxxmmdqgsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8grqsqqqa28a3lkds" { - t.Errorf("produced an unexpected naddr string: %s", naddr) - } + + assert.NoError(t, err) + assert.Equal(t, + "naddr1qqrxyctwv9hxzqfwwaehxw309aex2mrp0yhxummnw3ezuetcv9khqmr99ekhjer0d4skjm3wv4uxzmtsd3jjucm0d5q3vamnwvaz7tmwdaehgu3wvfskuctwvyhxxmmdqgsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8grqsqqqa28a3lkds", + naddr, "produced an unexpected naddr string: %s", naddr) prefix, data, err := Decode(naddr) - if err != nil { - t.Errorf("shouldn't error: %s", err) - } - if prefix != "naddr" { - t.Error("returned invalid prefix") - } + assert.NoError(t, err) + assert.Equal(t, "naddr", prefix) + ep := data.(nostr.EntityPointer) - if ep.PublicKey != "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d" { - t.Error("returned wrong pubkey") - } - if ep.Kind != nostr.KindArticle { - t.Error("returned wrong kind") - } - if ep.Identifier != "banana" { - t.Error("returned wrong identifier") - } - if ep.Relays[0] != "wss://relay.nostr.example.mydomain.example.com" || ep.Relays[1] != "wss://nostr.banana.com" { - t.Error("returned wrong relays") - } + assert.Equal(t, "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d", ep.PublicKey) + assert.Equal(t, ep.Kind, nostr.KindArticle) + assert.Equal(t, "banana", ep.Identifier) + + assert.Equal(t, "wss://relay.nostr.example.mydomain.example.com", ep.Relays[0]) + assert.Equal(t, "wss://nostr.banana.com", ep.Relays[1]) } func TestDecodeNaddrWithoutRelays(t *testing.T) { prefix, data, err := Decode("naddr1qq98yetxv4ex2mnrv4esygrl54h466tz4v0re4pyuavvxqptsejl0vxcmnhfl60z3rth2xkpjspsgqqqw4rsf34vl5") - if err != nil { - t.Errorf("shouldn't error: %s", err) - } - if prefix != "naddr" { - t.Error("returned invalid prefix") - } - ep := data.(nostr.EntityPointer) - if ep.PublicKey != "7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194" { - t.Error("returned wrong pubkey") - } - if ep.Kind != nostr.KindArticle { - t.Error("returned wrong kind") - } - if ep.Identifier != "references" { - t.Error("returned wrong identifier") - } - if len(ep.Relays) != 0 { - t.Error("relays should have been an empty array") - } + assert.NoError(t, err, "unexpected error during decoding of Naddr") + assert.Equal(t, "naddr", prefix, "returned invalid prefix") + + ep, ok := data.(nostr.EntityPointer) + assert.True(t, ok) + assert.Equal(t, "7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194", ep.PublicKey) + assert.Equal(t, nostr.KindArticle, ep.Kind) + assert.Equal(t, "references", ep.Identifier) + assert.Empty(t, ep.Relays) } -func TestEncodeDecodeNEventTestEncodeDecodeNEvent(t *testing.T) { +func TestEncodeDecodeNEvent(t *testing.T) { nevent, err := EncodeEvent( "45326f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194", []string{"wss://banana.com"}, "7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751abb88", ) - if err != nil { - t.Errorf("shouldn't error: %s", err) - } - prefix, res, err := Decode(nevent) - if err != nil { - t.Errorf("shouldn't error: %s", err) - } + assert.NoError(t, err) - if prefix != "nevent" { - t.Errorf("should have 'nevent' prefix, not '%s'", prefix) - } + expectedNEvent := "nevent1qqsy2vn0t45k92c78n2zfe6ccvqzhpn977cd3h8wnl579zxhw5dvr9qpzpmhxue69uhkyctwv9hxztnrdaksygrl54h466tz4v0re4pyuavvxqptsejl0vxcmnhfl60z3rth2x4m3q04ndyp" + assert.Equal(t, expectedNEvent, nevent) - ep, ok := res.(nostr.EventPointer) - if !ok { - t.Errorf("'%s' should be an nevent, not %v", nevent, res) - } + prefix, res, err := Decode(nevent) + assert.NoError(t, err) - if ep.Author != "7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751abb88" { - t.Error("wrong author") - } + assert.Equal(t, "nevent", prefix) - if ep.ID != "45326f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194" { - t.Error("wrong id") - } + ep, ok := res.(nostr.EventPointer) + assert.True(t, ok) - if len(ep.Relays) != 1 || ep.Relays[0] != "wss://banana.com" { - t.Error("wrong relay") - } + assert.Equal(t, "7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751abb88", ep.Author) + assert.Equal(t, "45326f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194", ep.ID) + assert.Equal(t, 1, len(ep.Relays), "wrong number of relays") + assert.Equal(t, "wss://banana.com", ep.Relays[0]) } diff --git a/nip29/nip29_test.go b/nip29/nip29_test.go index 189e51c..61e5d5d 100644 --- a/nip29/nip29_test.go +++ b/nip29/nip29_test.go @@ -2,6 +2,8 @@ package nip29 import ( "testing" + + "github.com/stretchr/testify/assert" ) const ( @@ -16,11 +18,10 @@ func TestGroupEventBackAndForth(t *testing.T) { group1.Name = "banana" group1.Private = true meta1 := group1.ToMetadataEvent() - if meta1.Tags.GetD() != "xyz" || - meta1.Tags.GetFirst([]string{"name", "banana"}) == nil || - meta1.Tags.GetFirst([]string{"private"}) == nil { - t.Fatalf("translation of group1 to metadata event failed: %s", meta1) - } + + assert.Equal(t, "xyz", meta1.Tags.GetD(), "translation of group1 to metadata event failed: %s", meta1) + assert.NotNil(t, meta1.Tags.GetFirst([]string{"name", "banana"}), "translation of group1 to metadata event failed: %s", meta1) + assert.NotNil(t, meta1.Tags.GetFirst([]string{"private"}), "translation of group1 to metadata event failed: %s", meta1) group2, _ := NewGroup("groups.com'abc") group2.Members[ALICE] = &Role{Name: "nada", Permissions: map[Permission]struct{}{PermAddUser: {}}} @@ -28,34 +29,31 @@ func TestGroupEventBackAndForth(t *testing.T) { group2.Members[CAROL] = EmptyRole group2.Members[DEREK] = EmptyRole admins2 := group2.ToAdminsEvent() - if admins2.Tags.GetD() != "abc" || - len(admins2.Tags) != 3 || - admins2.Tags.GetFirst([]string{"p", ALICE, "nada", "add-user"}) == nil || - admins2.Tags.GetFirst([]string{"p", BOB, "nada", "edit-metadata"}) == nil { - t.Fatalf("translation of group2 to admins event failed") - } + + assert.Equal(t, "abc", admins2.Tags.GetD(), "translation of group2 to admins event failed") + assert.Equal(t, 3, len(admins2.Tags), "translation of group2 to admins event failed") + assert.NotNil(t, admins2.Tags.GetFirst([]string{"p", ALICE, "nada", "add-user"}), "translation of group2 to admins event failed") + assert.NotNil(t, admins2.Tags.GetFirst([]string{"p", BOB, "nada", "edit-metadata"}), "translation of group2 to admins event failed") members2 := group2.ToMembersEvent() - if members2.Tags.GetD() != "abc" || - len(members2.Tags) != 5 || - members2.Tags.GetFirst([]string{"p", ALICE}) == nil || - members2.Tags.GetFirst([]string{"p", BOB}) == nil || - members2.Tags.GetFirst([]string{"p", CAROL}) == nil || - members2.Tags.GetFirst([]string{"p", DEREK}) == nil { - t.Fatalf("translation of group2 to members2 event failed") - } + assert.Equal(t, "abc", members2.Tags.GetD(), "translation of group2 to members2 event failed") + assert.Equal(t, 5, len(members2.Tags), "translation of group2 to members2 event failed") + assert.NotNil(t, members2.Tags.GetFirst([]string{"p", ALICE}), "translation of group2 to members2 event failed") + assert.NotNil(t, members2.Tags.GetFirst([]string{"p", BOB}), "translation of group2 to members2 event failed") + assert.NotNil(t, members2.Tags.GetFirst([]string{"p", CAROL}), "translation of group2 to members2 event failed") + assert.NotNil(t, members2.Tags.GetFirst([]string{"p", DEREK}), "translation of group2 to members2 event failed") group1.MergeInMembersEvent(members2) - if len(group1.Members) != 4 || group1.Members[ALICE] != EmptyRole || group1.Members[DEREK] != EmptyRole { - t.Fatalf("merge of members2 into group1 failed") - } + assert.Equal(t, 4, len(group1.Members), "merge of members2 into group1 failed") + assert.Equal(t, EmptyRole, group1.Members[ALICE], "merge of members2 into group1 failed") + assert.Equal(t, EmptyRole, group1.Members[DEREK], "merge of members2 into group1 failed") + group1.MergeInAdminsEvent(admins2) - if len(group1.Members) != 4 || group1.Members[ALICE].Name != "nada" || group1.Members[DEREK] != EmptyRole { - t.Fatalf("merge of admins2 into group1 failed") - } + assert.Equal(t, 4, len(group1.Members), "merge of admins2 into group1 failed") + assert.Equal(t, "nada", group1.Members[ALICE].Name, "merge of admins2 into group1 failed") + assert.Equal(t, EmptyRole, group1.Members[DEREK], "merge of admins2 into group1 failed") group2.MergeInMetadataEvent(meta1) - if group2.Name != "banana" || group2.Address.ID != "abc" { - t.Fatalf("merge of meta1 into group2 failed") - } + assert.Equal(t, "banana", group2.Name, "merge of meta1 into group2 failed") + assert.Equal(t, "abc", group2.Address.ID, "merge of meta1 into group2 failed") } diff --git a/nip44/nip44_test.go b/nip44/nip44_test.go index f31084a..89f2fb6 100644 --- a/nip44/nip44_test.go +++ b/nip44/nip44_test.go @@ -47,7 +47,7 @@ func assertCryptPriv(t *testing.T, sk1 string, sk2 string, conversationKey strin assert.Equal(t, decrypted, plaintext, "wrong decryption") } -func assertDecryptFail(t *testing.T, conversationKey string, plaintext string, ciphertext string, msg string) { +func assertDecryptFail(t *testing.T, conversationKey string, _ string, ciphertext string, msg string) { var ( k1 []byte ok bool diff --git a/nip46/nip46_test.go b/nip46/nip46_test.go index eef3960..79184c9 100644 --- a/nip46/nip46_test.go +++ b/nip46/nip46_test.go @@ -1,21 +1,24 @@ package nip46 -import "testing" +import ( + "testing" + + "github.com/stretchr/testify/assert" +) func TestValidBunkerURL(t *testing.T) { - if !IsValidBunkerURL("bunker://3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d?relay=wss%3A%2F%2Frelay.damus.io&relay=wss%3A%2F%2Frelay.snort.social&relay=wss%3A%2F%2Frelay.nsecbunker.com") { - t.Fatalf("should be valid") - } - if IsValidBunkerURL("askjdbkajdbv") { - t.Fatalf("should be invalid") - } - if IsValidBunkerURL("asdjasbndksa@asjdnksa.com") { - t.Fatalf("should be invalid") - } - if IsValidBunkerURL("https://hello.com?relays=wss://xxxxxx.xxxx") { - t.Fatalf("should be invalid") - } - if IsValidBunkerURL("bunker://fa883d107ef9e558472c4eb9aaaefa459d?relay=wss%3A%2F%2Frelay.damus.io&relay=wss%3A%2F%2Frelay.snort.social&relay=wss%3A%2F%2Frelay.nsecbunker.com") { - t.Fatalf("should be invalid") - } + valid := IsValidBunkerURL("bunker://3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d?relay=wss%3A%2F%2Frelay.damus.io&relay=wss%3A%2F%2Frelay.snort.social&relay=wss%3A%2F%2Frelay.nsecbunker.com") + assert.True(t, valid, "should be valid") + + inValid := IsValidBunkerURL("askjdbkajdbv") + assert.False(t, inValid, "should be invalid") + + inValid1 := IsValidBunkerURL("asdjasbndksa@asjdnksa.com") + assert.False(t, inValid1, "should be invalid") + + inValid2 := IsValidBunkerURL("https://hello.com?relays=wss://xxxxxx.xxxx") + assert.False(t, inValid2, "should be invalid") + + inValid3 := IsValidBunkerURL("bunker://fa883d107ef9e558472c4eb9aaaefa459d?relay=wss%3A%2F%2Frelay.damus.io&relay=wss%3A%2F%2Frelay.snort.social&relay=wss%3A%2F%2Frelay.nsecbunker.com") + assert.False(t, inValid3, "should be invalid") } diff --git a/nip49/nip49_test.go b/nip49/nip49_test.go index 9e4fa7e..7f447b9 100644 --- a/nip49/nip49_test.go +++ b/nip49/nip49_test.go @@ -5,17 +5,15 @@ import ( "slices" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestDecryptKeyFromNIPText(t *testing.T) { ncrypt := "ncryptsec1qgg9947rlpvqu76pj5ecreduf9jxhselq2nae2kghhvd5g7dgjtcxfqtd67p9m0w57lspw8gsq6yphnm8623nsl8xn9j4jdzz84zm3frztj3z7s35vpzmqf6ksu8r89qk5z2zxfmu5gv8th8wclt0h4p" secretKey, err := Decrypt(ncrypt, "nostr") - if err != nil { - t.Fatalf("failed to decrypt: %s", err) - } - if secretKey != "3501454135014541350145413501453fefb02227e449e57cf4d3a3ce05378683" { - t.Fatalf("decrypted wrongly: %s", secretKey) - } + assert.NoError(t, err) + assert.Equal(t, "3501454135014541350145413501453fefb02227e449e57cf4d3a3ce05378683", secretKey) } func TestEncryptAndDecrypt(t *testing.T) { @@ -38,20 +36,13 @@ func TestEncryptAndDecrypt(t *testing.T) { {"ÅΩṩ", "11b25a101667dd9208db93c0827c6bdad66729a5b521156a7e9d3b22b3ae8944", 9, 0x01}, } { bech32code, err := Encrypt(f.secretkey, f.password, f.logn, f.ksb) - if err != nil { - t.Fatalf("failed to encrypt %d: %s", i, err) - } - if !strings.HasPrefix(bech32code, "ncryptsec1") || len(bech32code) != 162 { - t.Fatalf("bech32 code is wrong %d: %s", i, bech32code) - } + assert.NoError(t, err) + assert.True(t, strings.HasPrefix(bech32code, "ncryptsec1"), "bech32 code is wrong %d: %s", i, bech32code) + assert.Equal(t, 162, len(bech32code), "bech32 code is wrong %d: %s", i, bech32code) secretKey, err := Decrypt(bech32code, f.password) - if err != nil { - t.Fatalf("failed to decrypt %d: %s", i, err) - } - if secretKey != f.secretkey { - t.Fatalf("decrypted to the wrong value %d: %s", i, secretKey) - } + assert.NoError(t, err) + assert.Equal(t, f.secretkey, secretKey) } } @@ -62,13 +53,9 @@ func TestNormalization(t *testing.T) { key2, err2 := getKey(string([]byte{0xC3, 0x85, 0xCE, 0xA9, 0xE1, 0xB9, 0xA9}), nonce, n) key3, err3 := getKey("ÅΩẛ̣", nonce, n) key4, err4 := getKey("ÅΩẛ̣", nonce, n) - if merr := errors.Join(err1, err2, err3, err4); merr != nil { - t.Fatalf("getKey errored: %s", merr) - return - } - - if !slices.Equal(key1, key2) || !slices.Equal(key2, key3) || !slices.Equal(key3, key4) { - t.Fatalf("normalization failed") - return - } + err := errors.Join(err1, err2, err3, err4) + assert.NoError(t, err) + assert.True(t, slices.Equal(key1, key2), "normalization failed") + assert.True(t, slices.Equal(key2, key3), "normalization failed") + assert.True(t, slices.Equal(key3, key4), "normalization failed") } diff --git a/nip96/nip96_test.go b/nip96/nip96_test.go index 3b1c298..4d1c412 100644 --- a/nip96/nip96_test.go +++ b/nip96/nip96_test.go @@ -6,10 +6,14 @@ import ( "testing" "github.com/nbd-wtf/go-nostr" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestUpload(t *testing.T) { - img, _ := os.Open("./testdata/image.png") + img, err := os.Open("./testdata/image.png") + require.NoError(t, err) + defer img.Close() ctx := context.Background() @@ -24,9 +28,7 @@ func TestUpload(t *testing.T) { ContentType: "image/png", NoTransform: true, }) - if err != nil { - t.Fatal(err, "client.Upload") - } + assert.NoError(t, err) t.Logf("resp: %#v\n", *resp) // nip96_test.go:28: resp: nip96.UploadResponse{Status:"success", Message:"Upload successful.", ProcessingURL:"", Nip94Event:struct { Tags nostr.Tags "json:\"tags\"" }{Tags:nostr.Tags{nostr.Tag{"url", "https://image.nostr.build/4ece05f1d77c9cb97d334ba9c0301b2960640df89bf5d75d6bffadefc4355673.jpg"}, nostr.Tag{"ox", "4ece05f1d77c9cb97d334ba9c0301b2960640df89bf5d75d6bffadefc4355673"}, nostr.Tag{"x", ""}, nostr.Tag{"m", "image/jpeg"}, nostr.Tag{"dim", "1125x750"}, nostr.Tag{"bh", "LLF=kB-;yH-;-;R#t7xKEZWA#_oM"}, nostr.Tag{"blurhash", "LLF=kB-;yH-;-;R#t7xKEZWA#_oM"}, nostr.Tag{"thumb", "https://image.nostr.build/thumb/4ece05f1d77c9cb97d334ba9c0301b2960640df89bf5d75d6bffadefc4355673.jpg"}}}} diff --git a/normalize_test.go b/normalize_test.go index dc5a6b0..213fc14 100644 --- a/normalize_test.go +++ b/normalize_test.go @@ -2,6 +2,8 @@ package nostr import ( "testing" + + "github.com/stretchr/testify/assert" ) type urlTest struct { @@ -29,8 +31,7 @@ var urlTests = []urlTest{ func TestNormalizeURL(t *testing.T) { for _, test := range urlTests { - if output := NormalizeURL(test.url); output != test.expected { - t.Errorf("Output '%s' not equal to expected '%s'", output, test.expected) - } + output := NormalizeURL(test.url) + assert.Equal(t, test.expected, output) } } diff --git a/relay_test.go b/relay_test.go index 81a5446..50feab1 100644 --- a/relay_test.go +++ b/relay_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "errors" "io" "net/http" "net/http/httptest" @@ -12,6 +11,8 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "golang.org/x/net/websocket" ) @@ -25,9 +26,8 @@ func TestPublish(t *testing.T) { Tags: Tags{[]string{"foo", "bar"}}, PubKey: pub, } - if err := textNote.Sign(priv); err != nil { - t.Fatalf("textNote.Sign: %v", err) - } + err := textNote.Sign(priv) + assert.NoError(t, err) // fake relay server var mu sync.Mutex // guards published to satisfy go test -race @@ -38,30 +38,25 @@ func TestPublish(t *testing.T) { mu.Unlock() // verify the client sent exactly the textNote var raw []json.RawMessage - if err := websocket.JSON.Receive(conn, &raw); err != nil { - t.Errorf("websocket.JSON.Receive: %v", err) - } + err := websocket.JSON.Receive(conn, &raw) + assert.NoError(t, err) + event := parseEventMessage(t, raw) - if !bytes.Equal(event.Serialize(), textNote.Serialize()) { - t.Errorf("received event:\n%+v\nwant:\n%+v", event, textNote) - } + assert.True(t, bytes.Equal(event.Serialize(), textNote.Serialize())) + // send back an ok nip-20 command result res := []any{"OK", textNote.ID, true, ""} - if err := websocket.JSON.Send(conn, res); err != nil { - t.Errorf("websocket.JSON.Send: %v", err) - } + err = websocket.JSON.Send(conn, res) + assert.NoError(t, err) }) defer ws.Close() // connect a client and send the text note - rl := mustRelayConnect(ws.URL) - err := rl.Publish(context.Background(), textNote) - if err != nil { - t.Errorf("publish should have succeeded") - } - if !published { - t.Errorf("fake relay server saw no event") - } + rl := mustRelayConnect(t, ws.URL) + err = rl.Publish(context.Background(), textNote) + assert.NoError(t, err) + + assert.True(t, published, "fake relay server saw no event") } func TestPublishBlocked(t *testing.T) { @@ -73,9 +68,9 @@ func TestPublishBlocked(t *testing.T) { ws := newWebsocketServer(func(conn *websocket.Conn) { // discard received message; not interested var raw []json.RawMessage - if err := websocket.JSON.Receive(conn, &raw); err != nil { - t.Errorf("websocket.JSON.Receive: %v", err) - } + err := websocket.JSON.Receive(conn, &raw) + assert.NoError(t, err) + // send back a not ok nip-20 command result res := []any{"OK", textNote.ID, false, "blocked"} websocket.JSON.Send(conn, res) @@ -83,11 +78,9 @@ func TestPublishBlocked(t *testing.T) { defer ws.Close() // connect a client and send a text note - rl := mustRelayConnect(ws.URL) + rl := mustRelayConnect(t, ws.URL) err := rl.Publish(context.Background(), textNote) - if err == nil { - t.Errorf("should have failed to publish") - } + assert.NoError(t, err) } func TestPublishWriteFailed(t *testing.T) { @@ -103,13 +96,11 @@ func TestPublishWriteFailed(t *testing.T) { defer ws.Close() // connect a client and send a text note - rl := mustRelayConnect(ws.URL) + rl := mustRelayConnect(t, ws.URL) // Force brief period of time so that publish always fails on closed socket. time.Sleep(1 * time.Millisecond) err := rl.Publish(context.Background(), textNote) - if err == nil { - t.Errorf("should have failed to publish") - } + assert.NoError(t, err) } func TestConnectContext(t *testing.T) { @@ -128,16 +119,13 @@ func TestConnectContext(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() r, err := RelayConnect(ctx, ws.URL) - if err != nil { - t.Fatalf("RelayConnectContext: %v", err) - } + assert.NoError(t, err) + defer r.Close() mu.Lock() defer mu.Unlock() - if !connected { - t.Error("fake relay server saw no client connect") - } + assert.True(t, connected, "fake relay server saw no client connect") } func TestConnectContextCanceled(t *testing.T) { @@ -149,9 +137,7 @@ func TestConnectContextCanceled(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) cancel() // make ctx expired _, err := RelayConnect(ctx, ws.URL) - if !errors.Is(err, context.Canceled) { - t.Errorf("RelayConnectContext returned %v error; want context.Canceled", err) - } + assert.ErrorIs(t, err, context.Canceled) } func TestConnectWithOrigin(t *testing.T) { @@ -166,9 +152,7 @@ func TestConnectWithOrigin(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() err := r.Connect(ctx) - if err != nil { - t.Errorf("unexpected error: %v", err) - } + assert.NoError(t, err) } func discardingHandler(conn *websocket.Conn) { @@ -191,59 +175,60 @@ var anyOriginHandshake = func(conf *websocket.Config, r *http.Request) error { func makeKeyPair(t *testing.T) (priv, pub string) { t.Helper() + privkey := GeneratePrivateKey() pubkey, err := GetPublicKey(privkey) - if err != nil { - t.Fatalf("GetPublicKey(%q): %v", privkey, err) - } + assert.NoError(t, err) + return privkey, pubkey } -func mustRelayConnect(url string) *Relay { +func mustRelayConnect(t *testing.T, url string) *Relay { + t.Helper() + rl, err := RelayConnect(context.Background(), url) - if err != nil { - panic(err.Error()) - } + require.NoError(t, err) + return rl } func parseEventMessage(t *testing.T, raw []json.RawMessage) Event { t.Helper() - if len(raw) < 2 { - t.Fatalf("len(raw) = %d; want at least 2", len(raw)) - } + + assert.Greater(t, len(raw), 2) + var typ string - json.Unmarshal(raw[0], &typ) - if typ != "EVENT" { - t.Errorf("typ = %q; want EVENT", typ) - } + err := json.Unmarshal(raw[0], &typ) + assert.NoError(t, err) + assert.Equal(t, "EVENT", typ) + var event Event - if err := json.Unmarshal(raw[1], &event); err != nil { - t.Errorf("json.Unmarshal(`%s`): %v", string(raw[1]), err) - } + err = json.Unmarshal(raw[1], &event) + require.NoError(t, err) + return event } func parseSubscriptionMessage(t *testing.T, raw []json.RawMessage) (subid string, filters []Filter) { t.Helper() - if len(raw) < 3 { - t.Fatalf("len(raw) = %d; want at least 3", len(raw)) - } + + assert.Greater(t, len(raw), 3) + var typ string - json.Unmarshal(raw[0], &typ) - if typ != "REQ" { - t.Errorf("typ = %q; want REQ", typ) - } + err := json.Unmarshal(raw[0], &typ) + + assert.NoError(t, err) + assert.Equal(t, "REQ", typ) + var id string - if err := json.Unmarshal(raw[1], &id); err != nil { - t.Errorf("json.Unmarshal sub id: %v", err) - } + err = json.Unmarshal(raw[1], &id) + assert.NoError(t, err) + var ff []Filter - for i, b := range raw[2:] { + for _, b := range raw[2:] { var f Filter - if err := json.Unmarshal(b, &f); err != nil { - t.Errorf("json.Unmarshal filter %d: %v", i, err) - } + err := json.Unmarshal(b, &f) + assert.NoError(t, err) ff = append(ff, f) } return id, ff diff --git a/subscription_test.go b/subscription_test.go index c8bd4c9..7aefbb9 100644 --- a/subscription_test.go +++ b/subscription_test.go @@ -6,20 +6,19 @@ import ( "sync/atomic" "testing" "time" + + "github.com/stretchr/testify/assert" ) const RELAY = "wss://nos.lol" // test if we can fetch a couple of random events func TestSubscribeBasic(t *testing.T) { - rl := mustRelayConnect(RELAY) + rl := mustRelayConnect(t, RELAY) defer rl.Close() sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{KindTextNote}, Limit: 2}}) - if err != nil { - t.Fatalf("subscription failed: %v", err) - return - } + assert.NoError(t, err) timeout := time.After(5 * time.Second) n := 0 @@ -27,9 +26,7 @@ func TestSubscribeBasic(t *testing.T) { for { select { case event := <-sub.Events: - if event == nil { - t.Fatalf("event is nil: %v", event) - } + assert.NotNil(t, event) n++ case <-sub.EndOfStoredEvents: goto end @@ -43,34 +40,26 @@ func TestSubscribeBasic(t *testing.T) { } end: - if n != 2 { - t.Fatalf("expected 2 events, got %d", n) - } + assert.Equal(t, 2, n) } // test if we can do multiple nested subscriptions func TestNestedSubscriptions(t *testing.T) { - rl := mustRelayConnect(RELAY) + rl := mustRelayConnect(t, RELAY) defer rl.Close() n := atomic.Uint32{} // fetch 2 replies to a note sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{KindTextNote}, Tags: TagMap{"e": []string{"0e34a74f8547e3b95d52a2543719b109fd0312aba144e2ef95cba043f42fe8c5"}}, Limit: 3}}) - if err != nil { - t.Fatalf("subscription 1 failed: %v", err) - return - } + assert.NoError(t, err) for { select { case event := <-sub.Events: // now fetch author of this sub, err := rl.Subscribe(context.Background(), Filters{{Kinds: []int{KindProfileMetadata}, Authors: []string{event.PubKey}, Limit: 1}}) - if err != nil { - t.Fatalf("subscription 2 failed: %v", err) - return - } + assert.NoError(t, err) for { select { diff --git a/tag_test.go b/tag_test.go index 40250ef..ddb00d0 100644 --- a/tag_test.go +++ b/tag_test.go @@ -2,6 +2,8 @@ package nostr import ( "testing" + + "github.com/stretchr/testify/assert" ) func TestTagHelpers(t *testing.T) { @@ -13,36 +15,14 @@ func TestTagHelpers(t *testing.T) { Tag{"e", "ffffff"}, } - if tags.GetFirst([]string{"x"}) == nil { - t.Error("failed to get existing prefix") - } - if tags.GetFirst([]string{"x", ""}) != nil { - t.Error("got with wrong prefix") - } - if tags.GetFirst([]string{"p", "abcdef", "wss://"}) == nil { - t.Error("failed to get with existing prefix") - } - if tags.GetFirst([]string{"p", "abcdef", ""}) == nil { - t.Error("failed to get with existing prefix (blank last string)") - } - if (*(tags.GetLast([]string{"e"})))[1] != "ffffff" { - t.Error("failed to get last") - } - - if len(tags.GetAll([]string{"e", ""})) != 2 { - t.Error("failed to get all") - } - - if len(tags.AppendUnique(Tag{"e", "ffffff"})) != 5 { - t.Error("append unique changed the array size when existed") - } - if len(tags.AppendUnique(Tag{"e", "bbbbbb"})) != 6 { - t.Error("append unique failed to append when didn't exist") - } - if tags.AppendUnique(Tag{"e", "eeeeee"})[4][1] != "ffffff" { - t.Error("append unique changed the order") - } - if tags.AppendUnique(Tag{"e", "eeeeee"})[3][1] != "eeeeee" { - t.Error("append unique changed the order") - } + assert.NotNil(t, tags.GetFirst([]string{"x"}), "failed to get existing prefix") + assert.Nil(t, tags.GetFirst([]string{"x", ""}), "got with wrong prefix") + assert.NotNil(t, tags.GetFirst([]string{"p", "abcdef", "wss://"}), "failed to get with existing prefix") + assert.NotNil(t, tags.GetFirst([]string{"p", "abcdef", ""}), "failed to get with existing prefix (blank last string)") + assert.Equal(t, "ffffff", (*(tags.GetLast([]string{"e"})))[1], "failed to get last") + assert.Equal(t, 2, len(tags.GetAll([]string{"e", ""})), "failed to get all") + assert.Equal(t, 5, len(tags.AppendUnique(Tag{"e", "ffffff"})), "append unique changed the array size when existed") + assert.Equal(t, 6, len(tags.AppendUnique(Tag{"e", "bbbbbb"})), "append unique failed to append when didn't exist") + assert.Equal(t, "ffffff", tags.AppendUnique(Tag{"e", "eeeeee"})[4][1], "append unique changed the order") + assert.Equal(t, "eeeeee", tags.AppendUnique(Tag{"e", "eeeeee"})[3][1], "append unique changed the order") } diff --git a/utils_test.go b/utils_test.go index 6c39f8d..89e4056 100644 --- a/utils_test.go +++ b/utils_test.go @@ -2,6 +2,8 @@ package nostr import ( "testing" + + "github.com/stretchr/testify/assert" ) func TestIsValidRelayURL(t *testing.T) { @@ -15,14 +17,10 @@ func TestIsValidRelayURL(t *testing.T) { {"wss://relay.nostr.com", true}, {"http://127.0.0.1", false}, {"127.0.0.1", false}, - //{"wss://relay.nostr.com'", false}, - //{"wss://relay.nostr.com'hiphop", true}, } for _, test := range tests { got := IsValidRelayURL(test.u) - if got != test.want { - t.Errorf("IsValidRelayURL want %v for %q but got %v", test.want, test.u, got) - } + assert.Equal(t, test.want, got) } }