import { describe, it, expect, beforeEach, vi } from 'vitest' import { setActivePinia, createPinia } from 'pinia' import { useAuthStore } from '@/stores/auth' import router from '@/router' // mock 后端数据接口(权限/菜单/多语言都走 loadDataApi) vi.mock('@/services/api', () => ({ loadDataApi: vi.fn(() => Promise.resolve({ data: [], code: 0, success: true })), })) beforeEach(async () => { localStorage.clear() sessionStorage.clear() setActivePinia(createPinia()) // 复位到登录页,避免上一用例的受保护路由状态泄漏 await router.push('/login').catch(() => {}) }) function login(account, id) { useAuthStore().setSession({ token: 't', user: { id, account, name: account } }, 'ORG') } describe('路由守卫:模块中心 /module/* 仅 g3soft 可访问', () => { it('普通用户:访问 /module/* 一律拦截回工作台', async () => { login('001', 123) await router.push('/module/i18n-type') expect(router.currentRoute.value.path).toBe('/dashboard') await router.push('/module/module-management') expect(router.currentRoute.value.path).toBe('/dashboard') }) it('普通用户:其它受保护页面不受影响', async () => { login('001', 123) await router.push('/demo') expect(router.currentRoute.value.path).toBe('/demo') }) it('g3soft:模块中心页面可正常访问', async () => { login('g3soft', 1) await router.push('/module/i18n-type') expect(router.currentRoute.value.path).toBe('/module/i18n-type') }) })