Skip to content

Commit 8a49f72

Browse files
committed
🐛 fix the handling of empty journal and status events
semver: patch
1 parent da5445a commit 8a49f72

5 files changed

Lines changed: 81 additions & 2 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,3 +600,4 @@ vite.config.ts.timestamp-*
600600
.direnv/
601601
result
602602
result-*
603+
/.claude

EliteAPI.Tests/FlatteningTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,4 +523,40 @@ public void NullInNestedObject()
523523

524524
paths.Should().BeEquivalentTo(expected);
525525
}
526+
527+
[Test]
528+
public void GetEventName_WithEmptyString_ReturnsEmptyString()
529+
{
530+
var json = "";
531+
var eventName = JsonUtils.GetEventName(json);
532+
533+
eventName.Should().BeEmpty();
534+
}
535+
536+
[Test]
537+
public void GetEventName_WithWhitespace_ReturnsEmptyString()
538+
{
539+
var json = " ";
540+
var eventName = JsonUtils.GetEventName(json);
541+
542+
eventName.Should().BeEmpty();
543+
}
544+
545+
[Test]
546+
public void GetEventName_WithValidEvent_ReturnsEventName()
547+
{
548+
var json = """{ "event": "TestEvent", "timestamp": "2025-01-01T00:00:00Z" }""";
549+
var eventName = JsonUtils.GetEventName(json);
550+
551+
eventName.Should().Be("TestEvent");
552+
}
553+
554+
[Test]
555+
public void GetEventName_WithNoEventField_ReturnsEmptyString()
556+
{
557+
var json = """{ "timestamp": "2025-01-01T00:00:00Z" }""";
558+
var eventName = JsonUtils.GetEventName(json);
559+
560+
eventName.Should().BeEmpty();
561+
}
526562
}

EliteAPI.Tests/StatusChangeEventTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,34 @@ public void FiresChangeEvent_ForBalance_WhenItChanges()
251251

252252
balanceChangeEventFired.Should().BeTrue();
253253
}
254+
255+
[Test]
256+
public void DoesNotThrow_WhenJsonIsEmpty()
257+
{
258+
var api = new EliteDangerousApi(null, null);
259+
var anyEventFired = false;
260+
261+
api.OnAllJson(e => anyEventFired = true);
262+
263+
var emptyJson = "";
264+
var invokeAction = () => api.Invoke(emptyJson);
265+
266+
invokeAction.Should().NotThrow("empty JSON should be handled gracefully");
267+
anyEventFired.Should().BeFalse("no events should fire for empty JSON");
268+
}
269+
270+
[Test]
271+
public void DoesNotThrow_WhenJsonIsWhitespace()
272+
{
273+
var api = new EliteDangerousApi(null, null);
274+
var anyEventFired = false;
275+
276+
api.OnAllJson(e => anyEventFired = true);
277+
278+
var whitespaceJson = " ";
279+
var invokeAction = () => api.Invoke(whitespaceJson);
280+
281+
invokeAction.Should().NotThrow("whitespace JSON should be handled gracefully");
282+
anyEventFired.Should().BeFalse("no events should fire for whitespace JSON");
283+
}
254284
}

EliteAPI/EliteDangerousApi.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class EliteDangerousApi
3232
private readonly List<Action<IReadOnlyCollection<Control>>> _bindingsHandlers = [];
3333

3434
public Version Version => typeof(EliteDangerousApi).Assembly.GetName().Version!;
35-
35+
3636
public EliteDangerousApi() : this(JournalUtils.GetJournalsDirectory(), BindingsUtils.GetBindingsDirectory())
3737
{
3838
}
@@ -53,7 +53,7 @@ public EliteDangerousApi(DirectoryInfo? journalDirectory, DirectoryInfo? binding
5353
"Outfitting.json",
5454
"ShipLocker.json",
5555
"Shipyard.json",
56-
"Status.json"
56+
"Status.json",
5757
];
5858

5959
_statusWatchers = statusFiles
@@ -186,6 +186,12 @@ public void Invoke(IEvent @event)
186186

187187
internal void Invoke(string json, IEvent? @event)
188188
{
189+
if (string.IsNullOrWhiteSpace(json))
190+
{
191+
Log.Debug("Skipping empty event");
192+
return;
193+
}
194+
189195
var eventName = JsonUtils.GetEventName(json);
190196
if (string.IsNullOrEmpty(eventName))
191197
{

EliteAPI/Json/JsonUtils.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public static class JsonUtils
1616

1717
public static string GetEventName(string json)
1818
{
19+
if (string.IsNullOrWhiteSpace(json))
20+
return string.Empty;
21+
1922
var obj = JObject.Parse(json);
2023

2124
{
@@ -33,6 +36,9 @@ public static string GetEventName(string json)
3336

3437
public static List<EventPath> FlattenJson(string json)
3538
{
39+
if (string.IsNullOrWhiteSpace(json))
40+
return [];
41+
3642
// TODO: call flattening function
3743
// arrays need Length
3844
// key + localisation

0 commit comments

Comments
 (0)