504 lines
22 KiB
C++
504 lines
22 KiB
C++
|
|
#include "agvmapdocument.h"
|
||
|
|
#include "agvmapeditorwidget.h"
|
||
|
|
#include "agvmapviewwidget.h"
|
||
|
|
#include "robotclient.h"
|
||
|
|
#include "cmvr/api/agv_service.grpc.pb.h"
|
||
|
|
|
||
|
|
#include <grpcpp/grpcpp.h>
|
||
|
|
#include <QApplication>
|
||
|
|
#include <QDialog>
|
||
|
|
#include <QElapsedTimer>
|
||
|
|
#include <QJsonArray>
|
||
|
|
#include <QJsonDocument>
|
||
|
|
#include <QJsonObject>
|
||
|
|
#include <QLabel>
|
||
|
|
#include <QMessageBox>
|
||
|
|
#include <QPointer>
|
||
|
|
#include <QPushButton>
|
||
|
|
#include <QTableWidget>
|
||
|
|
#include <QTabWidget>
|
||
|
|
#include <QThread>
|
||
|
|
#include <QTimer>
|
||
|
|
#include <QVBoxLayout>
|
||
|
|
|
||
|
|
#include <functional>
|
||
|
|
#include <iostream>
|
||
|
|
#include <mutex>
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
void require(bool condition, const char *message)
|
||
|
|
{
|
||
|
|
if (!condition)
|
||
|
|
qFatal("AGV delete integration failed: %s", message);
|
||
|
|
}
|
||
|
|
|
||
|
|
void waitFor(const std::function<bool()> &ready, const char *message)
|
||
|
|
{
|
||
|
|
QElapsedTimer timer;
|
||
|
|
timer.start();
|
||
|
|
while (!ready() && timer.elapsed() < 7000) {
|
||
|
|
QApplication::processEvents();
|
||
|
|
QThread::msleep(2);
|
||
|
|
}
|
||
|
|
require(ready(), message);
|
||
|
|
}
|
||
|
|
|
||
|
|
void nextModal(const QString &kind, const std::function<void(QDialog *)> &action)
|
||
|
|
{
|
||
|
|
auto *timer = new QTimer(qApp);
|
||
|
|
timer->setInterval(5);
|
||
|
|
QObject::connect(timer, &QTimer::timeout, timer, [=]() {
|
||
|
|
auto *dialog = qobject_cast<QDialog *>(QApplication::activeModalWidget());
|
||
|
|
if (!dialog || dialog->objectName() != kind)
|
||
|
|
return;
|
||
|
|
// Stop before an action can enter a nested confirmation event loop.
|
||
|
|
timer->stop();
|
||
|
|
timer->deleteLater();
|
||
|
|
action(dialog);
|
||
|
|
});
|
||
|
|
timer->start();
|
||
|
|
QTimer::singleShot(7000, qApp, [guard = QPointer<QTimer>(timer)]() {
|
||
|
|
require(!guard || !guard->isActive(), "expected local editor modal opened");
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
int rowFor(QTableWidget *table, const QString &id)
|
||
|
|
{
|
||
|
|
for (int row = 0; row < table->rowCount(); ++row) {
|
||
|
|
if (table->item(row, 0)->text() == id)
|
||
|
|
return row;
|
||
|
|
}
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool hasRoute(const AgvMapDocument &document, const QString &id)
|
||
|
|
{
|
||
|
|
for (const QVariant &value : document.routes()) {
|
||
|
|
if (value.toMap().value("id").toString() == id)
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
QString fixture(const QString &mapName)
|
||
|
|
{
|
||
|
|
const QString seed = QStringLiteral(R"JSON({
|
||
|
|
"header":{"mapType":"2D-Map","mapName":"%1","resolution":0.05,"vendorHeader":"unchanged"},
|
||
|
|
"normalPosList":[{"x":1.23456789012345,"y":2},{"x":3,"y":4}],
|
||
|
|
"advancedLineList":[{"instanceName":"do not touch line","customLine":9007199254740993}],
|
||
|
|
"advancedPointList":[
|
||
|
|
{"className":"LandMark","instanceName":"LM1","description":"first","pos":{"x":0,"y":0},"vendorPoint":9007199254740993},
|
||
|
|
{"className":"LandMark","instanceName":"LM2","description":"second","pos":{"x":2,"y":0}},
|
||
|
|
{"className":"LandMark","instanceName":"LM3","description":"third","pos":{"x":2,"y":2}},
|
||
|
|
{"className":"LandMark","instanceName":"LM4","description":"fourth","pos":{"x":4,"y":2}}
|
||
|
|
],
|
||
|
|
"advancedCurveList":[],
|
||
|
|
"rootUnknown":{"largeInteger":18446744073709551615,"quoted":"keep [ ] } \\\" exactly"}
|
||
|
|
})JSON").arg(mapName);
|
||
|
|
AgvMapDocument document;
|
||
|
|
QString error;
|
||
|
|
require(document.load(seed, &error)
|
||
|
|
&& document.addRoute("LM1_LM2", "LM1", "LM2", true, &error)
|
||
|
|
&& document.addRoute("LM2_LM3", "LM2", "LM3", true, &error)
|
||
|
|
&& document.addRoute("LM3_LM4", "LM3", "LM4", true, &error),
|
||
|
|
"local native map fixture has four points and six directed routes");
|
||
|
|
QString result = document.content();
|
||
|
|
result.replace(QStringLiteral("\"instanceName\":\"LM1_LM2\""),
|
||
|
|
QStringLiteral("\"instanceName\":\"LM1_LM2\",\"vendorRoute\":9007199254740993"));
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
// The test only starts this service on 127.0.0.1:0. It cannot reach a vehicle.
|
||
|
|
class FakeAgvService final : public cmvr::api::AgvService::Service
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
enum Result { Persist, AckOnly };
|
||
|
|
FakeAgvService()
|
||
|
|
{
|
||
|
|
m_maps.insert("delete_map", fixture("delete_map"));
|
||
|
|
m_maps.insert("other_map", fixture("other_map"));
|
||
|
|
}
|
||
|
|
|
||
|
|
grpc::Status downloadMap(grpc::ServerContext *,
|
||
|
|
const cmvr::api::AgvMapCommand::Request *request,
|
||
|
|
cmvr::api::AgvMapCommand::Feedback *response) override
|
||
|
|
{
|
||
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
||
|
|
++m_reads;
|
||
|
|
const QString map = QString::fromStdString(request->map_name());
|
||
|
|
response->mutable_header()->set_success(m_maps.contains(map));
|
||
|
|
response->set_content(m_maps.value(map).toUtf8().toStdString());
|
||
|
|
return grpc::Status::OK;
|
||
|
|
}
|
||
|
|
|
||
|
|
grpc::Status uploadMap(grpc::ServerContext *,
|
||
|
|
const cmvr::api::AgvMapCommand::Request *request,
|
||
|
|
cmvr::api::AgvMapCommand::Feedback *response) override
|
||
|
|
{
|
||
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
||
|
|
m_uploads.push_back(*request);
|
||
|
|
if (m_result == Persist)
|
||
|
|
m_maps[QString::fromStdString(request->map_name())] = QString::fromUtf8(
|
||
|
|
request->content().data(), static_cast<int>(request->content().size()));
|
||
|
|
m_result = Persist;
|
||
|
|
response->mutable_header()->set_success(true);
|
||
|
|
return grpc::Status::OK;
|
||
|
|
}
|
||
|
|
|
||
|
|
void replace(const QString &content)
|
||
|
|
{
|
||
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
||
|
|
m_maps["delete_map"] = content;
|
||
|
|
}
|
||
|
|
void next(Result result)
|
||
|
|
{
|
||
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
||
|
|
m_result = result;
|
||
|
|
}
|
||
|
|
int reads()
|
||
|
|
{
|
||
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
||
|
|
return m_reads;
|
||
|
|
}
|
||
|
|
int uploads()
|
||
|
|
{
|
||
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
||
|
|
return static_cast<int>(m_uploads.size());
|
||
|
|
}
|
||
|
|
QString stored()
|
||
|
|
{
|
||
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
||
|
|
return m_maps.value("delete_map");
|
||
|
|
}
|
||
|
|
QString uploaded()
|
||
|
|
{
|
||
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
||
|
|
require(!m_uploads.empty(), "at least one explicit upload exists");
|
||
|
|
const auto &request = m_uploads.back();
|
||
|
|
require(request.map_name() == "delete_map" && request.header().device_id() == "fixture_delete_device",
|
||
|
|
"actual protobuf deletion upload retains target map and device");
|
||
|
|
return QString::fromUtf8(request.content().data(), static_cast<int>(request.content().size()));
|
||
|
|
}
|
||
|
|
|
||
|
|
private:
|
||
|
|
std::mutex m_mutex;
|
||
|
|
QHash<QString, QString> m_maps;
|
||
|
|
std::vector<cmvr::api::AgvMapCommand::Request> m_uploads;
|
||
|
|
Result m_result = Persist;
|
||
|
|
int m_reads = 0;
|
||
|
|
};
|
||
|
|
|
||
|
|
class Harness
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
Harness()
|
||
|
|
{
|
||
|
|
grpc::ServerBuilder builder;
|
||
|
|
builder.AddListeningPort("127.0.0.1:0", grpc::InsecureServerCredentials(), &port);
|
||
|
|
builder.RegisterService(&service);
|
||
|
|
server = builder.BuildAndStart();
|
||
|
|
require(server && port > 0, "ephemeral loopback mock is the only connected server");
|
||
|
|
QObject::connect(&client, &RobotClient::connectionChanged,
|
||
|
|
[&](bool value, int, const QString &) { connected = value; });
|
||
|
|
client.connectToServer("127.0.0.1", port, false);
|
||
|
|
waitFor([&]() { return connected; }, "actual RobotClient connects to loopback mock");
|
||
|
|
|
||
|
|
auto *layout = new QVBoxLayout(&window);
|
||
|
|
view = new AgvMapViewWidget(&window);
|
||
|
|
editor = new AgvMapEditorWidget(&client, view, &window);
|
||
|
|
layout->addWidget(editor);
|
||
|
|
layout->addWidget(view);
|
||
|
|
editor->setContextProvider([&]() {
|
||
|
|
AgvMapEditorWidget::Context context;
|
||
|
|
context.endpoint = QStringLiteral("127.0.0.1:%1").arg(port);
|
||
|
|
context.deviceId = "fixture_delete_device";
|
||
|
|
context.mapName = selected;
|
||
|
|
context.connected = connected;
|
||
|
|
return context;
|
||
|
|
});
|
||
|
|
QObject::connect(editor, &AgvMapEditorWidget::mapSaved, [&]() { ++saved; });
|
||
|
|
QObject::connect(editor, &AgvMapEditorWidget::busyChanged, [&](bool value) { busy = value; });
|
||
|
|
QObject::connect(&client, &RobotClient::agvMapSaveFinished,
|
||
|
|
[&](quint64, const QString &, const QString &, bool success, const QString &) {
|
||
|
|
if (success)
|
||
|
|
++acks;
|
||
|
|
});
|
||
|
|
window.resize(1000, 700);
|
||
|
|
window.show();
|
||
|
|
setFrame();
|
||
|
|
manage = editor->findChild<QPushButton *>("agvManageMapObjectsButton");
|
||
|
|
save = editor->findChild<QPushButton *>("agvSaveMapObjectsButton");
|
||
|
|
undo = editor->findChild<QPushButton *>("agvUndoMapObjectsButton");
|
||
|
|
status = editor->findChild<QLabel *>("agvMapEditorStatus");
|
||
|
|
require(manage && save && undo && status, "public editor save, manager and undo controls exist");
|
||
|
|
editor->setAutoLoadEnabled(true);
|
||
|
|
waitFor([&]() { return service.reads() == 1 && !busy && manage->isEnabled()
|
||
|
|
&& status->text().contains(QStringLiteral("地图已读取")); },
|
||
|
|
"editor loads complete native fixture through real gRPC client");
|
||
|
|
}
|
||
|
|
|
||
|
|
~Harness()
|
||
|
|
{
|
||
|
|
client.disconnectFromServer();
|
||
|
|
waitFor([&]() { return !connected; }, "mock client disconnected");
|
||
|
|
server->Shutdown();
|
||
|
|
server->Wait();
|
||
|
|
}
|
||
|
|
|
||
|
|
void setFrame()
|
||
|
|
{
|
||
|
|
QImage image(400, 300, QImage::Format_RGB32);
|
||
|
|
image.fill(Qt::white);
|
||
|
|
view->setMapFrame(image, {{"mapId", selected}, {"dimension", 2},
|
||
|
|
{"resolution", 0.05}, {"renderScale", 1.0},
|
||
|
|
{"originX", 0.0}, {"originY", 0.0}, {"originTheta", 0.0}});
|
||
|
|
editor->refreshContext();
|
||
|
|
}
|
||
|
|
|
||
|
|
void changeMap(const QString &name)
|
||
|
|
{
|
||
|
|
selected = name;
|
||
|
|
setFrame();
|
||
|
|
waitFor([&]() { return !busy && manage->isEnabled(); }, "one selected-map change settles");
|
||
|
|
}
|
||
|
|
|
||
|
|
void inspect(const std::function<void(QTableWidget *, QTableWidget *)> &action)
|
||
|
|
{
|
||
|
|
nextModal("agvMapObjectsDialog", [=](QDialog *dialog) {
|
||
|
|
auto *stations = dialog->findChild<QTableWidget *>("agvMapStationsTable");
|
||
|
|
auto *routes = dialog->findChild<QTableWidget *>("agvMapRoutesTable");
|
||
|
|
require(stations && routes, "manager exposes point and route tables");
|
||
|
|
action(stations, routes);
|
||
|
|
dialog->accept();
|
||
|
|
});
|
||
|
|
manage->click();
|
||
|
|
}
|
||
|
|
|
||
|
|
void remove(const QString &id, bool station)
|
||
|
|
{
|
||
|
|
nextModal("agvMapObjectsDialog", [=](QDialog *dialog) {
|
||
|
|
auto *tabs = dialog->findChild<QTabWidget *>("agvMapObjectsTabs");
|
||
|
|
auto *table = dialog->findChild<QTableWidget *>(station ? "agvMapStationsTable" : "agvMapRoutesTable");
|
||
|
|
auto *button = dialog->findChild<QPushButton *>(station ? "agvDeleteStationButton" : "agvDeleteRouteButton");
|
||
|
|
require(tabs && table && button, "dedicated delete UI exists in correct manager tab");
|
||
|
|
tabs->setCurrentIndex(station ? 0 : 1);
|
||
|
|
const int row = rowFor(table, id);
|
||
|
|
require(row >= 0, "existing native object can be selected for deletion");
|
||
|
|
table->setCurrentCell(row, 0);
|
||
|
|
require(button->isEnabled(), "deletion requires a selected object");
|
||
|
|
nextModal(station ? "agvDeleteStationConfirmDialog" : "agvDeleteRouteConfirmDialog",
|
||
|
|
[=](QDialog *confirm) {
|
||
|
|
auto *yes = confirm->findChild<QPushButton *>(station
|
||
|
|
? "agvDeleteStationConfirmButton" : "agvDeleteRouteConfirmButton");
|
||
|
|
require(yes, "deletion has an explicit confirmation button");
|
||
|
|
yes->click();
|
||
|
|
});
|
||
|
|
button->click();
|
||
|
|
require(rowFor(table, id) < 0, "confirmed deletion immediately removes the local manager row");
|
||
|
|
dialog->accept();
|
||
|
|
});
|
||
|
|
manage->click();
|
||
|
|
require(save->isEnabled() && undo->isEnabled(), "deletion remains an explicit unsaved undoable draft");
|
||
|
|
require(service.uploads() == 0, "deleting in the manager sends no immediate vehicle upload");
|
||
|
|
}
|
||
|
|
|
||
|
|
FakeAgvService service;
|
||
|
|
std::unique_ptr<grpc::Server> server;
|
||
|
|
RobotClient client;
|
||
|
|
QWidget window;
|
||
|
|
AgvMapViewWidget *view = nullptr;
|
||
|
|
AgvMapEditorWidget *editor = nullptr;
|
||
|
|
QPushButton *manage = nullptr;
|
||
|
|
QPushButton *save = nullptr;
|
||
|
|
QPushButton *undo = nullptr;
|
||
|
|
QLabel *status = nullptr;
|
||
|
|
QString selected = "delete_map";
|
||
|
|
int port = 0;
|
||
|
|
int saved = 0;
|
||
|
|
int acks = 0;
|
||
|
|
bool connected = false;
|
||
|
|
bool busy = false;
|
||
|
|
};
|
||
|
|
|
||
|
|
void requireUntouchedNativeFields(const QString &content)
|
||
|
|
{
|
||
|
|
const QString source = fixture("delete_map");
|
||
|
|
const auto root = QJsonDocument::fromJson(source.toUtf8()).object();
|
||
|
|
const auto result = QJsonDocument::fromJson(content.toUtf8()).object();
|
||
|
|
const auto unchangedLine = [&](const QString &key) {
|
||
|
|
const int start = source.indexOf(QLatin1Char('"') + key + QLatin1Char('"'));
|
||
|
|
const int end = source.indexOf(QLatin1Char('\n'), start);
|
||
|
|
return start >= 0 && end > start && content.contains(source.mid(start, end - start));
|
||
|
|
};
|
||
|
|
require(result.value("header") == root.value("header")
|
||
|
|
&& result.value("normalPosList") == root.value("normalPosList")
|
||
|
|
&& result.value("advancedLineList") == root.value("advancedLineList")
|
||
|
|
&& content.contains("\"largeInteger\":18446744073709551615")
|
||
|
|
&& content.contains("\"vendorPoint\":9007199254740993")
|
||
|
|
&& content.contains("\"customLine\":9007199254740993")
|
||
|
|
&& unchangedLine("header") && unchangedLine("normalPosList")
|
||
|
|
&& unchangedLine("advancedLineList") && unchangedLine("rootUnknown"),
|
||
|
|
"deletion payload preserves complete native header, scan, lines and unknown integer/string tokens");
|
||
|
|
}
|
||
|
|
|
||
|
|
void testOneRouteAndRevisit()
|
||
|
|
{
|
||
|
|
Harness h;
|
||
|
|
h.remove("LM1_LM2", false);
|
||
|
|
AgvMapDocument concurrent;
|
||
|
|
QString error;
|
||
|
|
require(concurrent.load(h.service.stored(), &error)
|
||
|
|
&& concurrent.addStation("REMOTE", 6, 4, 0, &error),
|
||
|
|
"unrelated concurrent server change prepared");
|
||
|
|
h.service.replace(concurrent.content());
|
||
|
|
h.save->click();
|
||
|
|
waitFor([&]() { return h.saved == 1 && !h.busy; }, "route deletion must be read back before mapSaved");
|
||
|
|
require(h.service.uploads() == 1 && h.service.reads() == 3
|
||
|
|
&& !h.save->isEnabled() && !h.undo->isEnabled(),
|
||
|
|
"one explicit deletion save has one latest read, one upload and one confirming read");
|
||
|
|
AgvMapDocument stored;
|
||
|
|
require(stored.load(h.service.uploaded(), &error) && !hasRoute(stored, "LM1_LM2")
|
||
|
|
&& hasRoute(stored, "LM1_LM2_reverse") && stored.hasStation("LM2")
|
||
|
|
&& stored.hasStation("REMOTE") && stored.routes().size() == 5,
|
||
|
|
"selected route alone is removed while reverse route, points and concurrent additions survive");
|
||
|
|
requireUntouchedNativeFields(h.service.uploaded());
|
||
|
|
h.changeMap("other_map");
|
||
|
|
h.changeMap("delete_map");
|
||
|
|
h.inspect([](QTableWidget *stations, QTableWidget *routes) {
|
||
|
|
require(rowFor(routes, "LM1_LM2") < 0 && rowFor(routes, "LM1_LM2_reverse") >= 0
|
||
|
|
&& rowFor(stations, "REMOTE") >= 0,
|
||
|
|
"one away/back revisit reads the confirmed map without resurrecting deleted route");
|
||
|
|
});
|
||
|
|
require(h.service.uploads() == 1, "revisiting a saved map performs no hidden upload");
|
||
|
|
qInfo("PASS: single directed route deletion, native preservation, concurrent addition and single map revisit");
|
||
|
|
}
|
||
|
|
|
||
|
|
void testStationCascade()
|
||
|
|
{
|
||
|
|
Harness h;
|
||
|
|
h.remove("LM2", true);
|
||
|
|
h.inspect([](QTableWidget *stations, QTableWidget *routes) {
|
||
|
|
require(rowFor(stations, "LM2") < 0 && routes->rowCount() == 2
|
||
|
|
&& rowFor(routes, "LM3_LM4") >= 0 && rowFor(routes, "LM3_LM4_reverse") >= 0,
|
||
|
|
"station deletion stages every linked forward/reverse route and preserves unrelated pair");
|
||
|
|
});
|
||
|
|
h.save->click();
|
||
|
|
waitFor([&]() { return h.saved == 1 && !h.busy; }, "cascade deletion is persisted and verified");
|
||
|
|
QString error;
|
||
|
|
AgvMapDocument stored;
|
||
|
|
require(stored.load(h.service.stored(), &error) && !stored.hasStation("LM2")
|
||
|
|
&& stored.stations().size() == 3 && stored.routes().size() == 2
|
||
|
|
&& !hasRoute(stored, "LM1_LM2") && !hasRoute(stored, "LM1_LM2_reverse")
|
||
|
|
&& !hasRoute(stored, "LM2_LM3") && !hasRoute(stored, "LM2_LM3_reverse")
|
||
|
|
&& h.service.uploads() == 1,
|
||
|
|
"persisted deletion contains no dangling routes to the removed station");
|
||
|
|
requireUntouchedNativeFields(h.service.uploaded());
|
||
|
|
qInfo("PASS: station deletion cascades only associated directed routes and preserves native data");
|
||
|
|
}
|
||
|
|
|
||
|
|
void testUnconfirmedDeletion(bool station)
|
||
|
|
{
|
||
|
|
Harness h;
|
||
|
|
const QString id = station ? "LM2" : "LM1_LM2";
|
||
|
|
h.remove(id, station);
|
||
|
|
const int readsBefore = h.service.reads();
|
||
|
|
h.service.next(FakeAgvService::AckOnly);
|
||
|
|
h.save->click();
|
||
|
|
waitFor([&]() { return h.acks == 1 && h.service.reads() >= readsBefore + 2; },
|
||
|
|
"ACK-only deletion begins readback verification");
|
||
|
|
require(h.saved == 0 && h.busy && !h.save->isEnabled() && !h.undo->isEnabled(),
|
||
|
|
"upload ACK cannot clear pending deletion or report saved");
|
||
|
|
waitFor([&]() { return !h.busy && h.save->isEnabled(); },
|
||
|
|
"unchanged remote deletion result ends boundedly with retained draft");
|
||
|
|
require(h.saved == 0 && h.undo->isEnabled() && h.service.uploads() == 1
|
||
|
|
&& h.service.reads() > readsBefore + 1 && h.service.reads() <= readsBefore + 7,
|
||
|
|
"failed deletion readback keeps draft after at most six checks and one upload");
|
||
|
|
const QString desired = h.service.uploaded();
|
||
|
|
AgvMapDocument stillOld;
|
||
|
|
QString error;
|
||
|
|
require(stillOld.load(h.service.stored(), &error) && stillOld.hasStation("LM2")
|
||
|
|
&& hasRoute(stillOld, "LM1_LM2"),
|
||
|
|
"ACK-only mock independently proves deleted native objects remain remote");
|
||
|
|
h.changeMap("other_map");
|
||
|
|
h.changeMap("delete_map");
|
||
|
|
h.inspect([=](QTableWidget *stations, QTableWidget *routes) {
|
||
|
|
require(rowFor(station ? stations : routes, id) < 0,
|
||
|
|
"unconfirmed deletion draft survives switching away and back");
|
||
|
|
});
|
||
|
|
h.client.disconnectFromServer();
|
||
|
|
waitFor([&]() { return !h.connected; }, "loopback disconnected to test draft lifetime");
|
||
|
|
h.editor->refreshContext();
|
||
|
|
require(!h.save->isEnabled(), "disconnected deletion cannot be sent");
|
||
|
|
h.client.connectToServer("127.0.0.1", h.port, false);
|
||
|
|
waitFor([&]() { return h.connected; }, "loopback reconnected");
|
||
|
|
h.editor->refreshContext();
|
||
|
|
require(h.save->isEnabled() && h.undo->isEnabled() && h.saved == 0,
|
||
|
|
"reconnection restores the deletion draft without implicit save");
|
||
|
|
for (int index = 0; index < 10; ++index)
|
||
|
|
h.editor->refreshContext();
|
||
|
|
require(h.service.uploads() == 1, "context polling and reconnection never re-upload deletion");
|
||
|
|
|
||
|
|
// Simulate that a previously accepted upload becomes visible after the
|
||
|
|
// bounded check ended. The operator's next explicit retry is idempotent.
|
||
|
|
h.service.replace(desired);
|
||
|
|
h.save->click();
|
||
|
|
waitFor([&]() { return h.saved == 1 && !h.busy; },
|
||
|
|
"explicit retry succeeds even when the target objects are already missing");
|
||
|
|
require(h.service.uploads() == 2 && !h.save->isEnabled() && !h.undo->isEnabled(),
|
||
|
|
"already-missing target retry performs one upload then confirms and clears the draft");
|
||
|
|
qInfo("PASS: %s ACK without deletion retains draft across map revisit/disconnect; explicit retry is idempotent",
|
||
|
|
station ? "station cascade" : "route");
|
||
|
|
}
|
||
|
|
|
||
|
|
void testConflict(int kind)
|
||
|
|
{
|
||
|
|
Harness h;
|
||
|
|
h.remove(kind == 0 ? "LM1_LM2" : "LM2", kind != 0);
|
||
|
|
QString changed = h.service.stored();
|
||
|
|
if (kind == 0) {
|
||
|
|
require(changed.contains("\"vendorRoute\":9007199254740993"), "route conflict fixture custom field present");
|
||
|
|
changed.replace("\"vendorRoute\":9007199254740993", "\"vendorRoute\":9007199254740995");
|
||
|
|
} else if (kind == 1) {
|
||
|
|
changed.replace("\"description\":\"second\"", "\"description\":\"external rename\"");
|
||
|
|
} else {
|
||
|
|
AgvMapDocument latest;
|
||
|
|
QString error;
|
||
|
|
require(latest.load(changed, &error) && latest.addRoute("NEW_ASSOCIATION", "LM4", "LM2", false, &error),
|
||
|
|
"new external route to locally deleted station prepared");
|
||
|
|
changed = latest.content();
|
||
|
|
}
|
||
|
|
h.service.replace(changed);
|
||
|
|
const int reads = h.service.reads();
|
||
|
|
h.save->click();
|
||
|
|
waitFor([&]() { return h.service.reads() == reads + 1 && !h.busy && h.save->isEnabled(); },
|
||
|
|
"fresh latest-map deletion conflict returns an editable local draft");
|
||
|
|
require(h.service.uploads() == 0 && h.saved == 0 && h.undo->isEnabled()
|
||
|
|
&& h.service.stored() == changed,
|
||
|
|
"changed deleted object or new station association prevents upload and preserves external map");
|
||
|
|
h.inspect([=](QTableWidget *stations, QTableWidget *routes) {
|
||
|
|
require(rowFor(kind == 0 ? routes : stations, kind == 0 ? "LM1_LM2" : "LM2") < 0,
|
||
|
|
"conflict leaves the original local deletion available for undo or operator review");
|
||
|
|
});
|
||
|
|
qInfo("PASS: fresh-map conflict blocks deletion upload (%s)",
|
||
|
|
kind == 0 ? "route unknown-field change" : kind == 1 ? "station name change" : "new associated route");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
int main(int argc, char *argv[])
|
||
|
|
{
|
||
|
|
QApplication app(argc, argv);
|
||
|
|
Q_INIT_RESOURCE(resources);
|
||
|
|
testOneRouteAndRevisit();
|
||
|
|
testStationCascade();
|
||
|
|
testUnconfirmedDeletion(false);
|
||
|
|
testUnconfirmedDeletion(true);
|
||
|
|
testConflict(0);
|
||
|
|
testConflict(1);
|
||
|
|
testConflict(2);
|
||
|
|
std::cout << "PASS AGV delete integration: route and station/cascade persistence, readback, native preservation,"
|
||
|
|
" single revisit, disconnect/draft retention, idempotent explicit retry and external conflicts;"
|
||
|
|
" loopback only, no vehicle writes" << std::endl;
|
||
|
|
return 0;
|
||
|
|
}
|