import test from 'node:test' import assert from 'node:assert/strict' import { createWorkcellNodeGroups, validateWorkcellLiterals } from './scanMotorNodes.mjs' import { getDeviceKindForAction } from './robotDevice.js' import { normalizeFlowResourceSchema, validateFlowResources, pruneUnusedNodeResources } from './flowResources.js' import { SCANNER_OPERATIONS } from './scannerOperations.mjs' const node = (action, id = action) => ({ id, type: 'serviceNode', properties: structuredClone( createWorkcellNodeGroups().flatMap((group) => group.nodeList).find((item) => item.action === action), ), }) const set = (item, name, value) => { item.properties.nodeParams.find((param) => param.name === name).input = value } const graph = (...nodes) => ({ nodes: [ { id: 'start', type: 'start', properties: { action: 'START', nodeType: 'NONE' } }, ...nodes, ], edges: [] }) test('扫描与运动流程保存后保留设备角色、作业引用和负数位置', () => { const scan = node('SCANNER_START', 'scan') const wait = node('SCANNER_WAIT', 'wait') Object.assign(wait.properties.nodeParams.find((param) => param.name === 'taskId'), { type: 'quote', quoteType: 'output', quote: ['scan', 'OUTPUT', 'taskId'], }) const move = node('MOTOR_MOVE', 'move') set(move, 'targetPosition', -45.25) const saved = JSON.parse(JSON.stringify(normalizeFlowResourceSchema(graph(scan, wait, move)))) assert.deepEqual(validateFlowResources(saved).errors, []) const roles = saved.nodes[0].properties.resourceRoles assert.deepEqual(roles[0].deviceSlots.map((slot) => slot.deviceKind).sort(), [10, 14]) assert.deepEqual(saved.nodes[2].properties.nodeParams[0].quote, ['scan', 'OUTPUT', 'taskId']) assert.equal(saved.nodes[3].properties.nodeParams[0].input, -45.25) assert.equal(pruneUnusedNodeResources(saved).removedSlots, 0) }) test('修改已保存流程的设备用途不能把扫描动作绑定到电机', () => { const saved = normalizeFlowResourceSchema(graph(node('SCANNER_STATUS'), node('MOTOR_ENABLE'))) saved.nodes[1].properties.executionTarget.deviceSlotKey = saved.nodes[2].properties.executionTarget.deviceSlotKey assert.match(validateFlowResources(saved).errors.map((item) => item.message).join(','), /类型不匹配/) }) test('扫描作业编号允许引用,但不允许空的字面量', () => { const wait = node('SCANNER_WAIT') assert.match(validateWorkcellLiterals('SCANNER_WAIT', wait.properties.nodeParams).join(','), /任务编号/) Object.assign(wait.properties.nodeParams[0], { type: 'quote', quote: ['scan', 'OUTPUT', 'taskId'] }) assert.deepEqual(validateWorkcellLiterals('SCANNER_WAIT', wait.properties.nodeParams), []) }) test('超时必须为整数,电机允许负位置但不允许零速度或负容差', () => { const move = node('MOTOR_MOVE') set(move, 'targetPosition', -1) set(move, 'velocity', 0) set(move, 'timeoutMs', 1.5) assert.match(validateWorkcellLiterals('MOTOR_MOVE', move.properties.nodeParams).join(','), /速度/) assert.match(validateWorkcellLiterals('MOTOR_MOVE', move.properties.nodeParams).join(','), /超时/) assert.match(validateWorkcellLiterals('MOTOR_WAIT', []).join(','), /未开放/) }) test('各动作正确筛选设备,拖入多个节点时参数互不污染', () => { const first = createWorkcellNodeGroups() first[0].nodeList[1].nodeParams[0].input = 'CHANGED' assert.equal(createWorkcellNodeGroups()[0].nodeList[1].nodeParams[0].input, '{\n "mode": "trak"\n}') for (const group of createWorkcellNodeGroups()) for (const item of group.nodeList) { assert.equal(getDeviceKindForAction(item.action), group.collapseTitle === '扫描仪' ? 14 : 10) } assert.equal(getDeviceKindForAction('DEVICE_EXECUTE_JSON_COMMAND'), 2) }) test('43个接口映射39个工作流动作,上传、预览和下载由面板调用', () => { assert.equal(SCANNER_OPERATIONS.length, 43) assert.equal(SCANNER_OPERATIONS.filter(op => op.method === 'POST' && !op.upload).length, 31) const nodes = createWorkcellNodeGroups()[0].nodeList assert.equal(nodes.length, 39) assert.equal(new Set(nodes.map(node => node.action)).size, 39) assert.deepEqual(SCANNER_OPERATIONS.filter(op => !op.action).map(op => op.key), ['FRAMEWORK_UPLOAD', 'PREVIEW_IMAGE', 'PREVIEW_STREAM', 'FILE']) const start = nodes.find(node => node.action === 'SCANNER_START') assert.equal(start.nodeParams.find(param => param.name === 'requestJson').input, '{}') assert.equal(start.outputParams[0].name, 'taskId') }) test('JSON和任务编号校验,保留64位整数文本,拒绝旧协议字段', () => { const edit = node('SCANNER_EDIT') const body = '{"projectUuid":"p","pointIds":[9007199254740993],"confirm":true}' set(edit, 'requestJson', body) assert.deepEqual(validateWorkcellLiterals('SCANNER_EDIT', edit.properties.nodeParams), []) assert.equal(JSON.parse(JSON.stringify(edit)).properties.nodeParams[0].input, body) set(edit, 'requestJson', '[]') assert.match(validateWorkcellLiterals('SCANNER_EDIT', edit.properties.nodeParams).join(','), /JSON对象/) const wait = node('SCANNER_WAIT') set(wait, 'taskId', 'NOT-A-TASK') assert.match(validateWorkcellLiterals('SCANNER_WAIT', wait.properties.nodeParams).join(','), /32位/) assert.match(validateWorkcellLiterals('SCANNER_START', [{ name: 'planCode', type: 'input', input: 'old' }]).join(','), /旧扫描/) }) test('导入或编辑后缺少超时、设备任务编号时阻止执行', () => { assert.match(validateWorkcellLiterals('SCANNER_START', []).join(','), /缺少超时/) const wait = node('SCANNER_WAIT') assert.match(validateWorkcellLiterals('SCANNER_WAIT', wait.properties.nodeParams.filter(p => p.name !== 'taskId')).join(','), /缺少设备任务编号/) })