Compare commits

..

1 Commits

Author SHA1 Message Date
lids 6bd7608e5c 需求注释 2026-06-02 15:14:03 +08:00
3 changed files with 76 additions and 303 deletions

View File

@ -9,6 +9,10 @@
// ══════════════════════════════════════════════════════════════════════════════ // ══════════════════════════════════════════════════════════════════════════════
/** /**
* @requirement(name="核心引擎初始化", id="REQ-ENGINE-INIT-001")
*
*
*
* @brief * @brief
*/ */
CmsEngine::CmsEngine() CmsEngine::CmsEngine()
@ -21,8 +25,11 @@ CmsEngine::CmsEngine()
} }
/** /**
* @brief * @requirement(name="唯一标识生成", id="REQ-UTIL-IDGEN-001")
* "ID-xxxxx"
* ID
* *
* @brief
* @return std::string "ID-00001" * @return std::string "ID-00001"
*/ */
std::string CmsEngine::generateNextId() std::string CmsEngine::generateNextId()
@ -37,10 +44,11 @@ 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
@ -58,11 +66,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()
@ -90,8 +98,11 @@ size_t CmsEngine::processPendingEvents()
} }
/** /**
* @brief ID * @requirement(name="事件查询", id="REQ-EVENT-QUERY-001")
* ID
* nullptr
* *
* @brief ID
* @param eventId * @param eventId
* @return const EventRecord* nullptr * @return const EventRecord* nullptr
*/ */
@ -110,10 +121,11 @@ 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
@ -130,8 +142,10 @@ bool CmsEngine::createTaskPlan(TaskPlan& plan)
} }
/** /**
* @brief * @requirement(name="任务方案列表查询", id="REQ-PLAN-LIST-001")
*
* *
* @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
@ -140,8 +154,11 @@ const std::vector<TaskPlan>& CmsEngine::getAllPlans() const
} }
/** /**
* @brief ID * @requirement(name="任务方案查找", id="REQ-PLAN-FIND-001")
* ID
* nullptr
* *
* @brief ID
* @param planId * @param planId
* @return const TaskPlan* nullptr * @return const TaskPlan* nullptr
*/ */
@ -156,8 +173,11 @@ const TaskPlan* CmsEngine::findPlanById(const std::string& planId) const
} }
/** /**
* @brief * @requirement(name="任务方案状态更新", id="REQ-PLAN-UPDATE-001")
* ID
* 稿
* *
* @brief
* @param planId * @param planId
* @param newStatus * @param newStatus
* @return true * @return true
@ -179,8 +199,10 @@ bool CmsEngine::updatePlanStatus(const std::string& planId, PlanStatus newStatus
// ══════════════════════════════════════════════════════════════════════════════ // ══════════════════════════════════════════════════════════════════════════════
/** /**
* @brief * @requirement(name="模板注册", id="REQ-TMPL-REG-001")
*
* *
* @brief
* @param tmpl * @param tmpl
*/ */
void CmsEngine::registerTemplate(const TemplateInstance& tmpl) void CmsEngine::registerTemplate(const TemplateInstance& tmpl)
@ -189,10 +211,11 @@ 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
*/ */
@ -216,10 +239,11 @@ 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)
@ -228,10 +252,11 @@ 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
@ -268,10 +293,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
@ -289,10 +314,11 @@ 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
@ -313,8 +339,10 @@ bool CmsEngine::checkPermission(const std::string& userId,
// ══════════════════════════════════════════════════════════════════════════════ // ══════════════════════════════════════════════════════════════════════════════
/** /**
* @brief * @requirement(name="通知推送", id="REQ-NOTIF-PUSH-001")
*
* *
* @brief
* @param msg * @param msg
*/ */
void CmsEngine::pushNotification(const NotificationMessage& msg) void CmsEngine::pushNotification(const NotificationMessage& msg)
@ -323,8 +351,11 @@ void CmsEngine::pushNotification(const NotificationMessage& msg)
} }
/** /**
* @brief * @requirement(name="未读通知查询", id="REQ-NOTIF-UNREAD-001")
*
*
* *
* @brief
* @return std::vector<NotificationMessage> * @return std::vector<NotificationMessage>
*/ */
std::vector<NotificationMessage> CmsEngine::getUnreadNotifications() const std::vector<NotificationMessage> CmsEngine::getUnreadNotifications() const
@ -343,10 +374,11 @@ 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
@ -364,8 +396,11 @@ bool CmsEngine::switchMode(RunMode newMode)
} }
/** /**
* @brief * @requirement(name="系统状态查询", id="REQ-MODE-QUERY-001")
*
*
* *
* @brief
* @return const SystemStateContext& * @return const SystemStateContext&
*/ */
const SystemStateContext& CmsEngine::getSystemContext() const const SystemStateContext& CmsEngine::getSystemContext() const
@ -374,8 +409,11 @@ const SystemStateContext& CmsEngine::getSystemContext() const
} }
/** /**
* @brief * @requirement(name="引擎摘要生成", id="REQ-SUMM-GEN-001")
*
*
* *
* @brief
* @return std::string * @return std::string
*/ */
std::string CmsEngine::getSummary() const std::string CmsEngine::getSummary() const

