AI 自动生成测试用例
This commit is contained in:
parent
9ceeaf09c6
commit
31f60e9c8d
|
|
@ -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 14)
|
||||||
|
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)
|
||||||
|
|
@ -0,0 +1,271 @@
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include "app.hpp"
|
||||||
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
class CmsEngineTest : public ::testing::Test {
|
||||||
|
protected:
|
||||||
|
void SetUp() override {
|
||||||
|
engine_ = std::make_unique<CmsEngine>();
|
||||||
|
}
|
||||||
|
std::unique_ptr<CmsEngine> engine_;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 1. CmsEngine 构造函数测试
|
||||||
|
TEST_F(CmsEngineTest, testCmsEngineConstructorNormal) {
|
||||||
|
const auto& ctx = engine_->getSystemContext();
|
||||||
|
EXPECT_EQ(ctx.currentMode, RunMode::Idle);
|
||||||
|
EXPECT_EQ(ctx.consistencyMark, 0);
|
||||||
|
EXPECT_FALSE(ctx.contextSnapshot.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. generateNextId 自增ID生成测试
|
||||||
|
TEST_F(CmsEngineTest, testGenerateNextIdFormatAndIncrement) {
|
||||||
|
TaskPlan p1, p2;
|
||||||
|
p1.name = "PlanA";
|
||||||
|
p2.name = "PlanB";
|
||||||
|
EXPECT_TRUE(engine_->createTaskPlan(p1));
|
||||||
|
EXPECT_TRUE(engine_->createTaskPlan(p2));
|
||||||
|
EXPECT_EQ(p1.id, "ID-00001");
|
||||||
|
EXPECT_EQ(p2.id, "ID-00002");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. ingestEvent 正常事件接入测试
|
||||||
|
TEST_F(CmsEngineTest, testIngestEventNormal) {
|
||||||
|
EventRecord evt;
|
||||||
|
evt.id = "EVT-01";
|
||||||
|
evt.priority = 100;
|
||||||
|
evt.status = EventStatus::Pending;
|
||||||
|
EXPECT_TRUE(engine_->ingestEvent(evt));
|
||||||
|
EXPECT_NE(engine_->findEventById("EVT-01"), nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. ingestEvent 空ID异常测试
|
||||||
|
TEST_F(CmsEngineTest, testIngestEventEmptyId) {
|
||||||
|
EventRecord evt;
|
||||||
|
evt.id = "";
|
||||||
|
evt.priority = 100;
|
||||||
|
evt.status = EventStatus::Pending;
|
||||||
|
EXPECT_FALSE(engine_->ingestEvent(evt));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. ingestEvent 优先级越界边界测试
|
||||||
|
TEST_F(CmsEngineTest, testIngestEventHighPriority) {
|
||||||
|
EventRecord evt;
|
||||||
|
evt.id = "EVT-02";
|
||||||
|
evt.priority = 300;
|
||||||
|
evt.status = EventStatus::Pending;
|
||||||
|
EXPECT_FALSE(engine_->ingestEvent(evt));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6. processPendingEvents 批量处理测试
|
||||||
|
TEST_F(CmsEngineTest, testProcessPendingEventsNormal) {
|
||||||
|
EventRecord evt1, evt2;
|
||||||
|
evt1.id = "EVT-10"; evt1.priority = 50; evt1.status = EventStatus::Pending;
|
||||||
|
evt2.id = "EVT-11"; evt2.priority = 60; evt2.status = EventStatus::Generated;
|
||||||
|
engine_->ingestEvent(evt1);
|
||||||
|
engine_->ingestEvent(evt2);
|
||||||
|
size_t count = engine_->processPendingEvents();
|
||||||
|
EXPECT_EQ(count, 1);
|
||||||
|
const auto* found = engine_->findEventById("EVT-10");
|
||||||
|
ASSERT_NE(found, nullptr);
|
||||||
|
EXPECT_EQ(found->status, EventStatus::Generated);
|
||||||
|
EXPECT_EQ(engine_->getAllPlans().size(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7. processPendingEvents 空队列边界测试
|
||||||
|
TEST_F(CmsEngineTest, testProcessPendingEventsEmpty) {
|
||||||
|
EXPECT_EQ(engine_->processPendingEvents(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 8. findEventById 查找成功测试
|
||||||
|
TEST_F(CmsEngineTest, testFindEventByIdNormal) {
|
||||||
|
EventRecord evt;
|
||||||
|
evt.id = "EVT-20"; evt.priority = 10; evt.status = EventStatus::Pending;
|
||||||
|
engine_->ingestEvent(evt);
|
||||||
|
const auto* found = engine_->findEventById("EVT-20");
|
||||||
|
ASSERT_NE(found, nullptr);
|
||||||
|
EXPECT_EQ(found->id, "EVT-20");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 9. findEventById 查找失败异常测试
|
||||||
|
TEST_F(CmsEngineTest, testFindEventByIdNotFound) {
|
||||||
|
EXPECT_EQ(engine_->findEventById("NON_EXIST"), nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 10. createTaskPlan 正常创建测试
|
||||||
|
TEST_F(CmsEngineTest, testCreateTaskPlanNormal) {
|
||||||
|
TaskPlan plan;
|
||||||
|
plan.name = "ManualPlan";
|
||||||
|
EXPECT_TRUE(engine_->createTaskPlan(plan));
|
||||||
|
EXPECT_FALSE(plan.id.empty());
|
||||||
|
EXPECT_EQ(engine_->getAllPlans().size(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 11. createTaskPlan 空名拒绝异常测试
|
||||||
|
TEST_F(CmsEngineTest, testCreateTaskPlanEmptyName) {
|
||||||
|
TaskPlan plan;
|
||||||
|
plan.name = "";
|
||||||
|
EXPECT_FALSE(engine_->createTaskPlan(plan));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 12. getAllPlans 获取列表测试
|
||||||
|
TEST_F(CmsEngineTest, testGetAllPlansNormal) {
|
||||||
|
TaskPlan p; p.name = "P1"; engine_->createTaskPlan(p);
|
||||||
|
const auto& plans = engine_->getAllPlans();
|
||||||
|
EXPECT_EQ(plans.size(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 13. findPlanById 查找成功测试
|
||||||
|
TEST_F(CmsEngineTest, testFindPlanByIdNormal) {
|
||||||
|
TaskPlan p; p.name = "P2";
|
||||||
|
engine_->createTaskPlan(p);
|
||||||
|
const auto* found = engine_->findPlanById(p.id);
|
||||||
|
ASSERT_NE(found, nullptr);
|
||||||
|
EXPECT_EQ(found->name, "P2");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 14. findPlanById 查找失败异常测试
|
||||||
|
TEST_F(CmsEngineTest, testFindPlanByIdNotFound) {
|
||||||
|
EXPECT_EQ(engine_->findPlanById("FAKE_ID"), nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 15. updatePlanStatus 状态更新测试
|
||||||
|
TEST_F(CmsEngineTest, testUpdatePlanStatusNormal) {
|
||||||
|
TaskPlan p; p.name = "P3";
|
||||||
|
engine_->createTaskPlan(p);
|
||||||
|
EXPECT_TRUE(engine_->updatePlanStatus(p.id, PlanStatus::Drafting));
|
||||||
|
const auto* updated = engine_->findPlanById(p.id);
|
||||||
|
ASSERT_NE(updated, nullptr);
|
||||||
|
EXPECT_EQ(updated->status, PlanStatus::Drafting);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 16. updatePlanStatus 未找到异常测试
|
||||||
|
TEST_F(CmsEngineTest, testUpdatePlanStatusNotFound) {
|
||||||
|
EXPECT_FALSE(engine_->updatePlanStatus("NO_SUCH_PLAN", PlanStatus::Drafting));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 17. registerTemplate 注册测试
|
||||||
|
TEST_F(CmsEngineTest, testRegisterTemplateNormal) {
|
||||||
|
TemplateInstance tmpl;
|
||||||
|
tmpl.id = "T1";
|
||||||
|
tmpl.confidence = 0.8;
|
||||||
|
engine_->registerTemplate(tmpl);
|
||||||
|
EXPECT_NE(engine_->matchTemplate("any"), nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 18. matchTemplate 最佳匹配测试
|
||||||
|
TEST_F(CmsEngineTest, testMatchTemplateBestConfidence) {
|
||||||
|
TemplateInstance t1, t2;
|
||||||
|
t1.id = "T1"; t1.confidence = 0.5;
|
||||||
|
t2.id = "T2"; t2.confidence = 0.9;
|
||||||
|
engine_->registerTemplate(t1);
|
||||||
|
engine_->registerTemplate(t2);
|
||||||
|
const auto* best = engine_->matchTemplate("scenario");
|
||||||
|
ASSERT_NE(best, nullptr);
|
||||||
|
EXPECT_EQ(best->id, "T2");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 19. matchTemplate 空库边界测试
|
||||||
|
TEST_F(CmsEngineTest, testMatchTemplateEmpty) {
|
||||||
|
EXPECT_EQ(engine_->matchTemplate("scenario"), nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 20. reportExecutionStatus 上报测试
|
||||||
|
TEST_F(CmsEngineTest, testReportExecutionStatusNormal) {
|
||||||
|
ExecutionStatus st;
|
||||||
|
st.nodeId = "Node1";
|
||||||
|
st.healthIndex = 0.9;
|
||||||
|
st.errorCode = 0;
|
||||||
|
st.lastReport = std::chrono::system_clock::now();
|
||||||
|
engine_->reportExecutionStatus(st);
|
||||||
|
auto unhealthy = engine_->checkHealth();
|
||||||
|
EXPECT_TRUE(unhealthy.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 21. checkHealth 异常节点捕获测试
|
||||||
|
TEST_F(CmsEngineTest, testCheckHealthUnhealthy) {
|
||||||
|
ExecutionStatus st;
|
||||||
|
st.nodeId = "NodeBad";
|
||||||
|
st.healthIndex = 0.3;
|
||||||
|
st.errorCode = 1;
|
||||||
|
st.lastReport = std::chrono::system_clock::now();
|
||||||
|
engine_->reportExecutionStatus(st);
|
||||||
|
auto unhealthy = engine_->checkHealth();
|
||||||
|
EXPECT_EQ(unhealthy.size(), 1);
|
||||||
|
EXPECT_EQ(unhealthy[0].nodeId, "NodeBad");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 22. createSession 正常创建测试
|
||||||
|
TEST_F(CmsEngineTest, testCreateSessionNormal) {
|
||||||
|
UserSession sess;
|
||||||
|
sess.userId = "User1";
|
||||||
|
sess.permissionBitmap = 0xFF;
|
||||||
|
EXPECT_TRUE(engine_->createSession(sess));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 23. createSession 重复ID异常测试
|
||||||
|
TEST_F(CmsEngineTest, testCreateSessionDuplicate) {
|
||||||
|
UserSession s;
|
||||||
|
s.userId = "User2";
|
||||||
|
s.permissionBitmap = 0xFF;
|
||||||
|
EXPECT_TRUE(engine_->createSession(s));
|
||||||
|
EXPECT_FALSE(engine_->createSession(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 24. checkPermission 权限校验测试
|
||||||
|
TEST_F(CmsEngineTest, testCheckPermissionNormal) {
|
||||||
|
UserSession sess;
|
||||||
|
sess.userId = "Admin";
|
||||||
|
sess.permissionBitmap = 0x0F;
|
||||||
|
engine_->createSession(sess);
|
||||||
|
EXPECT_TRUE(engine_->checkPermission("Admin", 0x03));
|
||||||
|
EXPECT_FALSE(engine_->checkPermission("Admin", 0x10));
|
||||||
|
EXPECT_FALSE(engine_->checkPermission("Ghost", 0x01));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 25. pushNotification 推送测试
|
||||||
|
TEST_F(CmsEngineTest, testPushNotificationNormal) {
|
||||||
|
NotificationMessage msg;
|
||||||
|
msg.isRead = false;
|
||||||
|
engine_->pushNotification(msg);
|
||||||
|
EXPECT_EQ(engine_->getUnreadNotifications().size(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 26. getUnreadNotifications 过滤已读测试
|
||||||
|
TEST_F(CmsEngineTest, testGetUnreadNotificationsFilter) {
|
||||||
|
NotificationMessage msg1, msg2;
|
||||||
|
msg1.isRead = false;
|
||||||
|
msg2.isRead = true;
|
||||||
|
engine_->pushNotification(msg1);
|
||||||
|
engine_->pushNotification(msg2);
|
||||||
|
auto unread = engine_->getUnreadNotifications();
|
||||||
|
EXPECT_EQ(unread.size(), 1);
|
||||||
|
EXPECT_FALSE(unread[0].isRead);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 27. switchMode 正常切换测试
|
||||||
|
TEST_F(CmsEngineTest, testSwitchModeNormal) {
|
||||||
|
EXPECT_TRUE(engine_->switchMode(RunMode::AutoExec));
|
||||||
|
EXPECT_EQ(engine_->getSystemContext().currentMode, RunMode::AutoExec);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 28. switchMode 降级限制特殊场景测试
|
||||||
|
TEST_F(CmsEngineTest, testSwitchModeDegradedRestriction) {
|
||||||
|
engine_->switchMode(RunMode::Degraded);
|
||||||
|
EXPECT_FALSE(engine_->switchMode(RunMode::Idle));
|
||||||
|
EXPECT_TRUE(engine_->switchMode(RunMode::Degraded));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 29. getSystemContext 获取上下文测试
|
||||||
|
TEST_F(CmsEngineTest, testGetSystemContextNormal) {
|
||||||
|
const auto& ctx = engine_->getSystemContext();
|
||||||
|
EXPECT_EQ(ctx.currentMode, RunMode::Idle);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 30. getSummary 摘要生成测试
|
||||||
|
TEST_F(CmsEngineTest, testGetSummaryNormal) {
|
||||||
|
std::string summary = engine_->getSummary();
|
||||||
|
EXPECT_TRUE(summary.find("Events : 0") != std::string::npos);
|
||||||
|
EXPECT_TRUE(summary.find("Mode : Idle") != std::string::npos);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue