235 lines
8.7 KiB
TypeScript
235 lines
8.7 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
||
import {
|
||
componentForm,
|
||
emptyPartInformation,
|
||
customFields,
|
||
validateStepFile,
|
||
validateUrdfFolder,
|
||
urdfRelativePath,
|
||
partInformation,
|
||
urdfComponentName,
|
||
type ComponentDetail,
|
||
} from '@/domain/component-catalog'
|
||
const info = () => ({
|
||
...emptyPartInformation(),
|
||
name: ' 安装板 ',
|
||
category: '自由分类',
|
||
urdfInfo: {},
|
||
})
|
||
const file = (name: string, path = name, content = 'bytes') => {
|
||
const item = new File([content], name)
|
||
Object.defineProperty(item, 'webkitRelativePath', { value: path })
|
||
return item
|
||
}
|
||
describe('new component form contract', () => {
|
||
it('submits the latest flat fields and both JSON containers without obsolete metadata', () => {
|
||
expect(componentForm({ ...info(), weightKg: '0' })).toEqual({
|
||
componentName: '安装板',
|
||
rotationAxis: 'Z',
|
||
category: '自由分类',
|
||
description: '',
|
||
material: '',
|
||
massKg: 0,
|
||
degreesOfFreedom: 1,
|
||
functionDescription: '',
|
||
shape: '',
|
||
size: '',
|
||
torqueNm: null,
|
||
bomList: [],
|
||
urdfInfo: {},
|
||
basicFields: [],
|
||
parameterFields: [],
|
||
})
|
||
})
|
||
it.each(['-1', 'NaN', 'Infinity', '1e3', '0.1234567', '1000000000000'])(
|
||
'rejects numeric value %s',
|
||
(value) => {
|
||
expect(() => componentForm({ ...info(), weightKg: value })).toThrow('质量')
|
||
},
|
||
)
|
||
it('enforces UTF-8 description size and updated text limits', () => {
|
||
expect(() => componentForm({ ...info(), description: '中'.repeat(21846) })).toThrow('65535')
|
||
expect(() => componentForm({ ...info(), name: '名'.repeat(128) })).not.toThrow()
|
||
expect(() => componentForm({ ...info(), material: '材'.repeat(129) })).toThrow('材质')
|
||
})
|
||
it('uses typed top-level description fields and restores them without duplicating custom parameters', () => {
|
||
const form = componentForm({
|
||
...info(),
|
||
degreesOfFreedom: '6',
|
||
material: '铝合金',
|
||
function: ' 支撑 ',
|
||
shape: '圆柱',
|
||
size: '100 × 80 × 50 mm',
|
||
torque: '20.123456',
|
||
weightKg: '1.25',
|
||
description: '自由填写的描述',
|
||
basicFields: [{ name: 'partType', value: 'HEAD' }],
|
||
parameterFields: [{ name: '旧参数', value: '原值' }],
|
||
})
|
||
expect(form).toMatchObject({
|
||
degreesOfFreedom: 6,
|
||
functionDescription: '支撑',
|
||
shape: '圆柱',
|
||
size: '100 × 80 × 50 mm',
|
||
torqueNm: 20.123456,
|
||
material: '铝合金',
|
||
massKg: 1.25,
|
||
})
|
||
expect(form.parameterFields).toEqual([{ name: '旧参数', value: '原值' }])
|
||
const restored = partInformation(form as ComponentDetail)
|
||
expect(componentForm(restored)).toEqual(form)
|
||
expect(componentForm({ ...restored, torque: '' }).torqueNm).toBeNull()
|
||
expect(componentForm({ ...restored, torque: '30' }).torqueNm).toBe(30)
|
||
})
|
||
it('enforces new numeric, precision and text limits before uploading', () => {
|
||
for (const degreesOfFreedom of ['-1', '1.5', '2e1', '2147483648', 'abc'])
|
||
expect(() => componentForm({ ...info(), degreesOfFreedom })).toThrow('自由度')
|
||
expect(componentForm({ ...info(), degreesOfFreedom: '2147483647' }).degreesOfFreedom).toBe(
|
||
2147483647,
|
||
)
|
||
for (const torque of ['20 Nm', '-1', '0.1234567', '1000000000000'])
|
||
expect(() => componentForm({ ...info(), torque })).toThrow('扭矩')
|
||
for (const [key, limit] of [
|
||
['function', 512],
|
||
['shape', 128],
|
||
['size', 128],
|
||
] as const) {
|
||
expect(() => componentForm({ ...info(), [key]: '字'.repeat(limit) })).not.toThrow()
|
||
expect(() => componentForm({ ...info(), [key]: '字'.repeat(limit + 1) })).toThrow('最多')
|
||
}
|
||
})
|
||
it('allows absent URDF metadata and round-trips arbitrary JSON shapes without data loss', () => {
|
||
expect(componentForm({ ...info(), urdfInfo: null }).urdfInfo).toBeNull()
|
||
for (const payload of [{ robotName: '文件', links: [{ name: 'base' }], custom: 0 }, []]) {
|
||
const form = componentForm({
|
||
...info(),
|
||
urdfInfo: payload,
|
||
bomList: { items: [{ quantity: 4 }] },
|
||
})
|
||
expect(componentForm(partInformation(form as ComponentDetail))).toEqual(form)
|
||
expect(form.urdfInfo).not.toBe(payload)
|
||
}
|
||
})
|
||
it('allows every descriptive field to be blank without inventing numeric zeros or URDF data', () => {
|
||
const form = componentForm(emptyPartInformation())
|
||
expect(form).toMatchObject({
|
||
componentName: '',
|
||
category: '',
|
||
massKg: null,
|
||
degreesOfFreedom: 1,
|
||
torqueNm: null,
|
||
urdfInfo: null,
|
||
bomList: [],
|
||
})
|
||
expect(componentForm(partInformation(form as ComponentDetail))).toEqual(form)
|
||
const explicitZero = componentForm({
|
||
...emptyPartInformation(),
|
||
weightKg: '0',
|
||
degreesOfFreedom: '0',
|
||
torque: '0',
|
||
})
|
||
expect(explicitZero).toMatchObject({ massKg: 0, degreesOfFreedom: 0, torqueNm: 0 })
|
||
})
|
||
it('derives a default URDF name and preserves saved or manually edited names', () => {
|
||
expect(urdfComponentName('头-0-型号.URDF')).toBe('头-0-型号')
|
||
const form = componentForm(info())
|
||
const restored = partInformation({
|
||
...form,
|
||
componentName: '旧手填名称',
|
||
urdfMainObjectKey: 'components/CW-1/urdf/躯干-0-型号.urdf',
|
||
} as ComponentDetail)
|
||
expect(restored.name).toBe('旧手填名称')
|
||
expect(componentForm({ ...restored, name: '新的名称' }).componentName).toBe('新的名称')
|
||
expect(
|
||
partInformation({
|
||
...form,
|
||
componentName: '',
|
||
urdfMainObjectKey: 'urdf/躯干-0-型号.urdf',
|
||
} as ComponentDetail).name,
|
||
).toBe('')
|
||
})
|
||
it('trims custom field names, preserves order and empty values', () => {
|
||
expect(
|
||
customFields(
|
||
[
|
||
{ name: ' 电压 ', value: '' },
|
||
{ name: '扭矩', value: '20 Nm' },
|
||
],
|
||
'参数',
|
||
),
|
||
).toEqual([
|
||
{ name: '电压', value: '' },
|
||
{ name: '扭矩', value: '20 Nm' },
|
||
])
|
||
expect(() =>
|
||
customFields(
|
||
[
|
||
{ name: '电压', value: '' },
|
||
{ name: ' 电压 ', value: '' },
|
||
],
|
||
'参数',
|
||
),
|
||
).toThrow('重复')
|
||
expect(() => customFields([{ name: ' ', value: '' }], '参数')).toThrow('名称')
|
||
expect(() =>
|
||
customFields(
|
||
Array.from({ length: 101 }, (_, index) => ({ name: String(index), value: '' })),
|
||
'参数',
|
||
),
|
||
).toThrow('100')
|
||
expect(() => customFields([{ name: '电压', value: 'a'.repeat(2001) }], '参数')).toThrow('2000')
|
||
})
|
||
it('permits the same custom name across groups and round-trips complete forms', () => {
|
||
const form = componentForm({
|
||
...info(),
|
||
basicFields: [{ name: '型号', value: 'A' }],
|
||
parameterFields: [{ name: '型号', value: 'B' }],
|
||
})
|
||
const detail = { ...form } as ComponentDetail
|
||
expect(componentForm(partInformation(detail))).toEqual(form)
|
||
})
|
||
})
|
||
describe('paired source rules', () => {
|
||
it('accepts exactly one nonempty STEP/STP and rejects unsafe or multiple names', () => {
|
||
expect(validateStepFile([file('part.STP')]).name).toBe('part.STP')
|
||
for (const files of [
|
||
[],
|
||
[file('a.step'), file('b.step')],
|
||
[file('x.zip')],
|
||
[file('a.step', 'a.step', '')],
|
||
[file('../a.step')],
|
||
])
|
||
expect(() => validateStepFile(files)).toThrow()
|
||
})
|
||
it('removes only the outer folder and keeps nested resource paths', () => {
|
||
const robot = file('robot.URDF', 'L20/robot.URDF'),
|
||
mesh = file('base.stl', 'L20/meshes/base.stl')
|
||
expect(validateUrdfFolder([robot, mesh])).toBe(robot)
|
||
expect(urdfRelativePath(mesh)).toBe('meshes/base.stl')
|
||
})
|
||
it('rejects flat files, multiple roots/mains, traversal and duplicate paths', () => {
|
||
const robot = file('robot.urdf', 'L20/robot.urdf')
|
||
for (const files of [
|
||
[file('robot.urdf')],
|
||
[robot, file('x.stl', 'Other/x.stl')],
|
||
[robot, file('x.urdf', 'L20/x.urdf')],
|
||
[robot, file('x.stl', 'L20/../x.stl')],
|
||
[robot, robot],
|
||
])
|
||
expect(() => validateUrdfFolder(files)).toThrow()
|
||
})
|
||
})
|
||
|
||
it('rotationAxis saves at the top level, restores on edit and supports clearing without requiring URDF', () => {
|
||
const form = componentForm({ ...emptyPartInformation(), rotationAxis: ' Z ' })
|
||
expect(form.rotationAxis).toBe('Z')
|
||
expect(form.urdfInfo).toBeNull()
|
||
const restored = partInformation(form as ComponentDetail)
|
||
expect(restored.rotationAxis).toBe('Z')
|
||
expect(componentForm({ ...restored, rotationAxis: '' }).rotationAxis).toBeNull()
|
||
expect(componentForm({ ...restored, rotationAxis: '-X' }).rotationAxis).toBe('-X')
|
||
for (const axis of ['x', 'y', 'z', '-x', '-y', '-z'])
|
||
expect(componentForm({ ...restored, rotationAxis: axis }).rotationAxis).toBe(axis.toUpperCase())
|
||
expect(() => componentForm({ ...restored, rotationAxis: 'xyz' })).toThrow('旋转轴')
|
||
})
|