cleanup-kind3_test.go raw
1 package database
2
3 import (
4 "bytes"
5 "encoding/json"
6 "testing"
7
8 "next.orly.dev/pkg/nostr/encoders/event"
9 "github.com/stretchr/testify/assert"
10 )
11
12 // TestKind3TagRoundTrip tests that kind 3 events with p tags survive
13 // JSON -> binary -> JSON round trip
14 func TestKind3TagRoundTrip(t *testing.T) {
15 // Sample kind 3 event JSON with p tags
16 kind3JSON := `{
17 "id": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
18 "pubkey": "fedcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321",
19 "created_at": 1234567890,
20 "kind": 3,
21 "tags": [
22 ["p", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"],
23 ["p", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"],
24 ["p", "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"]
25 ],
26 "content": "",
27 "sig": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
28 }`
29
30 // 1. Unmarshal from JSON (simulates receiving from WebSocket)
31 ev1 := event.New()
32 err := json.Unmarshal([]byte(kind3JSON), ev1)
33 assert.NoError(t, err)
34 assert.NotNil(t, ev1.Tags)
35 assert.Equal(t, 3, ev1.Tags.Len(), "Should have 3 tags")
36
37 // Verify all tags have key "p"
38 pTagCount := 0
39 for _, tg := range *ev1.Tags {
40 if tg != nil && tg.Len() >= 2 {
41 key := tg.Key()
42 if len(key) == 1 && key[0] == 'p' {
43 pTagCount++
44 t.Logf("Found p tag with value length: %d bytes", len(tg.Value()))
45 }
46 }
47 }
48 assert.Equal(t, 3, pTagCount, "Should have 3 p tags after JSON unmarshal")
49
50 // 2. Marshal to binary (simulates database storage)
51 buf := new(bytes.Buffer)
52 ev1.MarshalBinary(buf)
53 binaryData := buf.Bytes()
54 t.Logf("Binary encoding size: %d bytes", len(binaryData))
55
56 // 3. Unmarshal from binary (simulates database retrieval)
57 ev2 := event.New()
58 err = ev2.UnmarshalBinary(bytes.NewBuffer(binaryData))
59 assert.NoError(t, err)
60 assert.NotNil(t, ev2.Tags)
61 assert.Equal(t, 3, ev2.Tags.Len(), "Should have 3 tags after binary round-trip")
62
63 // Verify all tags still have key "p"
64 pTagCount2 := 0
65 for _, tg := range *ev2.Tags {
66 if tg != nil && tg.Len() >= 2 {
67 key := tg.Key()
68 if len(key) == 1 && key[0] == 'p' {
69 pTagCount2++
70 t.Logf("Found p tag after round-trip with value length: %d bytes", len(tg.Value()))
71 }
72 }
73 }
74 assert.Equal(t, 3, pTagCount2, "Should have 3 p tags after binary round-trip")
75
76 // 4. Marshal back to JSON to verify tags are still there
77 jsonData2, err := json.Marshal(ev2)
78 assert.NoError(t, err)
79 t.Logf("JSON after round-trip: %s", string(jsonData2))
80
81 // Parse the JSON and count p tags
82 var jsonMap map[string]interface{}
83 err = json.Unmarshal(jsonData2, &jsonMap)
84 assert.NoError(t, err)
85
86 tags, ok := jsonMap["tags"].([]interface{})
87 assert.True(t, ok, "tags should be an array")
88 assert.Equal(t, 3, len(tags), "Should have 3 tags in final JSON")
89
90 for i, tag := range tags {
91 tagArray, ok := tag.([]interface{})
92 assert.True(t, ok, "tag should be an array")
93 assert.GreaterOrEqual(t, len(tagArray), 2, "tag should have at least 2 elements")
94 assert.Equal(t, "p", tagArray[0], "tag %d should have key 'p'", i)
95 t.Logf("Tag %d: %v", i, tagArray)
96 }
97 }
98