11package nostr .event .unit ;
22
33import nostr .base .Marker ;
4+ import nostr .event .BaseTag ;
5+ import nostr .event .json .codec .BaseTagEncoder ;
46import nostr .event .tag .EventTag ;
57import org .junit .jupiter .api .Test ;
68
911import java .util .UUID ;
1012import java .util .function .Predicate ;
1113
14+ import static nostr .base .IEvent .MAPPER_BLACKBIRD ;
15+ import static org .junit .jupiter .api .Assertions .assertEquals ;
1216import static org .junit .jupiter .api .Assertions .assertFalse ;
17+ import static org .junit .jupiter .api .Assertions .assertInstanceOf ;
18+ import static org .junit .jupiter .api .Assertions .assertNull ;
1319import static org .junit .jupiter .api .Assertions .assertTrue ;
1420
1521class EventTagTest {
1622
1723 @ Test
24+ // Verifies that getSupportedFields returns expected fields and values.
1825 void getSupportedFields () {
1926 String eventId = UUID .randomUUID ().toString ().concat (UUID .randomUUID ().toString ()).substring (0 , 64 );
2027 String recommendedRelayUrl = "ws://localhost:5555" ;
@@ -36,6 +43,42 @@ void getSupportedFields() {
3643 assertFalse (fields .stream ().flatMap (field -> eventTag .getFieldValue (field ).stream ()).anyMatch (fieldValue -> fieldValue .equals (eventId + "x" )));
3744 }
3845
46+ @ Test
47+ // Ensures that a newly created EventTag has a null marker and serializes without it.
48+ void serializeWithoutMarker () throws Exception {
49+ String eventId = "494001ac0c8af2a10f60f23538e5b35d3cdacb8e1cc956fe7a16dfa5cbfc4346" ;
50+ EventTag eventTag = new EventTag (eventId );
51+
52+ assertNull (eventTag .getMarker ());
53+
54+ String json = new BaseTagEncoder (eventTag ).encode ();
55+ assertEquals ("[\" e\" ,\" " + eventId + "\" ]" , json );
56+
57+ BaseTag decoded = MAPPER_BLACKBIRD .readValue (json , BaseTag .class );
58+ assertInstanceOf (EventTag .class , decoded );
59+ assertNull (((EventTag ) decoded ).getMarker ());
60+ }
61+
62+ @ Test
63+ // Checks that an explicit marker is serialized and restored on deserialization.
64+ void serializeWithMarker () throws Exception {
65+ String eventId = "494001ac0c8af2a10f60f23538e5b35d3cdacb8e1cc956fe7a16dfa5cbfc4346" ;
66+ EventTag eventTag = EventTag .builder ()
67+ .idEvent (eventId )
68+ .recommendedRelayUrl ("wss://relay.example.com" )
69+ .marker (Marker .ROOT )
70+ .build ();
71+
72+ String json = new BaseTagEncoder (eventTag ).encode ();
73+ assertEquals ("[\" e\" ,\" " + eventId + "\" ,\" wss://relay.example.com\" ,\" ROOT\" ]" , json );
74+
75+ BaseTag decoded = MAPPER_BLACKBIRD .readValue (json , BaseTag .class );
76+ assertInstanceOf (EventTag .class , decoded );
77+ EventTag decodedEventTag = (EventTag ) decoded ;
78+ assertEquals (Marker .ROOT , decodedEventTag .getMarker ());
79+ assertEquals ("wss://relay.example.com" , decodedEventTag .getRecommendedRelayUrl ());
80+ }
81+
3982 private static void anyFieldNameMatch (List <Field > fields , Predicate <Field > predicate ) {
4083 assertTrue (fields .stream ().anyMatch (predicate ));
4184 }
0 commit comments