View File

@ -1,25 +0,0 @@
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 17)
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)

View File

@ -1,240 +0,0 @@
#include <gtest/gtest.h>
#include "app.hpp"
#include <chrono>
#include <string>
class CmsEngineTest : public ::testing::Test {
protected:
CmsEngine engine;
EventRecord makeEvent(const std::string& id, int priority, EventStatus status) {
EventRecord evt;
evt.id = id;
evt.priority = priority;
evt.status = status;
return evt;
}
TaskPlan makePlan(const std::string& name, PlanType type, PlanStatus status) {
TaskPlan plan;
plan.name = name;
plan.type = type;
plan.status = status;
return plan;
}
TemplateInstance makeTemplate(const std::string& id, double confidence) {
TemplateInstance tmpl;
tmpl.id = id;
tmpl.confidence = confidence;
return tmpl;
}
ExecutionStatus makeExecStatus(const std::string& nodeId, double health, int errorCode, std::chrono::system_clock::time_point lastReport) {
ExecutionStatus s;
s.nodeId = nodeId;
s.healthIndex = health;
s.errorCode = errorCode;
s.lastReport = lastReport;
return s;
}
UserSession makeSession(const std::string& userId, uint64_t perm) {
UserSession s;
s.userId = userId;
s.permissionBitmap = perm;
return s;
}
NotificationMessage makeNotification(const std::string& id, bool isRead) {
NotificationMessage msg;
msg.id = id;
msg.isRead = isRead;
return msg;
}
};
// 1. CmsEngine 构造函数测试
TEST_F(CmsEngineTest, testConstructorInitializesCorrectly) {
EXPECT_EQ(engine.getSystemContext().currentMode, RunMode::Idle);
EXPECT_EQ(engine.getSystemContext().consistencyMark, 0);
}
// 2. generateNextId (私有函数,通过公开接口间接测试)
TEST_F(CmsEngineTest, testGenerateNextIdIndirect) {
TaskPlan plan = makePlan("TestPlan", PlanType::Centralized, PlanStatus::Drafting);
EXPECT_TRUE(engine.createTaskPlan(plan));
EXPECT_FALSE(plan.id.empty());
EXPECT_EQ(plan.id.substr(0, 3), "ID-");
}
// 3. ingestEvent 正常输入
TEST_F(CmsEngineTest, testIngestEventValidInput) {
EventRecord evt = makeEvent("EVT1", 100, EventStatus::Pending);
EXPECT_TRUE(engine.ingestEvent(evt));
EXPECT_NE(engine.findEventById("EVT1"), nullptr);
}
// 3. ingestEvent 边界值空ID
TEST_F(CmsEngineTest, testIngestEventEmptyId) {
EventRecord evt = makeEvent("", 50, EventStatus::Pending);
EXPECT_FALSE(engine.ingestEvent(evt));
}
// 3. ingestEvent 异常输入:优先级越界
TEST_F(CmsEngineTest, testIngestEventHighPriority) {
EventRecord evt = makeEvent("EVT2", 256, EventStatus::Pending);
EXPECT_FALSE(engine.ingestEvent(evt));
}
// 4. processPendingEvents 正常处理
TEST_F(CmsEngineTest, testProcessPendingEventsMultiple) {
engine.ingestEvent(makeEvent("EVT1", 10, EventStatus::Pending));
engine.ingestEvent(makeEvent("EVT2", 20, EventStatus::Pending));
EXPECT_EQ(engine.processPendingEvents(), 2);
EXPECT_EQ(engine.findEventById("EVT1")->status, EventStatus::Generated);
EXPECT_EQ(engine.getAllPlans().size(), 2);
}
// 5. findEventById 未找到场景
TEST_F(CmsEngineTest, testFindEventByIdNotFound) {
EXPECT_EQ(engine.findEventById("NONEXIST"), nullptr);
}
// 6. createTaskPlan 正常输入
TEST_F(CmsEngineTest, testCreateTaskPlanValid) {
TaskPlan plan = makePlan("ValidPlan", PlanType::Centralized, PlanStatus::Drafting);
EXPECT_TRUE(engine.createTaskPlan(plan));
EXPECT_FALSE(plan.id.empty());
}
// 6. createTaskPlan 边界值:空名称
TEST_F(CmsEngineTest, testCreateTaskPlanEmptyName) {
TaskPlan plan = makePlan("", PlanType::Centralized, PlanStatus::Drafting);
EXPECT_FALSE(engine.createTaskPlan(plan));
}
// 7. getAllPlans 正常获取
TEST_F(CmsEngineTest, testGetAllPlans) {
engine.createTaskPlan(makePlan("P1", PlanType::Centralized, PlanStatus::Drafting));
const auto& plans = engine.getAllPlans();
EXPECT_EQ(plans.size(), 1);
EXPECT_EQ(plans[0].name, "P1");
}
// 8. findPlanById 正常查找
TEST_F(CmsEngineTest, testFindPlanById) {
TaskPlan plan = makePlan("P1", PlanType::Centralized, PlanStatus::Drafting);
engine.createTaskPlan(plan);
const auto* found = engine.findPlanById(plan.id);
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->name, "P1");
}
// 9. updatePlanStatus 正常更新
TEST_F(CmsEngineTest, testUpdatePlanStatusSuccess) {
TaskPlan plan = makePlan("P1", PlanType::Centralized, PlanStatus::Drafting);
engine.createTaskPlan(plan);
auto planId = engine.getAllPlans()[0].id;
EXPECT_TRUE(engine.updatePlanStatus(planId, static_cast<PlanStatus>(1)));
EXPECT_EQ(engine.findPlanById(planId)->status, static_cast<PlanStatus>(1));
}
// 10. registerTemplate 正常注册
TEST_F(CmsEngineTest, testRegisterTemplate) {
TemplateInstance tmpl = makeTemplate("T1", 0.8);
engine.registerTemplate(tmpl);
EXPECT_EQ(engine.matchTemplate("any")->id, "T1");
}
// 11. matchTemplate 空列表边界
TEST_F(CmsEngineTest, testMatchTemplateEmptyList) {
EXPECT_EQ(engine.matchTemplate("scenario"), nullptr);
}
// 11. matchTemplate 最高置信度匹配
TEST_F(CmsEngineTest, testMatchTemplateHighestConfidence) {
engine.registerTemplate(makeTemplate("T1", 0.7));
engine.registerTemplate(makeTemplate("T2", 0.9));
const auto* best = engine.matchTemplate("any");
ASSERT_NE(best, nullptr);
EXPECT_EQ(best->id, "T2");
}
// 12. reportExecutionStatus 正常上报
TEST_F(CmsEngineTest, testReportExecutionStatus) {
auto now = std::chrono::system_clock::now();
ExecutionStatus s = makeExecStatus("N1", 0.9, 0, now);
engine.reportExecutionStatus(s);
EXPECT_EQ(engine.checkHealth().size(), 0);
}
// 13. checkHealth 异常节点检测
TEST_F(CmsEngineTest, testCheckHealthUnhealthyNode) {
auto now = std::chrono::system_clock::now();
engine.reportExecutionStatus(makeExecStatus("N1", 0.4, 0, now));
auto unhealthy = engine.checkHealth();
EXPECT_EQ(unhealthy.size(), 1);
EXPECT_EQ(unhealthy[0].nodeId, "N1");
}
// 14. createSession 正常创建与重复拦截
TEST_F(CmsEngineTest, testCreateSessionValidAndDuplicate) {
UserSession s1 = makeSession("U1", 0xFF);
UserSession s2 = makeSession("U1", 0xAA);
EXPECT_TRUE(engine.createSession(s1));
EXPECT_FALSE(engine.createSession(s2));
}
// 15. checkPermission 权限校验
TEST_F(CmsEngineTest, testCheckPermission) {
engine.createSession(makeSession("U1", 0x0F));
EXPECT_TRUE(engine.checkPermission("U1", 0x03));
EXPECT_FALSE(engine.checkPermission("U1", 0x10));
EXPECT_FALSE(engine.checkPermission("UNKNOWN", 0x01));
}
// 16. pushNotification 正常推送
TEST_F(CmsEngineTest, testPushNotification) {
engine.pushNotification(makeNotification("MSG1", false));
EXPECT_EQ(engine.getUnreadNotifications().size(), 1);
}
// 17. getUnreadNotifications 过滤已读
TEST_F(CmsEngineTest, testGetUnreadNotificationsFilter) {
engine.pushNotification(makeNotification("MSG1", false));
engine.pushNotification(makeNotification("MSG2", true));
auto unread = engine.getUnreadNotifications();
EXPECT_EQ(unread.size(), 1);
EXPECT_EQ(unread[0].id, "MSG1");
}
// 18. switchMode 正常切换
TEST_F(CmsEngineTest, testSwitchModeNormal) {
EXPECT_TRUE(engine.switchMode(RunMode::HumanLoop));
EXPECT_EQ(engine.getSystemContext().currentMode, RunMode::HumanLoop);
}
// 18. switchMode 降级模式限制
TEST_F(CmsEngineTest, testSwitchModeDegradedRestriction) {
engine.switchMode(RunMode::Degraded);
EXPECT_FALSE(engine.switchMode(RunMode::AutoExec));
EXPECT_TRUE(engine.switchMode(RunMode::Degraded));
}
// 19. getSystemContext 状态获取
TEST_F(CmsEngineTest, testGetSystemContext) {
const auto& ctx = engine.getSystemContext();
EXPECT_EQ(ctx.currentMode, RunMode::Idle);
EXPECT_FALSE(ctx.contextSnapshot.empty());
}
// 20. getSummary 摘要生成
TEST_F(CmsEngineTest, testGetSummaryContent) {
engine.ingestEvent(makeEvent("E1", 10, EventStatus::Pending));
engine.createTaskPlan(makePlan("P1", PlanType::Centralized, PlanStatus::Drafting));
auto 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("Mode : Idle"), std::string::npos);
}