Compare commits

..

1 Commits

Author SHA1 Message Date
lids ea2286872a AI 自动生成测试用例 2026-05-14 16:59:06 +08:00
3 changed files with 462 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_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)

399
tests/test_cms_engine.cpp Normal file
View File

@ -0,0 +1,399 @@
#include <gtest/gtest.h>
#include <string>
#include <vector>
#include <chrono>
#include "app.hpp"
// ============================================================================
// Test Fixture for CmsEngine
// ============================================================================
class CmsEngineTest : public ::testing::Test {
protected:
void SetUp() override {
engine = new CmsEngine();
}
void TearDown() override {
delete engine;
}
CmsEngine* engine;
};
// ============================================================================
// Tests for CmsEngine constructor
// ============================================================================
/**
* @brief Test constructor initializes system context correctly
*/
TEST_F(CmsEngineTest, testConstructorNormalInitialization) {
const SystemStateContext& ctx = engine->getSystemContext();
EXPECT_EQ(ctx.currentMode, RunMode::Idle);
EXPECT_EQ(ctx.consistencyMark, 0);
EXPECT_EQ(ctx.contextSnapshot, R"({"mode":"Idle","version":"1.0.0"})");
}
/**
* @brief Test constructor initializes empty containers
*/
TEST_F(CmsEngineTest, testConstructorEmptyContainers) {
EXPECT_TRUE(engine->getAllPlans().empty());
std::string summary = engine->getSummary();
EXPECT_NE(summary.find("Events : 0"), std::string::npos);
}
// ============================================================================
// Tests for ingestEvent
// ============================================================================
/**
* @brief Test ingestEvent with valid event
*/
TEST_F(CmsEngineTest, testIngestEventValidEvent) {
EventRecord event;
event.id = "EVT-001";
event.priority = 100;
event.status = EventStatus::Pending;
bool result = engine->ingestEvent(event);
EXPECT_TRUE(result);
const EventRecord* found = engine->findEventById("EVT-001");
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->priority, 100);
EXPECT_EQ(found->status, EventStatus::Pending);
}
/**
* @brief Test ingestEvent with empty event ID
*/
TEST_F(CmsEngineTest, testIngestEventEmptyId) {
EventRecord event;
event.id = "";
event.priority = 100;
bool result = engine->ingestEvent(event);
EXPECT_FALSE(result);
const EventRecord* found = engine->findEventById("");
EXPECT_EQ(found, nullptr);
}
/**
* @brief Test ingestEvent with priority exceeding maximum
*/
TEST_F(CmsEngineTest, testIngestEventPriorityExceedsMax) {
EventRecord event;
event.id = "EVT-002";
event.priority = 256;
bool result = engine->ingestEvent(event);
EXPECT_FALSE(result);
const EventRecord* found = engine->findEventById("EVT-002");
EXPECT_EQ(found, nullptr);
}
/**
* @brief Test ingestEvent with priority at boundary (255)
*/
TEST_F(CmsEngineTest, testIngestEventPriorityAtBoundary) {
EventRecord event;
event.id = "EVT-003";
event.priority = 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 Test ingestEvent with priority at zero
*/
TEST_F(CmsEngineTest, testIngestEventPriorityZero) {
EventRecord event;
event.id = "EVT-004";
event.priority = 0;
bool result = engine->ingestEvent(event);
EXPECT_TRUE(result);
const EventRecord* found = engine->findEventById("EVT-004");
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->priority, 0);
}
/**
* @brief Test ingestEvent with multiple events
*/
TEST_F(CmsEngineTest, testIngestEventMultipleEvents) {
EventRecord event1;
event1.id = "EVT-001";
event1.priority = 50;
EventRecord event2;
event2.id = "EVT-002";
event2.priority = 150;
EXPECT_TRUE(engine->ingestEvent(event1));
EXPECT_TRUE(engine->ingestEvent(event2));
const EventRecord* found1 = engine->findEventById("EVT-001");
const EventRecord* found2 = engine->findEventById("EVT-002");
ASSERT_NE(found1, nullptr);
ASSERT_NE(found2, nullptr);
EXPECT_EQ(found1->priority, 50);
EXPECT_EQ(found2->priority, 150);
}
// ============================================================================
// Tests for findPlanById
// ============================================================================
/**
* @brief Test findPlanById with existing plan
*/
TEST_F(CmsEngineTest, testFindPlanByIdExistingPlan) {
TaskPlan plan;
plan.name = "Test Plan";
plan.type = PlanType::Centralized;
plan.status = PlanStatus::Drafting;
plan.resourceQuota = 0.8;
plan.constraints = "Test constraints";
EXPECT_TRUE(engine->createTaskPlan(plan));
ASSERT_FALSE(plan.id.empty());
const TaskPlan* found = engine->findPlanById(plan.id);
ASSERT_NE(found, nullptr);
EXPECT_EQ(found->name, "Test Plan");
EXPECT_EQ(found->type, PlanType::Centralized);
EXPECT_EQ(found->status, PlanStatus::Drafting);
EXPECT_DOUBLE_EQ(found->resourceQuota, 0.8);
EXPECT_EQ(found->constraints, "Test constraints");
}
/**
* @brief Test findPlanById with non-existent plan
*/
TEST_F(CmsEngineTest, testFindPlanByIdNonExistent) {
const TaskPlan* found = engine->findPlanById("NONEXISTENT");
EXPECT_EQ(found, nullptr);
}
/**
* @brief Test findPlanById with empty string
*/
TEST_F(CmsEngineTest, testFindPlanByIdEmptyString) {
const TaskPlan* found = engine->findPlanById("");
EXPECT_EQ(found, nullptr);
}
/**
* @brief Test findPlanById after multiple plans created
*/
TEST_F(CmsEngineTest, testFindPlanByIdMultiplePlans) {
TaskPlan plan1;
plan1.name = "Plan Alpha";
engine->createTaskPlan(plan1);
TaskPlan plan2;
plan2.name = "Plan Beta";
engine->createTaskPlan(plan2);
const TaskPlan* found1 = engine->findPlanById(plan1.id);
const TaskPlan* found2 = engine->findPlanById(plan2.id);
ASSERT_NE(found1, nullptr);
ASSERT_NE(found2, nullptr);
EXPECT_EQ(found1->name, "Plan Alpha");
EXPECT_EQ(found2->name, "Plan Beta");
}
// ============================================================================
// Tests for updatePlanStatus
// ============================================================================
/**
* @brief Test updatePlanStatus with valid plan
*/
TEST_F(CmsEngineTest, testUpdatePlanStatusValidPlan) {
TaskPlan plan;
plan.name = "Status Test";
plan.status = PlanStatus::Drafting;
engine->createTaskPlan(plan);
bool result = engine->updatePlanStatus(plan.id, PlanStatus::Approved);
EXPECT_TRUE(result);
const TaskPlan* updated = engine->findPlanById(plan.id);
ASSERT_NE(updated, nullptr);
EXPECT_EQ(updated->status, PlanStatus::Approved);
}
/**
* @brief Test updatePlanStatus with non-existent plan
*/
TEST_F(CmsEngineTest, testUpdatePlanStatusNonExistent) {
bool result = engine->updatePlanStatus("NONEXISTENT", PlanStatus::Approved);
EXPECT_FALSE(result);
}
/**
* @brief Test updatePlanStatus with empty plan ID
*/
TEST_F(CmsEngineTest, testUpdatePlanStatusEmptyId) {
bool result = engine->updatePlanStatus("", PlanStatus::Approved);
EXPECT_FALSE(result);
}
/**
* @brief Test updatePlanStatus multiple times on same plan
*/
TEST_F(CmsEngineTest, testUpdatePlanStatusMultipleUpdates) {
TaskPlan plan;
plan.name = "Multi Update";
plan.status = PlanStatus::Drafting;
engine->createTaskPlan(plan);
EXPECT_TRUE(engine->updatePlanStatus(plan.id, PlanStatus::Approved));
EXPECT_TRUE(engine->updatePlanStatus(plan.id, PlanStatus::Active));
EXPECT_TRUE(engine->updatePlanStatus(plan.id, PlanStatus::Completed));
const TaskPlan* updated = engine->findPlanById(plan.id);
ASSERT_NE(updated, nullptr);
EXPECT_EQ(updated->status, PlanStatus::Completed);
}
/**
* @brief Test updatePlanStatus does not affect other plans
*/
TEST_F(CmsEngineTest, testUpdatePlanStatusIsolation) {
TaskPlan plan1;
plan1.name = "Plan One";
plan1.status = PlanStatus::Drafting;
engine->createTaskPlan(plan1);
TaskPlan plan2;
plan2.name = "Plan Two";
plan2.status = PlanStatus::Drafting;
engine->createTaskPlan(plan2);
engine->updatePlanStatus(plan1.id, PlanStatus::Approved);
const TaskPlan* updated1 = engine->findPlanById(plan1.id);
const TaskPlan* updated2 = engine->findPlanById(plan2.id);
ASSERT_NE(updated1, nullptr);
ASSERT_NE(updated2, nullptr);
EXPECT_EQ(updated1->status, PlanStatus::Approved);
EXPECT_EQ(updated2->status, PlanStatus::Drafting);
}
// ============================================================================
// Tests for registerTemplate
// ============================================================================
/**
* @brief Test registerTemplate with valid template
*/
TEST_F(CmsEngineTest, testRegisterTemplateValid) {
TemplateInstance tmpl;
tmpl.templateId = "TMPL-001";
tmpl.confidence = 0.85;
tmpl.scenario = "combat";
engine->registerTemplate(tmpl);
const TemplateInstance* matched = engine->matchTemplate("combat");
ASSERT_NE(matched, nullptr);
EXPECT_EQ(matched->templateId, "TMPL-001");
EXPECT_DOUBLE_EQ(matched->confidence, 0.85);
}
/**
* @brief Test registerTemplate with multiple templates
*/
TEST_F(CmsEngineTest, testRegisterTemplateMultiple) {
TemplateInstance tmpl1;
tmpl1.templateId = "TMPL-A";
tmpl1.confidence = 0.7;
TemplateInstance tmpl2;
tmpl2.templateId = "TMPL-B";
tmpl2.confidence = 0.9;
engine->registerTemplate(tmpl1);
engine->registerTemplate(tmpl2);
const TemplateInstance* matched = engine->matchTemplate("any");
ASSERT_NE(matched, nullptr);
EXPECT_EQ(matched->templateId, "TMPL-B");
EXPECT_DOUBLE_EQ(matched->confidence, 0.9);
}
/**
* @brief Test registerTemplate with empty template ID
*/
TEST_F(CmsEngineTest, testRegisterTemplateEmptyId) {
TemplateInstance tmpl;
tmpl.templateId = "";
tmpl.confidence = 0.5;
engine->registerTemplate(tmpl);
const TemplateInstance* matched = engine->matchTemplate("any");
ASSERT_NE(matched, nullptr);
EXPECT_TRUE(matched->templateId.empty());
}
/**
* @brief Test registerTemplate with zero confidence
*/
TEST_F(CmsEngineTest, testRegisterTemplateZeroConfidence) {
TemplateInstance tmpl;
tmpl.templateId = "TMPL-LOW";
tmpl.confidence = 0.0;
engine->registerTemplate(tmpl);
const TemplateInstance* matched = engine->matchTemplate("any");
ASSERT_NE(matched, nullptr);
EXPECT_DOUBLE_EQ(matched->confidence, 0.0);
}
/**
* @brief Test registerTemplate with maximum confidence
*/
TEST_F(CmsEngineTest, testRegisterTemplateMaxConfidence) {
TemplateInstance tmpl;
tmpl.templateId = "TMPL-HIGH";
tmpl.confidence = 1.0;
engine->registerTemplate(tmpl);
const TemplateInstance* matched = engine->matchTemplate("any");
ASSERT_NE(matched, nullptr);
EXPECT_DOUBLE_EQ(matched->confidence, 1.0);
}
/**
* @brief Test registerTemplate does not affect existing templates
*/
TEST_F(CmsEngineTest, testRegisterTemplateNoSideEffects) {
TemplateInstance tmpl1;
tmpl1.templateId = "TMPL-FIRST";
tmpl1.confidence = 0.6;
engine->registerTemplate(tmpl1);
TemplateInstance tmpl2;
tmpl2.templateId = "TMPL-SECOND";
tmpl2.confidence = 0.8;
engine->registerTemplate(tmpl2);
const TemplateInstance* matched = engine->matchTemplate("any");
ASSERT_NE(matched, nullptr);
EXPECT_EQ(matched->templateId, "TMPL-SECOND");
}