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

View File

@@ -8,6 +8,7 @@ mission_adapters:
mission_sources:
- {name: goal_src, type: GoalSourceAdapter}
- {name: vda5050_src, type: VDA5050SourceAdapter}
- {name: instant_src, type: InstantActionSourceAdapter}
mission_timeout: 0.0 # [s] 0 = tắt
clear_queue_on_failure: false # false = chỉ bỏ chặng lỗi, giữ phần còn lại của order
@@ -20,6 +21,11 @@ GoalSourceAdapter:
VDA5050SourceAdapter:
library_path: libmission_adapters_vda5050_source
# Cùng .so, alias thứ hai. plugin_registry_test khoá đúng điểm này: hai symbol khác nhau trong một
# thư viện phải nạp được thành hai adapter với hai schema khác nhau.
InstantActionSourceAdapter:
library_path: libmission_adapters_vda5050_source
# ── Các case lỗi mà plugin_registry_test cố ý dựng ra ────────────────────────────────────────────
#
# Mỗi case là một namespace riêng để test gọi loadFromConfig(nh, "<namespace>") mà không phải sinh
@@ -105,3 +111,18 @@ vda5050_bad_profile:
invalid_name:
steps:
- {move: 0.5, profile: dock}
# ── instant_src ───────────────────────────────────────────────────────────────────────────────────
#
# CỐ Ý không khai `compound_actions` ở đây: adapter phải đọc bảng của `vda5050_src`. Khai bảng thứ
# hai là đúng cái bệnh hai-bản-lệch-nhau mà việc dùng chung sinh ra để tránh.
instant_src:
compound_actions_from: vda5050_src
# Trỏ tới một nguồn không khai bảng nào: hợp lệ, adapter chạy không có compound action nào.
instant_src_no_table:
compound_actions_from: registry_test_empty
# compound_actions_from rỗng -> configure() phải trượt ngay lúc boot.
instant_src_empty_source:
compound_actions_from: ""

View File

@@ -79,19 +79,70 @@ protected:
// ── Đường nạp bình thường ────────────────────────────────────────────────────────────────────────
TEST_F(PluginRegistryTest, LoadsBothSourcesFromConfig)
TEST_F(PluginRegistryTest, LoadsEverySourceFromConfig)
{
PluginRegistry registry;
ASSERT_TRUE(registry.loadFromConfig(nh))
<< "could not load the plugin — check PNKX_NAV_CORE_CONFIG_DIR and devel/lib";
EXPECT_EQ(registry.size(), 2u);
EXPECT_EQ(registry.size(), 3u);
EXPECT_NE(registry.find(schema::kPoseStamped), nullptr);
EXPECT_NE(registry.find(schema::kVda5050Order), nullptr);
EXPECT_NE(registry.find(schema::kInstantAction), nullptr);
EXPECT_EQ(registry.find("does.not.exist"), nullptr);
}
TEST_F(PluginRegistryTest, TwoAliasesInOneLibraryLoadAsTwoAdapters)
{
// VDA5050SourceAdapter và InstantActionSourceAdapter là hai BOOST_DLL_ALIAS trong CÙNG một
// libmission_adapters_vda5050_source.so — cố ý, để hai adapter dùng chung một bản bộ nở compound
// action thay vì có hai bản trôi lệch nhau.
//
// Test này khoá chính cơ chế đó: `library_path` giống hệt nhau ở hai entry phải cho ra hai
// adapter riêng biệt với hai schema riêng biệt. Nếu loader gộp theo library thay vì theo symbol,
// một trong hai schema sẽ biến mất và đường vào tương ứng chết lặng.
PluginRegistry registry;
ASSERT_TRUE(registry.loadFromConfig(nh));
auto* order_adapter = registry.find(schema::kVda5050Order);
auto* instant_adapter = registry.find(schema::kInstantAction);
ASSERT_NE(order_adapter, nullptr);
ASSERT_NE(instant_adapter, nullptr);
EXPECT_NE(order_adapter, instant_adapter) << "hai alias phải là hai instance, không phải một";
EXPECT_EQ(order_adapter->schema(), std::string(schema::kVda5050Order));
EXPECT_EQ(instant_adapter->schema(), std::string(schema::kInstantAction));
}
TEST_F(PluginRegistryTest, TheInstantAdapterLoadedFromDiskExpandsCompoundActions)
{
// Không chỉ nạp được: adapter nạp qua Boost.DLL phải thật sự thấy bảng compound của nguồn order.
// `compound_actions_from` được đọc trong configure(), và configure() chạy bên trong
// loadFromConfig() — nên đây là chỗ duy nhất kiểm được cả chuỗi nạp + cấu hình + dùng chung bảng.
PluginRegistry registry;
ASSERT_TRUE(registry.loadFromConfig(nh));
auto* adapter = registry.find(schema::kInstantAction);
ASSERT_NE(adapter, nullptr);
robot_protocol_msgs::Action action;
action.actionType = "UnDockFromStation";
action.actionId = "undock_1";
action.blockingType = "HARD";
const auto request = MissionRequest::fromInstantAction(action);
std::string reason;
ASSERT_TRUE(adapter->validate(request, reason)) << reason;
const auto result = adapter->convert(request);
ASSERT_EQ(result.missions.size(), 1u);
EXPECT_EQ(result.missions.front()->motion_hint, "go_straight")
<< "nở được nghĩa là bảng compound của vda5050_src đã tới được adapter này";
EXPECT_DOUBLE_EQ(result.missions.front()->relative_distance, -1.0);
}
TEST_F(PluginRegistryTest, LoadedAdapterConvertsRealPayload)
{
PluginRegistry registry;