Files
cad-world/frontend/tests/integration/codecs/component-orientation.integration.spec.ts
T
2026-09-22 10:36:28 +08:00

90 lines
5.5 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { buildManagedComponentPreview } from '@/components/part-manager/buildManagedComponentPreview'
import { useAdminComponentDraft } from '@/composables/useAdminComponentDraft'
import { buildComponentUpsertPayload, mapComponentDetailToAdminRecord } from '@/modules/application/component-catalog/component-catalog-record'
import { buildPreviewFromUrdfFiles } from '@/modules/application/component-catalog/component-catalog-preview'
import { SplitSession, buildFragmentPreview, createValidatedUrdfSplitGraph } from '@/modules/application/urdf-splitter'
import { buildRfsProjectFilesPayloadFromModel } from '@/modules/infrastructure/codecs/rfs/model-to-rfs-exporter'
import { parseUrdfTextToKinematicGraph, parseUrdfTextToRfsModel } from '@/modules/infrastructure/codecs/urdf/urdf-to-model-importer'
const urdf = `<robot name="source_robot">
<link name="base"><visual><geometry><box size="1 1 1"/></geometry></visual></link>
<link name="arm"><visual><geometry><box size="0.1 0.1 0.5"/></geometry></visual></link>
<joint name="shoulder" type="revolute"><parent link="base"/><child link="arm"/>
<origin xyz="0 -0.3 0.5" rpy="0 0 1.5707963267948966"/><axis xyz="0 1 0"/>
<limit lower="-1" upper="1" effort="10" velocity="2"/>
</joint>
</robot>`
describe('component upload orientation roundtrip', () => {
it('keeps the rotated source frame through splitting, RFS packaging, reupload and the API payload', async () => {
const graph = createValidatedUrdfSplitGraph(parseUrdfTextToKinematicGraph({ urdfText: urdf }))
const source = buildManagedComponentPreview(parseUrdfTextToRfsModel({ urdfText: urdf }), [], { preserveUrdfLinkParts: true })
const session = new SplitSession(graph, 'source-group')
const cut = session.cut('fragment-root', 'shoulder')
session.updateBoundaryAttachmentFrame(cut.id, 'child', {
insertionAxis: { x: 0, y: 1, z: 0 }, forwardAxis: { x: 1, y: 0, z: 0 },
})
const fragment = buildFragmentPreview({ state: session.snapshot(), graph, sourcePreview: source, fragmentId: cut.childFragmentId })
const payload = await buildRfsProjectFilesPayloadFromModel(fragment.preview.model, fragment.preview.parts, {
components: [{ id: 'arm', name: 'Arm', interfaces: fragment.interfaces,
componentJson: fragment.componentJsonBase,
preview: { model: fragment.preview.model, jointInitialValues: fragment.preview.jointInitialValues } }],
})
const draft = useAdminComponentDraft()
try {
await draft.uploadRfs([new File([payload.rfsContent], payload.rfsFileName)])
const input = draft.buildInput()
const saved = buildComponentUpsertPayload(input, fragment.interfaces)
expect(saved.componentJson).toMatchObject(JSON.parse(JSON.stringify(fragment.componentJsonBase)))
expect(saved.componentJson.orientation).toMatchObject({
basis: 'urdf-convention', sourceContext: 'whole-robot', sourceRootLink: 'base',
rootLink: 'arm', sourceLateralCoordinate: -0.3,
sourceRootPose: { position: { x: 0, y: expect.closeTo(0.5), z: expect.closeTo(0.3) } },
})
expect(saved.componentJson.interfaces).toHaveLength(1)
expect(fragment.preview.model.sceneInstances[0].position).toEqual({ x: 0, y: 0, z: 0 })
const restored = mapComponentDetailToAdminRecord({ id: 'new-arm', ...saved, createdAt: '', updatedAt: '' })
draft.openEdit(restored)
expect(draft.buildInput().componentJsonBase).toEqual(JSON.parse(JSON.stringify(fragment.componentJsonBase)))
const legacy = JSON.parse(payload.rfsContent)
legacy.part.components.forEach((component: { componentJson?: unknown }) => { delete component.componentJson })
await draft.uploadRfs([new File([JSON.stringify(legacy)], payload.rfsFileName)])
expect(draft.buildInput().componentJsonBase).toEqual({})
} finally {
draft.resetDraft()
source.release()
fragment.preview.release()
}
})
it('extracts the selected URDF basis without classifying a standalone root as a central body part', async () => {
const result = await buildPreviewFromUrdfFiles([
new File(['<robot name="wrong"><link name="wrong"/></robot>'], 'a.urdf'),
new File([urdf], 'selected.urdf'),
], 'upload', { urdfFileName: 'selected.urdf' })
try {
expect(result.componentJsonBase?.orientation).toMatchObject({
basis: 'urdf-convention', sourceContext: 'standalone', robotName: 'source_robot',
forward: { x: 1, y: 0, z: 0 }, up: { x: 0, y: 1, z: 0 },
interfaces: [expect.objectContaining({ name: 'shoulder' })],
})
expect(result.componentJsonBase?.orientation).not.toHaveProperty('sourceLateralCoordinate')
} finally { result.preview.release() }
})
it('replaces stale source metadata when an existing component receives a different URDF', async () => {
const draft = useAdminComponentDraft()
draft.openEdit(mapComponentDetailToAdminRecord({ id: 'existing', componentName: 'Old arm', source: 'old.rfs',
componentJson: { robotSplit: { rootLink: 'old-root' }, orientation: { basis: 'old-calibration' } },
description: '', createdAt: '', updatedAt: '' }))
try {
await draft.uploadUrdf([new File([urdf], 'replacement.urdf')])
const input = draft.buildInput()
expect(input.componentId).toBe('existing')
expect(input.componentJsonBase).not.toHaveProperty('robotSplit')
expect(input.componentJsonBase.orientation).toMatchObject({ basis: 'urdf-convention', rootLink: 'base' })
} finally { draft.resetDraft() }
})
})