import assert from 'node:assert/strict' import test from 'node:test' import { dexHandAngleSnapshot, dexHandPositionTargets } from '../src/utils/robotDevice.js' function feedback(positions, model = 'ROH-LiteS001') { return { model, initialized: true, hands: positions.map((position, id) => ({ id, position })).reverse(), } } test('按自由度顺序回填全部位置并精确保留边端整数值', () => { for (let position = 0; position <= 2000; position += 1) { const raw = [position, 0, 2000, 251, 1009, 1614] const targets = dexHandPositionTargets(feedback(raw)) assert.deepEqual(targets.map(target => target.id), [0, 1, 2, 3, 4, 5]) assert.deepEqual(targets.map(({ value }) => Math.trunc(Math.fround(Math.fround(value) * 2000))), raw) } }) test('拒绝未初始化、有故障、缺失或无效的位置反馈', () => { const state = feedback([0, 100, 500, 1000, 1500, 2000], 'ROH-AP001') assert.equal(dexHandPositionTargets(state).length, 6) assert.throws(() => dexHandPositionTargets({ ...state, initialized: false }), /尚未初始化/) assert.throws(() => dexHandPositionTargets({ ...state, hands: state.hands.slice(1) }), /完整的六轴/) assert.throws(() => dexHandPositionTargets(feedback([0, 100, null, 1000, 1500, 2000])), /位置无效/) assert.throws(() => dexHandPositionTargets(feedback([0, 100, 2001, 1000, 1500, 2000])), /位置无效/) state.hands[0].errorMessages = ['servo disconnected'] assert.throws(() => dexHandPositionTargets(state), /存在故障/) }) test('拒绝无位置反馈能力的机型和重复自由度', () => { assert.throws(() => dexHandPositionTargets(feedback([0, 0, 0, 0, 0, 0], 'RH56D')), /不支持位置回填/) const state = feedback([0, 100, 500, 1000, 1500, 2000]) state.hands[0].id = state.hands[1].id assert.throws(() => dexHandPositionTargets(state), /自由度编号或位置无效/) }) test('实测角度按百分之一度显示,回放按边端行程比例生成', () => { const position = [0, 250, 501, 1000, 1501, 2000] const angles = [17470, 17209, 17168, 13701, 3354, 99] const state = feedback(position) for (const axis of state.hands) axis.angle = angles[axis.id] const snapshot = dexHandAngleSnapshot(state) assert.deepEqual(snapshot.angles, angles.map((angle, id) => ({ id, degrees: angle / 100 }))) assert.deepEqual( snapshot.values.map(({ value }) => Math.trunc(Math.fround(Math.fround(value) * 1000))), position.map(raw => Math.round(raw / 2)), ) assert.throws(() => dexHandAngleSnapshot({ ...state, model: 'RH56D' }), /不支持角度回填/) state.hands[0].angle = null assert.throws(() => dexHandAngleSnapshot(state), /完整的六轴实测角度/) })