Refactor event encoder to handle nil tags gracefully and optimize buffer allocation; update tests for improved coverage and data consistency.

This commit is contained in:
2025-08-22 21:47:19 +01:00
parent bf178eae4e
commit 225f949540
2 changed files with 35 additions and 23 deletions

View File

@@ -1,10 +1,10 @@
package event
import (
"encoding/json"
"testing"
"time"
"github.com/pkg/profile"
"lol.mleku.dev/chk"
"lukechampine.com/frand"
"next.orly.dev/pkg/encoders/hex"
@@ -13,11 +13,8 @@ import (
"next.orly.dev/pkg/utils/bufpool"
)
func TestMarshalJSON(t *testing.T) {
// lol.SetLogLevel("trace")
prof := profile.Start(profile.MemProfile)
defer prof.Stop()
for range 1000000 {
func TestMarshalJSONUnmarshalJSON(t *testing.T) {
for range 10000 {
ev := New()
ev.ID = frand.Bytes(32)
ev.Pubkey = frand.Bytes(32)
@@ -32,18 +29,23 @@ func TestMarshalJSON(t *testing.T) {
},
},
}
ev.Content = frand.Bytes(frand.Intn(1024) + 1)
ev.Content = []byte(`some text content
with line breaks and tabs and other stuff
`)
ev.Sig = frand.Bytes(64)
// log.I.S(ev)
b, err := ev.MarshalJSON()
if b, err = json.Marshal(ev); chk.E(err) {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}
var bc []byte
bc = append(bc, b...)
// log.I.F("%s", bc)
ev2 := New()
if err = ev2.UnmarshalJSON(b); chk.E(err) {
if err = json.Unmarshal(b, ev2); chk.E(err) {
t.Fatal(err)
}
var b2 []byte
@@ -61,3 +63,7 @@ func TestMarshalJSON(t *testing.T) {
bufpool.PutBytes(bc)
}
}
func TestExamplesCache(t *testing.T) {
}