345 lines
14 KiB
C++
345 lines
14 KiB
C++
// tests/plugin/test_plugin_external_gcs.cpp
|
||
// P3-9:通过现有 PluginManager::invoke_external_service 调用示例 GCS 外部服务
|
||
// (vde-service 能力 gcs:solve,schema_version=1)。
|
||
// 覆盖:能力/清单发现、请求/响应端到端(positions/dof/constraint_status/diagnostics)、
|
||
// 驱动参数、超时、坏响应、未知 operation、非法 sketch、schema 版本、未知约束、冲突诊断、
|
||
// 能力发现过滤(其他服务不声明 gcs:solve)。
|
||
#include <gtest/gtest.h>
|
||
|
||
#include "vde/plugin/plugin_manager.h"
|
||
|
||
#include <chrono>
|
||
#include "vde/fs_compat.h"
|
||
#include <fstream>
|
||
#include <sstream>
|
||
#include <string>
|
||
#include <thread>
|
||
#include <vector>
|
||
|
||
#if defined(_WIN32)
|
||
#include <cstdlib>
|
||
#include <windows.h>
|
||
#else
|
||
#include <cstdlib>
|
||
#endif
|
||
|
||
namespace fs = vde::fs;
|
||
using vde::plugin::ExternalServiceInfo;
|
||
using vde::plugin::ExternalServiceInvokeOptions;
|
||
using vde::plugin::ExternalServiceResult;
|
||
using vde::plugin::PluginManager;
|
||
|
||
#ifndef VDE_EXTERNAL_GCS_SERVICE_DIR
|
||
#define VDE_EXTERNAL_GCS_SERVICE_DIR ""
|
||
#endif
|
||
#ifndef VDE_TEST_SERVICE_EXE
|
||
#define VDE_TEST_SERVICE_EXE ""
|
||
#endif
|
||
|
||
namespace {
|
||
|
||
void set_test_env(const char* name, const char* value) {
|
||
#if defined(_WIN32)
|
||
_putenv_s(name, value ? value : "");
|
||
#else
|
||
if (value)
|
||
setenv(name, value, 1);
|
||
else
|
||
unsetenv(name);
|
||
#endif
|
||
}
|
||
|
||
std::string read_text(const fs::path& path) {
|
||
std::ifstream in(path.string(), std::ios::binary);
|
||
std::ostringstream stream;
|
||
stream << in.rdbuf();
|
||
return stream.str();
|
||
}
|
||
|
||
bool write_text(const fs::path& path, const std::string& text) {
|
||
std::ofstream out(path.string(), std::ios::binary | std::ios::trunc);
|
||
if (!out) return false;
|
||
out.write(text.data(), static_cast<std::streamsize>(text.size()));
|
||
return static_cast<bool>(out);
|
||
}
|
||
|
||
std::string make_valid_gcs_request(const std::string& request_id) {
|
||
return std::string("{") +
|
||
"\"request_id\":\"" + request_id + "\"," +
|
||
"\"operation\":\"gcs.solve\"," +
|
||
"\"schema_version\":1," +
|
||
"\"solver\":\"vdcs\"," +
|
||
"\"sketch\":{" +
|
||
"\"points\":["
|
||
"{\"id\":\"p1\",\"x\":0,\"y\":0,\"fix\":true},"
|
||
"{\"id\":\"p2\",\"x\":10,\"y\":0},"
|
||
"{\"id\":\"pc\",\"x\":5,\"y\":5}]," +
|
||
"\"lines\":[{\"id\":\"l1\",\"p1\":\"p1\",\"p2\":\"p2\"}]," +
|
||
"\"arcs\":[{\"id\":\"a1\",\"center\":\"pc\",\"r\":5,\"a0\":0,\"a1\":90}]," +
|
||
"\"circles\":[{\"id\":\"c1\",\"center\":\"pc\",\"r\":3}]," +
|
||
"\"constraints\":["
|
||
"{\"id\":\"c1\",\"type\":\"coincident\",\"refs\":[\"p1\",\"p2\"]},"
|
||
"{\"id\":\"c2\",\"type\":\"horizontal\",\"refs\":[\"l1\"]},"
|
||
"{\"id\":\"c3\",\"type\":\"radius\",\"refs\":[\"a1\"],\"value\":5}]," +
|
||
"\"parameters\":["
|
||
"{\"name\":\"p2.x\",\"value\":20,\"driving\":true},"
|
||
"{\"name\":\"p2.y\",\"value\":5,\"driving\":true}]}," +
|
||
"\"options\":{}}";
|
||
}
|
||
|
||
fs::path make_helper_service_dir(const fs::path& root, const std::string& id,
|
||
const std::vector<std::string>& env_allow) {
|
||
const fs::path dir = root / ("svc_" + id);
|
||
fs::create_directories(dir);
|
||
std::string env_json = "[";
|
||
for (std::size_t i = 0; i < env_allow.size(); ++i) {
|
||
if (i != 0) env_json += ",";
|
||
env_json += "\"" + env_allow[i] + "\"";
|
||
}
|
||
env_json += "]";
|
||
const std::string exe = fs::path(VDE_TEST_SERVICE_EXE).generic_string();
|
||
std::string manifest = "{\n";
|
||
manifest += " \"id\": \"" + id + "\",\n";
|
||
manifest += " \"name\": \"VDE Test External Service\",\n";
|
||
manifest += " \"version\": \"0.1.0\",\n";
|
||
manifest += " \"protocol\": { \"name\": \"vde-service\", \"version\": \"0.1\" },\n";
|
||
manifest += " \"executable\": { \"windows\": \"" + exe + "\", \"linux\": \"" + exe + "\" },\n";
|
||
manifest += " \"capabilities\": [\"service:test.echo\"],\n";
|
||
manifest += " \"timeouts\": { \"request_ms\": 5000 },\n";
|
||
manifest += " \"sandbox\": { \"network\": false, \"env_allow\": " + env_json + " },\n";
|
||
manifest += " \"license\": \"Apache-2.0\"\n";
|
||
manifest += "}\n";
|
||
write_text(dir / "vde-service.json", manifest);
|
||
return dir;
|
||
}
|
||
|
||
class GcsExternalServiceTest : public ::testing::Test {
|
||
protected:
|
||
void SetUp() override {
|
||
root_ = fs::temp_directory_path() /
|
||
("vde_gcs_service_test_" +
|
||
std::to_string(static_cast<long long>(
|
||
std::chrono::steady_clock::now().time_since_epoch().count())));
|
||
fs::create_directories(root_);
|
||
auto& manager = PluginManager::instance();
|
||
manager.clear();
|
||
manager.clear_external_services();
|
||
manager.clear_service_search_paths();
|
||
manager.set_service_sandbox_root((root_ / "sandbox").string());
|
||
set_test_env("VDE_TEST_SERVICE_MODE", nullptr);
|
||
}
|
||
|
||
void TearDown() override {
|
||
auto& manager = PluginManager::instance();
|
||
manager.clear();
|
||
manager.clear_external_services();
|
||
manager.clear_service_search_paths();
|
||
manager.set_service_sandbox_root("");
|
||
std::error_code remove_ec;
|
||
fs::remove_all(root_, remove_ec);
|
||
set_test_env("VDE_TEST_SERVICE_MODE", nullptr);
|
||
}
|
||
|
||
bool scan_gcs(std::string* error = nullptr) {
|
||
std::string local_error;
|
||
const std::size_t added =
|
||
PluginManager::instance().scan_service_directory(VDE_EXTERNAL_GCS_SERVICE_DIR,
|
||
&local_error);
|
||
if (error) *error = local_error;
|
||
return added >= 1u;
|
||
}
|
||
|
||
fs::path make_work_dir(const char* name) {
|
||
const fs::path dir = root_ / "sandbox" / name;
|
||
fs::create_directories(dir);
|
||
return dir;
|
||
}
|
||
|
||
ExternalServiceResult invoke(const std::string& service_id, const std::string& request,
|
||
const fs::path& work_dir, int timeout_ms = 0) {
|
||
ExternalServiceInvokeOptions options;
|
||
options.work_dir = work_dir.string();
|
||
options.timeout_ms = timeout_ms;
|
||
ExternalServiceResult result;
|
||
std::string error;
|
||
PluginManager::instance().invoke_external_service(service_id, request, options, &result,
|
||
&error);
|
||
return result;
|
||
}
|
||
|
||
fs::path root_;
|
||
};
|
||
|
||
TEST_F(GcsExternalServiceTest, ScanDiscoversGcsSolverCapability) {
|
||
std::string error;
|
||
ASSERT_TRUE(scan_gcs(&error)) << error;
|
||
auto& manager = PluginManager::instance();
|
||
const auto found = manager.external_service("com.vde.example.gcs_solver");
|
||
ASSERT_TRUE(found.has_value());
|
||
EXPECT_EQ(found->protocol_name, "vde-service");
|
||
EXPECT_EQ(found->protocol_version, "0.1");
|
||
EXPECT_EQ(found->request_timeout_ms, 15000);
|
||
ASSERT_EQ(found->capabilities.size(), 1u);
|
||
EXPECT_EQ(found->capabilities.front(), "gcs:solve");
|
||
EXPECT_TRUE(fs::is_regular_file(found->executable));
|
||
EXPECT_NE(found->name.find("GCS"), std::string::npos);
|
||
}
|
||
|
||
TEST_F(GcsExternalServiceTest, GcsSolveEndToEndReturnsPositionsAndDof) {
|
||
std::string error;
|
||
ASSERT_TRUE(scan_gcs(&error)) << error;
|
||
const ExternalServiceResult result =
|
||
invoke("com.vde.example.gcs_solver", make_valid_gcs_request("gcs-e2e-1"),
|
||
make_work_dir("e2e"));
|
||
ASSERT_TRUE(result.ok) << result.error;
|
||
EXPECT_EQ(result.status, "ok");
|
||
EXPECT_EQ(result.request_id, "gcs-e2e-1");
|
||
EXPECT_FALSE(result.timed_out);
|
||
EXPECT_FALSE(result.protocol_error);
|
||
EXPECT_FALSE(result.crashed);
|
||
EXPECT_EQ(result.exit_code, 0);
|
||
const std::string& json = result.response_json;
|
||
EXPECT_NE(json.find("\"schema_version\":1"), std::string::npos);
|
||
EXPECT_NE(json.find("\"status\":\"ok\""), std::string::npos);
|
||
EXPECT_NE(json.find("\"positions\""), std::string::npos);
|
||
// 驱动参数 p2.x=20 / p2.y=5 生效
|
||
EXPECT_NE(json.find("\"p2\":{\"x\":20,\"y\":5}"), std::string::npos) << json;
|
||
EXPECT_NE(json.find("\"dof\":{\"total\":6"), std::string::npos) << json;
|
||
EXPECT_NE(json.find("\"constraint_status\""), std::string::npos);
|
||
EXPECT_NE(json.find("\"id\":\"c1\",\"status\":\"ok\""), std::string::npos) << json;
|
||
EXPECT_NE(json.find("mock GCS solver"), std::string::npos);
|
||
EXPECT_NE(json.find("\"metrics\""), std::string::npos);
|
||
EXPECT_TRUE(result.stderr_excerpt.empty());
|
||
}
|
||
|
||
TEST_F(GcsExternalServiceTest, GcsSolveTimeoutIsolated) {
|
||
std::string error;
|
||
ASSERT_TRUE(scan_gcs(&error)) << error;
|
||
std::string request = make_valid_gcs_request("gcs-timeout-1");
|
||
const std::string marker = "\"options\":{}}";
|
||
const std::size_t pos = request.find(marker);
|
||
ASSERT_NE(pos, std::string::npos);
|
||
request.replace(pos, marker.size(), "\"options\":{\"sleep_ms\":5000}}");
|
||
const ExternalServiceResult result =
|
||
invoke("com.vde.example.gcs_solver", request, make_work_dir("timeout"), 200);
|
||
EXPECT_FALSE(result.ok);
|
||
EXPECT_TRUE(result.started);
|
||
EXPECT_TRUE(result.timed_out);
|
||
EXPECT_LT(result.duration_ms, 5000);
|
||
const ExternalServiceResult recovered =
|
||
invoke("com.vde.example.gcs_solver", make_valid_gcs_request("gcs-timeout-2"),
|
||
make_work_dir("timeout_recovered"));
|
||
EXPECT_TRUE(recovered.ok) << recovered.error;
|
||
}
|
||
|
||
TEST_F(GcsExternalServiceTest, GcsBadResponseIsProtocolError) {
|
||
auto& manager = PluginManager::instance();
|
||
const fs::path dir =
|
||
make_helper_service_dir(root_, "com.vde.test.gcs_badjson", {"VDE_TEST_SERVICE_MODE"});
|
||
std::string error;
|
||
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
|
||
set_test_env("VDE_TEST_SERVICE_MODE", "badjson");
|
||
const ExternalServiceResult result =
|
||
invoke("com.vde.test.gcs_badjson", make_valid_gcs_request("gcs-bad-1"),
|
||
make_work_dir("badjson"));
|
||
EXPECT_FALSE(result.ok);
|
||
EXPECT_TRUE(result.protocol_error);
|
||
EXPECT_FALSE(result.timed_out);
|
||
EXPECT_NE(result.error.find("invalid response JSON"), std::string::npos);
|
||
}
|
||
|
||
TEST_F(GcsExternalServiceTest, GcsUnknownOperationIsolated) {
|
||
std::string error;
|
||
ASSERT_TRUE(scan_gcs(&error)) << error;
|
||
std::string request = make_valid_gcs_request("gcs-unknown-1");
|
||
const std::size_t pos = request.find("\"operation\":\"gcs.solve\"");
|
||
ASSERT_NE(pos, std::string::npos);
|
||
request.replace(pos, std::string("\"operation\":\"gcs.solve\"").size(),
|
||
"\"operation\":\"gcs:rebuild\"");
|
||
const ExternalServiceResult result =
|
||
invoke("com.vde.example.gcs_solver", request, make_work_dir("unknown_op"));
|
||
EXPECT_FALSE(result.ok);
|
||
EXPECT_TRUE(result.protocol_error);
|
||
EXPECT_EQ(result.error_code, "unsupported_operation");
|
||
const ExternalServiceResult recovered =
|
||
invoke("com.vde.example.gcs_solver", make_valid_gcs_request("gcs-unknown-2"),
|
||
make_work_dir("unknown_op_recovered"));
|
||
EXPECT_TRUE(recovered.ok) << recovered.error;
|
||
}
|
||
|
||
TEST_F(GcsExternalServiceTest, GcsInvalidSketchRejectedAndRecoverable) {
|
||
std::string error;
|
||
ASSERT_TRUE(scan_gcs(&error)) << error;
|
||
const std::string bad_request =
|
||
"{\"request_id\":\"gcs-bad-sketch\",\"operation\":\"gcs.solve\",\"schema_version\":1,"
|
||
"\"solver\":\"vdcs\",\"sketch\":{},\"options\":{}}";
|
||
const ExternalServiceResult result =
|
||
invoke("com.vde.example.gcs_solver", bad_request, make_work_dir("bad_sketch"));
|
||
EXPECT_FALSE(result.ok);
|
||
EXPECT_TRUE(result.protocol_error);
|
||
EXPECT_EQ(result.error_code, "invalid_sketch");
|
||
const ExternalServiceResult recovered =
|
||
invoke("com.vde.example.gcs_solver", make_valid_gcs_request("gcs-bad-sketch-2"),
|
||
make_work_dir("bad_sketch_recovered"));
|
||
EXPECT_TRUE(recovered.ok) << recovered.error;
|
||
}
|
||
|
||
TEST_F(GcsExternalServiceTest, GcsSchemaVersionAndConstraintErrors) {
|
||
std::string error;
|
||
ASSERT_TRUE(scan_gcs(&error)) << error;
|
||
std::string request = make_valid_gcs_request("gcs-schema-2");
|
||
const std::size_t pos = request.find("\"schema_version\":1");
|
||
ASSERT_NE(pos, std::string::npos);
|
||
request.replace(pos, std::string("\"schema_version\":1").size(), "\"schema_version\":2");
|
||
ExternalServiceResult result =
|
||
invoke("com.vde.example.gcs_solver", request, make_work_dir("schema"));
|
||
EXPECT_FALSE(result.ok);
|
||
EXPECT_EQ(result.error_code, "unsupported_schema_version");
|
||
request = make_valid_gcs_request("gcs-magic-constraint");
|
||
const std::size_t type_pos = request.find("\"type\":\"horizontal\"");
|
||
ASSERT_NE(type_pos, std::string::npos);
|
||
request.replace(type_pos, std::string("\"type\":\"horizontal\"").size(),
|
||
"\"type\":\"magic_link\"");
|
||
result = invoke("com.vde.example.gcs_solver", request, make_work_dir("magic"));
|
||
EXPECT_FALSE(result.ok);
|
||
EXPECT_EQ(result.error_code, "unsupported_constraint");
|
||
}
|
||
|
||
TEST_F(GcsExternalServiceTest, GcsConflictDiagnosticsReported) {
|
||
std::string error;
|
||
ASSERT_TRUE(scan_gcs(&error)) << error;
|
||
std::string request = make_valid_gcs_request("gcs-conflict-1");
|
||
const std::string marker = "\"options\":{}}";
|
||
const std::size_t pos = request.find(marker);
|
||
ASSERT_NE(pos, std::string::npos);
|
||
request.replace(pos, marker.size(), "\"options\":{\"mock_force_conflict\":true}}");
|
||
const ExternalServiceResult result =
|
||
invoke("com.vde.example.gcs_solver", request, make_work_dir("conflict"));
|
||
EXPECT_TRUE(result.ok) << result.error;
|
||
EXPECT_NE(result.response_json.find("\"solve_status\":\"infeasible\""), std::string::npos);
|
||
EXPECT_NE(result.response_json.find("\"status\":\"conflict\""), std::string::npos);
|
||
}
|
||
|
||
TEST_F(GcsExternalServiceTest, GcsCapabilityDiscoveryFiltersOtherServices) {
|
||
std::string error;
|
||
ASSERT_TRUE(scan_gcs(&error)) << error;
|
||
const fs::path helper_dir =
|
||
make_helper_service_dir(root_, "com.vde.test.gcs_other", {});
|
||
ASSERT_GE(PluginManager::instance().scan_service_directory(helper_dir.string(), &error), 1u)
|
||
<< error;
|
||
std::vector<ExternalServiceInfo> gcs_providers;
|
||
for (const auto& service : PluginManager::instance().external_services()) {
|
||
for (const auto& capability : service.capabilities) {
|
||
if (capability == "gcs:solve") gcs_providers.push_back(service);
|
||
}
|
||
}
|
||
ASSERT_EQ(gcs_providers.size(), 1u);
|
||
EXPECT_EQ(gcs_providers.front().id, "com.vde.example.gcs_solver");
|
||
const auto other = PluginManager::instance().external_service("com.vde.test.gcs_other");
|
||
ASSERT_TRUE(other.has_value());
|
||
EXPECT_TRUE(other->capabilities.empty() ||
|
||
other->capabilities.front() != "gcs:solve");
|
||
}
|
||
|
||
} // namespace
|