import type { ProjectFile, ProjectManifest } from '../../project/types'; import { discoverMapEntries } from '../MapLoader'; import type { PlacedMapAsset } from '../types'; import { encodeEditableMapDocument } from './editorSchema'; import { compiledEditableMapBodyName, findCompiledEditableMapPick } from './compiledMapPick'; import { createEditableObject, type EditableMapDocument } from './types'; const encoder = new TextEncoder(); function file(path: string, data: Uint8Array | string): ProjectFile { const bytes = typeof data === 'string' ? encoder.encode(data) : data; return { path, data: bytes, size: bytes.byteLength, source: 'zip', mimeType: '' }; } function fixture(): { manifest: ProjectManifest; assets: PlacedMapAsset[] } { const document: EditableMapDocument = { schemaVersion: 1, mapId: 'scene', revision: 1, objects: [createEditableObject('box', 'box_1')], spawnPoints: [], }; const files = [ file( 'maps/scene/map.json', JSON.stringify({ schemaVersion: 2, id: 'scene', name: '场景', coordinateSystem: { units: 'm', up: 'Z', forward: '+X' }, physics: { source: 'physics/world.xml' }, authoring: { source: 'authoring/map.scene.json' }, spawnPoints: [], }), ), file('maps/scene/physics/world.xml', ''), file('maps/scene/authoring/map.scene.json', encodeEditableMapDocument(document)), file('robot.xml', ''), ]; return { manifest: { id: 'project', name: 'project', files, entries: [{ path: 'robot.xml', format: 'mjcf', label: 'robot.xml' }], maps: discoverMapEntries(files), totalBytes: files.reduce((total, item) => total + item.size, 0), }, assets: [ { id: 'map_instance-1', name: '场景', selection: { kind: 'project', descriptorPath: 'maps/scene/map.json' }, }, ], }; } describe('已编译认证资产拾取', () => { it('按地图实例命名空间反查创作对象', () => { const { manifest, assets } = fixture(); const bodyName = compiledEditableMapBodyName('scene', 'map_instance-1', 'box_1'); expect(bodyName).toBe('__platform_map_scene_map_instance-1_edit_body_box_1'); expect(findCompiledEditableMapPick(bodyName, manifest, assets)).toEqual({ mapAssetId: 'map_instance-1', descriptorPath: 'maps/scene/map.json', objectId: 'box_1', }); }); it('不接管机器人 Body、其他实例或已禁用对象', () => { const { manifest, assets } = fixture(); expect(findCompiledEditableMapPick('robot_base', manifest, assets)).toBeNull(); expect( findCompiledEditableMapPick( compiledEditableMapBodyName('scene', 'other_instance', 'box_1'), manifest, assets, ), ).toBeNull(); const authoring = manifest.files.find( (item) => item.path === 'maps/scene/authoring/map.scene.json', )!; const disabled = fixture().manifest; const disabledAuthoring = disabled.files.find((item) => item.path === authoring.path)!; const document: EditableMapDocument = { schemaVersion: 1, mapId: 'scene', revision: 2, objects: [{ ...createEditableObject('box', 'box_1'), enabled: false }], spawnPoints: [], }; const data = encodeEditableMapDocument(document); disabledAuthoring.data = data; disabledAuthoring.size = data.byteLength; expect( findCompiledEditableMapPick( compiledEditableMapBodyName('scene', 'map_instance-1', 'box_1'), disabled, assets, ), ).toBeNull(); }); });