Phase 2: rewrite subgraph_test.go with fixtures; add replaceable event tests

This commit is contained in:
Jay
2026-05-22 16:08:37 -04:00
parent 4a0ccb9abb
commit 94cfb35fb2
3 changed files with 82 additions and 94 deletions
+75 -82
View File
@@ -8,31 +8,10 @@ import (
"testing" "testing"
) )
var ids = map[string]string{ func baseSubgraph(e roots.ValidatedEvent) (*EventSubgraph, *Node, *Node) {
"a": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"b": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
"c": "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
"d": "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd",
}
var static = roots.NewEvent(
roots.WithCreatedAt(1000),
roots.WithKind(1),
roots.WithContent("hello"),
)
func newFullEventNode(id string, createdAt int64, kind int, content string) *Node {
n := NewEventNode(id)
n.Props["created_at"] = createdAt
n.Props["kind"] = kind
n.Props["content"] = content
return n
}
func baseSubgraph(eventID, pubkey string) (*EventSubgraph, *Node, *Node) {
s := NewEventSubgraph() s := NewEventSubgraph()
eventNode := newFullEventNode(eventID, static.CreatedAt, static.Kind, static.Content) eventNode := newEventNode(e.ID(), e.CreatedAt(), e.Kind(), e.Content())
userNode := NewUserNode(pubkey) userNode := NewUserNode(e.PubKey())
s.AddNode(eventNode) s.AddNode(eventNode)
s.AddNode(userNode) s.AddNode(userNode)
s.AddRel(NewSignedRel(userNode, eventNode, nil)) s.AddRel(NewSignedRel(userNode, eventNode, nil))
@@ -40,31 +19,28 @@ func baseSubgraph(eventID, pubkey string) (*EventSubgraph, *Node, *Node) {
} }
func TestEventToSubgraph(t *testing.T) { func TestEventToSubgraph(t *testing.T) {
fx := LoadFixtures(t)
cases := []struct { cases := []struct {
name string name string
event roots.Event event roots.ValidatedEvent
expected *EventSubgraph expected *EventSubgraph
}{ }{
{ {
name: "bare event", name: "bare event",
event: roots.Event{ event: fx.ValidatedEvent(t, "bare"),
ID: ids["a"], PubKey: ids["b"],
CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content,
},
expected: func() *EventSubgraph { expected: func() *EventSubgraph {
s, _, _ := baseSubgraph(ids["a"], ids["b"]) e := fx.ValidatedEvent(t, "bare")
s, _, _ := baseSubgraph(e)
return s return s
}(), }(),
}, },
{ {
name: "single generic tag", name: "single generic tag",
event: roots.Event{ event: fx.ValidatedEvent(t, "generic_tag"),
ID: ids["a"], PubKey: ids["b"],
CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content,
Tags: []roots.Tag{{"t", "bitcoin"}},
},
expected: func() *EventSubgraph { expected: func() *EventSubgraph {
s, eventNode, _ := baseSubgraph(ids["a"], ids["b"]) e := fx.ValidatedEvent(t, "generic_tag")
s, eventNode, _ := baseSubgraph(e)
tagNode := NewTagNode("t", "bitcoin") tagNode := NewTagNode("t", "bitcoin")
s.AddNode(tagNode) s.AddNode(tagNode)
s.AddRel(NewTaggedRel(eventNode, tagNode, nil)) s.AddRel(NewTaggedRel(eventNode, tagNode, nil))
@@ -72,28 +48,14 @@ func TestEventToSubgraph(t *testing.T) {
}(), }(),
}, },
{ {
name: "tag with fewer than 2 elements", name: "e tag with valid hex64",
event: roots.Event{ event: fx.ValidatedEvent(t, "e_tag_valid"),
ID: ids["a"], PubKey: ids["b"],
CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content,
Tags: []roots.Tag{{"t"}},
},
expected: func() *EventSubgraph { expected: func() *EventSubgraph {
s, _, _ := baseSubgraph(ids["a"], ids["b"]) e := fx.ValidatedEvent(t, "e_tag_valid")
return s carolID := fx.ValidatedEvent(t, "carol_placeholder").ID()
}(), s, eventNode, _ := baseSubgraph(e)
}, tagNode := NewTagNode("e", carolID)
{ referencedEvent := NewEventNode(carolID)
name: "e tag with valid hex64",
event: roots.Event{
ID: ids["a"], PubKey: ids["b"],
CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content,
Tags: []roots.Tag{{"e", ids["c"]}},
},
expected: func() *EventSubgraph {
s, eventNode, _ := baseSubgraph(ids["a"], ids["b"])
tagNode := NewTagNode("e", ids["c"])
referencedEvent := NewEventNode(ids["c"])
s.AddNode(tagNode) s.AddNode(tagNode)
s.AddNode(referencedEvent) s.AddNode(referencedEvent)
s.AddRel(NewTaggedRel(eventNode, tagNode, nil)) s.AddRel(NewTaggedRel(eventNode, tagNode, nil))
@@ -102,14 +64,11 @@ func TestEventToSubgraph(t *testing.T) {
}(), }(),
}, },
{ {
name: "e tag with invalid value", name: "e tag with invalid value",
event: roots.Event{ event: fx.ValidatedEvent(t, "e_tag_invalid"),
ID: ids["a"], PubKey: ids["b"],
CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content,
Tags: []roots.Tag{{"e", "notvalid"}},
},
expected: func() *EventSubgraph { expected: func() *EventSubgraph {
s, eventNode, _ := baseSubgraph(ids["a"], ids["b"]) e := fx.ValidatedEvent(t, "e_tag_invalid")
s, eventNode, _ := baseSubgraph(e)
tagNode := NewTagNode("e", "notvalid") tagNode := NewTagNode("e", "notvalid")
s.AddNode(tagNode) s.AddNode(tagNode)
s.AddRel(NewTaggedRel(eventNode, tagNode, nil)) s.AddRel(NewTaggedRel(eventNode, tagNode, nil))
@@ -117,16 +76,14 @@ func TestEventToSubgraph(t *testing.T) {
}(), }(),
}, },
{ {
name: "p tag with valid hex64", name: "p tag with valid hex64",
event: roots.Event{ event: fx.ValidatedEvent(t, "p_tag_valid"),
ID: ids["a"], PubKey: ids["b"],
CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content,
Tags: []roots.Tag{{"p", ids["d"]}},
},
expected: func() *EventSubgraph { expected: func() *EventSubgraph {
s, eventNode, _ := baseSubgraph(ids["a"], ids["b"]) e := fx.ValidatedEvent(t, "p_tag_valid")
tagNode := NewTagNode("p", ids["d"]) bobPubkey := fx.Keys["bob"]
referencedUser := NewUserNode(ids["d"]) s, eventNode, _ := baseSubgraph(e)
tagNode := NewTagNode("p", bobPubkey)
referencedUser := NewUserNode(bobPubkey)
s.AddNode(tagNode) s.AddNode(tagNode)
s.AddNode(referencedUser) s.AddNode(referencedUser)
s.AddRel(NewTaggedRel(eventNode, tagNode, nil)) s.AddRel(NewTaggedRel(eventNode, tagNode, nil))
@@ -135,20 +92,56 @@ func TestEventToSubgraph(t *testing.T) {
}(), }(),
}, },
{ {
name: "p tag with invalid value", name: "p tag with invalid value",
event: roots.Event{ event: fx.ValidatedEvent(t, "p_tag_invalid"),
ID: ids["a"], PubKey: ids["b"],
CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content,
Tags: []roots.Tag{{"p", "notvalid"}},
},
expected: func() *EventSubgraph { expected: func() *EventSubgraph {
s, eventNode, _ := baseSubgraph(ids["a"], ids["b"]) e := fx.ValidatedEvent(t, "p_tag_invalid")
s, eventNode, _ := baseSubgraph(e)
tagNode := NewTagNode("p", "notvalid") tagNode := NewTagNode("p", "notvalid")
s.AddNode(tagNode) s.AddNode(tagNode)
s.AddRel(NewTaggedRel(eventNode, tagNode, nil)) s.AddRel(NewTaggedRel(eventNode, tagNode, nil))
return s return s
}(), }(),
}, },
{
name: "replaceable kind 0",
event: fx.ValidatedEvent(t, "replaceable_k0"),
expected: func() *EventSubgraph {
e := fx.ValidatedEvent(t, "replaceable_k0")
s, eventNode, userNode := baseSubgraph(e)
rk := NewReplacementKeyNode(e.PubKey(), e.Kind())
s.AddNode(rk)
s.AddRel(NewIsReplaceableRel(eventNode, rk, nil))
s.AddRel(NewForUserRel(rk, userNode, nil))
return s
}(),
},
{
name: "replaceable kind 3",
event: fx.ValidatedEvent(t, "replaceable_k3"),
expected: func() *EventSubgraph {
e := fx.ValidatedEvent(t, "replaceable_k3")
s, eventNode, userNode := baseSubgraph(e)
rk := NewReplacementKeyNode(e.PubKey(), e.Kind())
s.AddNode(rk)
s.AddRel(NewIsReplaceableRel(eventNode, rk, nil))
s.AddRel(NewForUserRel(rk, userNode, nil))
return s
}(),
},
{
name: "replaceable kind 10000-19999",
event: fx.ValidatedEvent(t, "replaceable_k10k"),
expected: func() *EventSubgraph {
e := fx.ValidatedEvent(t, "replaceable_k10k")
s, eventNode, userNode := baseSubgraph(e)
rk := NewReplacementKeyNode(e.PubKey(), e.Kind())
s.AddNode(rk)
s.AddRel(NewIsReplaceableRel(eventNode, rk, nil))
s.AddRel(NewForUserRel(rk, userNode, nil))
return s
}(),
},
} }
expanders := NewExpanderPipeline(DefaultExpanders()...) expanders := NewExpanderPipeline(DefaultExpanders()...)
+1 -1
View File
@@ -235,7 +235,7 @@ func convertEventsToSubgraphs(
) { ) {
defer wg.Done() defer wg.Done()
for traveller := range inChan { for traveller := range inChan {
// temporary adapter — removed in Phase 5 // TODO: temporary adapter — removed in Phase 5
validated, _ := roots.NewValidatedEvent(traveller.Event) validated, _ := roots.NewValidatedEvent(traveller.Event)
subgraph := EventToSubgraph(validated, expanders) subgraph := EventToSubgraph(validated, expanders)
traveller.Subgraph = subgraph traveller.Subgraph = subgraph
+6 -11
View File
@@ -199,22 +199,17 @@ func TestParseEventJSON(t *testing.T) {
// Skip `enforcePolicyRules` -- requires BoltDB // Skip `enforcePolicyRules` -- requires BoltDB
func TestConvertEventsToSubgraphs(t *testing.T) { func TestConvertEventsToSubgraphs(t *testing.T) {
fx := LoadFixtures(t)
cases := []struct { cases := []struct {
name string name string
event roots.Event event roots.ValidatedEvent
wantNodeCount int wantNodeCount int
wantRelCount int wantRelCount int
}{ }{
{ {
name: "event with no tags", name: "event with no tags",
event: roots.Event{ event: fx.ValidatedEvent(t, "bare"),
ID: "abc123",
PubKey: "pubkey1",
CreatedAt: 1000,
Kind: 1,
Content: "test",
Tags: []roots.Tag{},
},
wantNodeCount: 2, // event + user wantNodeCount: 2, // event + user
wantRelCount: 1, // signed wantRelCount: 1, // signed
}, },
@@ -232,7 +227,7 @@ func TestConvertEventsToSubgraphs(t *testing.T) {
go convertEventsToSubgraphs(&wg, expanders, inChan, convertedChan) go convertEventsToSubgraphs(&wg, expanders, inChan, convertedChan)
go func() { go func() {
inChan <- EventTraveller{Event: tc.event} inChan <- EventTraveller{Event: tc.event.Event()}
close(inChan) close(inChan)
}() }()