Compare commits

..

1 Commits

Author SHA1 Message Date
linianlin b4dec7fb65 AI 自动生成测试用例 2026-05-06 22:19:00 +08:00
3 changed files with 225 additions and 76 deletions

View File

@ -9,10 +9,6 @@
// ══════════════════════════════════════════════════════════════════════════════
/**
* @requirement(name="核心引擎初始化", id="REQ-ENGINE-INIT-001")
*
*
*
* @brief
*/
CmsEngine::CmsEngine()
@ -25,11 +21,8 @@ CmsEngine::CmsEngine()
}
/**
* @requirement(name="唯一标识生成", id="REQ-UTIL-IDGEN-001")
* "ID-xxxxx"
* ID
*
* @brief
*
* @return std::string "ID-00001"
*/
std::string CmsEngine::generateNextId()
@ -44,11 +37,10 @@ std::string CmsEngine::generateNextId()
// ══════════════════════════════════════════════════════════════════════════════
/**
* @requirement(name="战场事件接入", id="REQ-EVENT-INGEST-001")
* 线
* ID
*
* @brief
*
* ID
*
* @param event
* @return true
* @return false
@ -66,11 +58,11 @@ bool CmsEngine::ingestEvent(const EventRecord& event)
}
/**
* @requirement(name="待处理事件处理", id="REQ-EVENT-PROC-001")
* Pending Generated
* TaskPlan
*
* @brief
*
* Pending Generated
* TaskPlan
*
* @return size_t
*/
size_t CmsEngine::processPendingEvents()
@ -98,11 +90,8 @@ size_t CmsEngine::processPendingEvents()
}
/**
* @requirement(name="事件查询", id="REQ-EVENT-QUERY-001")
* ID
* nullptr
*
* @brief ID
*
* @param eventId
* @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
*
* ID
*
* @param plan id
* @return true
* @return false
@ -142,10 +130,8 @@ bool CmsEngine::createTaskPlan(TaskPlan& plan)
}
/**
* @requirement(name="任务方案列表查询", id="REQ-PLAN-LIST-001")
*
*
* @brief
*
* @return const std::vector<TaskPlan>&
*/
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
*
* @param planId
* @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
*
* @param planId
* @param newStatus
* @return true
@ -199,10 +179,8 @@ bool CmsEngine::updatePlanStatus(const std::string& planId, PlanStatus newStatus
// ══════════════════════════════════════════════════════════════════════════════
/**
* @requirement(name="模板注册", id="REQ-TMPL-REG-001")
*
*
* @brief
*
* @param 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
*
*
*
* @param scenario
* @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
*
* nodeId
*
* @param 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
*
* < 0.5 != 0 > 30
*
* @return std::vector<ExecutionStatus>
*/
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
*
* userId
*
* @param session
* @return true
* @return false ID
@ -314,11 +289,10 @@ bool CmsEngine::createSession(const UserSession& session)
}
/**
* @requirement(name="权限校验", id="REQ-SESS-PERM-001")
*
*
*
* @brief
*
*
*
* @param userId ID
* @param permissionMask
* @return true
@ -339,10 +313,8 @@ bool CmsEngine::checkPermission(const std::string& userId,
// ══════════════════════════════════════════════════════════════════════════════
/**
* @requirement(name="通知推送", id="REQ-NOTIF-PUSH-001")
*
*
* @brief
*
* @param 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
*
* @return std::vector<NotificationMessage>
*/
std::vector<NotificationMessage> CmsEngine::getUnreadNotifications() const
@ -374,11 +343,10 @@ std::vector<NotificationMessage> CmsEngine::getUnreadNotifications() const
// ══════════════════════════════════════════════════════════════════════════════
/**
* @requirement(name="运行模式切换", id="REQ-MODE-SWITCH-001")
*
*
*
* @brief
*
*
*
* @param newMode
* @return true
* @return false
@ -396,11 +364,8 @@ bool CmsEngine::switchMode(RunMode newMode)
}
/**
* @requirement(name="系统状态查询", id="REQ-MODE-QUERY-001")
*
*
*
* @brief
*
* @return const SystemStateContext&
*/
const SystemStateContext& CmsEngine::getSystemContext() const
@ -409,11 +374,8 @@ const SystemStateContext& CmsEngine::getSystemContext() const
}
/**
* @requirement(name="引擎摘要生成", id="REQ-SUMM-GEN-001")
*
*
*
* @brief
*
* @return std::string
*/
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)

162
tests/test_app.cpp Normal file
View File

