import assert from 'node:assert/strict' import * as THREE from 'three' import { planPipeEnhancement } from '../../../src/modules/cockpit/domain/pipeEnhancement.js' import { planEnhancementDevices } from '../../../src/modules/cockpit/domain/enhancementDevices.js' import { mountPipeEnhancement } from '../../../src/modules/cockpit/services/threePipeEnhancement.js' const plan = planPipeEnhancement({ selected: { kind: 'pipe', code: 'P' }, visiblePipes: [ { code: 'P', industry: 'WATER_SUPPLY', diameter: 400, positions: [[110, 28], [110.001, 28]] }, ] }) Object.assign(plan, planEnhancementDevices(plan, [{ code: 'D', name: '真实供水设备', industry: 'WATER_SUPPLY', pointCode: 'MP', longitude: 110.0005, latitude: 28, coordinate: { renderable: true } }])) const canvas = new EventTarget() canvas.getBoundingClientRect = () => ({ left: 0, top: 0, width: 900, height: 500 }) let scene, camera, picked const host = { clientWidth: 900, clientHeight: 500, appendChild(n) { n.parentNode = this }, removeChild(n) { n.parentNode = null } } const viewer = mountPipeEnhancement(host, plan, { onDevicePick: (d) => { picked = d }, rendererFactory: () => ({ domElement: canvas, setSize() {}, dispose() {}, render(s, c) { scene = s; camera = c } }), }) scene.updateMatrixWorld(true) camera.updateMatrixWorld() const device = scene.getObjectByName('device:D') assert.ok(device) const screen = device.getWorldPosition(new THREE.Vector3()).project(camera) const clientX = (screen.x + 1) * 450 const clientY = (1 - screen.y) * 250 function pointer(type, x = clientX) { const event = new Event(type, { cancelable: true }) Object.assign(event, { button: 0, pointerId: 1, clientX: x, clientY }) canvas.dispatchEvent(event) } pointer('pointerdown'); pointer('pointerup') assert.equal(picked.code, 'D') assert.equal(picked.pointCode, 'MP') assert.equal(picked.placementText, '邻近设备 · 示意安装') picked = null const root = scene.getObjectByName('pipe-enhancement:P') const blocker = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 0.5), new THREE.MeshBasicMaterial()) blocker.position.copy(root.worldToLocal(camera.position.clone().lerp(device.getWorldPosition(new THREE.Vector3()), 0.5))) root.add(blocker) root.updateMatrixWorld(true) pointer('pointerdown'); pointer('pointerup') assert.equal(picked, null, '不能穿过前方不透明表面拾取后方设备') root.remove(blocker); blocker.geometry.dispose(); blocker.material.dispose() pointer('pointerdown'); pointer('pointermove', clientX + 30); pointer('pointerup', clientX + 30) assert.equal(picked, null) viewer.destroy() pointer('pointerdown'); pointer('pointerup') assert.equal(picked, null) console.log('PASS 真实Three设备模型射线点选派发业务身份;拖拽和销毁后不误派发')