69 lines
3.2 KiB
JavaScript
69 lines
3.2 KiB
JavaScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { registerHooks } from 'node:module'
|
|
|
|
registerHooks({
|
|
/** 将 Vite 别名指向当前项目源码,直接测试真实资源序列化函数。 */
|
|
resolve(specifier, context, nextResolve) {
|
|
if (specifier === '@/utils/robotDevice') return nextResolve(new URL('../src/utils/robotDevice.js', import.meta.url).href, context)
|
|
return nextResolve(specifier, context)
|
|
}
|
|
})
|
|
|
|
const { normalizeFlowResourceSchema, sanitizeFlowResourceDefinition, validateFlowResources } = await import('../src/utils/flowResources.js')
|
|
|
|
/** 构造视觉定位的相机、机械臂角色绑定,代表原公共包页面保存的现有流程。 */
|
|
function visionGraph() {
|
|
return { nodes: [
|
|
{ id: 'start', type: 'start', properties: { resourceRoles: [{
|
|
roleKey: 'robot', deviceSlots: [{ slotKey: 'camera', deviceKind: 5 }, { slotKey: 'arm', deviceKind: 2 }]
|
|
}] } },
|
|
{ id: 'vision', type: 'serviceNode', properties: {
|
|
action: 'VISION_LOCATE_TARGET', nodeType: 'EDGE', nodeParams: [], executionTarget: {
|
|
roleKey: 'robot', dispatchPolicy: 'ONE', deviceBindings: [
|
|
{ parameterName: 'cameraDeviceId', slotKey: 'camera' },
|
|
{ parameterName: 'armDeviceId', slotKey: 'arm' }
|
|
]
|
|
}
|
|
} }
|
|
], edges: [] }
|
|
}
|
|
|
|
test('加载与保存视觉定位流程都保留相机和机械臂具名绑定,原图保持不变', () => {
|
|
const graph = visionGraph()
|
|
const original = structuredClone(graph)
|
|
const expected = graph.nodes[1].properties.executionTarget.deviceBindings
|
|
for (const transform of [normalizeFlowResourceSchema, sanitizeFlowResourceDefinition]) {
|
|
const result = transform(graph)
|
|
assert.deepEqual(result.nodes[1].properties.executionTarget.deviceBindings, expected)
|
|
assert.equal(result.nodes[1].properties.executionTarget.deviceSlotKey, undefined)
|
|
}
|
|
assert.deepEqual(graph, original)
|
|
})
|
|
|
|
test('旧视觉定位参数迁移为两个正确类型的设备用途,而非单设备默认绑定', () => {
|
|
const graph = visionGraph()
|
|
graph.nodes[0].properties.resourceRoles[0].deviceSlots = []
|
|
graph.nodes[1].properties.nodeParams = [
|
|
{ name: 'cameraDeviceId', input: 'camera-1' }, { name: 'armDeviceId', input: 'arm-1' }
|
|
]
|
|
delete graph.nodes[1].properties.executionTarget.deviceBindings
|
|
const result = normalizeFlowResourceSchema(graph)
|
|
const role = result.nodes[0].properties.resourceRoles[0]
|
|
const target = result.nodes[1].properties.executionTarget
|
|
assert.equal(target.deviceBindings.length, 2)
|
|
for (const [parameterName, kind, legacyDeviceId] of [['cameraDeviceId', 5, 'camera-1'], ['armDeviceId', 2, 'arm-1']]) {
|
|
const binding = target.deviceBindings.find(item => item.parameterName === parameterName)
|
|
const slot = role.deviceSlots.find(item => item.slotKey === binding.slotKey)
|
|
assert.equal(slot.deviceKind, kind)
|
|
assert.equal(slot.legacyDeviceId, legacyDeviceId)
|
|
}
|
|
})
|
|
|
|
test('视觉定位引用不存在的第二设备用途仍会阻止保存', () => {
|
|
const graph = visionGraph()
|
|
graph.nodes[1].properties.executionTarget.deviceBindings[1].slotKey = 'missing-arm'
|
|
const { errors } = validateFlowResources(graph)
|
|
assert(errors.some(error => error.nodeId === 'vision' && error.message.includes('不存在')))
|
|
})
|