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 377 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_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)

View File

@ -1,314 +0,0 @@
#include <gtest/gtest.h>
#include <string>
#include <vector>
#include <chrono>
#include "app.hpp"
// ============================================================================
// 测试夹具CmsEngineTest
// ============================================================================
class CmsEngineTest : public ::testing::Test {
protected:
CmsEngine engine;
// 辅助函数创建一个有效的事件注意EventRecord 没有 timestamp 成员)
EventRecord createValidEvent(const std::string& id = "EVT-001", int priority = 100) {
EventRecord evt;
evt.id = id;
evt.priority = priority;
evt.status = EventStatus::Pending;
return evt;
}
// 辅助函数:向引擎添加多个事件
void addEventsToEngine(const std::vector<EventRecord>& events) {
for (const auto& evt : events) {
engine.ingestEvent(evt);
}
}
};
// ============================================================================
// ingestEvent 测试用例
// ============================================================================
/**
* @brief ingestEvent
*/
TEST_F(CmsEngineTest, testIngestEventValidEvent) {
EventRecord evt = createValidEvent("EVT-001", 100);
bool result = engine.ingestEvent(evt);
EXPECT_TRUE(result);
// 验证事件已存储
const EventRecord* found = engine.findEventById("EVT-001");
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->id, "EVT-001");
EXPECT_EQ(found->priority, 100);
EXPECT_EQ(found->status, EventStatus::Pending);
}
/**
* @brief ingestEvent 0
*/
TEST_F(CmsEngineTest, testIngestEventPriorityMin) {
EventRecord evt = createValidEvent("EVT-002", 0);
bool result = engine.ingestEvent(evt);
EXPECT_TRUE(result);
const EventRecord* found = engine.findEventById("EVT-002");
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->priority, 0);
}
/**
* @brief ingestEvent 255
*/
TEST_F(CmsEngineTest, testIngestEventPriorityMax) {
EventRecord evt = createValidEvent("EVT-003", 255);
bool result = engine.ingestEvent(evt);
EXPECT_TRUE(result);
const EventRecord* found = engine.findEventById("EVT-003");
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->priority, 255);
}
/**
* @brief ingestEvent ID false
*/
TEST_F(CmsEngineTest, testIngestEventEmptyId) {
EventRecord evt = createValidEvent("", 100);
bool result = engine.ingestEvent(evt);
EXPECT_FALSE(result);
// 验证事件未存储
const EventRecord* found = engine.findEventById("");
EXPECT_EQ(found, nullptr);
}
/**
* @brief ingestEvent 255 false
*/
TEST_F(CmsEngineTest, testIngestEventPriorityOverflow) {
EventRecord evt = createValidEvent("EVT-004", 256);
bool result = engine.ingestEvent(evt);
EXPECT_FALSE(result);
const EventRecord* found = engine.findEventById("EVT-004");
EXPECT_EQ(found, nullptr);
}
/**
* @brief ingestEvent ID
*/
TEST_F(CmsEngineTest, testIngestEventDuplicateId) {
EventRecord evt1 = createValidEvent("EVT-005", 100);
EventRecord evt2 = createValidEvent("EVT-005", 200);
bool result1 = engine.ingestEvent(evt1);
EXPECT_TRUE(result1);
bool result2 = engine.ingestEvent(evt2);
EXPECT_TRUE(result2);
// 验证两个事件都存储findEventById 返回第一个匹配的)
const EventRecord* found = engine.findEventById("EVT-005");
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->priority, 100);
}
// ============================================================================
// processPendingEvents 测试用例
// ============================================================================
/**
* @brief processPendingEvents Pending
*/
TEST_F(CmsEngineTest, testProcessPendingEventsAllPending) {
std::vector<EventRecord> events = {
createValidEvent("EVT-010", 100),
createValidEvent("EVT-011", 200),
createValidEvent("EVT-012", 150)
};
addEventsToEngine(events);
size_t processed = engine.processPendingEvents();
EXPECT_EQ(processed, 3);
// 验证事件状态已更新
const EventRecord* evt1 = engine.findEventById("EVT-010");
ASSERT_NE(evt1, nullptr);
EXPECT_EQ(evt1->status, EventStatus::Generated);
const EventRecord* evt2 = engine.findEventById("EVT-011");
ASSERT_NE(evt2, nullptr);
EXPECT_EQ(evt2->status, EventStatus::Generated);
// 验证生成了对应的 TaskPlan
const std::vector<TaskPlan>& plans = engine.getAllPlans();
EXPECT_EQ(plans.size(), 3);
EXPECT_EQ(plans[0].relatedEventId, "EVT-010");
EXPECT_EQ(plans[0].type, PlanType::Centralized);
EXPECT_EQ(plans[0].status, PlanStatus::Drafting);
}
/**
* @brief processPendingEvents Pending
*/
TEST_F(CmsEngineTest, testProcessPendingEventsNoPending) {
// 添加一个非 Pending 状态的事件
EventRecord evt = createValidEvent("EVT-020", 100);
evt.status = EventStatus::Generated;
engine.ingestEvent(evt);
size_t processed = engine.processPendingEvents();
EXPECT_EQ(processed, 0);
// 验证没有生成新的 TaskPlan
const std::vector<TaskPlan>& plans = engine.getAllPlans();
EXPECT_EQ(plans.size(), 0);
}
/**
* @brief processPendingEvents
*/
TEST_F(CmsEngineTest, testProcessPendingEventsEmpty) {
size_t processed = engine.processPendingEvents();
EXPECT_EQ(processed, 0);
const std::vector<TaskPlan>& plans = engine.getAllPlans();
EXPECT_EQ(plans.size(), 0);
}
/**
* @brief processPendingEvents
*/
TEST_F(CmsEngineTest, testProcessPendingEventsMixedStatus) {
EventRecord evt1 = createValidEvent("EVT-030", 100);
evt1.status = EventStatus::Pending;
engine.ingestEvent(evt1);
EventRecord evt2 = createValidEvent("EVT-031", 200);
evt2.status = EventStatus::Generated;
engine.ingestEvent(evt2);
EventRecord evt3 = createValidEvent("EVT-032", 150);
evt3.status = EventStatus::Pending;
engine.ingestEvent(evt3);
size_t processed = engine.processPendingEvents();
EXPECT_EQ(processed, 2);
// 验证只有 Pending 的事件被处理
const EventRecord* evt1_after = engine.findEventById("EVT-030");
ASSERT_NE(evt1_after, nullptr);
EXPECT_EQ(evt1_after->status, EventStatus::Generated);
const EventRecord* evt2_after = engine.findEventById("EVT-031");
ASSERT_NE(evt2_after, nullptr);
EXPECT_EQ(evt2_after->status, EventStatus::Generated);
const EventRecord* evt3_after = engine.findEventById("EVT-032");
ASSERT_NE(evt3_after, nullptr);
EXPECT_EQ(evt3_after->status, EventStatus::Generated);
// 验证生成了 2 个 TaskPlan
const std::vector<TaskPlan>& plans = engine.getAllPlans();
EXPECT_EQ(plans.size(), 2);
}
// ============================================================================
// findEventById 测试用例
// ============================================================================
/**
* @brief findEventById
*/
TEST_F(CmsEngineTest, testFindEventByIdExisting) {
EventRecord evt = createValidEvent("EVT-100", 100);
engine.ingestEvent(evt);
const EventRecord* found = engine.findEventById("EVT-100");
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->id, "EVT-100");
EXPECT_EQ(found->priority, 100);
EXPECT_EQ(found->status, EventStatus::Pending);
}
/**
* @brief findEventById ID
*/
TEST_F(CmsEngineTest, testFindEventByIdEmptyString) {
// 添加一个空 ID 的事件(虽然 ingestEvent 会拒绝,但直接测试 findEventById
const EventRecord* found = engine.findEventById("");
EXPECT_EQ(found, nullptr);
}
/**
* @brief findEventById
*/
TEST_F(CmsEngineTest, testFindEventByIdNonExistent) {
EventRecord evt = createValidEvent("EVT-200", 100);
engine.ingestEvent(evt);
const EventRecord* found = engine.findEventById("EVT-999");
EXPECT_EQ(found, nullptr);
}
/**
* @brief findEventById ID
*/
TEST_F(CmsEngineTest, testFindEventByIdLongId) {
std::string longId = "EVT-" + std::string(100, 'A');
EventRecord evt = createValidEvent(longId, 100);
engine.ingestEvent(evt);
const EventRecord* found = engine.findEventById(longId);
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->id, longId);
}
/**
* @brief findEventById ID
*/
TEST_F(CmsEngineTest, testFindEventByIdAmongMultiple) {
std::vector<EventRecord> events = {
createValidEvent("EVT-300", 100),
createValidEvent("EVT-301", 200),
createValidEvent("EVT-302", 150)
};
addEventsToEngine(events);
// 查找中间的事件
const EventRecord* found = engine.findEventById("EVT-301");
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->id, "EVT-301");
EXPECT_EQ(found->priority, 200);
// 查找第一个事件
found = engine.findEventById("EVT-300");
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->id, "EVT-300");
// 查找最后一个事件
found = engine.findEventById("EVT-302");
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->id, "EVT-302");
}
/**
* @brief findEventById
*/
TEST_F(CmsEngineTest, testFindEventByIdAfterProcessing) {
EventRecord evt = createValidEvent("EVT-400", 100);
engine.ingestEvent(evt);
// 处理事件
engine.processPendingEvents();
// 查找事件并验证状态已更新
const EventRecord* found = engine.findEventById("EVT-400");
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->status, EventStatus::Generated);
}