121 lines
6.3 KiB
JavaScript
121 lines
6.3 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import { readFileSync } from 'node:fs'
|
|
import { resolve } from 'node:path'
|
|
import { pathToFileURL } from 'node:url'
|
|
import test from 'node:test'
|
|
|
|
/** 使用实际源码并解析 Vite 的本地别名,让 Node 能验证资源协议而无需启动浏览器。 */
|
|
async function loadResourceModules() {
|
|
const sourceRoot = resolve(process.env.RESOURCE_SOURCE_ROOT || 'src')
|
|
const robotSource = readFileSync(resolve(sourceRoot, 'utils/robotDevice.js'), 'utf8')
|
|
const robotUrl = `data:text/javascript;base64,${Buffer.from(robotSource).toString('base64')}`
|
|
const resourceSource = readFileSync(resolve(sourceRoot, 'utils/flowResources.js'), 'utf8')
|
|
.replace("'@/utils/robotDevice'", JSON.stringify(robotUrl))
|
|
.replace("'@/utils/touchFlow'", JSON.stringify(pathToFileURL(resolve(sourceRoot, 'utils/touchFlow.js')).href))
|
|
return {
|
|
robot: await import(robotUrl),
|
|
resource: await import(`data:text/javascript;base64,${Buffer.from(resourceSource).toString('base64')}`)
|
|
}
|
|
}
|
|
|
|
test('灵巧手动作分配独立的 7 号设备用途', async () => {
|
|
const { robot, resource } = await loadResourceModules()
|
|
const graph = { nodes: [
|
|
{ id: 'start', type: 'start', properties: {} },
|
|
{ id: 'hand', type: 'serviceNode', properties: { nodeType: 'EDGE', action: 'DEX_HAND_SET_ANGLE' } },
|
|
], edges: [] }
|
|
const normalized = resource.normalizeFlowResourceSchema(graph)
|
|
const target = normalized.nodes[1].properties.executionTarget
|
|
const slot = normalized.nodes[0].properties.resourceRoles[0].deviceSlots.find(item => item.slotKey === target.deviceSlotKey)
|
|
assert.equal(slot.deviceKind, 7)
|
|
assert.equal(slot.displayName, '灵巧手')
|
|
assert.equal(robot.deviceMatchesSlot({ deviceKind: 2, deviceId: 'arm' }, slot), false)
|
|
assert.equal(robot.deviceMatchesSlot({ deviceKind: 7, deviceId: 'hand' }, slot), true)
|
|
})
|
|
|
|
test('旧触控流程补齐路径数组、最后一步和坐标引用字段', async () => {
|
|
const { resource } = await loadResourceModules()
|
|
const graph = { nodes: [
|
|
{ id: 'start', type: 'start', properties: {} },
|
|
{ id: 'search', type: 'serviceNode', properties: {
|
|
action: 'TI_PATH_SEARCH', outputParams: [{ name: 'isEnd', type: 'string' }],
|
|
} },
|
|
{ id: 'step', type: 'currentLoop', properties: {
|
|
action: 'GET_CURRENT_OBJECT', outputParams: [
|
|
{ name: 'object', type: 'object', children: [{ name: 'functionName', type: 'string' }] },
|
|
],
|
|
} },
|
|
{ id: 'touch', type: 'serviceNode', properties: { action: 'TOUCH', nodeParams: [] } },
|
|
], edges: [] }
|
|
const [, search, step, touch] = resource.normalizeFlowResourceSchema(graph).nodes
|
|
assert.equal(search.properties.outputParams.find(field => field.name === 'path').type, 'array<object>')
|
|
assert.equal(search.properties.outputParams.find(field => field.name === 'isEnd').type, 'boolean')
|
|
assert.equal(step.properties.outputParams[0].children.find(field => field.name === 'isLastStep').type, 'boolean')
|
|
assert.equal(touch.properties.nodeParams[0].name, 'touchCoordinates')
|
|
assert.equal(touch.properties.nodeParams.find(field => field.name === 'retryCount').input, 0)
|
|
assert.equal(touch.properties.outputParams.find(field => field.name === 'retryAttempts').type, 'number')
|
|
assert.equal(graph.nodes[1].properties.outputParams.length, 1)
|
|
})
|
|
|
|
test('视觉定位迁移保留相机和机械臂的独立资源绑定且不改写原图', async () => {
|
|
const { resource } = await loadResourceModules()
|
|
const graph = { nodes: [
|
|
{ id: 'start', type: 'start', properties: {} },
|
|
{ id: 'vision', type: 'serviceNode', properties: {
|
|
action: 'VISION_LOCATE_TARGET', nodeType: 'EDGE', nodeParams: [
|
|
{ name: 'cameraDeviceId', input: 'camera-1' },
|
|
{ name: 'armDeviceId', input: 'arm-1' }
|
|
]
|
|
} }
|
|
], edges: [] }
|
|
const original = structuredClone(graph)
|
|
const normalized = resource.normalizeFlowResourceSchema(graph)
|
|
const target = normalized.nodes[1].properties.executionTarget
|
|
assert.deepEqual(target.deviceBindings, [
|
|
{ parameterName: 'cameraDeviceId', slotKey: 'camera' },
|
|
{ parameterName: 'armDeviceId', slotKey: 'arm' }
|
|
])
|
|
assert.equal(target.deviceSlotKey, undefined)
|
|
const slots = normalized.nodes[0].properties.resourceRoles[0].deviceSlots
|
|
assert.deepEqual(slots.map(slot => [slot.slotKey, slot.deviceKind, slot.legacyDeviceId]), [
|
|
['camera', 5, 'camera-1'], ['arm', 2, 'arm-1']
|
|
])
|
|
assert.deepEqual(graph, original)
|
|
})
|
|
|
|
test('通用设备指令仍分配机械臂用途,触控及通用音频硬件匹配保持兼容', async () => {
|
|
const { robot } = await loadResourceModules()
|
|
assert.equal(robot.getDeviceKindForAction('DEVICE_EXECUTE_JSON_COMMAND'), 2)
|
|
assert.equal(robot.getDeviceKindForAction('TOUCH'), 2)
|
|
assert.equal(robot.getDeviceKindForAction('MICROPHONE_START'), 9)
|
|
assert.equal(robot.getDeviceKindForAction('SPEAKER_PLAYAUDIO'), 13)
|
|
})
|
|
|
|
test('颈部节点共用独立颈部用途,不能绑定同类型的普通机械臂', async () => {
|
|
const { robot, resource } = await loadResourceModules()
|
|
const makeNode = (id, action) => ({
|
|
id, type: 'serviceNode', properties: { action, nodeType: 'EDGE' },
|
|
})
|
|
const graph = resource.sanitizeFlowResourceDefinition({ nodes: [
|
|
{ id: 'start', type: 'start', properties: {} },
|
|
makeNode('arm', 'ARM_MOVE_TO_J'),
|
|
makeNode('moveNeck', 'ARM_NECK_MOVE_J'),
|
|
makeNode('stopNeck', 'ARM_NECK_STOP'),
|
|
], edges: [] })
|
|
const [armSlot, neckSlot] = graph.nodes[0].properties.resourceRoles[0].deviceSlots
|
|
assert.equal(armSlot.slotKey, 'arm')
|
|
assert.equal(neckSlot.slotKey, 'neck')
|
|
assert.equal(neckSlot.deviceKind, robot.DEVICE_KIND.ARM)
|
|
assert.equal(neckSlot.requiredDeviceId, robot.NECK_DEVICE_ID)
|
|
assert.deepEqual(graph.nodes.slice(1).map((node) => node.properties.executionTarget.deviceSlotKey), [
|
|
'arm', 'neck', 'neck',
|
|
])
|
|
assert.equal(robot.deviceMatchesSlot({ deviceKind: 2, deviceId: 'arm-1' }, neckSlot), false)
|
|
assert.equal(robot.deviceMatchesSlot({ deviceKind: 2, deviceId: 'neck_arm' }, neckSlot), true)
|
|
assert.equal(robot.deviceMatchesSlot({ deviceKind: 2, deviceId: 'xiaozhao_head' }, neckSlot), true)
|
|
neckSlot.requiredDeviceId = 'xiaozhao_head'
|
|
const updated = resource.normalizeFlowResourceSchema(graph)
|
|
assert.equal(updated.nodes[0].properties.resourceRoles[0].deviceSlots[1].requiredDeviceId, 'neck_arm')
|
|
assert.equal(updated.nodes[2].properties.executionTarget.deviceSlotKey, 'neck')
|
|
})
|