Compare commits

..

1 Commits

Author SHA1 Message Date
lids 2d14c9f5e3 AI 自动生成测试用例 2026-05-25 16:22:54 +08:00
3 changed files with 426 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 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)

363
tests/test_app.cpp Normal file
View File

@ -0,0 +1,363 @@
#include <gtest/gtest.h>
#include "app.hpp"
#include <string>
#include <vector>
// ============================================================================
// CmsEngine 测试夹具
// ============================================================================
class CmsEngineTest : public ::testing::Test {
protected:
void SetUp() override {
// 初始化引擎
engine = new CmsEngine();
// 准备测试事件
EventRecord event1;
event1.id = "EVT-001";
event1.priority = 100;
event1.status = EventStatus::Pending;
event1.description = "Test event 1";
EventRecord event2;
event2.id = "EVT-002";
event2.priority = 200;
event2.status = EventStatus::Pending;
event2.description = "Test event 2";
EventRecord event3;
event3.id = "EVT-003";
event3.priority = 50;
event3.status = EventStatus::Generated; // 非 Pending 状态
event3.description = "Test event 3";
// 注入测试事件
engine->ingestEvent(event1);
engine->ingestEvent(event2);
engine->ingestEvent(event3);
}
void TearDown() override {
delete engine;
}
CmsEngine* engine;
};
// ============================================================================
// processPendingEvents 测试用例
// ============================================================================
/**
* @brief
*
* processPendingEvents Pending
* TaskPlan
*/
TEST_F(CmsEngineTest, testProcessPendingEventsNormal) {
// 执行:处理待处理事件
size_t processedCount = engine->processPendingEvents();
// 验证:应该处理了 2 个 Pending 事件EVT-001 和 EVT-002
EXPECT_EQ(processedCount, 2);
// 验证:事件状态已更新
const EventRecord* event1 = engine->findEventById("EVT-001");
ASSERT_NE(event1, nullptr);
EXPECT_EQ(event1->status, EventStatus::Generated);
const EventRecord* event2 = engine->findEventById("EVT-002");
ASSERT_NE(event2, nullptr);
EXPECT_EQ(event2->status, EventStatus::Generated);
// 验证EVT-003 状态未改变(原本就是 Generated
const EventRecord* event3 = engine->findEventById("EVT-003");
ASSERT_NE(event3, nullptr);
EXPECT_EQ(event3->status, EventStatus::Generated);
// 验证:生成了对应的 TaskPlan
const std::vector<TaskPlan>& plans = engine->getAllPlans();
EXPECT_EQ(plans.size(), 2);
// 验证:生成的计划与事件关联
bool foundPlan1 = false;
bool foundPlan2 = false;
for (const auto& plan : plans) {
if (plan.relatedEventId == "EVT-001") {
foundPlan1 = true;
EXPECT_EQ(plan.name, "AutoPlan-EVT-001");
EXPECT_EQ(plan.type, PlanType::Centralized);
EXPECT_EQ(plan.status, PlanStatus::Drafting);
EXPECT_FLOAT_EQ(plan.resourceQuota, 0.5f);
EXPECT_EQ(plan.constraints, "自动生成约束");
} else if (plan.relatedEventId == "EVT-002") {
foundPlan2 = true;
EXPECT_EQ(plan.name, "AutoPlan-EVT-002");
}
}
EXPECT_TRUE(foundPlan1);
EXPECT_TRUE(foundPlan2);
}
/**
* @brief
*
* Pending processPendingEvents 0
*/
TEST_F(CmsEngineTest, testProcessPendingEventsNoPending) {
// 先处理所有 Pending 事件
engine->processPendingEvents();
// 再次执行:此时没有 Pending 事件
size_t processedCount = engine->processPendingEvents();
// 验证:处理数量为 0
EXPECT_EQ(processedCount, 0);
// 验证:计划数量没有增加
const std::vector<TaskPlan>& plans = engine->getAllPlans();
EXPECT_EQ(plans.size(), 2);
}
/**
* @brief
*
* processPendingEvents 0
*/
TEST_F(CmsEngineTest, testProcessPendingEventsEmpty) {
// 创建新的空引擎
CmsEngine emptyEngine;
// 执行:处理待处理事件
size_t processedCount = emptyEngine.processPendingEvents();
// 验证:处理数量为 0
EXPECT_EQ(processedCount, 0);
// 验证:没有生成任何计划
const std::vector<TaskPlan>& plans = emptyEngine.getAllPlans();
EXPECT_TRUE(plans.empty());
}
/**
* @brief
*
* processPendingEvents Pending
*
*/
TEST_F(CmsEngineTest, testProcessPendingEventsMixedStatus) {
// 添加更多不同状态的事件
EventRecord event4;
event4.id = "EVT-004";
event4.priority = 150;
event4.status = EventStatus::Pending;
event4.description = "Test event 4";
EventRecord event5;
event5.id = "EVT-005";
event5.priority = 180;
event5.status = EventStatus::Generated;
event5.description = "Test event 5";
engine->ingestEvent(event4);
engine->ingestEvent(event5);
// 执行:处理待处理事件
size_t processedCount = engine->processPendingEvents();
// 验证:只处理了 Pending 事件EVT-001, EVT-002, EVT-004
EXPECT_EQ(processedCount, 3);
// 验证EVT-005 状态未改变
const EventRecord* event5Ptr = engine->findEventById("EVT-005");
ASSERT_NE(event5Ptr, nullptr);
EXPECT_EQ(event5Ptr->status, EventStatus::Generated);
}
/**
* @brief processPendingEvents
*
* processPendingEvents
*/
TEST_F(CmsEngineTest, testProcessPendingEventsIdempotent) {
// 第一次调用
size_t firstCount = engine->processPendingEvents();
EXPECT_EQ(firstCount, 2);
// 第二次调用
size_t secondCount = engine->processPendingEvents();
EXPECT_EQ(secondCount, 0);
// 第三次调用
size_t thirdCount = engine->processPendingEvents();
EXPECT_EQ(thirdCount, 0);
// 验证:计划数量保持不变
const std::vector<TaskPlan>& plans = engine->getAllPlans();
EXPECT_EQ(plans.size(), 2);
}
// ============================================================================
// findEventById 测试用例
// ============================================================================
/**
* @brief
*
* findEventById
*/
TEST_F(CmsEngineTest, testFindEventByIdFound) {
// 执行:查找已存在的事件
const EventRecord* event = engine->findEventById("EVT-001");
// 验证:找到事件
ASSERT_NE(event, nullptr);
EXPECT_EQ(event->id, "EVT-001");
EXPECT_EQ(event->priority, 100);
EXPECT_EQ(event->status, EventStatus::Pending);
EXPECT_EQ(event->description, "Test event 1");
}
/**
* @brief
*
* findEventById nullptr
*/
TEST_F(CmsEngineTest, testFindEventByIdNotFound) {
// 执行:查找不存在的事件
const EventRecord* event = engine->findEventById("NONEXISTENT");
// 验证:未找到事件
EXPECT_EQ(event, nullptr);
}
/**
* @brief ID
*
* findEventById ID nullptr
*/
TEST_F(CmsEngineTest, testFindEventByIdEmptyId) {
// 执行:查找空字符串 ID
const EventRecord* event = engine->findEventById("");
// 验证:未找到事件
EXPECT_EQ(event, nullptr);
}
/**
* @brief ID
*
* findEventById ID
*/
TEST_F(CmsEngineTest, testFindEventByIdSpecialChars) {
// 添加包含特殊字符的事件
EventRecord specialEvent;
specialEvent.id = "EVT-@#$%";
specialEvent.priority = 50;
specialEvent.status = EventStatus::Pending;
specialEvent.description = "Special event";
engine->ingestEvent(specialEvent);
// 执行:查找特殊字符 ID
const EventRecord* event = engine->findEventById("EVT-@#$%");
// 验证:找到事件
ASSERT_NE(event, nullptr);
EXPECT_EQ(event->id, "EVT-@#$%");
}
/**
* @brief ID
*
* findEventById ID
*/
TEST_F(CmsEngineTest, testFindEventByIdLongId) {
// 添加超长 ID 的事件
std::string longId(1000, 'A');
EventRecord longEvent;
longEvent.id = longId;
longEvent.priority = 50;
longEvent.status = EventStatus::Pending;
longEvent.description = "Long ID event";
engine->ingestEvent(longEvent);
// 执行:查找超长 ID
const EventRecord* event = engine->findEventById(longId);
// 验证:找到事件
ASSERT_NE(event, nullptr);
EXPECT_EQ(event->id, longId);
}
/**
* @brief
*
* findEventById
*/
TEST_F(CmsEngineTest, testFindEventByIdAfterStatusChange) {
// 先处理事件,改变状态
engine->processPendingEvents();
// 执行:查找已处理的事件
const EventRecord* event = engine->findEventById("EVT-001");
// 验证:事件状态已更新
ASSERT_NE(event, nullptr);
EXPECT_EQ(event->status, EventStatus::Generated);
}
/**
* @brief
*
* findEventById nullptr
*/
TEST_F(CmsEngineTest, testFindEventByIdEmptyEngine) {
// 创建新的空引擎
CmsEngine emptyEngine;
// 执行:在空引擎中查找
const EventRecord* event = emptyEngine.findEventById("EVT-001");
// 验证:未找到事件
EXPECT_EQ(event, nullptr);
}
/**
* @brief
*
* findEventById
*/
TEST_F(CmsEngineTest, testFindEventByIdAmongMany) {
// 添加更多事件
for (int i = 0; i < 100; ++i) {
EventRecord event;
event.id = "EVT-" + std::to_string(1000 + i);
event.priority = i;
event.status = EventStatus::Pending;
event.description = "Bulk event " + std::to_string(i);
engine->ingestEvent(event);
}
// 执行:查找特定事件
const EventRecord* event = engine->findEventById("EVT-1050");
// 验证:找到事件
ASSERT_NE(event, nullptr);
EXPECT_EQ(event->id, "EVT-1050");
EXPECT_EQ(event->priority, 50);
}
/**
* @brief const
*
* findEventById const
*/
TEST_F(CmsEngineTest, testFindEventByIdConstCorrectness) {
// 执行:查找事件
const EventRecord* event = engine->findEventById("EVT-001");
// 验证:返回的是 const 指针
ASSERT_NE(event, nullptr);
// 以下代码如果取消注释编译应该失败const 正确性)
// event->status = EventStatus::Generated; // 编译错误
}