Compare commits

..

1 Commits

Author SHA1 Message Date
linianlin 5b959debc7 AI 自动生成测试用例 2026-05-13 11:53:09 +08:00
3 changed files with 268 additions and 76 deletions

View File

@ -9,10 +9,6 @@
// ══════════════════════════════════════════════════════════════════════════════ // ══════════════════════════════════════════════════════════════════════════════
/** /**
* @requirement(name="核心引擎初始化", id="REQ-ENGINE-INIT-001")
*
*
*
* @brief * @brief
*/ */
CmsEngine::CmsEngine() CmsEngine::CmsEngine()
@ -25,11 +21,8 @@ CmsEngine::CmsEngine()
} }
/** /**
* @requirement(name="唯一标识生成", id="REQ-UTIL-IDGEN-001")
* "ID-xxxxx"
* ID
*
* @brief * @brief
*
* @return std::string "ID-00001" * @return std::string "ID-00001"
*/ */
std::string CmsEngine::generateNextId() std::string CmsEngine::generateNextId()
@ -44,11 +37,10 @@ std::string CmsEngine::generateNextId()
// ══════════════════════════════════════════════════════════════════════════════ // ══════════════════════════════════════════════════════════════════════════════
/** /**
* @requirement(name="战场事件接入", id="REQ-EVENT-INGEST-001")
* 线
* ID
*
* @brief * @brief
*
* ID
*
* @param event * @param event
* @return true * @return true
* @return false * @return false
@ -66,11 +58,11 @@ bool CmsEngine::ingestEvent(const EventRecord& event)
} }
/** /**
* @requirement(name="待处理事件处理", id="REQ-EVENT-PROC-001")
* Pending Generated
* TaskPlan
*
* @brief * @brief
*
* Pending Generated
* TaskPlan
*
* @return size_t * @return size_t
*/ */
size_t CmsEngine::processPendingEvents() size_t CmsEngine::processPendingEvents()
@ -98,11 +90,8 @@ size_t CmsEngine::processPendingEvents()
} }
/** /**
* @requirement(name="事件查询", id="REQ-EVENT-QUERY-001")
* ID
* nullptr
*
* @brief ID * @brief ID
*
* @param eventId * @param eventId
* @return const EventRecord* nullptr * @return const EventRecord* nullptr
*/ */
@ -121,11 +110,10 @@ const EventRecord* CmsEngine::findEventById(const std::string& eventId) const
// ══════════════════════════════════════════════════════════════════════════════ // ══════════════════════════════════════════════════════════════════════════════
/** /**
* @requirement(name="任务方案创建", id="REQ-PLAN-CREATE-001")
* ID
*
*
* @brief * @brief
*
* ID
*
* @param plan id * @param plan id
* @return true * @return true
* @return false * @return false
@ -142,10 +130,8 @@ bool CmsEngine::createTaskPlan(TaskPlan& plan)
} }
/** /**
* @requirement(name="任务方案列表查询", id="REQ-PLAN-LIST-001")
*
*
* @brief * @brief
*
* @return const std::vector<TaskPlan>& * @return const std::vector<TaskPlan>&
*/ */
const std::vector<TaskPlan>& CmsEngine::getAllPlans() const const std::vector<TaskPlan>& CmsEngine::getAllPlans() const
@ -154,11 +140,8 @@ const std::vector<TaskPlan>& CmsEngine::getAllPlans() const
} }
/** /**
* @requirement(name="任务方案查找", id="REQ-PLAN-FIND-001")
* ID
* nullptr
*
* @brief ID * @brief ID
*
* @param planId * @param planId
* @return const TaskPlan* nullptr * @return const TaskPlan* nullptr
*/ */
@ -173,11 +156,8 @@ const TaskPlan* CmsEngine::findPlanById(const std::string& planId) const
} }
/** /**
* @requirement(name="任务方案状态更新", id="REQ-PLAN-UPDATE-001")
* ID
* 稿
*
* @brief * @brief
*
* @param planId * @param planId
* @param newStatus * @param newStatus
* @return true * @return true
@ -199,10 +179,8 @@ bool CmsEngine::updatePlanStatus(const std::string& planId, PlanStatus newStatus
// ══════════════════════════════════════════════════════════════════════════════ // ══════════════════════════════════════════════════════════════════════════════
/** /**
* @requirement(name="模板注册", id="REQ-TMPL-REG-001")
*
*
* @brief * @brief
*
* @param tmpl * @param tmpl
*/ */
void CmsEngine::registerTemplate(const TemplateInstance& tmpl) void CmsEngine::registerTemplate(const TemplateInstance& tmpl)
@ -211,11 +189,10 @@ void CmsEngine::registerTemplate(const TemplateInstance& tmpl)
} }
/** /**
* @requirement(name="模板匹配", id="REQ-TMPL-MATCH-001")
*
* nullptr
*
* @brief * @brief
*
*
*
* @param scenario * @param scenario
* @return const TemplateInstance* nullptr * @return const TemplateInstance* nullptr
*/ */
@ -239,11 +216,10 @@ const TemplateInstance* CmsEngine::matchTemplate(const std::string& scenario) co
// ══════════════════════════════════════════════════════════════════════════════ // ══════════════════════════════════════════════════════════════════════════════
/** /**
* @requirement(name="执行状态上报", id="REQ-MON-REPORT-001")
* nodeId
*
*
* @brief * @brief
*
* nodeId
*
* @param status * @param status
*/ */
void CmsEngine::reportExecutionStatus(const ExecutionStatus& status) void CmsEngine::reportExecutionStatus(const ExecutionStatus& status)
@ -252,11 +228,10 @@ void CmsEngine::reportExecutionStatus(const ExecutionStatus& status)
} }
/** /**
* @requirement(name="节点健康检查", id="REQ-MON-HEALTH-001")
* < 0.5 != 0
* 30
*
* @brief * @brief
*
* < 0.5 != 0 > 30
*
* @return std::vector<ExecutionStatus> * @return std::vector<ExecutionStatus>
*/ */
std::vector<ExecutionStatus> CmsEngine::checkHealth() const std::vector<ExecutionStatus> CmsEngine::checkHealth() const
@ -293,10 +268,10 @@ std::vector<ExecutionStatus> CmsEngine::checkHealth() const
// ══════════════════════════════════════════════════════════════════════════════ // ══════════════════════════════════════════════════════════════════════════════
/** /**
* @requirement(name="用户会话创建", id="REQ-SESS-CREATE-001")
* userId
*
* @brief * @brief
*
* userId
*
* @param session * @param session
* @return true * @return true
* @return false ID * @return false ID
@ -314,11 +289,10 @@ bool CmsEngine::createSession(const UserSession& session)
} }
/** /**
* @requirement(name="权限校验", id="REQ-SESS-PERM-001")
*
*
*
* @brief * @brief
*
*
*
* @param userId ID * @param userId ID
* @param permissionMask * @param permissionMask
* @return true * @return true
@ -339,10 +313,8 @@ bool CmsEngine::checkPermission(const std::string& userId,
// ══════════════════════════════════════════════════════════════════════════════ // ══════════════════════════════════════════════════════════════════════════════
/** /**
* @requirement(name="通知推送", id="REQ-NOTIF-PUSH-001")
*
*
* @brief * @brief
*
* @param msg * @param msg
*/ */
void CmsEngine::pushNotification(const NotificationMessage& msg) void CmsEngine::pushNotification(const NotificationMessage& msg)
@ -351,11 +323,8 @@ void CmsEngine::pushNotification(const NotificationMessage& msg)
} }
/** /**
* @requirement(name="未读通知查询", id="REQ-NOTIF-UNREAD-001")
*
*
*
* @brief * @brief
*
* @return std::vector<NotificationMessage> * @return std::vector<NotificationMessage>
*/ */
std::vector<NotificationMessage> CmsEngine::getUnreadNotifications() const std::vector<NotificationMessage> CmsEngine::getUnreadNotifications() const
@ -374,11 +343,10 @@ std::vector<NotificationMessage> CmsEngine::getUnreadNotifications() const
// ══════════════════════════════════════════════════════════════════════════════ // ══════════════════════════════════════════════════════════════════════════════
/** /**
* @requirement(name="运行模式切换", id="REQ-MODE-SWITCH-001")
*
*
*
* @brief * @brief
*
*
*
* @param newMode * @param newMode
* @return true * @return true
* @return false * @return false
@ -396,11 +364,8 @@ bool CmsEngine::switchMode(RunMode newMode)
} }
/** /**
* @requirement(name="系统状态查询", id="REQ-MODE-QUERY-001")
*
*
*
* @brief * @brief
*
* @return const SystemStateContext& * @return const SystemStateContext&
*/ */
const SystemStateContext& CmsEngine::getSystemContext() const const SystemStateContext& CmsEngine::getSystemContext() const
@ -409,11 +374,8 @@ const SystemStateContext& CmsEngine::getSystemContext() const
} }
/** /**
* @requirement(name="引擎摘要生成", id="REQ-SUMM-GEN-001")
*
*
*
* @brief * @brief
*
* @return std::string * @return std::string
*/ */
std::string CmsEngine::getSummary() const std::string CmsEngine::getSummary() const

