#include "seeragvmapclient_fake.h" #include "robotclient.h" #include "cmvr/api/agv_service.grpc.pb.h" #include #include #include #include namespace { const QString deviceId = QStringLiteral("fixture_seer"); void waitFor(const std::function &predicate, const char *message, int timeoutMs = 15000) { QElapsedTimer elapsed; elapsed.start(); while (!predicate() && elapsed.elapsed() < timeoutMs) { QCoreApplication::processEvents(); QThread::msleep(2); } require(predicate(), message); } void pumpFor(int milliseconds) { QElapsedTimer elapsed; elapsed.start(); while (elapsed.elapsed() < milliseconds) { QCoreApplication::processEvents(); QThread::msleep(2); } } // Record legacy map calls so tests prove the enabled bridge bypasses their // body packaging, while disabled direct mode still uses this existing API. class FakeLegacyAgv final : public cmvr::api::AgvService::Service { public: grpc::Status setVelocity(grpc::ServerContext *context, const cmvr::api::AgvSetVelocityCommand::Request *, cmvr::api::AgvSetVelocityCommand::Feedback *response) override { ++velocities; while (holdVelocity.load() && !context->IsCancelled()) std::this_thread::sleep_for(std::chrono::milliseconds(1)); ++velocitiesFinished; response->mutable_header()->set_success(true); return grpc::Status::OK; } grpc::Status downloadMap(grpc::ServerContext *, const cmvr::api::AgvMapCommand::Request *, cmvr::api::AgvMapCommand::Feedback *response) override { ++reads; response->mutable_header()->set_success(true); response->set_content(completeMap.toStdString()); return grpc::Status::OK; } grpc::Status uploadMap(grpc::ServerContext *, const cmvr::api::AgvMapCommand::Request *request, cmvr::api::AgvMapCommand::Feedback *response) override { ++uploads; { std::lock_guard lock(mutex); uploaded = request->content(); } response->mutable_header()->set_success(true); return grpc::Status::OK; } grpc::Status switchMap(grpc::ServerContext *, const cmvr::api::AgvMapCommand::Request *, cmvr::api::AgvMapCommand::Feedback *response) override { ++switches; response->mutable_header()->set_success(true); return grpc::Status::OK; } std::atomic reads{0}; std::atomic uploads{0}; std::atomic switches{0}; std::atomic velocities{0}; std::atomic velocitiesFinished{0}; std::atomic holdVelocity{false}; std::mutex mutex; std::string uploaded; }; class FakeServices final { public: explicit FakeServices(const FakeRobot &) { grpc::ServerBuilder builder; builder.AddListeningPort("127.0.0.1:0", grpc::InsecureServerCredentials(), &port); builder.RegisterService(&legacy); server = builder.BuildAndStart(); require(server && port > 0, "fake legacy services bind loopback ephemeral port"); } ~FakeServices() { server->Shutdown(); server->Wait(); } FakeLegacyAgv legacy; int port = 0; std::unique_ptr server; }; struct ReadEvent { bool success; QString device; QString map; QString content; QString detail; }; struct SaveEvent { bool success; QString detail; }; struct Capture { explicit Capture(RobotClient &client) { QObject::connect(&client, &RobotClient::connectionChanged, &context, [&] (bool value, int, const QString &) { connected = value; }); QObject::connect(&client, &RobotClient::agvMapDirectBusyChanged, &context, [&] (bool value) { busy = value; busyEvents.push_back(value); }); QObject::connect(&client, &RobotClient::agvMapContentReceived, &context, [&] (quint64 id, const QString &device, const QString &map, const QString &content, bool success, const QString &detail) { readEvents[id].push_back({success, device, map, content, detail}); }); QObject::connect(&client, &RobotClient::agvMapSaveFinished, &context, [&] (quint64 id, const QString &, const QString &, bool success, const QString &detail) { saveEvents[id].push_back({success, detail}); }); QObject::connect(&client, &RobotClient::commandFinished, &context, [&] (const QString &command, bool success, const QString &detail) { if (command == QStringLiteral("AGV SwitchMap")) switchEvents.push_back({success, detail}); if (command == QStringLiteral("AGV SetVelocity") && !success) ++rejectedVelocities; }); QObject::connect(&client, &RobotClient::agvDirectMapSwitchConfirmed, &context, [&] (const QString &device, const QString &map, const QString &) { confirmations.push_back({device, map}); }); } bool connected = false; bool busy = false; int rejectedVelocities = 0; std::vector busyEvents; std::map> readEvents; std::map> saveEvents; std::vector switchEvents; std::vector> confirmations; QObject context; }; void configure(RobotClient &client, Capture &capture, FakeServices &services, bool enabled = true, bool demo = false) { client.configureAgvMapDirect(enabled, QStringLiteral("127.0.0.1")); client.connectToServer(QStringLiteral("127.0.0.1"), services.port, demo); waitFor([&] { return capture.connected; }, "RobotClient connects to loopback or local Demo source"); } void successfulChain() { FakeRobot robot(Fault::None, true); robot.state.fragment.store(true); FakeServices services(robot); RobotClient client; Capture capture(client); configure(client, capture, services); // Mimic the editor's read-latest -> upload callback and the page's // switch-confirmed -> reload callback. Both must see busy already cleared. QObject::connect(&client, &RobotClient::agvMapContentReceived, [&] (quint64 id, const QString &, const QString &, const QString &content, bool success, const QString &) { if (id == 1 && success) client.saveAgvMapContent(2, deviceId, mapName, content); }); QObject::connect(&client, &RobotClient::agvMapSaveFinished, [&] (quint64 id, const QString &, const QString &, bool success, const QString &) { if (id == 2 && success) client.requestAgvSwitchMap(deviceId, mapName); }); QObject::connect(&client, &RobotClient::agvDirectMapSwitchConfirmed, [&] (const QString &, const QString &, const QString &) { client.requestAgvMapContent(3, deviceId, mapName); }); client.requestAgvMapContent(1, deviceId, mapName); waitFor([&] { return !capture.readEvents[3].empty(); }, "real bridge callback chain completes read/save/switch/reload"); require(capture.readEvents[1].size() == 1 && capture.readEvents[1][0].success, "bridge reports initial complete map read once"); require(capture.saveEvents[2].size() == 1 && capture.saveEvents[2][0].success, "read callback can start save without spurious busy rejection"); require(capture.readEvents[3].size() == 1 && capture.readEvents[3][0].success && capture.readEvents[3][0].content == completeMap, "confirmed switch callback reloads exact target map"); require(capture.switchEvents.size() == 1 && capture.switchEvents[0].success && capture.confirmations.size() == 1 && capture.confirmations[0].second == mapName, "bridge reports confirmed switch exactly once"); require(!capture.busy && capture.busyEvents.size() == 8, "four operations each open and close busy state"); require(robot.state.uploads == 1 && robot.state.switches == 1 && robot.state.reads == 2, "callback chain sends exactly one mutation per operation"); require(services.legacy.uploads == 0 && services.legacy.switches == 0 && services.legacy.reads == 0, "enabled map bridge bypasses old map gRPC wrapper"); std::lock_guard lock(robot.state.captureMutex); require(robot.state.uploaded == completeMap.toUtf8(), "actual RobotClient bridge preserves complete raw 4010 payload"); std::cout << "PASS bridge read-latest/save/switch-confirmed/reload callback chain\n"; } void definiteErrors() { { FakeRobot robot(Fault::HeaderRejected, true); FakeServices services(robot); RobotClient client; Capture capture(client); configure(client, capture, services); client.saveAgvMapContent(10, deviceId, mapName, completeMap); waitFor([&] { return !capture.saveEvents[10].empty(); }, "bridge returns controller save rejection"); require(capture.saveEvents[10].size() == 1 && !capture.saveEvents[10][0].success && capture.saveEvents[10][0].detail.contains(QStringLiteral("40050")), "save rejection never becomes success in queued signal delivery"); require(robot.state.uploads == 1 && services.legacy.uploads == 0, "save rejection has no hidden legacy fallback or resend"); } { FakeRobot robot(Fault::LoadRejected, true); FakeServices services(robot); RobotClient client; Capture capture(client); configure(client, capture, services); client.requestAgvSwitchMap(deviceId, mapName); waitFor([&] { return !capture.switchEvents.empty(); }, "bridge returns explicit failed map load after accepted ACK"); require(capture.switchEvents.size() == 1 && !capture.switchEvents[0].success && capture.confirmations.empty(), "accepted ACK plus explicit load failure never reports successful switch"); require(robot.state.switches == 1, "load failure is not resent by bridge"); } std::cout << "PASS bridge preserves save rejection and explicit load failure\n"; } void busyAndContextCancellation() { FakeRobot robot(Fault::None, true); robot.state.readDelayMs.store(500); FakeServices services(robot); RobotClient client; Capture capture(client); configure(client, capture, services); client.requestAgvMapContent(20, deviceId, mapName); require(capture.busy, "starting async map read immediately marks bridge busy"); client.saveAgvMapContent(21, deviceId, mapName, completeMap); require(capture.saveEvents[21].size() == 1 && !capture.saveEvents[21][0].success, "second mutation while busy is immediately rejected"); waitFor([&] { return robot.state.reads == 1; }, "fake first read reached TCP channel"); client.requestAgvMapContent(22, QStringLiteral("different_device"), mapName); waitFor([&] { return !capture.busy; }, "device context change cancels old direct worker promptly"); pumpFor(650); require(capture.readEvents[20].size() == 1 && !capture.readEvents[20][0].success, "old context receives cancellation failure but no late success"); require(capture.readEvents[22].size() == 1 && !capture.readEvents[22][0].success, "new context does not queue another operation while prior worker drains"); require(robot.state.uploads == 0 && robot.state.reads == 1, "busy rejection and context cancellation send no queued mutation or duplicate read"); robot.state.readDelayMs.store(0); client.requestAgvMapContent(23, deviceId, mapName); waitFor([&] { return !capture.readEvents[23].empty(); }, "bridge becomes usable again after cancelled context drains"); require(capture.readEvents[23].size() == 1 && capture.readEvents[23][0].success, "fresh context returns requested complete map without old result leakage"); std::cout << "PASS bridge busy deduplication and stale context cancellation\n"; } void legacyAndDemoPaths() { { FakeRobot robot(Fault::None, true); FakeServices services(robot); RobotClient client; Capture capture(client); configure(client, capture, services, false); client.requestAgvMapContent(30, deviceId, mapName); waitFor([&] { return !capture.readEvents[30].empty(); }, "disabled direct mode uses legacy read"); client.saveAgvMapContent(31, deviceId, mapName, completeMap); waitFor([&] { return !capture.saveEvents[31].empty(); }, "disabled direct mode uses legacy upload"); require(capture.readEvents[30][0].success && capture.saveEvents[31][0].success && services.legacy.reads == 1 && services.legacy.uploads == 1, "existing gRPC map interface still works when direct mode disabled"); require(robot.state.reads == 0 && robot.state.uploads == 0, "disabled direct mode opens no TCP or config discovery requests"); } { FakeRobot robot(Fault::None, true); FakeServices services(robot); RobotClient client; Capture capture(client); configure(client, capture, services, true, true); const QString demoDevice = QStringLiteral("demo_agv_1"); client.saveAgvMapContent(40, demoDevice, mapName, completeMap); waitFor([&] { return !capture.saveEvents[40].empty(); }, "Demo map save remains on local worker"); client.requestAgvMapContent(41, demoDevice, mapName); waitFor([&] { return !capture.readEvents[41].empty(); }, "Demo reload remains on local worker"); client.requestAgvSwitchMap(demoDevice, mapName); waitFor([&] { return !capture.switchEvents.empty(); }, "Demo switch remains on local worker"); require(capture.saveEvents[40][0].success && capture.readEvents[41][0].success && capture.readEvents[41][0].content == completeMap && capture.switchEvents[0].success, "Demo save/read/switch still completes with direct option enabled"); require(capture.busyEvents.empty() && capture.confirmations.empty() && robot.state.reads == 0 && robot.state.uploads == 0 && robot.state.switches == 0 && services.legacy.uploads == 0, "Demo uses neither real map TCP nor config lookup nor legacy network writes"); } std::cout << "PASS unchanged legacy gRPC and local Demo map paths\n"; } void backendQueueBarrier() { { FakeRobot robot(Fault::None, true); FakeServices services(robot); RobotClient client; Capture capture(client); configure(client, capture, services); services.legacy.holdVelocity.store(true); client.requestAgvVelocity(deviceId, 0.1, 0.0, 0.0); waitFor([&] { return services.legacy.velocities == 1; }, "old motion RPC is blocked in original worker"); client.requestAgvVelocity(deviceId, 0.2, 0.0, 0.0); client.saveAgvMapContent(50, deviceId, mapName, completeMap); client.requestAgvVelocity(deviceId, 0.3, 0.0, 0.0); pumpFor(300); require(capture.busy && capture.rejectedVelocities == 1, "busy map operation blocks newly requested motion"); require(robot.state.acquires == 0 && robot.state.uploads == 0, "direct lookup, authority and map upload all wait for prior backend worker commands"); services.legacy.holdVelocity.store(false); waitFor([&] { return !capture.saveEvents[50].empty(); }, "queued old motions drain before direct map operation"); require(capture.saveEvents[50].size() == 1 && capture.saveEvents[50][0].success, "direct operation succeeds once backend barrier is passed"); require(services.legacy.velocities == 2 && services.legacy.velocitiesFinished == 2 && robot.state.uploads == 1, "only preexisting motions and exactly one map mutation were dispatched"); } { FakeRobot robot(Fault::None, true); FakeServices services(robot); RobotClient client; Capture capture(client); configure(client, capture, services); services.legacy.holdVelocity.store(true); client.requestAgvVelocity(deviceId, 0.1, 0.0, 0.0); waitFor([&] { return services.legacy.velocities == 1; }, "cancellation case has old worker RPC in flight"); client.saveAgvMapContent(51, deviceId, mapName, completeMap); client.cancelAgvMapDirectRequests(); require(capture.saveEvents[51].size() == 1 && !capture.saveEvents[51][0].success, "cancelling barrier-waiting operation immediately reports cancelled save"); services.legacy.holdVelocity.store(false); waitFor([&] { return !capture.busy; }, "cancelled backend barrier drains boundedly"); pumpFor(100); require(capture.saveEvents[51].size() == 1 && robot.state.acquires == 0 && robot.state.uploads == 0, "cancelled barrier never runs direct lookup or mutation and produces no late result"); } std::cout << "PASS backend queue barrier, new motion rejection and cancellation before barrier\n"; } } // namespace int main(int argc, char **argv) { QApplication application(argc, argv); successfulChain(); definiteErrors(); busyAndContextCancellation(); legacyAndDemoPaths(); backendQueueBarrier(); std::cout << "PASS RobotClient SEER map bridge integration: loopback services only; standard vehicle ports and ephemeral gRPC port\n"; return 0; }