CmvrControlStation/tests/taskapi_test.cpp

67 lines
7.9 KiB
C++
Raw Normal View History

// No QApplication, network, SDK initialization or hardware commands.
#include "../src/taskapi.h"
#include <QJsonDocument>
#include <QFile>
#include <QDir>
#include <iostream>
#include <stdexcept>
#define CHECK(x) do{if(!(x))throw std::runtime_error(#x);}while(false)
QJsonObject task(const QString& action,const QJsonObject& request={}){
const bool native=action.startsWith(QString::fromUtf8("接口 · "));
return {{"kind","task"},{"name",action},{"deviceType",QString::fromUtf8("机械臂")},{"deviceId","aubo_arm"},{"action",action},
{"parameters",native?QJsonObject{{"service","cmvr.api.ArmService"},{"method",action.mid(QString::fromUtf8("接口 · ").size())},{"request",request},{"timeoutMs",180000}}:QJsonObject{{"positions",QJsonArray{0,0,0,0,0,0}},{"velocity",.1},{"acceleration",.5}}},{"waitMs",3000},{"waitUnit","s"}};
}
QJsonObject flow(const QJsonArray& nodes){return {{"version",1},{"groups",QJsonArray{QJsonObject{{"name","offline example"},{"nodes",nodes}}}}};}
int main(){try{
QString error;
const auto catalog=TaskApi::catalog();CHECK(catalog.value("services").toArray().size()==8);
int unary=0,streams=0;for(const auto& service:catalog.value("services").toArray())for(const auto& method:service.toObject().value("methods").toArray()){
const auto m=method.toObject();if(m.value("clientStreaming").toBool()||m.value("serverStreaming").toBool())++streams;else ++unary;
CHECK(catalog.value("messages").toObject().contains(m.value("requestType").toString()));}
CHECK(unary>100 && streams>0);
CHECK(TaskApi::definition("cmvr.api.MotorService","setMode").value("editorFields").toArray().contains("mode"));
CHECK(!TaskApi::definition("cmvr.api.MotorService","setMode").value("editorFields").toArray().contains("q"));
CHECK(TaskApi::definition("cmvr.api.MotorService","setLimitQd").value("editorFields").toArray().contains("qd"));
CHECK(!TaskApi::definition("cmvr.api.ArmService","ApplyUserCoordinateSystem").value("editorFields").toArray().contains("coordinate"));
for(const auto& adapter:catalog.value("clientAdapters").toArray())CHECK(adapter.toObject().contains("parametersSchema"));
auto legacy=flow(QJsonArray{task("MoveJ")});CHECK(TaskApi::normalize(legacy,error));CHECK(!legacy.contains("version"));CHECK(TaskApi::normalize(legacy,error));CHECK(!legacy.contains("version"));
auto exported=legacy;CHECK(TaskApi::serverDocument(exported,error));
const auto exportedNode=exported.value("groups").toArray()[0].toObject().value("nodes").toArray()[0].toObject();
CHECK(exportedNode.value("operationId")=="cmvr.api.ArmService.moveJ");
CHECK(exportedNode.value("execution").toObject().value("request").toObject().value("target").toObject().value("position").toArray().size()==6);
QJsonArray friendly;
for(const auto& value:catalog.value("clientAdapters").toArray()){
auto a=value.toObject();QJsonObject p;auto props=a.value("parametersSchema").toObject().value("properties").toObject();
for(auto it=props.begin();it!=props.end();++it){const auto t=it.value().toObject().value("type").toString();p.insert(it.key(),t=="array"?QJsonValue(QJsonArray{.1,.2}):t=="string"?QJsonValue("example"):QJsonValue(it.key()=="sampleRate"?48000:it.key()=="channels"?1:.5));}
friendly.append(QJsonObject{{"kind","task"},{"name",a.value("action")},{"deviceType",a.value("deviceType")},{"deviceId","offline-device"},{"action",a.value("action")},{"parameters",p},{"waitMs",0},{"waitUnit","ms"}});
}
auto allExported=flow(friendly);CHECK(TaskApi::serverDocument(allExported,error));
for(const auto& value:allExported.value("groups").toArray()[0].toObject().value("nodes").toArray()){
auto n=value.toObject();if(n.value("execution").toObject().value("kind")=="client_adapter")CHECK(!n.value("serverCalls").toArray().isEmpty());
}
QFile exportFile("docs/task-flow/example.server-export.json");CHECK(exportFile.open(QIODevice::WriteOnly));exportFile.write(QJsonDocument(allExported).toJson(QJsonDocument::Indented));exportFile.close();
auto again=legacy;CHECK(TaskApi::normalize(again,error));CHECK(again==legacy);
auto simple=TaskApi::execution(QString::fromUtf8("机械臂"),QString::fromUtf8("接口 · torqueOn"),"arm-b",{{"request",QJsonObject()}});
CHECK(simple.value("request").toObject().value("device_id")=="arm-b");CHECK(!simple.value("request").toObject().contains("header"));
CHECK(TaskApi::validateRequest("cmvr.api.ArmService","torqueOn",simple.value("request").toObject(),error));
auto apply=task(QString::fromUtf8("接口 · ApplyUserCoordinateSystem"),{{"name",QString::fromUtf8("测试坐标系")},{"expected_revision","0"}});
auto pose=task(QString::fromUtf8("接口 · moveJPose"),{{"user_coordinate_name",QString::fromUtf8("测试坐标系")},{"user_coordinate_revision","0"},{"target",QJsonObject{{"x",.1},{"y",.2},{"z",.3},{"rx",0},{"ry",0},{"rz",0}}},{"options",QJsonObject{{"velocity",.1},{"acceleration",.5}}}});
auto sample=flow(QJsonArray{apply,QJsonObject{{"kind","loop"},{"name","repeat"},{"count",2},{"children",QJsonArray{pose}}}});
CHECK(TaskApi::normalize(sample,error));
auto tamper=sample;auto groups=tamper.value("groups").toArray();auto group=groups[0].toObject();auto nodes=group.value("nodes").toArray();auto node=nodes[0].toObject();auto ex=node.value("execution").toObject();ex.insert("method","torqueOn");node.insert("execution",ex);nodes[0]=node;group.insert("nodes",nodes);groups[0]=group;tamper.insert("groups",groups);CHECK(!TaskApi::normalize(tamper,error));
auto unknown=flow(QJsonArray{task("unknown")});CHECK(!TaskApi::normalize(unknown,error));
auto invalid=flow(QJsonArray{task(QString::fromUtf8("接口 · moveJPose"),{{"not_a_field",1}})});CHECK(!TaskApi::normalize(invalid,error));
auto tooMany=flow(QJsonArray{QJsonObject{{"kind","loop"},{"count",100000},{"children",QJsonArray{task("MoveJ"),task("MoveJ")}}}});CHECK(!TaskApi::normalize(tooMany,error));
auto badPolicy=sample;badPolicy.insert("executionPolicy",QJsonObject{{"onError","continue"}});CHECK(!TaskApi::normalize(badPolicy,error));
QString detail;CHECK(!TaskApi::responseSucceeded(true,R"({"header":{"success":false,"errorMessage":"IK failed"}})",detail));CHECK(detail=="IK failed");
CHECK(TaskApi::responseSucceeded(true,R"({"success":true})",detail));CHECK(!TaskApi::responseSucceeded(false,"transport failure",detail));CHECK(!TaskApi::responseSucceeded(true,"{}",detail));
auto nan=flow(QJsonArray{task(QString::fromUtf8("接口 · moveL"),{{"target",QJsonObject{{"x","NaN"}}}})});CHECK(!TaskApi::normalize(nan,error));
auto fixture=sample;auto fixedGroups=fixture.value("groups").toArray();auto fixedGroup=fixedGroups[0].toObject();fixedGroup.insert("id","example-group");auto fixedNodes=fixedGroup.value("nodes").toArray();auto fixedApply=fixedNodes[0].toObject();fixedApply.insert("id","example-apply");fixedNodes[0]=fixedApply;auto fixedLoop=fixedNodes[1].toObject();fixedLoop.insert("id","example-loop");auto fixedChildren=fixedLoop.value("children").toArray();auto fixedPose=fixedChildren[0].toObject();fixedPose.insert("id","example-movej-pose");fixedChildren[0]=fixedPose;fixedLoop.insert("children",fixedChildren);fixedNodes[1]=fixedLoop;fixedGroup.insert("nodes",fixedNodes);fixedGroups[0]=fixedGroup;fixture.insert("groups",fixedGroups);sample=fixture;
QDir().mkpath("docs/task-flow");
for(const auto& pair:QList<QPair<QString,QJsonObject>>{{"task_operations.json",catalog},{"example.task.json",sample}}){QFile file("docs/task-flow/"+pair.first);CHECK(file.open(QIODevice::WriteOnly));file.write(QJsonDocument(pair.second).toJson(QJsonDocument::Indented));}
QFile saved("bin/release/data/task_orchestration.json");if(saved.open(QIODevice::ReadOnly)){auto doc=QJsonDocument::fromJson(saved.readAll()).object();CHECK(TaskApi::normalize(doc,error));}
std::cout<<"PASS: "<<unary<<" unary interfaces, "<<streams<<" streaming definitions; typed descriptors, legacy migration, id stability, header placement, loop limits, ambiguity rejection, business errors, existing-file read-only validation and example/catalog export; no network or hardware calls\n";
}catch(const std::exception& e){std::cerr<<"FAIL: "<<e.what()<<"\n";return 1;}return 0;}