feat: 新增 AI 用量统计页与头部入口

- 新增 /ai-usage 页面:统计卡、按任务类型/模型明细、内联 SVG 趋势图、上游余额
- 入口按钮放在「退出登录」左侧,显隐读登录响应写入的 aiAdmin
- token 为 null 时显示「未上报」,与 0 区分
This commit is contained in:
2026-10-10 18:46:11 +08:00
parent b48ce89f4e
commit 0a728cfba8
11 changed files with 1255 additions and 9 deletions
+45 -4
View File
@@ -96,15 +96,56 @@ describe('MyHead.vue', () => {
expect(welcomeWrapper.vm.pageTitle).toBeUndefined()
})
it('clears login state and navigates to login on logout', async () => { vi.mocked(logout).mockResolvedValue({ code: 200 } as any)
it('clears login state and navigates to login on logout', async () => {
vi.mocked(logout).mockResolvedValue({ code: 200 } as any)
localStorage.setItem('isLoggedIn', 'true')
const wrapper = await mountAt('/study')
localStorage.setItem('aiAdmin', 'true')
const wrapper = await mountRealAt('/study')
await wrapper.vm.handleLogout()
await wrapper.findAll('button').find((button) => button.text().includes('退出登录'))!.trigger('click')
await vi.waitFor(() => expect(router.currentRoute.value.path).toBe('/login'))
expect(localStorage.getItem('isLoggedIn')).toBeNull()
expect(localStorage.getItem('aiAdmin')).toBeNull()
expect(ElMessage.success).toHaveBeenCalledWith('已退出登录')
expect(router.currentRoute.value.path).toBe('/login')
})
it('白名单用户显示 AI 用量入口,点击后进入统计页', async () => {
localStorage.setItem('isLoggedIn', 'true')
localStorage.setItem('aiAdmin', 'true')
const wrapper = await mountRealAt('/study')
const entry = wrapper.findAll('button').find((button) => button.text().includes('AI 用量统计'))
expect(entry).toBeTruthy()
await entry!.trigger('click')
await vi.waitFor(() => expect(router.currentRoute.value.path).toBe('/ai-usage'))
})
it('非白名单用户不显示 AI 用量入口', async () => {
localStorage.setItem('isLoggedIn', 'true')
const wrapper = await mountRealAt('/study')
expect(wrapper.text()).not.toContain('AI 用量统计')
})
it('登录页即使是白名单也不显示 AI 用量入口', async () => {
localStorage.setItem('aiAdmin', 'true')
const wrapper = await mountRealAt('/login')
expect(wrapper.text()).not.toContain('AI 用量统计')
})
it('换账号登录后按路由变化重新读取白名单,不残留上一个账号的入口', async () => {
localStorage.setItem('isLoggedIn', 'true')
localStorage.setItem('aiAdmin', 'true')
const wrapper = await mountRealAt('/study')
expect(wrapper.text()).toContain('AI 用量统计')
// 非管理员重新登录后只写了 isLoggedIn 与 aiAdmin=false
localStorage.setItem('aiAdmin', 'false')
await router.push('/welcome')
await vi.waitFor(() => expect(wrapper.text()).not.toContain('AI 用量统计'))
})
it('clears login state even when logout API fails', async () => {