80 lines
3.9 KiB
JavaScript
80 lines
3.9 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import { readFileSync } from 'node:fs'
|
|
import { resolve } from 'node:path'
|
|
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))
|
|
return {
|
|
robot: await import(robotUrl),
|
|
resource: await import(`data:text/javascript;base64,${Buffer.from(resourceSource).toString('base64')}`)
|
|
}
|
|
}
|
|
|
|
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')
|
|
})
|