This commit is contained in:
2026-08-10 22:26:10 +07:00
parent 4a6b541773
commit ec11dc42ed
9 changed files with 724 additions and 9 deletions

View File

@@ -514,6 +514,317 @@ TEST(CompoundConfig, RejectsInvalidOrActionOnlyProfiles)
EXPECT_FALSE(adapter.configure("vda5050_bad_profile", nh));
}
// ═════════════════════════════════════════════════════════════════════════════════════════════════
// InstantActionSourceAdapter
//
// Thứ phải khoá ở đây, theo mức nghiêm trọng:
// 1. instantAction dùng ĐÚNG bảng compound của nguồn order — nếu không thì `charge` có hai nghĩa
// tuỳ đường vào, và một trong hai nghĩa sẽ bỏ bước dò rồi lùi vào trạm bằng toạ độ cũ;
// 2. `blockingType` map ra đúng SubmitMode — sai hướng nghĩa là một `MutedOn` xoá cả order đang
// chạy, hoặc một lệnh HARD phải xếp hàng sau cả tuyến đường;
// 3. thiếu tham số cấu trúc thì BỎ HẲN, không chạy nửa chuỗi;
// 4. không bao giờ sinh ra mission không goal cũng không action (EventProcessor sẽ vứt cả lô).
// ═════════════════════════════════════════════════════════════════════════════════════════════════
namespace
{
using mission_plugins::InstantActionSourceAdapter;
/// @brief instantAction hợp lệ tối thiểu: có actionType, actionId và blockingType.
robot_protocol_msgs::Action makeInstant(const std::string& type,
const std::string& blocking = "HARD",
const std::string& param_key = "",
const std::string& param_value = "")
{
robot_protocol_msgs::Action action;
action.actionType = type;
action.actionId = type + "_instant_id";
action.blockingType = blocking;
if (!param_key.empty())
{
robot_protocol_msgs::ActionParameter p;
p.key = param_key;
p.value = param_value;
action.actionParameters.push_back(p);
}
return action;
}
class InstantActionAdapterTest : public ::testing::Test
{
protected:
void SetUp() override
{
robot::NodeHandle nh;
ASSERT_TRUE(adapter.configure("instant_src", nh));
}
ConversionResult convert(const robot_protocol_msgs::Action& action)
{
return adapter.convert(MissionRequest::fromInstantAction(action));
}
ConversionResult convertAll(const std::vector<robot_protocol_msgs::Action>& actions)
{
return adapter.convert(MissionRequest::fromInstantActions(actions));
}
InstantActionSourceAdapter adapter;
};
} // namespace
TEST_F(InstantActionAdapterTest, DeclaresItsOwnSchema)
{
EXPECT_EQ(adapter.schema(), std::string(mission_adapters::schema::kInstantAction));
EXPECT_NE(adapter.schema(), std::string(mission_adapters::schema::kVda5050Order))
<< "trùng schema với nguồn order thì registry chỉ giữ được một trong hai";
}
TEST_F(InstantActionAdapterTest, SharesTheCompoundTableOfTheOrderSource)
{
EXPECT_EQ(adapter.compoundSource(), "vda5050_src");
}
TEST(InstantActionConfig, DefaultsToTheOrderSourceWhenNotConfigured)
{
robot::NodeHandle nh;
InstantActionSourceAdapter adapter;
// `instant_src_no_table` không khai compound_actions_from... nhưng nó khai. Dùng một namespace
// hoàn toàn trống để kiểm mặc định.
ASSERT_TRUE(adapter.configure("registry_test_empty", nh));
EXPECT_EQ(adapter.compoundSource(), "vda5050_src") << "mặc định phải trỏ về nguồn order";
}
TEST(InstantActionConfig, AcceptsASourceWithNoCompoundTable)
{
robot::NodeHandle nh;
InstantActionSourceAdapter adapter;
ASSERT_TRUE(adapter.configure("instant_src_no_table", nh))
<< "không có bảng là hợp lệ: mọi action chạy thẳng như action thường";
const auto result = adapter.convert(MissionRequest::fromInstantAction(makeInstant("charge")));
ASSERT_EQ(result.missions.size(), 1u) << "không có bảng thì `charge` KHÔNG được nở";
EXPECT_EQ(result.missions[0]->actions.at(0).action.actionType, "charge");
}
TEST(InstantActionConfig, RejectsAnEmptyCompoundSource)
{
robot::NodeHandle nh;
InstantActionSourceAdapter adapter;
EXPECT_FALSE(adapter.configure("instant_src_empty_source", nh))
<< "chuỗi rỗng không trỏ tới nguồn nào — phải trượt lúc boot, không phải lúc action đầu tới";
}
TEST_F(InstantActionAdapterTest, RejectsAMissingPayload)
{
MissionRequest request;
request.schema = mission_adapters::schema::kInstantAction;
std::string reason;
EXPECT_FALSE(adapter.validate(request, reason));
EXPECT_FALSE(reason.empty());
}
TEST_F(InstantActionAdapterTest, RejectsAnEmptyMessage)
{
std::string reason;
EXPECT_FALSE(adapter.validate(MissionRequest::fromInstantActions({}), reason))
<< "message rỗng để host ngồi chờ một kết quả không bao giờ tới";
}
TEST_F(InstantActionAdapterTest, RejectsAnEmptyActionType)
{
auto action = makeInstant("PickUp");
action.actionType.clear();
std::string reason;
EXPECT_FALSE(adapter.validate(MissionRequest::fromInstantAction(action), reason));
}
TEST_F(InstantActionAdapterTest, RejectsAnEmptyActionId)
{
auto action = makeInstant("PickUp");
action.actionId.clear();
std::string reason;
EXPECT_FALSE(adapter.validate(MissionRequest::fromInstantAction(action), reason))
<< "thiếu actionId thì action chạy xong fleet cũng không ghép được kết quả về đâu";
}
TEST_F(InstantActionAdapterTest, AcceptsAWellFormedAction)
{
std::string reason;
EXPECT_TRUE(adapter.validate(MissionRequest::fromInstantAction(makeInstant("MutedOn")), reason));
EXPECT_TRUE(reason.empty());
}
TEST_F(InstantActionAdapterTest, APlainActionBecomesOneActionOnlyMission)
{
const auto result = convert(makeInstant("MutedOn"));
ASSERT_EQ(result.missions.size(), 1u);
EXPECT_FALSE(result.missions[0]->has_goal) << "action thường không có quãng đường nào để đi";
ASSERT_EQ(result.missions[0]->actions.size(), 1u);
EXPECT_EQ(result.missions[0]->actions[0].action.actionType, "MutedOn");
EXPECT_EQ(result.missions[0]->actions[0].action.actionId, "MutedOn_instant_id")
<< "actionId phải đi nguyên vẹn — host dùng nó để ghép trạng thái trả về";
EXPECT_TRUE(result.missions[0]->motion_hint.empty())
<< "mission chỉ-action để trống profile, nếu không sẽ bị hiểu là yêu cầu điều khiển";
}
TEST_F(InstantActionAdapterTest, ACompoundActionExpandsExactlyLikeANodeAction)
{
const auto result = convert(makeInstant("charge", "HARD", "goal_frame", "charger_07_goal"));
// DetectCharger | docking | startCharging — KHÔNG có chặng nav dẫn tới, vì instantAction không
// mang node nào.
ASSERT_EQ(result.missions.size(), 3u);
EXPECT_FALSE(result.missions[0]->has_goal);
ASSERT_EQ(result.missions[0]->actions.size(), 1u);
EXPECT_EQ(result.missions[0]->actions[0].action.actionType, "DetectCharger");
EXPECT_TRUE(result.missions[1]->has_goal);
EXPECT_EQ(result.missions[1]->motion_hint, "docking");
EXPECT_EQ(result.missions[1]->goal_frame, "charger_07_goal")
<< "frame phải lấy từ actionParameters của chính instantAction";
EXPECT_EQ(result.missions[1]->marker, "charger");
EXPECT_FALSE(result.missions[2]->has_goal);
ASSERT_EQ(result.missions[2]->actions.size(), 1u);
EXPECT_EQ(result.missions[2]->actions[0].action.actionType, "startCharging");
}
TEST_F(InstantActionAdapterTest, TheCarrierLegIsDroppedOnceTheActionIsExpanded)
{
const auto result = convert(makeInstant("charge", "HARD", "goal_frame", "charger_07_goal"));
// Bất biến D8, cũng là thứ EventProcessor kiểm và vứt CẢ LÔ nếu vi phạm: chặng mang action gốc
// trở thành rỗng sau khi nở, nên nó phải bị bỏ chứ không được đẩy xuống.
for (const auto& mission : result.missions)
{
EXPECT_TRUE(mission->has_goal || !mission->actions.empty())
<< "mission không goal cũng không action là mission không có việc gì để làm";
}
}
TEST_F(InstantActionAdapterTest, ARelativeMoveCompoundCarriesTheDistance)
{
const auto result = convert(makeInstant("UnDockFromStation", "HARD"));
ASSERT_EQ(result.missions.size(), 1u);
EXPECT_TRUE(result.missions[0]->has_goal);
EXPECT_EQ(result.missions[0]->motion_hint, "go_straight");
EXPECT_DOUBLE_EQ(result.missions[0]->relative_distance, -1.0) << "[m] âm = lùi ra khỏi trạm";
}
TEST_F(InstantActionAdapterTest, AMissingCompoundParameterDropsTheWholeAction)
{
// `charge` cần `goal_frame` trong actionParameters; không có thì bước docking không biết lùi vào
// đâu. Chạy nửa chuỗi — dò xong rồi đóng relay sạc tại chỗ đang đứng — nguy hiểm hơn là không
// chạy.
const auto result = convert(makeInstant("charge", "HARD"));
EXPECT_TRUE(result.missions.empty());
}
TEST(InstantActionBlocking, EveryMessagePreemptsRegardlessOfBlockingType)
{
// Message instantActions mới LUÔN thay message cũ. `kAppend` không còn ca dùng nào đúng: xếp
// sau chuỗi cũ thì lệnh "instant" chạy sau vài chục giây, và chuỗi cũ vẫn kịp thao tác thiết bị
// dù đã bị lệnh mới thay thế.
EXPECT_EQ(InstantActionSourceAdapter::toSubmitMode("HARD"), SubmitMode::kReplace);
EXPECT_EQ(InstantActionSourceAdapter::toSubmitMode("SOFT"), SubmitMode::kReplace);
EXPECT_EQ(InstantActionSourceAdapter::toSubmitMode("NONE"), SubmitMode::kReplace);
EXPECT_EQ(InstantActionSourceAdapter::toSubmitMode(""), SubmitMode::kReplace);
EXPECT_EQ(InstantActionSourceAdapter::toSubmitMode("BLOCKING"), SubmitMode::kReplace);
}
TEST_F(InstantActionAdapterTest, TheConversionAlwaysAsksForPreemption)
{
EXPECT_EQ(convert(makeInstant("MutedOn", "HARD")).mode, SubmitMode::kReplace);
EXPECT_EQ(convert(makeInstant("MutedOn", "SOFT")).mode, SubmitMode::kReplace);
EXPECT_EQ(convert(makeInstant("MutedOn", "NONE")).mode, SubmitMode::kReplace);
}
TEST_F(InstantActionAdapterTest, TwoActionsInARowDoNotLeakStateIntoEachOther)
{
const auto first = convert(makeInstant("charge", "HARD", "goal_frame", "charger_01_goal"));
const auto second = convert(makeInstant("MutedOn", "SOFT"));
ASSERT_EQ(first.missions.size(), 3u);
ASSERT_EQ(second.missions.size(), 1u);
EXPECT_EQ(second.missions[0]->actions.at(0).action.actionType, "MutedOn");
EXPECT_EQ(second.mode, SubmitMode::kReplace)
<< "message sau không được thừa hưởng state của message trước";
}
// ── Nhiều action trong MỘT message ──────────────────────────────────────────────────────────────
//
// Đây là ca mà bản "một action một lần" hỏng: action `HARD` thứ hai preempt và xoá sạch chặng của
// action thứ nhất. Cả message phải thành MỘT lô, một lần vào hàng đợi.
TEST_F(InstantActionAdapterTest, EveryActionOfTheMessageIsConvertedInOrder)
{
const auto result = convertAll({ makeInstant("MutedOn", "SOFT"),
makeInstant("MutedOff", "SOFT") });
ASSERT_EQ(result.missions.size(), 2u);
EXPECT_EQ(result.missions[0]->actions.at(0).action.actionType, "MutedOn");
EXPECT_EQ(result.missions[1]->actions.at(0).action.actionType, "MutedOff");
}
TEST_F(InstantActionAdapterTest, CompoundAndPlainActionsMixInTheRightOrder)
{
// `[MutedOn, charge, MutedOff]` — chuỗi charge phải nằm GIỮA, không được gom hết lại: bật lại
// cảm biến an toàn trước khi robot lùi vào trạm là đảo ngược hợp đồng an toàn.
const auto result = convertAll({ makeInstant("MutedOn", "SOFT"),
makeInstant("charge", "SOFT", "goal_frame", "charger_09_goal"),
makeInstant("MutedOff", "SOFT") });
// MutedOn | DetectCharger | docking | startCharging | MutedOff
ASSERT_EQ(result.missions.size(), 5u);
EXPECT_EQ(result.missions[0]->actions.at(0).action.actionType, "MutedOn");
EXPECT_EQ(result.missions[1]->actions.at(0).action.actionType, "DetectCharger");
EXPECT_EQ(result.missions[2]->motion_hint, "docking");
EXPECT_EQ(result.missions[3]->actions.at(0).action.actionType, "startCharging");
EXPECT_EQ(result.missions[4]->actions.at(0).action.actionType, "MutedOff");
}
TEST_F(InstantActionAdapterTest, AMixedMessageStillPreemptsAsAWhole)
{
// Mode là của CẢ message, và luôn là preempt — kể cả message không có action `HARD` nào.
EXPECT_EQ(convertAll({ makeInstant("a", "SOFT"), makeInstant("b", "HARD") }).mode,
SubmitMode::kReplace);
EXPECT_EQ(convertAll({ makeInstant("a", "SOFT"), makeInstant("b", "NONE") }).mode,
SubmitMode::kReplace);
}
TEST_F(InstantActionAdapterTest, OneUnexpandableActionDropsTheWholeMessage)
{
// `charge` thiếu `goal_frame`. Những action đứng TRƯỚC nó đã dựng xong cũng phải bị bỏ: fleet
// chờ một câu trả lời done/fail cho cả message, mà `MutedOn` thì đã đụng vào thiết bị thật.
const auto result = convertAll({ makeInstant("MutedOn", "SOFT"),
makeInstant("charge", "SOFT"),
makeInstant("MutedOff", "SOFT") });
EXPECT_TRUE(result.missions.empty())
<< "chạy được nửa message rồi báo xong là báo sai";
}
TEST_F(InstantActionAdapterTest, ValidateRejectsTheMessageWhenAnyActionIsMalformed)
{
auto bad = makeInstant("PickUp");
bad.actionId.clear();
std::string reason;
EXPECT_FALSE(adapter.validate(
MissionRequest::fromInstantActions({ makeInstant("MutedOn"), bad }), reason))
<< "một action hỏng làm hỏng cả message — không chạy nửa vời";
EXPECT_NE(reason.find("PickUp"), std::string::npos) << "reason: " << reason;
}
int main(int argc, char** argv)
{
// Bảng `compound_actions` nằm trong cây config test. `overwrite = 0` để shell vẫn override được