Files
ViewDesignEngine/tests/plugin/test_plugin_mesher.cpp
T
hm e27068f828 feat(P4-11): 插件 ABI 1.5 optimizer + io_batch 扩展点(1.0.83)
### ABI 1.5
- VDE_PLUGIN_ABI_MINOR 5;VdePluginKind OPTIMIZER=9/IO_BATCH=10;
  VdeOptimizer*/VdeIoBatch* 结构(JSON 输入输出回调);VdePluginRegistration 尾追加两指针 56→72B(offset 56/64),
  旧 1.0–1.4 按 struct_size 安全加载、新 kind 结构不足拒绝;
- PluginManager:OptimizerInfo/IoBatchInfo、optimizers()/invoke_optimizer、io_batches()/invoke_io_batch、
  校验/失败隔离/卸载清理(IO_BATCH 支持 PARTIAL);
- sample_optimizer/sample_io_batch(mock);ABI 矩阵 1.0–1.5 accepted=6+2.0 rejected;--abi/--check 适配;
- test_plugin_abi15 6/6 + fixtures;既有 ABI 1.4 断言适配;
- 文档 plugin-development §16/§17、plugin-distribution ABI 矩阵、plugin-ecosystem-handoff/ecosystem、plugin-ci-template;

### WP/claim
- WP-P4-11 交付并移入 Delivered;claim Released 归档;统计 42=31 Delivered+11 Candidate;
- claim check 修正:Released claim 可指向 Delivered WP(仅 Active 需要释放);

### 工程
- 版本 1.0.83;全量构建 0 error(421 targets);全量 ctest 7576 注册、7481/7481 PASS(0 failed,25 skipped,302.66s);
- 收录 1.0.82 独立验证报告 80。
2026-10-29 18:44:12 +08:00

