1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
package grpc
import (
"testing"
pb "northwest.io/muxstr/api/nostr/v1"
"northwest.io/muxstr/internal/nostr"
)
func TestNostrToPB(t *testing.T) {
nostrEvent := &nostr.Event{
ID: "abc123",
PubKey: "pubkey123",
CreatedAt: 1234567890,
Kind: 1,
Tags: nostr.Tags{{"e", "event1"}, {"p", "pubkey1"}},
Content: "Hello, Nostr!",
Sig: "sig123",
}
pbEvent := NostrToPB(nostrEvent)
if pbEvent.Id != nostrEvent.ID {
t.Errorf("ID mismatch: expected %s, got %s", nostrEvent.ID, pbEvent.Id)
}
if pbEvent.Pubkey != nostrEvent.PubKey {
t.Errorf("Pubkey mismatch: expected %s, got %s", nostrEvent.PubKey, pbEvent.Pubkey)
}
if pbEvent.CreatedAt != nostrEvent.CreatedAt {
t.Errorf("CreatedAt mismatch: expected %d, got %d", nostrEvent.CreatedAt, pbEvent.CreatedAt)
}
if pbEvent.Kind != int32(nostrEvent.Kind) {
t.Errorf("Kind mismatch: expected %d, got %d", nostrEvent.Kind, pbEvent.Kind)
}
if pbEvent.Content != nostrEvent.Content {
t.Errorf("Content mismatch: expected %s, got %s", nostrEvent.Content, pbEvent.Content)
}
if pbEvent.Sig != nostrEvent.Sig {
t.Errorf("Sig mismatch: expected %s, got %s", nostrEvent.Sig, pbEvent.Sig)
}
if len(pbEvent.Tags) != len(nostrEvent.Tags) {
t.Fatalf("Tags length mismatch: expected %d, got %d", len(nostrEvent.Tags), len(pbEvent.Tags))
}
for i, tag := range pbEvent.Tags {
if len(tag.Values) != len(nostrEvent.Tags[i]) {
t.Errorf("Tag[%d] values length mismatch", i)
continue
}
for j, val := range tag.Values {
if val != nostrEvent.Tags[i][j] {
t.Errorf("Tag[%d][%d] mismatch: expected %s, got %s", i, j, nostrEvent.Tags[i][j], val)
}
}
}
}
func TestPBToNostr(t *testing.T) {
pbEvent := &pb.Event{
Id: "abc123",
Pubkey: "pubkey123",
CreatedAt: 1234567890,
Kind: 1,
Tags: []*pb.Tag{{Values: []string{"e", "event1"}}, {Values: []string{"p", "pubkey1"}}},
Content: "Hello, Nostr!",
Sig: "sig123",
}
nostrEvent := PBToNostr(pbEvent)
if nostrEvent.ID != pbEvent.Id {
t.Errorf("ID mismatch: expected %s, got %s", pbEvent.Id, nostrEvent.ID)
}
if nostrEvent.PubKey != pbEvent.Pubkey {
t.Errorf("Pubkey mismatch: expected %s, got %s", pbEvent.Pubkey, nostrEvent.PubKey)
}
if nostrEvent.CreatedAt != pbEvent.CreatedAt {
t.Errorf("CreatedAt mismatch: expected %d, got %d", pbEvent.CreatedAt, nostrEvent.CreatedAt)
}
if nostrEvent.Kind != int(pbEvent.Kind) {
t.Errorf("Kind mismatch: expected %d, got %d", pbEvent.Kind, nostrEvent.Kind)
}
if nostrEvent.Content != pbEvent.Content {
t.Errorf("Content mismatch: expected %s, got %s", pbEvent.Content, nostrEvent.Content)
}
if nostrEvent.Sig != pbEvent.Sig {
t.Errorf("Sig mismatch: expected %s, got %s", pbEvent.Sig, nostrEvent.Sig)
}
if len(nostrEvent.Tags) != len(pbEvent.Tags) {
t.Fatalf("Tags length mismatch: expected %d, got %d", len(pbEvent.Tags), len(nostrEvent.Tags))
}
for i, tag := range nostrEvent.Tags {
if len(tag) != len(pbEvent.Tags[i].Values) {
t.Errorf("Tag[%d] values length mismatch", i)
continue
}
for j, val := range tag {
if val != pbEvent.Tags[i].Values[j] {
t.Errorf("Tag[%d][%d] mismatch: expected %s, got %s", i, j, pbEvent.Tags[i].Values[j], val)
}
}
}
}
func TestRoundTrip(t *testing.T) {
original := &nostr.Event{
ID: "roundtrip123",
PubKey: "pubkey_roundtrip",
CreatedAt: 9876543210,
Kind: 7,
Tags: nostr.Tags{{"e", "evt"}, {"p", "pk", "relay"}},
Content: "Round trip test",
Sig: "signature",
}
pb := NostrToPB(original)
backToNostr := PBToNostr(pb)
if backToNostr.ID != original.ID {
t.Errorf("Round trip ID mismatch")
}
if backToNostr.PubKey != original.PubKey {
t.Errorf("Round trip PubKey mismatch")
}
if backToNostr.CreatedAt != original.CreatedAt {
t.Errorf("Round trip CreatedAt mismatch")
}
if backToNostr.Kind != original.Kind {
t.Errorf("Round trip Kind mismatch")
}
if backToNostr.Content != original.Content {
t.Errorf("Round trip Content mismatch")
}
if backToNostr.Sig != original.Sig {
t.Errorf("Round trip Sig mismatch")
}
if len(backToNostr.Tags) != len(original.Tags) {
t.Fatalf("Round trip Tags length mismatch")
}
}
|