Update Matches to accept ValidatedEvent; regenerate test fixtures with valid signatures

This commit is contained in:
Jay
2026-05-22 11:41:04 -04:00
parent 17789a7dbd
commit d699feb236
3 changed files with 49 additions and 54 deletions
+7 -6
View File
@@ -249,36 +249,37 @@ func (f *Filter) UnmarshalJSON(data []byte) error {
// Matches returns true if the event satisfies all filter conditions.
// Supports prefix matching for IDs and authors, and tag filtering.
// Does not account for custom extensions.
func Matches(f Filter, event events.Event) bool {
func Matches(f Filter, event events.ValidatedEvent) bool {
// Check ID
if len(f.IDs) > 0 {
if !matchesPrefix(event.ID, f.IDs) {
if !matchesPrefix(event.ID(), f.IDs) {
return false
}
}
// Check Author
if len(f.Authors) > 0 {
if !matchesPrefix(event.PubKey, f.Authors) {
if !matchesPrefix(event.PubKey(), f.Authors) {
return false
}
}
// Check Kind
if len(f.Kinds) > 0 {
if !matchesKinds(event.Kind, f.Kinds) {
if !matchesKinds(event.Kind(), f.Kinds) {
return false
}
}
// Check Timestamp
if !matchesTimeRange(event.CreatedAt, f.Since, f.Until) {
if !matchesTimeRange(event.CreatedAt(), f.Since, f.Until) {
return false
}
// Check Tags
if len(f.Tags) > 0 {
if !matchesTags(event.Tags, &f.Tags) {
tags := event.Tags()
if !matchesTags(tags, &f.Tags) {
return false
}
}
+27 -33
View File
@@ -8,16 +8,24 @@ import (
"testing"
)
var testEvents []events.Event
var testEvents []events.ValidatedEvent
func init() {
data, err := os.ReadFile("testdata/test_events.json")
if err != nil {
panic(err)
}
if err := json.Unmarshal(data, &testEvents); err != nil {
var raw []events.Event
if err := json.Unmarshal(data, &raw); err != nil {
panic(err)
}
for _, e := range raw {
ve, err := events.NewValidatedEvent(e)
if err != nil {
panic(err)
}
testEvents = append(testEvents, ve)
}
}
// Test keypairs corresponding to test events, for reference.
@@ -42,7 +50,7 @@ var filterTestCases = []FilterTestCase{
filter: NewFilter(),
expectedIDs: []string{
"e751d41f",
"562bc378",
"2e06c187",
"e67fa7b8",
"5e4c64f1",
"7a5d83d4",
@@ -58,7 +66,7 @@ var filterTestCases = []FilterTestCase{
filter: NewFilter(WithIDs([]string{})),
expectedIDs: []string{
"e751d41f",
"562bc378",
"2e06c187",
"e67fa7b8",
"5e4c64f1",
"7a5d83d4",
@@ -86,8 +94,8 @@ var filterTestCases = []FilterTestCase{
{
name: "multiple id prefixes",
filter: NewFilter(WithIDs([]string{"562bc378", "5e4c64f1"})),
expectedIDs: []string{"562bc378", "5e4c64f1"},
filter: NewFilter(WithIDs([]string{"2e06c187", "5e4c64f1"})),
expectedIDs: []string{"2e06c187", "5e4c64f1"},
},
{
@@ -101,7 +109,7 @@ var filterTestCases = []FilterTestCase{
filter: NewFilter(WithAuthors([]string{})),
expectedIDs: []string{
"e751d41f",
"562bc378",
"2e06c187",
"e67fa7b8",
"5e4c64f1",
"7a5d83d4",
@@ -115,7 +123,7 @@ var filterTestCases = []FilterTestCase{
{
name: "single author prefix",
filter: NewFilter(WithAuthors([]string{"d877e187"})),
expectedIDs: []string{"e751d41f", "562bc378", "e67fa7b8"},
expectedIDs: []string{"e751d41f", "2e06c187", "e67fa7b8"},
},
{
@@ -123,7 +131,7 @@ var filterTestCases = []FilterTestCase{
filter: NewFilter(WithAuthors([]string{"d877e187", "9e4b726a"})),
expectedIDs: []string{
"e751d41f",
"562bc378",
"2e06c187",
"e67fa7b8",
"5e4c64f1",
"7a5d83d4",
@@ -137,7 +145,7 @@ var filterTestCases = []FilterTestCase{
WithAuthors([]string{
"d877e187934bd942a71221b50ff2b426bd0777991b41b6c749119805dc40bcbe"}),
),
expectedIDs: []string{"e751d41f", "562bc378", "e67fa7b8"},
expectedIDs: []string{"e751d41f", "2e06c187", "e67fa7b8"},
},
{
@@ -151,7 +159,7 @@ var filterTestCases = []FilterTestCase{
filter: NewFilter(WithKinds([]int{})),
expectedIDs: []string{
"e751d41f",
"562bc378",
"2e06c187",
"e67fa7b8",
"5e4c64f1",
"7a5d83d4",
@@ -165,7 +173,7 @@ var filterTestCases = []FilterTestCase{
{
name: "single kind",
filter: NewFilter(WithKinds([]int{1})),
expectedIDs: []string{"562bc378", "7a5d83d4", "4b03b69a"},
expectedIDs: []string{"2e06c187", "7a5d83d4", "4b03b69a"},
},
{
@@ -204,7 +212,7 @@ var filterTestCases = []FilterTestCase{
filter: NewFilter(WithUntil(3000)),
expectedIDs: []string{
"e751d41f",
"562bc378",
"2e06c187",
"e67fa7b8",
},
},
@@ -230,7 +238,7 @@ var filterTestCases = []FilterTestCase{
filter: NewFilter(WithTag("e", []string{})),
expectedIDs: []string{
"e751d41f",
"562bc378",
"2e06c187",
"e67fa7b8",
"5e4c64f1",
"7a5d83d4",
@@ -247,7 +255,7 @@ var filterTestCases = []FilterTestCase{
WithTag("e", []string{
"5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"}),
),
expectedIDs: []string{"562bc378"},
expectedIDs: []string{"2e06c187"},
},
{
@@ -258,7 +266,7 @@ var filterTestCases = []FilterTestCase{
"ae3f2a91b6c3d8f7e9a1c5b4d8f2e7a9b6c3d8f7e9a1c5b4d8f2e7a9b6c3d8f7",
}),
),
expectedIDs: []string{"562bc378", "3a122100"},
expectedIDs: []string{"2e06c187", "3a122100"},
},
{
@@ -269,7 +277,7 @@ var filterTestCases = []FilterTestCase{
"cb7787c460a79187d6a13e75a0f19240e05fafca8ea42288f5765773ea69cf2f",
}),
),
expectedIDs: []string{"562bc378"},
expectedIDs: []string{"2e06c187"},
},
{
@@ -323,7 +331,7 @@ var filterTestCases = []FilterTestCase{
WithKinds([]int{1, 2}),
),
expectedIDs: []string{
"562bc378",
"2e06c187",
"e67fa7b8",
},
},
@@ -373,7 +381,7 @@ func TestEventFilterMatching(t *testing.T) {
actualIDs := []string{}
for _, event := range testEvents {
if Matches(tc.filter, event) {
actualIDs = append(actualIDs, event.ID[:8])
actualIDs = append(actualIDs, event.ID()[:8])
}
}
@@ -381,17 +389,3 @@ func TestEventFilterMatching(t *testing.T) {
})
}
}
// TestEventFilterMatchingSkipMalformedTags documents that filter.Matches()
// skips malformed tags during tag matching
func TestEventFilterMatchingSkipMalformedTags(t *testing.T) {
event := events.NewEvent(
events.WithTag(events.Tag{"malformed"}),
events.WithTag(events.Tag{"valid", "value"}),
)
filter := NewFilter(
WithTag("valid", []string{"value"}),
)
assert.True(t, Matches(filter, event))
}
+15 -15
View File
@@ -1,18 +1,18 @@
[
{
"kind": 0,
"id": "e751d41fa31e3a115634b41fb587cbd8270d10333a6d5330b1de24737448de70",
"pubkey": "d877e187934bd942a71221b50ff2b426bd0777991b41b6c749119805dc40bcbe",
"created_at": 1000,
"tags": [],
"kind": 0,
"tags": null,
"content": "Nayru profile",
"sig": "b3ba1ef2b4143e8c2fabc66bfd26839d6f3a14b5f8d24a8b96ce9c1aa41a53536444be61ed3e502cbeb04d34f8b893c84fa40bac408878c57ee4054d629c1452"
},
{
"kind": 1,
"id": "562bc378fc1a254b053b0cc1b8d61afec8e931ba79f0110ba9dd617496260758",
"id": "2e06c18793abeb368ffadecce7de8a3c5a50857907c91dc8b9f6b5f2f7b44a28",
"pubkey": "d877e187934bd942a71221b50ff2b426bd0777991b41b6c749119805dc40bcbe",
"created_at": 2000,
"kind": 1,
"tags": [
[
"e",
@@ -24,13 +24,13 @@
]
],
"content": "Hello from Nayru",
"sig": "18e48bf6be4e4104f95bfe90bd61e33c3d8cc5bf3e776ba8182fafe3f84b2e4ef6ce10256865cce556016e1b14ebad3079d3d0a3afcb0f690f12fa01e8f64201"
"sig": "5d90570e0dbf3cd8a9c44c5b25b03cf0005d9d3d79d17b43de39144343858b1a21cef22f07ac95ba9770ec39a87f428d860521ee9b7e1b33b973947382bed5bb"
},
{
"kind": 2,
"id": "e67fa7b84df6b0bb4c57f8719149de77f58955d7849da1be10b2267c72daad8b",
"pubkey": "d877e187934bd942a71221b50ff2b426bd0777991b41b6c749119805dc40bcbe",
"created_at": 3000,
"kind": 2,
"tags": [
[
"emoji",
@@ -45,10 +45,10 @@
"sig": "00e2c74374670b7623b793ddf4e9903ace17be621bbad74b808232eec1473271fa3e3d5e4ad01100f6c48bf36baa4e4dbaa012cd5ff060b644caac4e9a9c6b1e"
},
{
"kind": 0,
"id": "5e4c64f15a1ad510409e5cb3dc519dcde5416fbb8621bf65559f6b98f729a0d4",
"pubkey": "9e4b726ab0f25af580bdd2fd504fb245cf604f1fbc2482b89cf74beb4fb3aca9",
"created_at": 4000,
"kind": 0,
"tags": [
[
"website",
@@ -59,19 +59,19 @@
"sig": "6998b03fba4787ca6a44c4042143592bb9670ded905c06c1b258a7c1630666d7b033b7f5586f7a64ed92e912b555193112e8a590326f38809c46fe104907823e"
},
{
"kind": 1,
"id": "7a5d83d475576963f81e21d67208d6cf90c42b6a0c3a642c100a3571c5c96b68",
"pubkey": "9e4b726ab0f25af580bdd2fd504fb245cf604f1fbc2482b89cf74beb4fb3aca9",
"created_at": 5000,
"tags": [],
"kind": 1,
"tags": null,
"content": "Farore's message",
"sig": "e9f4986264c7eb7800b7a7d0e0de2928242cb4e93f8ba099fc1564b893dd7a77d2277dc3e8b67724c3887ccadbf14a656c80a229107eb2b5a44a20a00bc436d6"
},
{
"kind": 2,
"id": "3a122100196b065ec6c5e1e75dd5140eeb292ef96d2acd56354eb8c23c47649a",
"pubkey": "9e4b726ab0f25af580bdd2fd504fb245cf604f1fbc2482b89cf74beb4fb3aca9",
"created_at": 6000,
"kind": 2,
"tags": [
[
"category",
@@ -91,10 +91,10 @@
"sig": "1f5ccdd14b1313a39b6fabfc85a3535ba4f10ad99067803804c9478d63ef2cf53723fcee7041fcbaad4f846d4500183e92305d59b3e6ccb504ce291ad7f982e2"
},
{
"kind": 0,
"id": "4a15d963de8d26e8c4377e17fcf6daec499c454338951716a7d14cae1f7be835",
"pubkey": "e719e8f83b77a9efacb29fd19118b030cbf7cfbca1f8d3694235707ee213abc7",
"created_at": 7000,
"kind": 0,
"tags": [
[
"location",
@@ -109,10 +109,10 @@
"sig": "5a731404105aee9a04bd4d05024cb994a8d500edfceaeb83773438a70d376e6bb638e82e70380558f66aa078ab01f5c4ca86d8c37d291aafb7e33da053c856a9"
},
{
"kind": 1,
"id": "4b03b69a7e89796e1021ad3b7f914e6868a6e900b5e6edfa09d9019a05898ed3",
"pubkey": "e719e8f83b77a9efacb29fd19118b030cbf7cfbca1f8d3694235707ee213abc7",
"created_at": 8000,
"kind": 1,
"tags": [
[
"e",
@@ -123,12 +123,12 @@
"sig": "7330fd35e0be4a2a64a940a2841474f60b15d5dec9d4c4129905d97bd91cc8e6a97eec66091580b7351a807b7c250544cf500d0e2d47f5744387b1ce4ac49c4d"
},
{
"kind": 2,
"id": "d39e6f3f593bd754a45a6e2f77b1b0669cdfe89c19fb2a4b252ea095caa9874b",
"pubkey": "e719e8f83b77a9efacb29fd19118b030cbf7cfbca1f8d3694235707ee213abc7",
"created_at": 9000,
"tags": [],
"kind": 2,
"tags": null,
"content": "Final event",
"sig": "917d3fa8111cd9dfdc9acad121e7f71e4358d6a4eb0979eadc744b55f78d2647bf839282f6c10afacd64798007c3ef09b8a925c9b73f97c5219098eca1bacc4d"
}
]
]