140 lines
8.7 KiB
JavaScript
140 lines
8.7 KiB
JavaScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { buildNavigation } from './navigation.mjs'
|
|
import { projectResources } from './resources.mjs'
|
|
import { OFFICIAL_ORIGIN } from './policy.mjs'
|
|
|
|
const base = OFFICIAL_ORIGIN + '/admin/'
|
|
const leaf = (id, name, paths, more = {}) => ({ type: 'C', id, name, paths,
|
|
component: 'order/index', perms: 'order.order/lists', is_show: 1, is_disable: 0, ...more })
|
|
const group = (id, name, paths, children, more) => ({ ...leaf(id, name, paths), type: 'M', children, ...more })
|
|
const shape = tree => tree.map(node => [node.id, node.title, node.children ? shape(node.children) : null])
|
|
|
|
test('preserves server order, real titles/icons, nested and duplicate menu placements independent of live support', () => {
|
|
const menu = [
|
|
group(100, '诊疗管理', '/clinics/', [
|
|
leaf(200, '随访', '/follow/up/', { component: 'other/follow', perms: 'other.follow/lists', sort: 30 }),
|
|
group(101, '组内管理', 'team', [leaf(9, '台账', 'bills', { icon: 'local-icon-qianbao' })])
|
|
], { icon: 'el-icon-Lock' }),
|
|
group(20, '运营管理', 'ops', [
|
|
leaf(8, '台账', 'order', { icon: 'el-icon-Tickets' }),
|
|
leaf(7, '订单回访', 'revisit', { component: '/order/index/' })
|
|
], { sort: 999 })
|
|
]
|
|
const { tree, entries, firstId } = buildNavigation(menu, ['orders'])
|
|
assert.deepEqual(shape(tree), [
|
|
['m-100', '诊疗管理', [['m-200', '随访', null], ['m-101', '组内管理', [['m-9', '台账', null]]]]],
|
|
['m-20', '运营管理', [['m-8', '台账', null], ['m-7', '订单回访', null]]]
|
|
])
|
|
assert.deepEqual(entries.map(e => [e.id, e.liveKey, e.officialHref]), [
|
|
['m-200', null, base + 'clinics/follow/up'], ['m-9', 'orders', base + 'clinics/team/bills'],
|
|
['m-8', 'orders', base + 'ops/order'], ['m-7', 'orders', base + 'ops/revisit']
|
|
])
|
|
assert.equal(firstId, 'm-9')
|
|
assert.deepEqual(entries[1].breadcrumbs, ['诊疗管理', '组内管理', '台账'])
|
|
assert.equal(tree[0].icon, 'el-icon-Lock')
|
|
assert.equal(entries[1].icon, 'local-icon-qianbao')
|
|
assert.equal(entries[2].icon, 'el-icon-Tickets')
|
|
})
|
|
|
|
test('prunes whole disabled/hidden/button branches; menu with visible children is a group; empty visible directory remains', () => {
|
|
const m = leaf(1, '订单', 'order')
|
|
const menu = [
|
|
group(4, '隐藏', 'hide', [m], { is_show: '0' }), group(5, '停用', 'stop', [m], { is_disable: '1' }),
|
|
group(6, '按钮', 'action', [m], { type: 'A' }),
|
|
leaf(7, '目录页面', 'page', { children: [leaf(8, '订单查询', 'orders', { is_show: '1', is_disable: '0' })] }),
|
|
group(9, '权限管理', 'acl', [leaf(10, '不可见', 'hidden', { is_show: false })]),
|
|
leaf(11, '禁用', 'notHere', { is_disable: true }), leaf(12, '隐藏', 'hidden', { is_show: 0 })
|
|
]
|
|
const { tree, entries } = buildNavigation(menu, ['orders'])
|
|
assert.deepEqual(shape(tree), [['m-7', '目录页面', [['m-8', '订单查询', null]]], ['m-9', '权限管理', null]])
|
|
assert.deepEqual(entries.map(e => [e.id, e.liveKey]), [['m-8', 'orders'], ['m-9', null]])
|
|
assert.equal(entries[1].officialHref, base + 'acl')
|
|
})
|
|
|
|
test('navigation never expands keys; exact component, per-instance permission and authorized key all required', () => {
|
|
const m = leaf(1, '订单', 'order')
|
|
const menu = [m, { ...m, id: 2, perms: 'order.order/edit' }, { ...m, id: 3, component: 'alt/order/index' },
|
|
{ ...m, id: 4, component: 'https://example.invalid/file.vue' },
|
|
{ ...m, id: 5, paths: 'https://example.invalid/order' }]
|
|
const self = { user: { id: 64 }, menu, permissions: ['order.order/lists'] }
|
|
const { keys } = projectResources(self)
|
|
assert.deepEqual(keys, ['orders'])
|
|
assert.deepEqual(buildNavigation(menu, keys).entries.map(x => x.liveKey), ['orders', null, null, null, null])
|
|
for (const keys of [[], ['*'], ['ORDER'], undefined]) {
|
|
const n = buildNavigation(menu, keys)
|
|
assert.deepEqual(n.entries.map(x => x.liveKey), [null, null, null, null, null])
|
|
assert.equal(n.firstId, 'm-1')
|
|
}
|
|
assert.ok(!JSON.stringify(buildNavigation(menu, keys)).includes('file.vue'))
|
|
})
|
|
|
|
test('safe dedicated params field accepts both official syntaxes without passing redirects/credentials/second queries', () => {
|
|
const menu = [
|
|
leaf(1, '订单一', '/order', { params: '{"id":42,"type":"分类","name":"张 三","token":"SECRET","redirect":"https://evil.invalid"}' }),
|
|
leaf(2, '订单二', 'other', { params: 'tab=pending&next=evil&password=SECRET&status=1&__proto__=x&date=2026-12-01' }),
|
|
leaf(3, '详情', 'detail', { params: '{"id":"-1","name":{},"type":"//evil.invalid","token":"SECRET"}' }),
|
|
leaf(4, '异常', 'bad', { params: '{"tab":' }),
|
|
leaf(5, '双重编码', 'polluted', { params: 'tab=ok%26token%3DSECRET&type=%252f%252fother.invalid' })
|
|
]
|
|
const n = buildNavigation(menu, ['orders'])
|
|
assert.deepEqual(n.entries[0].query, { id: '42', type: '分类', name: '张 三' })
|
|
assert.deepEqual(n.tree[0].query, n.entries[0].query)
|
|
const directory = buildNavigation([group(900, '目录', 'dir', [menu[0]], { params: 'tab=all&token=SECRET' })], [])
|
|
assert.deepEqual(directory.tree[0].query, { tab: 'all' })
|
|
assert.deepEqual(directory.tree[0].children[0].query, n.entries[0].query)
|
|
assert.deepEqual(Object.fromEntries(new URL(n.entries[0].officialHref).searchParams), n.entries[0].query)
|
|
assert.deepEqual(n.entries[1].query, { tab: 'pending', status: '1', date: '2026-12-01' })
|
|
assert.equal(n.entries[2].officialHref, base + 'detail')
|
|
assert.equal(n.entries[3].query, undefined)
|
|
assert.equal(n.entries[4].query, undefined)
|
|
assert.ok(!JSON.stringify(n).includes('SECRET'))
|
|
})
|
|
|
|
test('invalid paths remain visible but inert, including descendants; explicit web links are distinct from local screens', () => {
|
|
const paths = ['../x', '%2e%2e/x', '/x//else', 'javascript:alert(1)', 'data:text/html,body',
|
|
'file:///tmp/none', 'mailto:name@example.invalid', '//elsewhere.invalid/path', 'https:///bad.example',
|
|
'https://user:secret@example.invalid', '\\evil', 'x?next=z', 'x#section', '\u0000path', '/%2felse']
|
|
const bad = paths.map((p, i) => leaf(i + 20, '未开放', p))
|
|
const menu = [...bad, group(1, '无效父', '../unsafe', [leaf(2, '子菜单', 'order')]),
|
|
leaf(3, '说明', 'https://docs.example.invalid/start?tab=first&token=SECRET#info'),
|
|
leaf(4, '帮助', 'http://docs.example.invalid/help', { params: 'type=admin' })]
|
|
const n = buildNavigation(menu, ['orders'])
|
|
assert.equal(n.entries.length, bad.length + 3)
|
|
for (const e of n.entries.slice(0, -2)) {
|
|
assert.equal(e.officialHref, null); assert.equal(e.liveKey, null)
|
|
}
|
|
assert.deepEqual(n.entries.slice(-2).map(x => [x.external, x.officialHref, x.liveKey]), [
|
|
[true, 'https://docs.example.invalid/start?tab=first#info', null],
|
|
[true, 'http://docs.example.invalid/help?type=admin', null]
|
|
])
|
|
assert.ok(!JSON.stringify(n).includes('SECRET'))
|
|
})
|
|
|
|
test('DB ids stable across reordering, missing ids path-derived, repeated paths distinct, empty inputs deterministic', () => {
|
|
const some = [leaf(null, '', 'first'), leaf('5', '二', 'second'), leaf(null, '三', 'third')]
|
|
const ids = rows => new Map(buildNavigation(rows, []).entries.map(e => [e.title, e.id]))
|
|
assert.deepEqual(ids(some), ids(some.toReversed()))
|
|
assert.match(ids(some).get('first'), /^p-[a-z0-9]+$/)
|
|
assert.equal(ids(some).get('二'), 'm-5')
|
|
const duplicated = [some[1], some[1], some[0], some[0]]
|
|
const e = buildNavigation(duplicated, []).entries
|
|
assert.equal(new Set(e.map(x => x.id)).size, 4)
|
|
for (const menu of [undefined, {}, [], [null, { type: 'A' }]]) {
|
|
assert.deepEqual(buildNavigation(menu, []), { tree: [], entries: [], firstId: null })
|
|
}
|
|
})
|
|
|
|
test('only registered Element icons or local SVG fragments reach the production dynamic icon component', () => {
|
|
const icons = ['el-icon-RouterView', 'el-icon-Iframe', 'local-icon-x#url', 'https://example.invalid/art.svg',
|
|
'el-icon-User', 'local-icon-set_pay', 'el-icon-house', 'el-icon-arrow-down', 'el-icon-arrowDown']
|
|
assert.deepEqual(buildNavigation(icons.map((icon, i) => leaf(i + 1, '名称', 'icon', { icon })), []).entries.map(e => e.icon),
|
|
['', '', '', '', 'el-icon-User', 'local-icon-set_pay', 'el-icon-House', 'el-icon-ArrowDown', 'el-icon-ArrowDown'])
|
|
})
|
|
|
|
|
|
test('an upstream absolute API action remains visible but never becomes a clickable handoff', () => {
|
|
const {entries}=buildNavigation([leaf(37,'同步配置',OFFICIAL_ORIGIN+'/adminapi/operation/run')],['orders'])
|
|
assert.equal(entries[0].title,'同步配置');assert.equal(entries[0].officialHref,null);assert.equal(entries[0].liveKey,null)
|
|
})
|