103 lines
5.3 KiB
JavaScript
103 lines
5.3 KiB
JavaScript
import test from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { filterRetainedRoutes, findFirstMenuRoute, isRemovedPath, resolveRoutePath } from '../src/utils/navigation.js'
|
||
|
||
test('删除旧模块整棵路由树,兼容自定义路径和后端组件标识', () => {
|
||
const input = [
|
||
...['vi', 'is'].map(path => ({ path: `/${path}`, children: [{ path: 'list' }] })),
|
||
{ path: '/index', meta: { title: '首页' } },
|
||
{ path: '/renamed', component: 'vi/project/index', meta: { title: '语音项目' } },
|
||
{ path: '/custom', meta: { title: '触控交互' }, children: [{ path: 'item' }] },
|
||
{ path: '/inspection', component: 'inspection/index', meta: { title: '智能巡检' } }
|
||
]
|
||
assert.deepEqual(filterRetainedRoutes(input).map(route => route.path), ['/inspection'])
|
||
})
|
||
|
||
test('只按路径段删除,不误伤其他业务、设备控制与通用index页面', () => {
|
||
for (const path of ['/inspection/robot', '/testing', '/island', '/device/index', '/speech_synthesis', '/touch_interaction', '/resource/device', '/test/configs#detail']) {
|
||
assert.equal(isRemovedPath(path), false, path)
|
||
}
|
||
for (const path of ['/index?old=1', '/vi', '/is/plan/index/1']) {
|
||
assert.equal(isRemovedPath(path), true, path)
|
||
}
|
||
})
|
||
|
||
test('恢复资源中心和测试管理授权菜单,保留自定义路径、权限与响应原对象', () => {
|
||
const input = [
|
||
{ path: '/resource', meta: { title: '资源中心' }, children: [
|
||
{ path: 'robot', component: 'resource/robot/index', meta: { title: '机器人' } }
|
||
] },
|
||
{ path: '/custom-test', meta: { title: '测试管理' }, children: [
|
||
{ path: 'tasks', component: 'test/configs/index', permissions: ['test:config:list'] }
|
||
] },
|
||
{ path: '/devices', component: 'resource/device/index', roles: ['admin'] }
|
||
]
|
||
const original = structuredClone(input)
|
||
assert.deepEqual(filterRetainedRoutes(input), original)
|
||
assert.deepEqual(findFirstMenuRoute(filterRetainedRoutes(input)), { path: '/resource/robot' })
|
||
assert.deepEqual(input, original)
|
||
})
|
||
|
||
test('沿用源项目隐藏终端和视觉标定菜单的规则,但保留路由与权限', () => {
|
||
const input = [{ path: '/resource', children: [
|
||
{ path: 'robot', component: 'resource/robot/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(input)
|
||
const result = filterRetainedRoutes(input)
|
||
assert.equal(result[0].children.length, 3)
|
||
assert.equal(result[0].children[0].hidden, undefined)
|
||
assert.equal(result[0].children[1].hidden, true)
|
||
assert.equal(result[0].children[2].hidden, true)
|
||
assert.deepEqual(result[0].children[1].permissions, ['resource:terminal:list'])
|
||
assert.deepEqual(input, original)
|
||
})
|
||
|
||
test('清理深层旧模块与空目录,保留其他系统菜单及原始输入', () => {
|
||
const input = [{ path: '/system', component: 'Layout', children: [
|
||
{ path: 'menu', component: 'system/menu/index', meta: { title: '菜单管理' } },
|
||
{ path: 'user', component: 'system/user/index', meta: { title: '用户管理' } },
|
||
{ path: 'old', component: 'ParentView', children: [{ path: '/vi/list' }] }
|
||
] }]
|
||
const original = structuredClone(input)
|
||
const result = filterRetainedRoutes(input)
|
||
assert.equal(result[0].children.length, 2)
|
||
assert.equal(result[0].children[0].hidden, true)
|
||
assert.equal(result[0].children[1].hidden, undefined)
|
||
assert.equal(filterRetainedRoutes(input, false)[0].children[0].hidden, undefined)
|
||
assert.deepEqual(input, original)
|
||
})
|
||
|
||
test('父级重定向到已删首页时不再保留该跳转', () => {
|
||
const result = filterRetainedRoutes([{ path: '/system', redirect: '/index', children: [
|
||
{ path: 'user', meta: { title: '用户管理' } }
|
||
] }])
|
||
assert.equal(result[0].redirect, undefined)
|
||
})
|
||
|
||
test('按后端顺序选择第一个可见保留叶子菜单,跳过隐藏父级和外链', () => {
|
||
const routes = [
|
||
{ path: '/hidden', hidden: true, children: [{ path: 'page', meta: { title: '隐藏页面' } }] },
|
||
{ path: 'https://example.com', meta: { title: '外链' } },
|
||
{ path: '/device', children: [
|
||
{ path: 'details/:id', meta: { title: '详情' } },
|
||
{ path: 'list', meta: { title: '设备列表' }, query: '{"online":true}' },
|
||
{ path: 'later', meta: { title: '后续页面' } }
|
||
] }
|
||
]
|
||
assert.deepEqual(findFirstMenuRoute(routes), { path: '/device/list', query: { online: true } })
|
||
})
|
||
|
||
test('绝对路径子路由不重复拼接父路径,未授权目录没有默认入口', () => {
|
||
assert.equal(resolveRoutePath('/system', '/system/user'), '/system/user')
|
||
assert.equal(resolveRoutePath('/system/', 'user'), '/system/user')
|
||
assert.deepEqual(findFirstMenuRoute([{ path: '/system', children: [{ path: '/system/user', meta: { title: '用户管理' } }] }]), { path: '/system/user' })
|
||
assert.equal(findFirstMenuRoute([]), null)
|
||
assert.equal(findFirstMenuRoute([{ path: '/system', meta: { title: '系统管理' }, children: [{ path: 'menu', hidden: true, meta: { title: '菜单管理' } }] }]), null)
|
||
})
|
||
|
||
test('无效菜单query不阻断默认导航', () => {
|
||
assert.deepEqual(findFirstMenuRoute([{ path: '/device', meta: { title: '设备' }, query: 'invalid json' }]), { path: '/device' })
|
||
})
|