import {test,expect,vi,beforeAll,beforeEach} from 'vitest' import {createPinia,setActivePinia,defineStore} from 'pinia' import {reactive} from 'vue' const data=vi.hoisted(()=>({user:null,getRouters:vi.fn(),remove:vi.fn()})) vi.mock('@/store/modules/user',()=>({default:()=>data.user})) vi.mock('@/api/menu',()=>({getRouters:data.getRouters})) vi.mock('@/plugins/auth',()=>({default:{hasPermiOr:()=>false,hasRoleOr:()=>false}})) vi.mock('@/router',()=>({default:{addRoute:vi.fn()},dynamicRoutes:[],constantRoutes:[{path:'',children:[{path:'/index',name:'Index',meta:{title:'首页',affix:true,homePage:true}}]},{path:'/user',hidden:true,children:[{path:'profile',name:'Profile'}]}]})) vi.mock('@/layout/index',()=>({default:{}})) vi.mock('@/components/ParentView',()=>({default:{}})) vi.mock('@/layout/components/InnerLink',()=>({default:{}})) vi.mock('@/store/modules/settings',()=>({default:()=>({tagsViewPersist:true})})) vi.mock('@/plugins/cache',()=>({default:{local:{getJSON:()=>[],setJSON:vi.fn(),remove:data.remove}}})) let permissionStore,tagsStore beforeAll(async()=>{vi.stubGlobal('defineStore',defineStore);permissionStore=(await import('@/store/modules/permission')).default;tagsStore=(await import('@/store/modules/tagsView')).default}) beforeEach(()=>{ setActivePinia(createPinia());data.user=reactive({roles:['archive_manager'],permissions:['archive:project:list']}) data.getRouters.mockResolvedValue({data:[{path:'/',component:'Layout',children:[{path:'index',component:'businessHome/index',meta:{title:'首页'}}]},{path:'/',component:'Layout',children:[{path:'project-archive',component:'archive/index',meta:{title:'项目资料'}}]}]}) }) test('the actual permission store excludes homepage from sidebar and topbar and selects documents as landing',async()=>{ const store=permissionStore();await store.generateRoutes() expect(store.canHome).toBe(false);expect(store.landingPath).toBe('/project-archive') expect(store.routes.flatMap(r=>r.children||[]).some(r=>r.meta?.homePage)).toBe(false) expect(store.sidebarRouters.flatMap(r=>r.children||[]).some(r=>r.path==='index'||r.path==='/index')).toBe(false) expect(store.topbarRouters.some(r=>r.path==='/index')).toBe(false) }) test('authorized users receive exactly one homepage rather than a duplicate database route',async()=>{ data.user.permissions=['business:home:view'];const store=permissionStore();await store.generateRoutes() expect(store.canHome).toBe(true);expect(store.landingPath).toBe('/index') expect(store.routes.flatMap(r=>r.children||[]).filter(r=>r.path==='index'||r.path==='/index')).toHaveLength(1) expect(store.topbarRouters.filter(r=>r.path==='/index')).toHaveLength(1) }) test('account switching clears cached and persisted page tabs, not business records',()=>{ const store=tagsStore();store.visitedViews=[{path:'/index',meta:{affix:true}},{path:'/warehouse/stock'}];store.cachedViews=['/index','/warehouse/stock'];store.iframeViews=[{path:'/private'}] store.resetUserViews();expect(store.visitedViews).toEqual([]);expect(store.cachedViews).toEqual([]);expect(store.iframeViews).toEqual([]);expect(data.remove).toHaveBeenCalledWith('tags-view-visited') })