// @ts-ignore import { illusionModule } from '../wasm/illusion.js'; import { IllusionCore } from './illusion-core'; import { MessageType } from './message-type'; import { filterHandler } from './handlers/filter-handler'; import { FilterType } from '../enums/filter-type'; import { HandlerType } from '../enums/handler-type'; jest.mock('../wasm/illusion.js'); describe('core/illusionCore', () => { it('指令参数正确传递,并获取正确返回', async () => { const illusionCore = new IllusionCore(); const { id: filterId } = await illusionCore.do(MessageType.filter.createByFilterType, [FilterType.BILATERAL]); expect(illusionModule.Filter.createByFilterType.mock.calls[0]).toEqual([FilterType.BILATERAL]); expect(filterHandler.map.has(filterId)).toBe(true); const params = await illusionCore.do(MessageType.filter.getParams, [filterId]); const keys = Object.getOwnPropertyNames(params); expect(keys.length).toBe(3); expect(keys).toContain('floatParam2'); const param = params['floatParam2']; expect(param.type).toBe(1); expect(param.dimension).toBe(3); }); it('指令类型错误时,抛出异常', async () => { const illusionCore = new IllusionCore(); const command = 'invalid' try { await illusionCore.do(command); throw new Error('不能执行到这里'); } catch(error) { expect(error.message).toBe(`invalid command type: ${command}`); } }); it('行为类型错误时,抛出异常', async() => { const illusionCore = new IllusionCore(); const command = 'layer.createFilter'; try { await illusionCore.do(command); throw new Error('不能执行到这里'); } catch(error) { expect(error.message).toBe('invalid action type: createFilter'); } }); it('HandlerType 中的所有字段有在 handlerMap 中建立映射', () => { for(const key in HandlerType) { // @ts-ignore const handlerType = HandlerType[key]; // @ts-ignore expect(IllusionCore.handlerMap[handlerType]).toBeDefined(); } }); });