Files

232 lines
9.4 KiB
TypeScript

import { readFileSync } from 'node:fs'
import { expect, it } from 'vitest'
import { isInterfaceFrame, readUrdfFrames, urdfInfoFromFrames } from '@/domain/urdf-frames'
import {
componentForm,
emptyPartInformation,
partInformation,
type ComponentDetail,
} from '@/domain/component-catalog'
import { componentGateway } from '@/application/component-gateway'
import { sameJson } from '@/domain/json'
import { vi } from 'vitest'
it('reads the original torso coordinate frames without inferring mounting roles', () => {
const xml = readFileSync('public/assembly-samples/torso/urdf/机器人_3.urdf', 'utf8')
const data = readUrdfFrames(xml)
expect(data.rootLink).toBe('BASE')
expect(data.frames.map((item) => item.name)).toEqual(['J1', 'J2', 'J3', 'J4'])
const first = data.frames[0]
expect(first).toMatchObject({
type: 'fixed',
parent: 'BASE',
child: 'LINK1.1',
xyz: [-0.10038, 0.01, 0],
rpy: [0, -1.5708, 0],
})
expect(first).not.toHaveProperty('role')
})
it('reads head motion axis and limits from the file, independent of mesh geometry', () => {
const xml = readFileSync('public/assembly-samples/h-dof0-r/urdf/H-DOF0-R(URDF).urdf', 'utf8')
const data = readUrdfFrames(xml)
expect(data.rootLink).toBe('base_link')
expect(data.frames[0]).toMatchObject({
name: 'Link1',
type: 'revolute',
parent: 'base_link',
child: 'Link1',
axis: [0, 0, -1],
limits: { lower: -1.8, upper: 1.8, effort: 100, velocity: 50 },
})
})
it('reads joint origins without treating visual or inertial origins as installation points', () => {
const data = readUrdfFrames(
'<robot><link name="base"><visual><origin xyz="1 2 3"/></visual><inertial><origin xyz="4 5 6"/></inertial></link><link name="child"/><joint name="J" type="fixed"><parent link="base"/><child link="child"/><origin xyz="7 8 9" rpy="0.123456789 0 0"/></joint></robot>',
)
expect(data.frames).toHaveLength(1)
expect(data.frames[0].xyz).toEqual([7, 8, 9])
expect(data.frames[0].rpy).toEqual([0.123456789, 0, 0])
expect(data.frames[0]).not.toHaveProperty('rootXyz')
})
it('keeps omitted coordinates undefined and retains source precision', () => {
const data = readUrdfFrames(
'<robot name="defaults"><link name="base"/><link name="child"/><joint name="J" type="fixed"><parent link="base"/><child link="child"/></joint></robot>',
)
expect(data.frames[0]).toMatchObject({
xyz: undefined,
rpy: undefined,
originExplicit: false,
axis: undefined,
})
expect(
readUrdfFrames(
'<robot><link name="base"/><link name="child"/><joint name="J" type="fixed"><parent link="base"/><child link="child"/><origin xyz="0.000000000123 0 0"/></joint></robot>',
).frames[0].xyz?.[0],
).toBe(0.000000000123)
})
it('keeps a standard root link available when an URDF has no joints', () => {
const data = readUrdfFrames('<robot><link name="LINK_HEAD_0 "/></robot>')
expect(data.rootLink).toBe('LINK_HEAD_0 ')
expect(data.frames).toMatchObject([
{
name: '__root_interface__',
kind: 'interface',
parent: null,
child: 'LINK_HEAD_0 ',
interfaceName: 'LINK_HEAD_0',
interfaceType: 'HEAD_0',
isRoot: true,
synthetic: true,
xyz: [0, 0, 0],
rpy: [0, 0, 0],
},
])
})
it('recognizes an undifferentiated leg root link as a synthetic interface', () => {
const data = readUrdfFrames(
'<robot><link name="LINK_LEG_0"/><link name="LINK_FOOT_0"/><joint name="J" type="fixed"><parent link="LINK_LEG_0"/><child link="LINK_FOOT_0"/></joint></robot>',
)
expect(data.rootLink).toBe('LINK_LEG_0')
expect(data.frames.at(-1)).toMatchObject({
name: '__root_interface__',
child: 'LINK_LEG_0',
interfaceName: 'LINK_LEG_0',
interfaceType: 'LEG_0',
isRoot: true,
synthetic: true,
})
const serialized = urdfInfoFromFrames(data) as { frames: Array<Record<string, unknown>> }
expect(serialized.frames).toContainEqual(
expect.objectContaining({ child: 'LINK_FOOT_0', interfaceType: 'FOOT_0' }),
)
})
it('rejects malformed coordinates and disconnected frames instead of showing invented zeros', () => {
expect(() =>
readUrdfFrames(
'<robot><link name="base"/><link name="child"/><joint name="J" type="fixed"><parent link="base"/><child link="child"/><origin xyz="bad 0 0"/></joint></robot>',
),
).toThrow('三个有效数值')
expect(() => readUrdfFrames('<robot><link name="base"/><link name="orphan"/></robot>')).toThrow(
'单根',
)
})
it('does not supply missing coordinate attributes or select a mounting relationship', () => {
const data = readUrdfFrames(
'<robot><link name="base"/><link name="child"/><joint name="J" type="fixed"><parent link="base"/><child link="child"/><origin xyz="1 2 3"/><axis/></joint></robot>',
)
expect(data.frames[0]).toMatchObject({ xyz: [1, 2, 3], rpy: undefined, axis: undefined })
expect(data.frames[0]).not.toHaveProperty('mate')
expect(data.frames[0]).not.toHaveProperty('role')
})
it('filters display interfaces without removing internal joints from the raw frames', () => {
const data = readUrdfFrames(
'<robot><link name="base"/><link name="LINK_ARM_0"/><link name="elbow"/><joint name="arm_mount" type="fixed"><parent link="base"/><child link="LINK_ARM_0"/></joint><joint name="elbow_joint" type="revolute"><parent link="LINK_ARM_0"/><child link="elbow"/></joint></robot>',
)
expect(data.frames.map((frame) => frame.name)).toEqual(['arm_mount', 'elbow_joint'])
expect(data.frames.filter(isInterfaceFrame).map((frame) => frame.name)).toEqual(['arm_mount'])
expect(data.frames.find((frame) => frame.name === 'elbow_joint')).toBeDefined()
})
it('saves named raw frames in the required urdfInfo on POST and on form-only PUT', async () => {
const xml =
'<robot name="原文件"><link name="BASE"/><link name="LINK_HEAD_0"/><joint name="J2" type="fixed"><parent link="BASE"/><child link="LINK_HEAD_0"/><origin xyz="0 0.0434269852498788 0" rpy="-1.5707963267949 -1.5707963267949 0"/><axis xyz="0 0 0"/></joint></robot>'
const data = readUrdfFrames(xml)
const urdfInfo = urdfInfoFromFrames(data)
expect(urdfInfo).toMatchObject({
rootLink: 'BASE',
frames: [
{
name: 'J2',
child: 'LINK_HEAD_0',
interfaceType: 'HEAD_0',
interfaceName: 'LINK_HEAD_0',
xyz: [0, 0.0434269852498788, 0],
limits: {},
},
],
})
expect(data.frames[0]).not.toHaveProperty('interfaceType')
const form = componentForm({
...emptyPartInformation(),
name: '头',
category: 'HEAD_0',
urdfInfo,
})
const fetcher = vi.fn<typeof fetch>(async () =>
Response.json({ code: 'OK', data: { ...form, id: 1, version: 1 } }),
)
vi.stubGlobal('fetch', fetcher)
try {
await componentGateway.save(form, {
uploadSessionToken: 'test',
stepObjectKey: 'step/test',
urdfObjectKeys: ['urdf/test'],
})
const body = JSON.parse(fetcher.mock.calls[0][1]!.body as string)
expect(body.urdfInfo).toEqual(urdfInfo)
expect(body).toHaveProperty('bomList', [])
const detail = { ...form, id: 1, version: 2 } as ComponentDetail
await componentGateway.save(
componentForm({ ...partInformation(detail), description: '只改说明' }),
{},
detail,
)
const put = JSON.parse(fetcher.mock.calls[1][1]!.body as string)
expect(put.urdfInfo).toEqual(urdfInfo)
expect(put.version).toBe(2)
expect(put).not.toHaveProperty('uploadSessionToken')
} finally {
vi.unstubAllGlobals()
}
})
it('keeps duplicate Link ids in raw frames but canonicalizes submitted URDF metadata', () => {
const data = readUrdfFrames(
'<robot><link name="LINK#2_TORSO_0"/><link name="LINK#4_LEG_L"/><joint name="J" type="fixed"><parent link="LINK#2_TORSO_0"/><child link="LINK#4_LEG_L"/></joint></robot>',
)
expect(data.rootLink).toBe('LINK#2_TORSO_0')
expect(data.frames.find((frame) => frame.name === 'J')).toMatchObject({
parent: 'LINK#2_TORSO_0',
child: 'LINK#4_LEG_L',
})
const submitted = urdfInfoFromFrames(data) as {
rootLink: string
frames: Array<Record<string, unknown>>
}
expect(submitted.rootLink).toBe('LINK_TORSO_0')
expect(submitted.frames.find((frame) => frame.name === 'J')).toMatchObject({
parent: 'LINK_TORSO_0',
child: 'LINK_LEG_L',
interfaceType: 'LEG_L',
interfaceName: 'LINK_LEG_L',
})
})
it('does not rewrite ordinary Link names or joint names in submitted metadata', () => {
const data = readUrdfFrames(
'<robot><link name="BASK"/><link name="LINK#3_LEG_L"/><joint name="J#3" type="fixed"><parent link="BASK"/><child link="LINK#3_LEG_L"/></joint></robot>',
)
expect(urdfInfoFromFrames(data)).toMatchObject({
rootLink: 'BASK',
frames: [{ name: 'J#3', parent: 'BASK', child: 'LINK_LEG_L' }],
})
})
it('serializes absent attributes without inventing zeros and rejects missing round-trip JSON', () => {
const info = urdfInfoFromFrames(
readUrdfFrames(
'<robot><link name="base"/><link name="child"/><joint name="J" type="fixed"><parent link="base"/><child link="child"/></joint></robot>',
),
)
expect(info).toMatchObject({ frames: [{ originExplicit: false, interfaceType: null }] })
const serialized = JSON.stringify(info)
for (const field of ['xyz', 'rpy', 'axis']) expect(serialized).not.toContain(`"${field}"`)
expect(sameJson(info, undefined)).toBe(false)
expect(sameJson(info, {})).toBe(false)
expect(sameJson({ a: 1, b: [2, 3] }, { b: [2, 3], a: 1 })).toBe(true)
expect(sameJson({ a: 1, b: [2, 3] }, { b: [3, 2], a: 1 })).toBe(false)
})