import type { RouteRecordRaw } from 'vue-router'; import { cloneDeep, generateRoutesByFrontend } from '@vben/utils'; import { describe, expect, it } from 'vitest'; import { firstMenuPath, parentPaths, pruneEmptyMenus } from './menu-access'; import consoleRoutes from './routes/modules/console'; const routes = [ { children: [{ path: 'catalog' }], path: '/model' }, { children: [{ path: 'accounts' }], path: '/desktop' }, { children: [{ path: 'center' }], path: '/knowledge' }, { path: '/profile' }, ] as unknown as RouteRecordRaw[]; describe('menus after button-level filtering', () => { it('drops top-level menus whose pages were all filtered out', () => { const menus = [ { children: [{ path: '/model/catalog' }], path: '/model' }, { children: [], path: '/desktop' }, { children: [{ path: '/knowledge/center' }], path: '/knowledge' }, { path: '/profile' }, ]; expect(pruneEmptyMenus(menus, parentPaths(routes)).map((menu) => menu.path)).toEqual(['/model', '/knowledge', '/profile']); }); it('lands on the first page the user can open', () => { expect(firstMenuPath([ { path: '/profile', show: false }, { children: [{ path: 'https://example.com/report' }, { path: '/knowledge/center' }], path: '/knowledge' }, ])).toBe('/knowledge/center'); expect(firstMenuPath([])).toBeUndefined(); }); it.each([ [['knowledge:review:assign'], ['assignments']], [['knowledge:read', 'knowledge:review', 'im:content:read'], ['center']], [['knowledge:read', 'knowledge:review:assign'], ['center', 'assignments']], [['knowledge:tasks:read'], ['tasks']], [['knowledge:search'], ['search']], [['knowledge:tasks:read', 'knowledge:search'], ['tasks', 'search']], [['knowledge:review', 'knowledge:run', 'knowledge:run:control', 'knowledge:watch', 'im:content:read'], []], ])('filters the actual knowledge routes with %j', async (codes, visible) => { const filtered = await generateRoutesByFrontend(cloneDeep(consoleRoutes), codes); const knowledge = filtered.find((route) => route.path === '/knowledge'); expect(knowledge?.children?.map((route) => route.path) ?? []).toEqual(visible); const knowledgeParent = consoleRoutes.find((route) => route.path === '/knowledge'); expect(knowledgeParent?.meta?.authority).toEqual([ 'knowledge:read', 'knowledge:review:assign', 'knowledge:tasks:read', 'knowledge:search', ]); }); });