06c1a3accd
- 新建 src/config/company-scope.ts:SUPER_ADMIN_ONLY_MENU_KEYS=['admin_companies'] 与 COMPANY_SCOPED_PAGES(批次 1 的 8 页口径清单,后续任务的唯一分母) - routes.ts:AdminPageDef 加 superAdminOnly/companyScoped;公司管理标 superAdminOnly: true, 8 个 A 类页标 companyScoped: true,C 类两页显式写 false(声明而非留空) - helpers.ts:pruneSuperAdminOnly 纯函数(超管原样返回,非超管递归剪掉专属节点) - router/index.ts:菜单授权之外补一条与菜单树无关的角色判定 - admin-session.ts:菜单树唯一写点内接剪枝(顺序仍在 fetchCurrentUser 之后) - 新增 tests/company-super-admin-only.test.ts 5 支护栏 - task-29 的主路径断言随唯一写点改写(原断言只认 canonicalize 裸赋值, 剪枝并入同一写点后必然失配;改为同时要求 prune + canonicalize,意图不降反强)
48 lines
2.6 KiB
TypeScript
48 lines
2.6 KiB
TypeScript
import test from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { readSource } from './helpers.ts'
|
||
import { adminPages } from '../src/router/routes.ts'
|
||
import { pruneSuperAdminOnly } from '../src/router/helpers.ts'
|
||
import { SUPER_ADMIN_ONLY_MENU_KEYS } from '../src/config/company-scope.ts'
|
||
|
||
/** 公司配置是角色内在闸门,不是菜单授权(spec §2:菜单授权可以被人改坏,role 不能)。
|
||
* 12:3x 事故的教训是"把页面可见性当数据可见性用",这里反过来防另一个方向:只挂一处会留旁路。 */
|
||
|
||
test('超管专属页必须同时被注册表与口径清单认领(两处不一致就是漏了一处)', () => {
|
||
const flagged = adminPages.filter((p) => p.superAdminOnly).map((p) => p.menuKey)
|
||
assert.deepEqual(flagged.sort(), [...SUPER_ADMIN_ONLY_MENU_KEYS].sort())
|
||
assert.ok(SUPER_ADMIN_ONLY_MENU_KEYS.includes('admin_companies'))
|
||
})
|
||
|
||
test('路由守卫里必须有一条与菜单树无关的角色判定', () => {
|
||
const guard = readSource('src/router/index.ts')
|
||
assert.match(guard, /superAdminOnly/, '守卫没查角色硬闸门')
|
||
assert.match(guard, /isSuperAdmin/, '守卫必须先判 session.isSuperAdmin')
|
||
})
|
||
|
||
test('剪枝:非超管拿不到超管专属节点,超管一个字节都不少', () => {
|
||
const tree = [
|
||
{ key: 'g1', children: [{ key: 'admin_companies', children: [] }, { key: 'admin_users', children: [] }] },
|
||
{ key: 'admin_history', children: [] },
|
||
]
|
||
const forAdmin = pruneSuperAdminOnly(tree, false)
|
||
assert.equal(JSON.stringify(forAdmin).includes('admin_companies'), false, '非超管菜单树里不许留公司管理')
|
||
assert.ok(JSON.stringify(forAdmin).includes('admin_users'), '普通节点不许被误剪')
|
||
assert.equal(pruneSuperAdminOnly(tree, true), tree, '超管原样返回,不做无谓拷贝')
|
||
})
|
||
|
||
test('剪枝是纯函数且幂等(store 里会反复调用)', () => {
|
||
const tree = [{ key: 'admin_companies', children: [] as unknown[] }]
|
||
const once = pruneSuperAdminOnly(tree, false)
|
||
assert.deepEqual(pruneSuperAdminOnly(once, false), once)
|
||
})
|
||
|
||
test('C 类两页必须显式声明 companyScoped: false(spec §5:声明,不是遗漏)', () => {
|
||
// 留空和 false 运行时等价,但只有 false 是"读过普查、决定不参与"的证据。
|
||
for (const key of ['admin_shop_keys', 'admin_product_categories']) {
|
||
const page = adminPages.find((p) => p.menuKey === key)
|
||
assert.ok(page, `注册表里找不到 ${key}`) // 取样非空自检
|
||
assert.equal(page.companyScoped, false, `${key} 必须显式写 companyScoped: false,不能留空`)
|
||
}
|
||
})
|