import test from 'node:test' import assert from 'node:assert/strict' import { organizeBusinessMenus } from './businessMenus.mjs' import { resolveNavigationPath } from './navigationPath.mjs' test('坐标系建立归配置中心,位于夹具之后且不凭空增加无权限入口', () => { const routes = [ { path: '/lab/master', children: [ { path: 'coordinate-system', meta: { title: '坐标系建立' } }, { path: 'fixture', meta: { title: '夹具管理' } } ] } ] const menus = organizeBusinessMenus(routes) assert.equal(menus[0].meta.title, '配置中心') assert.deepEqual( menus[0].children.map((n) => n.path), ['/lab/master/fixture', '/lab/master/coordinate-system'] ) assert.equal( organizeBusinessMenus([{ path: '/lab/master', children: [{ path: 'fixture' }] }])[0].children .length, 1 ) }) test('菜单按业务组织,但不修改原注册路由或丢失系统菜单', () => { const routes = [ { path: '/lab/task', meta: { title: '任务管理' }, children: [ { path: 'pool', meta: { title: '任务池' } }, { path: 'import', meta: { title: '导入' } } ] }, { path: '/cmvr/intelligence-test', children: [{ path: 'test-case', meta: { title: '测试用例' } }] }, { path: '/cmvr/resource', children: [ { path: 'robot', meta: { title: '机器人' } }, { path: 'device', meta: { title: '设备' } } ] }, { path: '/lab/document', children: [{ path: 'report', meta: { title: '报告' } }] }, { path: '/system', meta: { title: '系统管理' } } ] const before = JSON.stringify(routes), menus = organizeBusinessMenus(routes) assert.equal(JSON.stringify(routes), before) assert.deepEqual( menus.map((m) => m.meta.title), ['任务中心', '成果中心', '配置中心', '设备与资源', '系统管理'] ) assert.equal(menus[2].children[0].path, '/cmvr/intelligence-test/test-case') assert.equal(menus[3].children[1].path, '/cmvr/resource/device') assert.equal(menus[0].children[2].path, '/lab/task/import') for (const group of menus.filter((m) => m.children)) { for (const child of group.children) { assert.equal(resolveNavigationPath(group.path, child.path), child.path) } } }) test('没有任务菜单权限时不凭空增加任务或成果入口', () => { const menus = organizeBusinessMenus([ { path: '/cmvr/resource', children: [{ path: 'device', meta: { title: '设备' } }] } ]) assert.equal(menus.length, 1) assert.equal(menus[0].meta.title, '设备与资源') assert.equal(menus[0].children.length, 1) }) test('隐藏目录不被导航整理意外展开,只有设备档案权限的角色仍有原入口', () => { const menus = organizeBusinessMenus([ { path: '/lab/integration', meta: { hidden: true }, children: [{ path: 'provider', meta: { title: '接入方' } }] }, { path: '/lab/master', children: [{ path: 'device', meta: { title: '设备档案' } }] } ]) assert.equal(menus.length, 1) assert.equal(menus[0].meta.title, '设备与资源') assert.equal(menus[0].children[0].path, '/lab/master/device') }) test('原相对菜单、绝对跨组菜单、空子路径和外部链接均保留正确地址', () => { assert.equal(resolveNavigationPath('/system', 'user'), '/system/user') assert.equal( resolveNavigationPath('/cmvr/resource', '/cmvr/resource/device'), '/cmvr/resource/device' ) assert.equal( resolveNavigationPath('/lab/master', '/cmvr/intelligence-test/test-case'), '/cmvr/intelligence-test/test-case' ) assert.equal(resolveNavigationPath('/lab/task', '/lab/dimension/tasks'), '/lab/dimension/tasks') assert.equal(resolveNavigationPath('/system', ''), '/system') assert.equal( resolveNavigationPath('/system', 'https://example.com/help'), 'https://example.com/help' ) }) test('成果保留独立入口,旧数据折叠;异常归维护且不修改原路由或授权菜单', () => { const routes = [ { path: '/lab/task', children: [{ path: 'pool' }] }, { path: '/lab/result', children: [{ path: 'index' }] }, { path: '/lab/document', children: ['report', 'record', 'template', 'package'].map((path) => ({ path })) }, { path: '/lab/data', children: [{ path: 'files' }, { path: 'upload-failures' }] }, { path: '/system', meta: { title: '系统管理' }, children: [{ path: 'user', meta: { title: '用户管理' } }] } ] const before = JSON.stringify(routes), menus = organizeBusinessMenus(routes) assert.equal(JSON.stringify(routes), before) const results = menus.find((n) => n.meta.title === '成果中心') assert.deepEqual( results.children.map((n) => n.meta.title), ['最终成果', '文件查询', '历史数据'] ) const history = results.children[2] assert.deepEqual( history.children.map((n) => n.path), ['/lab/result/index', '/lab/document/report', '/lab/document/record', '/lab/document/package'] ) assert.equal(history.meta.alwaysShow, true) const system = menus.find((n) => n.path === '/system') assert.equal(system.children[0].path, 'user') assert.equal(system.children[1].children[0].path, '/lab/data/upload-failures') const paths = [] const visit = (nodes) => nodes.forEach((n) => { paths.push(n.path) if (n.children) visit(n.children) }) visit(menus) assert.equal(paths.includes('/lab/document/template'), false) assert.equal(paths.filter((p) => p === '/lab/data/upload-failures').length, 1) }) test('只有异常查看权限时提供维护入口,不增加其他系统权限或空成果目录', () => { const menus = organizeBusinessMenus([ { path: '/lab/data', children: [{ path: 'upload-failures' }] } ]) assert.equal(menus.length, 1) assert.equal(menus[0].meta.title, '系统维护') assert.deepEqual( menus[0].children.map((n) => n.path), ['/lab/data/upload-failures'] ) assert.deepEqual( organizeBusinessMenus([{ path: '/lab/document', children: [{ path: 'template' }] }]), [] ) })