#include #include "vde/fs_compat.h" #include "vde/plugin/geometry_host_services.h" #include "vde/plugin/plugin_manager.h" #include #include #include #include #include #include // ═══════════════════════════════════════════════════════════ // Wave 8 补测: 插件生命周期 / 能力注册边界 // // 覆盖此前未执行的路径: // - CapabilityRegistry 冲突记录、按提供者移除、空 ID 忽略、providers 去重 // - PluginManager 空状态下查询 (plugins/formats/stats/manifest/diagnostics) // - 未发现插件的 load/unload/reload 错误语义与 last_error // - set_host_services 的 struct_size / ABI major 校验边界 // - 合成清单的 scan_directory 发现、二次扫描幂等、plugins/manifest 查询 // ═══════════════════════════════════════════════════════════ namespace fs = vde::fs; using vde::plugin::CapabilityRegistry; using vde::plugin::PluginManager; namespace { /// 临时目录 RAII(与 test_plugin_manager.cpp 一致的做法) class TempDir { public: TempDir() { std::error_code ec; const auto stamp = std::chrono::steady_clock::now().time_since_epoch().count(); path_ = fs::temp_directory_path(ec) / ("vde_cov_plugin_" + std::to_string(stamp)); fs::create_directories(path_, ec); } ~TempDir() { std::error_code ec; fs::remove_all(path_, ec); } TempDir(const TempDir&) = delete; TempDir& operator=(const TempDir&) = delete; const fs::path& path() const { return path_; } fs::path file(const std::string& relative) const { return path_ / relative; } bool write(const std::string& relative, const std::string& content) const { const fs::path target = file(relative); std::error_code ec; if (!target.parent_path().empty()) fs::create_directories(target.parent_path(), ec); std::ofstream out(target.string(), std::ios::binary); if (!out) return false; out << content; return static_cast(out); } private: fs::path path_; }; void reset_manager() { PluginManager& manager = PluginManager::instance(); manager.clear(); manager.clear_search_paths(); manager.clear_host_services(); } std::string manifest_json(const std::string& id, const std::string& version, const std::string& kind, int abi_major, int abi_minor, const std::string& library) { std::ostringstream os; os << "{\n" << " \"id\": \"" << id << "\",\n" << " \"name\": \"" << id << " display\",\n" << " \"version\": \"" << version << "\",\n" << " \"kind\": \"" << kind << "\",\n" << " \"abi\": { \"major\": " << abi_major << ", \"minor\": " << abi_minor << " },\n" << " \"entry\": { \"windows\": \"" << library << "\", \"linux\": \"" << library << "\", \"macos\": \"" << library << "\" },\n" << " \"capabilities\": [\"format:demo.read\"],\n" << " \"dependencies\": [],\n" << " \"license\": \"Apache-2.0\"\n" << "}\n"; return os.str(); } void write_plugin_manifest(const TempDir& dir, const std::string& subdir, const std::string& id) { dir.write(subdir + "/plugin.dll", "not a real dynamic library"); dir.write(subdir + "/vde-plugin.json", manifest_json(id, "1.0.0", "format", 1, 0, "plugin.dll")); } } // namespace // ── 能力注册表: 冲突 / 移除 / 去重 ─────────────────────── TEST(CovPluginLifecycle, RegistryConflictAndRemovalSemantics) { CapabilityRegistry registry; EXPECT_EQ(registry.size(), 0u); EXPECT_TRUE(registry.conflicts().empty()); // 空 capability / 空提供者均被忽略 registry.add("", "com.test.p1"); registry.add("format:demo.read", ""); EXPECT_EQ(registry.size(), 0u); registry.add("format:demo.read", "com.test.p1"); EXPECT_EQ(registry.size(), 1u); EXPECT_TRUE(registry.has("format:demo.read")); ASSERT_TRUE(registry.provider("format:demo.read").has_value()); EXPECT_EQ(*registry.provider("format:demo.read"), "com.test.p1"); // 同提供者重复注册: 幂等, 不产生冲突 registry.add("format:demo.read", "com.test.p1"); EXPECT_EQ(registry.size(), 1u); EXPECT_TRUE(registry.conflicts().empty()); // 不同提供者重复注册: 首次为准并记录冲突 registry.add("format:demo.read", "com.test.p2"); EXPECT_EQ(registry.size(), 1u); EXPECT_EQ(*registry.provider("format:demo.read"), "com.test.p1"); const auto conflicts = registry.conflicts(); ASSERT_EQ(conflicts.size(), 1u); EXPECT_NE(conflicts[0].find("format:demo.read"), std::string::npos); EXPECT_NE(conflicts[0].find("com.test.p1"), std::string::npos); EXPECT_NE(conflicts[0].find("com.test.p2"), std::string::npos); // providers() 去重: 冲突方 com.test.p2 未被接纳, 因此当前只有 p1 EXPECT_EQ(registry.providers().size(), 1u); EXPECT_EQ(registry.providers().front(), "com.test.p1"); // 按提供者移除: 该提供者全部能力消失 registry.add("geom:box.create", "com.test.p1"); registry.add("mesh:export", "com.test.p2"); ASSERT_EQ(registry.size(), 3u); EXPECT_EQ(registry.providers().size(), 2u); registry.remove_provider("com.test.p1"); EXPECT_FALSE(registry.has("format:demo.read")); EXPECT_FALSE(registry.has("geom:box.create")); EXPECT_TRUE(registry.has("mesh:export")); EXPECT_FALSE(registry.provider("format:demo.read").has_value()); EXPECT_EQ(registry.size(), 1u); registry.clear(); EXPECT_EQ(registry.size(), 0u); EXPECT_TRUE(registry.capabilities().empty()); EXPECT_TRUE(registry.providers().empty()); EXPECT_TRUE(registry.conflicts().empty()); } // ── 管理器空状态与未知插件查询 ────────────────────────── TEST(CovPluginLifecycle, ManagerEmptyStateAndUnknownQueries) { reset_manager(); PluginManager& manager = PluginManager::instance(); EXPECT_FALSE(manager.has_host_services()); EXPECT_TRUE(manager.plugins().empty()); EXPECT_TRUE(manager.formats().empty()); EXPECT_TRUE(manager.cae_solvers().empty()); EXPECT_TRUE(manager.cam_strategies().empty()); EXPECT_TRUE(manager.gcs_solvers().empty()); EXPECT_TRUE(manager.meshers().empty()); EXPECT_TRUE(manager.optimizers().empty()); EXPECT_TRUE(manager.io_batches().empty()); EXPECT_TRUE(manager.plugin_stats().empty()); EXPECT_TRUE(manager.last_error().empty()); EXPECT_FALSE(manager.plugin_stats("com.test.absent").has_value()); EXPECT_FALSE(manager.manifest("com.test.absent").has_value()); EXPECT_FALSE(manager.is_loaded("com.test.absent")); EXPECT_EQ(manager.diagnostics_json(), "[]"); // 未发现插件: load 失败且写 error std::string error; EXPECT_FALSE(manager.load("com.test.absent", &error)); EXPECT_FALSE(error.empty()); EXPECT_FALSE(manager.is_loaded("com.test.absent")); // 未加载插件: unload / unload_cascade 幂等成功 std::string unload_error; EXPECT_TRUE(manager.unload("com.test.absent", &unload_error)) << unload_error; EXPECT_TRUE(manager.unload_cascade("com.test.absent", &unload_error)) << unload_error; // 未加载插件: reload 等价于 load, 失败 std::string reload_error; EXPECT_FALSE(manager.reload("com.test.absent", &reload_error)); EXPECT_FALSE(reload_error.empty()); // 缺失目录扫描: 返回 0 并记录错误, 不抛异常 std::string scan_error; EXPECT_EQ(manager.scan_directory("Z:/vde_cov/definitely_missing_dir", &scan_error), 0u); EXPECT_FALSE(scan_error.empty()); reset_manager(); EXPECT_EQ(manager.diagnostics_json(), "[]"); } // ── 宿主服务校验边界 ──────────────────────────────────── TEST(CovPluginLifecycle, HostServicesValidationBoundaries) { reset_manager(); PluginManager& manager = PluginManager::instance(); // struct_size 不足: 拒绝并记录 last_error, 不得标记可用 VdePluginHost bad{}; bad.struct_size = 0; bad.abi_major = 1; manager.set_host_services(bad); EXPECT_FALSE(manager.has_host_services()); EXPECT_FALSE(manager.last_error().empty()); // ABI major 不匹配: 拒绝 VdePluginHost wrong_major = vde::plugin::make_geometry_host_services(); wrong_major.abi_major = 999; manager.set_host_services(wrong_major); EXPECT_FALSE(manager.has_host_services()); EXPECT_FALSE(manager.last_error().empty()); // 合法宿主: 接受 const VdePluginHost valid = vde::plugin::make_geometry_host_services(); manager.set_host_services(valid); EXPECT_TRUE(manager.has_host_services()); EXPECT_EQ(manager.host_services().abi_major, 1u); // clear 后不可用 manager.clear_host_services(); EXPECT_FALSE(manager.has_host_services()); reset_manager(); } // ── 合成清单发现: 扫描 / 幂等 / 查询 ──────────────────── TEST(CovPluginLifecycle, SyntheticScanOrderAndManifestQueries) { reset_manager(); PluginManager& manager = PluginManager::instance(); TempDir dir; write_plugin_manifest(dir, "plugin_b", "com.test.zeta"); write_plugin_manifest(dir, "plugin_a", "com.test.alpha"); std::string error; EXPECT_EQ(manager.scan_directory(dir.path().string(), &error), 2u) << error; const auto plugins = manager.plugins(); ASSERT_EQ(plugins.size(), 2u); bool found_alpha = false; bool found_zeta = false; for (const auto& manifest : plugins) { if (manifest.id == "com.test.alpha") { found_alpha = true; EXPECT_TRUE(manifest.version.valid); EXPECT_EQ(manifest.version.major, 1); EXPECT_EQ(manifest.version.minor, 0); EXPECT_EQ(manifest.version.patch, 0); } if (manifest.id == "com.test.zeta") found_zeta = true; } EXPECT_TRUE(found_alpha); EXPECT_TRUE(found_zeta); const auto alpha = manager.manifest("com.test.alpha"); ASSERT_TRUE(alpha.has_value()); EXPECT_EQ(alpha->id, "com.test.alpha"); EXPECT_FALSE(alpha->manifest_path.empty()); // 二次扫描: 无新增清单 EXPECT_EQ(manager.scan_directory(dir.path().string(), &error), 0u); EXPECT_EQ(manager.plugins().size(), 2u); // clear 清空发现结果与统计 manager.clear(); EXPECT_TRUE(manager.plugins().empty()); EXPECT_TRUE(manager.plugin_stats().empty()); EXPECT_EQ(manager.diagnostics_json(), "[]"); reset_manager(); }