import assert from 'node:assert/strict'; import {readFile} from 'node:fs/promises'; import {flatten,execute} from '../docs/task-flow/task-flow.mjs'; const catalog=JSON.parse(await readFile(new URL('../docs/task-flow/task_operations.json',import.meta.url))); const sample=JSON.parse(await readFile(new URL('../docs/task-flow/example.task.json',import.meta.url))); const group=sample.groups[0].id; const steps=flatten(sample,catalog,group); assert.equal(steps.length,3); assert.deepEqual(steps.map(s=>s.iterationPath.map(p=>p.iteration)),[[],[0],[1]]); const plan=structuredClone(sample); const clearWait=nodes=>{for(const n of nodes)if(n.kind==='loop')clearWait(n.children);else n.waitMs=0;}; clearWait(plan.groups[0].nodes); let calls=[]; const result=await execute(plan,catalog,group,{rpc:async(service,method,request)=>{ calls.push({service,method,request});return {header:{success:true}}; }}); assert.equal(result.completedSteps,3); assert.deepEqual(calls.map(c=>c.method),['ApplyUserCoordinateSystem','moveJPose','moveJPose']); assert.equal(calls[1].request.target.x,.1);assert.equal(calls[1].request.user_coordinate_name,'测试坐标系'); assert.notEqual(calls[1].request.header.command_id,calls[2].request.header.command_id); assert.equal(calls[1].request.header.device_id,'aubo_arm'); calls=[]; await assert.rejects(execute(plan,catalog,group,{rpc:async(s,m)=>{calls.push(m);return {header:{success:m!=='moveJPose',error_message:'fake IK failure'}};}}),/fake IK failure/); assert.equal(calls.length,2); const bad=structuredClone(plan);bad.groups[0].nodes[0].execution.method='torqueOn';assert.throws(()=>flatten(bad,catalog,group),/Conflicting/); const duplicate=structuredClone(plan);duplicate.groups[0].nodes[1].id=duplicate.groups[0].nodes[0].id;assert.throws(()=>flatten(duplicate,catalog,group),/duplicate/); const legacy=structuredClone(plan);legacy.groups[0].nodes=[{ id:'legacy',kind:'task',name:'joint',deviceType:'机械臂',deviceId:'aubo_arm',action:'MoveJ',operationId:'client.arm.move_j', parameters:{positions:[0,0,0,0,0,0],velocity:.1,acceleration:.5},waitMs:0,waitUnit:'ms', execution:{kind:'client_adapter',adapter:'client.arm.move_j',deviceId:'aubo_arm',parameters:{positions:[0,0,0,0,0,0],velocity:.1,acceleration:.5},completion:'adapter_callback'} }]; await assert.rejects(execute(legacy,catalog,group,{rpc:async()=>{throw Error('must not call');}}),/Missing adapter/); let invoked=0;await execute(legacy,catalog,group,{adapters:{'client.arm.move_j':async(p)=>{assert.equal(p.positions.length,6);invoked++;return {success:true};}}});assert.equal(invoked,1); const abort=new AbortController();calls=[]; await assert.rejects(execute(plan,catalog,group,{signal:abort.signal,rpc:async(s,m)=>{calls.push(m);abort.abort();return {header:{success:true}};}}),/Canceled/); assert.equal(calls.length,1); const linear=structuredClone(plan),target=linear.groups[0].nodes[1].children[0]; target.action='接口 · moveL';target.operationId='cmvr.api.ArmService.moveL';target.parameters.method='moveL';target.execution.method='moveL'; let motionCalls=0; await execute(linear,catalog,group,{rpc:async(s,m)=>{ if(m==='ExecuteJsonCommand')return {header:{success:true},response_json:'{"coordinate_api_version":4,"movel_target_mode":"absolute_selected_frame"}'}; if(m==='moveL')motionCalls++;return {header:{success:true}}; }});assert.equal(motionCalls,2); motionCalls=0;await assert.rejects(execute(linear,catalog,group,{rpc:async(s,m)=>{ if(m==='ExecuteJsonCommand')return {header:{success:true},response_json:'{"coordinate_api_version":3}'}; if(m==='moveL')motionCalls++;return {header:{success:true}}; }}),/unsupported/);assert.equal(motionCalls,0); console.log('PASS: external JSON parsing, typed catalog, loop expansion, absolute target transmission, per-call IDs, fail-stop, ambiguous/duplicate rejection, adapter preflight and cancellation; only mock transports');