From 31f60e9c8d687f790cbc36a705024a7fe4703c0b Mon Sep 17 00:00:00 2001 From: linianlin Date: Thu, 14 May 2026 13:47:02 +0800 Subject: [PATCH] =?UTF-8?q?AI=20=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/CMakeLists.txt | 25 ++++ tests/test_cms_engine.cpp | 271 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 296 insertions(+) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/test_cms_engine.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..d27391c --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.10.0) +project(test_plan_execute_t1) +include(FetchContent) +if (MSVC) + add_compile_options(/utf-8) +endif() +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip +) +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include) + +include(CTest) +enable_testing() + +add_executable(test_plan_execute_t1 test_cms_engine.cpp ../src/app.cpp) + +target_link_libraries(test_plan_execute_t1 gtest gmock gtest_main) +include(GoogleTest) +gtest_discover_tests(test_plan_execute_t1) \ No newline at end of file diff --git a/tests/test_cms_engine.cpp b/tests/test_cms_engine.cpp new file mode 100644 index 0000000..32d3b63 --- /dev/null +++ b/tests/test_cms_engine.cpp @@ -0,0 +1,271 @@ +#include +#include "app.hpp" +#include +#include + +class CmsEngineTest : public ::testing::Test { +protected: + void SetUp() override { + engine_ = std::make_unique(); + } + std::unique_ptr engine_; +}; + +// 1. CmsEngine 构造函数测试 +TEST_F(CmsEngineTest, testCmsEngineConstructorNormal) { + const auto& ctx = engine_->getSystemContext(); + EXPECT_EQ(ctx.currentMode, RunMode::Idle); + EXPECT_EQ(ctx.consistencyMark, 0); + EXPECT_FALSE(ctx.contextSnapshot.empty()); +} + +// 2. generateNextId 自增ID生成测试 +TEST_F(CmsEngineTest, testGenerateNextIdFormatAndIncrement) { + TaskPlan p1, p2; + p1.name = "PlanA"; + p2.name = "PlanB"; + EXPECT_TRUE(engine_->createTaskPlan(p1)); + EXPECT_TRUE(engine_->createTaskPlan(p2)); + EXPECT_EQ(p1.id, "ID-00001"); + EXPECT_EQ(p2.id, "ID-00002"); +} + +// 3. ingestEvent 正常事件接入测试 +TEST_F(CmsEngineTest, testIngestEventNormal) { + EventRecord evt; + evt.id = "EVT-01"; + evt.priority = 100; + evt.status = EventStatus::Pending; + EXPECT_TRUE(engine_->ingestEvent(evt)); + EXPECT_NE(engine_->findEventById("EVT-01"), nullptr); +} + +// 4. ingestEvent 空ID异常测试 +TEST_F(CmsEngineTest, testIngestEventEmptyId) { + EventRecord evt; + evt.id = ""; + evt.priority = 100; + evt.status = EventStatus::Pending; + EXPECT_FALSE(engine_->ingestEvent(evt)); +} + +// 5. ingestEvent 优先级越界边界测试 +TEST_F(CmsEngineTest, testIngestEventHighPriority) { + EventRecord evt; + evt.id = "EVT-02"; + evt.priority = 300; + evt.status = EventStatus::Pending; + EXPECT_FALSE(engine_->ingestEvent(evt)); +} + +// 6. processPendingEvents 批量处理测试 +TEST_F(CmsEngineTest, testProcessPendingEventsNormal) { + EventRecord evt1, evt2; + evt1.id = "EVT-10"; evt1.priority = 50; evt1.status = EventStatus::Pending; + evt2.id = "EVT-11"; evt2.priority = 60; evt2.status = EventStatus::Generated; + engine_->ingestEvent(evt1); + engine_->ingestEvent(evt2); + size_t count = engine_->processPendingEvents(); + EXPECT_EQ(count, 1); + const auto* found = engine_->findEventById("EVT-10"); + ASSERT_NE(found, nullptr); + EXPECT_EQ(found->status, EventStatus::Generated); + EXPECT_EQ(engine_->getAllPlans().size(), 1); +} + +// 7. processPendingEvents 空队列边界测试 +TEST_F(CmsEngineTest, testProcessPendingEventsEmpty) { + EXPECT_EQ(engine_->processPendingEvents(), 0); +} + +// 8. findEventById 查找成功测试 +TEST_F(CmsEngineTest, testFindEventByIdNormal) { + EventRecord evt; + evt.id = "EVT-20"; evt.priority = 10; evt.status = EventStatus::Pending; + engine_->ingestEvent(evt); + const auto* found = engine_->findEventById("EVT-20"); + ASSERT_NE(found, nullptr); + EXPECT_EQ(found->id, "EVT-20"); +} + +// 9. findEventById 查找失败异常测试 +TEST_F(CmsEngineTest, testFindEventByIdNotFound) { + EXPECT_EQ(engine_->findEventById("NON_EXIST"), nullptr); +} + +// 10. createTaskPlan 正常创建测试 +TEST_F(CmsEngineTest, testCreateTaskPlanNormal) { + TaskPlan plan; + plan.name = "ManualPlan"; + EXPECT_TRUE(engine_->createTaskPlan(plan)); + EXPECT_FALSE(plan.id.empty()); + EXPECT_EQ(engine_->getAllPlans().size(), 1); +} + +// 11. createTaskPlan 空名拒绝异常测试 +TEST_F(CmsEngineTest, testCreateTaskPlanEmptyName) { + TaskPlan plan; + plan.name = ""; + EXPECT_FALSE(engine_->createTaskPlan(plan)); +} + +// 12. getAllPlans 获取列表测试 +TEST_F(CmsEngineTest, testGetAllPlansNormal) { + TaskPlan p; p.name = "P1"; engine_->createTaskPlan(p); + const auto& plans = engine_->getAllPlans(); + EXPECT_EQ(plans.size(), 1); +} + +// 13. findPlanById 查找成功测试 +TEST_F(CmsEngineTest, testFindPlanByIdNormal) { + TaskPlan p; p.name = "P2"; + engine_->createTaskPlan(p); + const auto* found = engine_->findPlanById(p.id); + ASSERT_NE(found, nullptr); + EXPECT_EQ(found->name, "P2"); +} + +// 14. findPlanById 查找失败异常测试 +TEST_F(CmsEngineTest, testFindPlanByIdNotFound) { + EXPECT_EQ(engine_->findPlanById("FAKE_ID"), nullptr); +} + +// 15. updatePlanStatus 状态更新测试 +TEST_F(CmsEngineTest, testUpdatePlanStatusNormal) { + TaskPlan p; p.name = "P3"; + engine_->createTaskPlan(p); + EXPECT_TRUE(engine_->updatePlanStatus(p.id, PlanStatus::Drafting)); + const auto* updated = engine_->findPlanById(p.id); + ASSERT_NE(updated, nullptr); + EXPECT_EQ(updated->status, PlanStatus::Drafting); +} + +// 16. updatePlanStatus 未找到异常测试 +TEST_F(CmsEngineTest, testUpdatePlanStatusNotFound) { + EXPECT_FALSE(engine_->updatePlanStatus("NO_SUCH_PLAN", PlanStatus::Drafting)); +} + +// 17. registerTemplate 注册测试 +TEST_F(CmsEngineTest, testRegisterTemplateNormal) { + TemplateInstance tmpl; + tmpl.id = "T1"; + tmpl.confidence = 0.8; + engine_->registerTemplate(tmpl); + EXPECT_NE(engine_->matchTemplate("any"), nullptr); +} + +// 18. matchTemplate 最佳匹配测试 +TEST_F(CmsEngineTest, testMatchTemplateBestConfidence) { + TemplateInstance t1, t2; + t1.id = "T1"; t1.confidence = 0.5; + t2.id = "T2"; t2.confidence = 0.9; + engine_->registerTemplate(t1); + engine_->registerTemplate(t2); + const auto* best = engine_->matchTemplate("scenario"); + ASSERT_NE(best, nullptr); + EXPECT_EQ(best->id, "T2"); +} + +// 19. matchTemplate 空库边界测试 +TEST_F(CmsEngineTest, testMatchTemplateEmpty) { + EXPECT_EQ(engine_->matchTemplate("scenario"), nullptr); +} + +// 20. reportExecutionStatus 上报测试 +TEST_F(CmsEngineTest, testReportExecutionStatusNormal) { + ExecutionStatus st; + st.nodeId = "Node1"; + st.healthIndex = 0.9; + st.errorCode = 0; + st.lastReport = std::chrono::system_clock::now(); + engine_->reportExecutionStatus(st); + auto unhealthy = engine_->checkHealth(); + EXPECT_TRUE(unhealthy.empty()); +} + +// 21. checkHealth 异常节点捕获测试 +TEST_F(CmsEngineTest, testCheckHealthUnhealthy) { + ExecutionStatus st; + st.nodeId = "NodeBad"; + st.healthIndex = 0.3; + st.errorCode = 1; + st.lastReport = std::chrono::system_clock::now(); + engine_->reportExecutionStatus(st); + auto unhealthy = engine_->checkHealth(); + EXPECT_EQ(unhealthy.size(), 1); + EXPECT_EQ(unhealthy[0].nodeId, "NodeBad"); +} + +// 22. createSession 正常创建测试 +TEST_F(CmsEngineTest, testCreateSessionNormal) { + UserSession sess; + sess.userId = "User1"; + sess.permissionBitmap = 0xFF; + EXPECT_TRUE(engine_->createSession(sess)); +} + +// 23. createSession 重复ID异常测试 +TEST_F(CmsEngineTest, testCreateSessionDuplicate) { + UserSession s; + s.userId = "User2"; + s.permissionBitmap = 0xFF; + EXPECT_TRUE(engine_->createSession(s)); + EXPECT_FALSE(engine_->createSession(s)); +} + +// 24. checkPermission 权限校验测试 +TEST_F(CmsEngineTest, testCheckPermissionNormal) { + UserSession sess; + sess.userId = "Admin"; + sess.permissionBitmap = 0x0F; + engine_->createSession(sess); + EXPECT_TRUE(engine_->checkPermission("Admin", 0x03)); + EXPECT_FALSE(engine_->checkPermission("Admin", 0x10)); + EXPECT_FALSE(engine_->checkPermission("Ghost", 0x01)); +} + +// 25. pushNotification 推送测试 +TEST_F(CmsEngineTest, testPushNotificationNormal) { + NotificationMessage msg; + msg.isRead = false; + engine_->pushNotification(msg); + EXPECT_EQ(engine_->getUnreadNotifications().size(), 1); +} + +// 26. getUnreadNotifications 过滤已读测试 +TEST_F(CmsEngineTest, testGetUnreadNotificationsFilter) { + NotificationMessage msg1, msg2; + msg1.isRead = false; + msg2.isRead = true; + engine_->pushNotification(msg1); + engine_->pushNotification(msg2); + auto unread = engine_->getUnreadNotifications(); + EXPECT_EQ(unread.size(), 1); + EXPECT_FALSE(unread[0].isRead); +} + +// 27. switchMode 正常切换测试 +TEST_F(CmsEngineTest, testSwitchModeNormal) { + EXPECT_TRUE(engine_->switchMode(RunMode::AutoExec)); + EXPECT_EQ(engine_->getSystemContext().currentMode, RunMode::AutoExec); +} + +// 28. switchMode 降级限制特殊场景测试 +TEST_F(CmsEngineTest, testSwitchModeDegradedRestriction) { + engine_->switchMode(RunMode::Degraded); + EXPECT_FALSE(engine_->switchMode(RunMode::Idle)); + EXPECT_TRUE(engine_->switchMode(RunMode::Degraded)); +} + +// 29. getSystemContext 获取上下文测试 +TEST_F(CmsEngineTest, testGetSystemContextNormal) { + const auto& ctx = engine_->getSystemContext(); + EXPECT_EQ(ctx.currentMode, RunMode::Idle); +} + +// 30. getSummary 摘要生成测试 +TEST_F(CmsEngineTest, testGetSummaryNormal) { + std::string summary = engine_->getSummary(); + EXPECT_TRUE(summary.find("Events : 0") != std::string::npos); + EXPECT_TRUE(summary.find("Mode : Idle") != std::string::npos); +} \ No newline at end of file