CmvrControlStation/tests/agvmapeditor_failure_test.cpp

687 lines
34 KiB
C++
Raw Permalink Normal View History

#include "agvmapdocument.h"
#include "agvmapeditorwidget.h"
#include "agvmapviewwidget.h"
#include "robotclient.h"
#include <QApplication>
#include <QCheckBox>
#include <QComboBox>
#include <QDialog>
#include <QDialogButtonBox>
#include <QDoubleSpinBox>
#include <QFile>
#include <QLabel>
#include <QLineEdit>
#include <QPointer>
#include <QPushButton>
#include <QTableWidget>
#include <QTimer>
#include <QVBoxLayout>
#include <QVector>
#include <functional>
namespace {
void require(bool condition, const char *message)
{
if (!condition) {
QFile failure(QStringLiteral("agvmapeditor_failure_assertion.txt"));
if (failure.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
failure.write(message);
failure.write("\n");
failure.close();
}
qFatal("AGV editor failure test failed: %s", message);
}
}
struct Request {
quint64 id;
QString device;
QString map;
QString content;
};
QString mapContent(const QString &name)
{
return QStringLiteral(R"JSON({
"header":{"mapType":"2D-Map","mapName":"%1","resolution":0.05},
"unknownInteger":9007199254740993,
"normalPosList":[{"x":1,"y":2}],
"advancedPointList":[
{"className":"LandMark","instanceName":"HOME","pos":{"x":0,"y":0}},
{"className":"LandMark","instanceName":"B","pos":{"x":2,"y":0}}
],
"advancedCurveList":[]
})JSON").arg(name);
}
void fillNextModal(const std::function<bool(QDialog *)> &fill)
{
auto *timer = new QTimer(qApp);
timer->setInterval(10);
QObject::connect(timer, &QTimer::timeout, timer, [timer, fill]() {
auto *dialog = qobject_cast<QDialog *>(QApplication::activeModalWidget());
if (!dialog || !fill(dialog))
return;
timer->stop();
timer->deleteLater();
});
timer->start();
QTimer::singleShot(7000, qApp, [guard = QPointer<QTimer>(timer)]() {
if (guard && guard->isActive())
qFatal("AGV editor failure test failed: expected dialog did not open");
});
}
void fillNextStation(const QString &id, double x, double y, const QString &displayName = QString())
{
fillNextModal([=](QDialog *dialog) {
if (dialog->objectName() != QStringLiteral("agvStationDialog"))
return false;
auto *name = dialog->findChild<QLineEdit *>("agvNewStationId");
auto *label = dialog->findChild<QLineEdit *>("agvNewStationName");
auto *xSpin = dialog->findChild<QDoubleSpinBox *>("agvNewStationX");
auto *ySpin = dialog->findChild<QDoubleSpinBox *>("agvNewStationY");
auto *box = dialog->findChild<QDialogButtonBox *>();
require(name && label && xSpin && ySpin && box, "station dialog controls exist");
name->setText(id);
label->setText(displayName);
xSpin->setValue(x);
ySpin->setValue(y);
box->button(QDialogButtonBox::Ok)->click();
return true;
});
}
void fillNextRoute(const QString &id)
{
fillNextModal([=](QDialog *dialog) {
if (dialog->objectName() != QStringLiteral("agvRouteDialog"))
return false;
auto *name = dialog->findChild<QLineEdit *>("agvNewRouteId");
auto *both = dialog->findChild<QCheckBox *>("agvNewRouteBidirectional");
auto *box = dialog->findChild<QDialogButtonBox *>();
require(name && both && box, "route dialog controls exist");
name->setText(id);
both->setChecked(false);
box->button(QDialogButtonBox::Ok)->click();
return true;
});
}
void inspectNextManager(const std::function<void(QTableWidget *)> &inspect)
{
fillNextModal([=](QDialog *dialog) {
if (dialog->objectName() != QStringLiteral("agvMapObjectsDialog"))
return false;
auto *stations = dialog->findChild<QTableWidget *>("agvMapStationsTable");
require(stations, "manager stations table exists");
inspect(stations);
dialog->accept();
return true;
});
}
QString managerStationName(QTableWidget *stations, const QString &id)
{
for (int row = 0; row < stations->rowCount(); ++row) {
if (stations->item(row, 0)->text() == id)
return stations->item(row, 1)->text();
}
return QString();
}
void renameNextStation(const QString &id, const QString &displayName)
{
fillNextModal([=](QDialog *dialog) {
if (dialog->objectName() != QStringLiteral("agvMapObjectsDialog"))
return false;
auto *stations = dialog->findChild<QTableWidget *>("agvMapStationsTable");
auto *rename = dialog->findChild<QPushButton *>("agvRenameStationButton");
require(stations && rename, "manager rename controls exist");
int selected = -1;
for (int row = 0; row < stations->rowCount(); ++row) {
if (stations->item(row, 0)->text() == id)
selected = row;
}
require(selected >= 0, "existing station can be selected for rename");
stations->setCurrentCell(selected, 0);
fillNextModal([=](QDialog *nameDialog) {
if (nameDialog->objectName() != QStringLiteral("agvStationNameDialog"))
return false;
auto *name = nameDialog->findChild<QLineEdit *>("agvStationName");
auto *box = nameDialog->findChild<QDialogButtonBox *>();
require(name && box, "station name dialog controls exist");
name->setText(displayName);
box->button(QDialogButtonBox::Ok)->click();
return true;
});
// Start the nested dialog after this modal observer stops its timer.
QTimer::singleShot(0, dialog, [dialog, rename]() {
rename->click();
dialog->accept();
});
return true;
});
}
QString documentStationName(const AgvMapDocument &document, const QString &id)
{
for (const QVariant &value : document.stations()) {
if (value.toMap().value("id").toString() == id)
return value.toMap().value("description").toString();
}
return QString();
}
void requireSavedStation(const Request &request, const QString &id)
{
AgvMapDocument document;
QString error;
require(document.load(request.content, &error), "upload contains a valid full map");
require(document.hasStation(id), "upload retains the station draft");
require(request.content.contains(QStringLiteral("9007199254740993")),
"upload preserves unknown integer text");
}
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTimer::singleShot(30000, &app, []() {
qFatal("AGV editor failure test failed: test timed out");
});
RobotClient client;
// Detach outbound worker requests before attaching the editor and observers.
// Every response below is injected in-process; no server is connected.
QObject::disconnect(&client, nullptr, nullptr, nullptr);
QVector<Request> reads;
QVector<Request> writes;
QObject::connect(&client, &RobotClient::agvMapContentRequested,
[&](quint64 id, const QString &device, const QString &map) {
reads.append({id, device, map, QString()});
});
QObject::connect(&client, &RobotClient::agvMapSaveRequested,
[&](quint64 id, const QString &device, const QString &map, const QString &content) {
writes.append({id, device, map, content});
});
QWidget window;
auto *layout = new QVBoxLayout(&window);
auto *view = new AgvMapViewWidget(&window);
auto *editor = new AgvMapEditorWidget(&client, view, &window);
layout->addWidget(editor);
layout->addWidget(view);
window.resize(1050, 700);
bool connected = true;
bool demo = true;
QString selectedMap = QStringLiteral("map_A");
editor->setContextProvider([&]() {
AgvMapEditorWidget::Context context;
context.connected = connected;
context.demo = demo;
context.endpoint = QStringLiteral("offline:50052");
context.deviceId = QStringLiteral("agv_failure_test");
context.mapName = selectedMap;
return context;
});
const auto setViewMap = [&]() {
QImage image(1280, 720, QImage::Format_RGB32);
image.fill(Qt::white);
view->setMapFrame(image, {{"mapId", selectedMap}, {"dimension", 2},
{"resolution", 0.05}, {"renderScale", 1.0},
{"originX", 0.0}, {"originY", 0.0}, {"originTheta", 0.0}});
};
setViewMap();
window.show();
app.processEvents();
editor->refreshContext();
auto *coordinates = editor->findChild<QPushButton *>("agvCoordinateStationButton");
auto *save = editor->findChild<QPushButton *>("agvSaveMapObjectsButton");
auto *undo = editor->findChild<QPushButton *>("agvUndoMapObjectsButton");
auto *manage = editor->findChild<QPushButton *>("agvManageMapObjectsButton");
auto *addStation = editor->findChild<QPushButton *>("agvAddStationButton");
auto *addRoute = editor->findChild<QPushButton *>("agvAddRouteButton");
auto *status = editor->findChild<QLabel *>("agvMapEditorStatus");
require(coordinates && save && undo && manage && addStation && addRoute && status,
"editor controls exist");
int saved = 0;
QObject::connect(editor, &AgvMapEditorWidget::mapSaved, [&]() { ++saved; });
const auto replyRead = [&](const Request &request, bool success,
const QString &content, const QString &detail) {
// A read reply may synchronously open a dialog or append another request.
const Request copy = request;
emit client.agvMapContentReceived(copy.id, copy.device, copy.map,
content, success, detail);
};
const auto replyWrite = [&](const Request &request, bool success, const QString &detail) {
const Request copy = request;
emit client.agvMapSaveFinished(copy.id, copy.device, copy.map, success, detail);
};
coordinates->click();
require(reads.size() == 1 && !coordinates->isEnabled(), "initial map read is pending");
replyRead(reads.last(), false, QString(), QStringLiteral("initial read failed"));
require(coordinates->isEnabled() && !save->isEnabled() && !undo->isEnabled(),
"failed initial read releases busy state without drafts");
require(status->text().contains(QStringLiteral("initial read failed")),
"initial read error is visible");
coordinates->click();
require(reads.size() == 2, "initial read can be retried");
fillNextStation(QStringLiteral("P1"), 3.0, 4.0);
replyRead(reads.last(), true, mapContent(selectedMap), QString());
require(save->isEnabled() && undo->isEnabled(), "retry creates a station draft");
qInfo("PASS: initial map read failure and retry");
save->click();
require(reads.size() == 3 && !save->isEnabled(), "save first requests the latest map");
replyRead(reads.last(), false, QString(), QStringLiteral("save read failed"));
require(save->isEnabled() && undo->isEnabled() && coordinates->isEnabled(),
"failed pre-save read retains draft and releases busy state");
require(writes.isEmpty() && saved == 0, "failed pre-save read never uploads or marks saved");
require(status->text().contains(QStringLiteral("save read failed")),
"pre-save read error is visible");
qInfo("PASS: pre-save read failure preserves drafts");
save->click();
require(reads.size() == 4, "save retries the latest-map read");
replyRead(reads.last(), true, mapContent(selectedMap), QString());
require(writes.size() == 1 && !save->isEnabled() && !coordinates->isEnabled(),
"successful re-read starts upload and remains busy");
requireSavedStation(writes.last(), QStringLiteral("P1"));
replyWrite(writes.last(), false, QStringLiteral("upload failed"));
require(save->isEnabled() && undo->isEnabled() && coordinates->isEnabled() && saved == 0,
"failed upload retains draft and does not emit mapSaved");
require(status->text().contains(QStringLiteral("upload failed")), "upload error is visible");
save->click();
require(reads.size() == 5, "upload retry re-reads latest full map");
replyRead(reads.last(), true, mapContent(selectedMap), QString());
require(writes.size() == 2, "upload can be retried");
requireSavedStation(writes.last(), QStringLiteral("P1"));
const Request acceptedPreSaveRead = reads.last();
replyWrite(writes.last(), true, QString());
require(saved == 0 && reads.size() == 6 && !save->isEnabled() && !undo->isEnabled()
&& !coordinates->isEnabled() && !manage->isEnabled(),
"upload ACK starts readback verification and keeps all edit controls busy");
const Request acceptedVerificationRead = reads.last();
require(acceptedVerificationRead.id != acceptedPreSaveRead.id,
"readback verification uses a fresh request token");
replyRead(acceptedPreSaveRead, true, writes.last().content, QString());
require(saved == 0 && !save->isEnabled() && !coordinates->isEnabled(),
"old pre-save read cannot commit an accepted upload");
replyRead(acceptedVerificationRead, true, writes.last().content, QString());
require(saved == 1 && !save->isEnabled() && !undo->isEnabled() && coordinates->isEnabled(),
"verified successful retry clears drafts and emits mapSaved once");
qInfo("PASS: upload failure preserves drafts and successful retry commits them");
fillNextStation(QStringLiteral("P2"), 5.0, 6.0);
coordinates->click();
require(save->isEnabled(), "second station is a draft");
save->click();
require(reads.size() == 7, "save re-read is pending before disconnect");
const Request disconnectedRead = reads.last();
connected = false;
editor->refreshContext();
require(!save->isEnabled() && !coordinates->isEnabled(), "disconnect disables editing");
replyRead(disconnectedRead, true, mapContent(QStringLiteral("map_A")), QString());
require(writes.size() == 2 && saved == 1, "old read after disconnect cannot upload");
connected = true;
editor->refreshContext();
require(save->isEnabled() && undo->isEnabled(), "returning to original context retains draft");
replyRead(disconnectedRead, true, mapContent(QStringLiteral("map_A")), QString());
require(writes.size() == 2, "old read remains invalid after reconnect");
save->click();
require(reads.size() == 8, "save re-read is pending before map change");
const Request previousMapRead = reads.last();
selectedMap = QStringLiteral("map_B");
setViewMap();
editor->refreshContext();
require(coordinates->isEnabled() && !save->isEnabled(), "new map releases busy state");
replyRead(previousMapRead, true, mapContent(QStringLiteral("map_A")), QString());
require(writes.size() == 2 && saved == 1, "old map read cannot upload after map change");
coordinates->click();
require(reads.size() == 9 && reads.last().map == selectedMap, "new map is read normally");
fillNextStation(QStringLiteral("P3"), 7.0, 8.0, QStringLiteral("地图B站点"));
replyRead(reads.last(), true, mapContent(selectedMap), QString());
require(writes.size() == 2 && save->isEnabled() && undo->isEnabled(),
"ordinary new-map read creates a draft without a stale save flag uploading it");
require(status->text().contains(QStringLiteral("地图B站点")), "new-map named station dialog completed");
// A late map_A frame must disable every action while map_B is selected.
selectedMap = QStringLiteral("map_A");
setViewMap();
selectedMap = QStringLiteral("map_B");
editor->refreshContext();
require(!coordinates->isEnabled() && !manage->isEnabled() && !addStation->isEnabled()
&& !addRoute->isEnabled() && !save->isEnabled() && !undo->isEnabled(),
"late frame for another map disables editing save and undo");
const int readsBeforeWrongFrame = reads.size();
undo->click();
save->click();
coordinates->click();
require(reads.size() == readsBeforeWrongFrame && writes.size() == 2,
"wrong-map frame actions cannot consume drafts or request a save");
setViewMap();
editor->refreshContext();
inspectNextManager([](QTableWidget *stations) {
require(managerStationName(stations, "P3") == QStringLiteral("地图B站点")
&& managerStationName(stations, "P2").isEmpty(),
"wrong-map undo cannot remove B draft or expose A draft");
});
manage->click();
selectedMap = QStringLiteral("map_A");
setViewMap();
editor->refreshContext();
require(save->isEnabled() && undo->isEnabled() && reads.size() == 9,
"returning to A restores its draft and history without downloading");
inspectNextManager([](QTableWidget *stations) {
require(managerStationName(stations, "P2") == QStringLiteral("P2")
&& managerStationName(stations, "P3").isEmpty(),
"returning to A restores only A unsaved station");
});
manage->click();
fillNextStation(QStringLiteral("A_TEMP"), 8.0, 9.0);
coordinates->click();
undo->click();
require(save->isEnabled() && undo->isEnabled(), "A undo removes only its latest edit");
save->click();
require(reads.size() == 10 && reads.last().map == selectedMap,
"restored A draft saves against A latest map");
replyRead(reads.last(), true, mapContent(selectedMap), QString());
require(writes.size() == 3 && writes.last().map == selectedMap,
"restored A draft uploads to A rather than B");
AgvMapDocument restoredA;
QString error;
require(restoredA.load(writes.last().content, &error) && restoredA.hasStation("P2")
&& !restoredA.hasStation("A_TEMP") && !restoredA.hasStation("P3"),
"restored A upload contains its retained draft after undo");
replyWrite(writes.last(), true, QString());
require(saved == 1 && reads.size() == 11 && !save->isEnabled() && !undo->isEnabled(),
"restored A ACK retains its draft until verification returns");
replyRead(reads.last(), true, writes.last().content, QString());
require(saved == 2 && !save->isEnabled() && !undo->isEnabled(),
"restored A successful save clears only its draft");
selectedMap = QStringLiteral("map_B");
setViewMap();
editor->refreshContext();
require(save->isEnabled() && undo->isEnabled() && reads.size() == 11,
"B unsaved work survives saving A");
qInfo("PASS: disconnect/map switch discard stale reads and reset pre-save state");
editor->setAutoLoadEnabled(true);
const int automaticStart = reads.size();
selectedMap = QStringLiteral("map_C");
editor->refreshContext();
require(reads.size() == automaticStart && !save->isEnabled() && !undo->isEnabled(),
"automatic load waits for selected map frame and disables old draft actions");
QImage threeDimensionalPreview(1280, 720, QImage::Format_RGB32);
threeDimensionalPreview.fill(Qt::white);
view->setMapFrame(threeDimensionalPreview, {{"mapId", selectedMap}, {"dimension", 3}});
editor->refreshContext();
require(reads.size() == automaticStart && !coordinates->isEnabled(),
"automatic load and editing require valid 2D coordinate projection");
setViewMap();
editor->refreshContext();
require(reads.size() == automaticStart + 1 && reads.last().map == selectedMap,
"ready new map loads automatically while previous map draft stays preserved");
for (int i = 0; i < 20; ++i)
editor->refreshContext();
require(reads.size() == automaticStart + 1, "polling never duplicates an in-flight automatic load");
const Request failedAutomaticRead = reads.last();
replyRead(failedAutomaticRead, false, QString(), QStringLiteral("automatic read failed"));
for (int i = 0; i < 20; ++i)
editor->refreshContext();
require(reads.size() == automaticStart + 1 && coordinates->isEnabled() && !save->isEnabled(),
"failed automatic load releases busy state and is not retried by polling");
require(status->text().contains(QStringLiteral("automatic read failed")),
"automatic read failure stays visible");
coordinates->click();
require(reads.size() == automaticStart + 2, "explicit editor action retries a failed automatic load");
replyRead(reads.last(), false, QString(), QStringLiteral("explicit retry failed"));
editor->refreshContext();
require(reads.size() == automaticStart + 2, "failed explicit retry does not re-enable automatic polling");
selectedMap = QStringLiteral("map_B");
setViewMap();
editor->refreshContext();
require(reads.size() == automaticStart + 2 && save->isEnabled() && undo->isEnabled(),
"failed load of C leaves B draft available when returning");
undo->click();
require(!save->isEnabled() && !undo->isEnabled(), "B undo remains usable after another map's failure");
selectedMap = QStringLiteral("map_D");
setViewMap();
editor->refreshContext();
require(reads.size() == automaticStart + 3, "another map receives its own automatic attempt");
replyRead(reads.last(), true, mapContent(selectedMap), QString());
require(coordinates->isEnabled() && !save->isEnabled() && !undo->isEnabled(),
"automatic load shows existing objects without creating a draft");
require(status->text().contains(QStringLiteral("2 个站点")),
"automatic load includes all existing stations");
for (int i = 0; i < 20; ++i)
editor->refreshContext();
require(reads.size() == automaticStart + 3, "successful automatic load is not repeated");
selectedMap = QStringLiteral("map_C");
setViewMap();
editor->refreshContext();
selectedMap = QStringLiteral("map_D");
setViewMap();
editor->refreshContext();
require(reads.size() == automaticStart + 4,
"clean map revisit downloads current content rather than retaining a cache");
replyRead(reads.last(), true, mapContent(selectedMap), QString());
selectedMap = QStringLiteral("map_C");
setViewMap();
editor->refreshContext();
require(reads.size() == automaticStart + 4, "temporary map changes never retry a failed map automatically");
connected = false;
editor->refreshContext();
connected = true;
editor->refreshContext();
require(reads.size() == automaticStart + 5 && !coordinates->isEnabled(),
"a new connection permits a new automatic attempt");
replyRead(failedAutomaticRead, true, mapContent(selectedMap), QString());
require(!coordinates->isEnabled() && writes.size() == 3 && saved == 2,
"old automatic reply cannot replace a newer same-map request");
replyRead(reads.last(), true, mapContent(selectedMap), QString());
require(coordinates->isEnabled() && !save->isEnabled() && writes.size() == 3 && saved == 2,
"fresh automatic reply loads without uploading or marking a save");
selectedMap = QStringLiteral("map_D");
setViewMap();
editor->refreshContext();
require(reads.size() == automaticStart + 6, "returning to a successfully loaded map downloads fresh content");
AgvMapDocument freshMap;
require(freshMap.load(mapContent(selectedMap), &error), "prepare updated map content");
require(freshMap.addStation(QStringLiteral("REMOTE_D"), 9.0, 10.0, 0.0, &error),
"independent station was added since the last visit");
require(freshMap.addRoute(QStringLiteral("HOME_REMOTE_D"), QStringLiteral("HOME"),
QStringLiteral("REMOTE_D"), false, &error),
"independent route was added since the last visit");
replyRead(reads.last(), true, freshMap.content(), QString());
require(status->text().contains(QStringLiteral("3 个站点")) && !save->isEnabled(),
"returning to a map displays new remote objects");
require(status->text().contains(QStringLiteral("1 条路线")),
"automatic load includes existing routes as well as stations");
for (int i = 0; i < 20; ++i)
editor->refreshContext();
require(reads.size() == automaticStart + 6, "loaded context suppresses polling after a successful revisit");
connected = false;
editor->refreshContext();
connected = true;
editor->refreshContext();
require(reads.size() == automaticStart + 7, "reconnect invalidates the clean loaded document");
replyRead(reads.last(), true, freshMap.content(), QString());
fillNextStation(QStringLiteral("P4"), 12.0, 13.0);
coordinates->click();
require(save->isEnabled(), "station draft exists before automatic-mode reconnect");
connected = false;
editor->refreshContext();
connected = true;
editor->refreshContext();
require(reads.size() == automaticStart + 7 && save->isEnabled() && undo->isEnabled(),
"reconnect preserves dirty document context and does not auto-replace drafts");
qInfo("PASS: automatic map load readiness, one attempt, explicit retry and stale replies");
undo->click();
require(!save->isEnabled(), "remove new-station draft before testing an existing name edit");
renameNextStation(QStringLiteral("HOME"), QStringLiteral("本地上料站"));
manage->click();
require(save->isEnabled() && undo->isEnabled(), "existing station rename creates an editable draft");
save->click();
require(reads.size() == automaticStart + 8 && !save->isEnabled(),
"name save first re-reads complete latest map");
AgvMapDocument external;
require(external.load(freshMap.content(), &error)
&& external.setStationName("HOME", QStringLiteral("外部修改的站点"), &error),
"prepare concurrent station name update");
replyRead(reads.last(), true, external.content(), QString());
require(writes.size() == 3 && saved == 2 && save->isEnabled() && undo->isEnabled(),
"concurrent name conflict refuses upload and retains local editable draft");
require(status->text().contains(QStringLiteral("名称已被其他操作修改")),
"name conflict is visible to the operator");
inspectNextManager([](QTableWidget *stations) {
require(managerStationName(stations, "HOME") == QStringLiteral("本地上料站"),
"conflict does not replace user's name with remote name");
});
manage->click();
require(external.setStationName("HOME", QString(), &error)
&& external.setStationName("B", QStringLiteral("远端等待站"), &error),
"prepare latest map without conflicting name but with unrelated remote name edit");
save->click();
require(reads.size() == automaticStart + 9, "conflicted name draft can retry latest-map read");
replyRead(reads.last(), true, external.content(), QString());
require(writes.size() == 4 && writes.last().map == selectedMap && !save->isEnabled(),
"nonconflicting name merge starts a save for the active map");
AgvMapDocument merged;
require(merged.load(writes.last().content, &error)
&& documentStationName(merged, "HOME") == QStringLiteral("本地上料站")
&& documentStationName(merged, "B") == QStringLiteral("远端等待站")
&& merged.hasStation("REMOTE_D") && !merged.hasStation("P4"),
"name rebase combines local rename with latest unrelated remote changes");
replyWrite(writes.last(), true, QString());
require(saved == 2 && !save->isEnabled() && !undo->isEnabled(),
"name upload ACK does not clear the pending name edit");
replyRead(reads.last(), true, writes.last().content, QString());
require(saved == 3 && !save->isEnabled() && !undo->isEnabled() && manage->isEnabled(),
"successful name upload clears dirty name and history exactly once");
inspectNextManager([](QTableWidget *stations) {
require(managerStationName(stations, "HOME") == QStringLiteral("本地上料站")
&& managerStationName(stations, "B") == QStringLiteral("远端等待站"),
"saved manager shows local and latest remote names");
});
manage->click();
qInfo("PASS: station name conflict preserves draft and nonconflicting latest-map merge saves");
// ACK success is provisional: a lost verification read must leave the
// original local additions available, even if the write reached the robot.
editor->setAutoLoadEnabled(false);
const QString beforeVerificationDisconnect = writes.last().content;
fillNextStation(QStringLiteral("VERIFY12"), 14.0, 15.0, QStringLiteral("验证中断站点"));
coordinates->click();
fillNextRoute(QStringLiteral("VERIFY_ROUTE"));
addRoute->click();
emit view->stationSelected(QStringLiteral("HOME"));
emit view->stationSelected(QStringLiteral("B"));
const int writesBeforeVerificationDisconnect = writes.size();
const int readsBeforeVerificationDisconnect = reads.size();
save->click();
require(reads.size() == readsBeforeVerificationDisconnect + 1,
"verification-disconnect save starts by downloading latest map");
replyRead(reads.last(), true, beforeVerificationDisconnect, QString());
require(writes.size() == writesBeforeVerificationDisconnect + 1,
"verification-disconnect save uploads one merged map");
const Request uploadedBeforeDisconnect = writes.last();
requireSavedStation(uploadedBeforeDisconnect, QStringLiteral("VERIFY12"));
require(uploadedBeforeDisconnect.content.contains(QStringLiteral("VERIFY_ROUTE")),
"verification candidate includes station and route drafts");
replyWrite(uploadedBeforeDisconnect, true, QString());
require(saved == 3 && reads.size() == readsBeforeVerificationDisconnect + 2
&& !save->isEnabled() && !undo->isEnabled() && !coordinates->isEnabled(),
"accepted upload remains pending while its fresh readback is outstanding");
const Request verificationBeforeDisconnect = reads.last();
connected = false;
editor->refreshContext();
replyRead(verificationBeforeDisconnect, true, uploadedBeforeDisconnect.content, QString());
require(saved == 3 && !save->isEnabled(),
"late verification after disconnect cannot commit or mark drafts saved");
connected = true;
editor->refreshContext();
require(save->isEnabled() && undo->isEnabled() && coordinates->isEnabled(),
"reconnecting restores station and route drafts after interrupted verification");
const int writesBeforeConfirmedRetry = writes.size();
save->click();
const Request retryPreSaveRead = reads.last();
// This simulates an ACKed write that really reached the vehicle before
// its readback was interrupted. Explicit retry must not reject its own IDs.
replyRead(retryPreSaveRead, true, uploadedBeforeDisconnect.content, QString());
require(writes.size() == writesBeforeConfirmedRetry + 1,
"explicit retry merges already persisted station and route idempotently and uploads once");
replyWrite(writes.last(), true, QString());
const Request retryVerificationRead = reads.last();
replyRead(verificationBeforeDisconnect, true, uploadedBeforeDisconnect.content, QString());
require(saved == 3 && !save->isEnabled(),
"old verification token remains invalid after reconnect and a newer save");
replyRead(retryVerificationRead, true, writes.last().content, QString());
require(saved == 4 && !save->isEnabled() && !undo->isEnabled() && coordinates->isEnabled(),
"matching retry readback commits the retained draft exactly once");
const QString beforeVerificationMapChange = writes.last().content;
fillNextStation(QStringLiteral("VERIFY13"), 16.0, 17.0);
coordinates->click();
save->click();
replyRead(reads.last(), true, beforeVerificationMapChange, QString());
const Request uploadedBeforeMapChange = writes.last();
replyWrite(uploadedBeforeMapChange, true, QString());
const Request verificationBeforeMapChange = reads.last();
require(saved == 4 && !save->isEnabled() && !undo->isEnabled(),
"map-change verification starts without clearing its local draft");
const QString verificationMap = selectedMap;
selectedMap = QStringLiteral("map_E");
setViewMap();
editor->refreshContext();
replyRead(verificationBeforeMapChange, true, uploadedBeforeMapChange.content, QString());
require(saved == 4 && !save->isEnabled() && coordinates->isEnabled(),
"verification reply for the previous map cannot commit in a different context");
selectedMap = verificationMap;
setViewMap();
editor->refreshContext();
require(save->isEnabled() && undo->isEnabled(),
"returning to the interrupted verification map restores its unsaved station");
const int writesBeforeMapRetry = writes.size();
save->click();
replyRead(reads.last(), true, uploadedBeforeMapChange.content, QString());
require(writes.size() == writesBeforeMapRetry + 1,
"map-context retry accepts its already persisted station without duplicating it");
replyWrite(writes.last(), true, QString());
replyRead(reads.last(), true, writes.last().content, QString());
require(saved == 5 && !save->isEnabled() && !undo->isEnabled(),
"new verification after returning to the map commits once");
qInfo("PASS: upload ACK readback, stale verification, disconnect/context retention and idempotent explicit retry");
fillNextStation(QStringLiteral("VERIFY14"), 18.0, 19.0);
coordinates->click();
const int writesBeforeWrongHeader = writes.size();
save->click();
const Request expectedMapRead = reads.last();
require(expectedMapRead.map == selectedMap, "pre-save request envelope identifies the selected map");
replyRead(expectedMapRead, true, mapContent(QStringLiteral("wrong_map")), QString());
require(writes.size() == writesBeforeWrongHeader && saved == 5
&& save->isEnabled() && undo->isEnabled() && coordinates->isEnabled(),
"correct envelope with a different header map name rejects upload and preserves the dirty draft");
qInfo("PASS: pre-save payload header identity protects against a wrong-map response");
qInfo("PASS: AGV editor offline failure handling tests");
return 0;
}