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 379 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,316 +0,0 @@
#include <gtest/gtest.h>
#include <string>
#include <vector>
#include "app.hpp"
// ============================================================================
// Test Fixture for CmsEngine
// ============================================================================
class CmsEngineTest : public ::testing::Test {
protected:
CmsEngine engine;
void SetUp() override {
// Initialize with some plans for findPlanById and updatePlanStatus tests
TaskPlan plan1;
plan1.name = "Plan1";
engine.createTaskPlan(plan1);
TaskPlan plan2;
plan2.name = "Plan2";
engine.createTaskPlan(plan2);
// Register a template for registerTemplate tests
TemplateInstance tmpl;
tmpl.templateId = "TMPL-001";
tmpl.confidence = 0.8;
engine.registerTemplate(tmpl);
}
// Helper to get the first plan's ID
std::string getFirstPlanId() const {
const auto& plans = engine.getAllPlans();
if (!plans.empty()) {
return plans[0].id;
}
return "";
}
// Helper to get the second plan's ID
std::string getSecondPlanId() const {
const auto& plans = engine.getAllPlans();
if (plans.size() > 1) {
return plans[1].id;
}
return "";
}
};
// ============================================================================
// Tests for findPlanById
// ============================================================================
/**
* @test -
* @description 使 planId
*/
TEST_F(CmsEngineTest, testFindPlanByIdExistingPlan) {
std::string planId = getFirstPlanId();
ASSERT_FALSE(planId.empty()) << "Plan ID should not be empty";
const TaskPlan* result = engine.findPlanById(planId);
ASSERT_NE(result, nullptr) << "Should find the existing plan";
EXPECT_EQ(result->id, planId) << "Plan ID should match";
EXPECT_EQ(result->name, "Plan1") << "Plan name should match";
}
/**
* @test -
* @description 使 planId nullptr
*/
TEST_F(CmsEngineTest, testFindPlanByIdNonExistingPlan) {
const std::string nonExistingId = "ID-99999";
const TaskPlan* result = engine.findPlanById(nonExistingId);
EXPECT_EQ(result, nullptr) << "Should return nullptr for non-existing plan";
}
/**
* @test - ID
* @description 使 planId nullptr
*/
TEST_F(CmsEngineTest, testFindPlanByIdEmptyId) {
const std::string emptyId = "";
const TaskPlan* result = engine.findPlanById(emptyId);
EXPECT_EQ(result, nullptr) << "Should return nullptr for empty ID";
}
/**
* @test -
* @description
*/
TEST_F(CmsEngineTest, testFindPlanByIdSecondPlan) {
std::string planId = getSecondPlanId();
ASSERT_FALSE(planId.empty()) << "Second plan ID should not be empty";
const TaskPlan* result = engine.findPlanById(planId);
ASSERT_NE(result, nullptr) << "Should find the second plan";
EXPECT_EQ(result->id, planId) << "Plan ID should match";
EXPECT_EQ(result->name, "Plan2") << "Plan name should match";
}
/**
* @test - ID
* @description 使 planId nullptr
*/
TEST_F(CmsEngineTest, testFindPlanByIdSpecialChars) {
const std::string specialId = "!@#$%^&*()";
const TaskPlan* result = engine.findPlanById(specialId);
EXPECT_EQ(result, nullptr) << "Should return nullptr for special character ID";
}
// ============================================================================
// Tests for updatePlanStatus
// ============================================================================
/**
* @test -
* @description 使 planId true
*/
TEST_F(CmsEngineTest, testUpdatePlanStatusSuccess) {
std::string planId = getFirstPlanId();
ASSERT_FALSE(planId.empty()) << "Plan ID should not be empty";
PlanStatus newStatus = PlanStatus::Approved;
bool result = engine.updatePlanStatus(planId, newStatus);
EXPECT_TRUE(result) << "Should return true for successful update";
// Verify the status was updated
const TaskPlan* updatedPlan = engine.findPlanById(planId);
ASSERT_NE(updatedPlan, nullptr) << "Plan should still exist";
EXPECT_EQ(updatedPlan->status, newStatus) << "Plan status should be updated";
}
/**
* @test -
* @description 使 planId false
*/
TEST_F(CmsEngineTest, testUpdatePlanStatusNonExistingPlan) {
const std::string nonExistingId = "ID-99999";
bool result = engine.updatePlanStatus(nonExistingId, PlanStatus::Approved);
EXPECT_FALSE(result) << "Should return false for non-existing plan";
}
/**
* @test - ID
* @description 使 planId false
*/
TEST_F(CmsEngineTest, testUpdatePlanStatusEmptyId) {
const std::string emptyId = "";
bool result = engine.updatePlanStatus(emptyId, PlanStatus::Approved);
EXPECT_FALSE(result) << "Should return false for empty ID";
}
/**
* @test - Drafting
* @description Drafting true
*/
TEST_F(CmsEngineTest, testUpdatePlanStatusToDrafting) {
std::string planId = getFirstPlanId();
ASSERT_FALSE(planId.empty()) << "Plan ID should not be empty";
bool result = engine.updatePlanStatus(planId, PlanStatus::Drafting);
EXPECT_TRUE(result) << "Should return true for successful update";
const TaskPlan* updatedPlan = engine.findPlanById(planId);
ASSERT_NE(updatedPlan, nullptr) << "Plan should still exist";
EXPECT_EQ(updatedPlan->status, PlanStatus::Drafting) << "Plan status should be Drafting";
}
/**
* @test -
* @description true
*/
TEST_F(CmsEngineTest, testUpdatePlanStatusMultipleTimes) {
std::string planId = getFirstPlanId();
ASSERT_FALSE(planId.empty()) << "Plan ID should not be empty";
// First update
bool result1 = engine.updatePlanStatus(planId, PlanStatus::Approved);
EXPECT_TRUE(result1) << "First update should succeed";
// Second update
bool result2 = engine.updatePlanStatus(planId, PlanStatus::Executing);
EXPECT_TRUE(result2) << "Second update should succeed";
// Third update
bool result3 = engine.updatePlanStatus(planId, PlanStatus::Completed);
EXPECT_TRUE(result3) << "Third update should succeed";
// Verify final status
const TaskPlan* updatedPlan = engine.findPlanById(planId);
ASSERT_NE(updatedPlan, nullptr) << "Plan should still exist";
EXPECT_EQ(updatedPlan->status, PlanStatus::Completed) << "Plan status should be Completed";
}
// ============================================================================
// Tests for registerTemplate
// ============================================================================
/**
* @test -
* @description
*/
TEST_F(CmsEngineTest, testRegisterTemplateSuccess) {
TemplateInstance tmpl;
tmpl.templateId = "TMPL-002";
tmpl.confidence = 0.9;
engine.registerTemplate(tmpl);
// Verify the template was registered by matching
const TemplateInstance* matched = engine.matchTemplate("test_scenario");
ASSERT_NE(matched, nullptr) << "Should find a template after registration";
EXPECT_EQ(matched->templateId, "TMPL-002") << "Should match the newly registered template";
EXPECT_EQ(matched->confidence, 0.9) << "Confidence should match";
}
/**
* @test - ID
* @description templateId ID
*/
TEST_F(CmsEngineTest, testRegisterTemplateEmptyId) {
TemplateInstance tmpl;
tmpl.templateId = "";
tmpl.confidence = 0.5;
engine.registerTemplate(tmpl);
// Verify the template was registered
const TemplateInstance* matched = engine.matchTemplate("test_scenario");
ASSERT_NE(matched, nullptr) << "Should find a template after registration";
EXPECT_EQ(matched->templateId, "") << "Template ID should be empty";
}
/**
* @test -
* @description 0
*/
TEST_F(CmsEngineTest, testRegisterTemplateZeroConfidence) {
TemplateInstance tmpl;
tmpl.templateId = "TMPL-003";
tmpl.confidence = 0.0;
engine.registerTemplate(tmpl);
// Verify the template was registered
const TemplateInstance* matched = engine.matchTemplate("test_scenario");
ASSERT_NE(matched, nullptr) << "Should find a template after registration";
EXPECT_EQ(matched->confidence, 0.0) << "Confidence should be 0.0";
}
/**
* @test -
* @description
*/
TEST_F(CmsEngineTest, testRegisterTemplateMultiple) {
TemplateInstance tmpl1;
tmpl1.templateId = "TMPL-004";
tmpl1.confidence = 0.7;
TemplateInstance tmpl2;
tmpl2.templateId = "TMPL-005";
tmpl2.confidence = 0.9;
TemplateInstance tmpl3;
tmpl3.templateId = "TMPL-006";
tmpl3.confidence = 0.5;
engine.registerTemplate(tmpl1);
engine.registerTemplate(tmpl2);
engine.registerTemplate(tmpl3);
// Verify the best template is the one with highest confidence
const TemplateInstance* matched = engine.matchTemplate("test_scenario");
ASSERT_NE(matched, nullptr) << "Should find a template after registration";
EXPECT_EQ(matched->templateId, "TMPL-005") << "Should match the template with highest confidence";
EXPECT_EQ(matched->confidence, 0.9) << "Confidence should be 0.9";
}
/**
* @test - ID
* @description templateId
*/
TEST_F(CmsEngineTest, testRegisterTemplateDuplicateId) {
TemplateInstance tmpl1;
tmpl1.templateId = "TMPL-007";
tmpl1.confidence = 0.6;
TemplateInstance tmpl2;
tmpl2.templateId = "TMPL-007";
tmpl2.confidence = 0.8;
engine.registerTemplate(tmpl1);
engine.registerTemplate(tmpl2);
// Verify both templates are registered and the one with higher confidence is matched
const TemplateInstance* matched = engine.matchTemplate("test_scenario");
ASSERT_NE(matched, nullptr) << "Should find a template after registration";
EXPECT_EQ(matched->confidence, 0.8) << "Should match the template with higher confidence";
}
/**
* @test -
* @description
*/
TEST_F(CmsEngineTest, testRegisterTemplateAndMatchImmediately) {
TemplateInstance tmpl;
tmpl.templateId = "TMPL-008";
tmpl.confidence = 1.0;
engine.registerTemplate(tmpl);
const TemplateInstance* matched = engine.matchTemplate("any_scenario");
ASSERT_NE(matched, nullptr) << "Should find a template immediately after registration";
EXPECT_EQ(matched->templateId, "TMPL-008") << "Should match the just registered template";
EXPECT_EQ(matched->confidence, 1.0) << "Confidence should be 1.0";
}