210 lines
8.8 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.4:Mesher(网格生成)扩展点测试。
//
// 覆盖:
// 1) ABI 1.4 二进制布局:minor=4、kind MESHER=8、VdePluginRegistration 56B(64-bit)、
// mesher 指针 offset 48(旧 1.3 插件 struct_size=48 安全);
// 2) sample_mesher 发现/加载/注册:kind=mesher、capability mesher:tet、meshers() 查询;
// 3) invoke_mesher 成功:options JSON(element_size/tolerance)传递、结果 JSON/计数;
// 4) 失败隔离:未知 mesher / 空模型 / 非法 flags / 卸载后调用 → false+error,之后可恢复;
// 5) 旧 ABI 兼容:真实 48B struct_size 的 1.3 格式插件可加载;同布局 mesher 声明被明确拒绝;
// 6) 旧 ABI 矩阵 abi1_3(manifest ABI 1.3)仍被宿主 1.4 接受,2.0 被拒绝;
// 7) vde_plugin_tool --abi 打印 1.4/VdeMesherPlugin;--check 接受 sample_mesher。
#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_LEGACY_FORMAT_DIR
#define VDE_LEGACY_FORMAT_DIR ""
#endif
#ifndef VDE_LEGACY_MESHER48_DIR
#define VDE_LEGACY_MESHER48_DIR ""
#endif
namespace {
using vde::plugin::MesherInfo;
using vde::plugin::PluginManager;
constexpr const char* kMesherPluginId = "com.vde.sample.mesher";
constexpr const char* kMesherId = "mesher.mock.tet4";
bool contains(const std::string& text, const std::string& needle) {
return text.find(needle) != std::string::npos;
}
class MesherExtensionFixture : 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_);
}
void load_sample(std::string* error) {
PluginManager& manager = PluginManager::instance();
if (manager.is_loaded(kMesherPluginId)) return;
ASSERT_TRUE(manager.load(kMesherPluginId, error)) << (error ? *error : "");
}
VdePluginHost host_{};
};
TEST(MesherAbi14LayoutTest, RegistrationAndStructSizes) {
static_assert(VDE_PLUGIN_ABI_MAJOR == 1, "ABI major must stay 1");
EXPECT_EQ(VDE_PLUGIN_ABI_MINOR, 5);
EXPECT_EQ(static_cast<int>(VDE_PLUGIN_KIND_MESHER), 8);
EXPECT_GT(sizeof(VdeMesherPlugin), 0u);
EXPECT_GE(sizeof(VdeMesherOptions), 8u + sizeof(double) * 2 + sizeof(uint32_t) * 2);
EXPECT_GE(sizeof(VdeMesherResult), sizeof(VdeBuffer) + sizeof(uint32_t) * 6);
EXPECT_EQ(offsetof(VdeMesherOptions, struct_size), 0u);
EXPECT_EQ(offsetof(VdeMesherResult, struct_size), 0u);
EXPECT_EQ(offsetof(VdeMesherPlugin, struct_size), 0u);
EXPECT_GE(offsetof(VdePluginRegistration, mesher), offsetof(VdePluginRegistration, gcs_solver));
if (sizeof(void*) == 8) {
// 旧 ABI 1.3 注册结构 48B;ABI 1.4 尾追加 mesher 指针后 56B。
EXPECT_EQ(offsetof(VdePluginRegistration, mesher), 48u);
EXPECT_EQ(sizeof(VdePluginRegistration), 72u);
EXPECT_EQ(offsetof(VdePluginRegistration, mesher) + sizeof(const VdeMesherPlugin*), 56u);
EXPECT_EQ(offsetof(VdePluginRegistration, optimizer), 56u);
EXPECT_EQ(offsetof(VdePluginRegistration, io_batch), 64u);
}
}
TEST_F(MesherExtensionFixture, DiscoversAndRegistersSampleMesher) {
std::string error;
load_sample(&error);
PluginManager& manager = PluginManager::instance();
ASSERT_TRUE(manager.is_loaded(kMesherPluginId)) << error;
const auto manifest = manager.manifest(kMesherPluginId);
ASSERT_TRUE(manifest.has_value());
EXPECT_EQ(manifest->abi_major, 1);
EXPECT_EQ(manifest->abi_minor, 4);
EXPECT_EQ(manifest->kind, vde::plugin::PluginKind::Mesher);
const std::vector<MesherInfo> meshers = manager.meshers();
ASSERT_EQ(meshers.size(), 1u);
EXPECT_EQ(meshers[0].mesher_id, kMesherId);
EXPECT_TRUE(contains(meshers[0].capabilities_json, "tet4"));
}
TEST_F(MesherExtensionFixture, InvokeSuccessWithOptions) {
std::string error;
load_sample(&error);
PluginManager& manager = PluginManager::instance();
const std::string model = "VDE-TEXT\nbody box 10 20 30\n";
std::string mesh_json;
ASSERT_TRUE(manager.invoke_mesher(kMesherId, model.data(), model.size(),
"{\"element_size\":2.5,\"tolerance\":1e-4}",
&mesh_json, &error))
<< error;
EXPECT_TRUE(contains(mesh_json, "\"ok\":true"));
EXPECT_TRUE(contains(mesh_json, "\"mesher_id\":\"mesher.mock.tet4\""));
EXPECT_TRUE(contains(mesh_json, "\"nodes\":8"));
EXPECT_TRUE(contains(mesh_json, "\"elements\":5"));
EXPECT_TRUE(contains(mesh_json, "\"model_bytes\":" + std::to_string(model.size())));
EXPECT_TRUE(contains(mesh_json, "\"element_size\":2.5"));
EXPECT_TRUE(contains(mesh_json, "\"tolerance\":0.0001"));
EXPECT_TRUE(contains(mesh_json, "\"tets\":["));
}
TEST_F(MesherExtensionFixture, ErrorsAreIsolated) {
std::string error;
load_sample(&error);
PluginManager& manager = PluginManager::instance();
std::string mesh_json;
EXPECT_FALSE(manager.invoke_mesher("mesher.unknown", "x", 1, "", &mesh_json, &error));
EXPECT_TRUE(contains(error, "not registered"));
error.clear();
EXPECT_FALSE(manager.invoke_mesher(kMesherId, nullptr, 0, "", &mesh_json, &error));
EXPECT_TRUE(contains(error, "empty model data"));
error.clear();
EXPECT_FALSE(manager.invoke_mesher(kMesherId, "x", 1, "{\"flags\":7}", &mesh_json, &error));
EXPECT_TRUE(contains(error, "flags"));
// 失败隔离:后续正常调用不受影响。
const std::string model = "VDE-TEXT\n";
ASSERT_TRUE(manager.invoke_mesher(kMesherId, model.data(), model.size(), "", &mesh_json,
&error))
<< error;
EXPECT_TRUE(contains(mesh_json, "\"ok\":true"));
EXPECT_TRUE(manager.unload(kMesherPluginId, &error)) << error;
EXPECT_TRUE(manager.meshers().empty());
error.clear();
EXPECT_FALSE(manager.invoke_mesher(kMesherId, model.data(), model.size(), "", &mesh_json,
&error));
EXPECT_TRUE(contains(error, "not registered"));
}
TEST_F(MesherExtensionFixture, LegacyAbi13RegistrationStillLoads) {
PluginManager& manager = PluginManager::instance();
manager.clear();
manager.set_host_services(host_);
manager.clear_search_paths();
if (VDE_LEGACY_FORMAT_DIR[0] != '\0') manager.add_search_path(VDE_LEGACY_FORMAT_DIR);
manager.scan_search_paths();
std::string error;
ASSERT_TRUE(manager.load("com.vde.test.legacy.format", &error)) << error;
const auto manifest = manager.manifest("com.vde.test.legacy.format");
ASSERT_TRUE(manifest.has_value());
EXPECT_EQ(manifest->abi_minor, 3);
// 旧插件只填到 gcs_solver(struct_size=48),宿主不得读取 mesher 字段。
ASSERT_EQ(manager.formats().size(), 1u);
EXPECT_EQ(manager.formats()[0].format_id, "legacy.format");
EXPECT_TRUE(manager.meshers().empty());
}
TEST_F(MesherExtensionFixture, Legacy48ByteMesherRegistrationRejected) {
PluginManager& manager = PluginManager::instance();
manager.clear();
manager.set_host_services(host_);
manager.clear_search_paths();
if (VDE_LEGACY_MESHER48_DIR[0] != '\0') manager.add_search_path(VDE_LEGACY_MESHER48_DIR);
manager.scan_search_paths();
std::string error;
EXPECT_FALSE(manager.load("com.vde.test.legacy.mesher48", &error));
EXPECT_TRUE(contains(error, "mesher field"));
}
TEST_F(MesherExtensionFixture, AbiMatrix13Accepted20Rejected) {
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_3");
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, 3);
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