feat(automation): JSON-RPC dispatcher envelope + version + error model

This commit is contained in:
SoftFever
2026-06-03 01:49:49 +08:00
parent 94c356845e
commit aac14ae161
4 changed files with 184 additions and 0 deletions

View File

@@ -4,9 +4,11 @@ add_executable(${_TEST_NAME}_tests
automation_tests.cpp
test_serializer.cpp
test_locator.cpp
test_dispatcher.cpp
MockUiBackend.cpp
${CMAKE_SOURCE_DIR}/src/slic3r/GUI/Automation/WidgetSerializer.cpp
${CMAKE_SOURCE_DIR}/src/slic3r/GUI/Automation/Locator.cpp
${CMAKE_SOURCE_DIR}/src/slic3r/GUI/Automation/JsonRpcDispatcher.cpp
)
target_link_libraries(${_TEST_NAME}_tests test_common Catch2::Catch2WithMain nlohmann_json)
# The nlohmann_json INTERFACE target only exposes deps_src/nlohmann on the include

View File

@@ -0,0 +1,44 @@
#include <catch2/catch_all.hpp>
#include "slic3r/GUI/Automation/JsonRpcDispatcher.hpp"
#include "MockUiBackend.hpp"
using namespace Slic3r::GUI::Automation;
using nlohmann::json;
TEST_CASE("dispatch automation.version", "[automation][rpc]") {
MockUiBackend mock;
JsonRpcDispatcher d(mock);
const json req = {{"jsonrpc","2.0"},{"id",1},{"method","automation.version"}};
const json resp = d.dispatch(req);
CHECK(resp.at("jsonrpc") == "2.0");
CHECK(resp.at("id") == 1);
CHECK(resp.at("result").at("version") == kAutomationVersion);
CHECK(resp.at("result").at("protocol") == "2.0");
CHECK(resp.at("result").at("capabilities").is_array());
}
TEST_CASE("unknown method -> -32601", "[automation][rpc]") {
MockUiBackend mock;
JsonRpcDispatcher d(mock);
const json req = {{"jsonrpc","2.0"},{"id",7},{"method","does.not.exist"}};
const json resp = d.dispatch(req);
CHECK(resp.at("id") == 7);
CHECK(resp.at("error").at("code") == kMethodNotFound);
}
TEST_CASE("malformed JSON body -> parse error", "[automation][rpc]") {
MockUiBackend mock;
JsonRpcDispatcher d(mock);
const std::string resp = d.handle_request("{not json");
const json j = json::parse(resp);
CHECK(j.at("error").at("code") == kParseError);
CHECK(j.at("id").is_null());
}
TEST_CASE("missing method field -> invalid request", "[automation][rpc]") {
MockUiBackend mock;
JsonRpcDispatcher d(mock);
const json req = {{"jsonrpc","2.0"},{"id",2}};
const json resp = d.dispatch(req);
CHECK(resp.at("error").at("code") == kInvalidRequest);
}