Compare commits

..

1 Commits

Author SHA1 Message Date
lids 840ccd6725 AI 自动生成测试用例 2026-06-02 11:49:24 +08:00
3 changed files with 400 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)

337
tests/test_app.cpp Normal file
View File

@ -0,0 +1,337 @@
#include <gtest/gtest.h>
#include <string>
#include <vector>
#include <chrono>
#include "app.hpp"
// 辅助函数:创建一个有效的 EventRecord
EventRecord createValidEvent(const std::string& id = "EVT-001", uint32_t priority = 100, EventStatus status = EventStatus::Pending) {
EventRecord event;
event.id = id;
event.priority = priority;
event.status = status;
event.timestamp = std::chrono::system_clock::now();
event.source = "test_source";
event.type = "test_type";
event.payload = R"({"key":"value"})";
return event;
}
// ============================================================================
// 测试 ingestEvent 函数
// ============================================================================
/**
* @brief ingestEvent
*/
TEST(CmsEngineTest, testIngestEventNormal) {
CmsEngine engine;
EventRecord event = createValidEvent();
bool result = engine.ingestEvent(event);
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);
}
/**
* @brief ingestEvent ID false
*/
TEST(CmsEngineTest, testIngestEventEmptyId) {
CmsEngine engine;
EventRecord event = createValidEvent("", 100);
bool result = engine.ingestEvent(event);
EXPECT_FALSE(result);
// 验证事件未被存储
const EventRecord* found = engine.findEventById("");
EXPECT_EQ(found, nullptr);
}
/**
* @brief ingestEvent 0
*/
TEST(CmsEngineTest, testIngestEventPriorityMin) {
CmsEngine engine;
EventRecord event = createValidEvent("EVT-002", 0);
bool result = engine.ingestEvent(event);
EXPECT_TRUE(result);
const EventRecord* found = engine.findEventById("EVT-002");
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->priority, 0);
}
/**
* @brief ingestEvent 255
*/
TEST(CmsEngineTest, testIngestEventPriorityMax) {
CmsEngine engine;
EventRecord event = createValidEvent("EVT-003", 255);
bool result = engine.ingestEvent(event);
EXPECT_TRUE(result);
const EventRecord* found = engine.findEventById("EVT-003");
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->priority, 255);
}
/**
* @brief ingestEvent 256
*/
TEST(CmsEngineTest, testIngestEventPriorityOverflow) {
CmsEngine engine;
EventRecord event = createValidEvent("EVT-004", 256);
bool result = engine.ingestEvent(event);
EXPECT_FALSE(result);
const EventRecord* found = engine.findEventById("EVT-004");
EXPECT_EQ(found, nullptr);
}
/**
* @brief ingestEvent
*/
TEST(CmsEngineTest, testIngestEventMultipleEvents) {
CmsEngine engine;
const int eventCount = 100;
for (int i = 0; i < eventCount; ++i) {
std::string id = "EVT-" + std::to_string(i);
EventRecord event = createValidEvent(id, i % 256);
EXPECT_TRUE(engine.ingestEvent(event));
}
// 验证所有事件都能被找到
for (int i = 0; i < eventCount; ++i) {
std::string id = "EVT-" + std::to_string(i);
const EventRecord* found = engine.findEventById(id);
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->priority, i % 256);
}
}
// ============================================================================
// 测试 processPendingEvents 函数
// ============================================================================
/**
* @brief processPendingEvents Pending
*/
TEST(CmsEngineTest, testProcessPendingEventsNormal) {
CmsEngine engine;
// 添加多个 Pending 事件
engine.ingestEvent(createValidEvent("EVT-001", 100, EventStatus::Pending));
engine.ingestEvent(createValidEvent("EVT-002", 200, EventStatus::Pending));
engine.ingestEvent(createValidEvent("EVT-003", 50, EventStatus::Pending));
size_t processed = engine.processPendingEvents();
EXPECT_EQ(processed, 3);
// 验证事件状态已更新
const EventRecord* evt1 = engine.findEventById("EVT-001");
ASSERT_NE(evt1, nullptr);
EXPECT_EQ(evt1->status, EventStatus::Generated);
const EventRecord* evt2 = engine.findEventById("EVT-002");
ASSERT_NE(evt2, nullptr);
EXPECT_EQ(evt2->status, EventStatus::Generated);
}
/**
* @brief processPendingEvents Pending
*/
TEST(CmsEngineTest, testProcessPendingEventsNoPending) {
CmsEngine engine;
// 添加非 Pending 事件
engine.ingestEvent(createValidEvent("EVT-001", 100, EventStatus::Generated));
engine.ingestEvent(createValidEvent("EVT-002", 200, EventStatus::Completed));
size_t processed = engine.processPendingEvents();
EXPECT_EQ(processed, 0);
// 验证事件状态未改变
const EventRecord* evt1 = engine.findEventById("EVT-001");
ASSERT_NE(evt1, nullptr);
EXPECT_EQ(evt1->status, EventStatus::Generated);
}
/**
* @brief processPendingEvents
*/
TEST(CmsEngineTest, testProcessPendingEventsMixed) {
CmsEngine engine;
engine.ingestEvent(createValidEvent("EVT-001", 100, EventStatus::Pending));
engine.ingestEvent(createValidEvent("EVT-002", 200, EventStatus::Generated));
engine.ingestEvent(createValidEvent("EVT-003", 50, EventStatus::Pending));
engine.ingestEvent(createValidEvent("EVT-004", 150, EventStatus::Failed));
size_t processed = engine.processPendingEvents();
EXPECT_EQ(processed, 2);
// 验证只有 Pending 事件被处理
const EventRecord* evt1 = engine.findEventById("EVT-001");
ASSERT_NE(evt1, nullptr);
EXPECT_EQ(evt1->status, EventStatus::Generated);
const EventRecord* evt2 = engine.findEventById("EVT-002");
ASSERT_NE(evt2, nullptr);
EXPECT_EQ(evt2->status, EventStatus::Generated); // 未改变
const EventRecord* evt4 = engine.findEventById("EVT-004");
ASSERT_NE(evt4, nullptr);
EXPECT_EQ(evt4->status, EventStatus::Failed); // 未改变
}
/**
* @brief processPendingEvents TaskPlan
*/
TEST(CmsEngineTest, testProcessPendingEventsTaskPlanGeneration) {
CmsEngine engine;
engine.ingestEvent(createValidEvent("EVT-001", 100, EventStatus::Pending));
size_t processed = engine.processPendingEvents();
EXPECT_EQ(processed, 1);
// 验证生成的 TaskPlan
const std::vector<TaskPlan>& plans = engine.getAllPlans();
ASSERT_EQ(plans.size(), 1);
const TaskPlan& plan = plans[0];
EXPECT_EQ(plan.name, "AutoPlan-EVT-001");
EXPECT_EQ(plan.type, PlanType::Centralized);
EXPECT_EQ(plan.status, PlanStatus::Drafting);
EXPECT_EQ(plan.relatedEventId, "EVT-001");
EXPECT_DOUBLE_EQ(plan.resourceQuota, 0.5);
EXPECT_EQ(plan.constraints, "自动生成约束");
EXPECT_FALSE(plan.id.empty());
}
/**
* @brief processPendingEvents
*/
TEST(CmsEngineTest, testProcessPendingEventsEmpty) {
CmsEngine engine;
size_t processed = engine.processPendingEvents();
EXPECT_EQ(processed, 0);
EXPECT_TRUE(engine.getAllPlans().empty());
}
// ============================================================================
// 测试 findEventById 函数
// ============================================================================
/**
* @brief findEventById
*/
TEST(CmsEngineTest, testFindEventByIdNormal) {
CmsEngine engine;
engine.ingestEvent(createValidEvent("EVT-001", 100));
engine.ingestEvent(createValidEvent("EVT-002", 200));
const EventRecord* found = engine.findEventById("EVT-001");
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->id, "EVT-001");
EXPECT_EQ(found->priority, 100);
}
/**
* @brief findEventById ID
*/
TEST(CmsEngineTest, testFindEventByIdNotFound) {
CmsEngine engine;
engine.ingestEvent(createValidEvent("EVT-001", 100));
const EventRecord* found = engine.findEventById("NONEXISTENT");
EXPECT_EQ(found, nullptr);
}
/**
* @brief findEventById
*/
TEST(CmsEngineTest, testFindEventByIdEmptyString) {
CmsEngine engine;
engine.ingestEvent(createValidEvent("EVT-001", 100));
const EventRecord* found = engine.findEventById("");
EXPECT_EQ(found, nullptr);
}
/**
* @brief findEventById
*/
TEST(CmsEngineTest, testFindEventByIdEmptyEngine) {
CmsEngine engine;
const EventRecord* found = engine.findEventById("EVT-001");
EXPECT_EQ(found, nullptr);
}
/**
* @brief findEventById
*/
TEST(CmsEngineTest, testFindEventByIdLastEvent) {
CmsEngine engine;
engine.ingestEvent(createValidEvent("EVT-001", 100));
engine.ingestEvent(createValidEvent("EVT-002", 200));
engine.ingestEvent(createValidEvent("EVT-003", 300));
const EventRecord* found = engine.findEventById("EVT-003");
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->id, "EVT-003");
EXPECT_EQ(found->priority, 300);
}
/**
* @brief findEventById
*/
TEST(CmsEngineTest, testFindEventByIdCaseSensitive) {
CmsEngine engine;
engine.ingestEvent(createValidEvent("EVT-001", 100));
const EventRecord* found = engine.findEventById("evt-001");
EXPECT_EQ(found, nullptr);
}
/**
* @brief findEventById
*/
TEST(CmsEngineTest, testFindEventByIdConstCorrectness) {
CmsEngine engine;
engine.ingestEvent(createValidEvent("EVT-001", 100));
const CmsEngine& constEngine = engine;
const EventRecord* found = constEngine.findEventById("EVT-001");
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->id, "EVT-001");
}