feat(web-platform): release V0.7 地图模块
This commit is contained in:
@@ -35,15 +35,39 @@ describe('project importer', () => {
|
||||
expect(entries).toHaveLength(3);
|
||||
expect(choosePreferredEntry(entries)).toBe('model.xml');
|
||||
});
|
||||
it('解压 ZIP 并保留二进制数据', async () => {
|
||||
it('异步解压 ZIP、保留二进制数据并报告阶段进度', async () => {
|
||||
const zipped = zipSync({
|
||||
'robot/model.urdf': encode('<robot name="r"/>'),
|
||||
'robot/mesh.obj': encode('v 0 0 0'),
|
||||
});
|
||||
const file = new File([zipped], 'robot.zip', { type: 'application/zip' });
|
||||
const result = await importBrowserFiles([file]);
|
||||
const phases: string[] = [];
|
||||
const result = await importBrowserFiles([file], undefined, (progress) =>
|
||||
phases.push(`${progress.phase}:${progress.completed}`),
|
||||
);
|
||||
expect(result.files.map((f) => f.path)).toContain('robot/mesh.obj');
|
||||
expect(result.selectedEntry).toBe('robot/model.urdf');
|
||||
expect(phases).toContain('extracting:0');
|
||||
expect(phases.at(-1)).toBe('indexing:1');
|
||||
});
|
||||
it('发现工程地图且不会将 map.json 当作模型入口', async () => {
|
||||
const mapJson = JSON.stringify({
|
||||
schemaVersion: 1,
|
||||
id: 'room',
|
||||
name: '房间',
|
||||
coordinateSystem: { units: 'm', up: 'Z', forward: '+X' },
|
||||
physics: { source: 'world.xml' },
|
||||
spawnPoints: [],
|
||||
});
|
||||
const result = await importBrowserFiles([
|
||||
new File(['<mujoco><worldbody/></mujoco>'], 'model.xml'),
|
||||
new File([mapJson], 'map.json'),
|
||||
new File(['<mujoco><worldbody/></mujoco>'], 'world.xml'),
|
||||
]);
|
||||
expect(result.maps).toEqual([
|
||||
expect.objectContaining({ descriptorPath: 'map.json', id: 'room', name: '房间' }),
|
||||
]);
|
||||
expect(result.entries.map((entry) => entry.path)).toEqual(['model.xml']);
|
||||
});
|
||||
it('拒绝 ZIP 路径穿越', async () => {
|
||||
const zipped = zipSync({ '../model.xml': encode('<mujoco/>') });
|
||||
@@ -56,8 +80,9 @@ describe('project importer', () => {
|
||||
const b = new File(['<mujoco/>'], 'model.xml');
|
||||
await expect(importBrowserFiles([a, b])).rejects.toThrow('同名路径');
|
||||
});
|
||||
it('拒绝超过限制的文件', async () => {
|
||||
const file = new File(['<mujoco/>'], 'model.xml');
|
||||
it('读取文件内容前根据元数据拒绝超过限制的输入', async () => {
|
||||
const file = new File(['<mujoco/>'], 'model.xml'),
|
||||
read = vi.spyOn(file, 'arrayBuffer');
|
||||
await expect(
|
||||
importBrowserFiles([file], {
|
||||
maxFiles: 1,
|
||||
@@ -66,8 +91,20 @@ describe('project importer', () => {
|
||||
maxZipBytes: 2,
|
||||
}),
|
||||
).rejects.toThrow('单文件超过限制');
|
||||
expect(read).not.toHaveBeenCalled();
|
||||
});
|
||||
it('规范化 MuJoCo 不接受的重复 material 和 ROS package URI', () => {
|
||||
it('按完成文件数报告普通工程读取进度', async () => {
|
||||
const updates: Array<{ phase: string; completed: number; total: number }> = [];
|
||||
const result = await importBrowserFiles(
|
||||
[new File(['<mujoco/>'], 'model.xml'), new File(['v 0 0 0'], 'mesh.obj')],
|
||||
undefined,
|
||||
({ phase, completed, total }) => updates.push({ phase, completed, total }),
|
||||
);
|
||||
expect(result.files).toHaveLength(2);
|
||||
expect(updates).toContainEqual({ phase: 'reading', completed: 2, total: 2 });
|
||||
expect(updates.at(-1)).toEqual({ phase: 'indexing', completed: 1, total: 1 });
|
||||
});
|
||||
it('规范化 MuJoCo 不接受的重复 material 和 ROS package URI', async () => {
|
||||
const urdf = projectFile(
|
||||
'go2w_description/urdf/robot.urdf',
|
||||
'<robot><link name="base"><visual><geometry><mesh filename="package://go2w_description/meshes/base.obj"/></geometry><material name="a"/><material name="b"/></visual></link></robot>',
|
||||
@@ -84,10 +121,11 @@ describe('project importer', () => {
|
||||
name: 'go2w',
|
||||
files: [urdf, mesh],
|
||||
entries: [{ path: urdf.path, format: 'urdf' as const, label: 'robot' }],
|
||||
maps: [],
|
||||
selectedEntry: urdf.path,
|
||||
totalBytes: urdf.size + 1,
|
||||
};
|
||||
const prepared = prepareProjectForMujoco(manifest, urdf.path);
|
||||
const prepared = await prepareProjectForMujoco(manifest, urdf.path);
|
||||
const text = new TextDecoder().decode(prepared.manifest.files[0].data);
|
||||
expect(text.match(/<material/g) ?? []).toHaveLength(1);
|
||||
expect(text).toContain('filename="../meshes/base.obj"');
|
||||
@@ -95,7 +133,7 @@ describe('project importer', () => {
|
||||
expect(text).toContain('fusestatic="false"');
|
||||
expect(prepared.warnings).toHaveLength(2);
|
||||
});
|
||||
it('将 DAE mesh 转换为 MuJoCo 可读取的 OBJ,并复用于 visual/collision', () => {
|
||||
it('将 DAE mesh 转换为 MuJoCo 可读取的 OBJ,并复用于 visual/collision', async () => {
|
||||
const urdf = projectFile(
|
||||
'robot/robot.urdf',
|
||||
'<robot><link name="base"><visual><geometry><mesh filename="meshes/triangle.dae"/></geometry></visual><collision><geometry><mesh filename="meshes/triangle.dae"/></geometry></collision></link></robot>',
|
||||
@@ -106,10 +144,11 @@ describe('project importer', () => {
|
||||
name: 'dae',
|
||||
files: [urdf, dae],
|
||||
entries: [{ path: urdf.path, format: 'urdf' as const, label: 'robot' }],
|
||||
maps: [],
|
||||
selectedEntry: urdf.path,
|
||||
totalBytes: urdf.size + dae.size,
|
||||
};
|
||||
const prepared = prepareProjectForMujoco(manifest, urdf.path);
|
||||
const prepared = await prepareProjectForMujoco(manifest, urdf.path);
|
||||
const text = new TextDecoder().decode(
|
||||
prepared.manifest.files.find((file) => file.path === urdf.path)!.data,
|
||||
);
|
||||
@@ -121,7 +160,7 @@ describe('project importer', () => {
|
||||
expect(new TextDecoder().decode(obj!.data)).toMatch(/^f\s/m);
|
||||
expect(prepared.warnings.join(' ')).toContain('1 个 DAE 文件转换为 OBJ');
|
||||
});
|
||||
it('DAE 缺失或转换失败时安全降级', () => {
|
||||
it('DAE 缺失或转换失败时安全降级', async () => {
|
||||
const urdf = projectFile(
|
||||
'robot.urdf',
|
||||
'<robot><link name="base"><visual><geometry><mesh filename="visual.dae"/></geometry></visual><collision><geometry><mesh filename="collision.dae"/></geometry></collision></link></robot>',
|
||||
@@ -131,10 +170,11 @@ describe('project importer', () => {
|
||||
name: 'dae',
|
||||
files: [urdf],
|
||||
entries: [{ path: urdf.path, format: 'urdf' as const, label: 'robot' }],
|
||||
maps: [],
|
||||
selectedEntry: urdf.path,
|
||||
totalBytes: urdf.size,
|
||||
};
|
||||
const prepared = prepareProjectForMujoco(manifest, urdf.path);
|
||||
const prepared = await prepareProjectForMujoco(manifest, urdf.path);
|
||||
const text = new TextDecoder().decode(prepared.manifest.files[0].data);
|
||||
expect(text).not.toContain('<visual>');
|
||||
expect(text).toContain('<collision>');
|
||||
|
||||
Reference in New Issue
Block a user