diff --git a/src/modules/hyprland/backend.cpp b/src/modules/hyprland/backend.cpp index b3fb36d4..8ec6edda 100644 --- a/src/modules/hyprland/backend.cpp +++ b/src/modules/hyprland/backend.cpp @@ -218,7 +218,13 @@ std::string IPC::getSocket1Reply(const std::string& rq) { } Json::Value IPC::getSocket1JsonReply(const std::string& rq) { - return parser_.parse(getSocket1Reply("j/" + rq)); + std::string reply = getSocket1Reply("j/" + rq); + + if (reply.empty()) { + return {}; + } + + return parser_.parse(reply); } } // namespace waybar::modules::hyprland diff --git a/test/hyprland/backend.cpp b/test/hyprland/backend.cpp index e6b209da..dcae0509 100644 --- a/test/hyprland/backend.cpp +++ b/test/hyprland/backend.cpp @@ -1,4 +1,3 @@ -#include #if __has_include() #include #else @@ -6,7 +5,6 @@ #endif #include "fixtures/IPCTestFixture.hpp" -#include "modules/hyprland/backend.hpp" namespace fs = std::filesystem; namespace hyprland = waybar::modules::hyprland; @@ -53,3 +51,11 @@ TEST_CASE_METHOD(IPCTestFixture, "XDGRuntimeDirExistsNoHyprDir", "[getSocketFold // Assert expected result REQUIRE(actualPath == expectedPath); } + +TEST_CASE_METHOD(IPCMock, "getSocket1JsonReply handles empty response", "[getSocket1JsonReply]") { + std::string request = "test_request"; + + Json::Value jsonResponse = getSocket1JsonReply(request); + + REQUIRE(jsonResponse.isNull()); +} diff --git a/test/hyprland/fixtures/IPCTestFixture.hpp b/test/hyprland/fixtures/IPCTestFixture.hpp index 3b04b3b0..f6fa335f 100644 --- a/test/hyprland/fixtures/IPCTestFixture.hpp +++ b/test/hyprland/fixtures/IPCTestFixture.hpp @@ -14,3 +14,9 @@ class IPCTestFixture : public hyprland::IPC { private: }; + +class IPCMock : public IPCTestFixture { + public: + // Mock getSocket1Reply to return an empty string + static std::string getSocket1Reply(const std::string& rq) { return ""; } +};