import {test,expect,vi,beforeEach} from 'vitest' const state=vi.hoisted(()=>({guard:null,user:null,permission:null,lock:{isLock:false}})) vi.mock('@/router',()=>({default:{beforeEach:callback=>{state.guard=callback},afterEach:vi.fn()}})) vi.mock('@/utils/page-guards',()=>({confirmPageLeave:vi.fn().mockResolvedValue(true)})) vi.mock('@/utils/auth',()=>({getToken:()=> 'synthetic-token'})) vi.mock('@/utils/request',()=>({isRelogin:{show:false}})) vi.mock('@/store/modules/user',()=>({default:()=>state.user})) vi.mock('@/store/modules/permission',()=>({default:()=>state.permission})) vi.mock('@/store/modules/lock',()=>({default:()=>state.lock})) vi.mock('@/store/modules/settings',()=>({default:()=>({setTitle:vi.fn()})})) vi.mock('nprogress',()=>({default:{configure:vi.fn(),start:vi.fn(),done:vi.fn()}})) import '@/permission' beforeEach(()=>{ state.user={roles:['archive_manager'],getInfo:vi.fn(),logOut:vi.fn()} state.permission={canHome:false,landingPath:'/project-archive',generateRoutes:vi.fn().mockResolvedValue([])} }) const target=path=>({path,fullPath:path,meta:{}}),from=target('/login') test('an archive-only manager opening Index is redirected once to project documents',async()=>{ expect(await state.guard(target('/index'),from)).toEqual({path:'/project-archive',replace:true}) expect(await state.guard(target('/project-archive'),target('/index'))).toBe(true) expect(await state.guard(target('/redirect/index'),from)).toEqual({path:'/project-archive',replace:true}) }) test('authorized home remains accessible and a no-menu account falls back to profile without a loop',async()=>{ state.permission.canHome=true;expect(await state.guard(target('/index'),from)).toBe(true) state.permission.canHome=false;state.permission.landingPath='/user/profile' expect(await state.guard(target('/index'),from)).toEqual({path:'/user/profile',replace:true}) expect(await state.guard(target('/user/profile'),target('/index'))).toBe(true) })