Compare commits

..

1 Commits

Author SHA1 Message Date
lids 7d5956c8a7 AI 自动生成测试用例 2026-05-14 17:12:08 +08:00
2 changed files with 404 additions and 0 deletions

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)

379
tests/test_cms_engine.cpp Normal file
View File

@ -0,0 +1,379 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "app.hpp"
#include <string>
#include <chrono>
using namespace testing;
// ============================================================================
// Test Fixture for CmsEngine
// ============================================================================
class CmsEngineTest : public ::testing::Test {
protected:
void SetUp() override {
engine = std::make_unique<CmsEngine>();
}
void TearDown() override {
engine.reset();
}
std::unique_ptr<CmsEngine> engine;
};
// ============================================================================
// Tests for CmsEngine Constructor
// ============================================================================
/**
* @brief
*
*
*/
TEST_F(CmsEngineTest, testConstructorInitializesSystemContext) {
const auto& context = engine->getSystemContext();
EXPECT_EQ(context.currentMode, RunMode::Idle);
EXPECT_EQ(context.consistencyMark, 0);
auto now = std::chrono::system_clock::now();
auto diff = std::chrono::duration_cast<std::chrono::seconds>(now - context.switchTime).count();
EXPECT_GE(diff, 0);
EXPECT_LT(diff, 5);
EXPECT_THAT(context.contextSnapshot, HasSubstr("\"mode\":\"Idle\""));
EXPECT_THAT(context.contextSnapshot, HasSubstr("\"version\":\"1.0.0\""));
}
/**
* @brief
*
*
*/
TEST_F(CmsEngineTest, testConstructorInitializesEmptyContainers) {
EXPECT_EQ(engine->getAllPlans().size(), 0);
std::string summary = engine->getSummary();
EXPECT_THAT(summary, HasSubstr("Events : 0"));
EXPECT_THAT(summary, HasSubstr("Plans : 0"));
EXPECT_THAT(summary, HasSubstr("Templates : 0"));
EXPECT_THAT(summary, HasSubstr("Monitor Nodes : 0"));
EXPECT_THAT(summary, HasSubstr("Active Sessions : 0"));
EXPECT_THAT(summary, HasSubstr("Notifications : 0"));
EXPECT_THAT(summary, HasSubstr("Mode : Idle"));
}
// ============================================================================
// Tests for ingestEvent
// ============================================================================
/**
* @brief
*
* ID
*/
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* storedEvent = engine->findEventById("EVT-001");
ASSERT_NE(storedEvent, nullptr);
EXPECT_EQ(storedEvent->id, "EVT-001");
EXPECT_EQ(storedEvent->priority, 100);
EXPECT_EQ(storedEvent->status, EventStatus::Pending);
}
/**
* @brief 0
*
* 0
*/
TEST_F(CmsEngineTest, testIngestEventMinPriority) {
EventRecord event;
event.id = "EVT-002";
event.priority = 0;
event.status = EventStatus::Pending;
bool result = engine->ingestEvent(event);
EXPECT_TRUE(result);
const EventRecord* storedEvent = engine->findEventById("EVT-002");
ASSERT_NE(storedEvent, nullptr);
EXPECT_EQ(storedEvent->priority, 0);
}
/**
* @brief 255
*
* 255
*/
TEST_F(CmsEngineTest, testIngestEventMaxPriority) {
EventRecord event;
event.id = "EVT-003";
event.priority = 255;
event.status = EventStatus::Pending;
bool result = engine->ingestEvent(event);
EXPECT_TRUE(result);
const EventRecord* storedEvent = engine->findEventById("EVT-003");
ASSERT_NE(storedEvent, nullptr);
EXPECT_EQ(storedEvent->priority, 255);
}
/**
* @brief ID
*
* ID false
*/
TEST_F(CmsEngineTest, testIngestEventEmptyId) {
EventRecord event;
event.id = "";
event.priority = 100;
event.status = EventStatus::Pending;
bool result = engine->ingestEvent(event);
EXPECT_FALSE(result);
const EventRecord* storedEvent = engine->findEventById("");
EXPECT_EQ(storedEvent, nullptr);
}
/**
* @brief
*
* 255 false
*/
TEST_F(CmsEngineTest, testIngestEventPriorityExceedsMax) {
EventRecord event;
event.id = "EVT-004";
event.priority = 256;
event.status = EventStatus::Pending;
bool result = engine->ingestEvent(event);
EXPECT_FALSE(result);
const EventRecord* storedEvent = engine->findEventById("EVT-004");
EXPECT_EQ(storedEvent, nullptr);
}
/**
* @brief
*
*
*/
TEST_F(CmsEngineTest, testIngestEventMultipleEvents) {
EventRecord event1;
event1.id = "EVT-005";
event1.priority = 50;
event1.status = EventStatus::Pending;
EventRecord event2;
event2.id = "EVT-006";
event2.priority = 150;
event2.status = EventStatus::Pending;
EXPECT_TRUE(engine->ingestEvent(event1));
EXPECT_TRUE(engine->ingestEvent(event2));
const EventRecord* storedEvent1 = engine->findEventById("EVT-005");
ASSERT_NE(storedEvent1, nullptr);
EXPECT_EQ(storedEvent1->priority, 50);
const EventRecord* storedEvent2 = engine->findEventById("EVT-006");
ASSERT_NE(storedEvent2, nullptr);
EXPECT_EQ(storedEvent2->priority, 150);
}
/**
* @brief
*
* Pending processPendingEvents
*/
TEST_F(CmsEngineTest, testIngestEventAndProcessPending) {
EventRecord event;
event.id = "EVT-007";
event.priority = 100;
event.status = EventStatus::Pending;
EXPECT_TRUE(engine->ingestEvent(event));
size_t processedCount = engine->processPendingEvents();
EXPECT_EQ(processedCount, 1);
const EventRecord* storedEvent = engine->findEventById("EVT-007");
ASSERT_NE(storedEvent, nullptr);
EXPECT_EQ(storedEvent->status, EventStatus::Generated);
// 通过 getAllPlans 获取方案列表,验证自动生成的方案
const auto& plans = engine->getAllPlans();
ASSERT_EQ(plans.size(), 1);
EXPECT_EQ(plans[0].relatedEventId, "EVT-007");
EXPECT_EQ(plans[0].name, "AutoPlan-EVT-007");
}
// ============================================================================
// Tests for findPlanById
// ============================================================================
/**
* @brief
*
* ID
*/
TEST_F(CmsEngineTest, testFindPlanByIdExistingPlan) {
TaskPlan plan;
plan.name = "TestPlan";
plan.type = PlanType::Centralized;
plan.status = PlanStatus::Drafting;
plan.resourceQuota = 0.8;
plan.constraints = "测试约束";
EXPECT_TRUE(engine->createTaskPlan(plan));
std::string planId = plan.id;
EXPECT_FALSE(planId.empty());
const TaskPlan* foundPlan = engine->findPlanById(planId);
ASSERT_NE(foundPlan, nullptr);
EXPECT_EQ(foundPlan->id, planId);
EXPECT_EQ(foundPlan->name, "TestPlan");
EXPECT_EQ(foundPlan->type, PlanType::Centralized);
EXPECT_EQ(foundPlan->status, PlanStatus::Drafting);
EXPECT_DOUBLE_EQ(foundPlan->resourceQuota, 0.8);
EXPECT_EQ(foundPlan->constraints, "测试约束");
}
/**
* @brief
*
* ID nullptr
*/
TEST_F(CmsEngineTest, testFindPlanByIdNonExistentPlan) {
const TaskPlan* foundPlan = engine->findPlanById("NONEXISTENT");
EXPECT_EQ(foundPlan, nullptr);
}
/**
* @brief ID
*
* ID nullptr
*/
TEST_F(CmsEngineTest, testFindPlanByIdEmptyId) {
const TaskPlan* foundPlan = engine->findPlanById("");
EXPECT_EQ(foundPlan, nullptr);
}
/**
* @brief
*
* ID
*/
TEST_F(CmsEngineTest, testFindPlanByIdAmongMultiplePlans) {
TaskPlan plan1;
plan1.name = "PlanAlpha";
EXPECT_TRUE(engine->createTaskPlan(plan1));
std::string id1 = plan1.id;
TaskPlan plan2;
plan2.name = "PlanBeta";
EXPECT_TRUE(engine->createTaskPlan(plan2));
std::string id2 = plan2.id;
TaskPlan plan3;
plan3.name = "PlanGamma";
EXPECT_TRUE(engine->createTaskPlan(plan3));
std::string id3 = plan3.id;
EXPECT_NE(id1, id2);
EXPECT_NE(id2, id3);
EXPECT_NE(id1, id3);
const TaskPlan* foundPlan = engine->findPlanById(id2);
ASSERT_NE(foundPlan, nullptr);
EXPECT_EQ(foundPlan->id, id2);
EXPECT_EQ(foundPlan->name, "PlanBeta");
foundPlan = engine->findPlanById(id1);
ASSERT_NE(foundPlan, nullptr);
EXPECT_EQ(foundPlan->id, id1);
EXPECT_EQ(foundPlan->name, "PlanAlpha");
foundPlan = engine->findPlanById(id3);
ASSERT_NE(foundPlan, nullptr);
EXPECT_EQ(foundPlan->id, id3);
EXPECT_EQ(foundPlan->name, "PlanGamma");
}
/**
* @brief
*
*
*/
TEST_F(CmsEngineTest, testFindPlanByIdAfterStatusUpdate) {
TaskPlan plan;
plan.name = "StatusTestPlan";
EXPECT_TRUE(engine->createTaskPlan(plan));
std::string planId = plan.id;
EXPECT_TRUE(engine->updatePlanStatus(planId, PlanStatus::Approved));
const TaskPlan* foundPlan = engine->findPlanById(planId);
ASSERT_NE(foundPlan, nullptr);
EXPECT_EQ(foundPlan->status, PlanStatus::Approved);
}
/**
* @brief
*
* processPendingEvents
*/
TEST_F(CmsEngineTest, testFindPlanByIdGeneratedFromEvent) {
EventRecord event;
event.id = "EVT-GEN-001";
event.priority = 100;
event.status = EventStatus::Pending;
EXPECT_TRUE(engine->ingestEvent(event));
size_t processedCount = engine->processPendingEvents();
EXPECT_EQ(processedCount, 1);
// 通过 getAllPlans 获取方案列表,验证自动生成的方案
const auto& plans = engine->getAllPlans();
ASSERT_EQ(plans.size(), 1);
std::string planId = plans[0].id;
EXPECT_FALSE(planId.empty());
const TaskPlan* foundPlan = engine->findPlanById(planId);
ASSERT_NE(foundPlan, nullptr);
EXPECT_EQ(foundPlan->relatedEventId, "EVT-GEN-001");
EXPECT_EQ(foundPlan->name, "AutoPlan-EVT-GEN-001");
EXPECT_EQ(foundPlan->type, PlanType::Centralized);
EXPECT_EQ(foundPlan->status, PlanStatus::Drafting);
EXPECT_DOUBLE_EQ(foundPlan->resourceQuota, 0.5);
EXPECT_EQ(foundPlan->constraints, "自动生成约束");
}
/**
* @brief
*
* const findPlanById
*/
TEST_F(CmsEngineTest, testFindPlanByIdConstCorrectness) {
TaskPlan plan;
plan.name = "ConstTestPlan";
EXPECT_TRUE(engine->createTaskPlan(plan));
std::string planId = plan.id;
const CmsEngine& constEngine = *engine;
const TaskPlan* foundPlan = constEngine.findPlanById(planId);
ASSERT_NE(foundPlan, nullptr);
EXPECT_EQ(foundPlan->id, planId);
EXPECT_EQ(foundPlan->name, "ConstTestPlan");
}