plan_execute_t1/tests/test_app.cpp

337 lines
9.9 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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");
}