import assert from 'node:assert/strict' import test from 'node:test' // 每个用例动态加载真实实现,使首次运行能够直接报告尚未实现的菜单行为。 const implementationUrl = new URL('../src/router/menuFilter.js', import.meta.url) test('混合菜单仅删除语音语料和缺失页面,保留其他业务及权限元数据', async () => { const { filterAvailableMenus } = await import(implementationUrl) const routes = [{ path: '/business', component: 'Layout', meta: { title: '业务' }, children: [ { path: 'voice', component: 'vi/project/index' }, { path: 'corpus', component: 'corpusManger/index' }, { path: 'touch', component: 'is/project/index', permissions: ['is:project:list'] }, { path: 'evaluation', component: 'evaluate/il/touch/index', roles: ['admin'] }, { path: 'robot', component: 'resource/robot/index' }, { path: 'device', component: 'resource/device/index' }, { path: 'map', component: 'resource/map/index' }, { path: 'terminal', component: 'resource/terminal/index' }, { path: 'calibration', component: 'resource/visionCalibration/index' }, { path: 'flow', component: 'flow/index' }, { path: 'test', component: 'test/history/index' }, { path: 'system', component: 'system/user/index' }, { path: 'monitor', component: 'monitor/job/index' }, { path: 'tool', component: 'tool/gen/index' }, { path: 'microphone', component: 'device/register/components/Microphone/index' }, { path: 'speaker', component: 'device/register/components/Speaker/index' }, { path: 'missing', component: 'removed/index' } ] }] const available = [ 'is/project/index', 'evaluate/il/touch/index', 'resource/robot/index', 'resource/device/index', 'resource/map/index', 'resource/terminal/index', 'resource/visionCalibration/index', 'flow/index', 'test/history/index', 'system/user/index', 'monitor/job/index', 'tool/gen/index', 'device/register/components/Microphone/index', 'device/register/components/Speaker/index' ] assert.deepEqual(filterAvailableMenus(routes, available), [{ path: '/business', component: 'Layout', meta: { title: '业务' }, children: [ { path: 'touch', component: 'is/project/index', permissions: ['is:project:list'] }, { path: 'evaluation', component: 'evaluate/il/touch/index', roles: ['admin'] }, { path: 'robot', component: 'resource/robot/index' }, { path: 'device', component: 'resource/device/index' }, { path: 'map', component: 'resource/map/index' }, { path: 'terminal', component: 'resource/terminal/index', hidden: true }, { path: 'calibration', component: 'resource/visionCalibration/index', hidden: true }, { path: 'flow', component: 'flow/index' }, { path: 'test', component: 'test/history/index' }, { path: 'system', component: 'system/user/index' }, { path: 'monitor', component: 'monitor/job/index' }, { path: 'tool', component: 'tool/gen/index' }, { path: 'microphone', component: 'device/register/components/Microphone/index' }, { path: 'speaker', component: 'device/register/components/Speaker/index' } ] }]) }) test('资源中心隐藏边缘终端和视觉标定菜单,保留路由、权限及其他菜单', async () => { const { filterAvailableMenus } = await import(implementationUrl) const routes = [{ path: '/resource', component: 'Layout', children: [ { path: 'robot', component: 'resource/robot/index' }, { path: 'device', component: 'resource/device/index' }, { path: 'map', component: 'resource/map/index' }, { path: 'terminal', component: '@/views/resource/terminal/index.vue', hidden: false, permissions: ['resource:terminal:list'] }, { path: 'calibration', component: 'src/views/resource/visionCalibration/index.vue', hidden: false } ] }] const original = structuredClone(routes) const filtered = filterAvailableMenus(routes, [ 'resource/robot/index', 'resource/device/index', 'resource/map/index', 'resource/terminal/index', 'resource/visionCalibration/index' ]) assert.deepEqual(filtered[0].children.filter(route => !route.hidden).map(route => route.path), ['robot', 'device', 'map']) assert.equal(filtered[0].children.length, 5) assert.deepEqual(filtered[0].children[3].permissions, ['resource:terminal:list']) assert.deepEqual(routes, original) }) test('已移除语音语料组件即使出现在可用视图中也不能恢复', async () => { const { filterAvailableMenus } = await import(implementationUrl) const routes = [ { path: 'voice-root', component: 'vi' }, { path: 'voice', component: '@/views/vi/project/index.vue' }, { path: 'corpus', component: '/src/views/corpusManger/index.vue' }, { path: 'evaluation', component: 'evaluate/il/speech/index' }, { path: 'synthesis', component: 'device\\register\\components\\SpeechSynthesis\\index.vue' }, { path: 'vision', component: 'vision/index' }, { path: 'other-evaluation', component: 'evaluate/il/speechMetrics/index' } ] const available = [ 'vi', 'vi/project/index', 'corpusManger/index', 'evaluate/il/speech/index', 'device/register/components/SpeechSynthesis/index', 'vision/index', 'evaluate/il/speechMetrics/index' ] assert.deepEqual(filterAvailableMenus(routes, available), [ { path: 'vision', component: 'vision/index' }, { path: 'other-evaluation', component: 'evaluate/il/speechMetrics/index' } ]) }) test('组件路径兼容后端常见前缀、扩展名和斜杠格式', async () => { const { filterAvailableMenus, normalizeViewPath } = await import(implementationUrl) const components = [ 'resource/robot/index', '/resource/robot/index.vue', '@/views/resource/robot/index.vue', 'src/views/resource/robot/index.vue', '/src/views/resource/robot/index.vue', 'views/resource/robot/index.vue', './../../views/resource/robot/index.vue', ' resource\\robot\\index.vue ', './src/views/resource//robot/index.vue' ] const routes = components.map((component, index) => ({ path: `robot-${index}`, component })) assert.deepEqual(filterAvailableMenus(routes, new Set(['./../../views/resource/robot/index.vue'])), routes) for (const component of components) { assert.equal(normalizeViewPath(component), 'resource/robot/index') } }) test('可用视图同样归一化,名称不完整或大小写错误的页面仍会删除', async () => { const { filterAvailableMenus } = await import(implementationUrl) assert.deepEqual(filterAvailableMenus([ { path: 'user', component: 'system/user/index' }, { path: 'case-error', component: 'System/user/index' }, { path: 'partial', component: 'user/index' }, { path: 'no-index', component: 'system/user' } ], ['src\\views\\system\\user\\index.vue']), [ { path: 'user', component: 'system/user/index' } ]) }) test('递归删除空的 Layout、ParentView 和无页面目录', async () => { const { filterAvailableMenus } = await import(implementationUrl) const routes = [ { path: '/empty-layout', component: 'Layout' }, { path: '/empty-parent', component: 'ParentView', children: [] }, { path: '/empty-group' }, { path: '/voice', component: 'Layout', children: [{ path: 'projects', component: 'ParentView', children: [{ path: 'index', component: 'vi/project/index' }] }] }, { path: '/business', component: 'Layout', children: [ { path: 'removed', component: 'ParentView', children: [{ path: 'missing', component: 'removed/index' }] }, { path: 'group', children: [{ path: 'robot', component: 'resource/robot/index' }] } ] } ] assert.deepEqual(filterAvailableMenus(routes, ['resource/robot/index']), [{ path: '/business', component: 'Layout', children: [ { path: 'group', children: [{ path: 'robot', component: 'resource/robot/index' }] } ] }]) }) test('真实页面的子菜单被清空后仍保留该页面', async () => { const { filterAvailableMenus } = await import(implementationUrl) assert.deepEqual(filterAvailableMenus([{ path: 'robot', component: 'resource/robot/index', children: [{ path: 'old', component: 'removed/index' }] }], ['resource/robot/index']), [{ path: 'robot', component: 'resource/robot/index', children: [] }]) }) test('外链和 InnerLink 不需要对应本地视图且可以保留在目录下', async () => { const { filterAvailableMenus } = await import(implementationUrl) const routes = [ { path: 'https://example.com/docs', component: 'Layout', meta: { title: '文档' } }, { path: 'https://example.com/support' }, { path: 'mailto:support@example.com' }, { path: 'tel:123456' }, { path: '/links', component: 'Layout', children: [ { path: 'docs', component: 'InnerLink', meta: { link: 'https://example.com/docs' } } ] } ] assert.deepEqual(filterAvailableMenus(routes, []), routes) }) test('过滤不改动输入,后续路由转换也不会改写原始路由节点', async () => { const { filterAvailableMenus } = await import(implementationUrl) const routes = [{ path: '/resource', component: 'Layout', children: [ { path: 'robot', name: 'Index', component: 'resource/robot/index', meta: { title: '机器人' } }, { path: 'old', component: 'vi/project/index' } ] }] const original = structuredClone(routes) freezeTree(routes) const available = Object.freeze(['resource/robot/index']) const filtered = filterAvailableMenus(routes, available) assert.deepEqual(routes, original) filtered[0].component = 'resolved layout' filtered[0].children[0].name = 'Renamed' filtered[0].children[0].component = 'resolved view' assert.deepEqual(routes, original) assert.deepEqual(filterAvailableMenus([], available), []) }) test('重名处理继续保留所有菜单并给冲突名称添加完整路径后缀', async () => { const { filterAvailableMenus, normalizeRouteNames } = await import(implementationUrl) const routes = filterAvailableMenus([ { path: '/resource', component: 'Layout', children: [ { path: 'robot', name: 'Index', component: 'resource/robot/index' }, { path: 'device', name: 'Index', component: 'resource/device/index' }, { path: 'device', name: 'Index', component: 'resource/device/index' } ] }, { path: '/system', component: 'Layout', children: [ { path: 'user', name: 'Index', component: 'system/user/index' } ] } ], ['resource/robot/index', 'resource/device/index', 'system/user/index']) normalizeRouteNames(routes) assert.deepEqual(routes[0].children.map(route => route.name), ['Index', 'Index_resource_device', 'Index_resource_device_2']) assert.deepEqual(routes[1].children.map(route => route.name), ['Index_system_user']) }) /** * 冻结测试输入的所有层级,使原地修改菜单或子节点时立即失败。 * @param {object} value 需要保护的菜单树或嵌套字段。 * @returns {object} 原对象;仅用于测试输入保护。 */ function freezeTree(value) { for (const nested of Object.values(value)) { if (nested && typeof nested === 'object') freezeTree(nested) } return Object.freeze(value) }