52 lines
2.4 KiB
JavaScript
52 lines
2.4 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)
|
|
})
|