Skip to content

Commit 48e9b35

Browse files
authored
Merge pull request #406 from tcheeric/codex/refactor-marker-handling-in-eventtag
fix: remove default marker assignment in EventTag
2 parents c7c39f8 + be0df6b commit 48e9b35

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

nostr-java-event/src/main/java/nostr/event/tag/EventTag.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ public class EventTag extends BaseTag {
4242
private Marker marker;
4343

4444
public EventTag(String idEvent) {
45-
this.recommendedRelayUrl = null;
4645
this.idEvent = idEvent;
47-
48-
// TODO: This is a bug. The marker should not be set, or at least not like this.
49-
//this.marker = this.idEvent == null ? Marker.ROOT : Marker.REPLY;
5046
}
5147

5248
public static <T extends BaseTag> T deserialize(@NonNull JsonNode node) {

nostr-java-event/src/test/java/nostr/event/unit/EventTagTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package nostr.event.unit;
22

33
import nostr.base.Marker;
4+
import nostr.event.BaseTag;
5+
import nostr.event.json.codec.BaseTagEncoder;
46
import nostr.event.tag.EventTag;
57
import org.junit.jupiter.api.Test;
68

@@ -9,12 +11,17 @@
911
import java.util.UUID;
1012
import java.util.function.Predicate;
1113

14+
import static nostr.base.IEvent.MAPPER_BLACKBIRD;
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
1216
import 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;
1319
import static org.junit.jupiter.api.Assertions.assertTrue;
1420

1521
class 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
}

nostr-java-event/src/test/java/nostr/event/unit/TagDeserializerTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
import static nostr.base.IEvent.MAPPER_BLACKBIRD;
1414
import static org.junit.jupiter.api.Assertions.assertEquals;
1515
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
16+
import static org.junit.jupiter.api.Assertions.assertNull;
1617

1718
class TagDeserializerTest {
1819

1920
@Test
21+
// Parses an AddressTag from JSON and verifies its fields.
2022
void testAddressTagDeserialization() throws Exception {
2123
String pubKey = "bbbd79f81439ff794cf5ac5f7bff9121e257f399829e472c7a14d3e86fe76984";
2224
String json = "[\"a\",\"1:" + pubKey + ":test\",\"ws://localhost:8080\"]";
@@ -30,6 +32,7 @@ void testAddressTagDeserialization() throws Exception {
3032
}
3133

3234
@Test
35+
// Parses an EventTag with relay and marker and checks values.
3336
void testEventTagDeserialization() throws Exception {
3437
String id = "494001ac0c8af2a10f60f23538e5b35d3cdacb8e1cc956fe7a16dfa5cbfc4346";
3538
String json = "[\"e\",\"" + id + "\",\"wss://relay.example.com\",\"root\"]";
@@ -42,6 +45,20 @@ void testEventTagDeserialization() throws Exception {
4245
}
4346

4447
@Test
48+
// Parses an EventTag without relay or marker and ensures marker is null.
49+
void testEventTagDeserializationWithoutMarker() throws Exception {
50+
String id = "494001ac0c8af2a10f60f23538e5b35d3cdacb8e1cc956fe7a16dfa5cbfc4346";
51+
String json = "[\"e\",\"" + id + "\"]";
52+
BaseTag tag = MAPPER_BLACKBIRD.readValue(json, BaseTag.class);
53+
assertInstanceOf(EventTag.class, tag);
54+
EventTag eTag = (EventTag) tag;
55+
assertEquals(id, eTag.getIdEvent());
56+
assertNull(eTag.getMarker());
57+
assertNull(eTag.getRecommendedRelayUrl());
58+
}
59+
60+
@Test
61+
// Parses a PriceTag from JSON and validates number and currency.
4562
void testPriceTagDeserialization() throws Exception {
4663
String json = "[\"price\",\"10.99\",\"USD\"]";
4764
BaseTag tag = MAPPER_BLACKBIRD.readValue(json, BaseTag.class);
@@ -52,6 +69,7 @@ void testPriceTagDeserialization() throws Exception {
5269
}
5370

5471
@Test
72+
// Parses a UrlTag from JSON and checks the URL value.
5573
void testUrlTagDeserialization() throws Exception {
5674
String json = "[\"u\",\"http://example.com\"]";
5775
BaseTag tag = MAPPER_BLACKBIRD.readValue(json, BaseTag.class);
@@ -61,6 +79,7 @@ void testUrlTagDeserialization() throws Exception {
6179
}
6280

6381
@Test
82+
// Falls back to GenericTag for unknown tag codes.
6483
void testGenericFallback() throws Exception {
6584
String json = "[\"unknown\",\"value\"]";
6685
BaseTag tag = MAPPER_BLACKBIRD.readValue(json, BaseTag.class);

0 commit comments

Comments
 (0)