CmvrControlStation/tests/inspector_refresh_test.cpp
linbo ad1a42c761 feat: support multi-robot workflows and sync server protocol
Add independent robot connections, per-node device binding and isolated demo state. Improve canvas multi-selection, undo and parameter layouts. Remove remote configuration management and align protos with cmvr-es c8b71b2b. Include Release startup and loopback regression coverage.
2026-11-25 17:25:39 +08:00

76 lines
6.1 KiB
C++

// Reproduce repeated device/model refreshes against the actual inspector.
// Includes the implementation for private role constants, without a RobotClient.
#include "../src/taskorchestrationwidget.cpp"
#include <QTabWidget>
#include <QPointer>
#include <iostream>
#include <stdexcept>
static void check(bool value,const char* message){if(!value)throw std::runtime_error(message);}
int main(int argc,char** argv){
QApplication app(argc,argv);ApplicationStyle::install(app);
try{
TaskOrchestrationWidget owner;
const QVariantMap metadata{{"jointNames",QStringList{"a","b","c","d","e","f"}}};
owner.setDeviceInfo("test_arm",metadata);
QVariantList devices{QVariantMap{{"deviceId","test_arm"},{"type",6}}};
owner.setAvailableDevices(devices);
QTreeWidget* tree=nullptr;
for(auto* candidate:owner.findChildren<QTreeWidget*>())if(candidate->columnCount()==6)tree=candidate;
check(tree,"hidden workflow tree exists");
auto makeNode=[&](const QString& id,double angle){
auto* item=new QTreeWidgetItem;
item->setText(0,id);item->setData(0,NodeIdRole,id);item->setData(0,KindRole,"task");
item->setData(0,DeviceTypeRole,QStringLiteral("机械臂"));item->setData(0,DeviceIdRole,"test_arm");
item->setData(0,ActionRole,QStringLiteral("接口 · moveJ"));
const QJsonObject request{{"target",QJsonObject{{"position",QJsonArray{angle,0,0,0,0,0}}}}};
item->setData(0,ParametersRole,QString::fromUtf8(QJsonDocument(QJsonObject{{"service","cmvr.api.ArmService"},{"method","moveJ"},{"request",request}}).toJson()));
tree->addTopLevelItem(item);return item;
};
auto* first=makeNode("first-node",0.);tree->setCurrentItem(first);
TaskProtoEditor* editor=nullptr;for(auto* widget:owner.findChildren<QWidget*>())if(auto* candidate=dynamic_cast<TaskProtoEditor*>(widget)){editor=candidate;break;}check(editor,"inspector editor exists");
auto* tabs=editor->findChild<QTabWidget*>();check(tabs&&tabs->count()>=2,"motion tabs exist");
int motion=-1;for(int i=0;i<tabs->count();++i)if(tabs->tabText(i)==QStringLiteral("运动设置"))motion=i;
check(motion>=0,"motion tab exists");tabs->setCurrentIndex(motion);
auto* table=editor->findChild<QTableWidget*>("taskJointTable");check(table,"joint editor exists");
auto* angle=editor->findChild<QLineEdit*>("jointTarget_0");check(angle,"joint input exists");angle->setText("12.");
QPointer<QTabWidget> originalTabs=tabs;
for(int i=0;i<5;++i){owner.setAvailableDevices(devices);first->setData(0,RunStateRole,QString::number(i));QApplication::processEvents();}
check(originalTabs&&originalTabs->currentIndex()==motion,"polls/model refreshes keep the live motion tab");
check(angle->text()=="12.","partial joint angle survives repeated refreshes");
devices.append(QVariantMap{{"deviceId","second_arm"},{"type",6}});owner.setAvailableDevices(devices);
check(originalTabs&&originalTabs->currentIndex()==motion,"device inventory change preserves live form");
owner.setDeviceInfo("test_arm",{{"jointLimits",QVariantMap{{"a",QVariantMap{{"lower",-2.0},{"upper",2.0}}}}}});
tabs=editor->findChild<QTabWidget*>();table=editor->findChild<QTableWidget*>("taskJointTable");
check(tabs&&tabs->tabText(tabs->currentIndex())==QStringLiteral("运动设置"),"metadata rebuild restores active tab");
check(editor->findChild<QLineEdit*>("jointTarget_0")->text()=="12.","metadata rebuild preserves unfinished input");
auto* second=makeNode("second-node",.5);tree->setCurrentItem(second);
table=editor->findChild<QTableWidget*>("taskJointTable");
check(editor->findChild<QLineEdit*>("jointTarget_0")->text()!="12.","different node does not inherit previous draft");
int covered=0;
for(const auto& serviceValue:TaskApi::catalog().value("services").toArray()) {
const auto service=serviceValue.toObject();
for(const auto& methodValue:service.value("methods").toArray()) {
const auto method=methodValue.toObject();
if(method.value("clientStreaming").toBool()||method.value("serverStreaming").toBool())continue;
auto* item=new QTreeWidgetItem;
item->setText(0,QString("interface-%1").arg(covered));item->setData(0,NodeIdRole,item->text(0));item->setData(0,KindRole,"task");
item->setData(0,DeviceTypeRole,service.value("deviceType").toString());item->setData(0,DeviceIdRole,"test_arm");
item->setData(0,ParametersRole,QString::fromUtf8(QJsonDocument(QJsonObject{{"service",service.value("service")},{"method",method.value("method")},{"request",QJsonObject()}}).toJson()));
tree->addTopLevelItem(item);tree->setCurrentItem(item);
auto* currentTabs=editor->findChild<QTabWidget*>();
if(currentTabs&&currentTabs->count()>1)currentTabs->setCurrentIndex(currentTabs->count()-1);
const auto edits=editor->findChildren<QLineEdit*>();
if(!edits.isEmpty())edits.first()->setText("12.");
const auto draft=editor->inputState();
for(int repeat=0;repeat<3;++repeat){devices[0]=QVariantMap{{"deviceId","test_arm"},{"type",6},{"poll",covered*3+repeat}};owner.setAvailableDevices(devices);item->setData(0,RunStateRole,QString::number(repeat));}
check(editor->inputState()==draft,qPrintable("poll reset: "+service.value("service").toString()+"."+method.value("method").toString()));
owner.setDeviceInfo("test_arm",{{"metadataRevision",++covered}});
check(editor->inputState()==draft,qPrintable("metadata reset: "+service.value("service").toString()+"."+method.value("method").toString()));
}
}
check(covered==123,"all unary interface nodes covered");
std::cout<<"PASS: "<<covered<<" unary node forms across 8 device categories retain active tabs and partial input on inventory/model/metadata refresh; node drafts isolated; no RPCs\n";
return 0;
}catch(const std::exception& error){std::cerr<<"FAIL: "<<error.what()<<"\n";return 1;}
}