first commit

This commit is contained in:
2026-08-10 10:52:16 +07:00
commit 8e2a4182c5
20 changed files with 3413 additions and 0 deletions

View File

@@ -0,0 +1,136 @@
/*********************************************************************
*
* Software License Agreement (BSD License)
*
* Kiểm handler `wait` — handler default duy nhất lấy tham số từ CHÍNH action, không từ YAML.
*
* Trọng tâm là các đường từ chối: một `duration` hỏng phải làm action thất bại có lý do, chứ không
* được biến thành một khoảng chờ đoán bừa mà không ai giải thích được vì sao robot đứng im.
*
* Author: DuongTD
*********************************************************************/
#include <gtest/gtest.h>
#include <cstdlib>
#include <string>
#include <robot/node_handle.h>
#include <action_core/action_registry.h>
namespace
{
using action_core::ActionHandler;
using action_core::ActionRegistry;
using action_core::ActionStatus;
robot_protocol_msgs::Action makeWait(const std::string& duration_value)
{
robot_protocol_msgs::Action action;
action.actionType = "wait";
action.actionId = "wait-1";
if (!duration_value.empty())
{
robot_protocol_msgs::ActionParameter parameter;
parameter.key = "duration";
parameter.value = duration_value;
action.actionParameters.push_back(parameter);
}
return action;
}
class WaitRig
{
public:
WaitRig()
{
loaded_ = registry_.loadFromConfig(nh_, "actions_wait");
handler_ = registry_.find("wait");
}
bool loaded_ = false;
robot::NodeHandle nh_;
ActionRegistry registry_;
ActionHandler* handler_ = nullptr;
};
TEST(WaitActionHandler, TakesDurationFromTheActionItself)
{
WaitRig rig;
ASSERT_TRUE(rig.loaded_);
ASSERT_NE(rig.handler_, nullptr);
const robot::Time now = robot::Time::now();
ASSERT_TRUE(rig.handler_->start(makeWait("2.0"), now));
EXPECT_EQ(rig.handler_->update(now).status, ActionStatus::kRunning);
EXPECT_EQ(rig.handler_->update(now + robot::Duration(1.9)).status, ActionStatus::kRunning);
EXPECT_EQ(rig.handler_->update(now + robot::Duration(2.0)).status, ActionStatus::kSucceeded);
}
TEST(WaitActionHandler, FallsBackToDefaultWhenTheActionCarriesNoDuration)
{
WaitRig rig;
ASSERT_NE(rig.handler_, nullptr);
// default_duration = 0 trong config test -> xong ngay ở tick đầu.
const robot::Time now = robot::Time::now();
ASSERT_TRUE(rig.handler_->start(makeWait(""), now));
EXPECT_EQ(rig.handler_->update(now).status, ActionStatus::kSucceeded);
}
TEST(WaitActionHandler, RefusesNonNumericDuration)
{
WaitRig rig;
ASSERT_NE(rig.handler_, nullptr);
// Từ chối tại start(): caller coi như action hỏng và fleet nhận được lý do.
EXPECT_FALSE(rig.handler_->start(makeWait("mot-luc-nua"), robot::Time::now()));
}
TEST(WaitActionHandler, RefusesNegativeDuration)
{
WaitRig rig;
ASSERT_NE(rig.handler_, nullptr);
EXPECT_FALSE(rig.handler_->start(makeWait("-1.0"), robot::Time::now()));
}
TEST(WaitActionHandler, RefusesDurationAboveTheConfiguredCeiling)
{
WaitRig rig;
ASSERT_NE(rig.handler_, nullptr);
// max_duration = 5 s trong config test. Vượt trần phải là THẤT BẠI, không phải bị cắt xuống 5 s:
// im lặng rút ngắn là giấu đi một lỗi cấu hình của fleet.
EXPECT_FALSE(rig.handler_->start(makeWait("120.0"), robot::Time::now()));
}
TEST(WaitActionHandler, RestartResetsTheClock)
{
WaitRig rig;
ASSERT_NE(rig.handler_, nullptr);
const robot::Time now = robot::Time::now();
ASSERT_TRUE(rig.handler_->start(makeWait("1.0"), now));
EXPECT_EQ(rig.handler_->update(now + robot::Duration(1.0)).status, ActionStatus::kSucceeded);
const robot::Time later = now + robot::Duration(10.0);
ASSERT_TRUE(rig.handler_->start(makeWait("1.0"), later));
EXPECT_EQ(rig.handler_->update(later).status, ActionStatus::kRunning);
}
} // namespace
int main(int argc, char** argv)
{
#ifdef ACTION_CORE_TEST_CONFIG_DIR
setenv("PNKX_NAV_CORE_CONFIG_DIR", ACTION_CORE_TEST_CONFIG_DIR, 0);
#endif
#ifdef ACTION_CORE_TEST_LIBRARY_DIR
setenv("PNKX_NAV_CORE_LIBRARY_PATH", ACTION_CORE_TEST_LIBRARY_DIR, 0);
#endif
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}