Compare commits

...

2 Commits

Author SHA1 Message Date
c16ee76638 add error message for failed fetch 2025-07-22 14:55:09 +01:00
132fdc9f36 Add test for NostrEscape and NostrUnescape handling in JSON tags
- Introduced a new test case `ExplicitlyEscapedJSON` in `json_tags_test.go`
- Validates the behavior of `NostrEscape` and `NostrUnescape` with explicitly escaped JSON
- Ensures proper marshaling, unmarshaling, and verification of nested and special characters in JSON tags
2025-07-22 13:16:09 +01:00
2 changed files with 59 additions and 1 deletions

View File

@@ -8,6 +8,7 @@ import (
"orly.dev/pkg/encoders/codecbuf"
"orly.dev/pkg/interfaces/store"
"orly.dev/pkg/utils/chk"
"orly.dev/pkg/utils/errorf"
)
func (d *D) GetFullIdPubkeyBySerial(ser *types.Uint40) (
@@ -53,5 +54,11 @@ func (d *D) GetFullIdPubkeyBySerial(ser *types.Uint40) (
); chk.E(err) {
return
}
if fidpk != nil {
err = errorf.E(
"failed to fetch full id pubkey by serial %d",
ser.Get(),
)
}
return
}

View File

@@ -5,6 +5,7 @@ import (
"orly.dev/pkg/encoders/kind"
"orly.dev/pkg/encoders/tag"
"orly.dev/pkg/encoders/tags"
text2 "orly.dev/pkg/encoders/text"
"orly.dev/pkg/encoders/timestamp"
"testing"
)
@@ -142,7 +143,7 @@ func TestUnmarshalEscapedJSONInTags(t *testing.T) {
}
})
// Test 3: Tag with nested JSON that contains already escaped content
// Test 3: Tag with nested JSON that contains already escaped content
t.Run("NestedEscapedJSON", func(t *testing.T) {
// JSON with already escaped content
jsonContent := `{"escaped":"This JSON contains \\\"already escaped\\\" content"}`
@@ -178,6 +179,56 @@ func TestUnmarshalEscapedJSONInTags(t *testing.T) {
t.Errorf("Expected tag value '%s', got '%s'", jsonContent, unmarshaledTag.B(1))
}
})
// Test 4: Tag with JSON that has been explicitly escaped using NostrEscape
t.Run("ExplicitlyEscapedJSON", func(t *testing.T) {
// Original JSON with characters that need escaping
originalJSON := []byte(`{"key":"value with "quotes"","nested":{"array":[1,2,3],"special":"\n\r\t"}}`)
// Explicitly escape the JSON using NostrEscape
escapedJSON := make([]byte, 0, len(originalJSON)*2)
escapedJSON = text2.NostrEscape(escapedJSON, originalJSON)
// Create the event with the tag containing explicitly escaped JSON
originalEvent := &E{
Id: bytes.Repeat([]byte{0x01}, 32),
Pubkey: bytes.Repeat([]byte{0x02}, 32),
CreatedAt: timestamp.FromUnix(1609459200),
Kind: kind.TextNote,
Tags: tags.New(),
Content: []byte("Event with explicitly escaped JSON in tag"),
Sig: bytes.Repeat([]byte{0x03}, 64),
}
// Add a tag with the explicitly escaped JSON content
jsonTag := tag.New("j", string(escapedJSON))
originalEvent.Tags.AppendTags(jsonTag)
// Marshal the event
marshaled := originalEvent.Marshal(nil)
// Unmarshal back into a new event
unmarshaledEvent := &E{}
_, err := unmarshaledEvent.Unmarshal(marshaled)
if err != nil {
t.Fatalf("Failed to unmarshal event with explicitly escaped JSON: %v", err)
}
// Verify the tag was correctly unmarshaled
unmarshaledTag := unmarshaledEvent.Tags.GetTagElement(0)
if string(unmarshaledTag.B(1)) != string(escapedJSON) {
t.Errorf("Expected tag value '%s', got '%s'", string(escapedJSON), unmarshaledTag.B(1))
}
// Unescape the unmarshaled JSON to verify it matches the original
unescapedJSON := make([]byte, len(unmarshaledTag.B(1)))
copy(unescapedJSON, unmarshaledTag.B(1))
unescapedJSON = text2.NostrUnescape(unescapedJSON)
if string(unescapedJSON) != string(originalJSON) {
t.Errorf("Unescaped JSON doesn't match original. Expected '%s', got '%s'", string(originalJSON), string(unescapedJSON))
}
})
}
func TestUnmarshalTags(t *testing.T) {