Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions nostr-java-event/src/main/java/nostr/event/tag/EventTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ public class EventTag extends BaseTag {
private Marker marker;

public EventTag(String idEvent) {
this.recommendedRelayUrl = null;
this.idEvent = idEvent;

// TODO: This is a bug. The marker should not be set, or at least not like this.
//this.marker = this.idEvent == null ? Marker.ROOT : Marker.REPLY;
}

public static <T extends BaseTag> T deserialize(@NonNull JsonNode node) {
Expand Down
43 changes: 43 additions & 0 deletions nostr-java-event/src/test/java/nostr/event/unit/EventTagTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package nostr.event.unit;

import nostr.base.Marker;
import nostr.event.BaseTag;
import nostr.event.json.codec.BaseTagEncoder;
import nostr.event.tag.EventTag;
import org.junit.jupiter.api.Test;

Expand All @@ -9,12 +11,17 @@
import java.util.UUID;
import java.util.function.Predicate;

import static nostr.base.IEvent.MAPPER_BLACKBIRD;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class EventTagTest {

@Test
// Verifies that getSupportedFields returns expected fields and values.
void getSupportedFields() {
String eventId = UUID.randomUUID().toString().concat(UUID.randomUUID().toString()).substring(0, 64);
String recommendedRelayUrl = "ws://localhost:5555";
Expand All @@ -36,6 +43,42 @@ void getSupportedFields() {
assertFalse(fields.stream().flatMap(field -> eventTag.getFieldValue(field).stream()).anyMatch(fieldValue -> fieldValue.equals(eventId + "x")));
}

@Test
// Ensures that a newly created EventTag has a null marker and serializes without it.
void serializeWithoutMarker() throws Exception {
String eventId = "494001ac0c8af2a10f60f23538e5b35d3cdacb8e1cc956fe7a16dfa5cbfc4346";
EventTag eventTag = new EventTag(eventId);

assertNull(eventTag.getMarker());

String json = new BaseTagEncoder(eventTag).encode();
assertEquals("[\"e\",\"" + eventId + "\"]", json);

BaseTag decoded = MAPPER_BLACKBIRD.readValue(json, BaseTag.class);
assertInstanceOf(EventTag.class, decoded);
assertNull(((EventTag) decoded).getMarker());
}

@Test
// Checks that an explicit marker is serialized and restored on deserialization.
void serializeWithMarker() throws Exception {
String eventId = "494001ac0c8af2a10f60f23538e5b35d3cdacb8e1cc956fe7a16dfa5cbfc4346";
EventTag eventTag = EventTag.builder()
.idEvent(eventId)
.recommendedRelayUrl("wss://relay.example.com")
.marker(Marker.ROOT)
.build();

String json = new BaseTagEncoder(eventTag).encode();
assertEquals("[\"e\",\"" + eventId + "\",\"wss://relay.example.com\",\"ROOT\"]", json);

BaseTag decoded = MAPPER_BLACKBIRD.readValue(json, BaseTag.class);
assertInstanceOf(EventTag.class, decoded);
EventTag decodedEventTag = (EventTag) decoded;
assertEquals(Marker.ROOT, decodedEventTag.getMarker());
assertEquals("wss://relay.example.com", decodedEventTag.getRecommendedRelayUrl());
}

private static void anyFieldNameMatch(List<Field> fields, Predicate<Field> predicate) {
assertTrue(fields.stream().anyMatch(predicate));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
import static nostr.base.IEvent.MAPPER_BLACKBIRD;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;

class TagDeserializerTest {

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

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

@Test
// Parses an EventTag without relay or marker and ensures marker is null.
void testEventTagDeserializationWithoutMarker() throws Exception {
String id = "494001ac0c8af2a10f60f23538e5b35d3cdacb8e1cc956fe7a16dfa5cbfc4346";
String json = "[\"e\",\"" + id + "\"]";
BaseTag tag = MAPPER_BLACKBIRD.readValue(json, BaseTag.class);
assertInstanceOf(EventTag.class, tag);
EventTag eTag = (EventTag) tag;
assertEquals(id, eTag.getIdEvent());
assertNull(eTag.getMarker());
assertNull(eTag.getRecommendedRelayUrl());
}

@Test
// Parses a PriceTag from JSON and validates number and currency.
void testPriceTagDeserialization() throws Exception {
String json = "[\"price\",\"10.99\",\"USD\"]";
BaseTag tag = MAPPER_BLACKBIRD.readValue(json, BaseTag.class);
Expand All @@ -52,6 +69,7 @@ void testPriceTagDeserialization() throws Exception {
}

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

@Test
// Falls back to GenericTag for unknown tag codes.
void testGenericFallback() throws Exception {
String json = "[\"unknown\",\"value\"]";
BaseTag tag = MAPPER_BLACKBIRD.readValue(json, BaseTag.class);
Expand Down
Loading