| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- import test from 'node:test'
- import assert from 'node:assert/strict'
- import { normalizeRootTypeId, collectHomeScopes, resolveHomeScope } from '../src/utils/homeScope.js'
- const homeMenus = [
- {
- path: '/subSystem/waterSupply',
- children: [
- { path: 'home', name: 'WaterHome', query: '{"rootTypeId":1}' }
- ]
- },
- {
- path: '/subSystem/drainage',
- children: [
- { path: 'home', name: 'DrainageHome', query: '{"rootTypeId":2}' }
- ]
- }
- ]
- test('只接受四个设备大类,绝不回退到默认值', () => {
- assert.equal(normalizeRootTypeId('1'), 1)
- assert.equal(normalizeRootTypeId(4), 4)
- assert.equal(normalizeRootTypeId(undefined), undefined)
- assert.equal(normalizeRootTypeId(''), undefined)
- assert.equal(normalizeRootTypeId(0), undefined)
- assert.equal(normalizeRootTypeId(5), undefined)
- assert.equal(normalizeRootTypeId('abc'), undefined)
- })
- test('URL 带 rootTypeId 时按 URL 取范围,不会变成窨井', () => {
- const scope = resolveHomeScope({
- query: { rootTypeId: '2' },
- path: '/subSystem/drainage/home',
- menus: homeMenus
- })
- assert.equal(scope.rootTypeId, 2)
- assert.equal(scope.source, 'url')
- })
- test('URL 丢失参数时从 /getRouters 的菜单 query 恢复', () => {
- const scope = resolveHomeScope({
- query: {},
- path: '/subSystem/waterSupply/home',
- menus: homeMenus
- })
- assert.equal(scope.rootTypeId, 1)
- assert.equal(scope.source, 'menu')
- })
- test('菜单 query 是 JSON 字符串也能解析', () => {
- const scopes = collectHomeScopes(homeMenus)
- assert.deepEqual(scopes.map(item => item.rootTypeId), [1, 2])
- assert.equal(scopes[0].path, '/subSystem/waterSupply/home')
- })
- test('未配置 query 时不返回任何范围,页面应报错而不是查窨井数据', () => {
- const scope = resolveHomeScope({ query: {}, path: '/subSystem/gas/home', menus: [] })
- assert.equal(scope.rootTypeId, undefined)
- assert.equal(scope.source, undefined)
- })
- test('多个首页菜单共用同一路径时由 URL 参数区分,不猜测默认值', () => {
- const menus = [
- { path: '/home', query: '{"rootTypeId":3}' },
- { path: '/home', query: '{"rootTypeId":4}' }
- ]
- assert.equal(resolveHomeScope({ query: { rootTypeId: 3 }, path: '/home', menus }).rootTypeId, 3)
- assert.equal(resolveHomeScope({ query: {}, path: '/home', menus }).rootTypeId, undefined)
- })
- test('账号只有一个已授权首页菜单时可据此兜底', () => {
- const scope = resolveHomeScope({ query: {}, path: '/other', menus: [homeMenus[0]] })
- assert.equal(scope.rootTypeId, 1)
- assert.equal(scope.source, 'only-menu')
- })
- test('兼容 ?query={"rootTypeId":N} 形式的透传链接', () => {
- const scope = resolveHomeScope({
- query: { query: '{"rootTypeId":3}' },
- path: '/subSystem/gas/home',
- menus: []
- })
- assert.equal(scope.rootTypeId, 3)
- })
|