import { beforeEach, describe, expect, it, vi } from 'vitest' import { createPinia, setActivePinia } from 'pinia' describe('Auth Store', () => { beforeEach(() => { setActivePinia(createPinia()) localStorage.clear() vi.clearAllMocks() window.history.replaceState({}, '', '/') }) it('prefers URL token and validates it even in dev mode', async () => { window.history.replaceState({}, '', '/?token=url-token') const fetchMock = vi.mocked(fetch) fetchMock.mockResolvedValueOnce({ ok: true, json: async () => ({ status: 1000, message: '返回成功', data: { token: 'url-token', userInfo: { userId: 19, userName: 'test20', realName: '测试学生3', email: '', userIcon: 'defaultUserIcon.png', }, }, }), } as Response) const { useAuthStore } = await import('@/stores/auth') const store = useAuthStore() await store.init() expect(fetchMock).toHaveBeenCalledTimes(1) expect(fetchMock).toHaveBeenCalledWith( 'http://test.xueai.art/newapi/api/login/validateToken', { method: 'POST', headers: { authorization: 'url-token', }, }, ) expect(store.token).toBe('url-token') expect(store.user?.id).toBe('19') expect(store.user?.username).toBe('test20') expect(store.user?.nickname).toBe('测试学生3') }) })