Files
ViewDesignEngine/tests/plugin/test_plugin_abi15.cpp
T

265 lines
11 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// P4 / ABI 1.5/1.6:Optimizer + IO_BATCH 扩展点测试(ABI 1.6 = IO_BATCH run_v2)。
//
// 覆盖:
// 1) ABI 1.6 二进制布局:minor=6、OPTIMIZER=9/IO_BATCH=10、Registration 72B、
// optimizer offset 56 / io_batch offset 64(旧 1.4=56B 安全);
// 2) sample_optimizer 发现/注册/invoke(options_json + input JSON → objective/iterations);
// 3) sample_io_batch 发现/注册/invoke(format options + request JSON → processed/failed);
// 4) 失败隔离/卸载清理;5) 旧 1.4 56B 格式插件正常加载;
// 6) 新 kind 仍用 56B 注册结构 → 明确拒绝;7) ABI 矩阵 1.5 accepted / 2.0 rejected。
#include <gtest/gtest.h>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include "vde/plugin/geometry_host_services.h"
#include "vde/plugin/plugin_manager.h"
#include "vde/plugin/vde_plugin_api.h"
#ifndef VDE_PLUGIN_DIRS
#define VDE_PLUGIN_DIRS ""
#endif
#ifndef VDE_ABI_MATRIX_DIR
#define VDE_ABI_MATRIX_DIR ""
#endif
#ifndef VDE_ABI15_FIXTURE_DIR
#define VDE_ABI15_FIXTURE_DIR ""
#endif
namespace {
using vde::plugin::IoBatchInfo;
using vde::plugin::OptimizerInfo;
using vde::plugin::PluginManager;
constexpr const char* kOptimizerPluginId = "com.vde.sample.optimizer";
constexpr const char* kOptimizerId = "optimizer.mock.gradient";
constexpr const char* kIoBatchPluginId = "com.vde.sample.io_batch";
constexpr const char* kIoBatchId = "io_batch.mock";
bool contains(const std::string& text, const std::string& needle) {
return text.find(needle) != std::string::npos;
}
class Abi15Fixture : public ::testing::Test {
protected:
void SetUp() override {
host_ = vde::plugin::make_geometry_host_services();
PluginManager& manager = PluginManager::instance();
manager.clear();
manager.set_host_services(host_);
manager.clear_search_paths();
if (VDE_PLUGIN_DIRS[0] != '\0') manager.add_search_path(VDE_PLUGIN_DIRS);
manager.scan_search_paths();
}
void TearDown() override {
PluginManager::instance().clear();
vde::plugin::release_geometry_host_services(&host_);
}
VdePluginHost host_{};
};
TEST(Abi15LayoutTest, RegistrationAndStructSizes) {
static_assert(VDE_PLUGIN_ABI_MAJOR == 1, "ABI major must stay 1");
EXPECT_EQ(VDE_PLUGIN_ABI_MINOR, 9);
EXPECT_EQ(static_cast<int>(VDE_PLUGIN_KIND_OPTIMIZER), 9);
EXPECT_EQ(static_cast<int>(VDE_PLUGIN_KIND_IO_BATCH), 10);
EXPECT_EQ(offsetof(VdeOptimizerOptions, struct_size), 0u);
EXPECT_EQ(offsetof(VdeOptimizerResult, struct_size), 0u);
EXPECT_EQ(offsetof(VdeOptimizerPlugin, struct_size), 0u);
EXPECT_EQ(offsetof(VdeIoBatchOptions, struct_size), 0u);
EXPECT_EQ(offsetof(VdeIoBatchResult, struct_size), 0u);
EXPECT_EQ(offsetof(VdeIoBatchPlugin, struct_size), 0u);
if (sizeof(void*) == 8) {
// ABI 1.6:run_v2 尾追加;旧 1.5 struct_size = 40B。
EXPECT_EQ(offsetof(VdeIoBatchPlugin, run), 24u);
EXPECT_EQ(offsetof(VdeIoBatchPlugin, describe), 32u);
EXPECT_EQ(offsetof(VdeIoBatchPlugin, run_v2), 40u);
EXPECT_EQ(sizeof(VdeIoBatchPlugin), 48u);
}
if (sizeof(void*) == 8) {
// 1.4=56B,尾追加 optimizer+io_batch 两指针 → 72B。
EXPECT_EQ(sizeof(VdePluginRegistration), 96u);
EXPECT_EQ(offsetof(VdePluginRegistration, optimizer), 56u);
EXPECT_EQ(offsetof(VdePluginRegistration, io_batch), 64u);
EXPECT_EQ(offsetof(VdePluginRegistration, mesher) + sizeof(const VdeMesherPlugin*), 56u);
EXPECT_EQ(offsetof(VdePluginRegistration, io_batch) + sizeof(const VdeIoBatchPlugin*),
72u);
}
}
TEST_F(Abi15Fixture, DiscoversAndInvokesOptimizer) {
PluginManager& manager = PluginManager::instance();
std::string error;
ASSERT_TRUE(manager.load(kOptimizerPluginId, &error)) << error;
ASSERT_TRUE(manager.is_loaded(kOptimizerPluginId));
const auto manifest = manager.manifest(kOptimizerPluginId);
ASSERT_TRUE(manifest.has_value());
EXPECT_EQ(manifest->kind, vde::plugin::PluginKind::Optimizer);
EXPECT_EQ(manifest->abi_minor, 5);
const std::vector<OptimizerInfo> optimizers = manager.optimizers();
ASSERT_EQ(optimizers.size(), 1u);
EXPECT_EQ(optimizers[0].optimizer_id, kOptimizerId);
EXPECT_TRUE(contains(optimizers[0].capabilities_json, "mock"));
const std::string input = "{\"variables\":[{\"name\":\"x\",\"init\":1.0}],\"objective\":\"x^2\"}";
std::string result_json;
ASSERT_TRUE(manager.invoke_optimizer(kOptimizerId,
"{\"max_iterations\":7,\"tolerance\":1e-3}",
input.data(), input.size(), &result_json, &error))
<< error;
EXPECT_TRUE(contains(result_json, "\"optimizer_id\":\"optimizer.mock.gradient\""));
EXPECT_TRUE(contains(result_json, "\"objective_value\""));
EXPECT_TRUE(contains(result_json, "\"iterations\":3"));
EXPECT_TRUE(contains(result_json, "\"input_bytes\":" + std::to_string(input.size())));
error.clear();
EXPECT_FALSE(manager.invoke_optimizer("optimizer.unknown", "", input.data(), input.size(),
&result_json, &error));
EXPECT_TRUE(contains(error, "not registered"));
error.clear();
EXPECT_FALSE(manager.invoke_optimizer(kOptimizerId, "", nullptr, 0, &result_json, &error));
EXPECT_TRUE(contains(error, "empty input"));
error.clear();
EXPECT_FALSE(manager.invoke_optimizer(kOptimizerId, "{\"flags\":3}", input.data(),
input.size(), &result_json, &error));
EXPECT_TRUE(contains(error, "flags"));
error.clear();
ASSERT_TRUE(manager.unload(kOptimizerPluginId, &error)) << error;
EXPECT_TRUE(manager.optimizers().empty());
EXPECT_FALSE(manager.invoke_optimizer(kOptimizerId, "", input.data(), input.size(),
&result_json, &error));
}
TEST_F(Abi15Fixture, DiscoversAndInvokesIoBatch) {
PluginManager& manager = PluginManager::instance();
std::string error;
ASSERT_TRUE(manager.load(kIoBatchPluginId, &error)) << error;
const auto manifest = manager.manifest(kIoBatchPluginId);
ASSERT_TRUE(manifest.has_value());
EXPECT_EQ(manifest->kind, vde::plugin::PluginKind::IoBatch);
EXPECT_EQ(manifest->abi_minor, 6);
const std::vector<IoBatchInfo> batches = manager.io_batches();
ASSERT_EQ(batches.size(), 1u);
EXPECT_EQ(batches[0].batch_id, kIoBatchId);
EXPECT_TRUE(contains(batches[0].capabilities_json, "mock"));
const std::string request =
"{\"items\":[{\"path\":\"a.step\"},{\"path\":\"b.step\"}],\"direction\":\"import\"}";
std::string response_json;
ASSERT_TRUE(manager.invoke_io_batch(kIoBatchId, "{\"format\":\"step\"}", request.data(),
request.size(), &response_json, &error))
<< error;
EXPECT_TRUE(contains(response_json, "\"batch_id\":\"io_batch.mock\""));
EXPECT_TRUE(contains(response_json, "\"api\":\"run_v2\""));
EXPECT_TRUE(contains(response_json, "\"processed\":"));
EXPECT_TRUE(contains(response_json, "\"failed\":0"));
error.clear();
EXPECT_FALSE(manager.invoke_io_batch("io_batch.unknown", "", request.data(), request.size(),
&response_json, &error));
EXPECT_TRUE(contains(error, "not registered"));
error.clear();
EXPECT_FALSE(manager.invoke_io_batch(kIoBatchId, "", nullptr, 0, &response_json, &error));
EXPECT_TRUE(contains(error, "empty request"));
error.clear();
ASSERT_TRUE(manager.unload(kIoBatchPluginId, &error)) << error;
EXPECT_TRUE(manager.io_batches().empty());
}
TEST_F(Abi15Fixture, LegacyAbi14RegistrationLoads) {
PluginManager& manager = PluginManager::instance();
manager.clear();
manager.set_host_services(host_);
manager.clear_search_paths();
manager.add_search_path(std::string(VDE_ABI15_FIXTURE_DIR) + "/format56");
manager.scan_search_paths();
std::string error;
ASSERT_TRUE(manager.load("com.vde.test.abi15.format56", &error)) << error;
ASSERT_EQ(manager.formats().size(), 1u);
EXPECT_EQ(manager.formats()[0].format_id, "abi15.format56");
EXPECT_EQ(manager.manifest("com.vde.test.abi15.format56")->abi_minor, 4);
EXPECT_TRUE(manager.optimizers().empty());
EXPECT_TRUE(manager.io_batches().empty());
}
TEST_F(Abi15Fixture, NewKindsWith56ByteRegistrationRejected) {
PluginManager& manager = PluginManager::instance();
manager.clear();
manager.set_host_services(host_);
manager.clear_search_paths();
manager.add_search_path(std::string(VDE_ABI15_FIXTURE_DIR) + "/optimizer56");
manager.scan_search_paths();
std::string error;
EXPECT_FALSE(manager.load("com.vde.test.abi15.optimizer56", &error));
EXPECT_TRUE(contains(error, "optimizer field")) << error;
manager.clear();
manager.set_host_services(host_);
manager.clear_search_paths();
manager.add_search_path(std::string(VDE_ABI15_FIXTURE_DIR) + "/iobatch56");
manager.scan_search_paths();
error.clear();
EXPECT_FALSE(manager.load("com.vde.test.abi15.iobatch56", &error));
EXPECT_TRUE(contains(error, "io_batch field")) << error;
}
TEST_F(Abi15Fixture, AbiMatrix15To19Accepted20Rejected) {
if (VDE_ABI_MATRIX_DIR[0] == '\0') GTEST_SKIP() << "VDE_ABI_MATRIX_DIR not set";
PluginManager& manager = PluginManager::instance();
manager.clear();
manager.set_host_services(host_);
manager.clear_search_paths();
manager.add_search_path(std::string(VDE_ABI_MATRIX_DIR) + "/abi1_5");
manager.scan_search_paths();
std::string error;
ASSERT_TRUE(manager.load("com.vde.sample.vbox", &error)) << error;
EXPECT_EQ(manager.manifest("com.vde.sample.vbox")->abi_minor, 5);
manager.clear();
manager.set_host_services(host_);
manager.clear_search_paths();
manager.add_search_path(std::string(VDE_ABI_MATRIX_DIR) + "/abi1_6");
manager.scan_search_paths();
ASSERT_TRUE(manager.load("com.vde.sample.vbox", &error)) << error;
EXPECT_EQ(manager.manifest("com.vde.sample.vbox")->abi_minor, 6);
manager.clear();
manager.set_host_services(host_);
manager.clear_search_paths();
manager.add_search_path(std::string(VDE_ABI_MATRIX_DIR) + "/abi1_7");
manager.scan_search_paths();
ASSERT_TRUE(manager.load("com.vde.sample.vbox", &error)) << error;
EXPECT_EQ(manager.manifest("com.vde.sample.vbox")->abi_minor, 7);
manager.clear();
manager.set_host_services(host_);
manager.clear_search_paths();
manager.add_search_path(std::string(VDE_ABI_MATRIX_DIR) + "/abi1_8");
manager.scan_search_paths();
ASSERT_TRUE(manager.load("com.vde.sample.vbox", &error)) << error;
EXPECT_EQ(manager.manifest("com.vde.sample.vbox")->abi_minor, 8);
manager.clear();
manager.set_host_services(host_);
manager.clear_search_paths();
manager.add_search_path(std::string(VDE_ABI_MATRIX_DIR) + "/abi1_9");
manager.scan_search_paths();
ASSERT_TRUE(manager.load("com.vde.sample.vbox", &error)) << error;
EXPECT_EQ(manager.manifest("com.vde.sample.vbox")->abi_minor, 9);
manager.clear();
manager.set_host_services(host_);
manager.clear_search_paths();
manager.add_search_path(std::string(VDE_ABI_MATRIX_DIR) + "/abi2_0");
manager.scan_search_paths();
error.clear();
EXPECT_FALSE(manager.load("com.vde.sample.vbox", &error));
EXPECT_TRUE(contains(error, "ABI"));
}
} // namespace