221 lines
6.0 KiB
TypeScript
221 lines
6.0 KiB
TypeScript
/**
|
|
* HardwareDisplayStrategy 单元测试
|
|
*/
|
|
|
|
import * as THREE from 'three'
|
|
import { HardwareDisplayStrategy, HardwareInstance, DisplayPhase } from '@/lib/3d/hardware-display-strategy'
|
|
|
|
// Mock GSAP
|
|
jest.mock('gsap', () => ({
|
|
timeline: jest.fn(() => ({
|
|
to: jest.fn().mockReturnThis(),
|
|
kill: jest.fn(),
|
|
})),
|
|
to: jest.fn(),
|
|
}))
|
|
|
|
describe('HardwareDisplayStrategy', () => {
|
|
let strategy: HardwareDisplayStrategy
|
|
let camera: THREE.PerspectiveCamera
|
|
|
|
const createMockInstance = (id: string, typeId?: string): HardwareInstance => {
|
|
const mesh = new THREE.Mesh(
|
|
new THREE.BoxGeometry(10, 10, 10),
|
|
new THREE.MeshStandardMaterial()
|
|
)
|
|
return {
|
|
id,
|
|
mesh,
|
|
position: new THREE.Vector3(Math.random() * 100, Math.random() * 100, Math.random() * 100),
|
|
typeId,
|
|
}
|
|
}
|
|
|
|
beforeEach(() => {
|
|
camera = new THREE.PerspectiveCamera()
|
|
strategy = new HardwareDisplayStrategy(camera)
|
|
})
|
|
|
|
afterEach(() => {
|
|
strategy.dispose()
|
|
})
|
|
|
|
describe('注册五金件', () => {
|
|
it('应该正确注册单个五金件', () => {
|
|
const instance = createMockInstance('hw-1')
|
|
strategy.registerHardware(instance)
|
|
expect(strategy.getRegisteredCount()).toBe(1)
|
|
})
|
|
|
|
it('应该正确批量注册五金件', () => {
|
|
const instances = [
|
|
createMockInstance('hw-1'),
|
|
createMockInstance('hw-2'),
|
|
createMockInstance('hw-3'),
|
|
]
|
|
strategy.registerMany(instances)
|
|
expect(strategy.getRegisteredCount()).toBe(3)
|
|
})
|
|
|
|
it('注册后五金件应该初始隐藏', () => {
|
|
const instance = createMockInstance('hw-1')
|
|
strategy.registerHardware(instance)
|
|
expect(instance.mesh.visible).toBe(false)
|
|
})
|
|
|
|
it('应该按类型分组', () => {
|
|
const instances = [
|
|
createMockInstance('hw-1', 'screw'),
|
|
createMockInstance('hw-2', 'screw'),
|
|
createMockInstance('hw-3', 'hinge'),
|
|
]
|
|
strategy.registerMany(instances)
|
|
|
|
const groups = strategy.getTypeGroups()
|
|
expect(groups.get('screw')?.length).toBe(2)
|
|
expect(groups.get('hinge')?.length).toBe(1)
|
|
})
|
|
})
|
|
|
|
describe('注销五金件', () => {
|
|
it('应该正确注销五金件', () => {
|
|
const instance = createMockInstance('hw-1', 'screw')
|
|
strategy.registerHardware(instance)
|
|
expect(strategy.getRegisteredCount()).toBe(1)
|
|
|
|
const result = strategy.unregisterHardware('hw-1')
|
|
expect(result).toBe(true)
|
|
expect(strategy.getRegisteredCount()).toBe(0)
|
|
})
|
|
|
|
it('注销不存在的五金件应该返回 false', () => {
|
|
const result = strategy.unregisterHardware('not-exist')
|
|
expect(result).toBe(false)
|
|
})
|
|
|
|
it('注销后应该从类型分组中移除', () => {
|
|
const instances = [
|
|
createMockInstance('hw-1', 'screw'),
|
|
createMockInstance('hw-2', 'screw'),
|
|
]
|
|
strategy.registerMany(instances)
|
|
|
|
strategy.unregisterHardware('hw-1')
|
|
|
|
const groups = strategy.getTypeGroups()
|
|
expect(groups.get('screw')?.length).toBe(1)
|
|
expect(groups.get('screw')).toContain('hw-2')
|
|
})
|
|
|
|
it('clear 应该清空所有注册', () => {
|
|
const instances = [
|
|
createMockInstance('hw-1'),
|
|
createMockInstance('hw-2'),
|
|
]
|
|
strategy.registerMany(instances)
|
|
expect(strategy.getRegisteredCount()).toBe(2)
|
|
|
|
strategy.clear()
|
|
expect(strategy.getRegisteredCount()).toBe(0)
|
|
expect(strategy.getTypeGroups().size).toBe(0)
|
|
})
|
|
})
|
|
|
|
describe('显示控制', () => {
|
|
it('hideAll 应该隐藏所有五金件', () => {
|
|
const instances = [
|
|
createMockInstance('hw-1'),
|
|
createMockInstance('hw-2'),
|
|
]
|
|
strategy.registerMany(instances)
|
|
|
|
// 先显示
|
|
instances.forEach(i => i.mesh.visible = true)
|
|
|
|
strategy.hideAll()
|
|
|
|
instances.forEach(i => {
|
|
expect(i.mesh.visible).toBe(false)
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('阶段控制', () => {
|
|
it('初始阶段应该是 idle', () => {
|
|
expect(strategy.getCurrentPhase()).toBe('idle')
|
|
})
|
|
|
|
it('stop 应该重置阶段为 idle', () => {
|
|
strategy.stop()
|
|
expect(strategy.getCurrentPhase()).toBe('idle')
|
|
})
|
|
})
|
|
|
|
describe('回调', () => {
|
|
it('应该正确设置回调', () => {
|
|
const callbacks = {
|
|
onPhaseStart: jest.fn(),
|
|
onPhaseComplete: jest.fn(),
|
|
onSequenceComplete: jest.fn(),
|
|
}
|
|
|
|
expect(() => strategy.setCallbacks(callbacks)).not.toThrow()
|
|
})
|
|
})
|
|
|
|
describe('控制器', () => {
|
|
it('应该正确设置轨道控制器', () => {
|
|
const mockControls = {
|
|
target: new THREE.Vector3(),
|
|
update: jest.fn(),
|
|
}
|
|
|
|
expect(() => strategy.setControls(mockControls)).not.toThrow()
|
|
})
|
|
})
|
|
|
|
describe('类型分组', () => {
|
|
it('getTypeGroups 应该返回正确的分组', () => {
|
|
const instances = [
|
|
createMockInstance('hw-1', 'type-a'),
|
|
createMockInstance('hw-2', 'type-a'),
|
|
createMockInstance('hw-3', 'type-b'),
|
|
createMockInstance('hw-4'), // 无类型
|
|
]
|
|
strategy.registerMany(instances)
|
|
|
|
const groups = strategy.getTypeGroups()
|
|
|
|
expect(groups.size).toBe(2)
|
|
expect(groups.get('type-a')).toEqual(['hw-1', 'hw-2'])
|
|
expect(groups.get('type-b')).toEqual(['hw-3'])
|
|
})
|
|
|
|
it('注销后类型分组应该为空时被删除', () => {
|
|
const instance = createMockInstance('hw-1', 'only-one')
|
|
strategy.registerHardware(instance)
|
|
|
|
expect(strategy.getTypeGroups().has('only-one')).toBe(true)
|
|
|
|
strategy.unregisterHardware('hw-1')
|
|
|
|
expect(strategy.getTypeGroups().has('only-one')).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('销毁', () => {
|
|
it('dispose 应该清理所有资源', () => {
|
|
const instances = [
|
|
createMockInstance('hw-1'),
|
|
createMockInstance('hw-2'),
|
|
]
|
|
strategy.registerMany(instances)
|
|
|
|
strategy.dispose()
|
|
|
|
expect(strategy.getRegisteredCount()).toBe(0)
|
|
expect(strategy.getCurrentPhase()).toBe('idle')
|
|
})
|
|
})
|
|
})
|