53 lines
1.9 KiB
Go
53 lines
1.9 KiB
Go
package events
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/stretchr/testify/assert"
|
|
"testing"
|
|
)
|
|
|
|
func TestUnmarshalEventJSON(t *testing.T) {
|
|
event := NewEvent()
|
|
json.Unmarshal(testEventJSONBytes, &event)
|
|
if err := Validate(event); err != nil {
|
|
t.Error("unmarshalled event is invalid")
|
|
}
|
|
expectEqualEvents(t, event, testEvent)
|
|
}
|
|
|
|
func TestMarshalEventJSON(t *testing.T) {
|
|
eventJSONBytes, err := json.Marshal(testEvent)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, testEventJSON, string(eventJSONBytes))
|
|
}
|
|
|
|
func TestEventJSONRoundTrip(t *testing.T) {
|
|
event := NewEvent(
|
|
WithID("86e856d0527dd08527498cd8afd8a7d296bde37e4757a8921f034f0b344df3ad"),
|
|
WithPubKey(testEvent.PubKey),
|
|
WithCreatedAt(testEvent.CreatedAt),
|
|
WithKind(testEvent.Kind),
|
|
WithTag(Tag{"a", "value"}),
|
|
WithTag(Tag{"b", "value", "optional"}),
|
|
WithTag(Tag{"name", "value", "optional", "optional"}),
|
|
WithContent(testEvent.Content),
|
|
WithSig("c05fe02a9c082ff56aad2b16b5347498a21665f02f050ba086dbe6bd593c8cd448505d2831d1c0340acc1793eaf89b7c0cb21bb696c71da6b8d6b857702bb557"),
|
|
)
|
|
expectedJSON := `{"id":"86e856d0527dd08527498cd8afd8a7d296bde37e4757a8921f034f0b344df3ad","pubkey":"cfa87f35acbde29ba1ab3ee42de527b2cad33ac487e80cf2d6405ea0042c8fef","created_at":1760740551,"kind":1,"tags":[["a","value"],["b","value","optional"],["name","value","optional","optional"]],"content":"hello world","sig":"c05fe02a9c082ff56aad2b16b5347498a21665f02f050ba086dbe6bd593c8cd448505d2831d1c0340acc1793eaf89b7c0cb21bb696c71da6b8d6b857702bb557"}`
|
|
|
|
if err := Validate(event); err != nil {
|
|
t.Error("test event is invalid")
|
|
}
|
|
eventJSON, err := json.Marshal(event)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, expectedJSON, string(eventJSON))
|
|
|
|
unmarshalledEvent := Event{}
|
|
json.Unmarshal(eventJSON, &unmarshalledEvent)
|
|
|
|
if err := Validate(unmarshalledEvent); err != nil {
|
|
t.Error("unmarshalled event is invalid")
|
|
}
|
|
expectEqualEvents(t, unmarshalledEvent, event)
|
|
}
|