summaryrefslogtreecommitdiffstats
path: root/tags_test.go
blob: 7796606d2fd3b3ed5c94c21d6582963f52e71632 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package nostr

import (
	"testing"
)

func TestTagKey(t *testing.T) {
	tests := []struct {
		name string
		tag  Tag
		want string
	}{
		{"empty tag", Tag{}, ""},
		{"single element", Tag{"e"}, "e"},
		{"multiple elements", Tag{"p", "abc123", "relay"}, "p"},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := tt.tag.Key(); got != tt.want {
				t.Errorf("Tag.Key() = %q, want %q", got, tt.want)
			}
		})
	}
}

func TestTagValue(t *testing.T) {
	tests := []struct {
		name string
		tag  Tag
		want string
	}{
		{"empty tag", Tag{}, ""},
		{"single element", Tag{"e"}, ""},
		{"two elements", Tag{"p", "abc123"}, "abc123"},
		{"multiple elements", Tag{"e", "eventid", "relay", "marker"}, "eventid"},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := tt.tag.Value(); got != tt.want {
				t.Errorf("Tag.Value() = %q, want %q", got, tt.want)
			}
		})
	}
}

func TestTagsFind(t *testing.T) {
	tags := Tags{
		{"e", "event1"},
		{"p", "pubkey1"},
		{"e", "event2"},
		{"d", "identifier"},
	}

	tests := []struct {
		name     string
		key      string
		wantNil  bool
		wantVal  string
	}{
		{"find first e", "e", false, "event1"},
		{"find p", "p", false, "pubkey1"},
		{"find d", "d", false, "identifier"},
		{"find nonexistent", "x", true, ""},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := tags.Find(tt.key)
			if tt.wantNil {
				if got != nil {
					t.Errorf("Tags.Find(%q) = %v, want nil", tt.key, got)
				}
			} else {
				if got == nil {
					t.Errorf("Tags.Find(%q) = nil, want value %q", tt.key, tt.wantVal)
				} else if got.Value() != tt.wantVal {
					t.Errorf("Tags.Find(%q).Value() = %q, want %q", tt.key, got.Value(), tt.wantVal)
				}
			}
		})
	}
}

func TestTagsFindAll(t *testing.T) {
	tags := Tags{
		{"e", "event1"},
		{"p", "pubkey1"},
		{"e", "event2"},
		{"e", "event3"},
	}

	found := tags.FindAll("e")
	if len(found) != 3 {
		t.Errorf("Tags.FindAll(\"e\") returned %d tags, want 3", len(found))
	}

	found = tags.FindAll("p")
	if len(found) != 1 {
		t.Errorf("Tags.FindAll(\"p\") returned %d tags, want 1", len(found))
	}

	found = tags.FindAll("x")
	if len(found) != 0 {
		t.Errorf("Tags.FindAll(\"x\") returned %d tags, want 0", len(found))
	}
}

func TestTagsGetD(t *testing.T) {
	tests := []struct {
		name string
		tags Tags
		want string
	}{
		{"no d tag", Tags{{"e", "event1"}}, ""},
		{"empty d tag", Tags{{"d"}}, ""},
		{"d tag present", Tags{{"d", "my-identifier"}}, "my-identifier"},
		{"d tag with extras", Tags{{"d", "id", "extra"}}, "id"},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := tt.tags.GetD(); got != tt.want {
				t.Errorf("Tags.GetD() = %q, want %q", got, tt.want)
			}
		})
	}
}

func TestTagsContainsValue(t *testing.T) {
	tags := Tags{
		{"e", "event1"},
		{"p", "pubkey1"},
		{"e", "event2"},
	}

	tests := []struct {
		key   string
		value string
		want  bool
	}{
		{"e", "event1", true},
		{"e", "event2", true},
		{"e", "event3", false},
		{"p", "pubkey1", true},
		{"p", "pubkey2", false},
		{"x", "anything", false},
	}

	for _, tt := range tests {
		t.Run(tt.key+"="+tt.value, func(t *testing.T) {
			if got := tags.ContainsValue(tt.key, tt.value); got != tt.want {
				t.Errorf("Tags.ContainsValue(%q, %q) = %v, want %v", tt.key, tt.value, got, tt.want)
			}
		})
	}
}