ai-chat-ui/src/__tests__/authStore.test.ts
MT-Mint 4a434f9580 fix(auth): 修复启动脚本与前端认证流程
修复 start.sh 未记录后台进程 PID 导致启动后误判失败的问题。\n调整前端认证逻辑,支持开发环境下优先读取 URL token,并使用 POST 请求直连外部认证接口解析新的返回结构。\n补充认证回归测试,覆盖请求方法、请求头和用户信息映射。
2026-06-12 14:09:19 +08:00

55 lines
1.4 KiB
TypeScript

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')
})
})