plan_execute_t1/tests/test_app.cpp

163 lines
5.6 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 "app.hpp"
// 测试 getUnreadNotifications
TEST(CmsEngineTest, GetUnreadNotifications_NoNotifications) {
CmsEngine engine;
auto unread = engine.getUnreadNotifications();
EXPECT_TRUE(unread.empty());
}
TEST(CmsEngineTest, GetUnreadNotifications_AllRead) {
CmsEngine engine;
NotificationMessage msg1, msg2;
msg1.isRead = true;
msg2.isRead = true;
engine.pushNotification(msg1);
engine.pushNotification(msg2);
auto unread = engine.getUnreadNotifications();
EXPECT_TRUE(unread.empty());
}
TEST(CmsEngineTest, GetUnreadNotifications_AllUnread) {
CmsEngine engine;
NotificationMessage msg1, msg2;
msg1.isRead = false;
msg2.isRead = false;
engine.pushNotification(msg1);
engine.pushNotification(msg2);
auto unread = engine.getUnreadNotifications();
EXPECT_EQ(unread.size(), 2);
}
TEST(CmsEngineTest, GetUnreadNotifications_Mixed) {
CmsEngine engine;
NotificationMessage msg1, msg2, msg3;
msg1.isRead = true;
msg2.isRead = false;
msg3.isRead = false;
engine.pushNotification(msg1);
engine.pushNotification(msg2);
engine.pushNotification(msg3);
auto unread = engine.getUnreadNotifications();
EXPECT_EQ(unread.size(), 2);
// 验证返回的是未读消息
for (const auto& msg : unread) {
EXPECT_FALSE(msg.isRead);
}
}
// 测试 switchMode
TEST(CmsEngineTest, SwitchMode_FromIdleToHumanLoop) {
CmsEngine engine;
EXPECT_TRUE(engine.switchMode(RunMode::HumanLoop));
const auto& ctx = engine.getSystemContext();
EXPECT_EQ(ctx.currentMode, RunMode::HumanLoop);
}
TEST(CmsEngineTest, SwitchMode_FromIdleToAutoExec) {
CmsEngine engine;
EXPECT_TRUE(engine.switchMode(RunMode::AutoExec));
EXPECT_EQ(engine.getSystemContext().currentMode, RunMode::AutoExec);
}
TEST(CmsEngineTest, SwitchMode_FromDegradedToOther) {
CmsEngine engine;
// 先切换到 Degraded
engine.switchMode(RunMode::Degraded);
// 尝试切换到其他模式,应失败
EXPECT_FALSE(engine.switchMode(RunMode::Idle));
EXPECT_EQ(engine.getSystemContext().currentMode, RunMode::Degraded);
}
TEST(CmsEngineTest, SwitchMode_FromDegradedToDegraded) {
CmsEngine engine;
engine.switchMode(RunMode::Degraded);
// 切换到自身,应成功(因为 newMode == Degraded
EXPECT_TRUE(engine.switchMode(RunMode::Degraded));
EXPECT_EQ(engine.getSystemContext().currentMode, RunMode::Degraded);
}
TEST(CmsEngineTest, SwitchMode_FromHumanLoopToDegraded) {
CmsEngine engine;
engine.switchMode(RunMode::HumanLoop);
EXPECT_TRUE(engine.switchMode(RunMode::Degraded));
EXPECT_EQ(engine.getSystemContext().currentMode, RunMode::Degraded);
}
// 测试 getSystemContext
TEST(CmsEngineTest, GetSystemContext_InitialState) {
CmsEngine engine;
const auto& ctx = engine.getSystemContext();
EXPECT_EQ(ctx.currentMode, RunMode::Idle);
EXPECT_EQ(ctx.consistencyMark, 0);
// switchTime 应接近当前时间,但无法精确断言
EXPECT_FALSE(ctx.contextSnapshot.empty());
}
TEST(CmsEngineTest, GetSystemContext_AfterSwitch) {
CmsEngine engine;
engine.switchMode(RunMode::AutoExec);
const auto& ctx = engine.getSystemContext();
EXPECT_EQ(ctx.currentMode, RunMode::AutoExec);
// consistencyMark 应为 nextId_ 的值初始0但 switchMode 会赋值为 nextId_nextId_ 初始0但 generateNextId 会递增,但 switchMode 中 consistencyMark = nextId_此时 nextId_ 仍为0
// 注意switchMode 中 consistencyMark = nextId_而 nextId_ 初始为0所以 consistencyMark 应为0
EXPECT_EQ(ctx.consistencyMark, 0);
}
// 测试 getSummary
TEST(CmsEngineTest, GetSummary_Initial) {
CmsEngine engine;
std::string summary = engine.getSummary();
EXPECT_NE(summary.find("Events : 0"), std::string::npos);
EXPECT_NE(summary.find("Plans : 0"), std::string::npos);
EXPECT_NE(summary.find("Templates : 0"), std::string::npos);
EXPECT_NE(summary.find("Monitor Nodes : 0"), std::string::npos);
EXPECT_NE(summary.find("Active Sessions : 0"), std::string::npos);
EXPECT_NE(summary.find("Notifications : 0"), std::string::npos);
EXPECT_NE(summary.find("Mode : Idle"), std::string::npos);
}
TEST(CmsEngineTest, GetSummary_AfterOperations) {
CmsEngine engine;
// 添加事件
EventRecord evt;
evt.id = "evt1";
evt.priority = 10;
engine.ingestEvent(evt);
// 添加方案
TaskPlan plan;
plan.name = "plan1";
engine.createTaskPlan(plan);
// 添加模板
TemplateInstance tmpl;
tmpl.confidence = 0.8;
engine.registerTemplate(tmpl);
// 添加节点状态
ExecutionStatus status;
status.nodeId = "node1";
status.healthIndex = 1.0;
status.errorCode = 0;
status.lastReport = std::chrono::system_clock::now();
engine.reportExecutionStatus(status);
// 添加会话
UserSession session;
session.userId = "user1";
engine.createSession(session);
// 添加通知
NotificationMessage msg;
msg.isRead = false;
engine.pushNotification(msg);
// 切换模式
engine.switchMode(RunMode::AutoExec);
std::string summary = engine.getSummary();
EXPECT_NE(summary.find("Events : 1"), std::string::npos);
EXPECT_NE(summary.find("Plans : 1"), std::string::npos);
EXPECT_NE(summary.find("Templates : 1"), std::string::npos);
EXPECT_NE(summary.find("Monitor Nodes : 1"), std::string::npos);
EXPECT_NE(summary.find("Active Sessions : 1"), std::string::npos);
EXPECT_NE(summary.find("Notifications : 1"), std::string::npos);
EXPECT_NE(summary.find("Mode : AutoExec"), std::string::npos);
}