25
tests/CMakeLists.txt Normal file
View File

@ -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_app.cpp ../src/app.cpp)
target_link_libraries(test_plan_execute_t1 gtest gmock gtest_main)
include(GoogleTest)
gtest_discover_tests(test_plan_execute_t1)

205
tests/test_app.cpp Normal file
View File

@ -0,0 +1,205 @@
#include "app.hpp"
#include <gtest/gtest.h>
#include <thread>
#include <chrono>
using namespace std;
class CmsEngineTest : public ::testing::Test {
protected:
CmsEngine engine;
};
// 1. CmsEngine 构造函数
TEST_F(CmsEngineTest, testCmsEngineNormal) {
const auto& ctx = engine.getSystemContext();
EXPECT_EQ(ctx.currentMode, RunMode::Idle);
EXPECT_EQ(ctx.consistencyMark, 0);
EXPECT_FALSE(ctx.contextSnapshot.empty());
}
// 2. generateNextId (通过公开接口间接验证或假设测试友元)
TEST_F(CmsEngineTest, testGenerateNextIdNormal) {
TaskPlan p1, p2;
p1.name = "P1"; p2.name = "P2";
engine.createTaskPlan(p1);
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 = "evt1"; evt.priority = 100; evt.status = EventStatus::Pending;
EXPECT_TRUE(engine.ingestEvent(evt));
EXPECT_NE(engine.findEventById("evt1"), nullptr);
}
TEST_F(CmsEngineTest, testIngestEventBoundary) {
EventRecord e1, e2;
e1.id = ""; e1.priority = 100;
e2.id = "e2"; e2.priority = 256;
EXPECT_FALSE(engine.ingestEvent(e1));
EXPECT_FALSE(engine.ingestEvent(e2));
}
// 4. processPendingEvents
TEST_F(CmsEngineTest, testProcessPendingEventsNormal) {
EventRecord e1, e2;
e1.id = "e1"; e1.priority = 1; e1.status = EventStatus::Pending;
e2.id = "e2"; e2.priority = 1; e2.status = EventStatus::Generated;
engine.ingestEvent(e1); engine.ingestEvent(e2);
EXPECT_EQ(engine.processPendingEvents(), 1);
EXPECT_EQ(engine.findEventById("e1")->status, EventStatus::Generated);
EXPECT_EQ(engine.getAllPlans().size(), 1);
}
// 5. findEventById
TEST_F(CmsEngineTest, testFindEventByIdNormal) {
EventRecord e; e.id = "findMe"; e.priority = 1; e.status = EventStatus::Pending;
engine.ingestEvent(e);
EXPECT_NE(engine.findEventById("findMe"), nullptr);
EXPECT_EQ(engine.findEventById("notExist"), nullptr);
}
// 6. createTaskPlan
TEST_F(CmsEngineTest, testCreateTaskPlanNormal) {
TaskPlan p; p.name = "ValidPlan";
EXPECT_TRUE(engine.createTaskPlan(p));
EXPECT_FALSE(p.id.empty());
}
TEST_F(CmsEngineTest, testCreateTaskPlanBoundary) {
TaskPlan p; p.name = "";
EXPECT_FALSE(engine.createTaskPlan(p));
}
// 7. getAllPlans
TEST_F(CmsEngineTest, testGetAllPlansNormal) {
EXPECT_TRUE(engine.getAllPlans().empty());
TaskPlan p; p.name = "P1";
engine.createTaskPlan(p);
EXPECT_EQ(engine.getAllPlans().size(), 1);
}
// 8. findPlanById
TEST_F(CmsEngineTest, testFindPlanByIdNormal) {
TaskPlan p; p.name = "P1";
engine.createTaskPlan(p);
EXPECT_NE(engine.findPlanById(p.id), nullptr);
EXPECT_EQ(engine.findPlanById("fake"), nullptr);
}
// 9. updatePlanStatus
TEST_F(CmsEngineTest, testUpdatePlanStatusNormal) {
TaskPlan p; p.name = "P1";
engine.createTaskPlan(p);
EXPECT_TRUE(engine.updatePlanStatus(p.id, PlanStatus::Approved));
EXPECT_EQ(engine.findPlanById(p.id)->status, PlanStatus::Approved);
}
TEST_F(CmsEngineTest, testUpdatePlanStatusException) {
EXPECT_FALSE(engine.updatePlanStatus("nonexistent", PlanStatus::Approved));
}
// 10. registerTemplate
TEST_F(CmsEngineTest, testRegisterTemplateNormal) {
TemplateInstance t; t.confidence = 0.7;
engine.registerTemplate(t);
EXPECT_EQ(engine.matchTemplate("any")->confidence, 0.7);
}
// 11. matchTemplate
TEST_F(CmsEngineTest, testMatchTemplateNormal) {
TemplateInstance t1, t2;
t1.confidence = 0.5; t2.confidence = 0.9;
engine.registerTemplate(t1); engine.registerTemplate(t2);
EXPECT_EQ(engine.matchTemplate("scene")->confidence, 0.9);
}
TEST_F(CmsEngineTest, testMatchTemplateBoundary) {
EXPECT_EQ(engine.matchTemplate("empty"), nullptr);
}
// 12. reportExecutionStatus
TEST_F(CmsEngineTest, testReportExecutionStatusNormal) {
ExecutionStatus s; s.nodeId = "n1"; s.healthIndex = 0.9; s.errorCode = 0;
s.lastReport = chrono::system_clock::now();
engine.reportExecutionStatus(s);
EXPECT_EQ(engine.checkHealth().size(), 0);
}
// 13. checkHealth
TEST_F(CmsEngineTest, testCheckHealthSpecial) {
ExecutionStatus s1, s2, s3;
s1.nodeId = "low_health"; s1.healthIndex = 0.4; s1.errorCode = 0; s1.lastReport = chrono::system_clock::now();
s2.nodeId = "error"; s2.healthIndex = 0.9; s2.errorCode = 1; s2.lastReport = chrono::system_clock::now();
s3.nodeId = "timeout"; s3.healthIndex = 0.9; s3.errorCode = 0; s3.lastReport = chrono::system_clock::now() - chrono::seconds(31);
engine.reportExecutionStatus(s1); engine.reportExecutionStatus(s2); engine.reportExecutionStatus(s3);
EXPECT_EQ(engine.checkHealth().size(), 3);
}
// 14. createSession
TEST_F(CmsEngineTest, testCreateSessionNormal) {
UserSession sess; sess.userId = "u1"; sess.permissionBitmap = 0xFF;
EXPECT_TRUE(engine.createSession(sess));
}
TEST_F(CmsEngineTest, testCreateSessionBoundary) {
UserSession s1, s2;
s1.userId = ""; s1.permissionBitmap = 1;
s2.userId = "dup"; s2.permissionBitmap = 1;
EXPECT_FALSE(engine.createSession(s1));
EXPECT_TRUE(engine.createSession(s2));
EXPECT_FALSE(engine.createSession(s2));
}
// 15. checkPermission
TEST_F(CmsEngineTest, testCheckPermissionNormal) {
UserSession sess; sess.userId = "u1"; sess.permissionBitmap = 0x0F;
engine.createSession(sess);
EXPECT_TRUE(engine.checkPermission("u1", 0x03));
EXPECT_FALSE(engine.checkPermission("u1", 0x10));
EXPECT_FALSE(engine.checkPermission("u2", 0x01));
}
// 16. pushNotification
TEST_F(CmsEngineTest, testPushNotificationNormal) {
NotificationMessage msg; msg.isRead = false;
engine.pushNotification(msg);
EXPECT_EQ(engine.getUnreadNotifications().size(), 1);
}
// 17. getUnreadNotifications
TEST_F(CmsEngineTest, testGetUnreadNotificationsNormal) {
NotificationMessage m1, m2;
m1.isRead = false; m2.isRead = true;
engine.pushNotification(m1); engine.pushNotification(m2);
EXPECT_EQ(engine.getUnreadNotifications().size(), 1);
}
// 18. switchMode
TEST_F(CmsEngineTest, testSwitchModeNormal) {
EXPECT_TRUE(engine.switchMode(RunMode::AutoExec));
EXPECT_EQ(engine.getSystemContext().currentMode, RunMode::AutoExec);
}
TEST_F(CmsEngineTest, testSwitchModeSpecial) {
engine.switchMode(RunMode::Degraded);
EXPECT_FALSE(engine.switchMode(RunMode::Idle));
EXPECT_TRUE(engine.switchMode(RunMode::Degraded));
}
// 19. getSystemContext
TEST_F(CmsEngineTest, testGetSystemContextNormal) {
const auto& ctx = engine.getSystemContext();
EXPECT_EQ(ctx.currentMode, RunMode::Idle);
}
// 20. getSummary
TEST_F(CmsEngineTest, testGetSummaryNormal) {
string summary = engine.getSummary();
EXPECT_NE(summary.find("CMS Engine Summary"), string::npos);
EXPECT_NE(summary.find("Events"), string::npos);
EXPECT_NE(summary.find("Idle"), string::npos);
}