@ -0,0 +1,162 @@
#include <gtest/gtest.h>
#include "app.hpp"
// 测试 getUnreadNotifications
TEST(CmsEngineTest, GetUnreadNotifications_NoNotifications) {
CmsEngine engine;
auto unread = engine.getUnreadNotifications();
EXPECT_TRUE(unread.empty());
}
TEST(CmsEngineTest, GetUnreadNotifications_AllRead) {
CmsEngine engine;
NotificationMessage msg1, msg2;
msg1.isRead = true;
msg2.isRead = true;
engine.pushNotification(msg1);
engine.pushNotification(msg2);
auto unread = engine.getUnreadNotifications();
EXPECT_TRUE(unread.empty());
}
TEST(CmsEngineTest, GetUnreadNotifications_AllUnread) {
CmsEngine engine;
NotificationMessage msg1, msg2;
msg1.isRead = false;
msg2.isRead = false;
engine.pushNotification(msg1);
engine.pushNotification(msg2);
auto unread = engine.getUnreadNotifications();
EXPECT_EQ(unread.size(), 2);
}
TEST(CmsEngineTest, GetUnreadNotifications_Mixed) {
CmsEngine engine;
NotificationMessage msg1, msg2, msg3;
msg1.isRead = true;
msg2.isRead = false;
msg3.isRead = false;
engine.pushNotification(msg1);
engine.pushNotification(msg2);
engine.pushNotification(msg3);
auto unread = engine.getUnreadNotifications();
EXPECT_EQ(unread.size(), 2);
// 验证返回的是未读消息
for (const auto& msg : unread) {
EXPECT_FALSE(msg.isRead);
}
}
// 测试 switchMode
TEST(CmsEngineTest, SwitchMode_FromIdleToHumanLoop) {
CmsEngine engine;
EXPECT_TRUE(engine.switchMode(RunMode::HumanLoop));
const auto& ctx = engine.getSystemContext();
EXPECT_EQ(ctx.currentMode, RunMode::HumanLoop);
}
TEST(CmsEngineTest, SwitchMode_FromIdleToAutoExec) {
CmsEngine engine;
EXPECT_TRUE(engine.switchMode(RunMode::AutoExec));
EXPECT_EQ(engine.getSystemContext().currentMode, RunMode::AutoExec);
}
TEST(CmsEngineTest, SwitchMode_FromDegradedToOther) {
CmsEngine engine;
// 先切换到 Degraded
engine.switchMode(RunMode::Degraded);
// 尝试切换到其他模式,应失败
EXPECT_FALSE(engine.switchMode(RunMode::Idle));
EXPECT_EQ(engine.getSystemContext().currentMode, RunMode::Degraded);
}
TEST(CmsEngineTest, SwitchMode_FromDegradedToDegraded) {
CmsEngine engine;
engine.switchMode(RunMode::Degraded);
// 切换到自身,应成功(因为 newMode == Degraded
EXPECT_TRUE(engine.switchMode(RunMode::Degraded));
EXPECT_EQ(engine.getSystemContext().currentMode, RunMode::Degraded);
}
TEST(CmsEngineTest, SwitchMode_FromHumanLoopToDegraded) {
CmsEngine engine;
engine.switchMode(RunMode::HumanLoop);
EXPECT_TRUE(engine.switchMode(RunMode::Degraded));
EXPECT_EQ(engine.getSystemContext().currentMode, RunMode::Degraded);
}
// 测试 getSystemContext
TEST(CmsEngineTest, GetSystemContext_InitialState) {
CmsEngine engine;
const auto& ctx = engine.getSystemContext();
EXPECT_EQ(ctx.currentMode, RunMode::Idle);
EXPECT_EQ(ctx.consistencyMark, 0);
// switchTime 应接近当前时间,但无法精确断言
EXPECT_FALSE(ctx.contextSnapshot.empty());
}
TEST(CmsEngineTest, GetSystemContext_AfterSwitch) {
CmsEngine engine;
engine.switchMode(RunMode::AutoExec);
const auto& ctx = engine.getSystemContext();
EXPECT_EQ(ctx.currentMode, RunMode::AutoExec);
// consistencyMark 应为 nextId_ 的值初始0但 switchMode 会赋值为 nextId_nextId_ 初始0但 generateNextId 会递增,但 switchMode 中 consistencyMark = nextId_此时 nextId_ 仍为0
// 注意switchMode 中 consistencyMark = nextId_而 nextId_ 初始为0所以 consistencyMark 应为0
EXPECT_EQ(ctx.consistencyMark, 0);
}
// 测试 getSummary
TEST(CmsEngineTest, GetSummary_Initial) {
CmsEngine engine;
std::string summary = engine.getSummary();
EXPECT_NE(summary.find("Events : 0"), std::string::npos);
EXPECT_NE(summary.find("Plans : 0"), std::string::npos);
EXPECT_NE(summary.find("Templates : 0"), std::string::npos);
EXPECT_NE(summary.find("Monitor Nodes : 0"), std::string::npos);
EXPECT_NE(summary.find("Active Sessions : 0"), std::string::npos);
EXPECT_NE(summary.find("Notifications : 0"), std::string::npos);
EXPECT_NE(summary.find("Mode : Idle"), std::string::npos);
}
TEST(CmsEngineTest, GetSummary_AfterOperations) {
CmsEngine engine;
// 添加事件
EventRecord evt;
evt.id = "evt1";
evt.priority = 10;
engine.ingestEvent(evt);
// 添加方案
TaskPlan plan;
plan.name = "plan1";
engine.createTaskPlan(plan);
// 添加模板
TemplateInstance tmpl;
tmpl.confidence = 0.8;
engine.registerTemplate(tmpl);
// 添加节点状态
ExecutionStatus status;
status.nodeId = "node1";
status.healthIndex = 1.0;
status.errorCode = 0;
status.lastReport = std::chrono::system_clock::now();
engine.reportExecutionStatus(status);
// 添加会话
UserSession session;
session.userId = "user1";
engine.createSession(session);
// 添加通知
NotificationMessage msg;
msg.isRead = false;
engine.pushNotification(msg);
// 切换模式
engine.switchMode(RunMode::AutoExec);
std::string summary = engine.getSummary();
EXPECT_NE(summary.find("Events : 1"), std::string::npos);
EXPECT_NE(summary.find("Plans : 1"), std::string::npos);
EXPECT_NE(summary.find("Templates : 1"), std::string::npos);
EXPECT_NE(summary.find("Monitor Nodes : 1"), std::string::npos);
EXPECT_NE(summary.find("Active Sessions : 1"), std::string::npos);
EXPECT_NE(summary.find("Notifications : 1"), std::string::npos);
EXPECT_NE(summary.find("Mode : AutoExec"), std::string::npos);
}