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) } }) } }