test: 完善自动化测试体系(7个模块,73个任务)
系统性地修复、补充和强化项目的自动化测试能力: 1. 测试基础设施修复 - 修复 stubConcurrencyCache 缺失方法和构造函数参数不匹配 - 创建 testutil 共享包(stubs.go, fixtures.go, httptest.go) - 为所有 Stub 添加编译期接口断言 2. 中间件测试补充 - 新增 JWT 认证中间件测试(有效/过期/篡改/缺失 Token) - 补充 rate_limiter 和 recovery 中间件测试场景 3. 网关核心路径测试 - 新增账户选择、等待队列、流式响应、并发控制、计费、Claude Code 检测测试 - 覆盖负载均衡、粘性会话、SSE 转发、槽位管理等关键逻辑 4. 前端测试体系(11个新测试文件,163个测试用例) - Pinia stores: auth, app, subscriptions - API client: 请求拦截器、响应拦截器、401 刷新 - Router guards: 认证重定向、管理员权限、简易模式限制 - Composables: useForm, useTableLoader, useClipboard - Components: LoginForm, ApiKeyCreate, Dashboard 5. CI/CD 流水线重构 - 重构 backend-ci.yml 为统一的 ci.yml - 前后端 4 个并行 Job + Postgres/Redis services - Race 检测、覆盖率收集与门禁、Docker 构建验证 6. E2E 自动化测试 - e2e-test.sh 自动化脚本(Docker 启动→健康检查→测试→清理) - 用户注册→登录→API Key→网关调用完整链路测试 - Mock 模式和 API Key 脱敏支持 7. 修复预存问题 - tlsfingerprint dialer_test.go 缺失 build tag 导致集成测试编译冲突 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
293
frontend/src/stores/__tests__/app.spec.ts
Normal file
293
frontend/src/stores/__tests__/app.spec.ts
Normal file
@@ -0,0 +1,293 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { setActivePinia, createPinia } from 'pinia'
|
||||
import { useAppStore } from '@/stores/app'
|
||||
|
||||
// Mock API 模块
|
||||
vi.mock('@/api/admin/system', () => ({
|
||||
checkUpdates: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/api/auth', () => ({
|
||||
getPublicSettings: vi.fn(),
|
||||
}))
|
||||
|
||||
describe('useAppStore', () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia())
|
||||
vi.useFakeTimers()
|
||||
// 清除 window.__APP_CONFIG__
|
||||
delete (window as any).__APP_CONFIG__
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
// --- Toast 消息管理 ---
|
||||
|
||||
describe('Toast 消息管理', () => {
|
||||
it('showSuccess 创建 success 类型 toast', () => {
|
||||
const store = useAppStore()
|
||||
const id = store.showSuccess('操作成功')
|
||||
|
||||
expect(id).toMatch(/^toast-/)
|
||||
expect(store.toasts).toHaveLength(1)
|
||||
expect(store.toasts[0].type).toBe('success')
|
||||
expect(store.toasts[0].message).toBe('操作成功')
|
||||
})
|
||||
|
||||
it('showError 创建 error 类型 toast', () => {
|
||||
const store = useAppStore()
|
||||
store.showError('出错了')
|
||||
|
||||
expect(store.toasts).toHaveLength(1)
|
||||
expect(store.toasts[0].type).toBe('error')
|
||||
expect(store.toasts[0].message).toBe('出错了')
|
||||
})
|
||||
|
||||
it('showWarning 创建 warning 类型 toast', () => {
|
||||
const store = useAppStore()
|
||||
store.showWarning('警告信息')
|
||||
|
||||
expect(store.toasts).toHaveLength(1)
|
||||
expect(store.toasts[0].type).toBe('warning')
|
||||
})
|
||||
|
||||
it('showInfo 创建 info 类型 toast', () => {
|
||||
const store = useAppStore()
|
||||
store.showInfo('提示信息')
|
||||
|
||||
expect(store.toasts).toHaveLength(1)
|
||||
expect(store.toasts[0].type).toBe('info')
|
||||
})
|
||||
|
||||
it('toast 在指定 duration 后自动消失', () => {
|
||||
const store = useAppStore()
|
||||
store.showSuccess('临时消息', 3000)
|
||||
|
||||
expect(store.toasts).toHaveLength(1)
|
||||
|
||||
vi.advanceTimersByTime(3000)
|
||||
|
||||
expect(store.toasts).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('hideToast 移除指定 toast', () => {
|
||||
const store = useAppStore()
|
||||
const id = store.showSuccess('消息1')
|
||||
store.showError('消息2')
|
||||
|
||||
expect(store.toasts).toHaveLength(2)
|
||||
|
||||
store.hideToast(id)
|
||||
|
||||
expect(store.toasts).toHaveLength(1)
|
||||
expect(store.toasts[0].type).toBe('error')
|
||||
})
|
||||
|
||||
it('clearAllToasts 清除所有 toast', () => {
|
||||
const store = useAppStore()
|
||||
store.showSuccess('消息1')
|
||||
store.showError('消息2')
|
||||
store.showWarning('消息3')
|
||||
|
||||
expect(store.toasts).toHaveLength(3)
|
||||
|
||||
store.clearAllToasts()
|
||||
|
||||
expect(store.toasts).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('hasActiveToasts 正确反映 toast 状态', () => {
|
||||
const store = useAppStore()
|
||||
expect(store.hasActiveToasts).toBe(false)
|
||||
|
||||
store.showSuccess('消息')
|
||||
expect(store.hasActiveToasts).toBe(true)
|
||||
|
||||
store.clearAllToasts()
|
||||
expect(store.hasActiveToasts).toBe(false)
|
||||
})
|
||||
|
||||
it('多个 toast 的 ID 唯一', () => {
|
||||
const store = useAppStore()
|
||||
const id1 = store.showSuccess('消息1')
|
||||
const id2 = store.showSuccess('消息2')
|
||||
const id3 = store.showSuccess('消息3')
|
||||
|
||||
expect(id1).not.toBe(id2)
|
||||
expect(id2).not.toBe(id3)
|
||||
})
|
||||
})
|
||||
|
||||
// --- 侧边栏 ---
|
||||
|
||||
describe('侧边栏管理', () => {
|
||||
it('toggleSidebar 切换折叠状态', () => {
|
||||
const store = useAppStore()
|
||||
expect(store.sidebarCollapsed).toBe(false)
|
||||
|
||||
store.toggleSidebar()
|
||||
expect(store.sidebarCollapsed).toBe(true)
|
||||
|
||||
store.toggleSidebar()
|
||||
expect(store.sidebarCollapsed).toBe(false)
|
||||
})
|
||||
|
||||
it('setSidebarCollapsed 直接设置状态', () => {
|
||||
const store = useAppStore()
|
||||
|
||||
store.setSidebarCollapsed(true)
|
||||
expect(store.sidebarCollapsed).toBe(true)
|
||||
|
||||
store.setSidebarCollapsed(false)
|
||||
expect(store.sidebarCollapsed).toBe(false)
|
||||
})
|
||||
|
||||
it('toggleMobileSidebar 切换移动端状态', () => {
|
||||
const store = useAppStore()
|
||||
expect(store.mobileOpen).toBe(false)
|
||||
|
||||
store.toggleMobileSidebar()
|
||||
expect(store.mobileOpen).toBe(true)
|
||||
|
||||
store.toggleMobileSidebar()
|
||||
expect(store.mobileOpen).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
// --- Loading 状态 ---
|
||||
|
||||
describe('Loading 状态管理', () => {
|
||||
it('setLoading 管理引用计数', () => {
|
||||
const store = useAppStore()
|
||||
expect(store.loading).toBe(false)
|
||||
|
||||
store.setLoading(true)
|
||||
expect(store.loading).toBe(true)
|
||||
|
||||
store.setLoading(true) // 两次 true
|
||||
expect(store.loading).toBe(true)
|
||||
|
||||
store.setLoading(false) // 第一次 false,计数还是 1
|
||||
expect(store.loading).toBe(true)
|
||||
|
||||
store.setLoading(false) // 第二次 false,计数为 0
|
||||
expect(store.loading).toBe(false)
|
||||
})
|
||||
|
||||
it('setLoading(false) 不会使计数为负', () => {
|
||||
const store = useAppStore()
|
||||
|
||||
store.setLoading(false)
|
||||
store.setLoading(false)
|
||||
expect(store.loading).toBe(false)
|
||||
|
||||
store.setLoading(true)
|
||||
expect(store.loading).toBe(true)
|
||||
|
||||
store.setLoading(false)
|
||||
expect(store.loading).toBe(false)
|
||||
})
|
||||
|
||||
it('withLoading 自动管理 loading 状态', async () => {
|
||||
const store = useAppStore()
|
||||
|
||||
const result = await store.withLoading(async () => {
|
||||
expect(store.loading).toBe(true)
|
||||
return 'done'
|
||||
})
|
||||
|
||||
expect(result).toBe('done')
|
||||
expect(store.loading).toBe(false)
|
||||
})
|
||||
|
||||
it('withLoading 错误时也恢复 loading 状态', async () => {
|
||||
const store = useAppStore()
|
||||
|
||||
await expect(
|
||||
store.withLoading(async () => {
|
||||
throw new Error('操作失败')
|
||||
})
|
||||
).rejects.toThrow('操作失败')
|
||||
|
||||
expect(store.loading).toBe(false)
|
||||
})
|
||||
|
||||
it('withLoadingAndError 错误时显示 toast 并返回 null', async () => {
|
||||
const store = useAppStore()
|
||||
|
||||
const result = await store.withLoadingAndError(async () => {
|
||||
throw new Error('网络错误')
|
||||
})
|
||||
|
||||
expect(result).toBeNull()
|
||||
expect(store.loading).toBe(false)
|
||||
expect(store.toasts).toHaveLength(1)
|
||||
expect(store.toasts[0].type).toBe('error')
|
||||
})
|
||||
})
|
||||
|
||||
// --- reset ---
|
||||
|
||||
describe('reset', () => {
|
||||
it('重置所有 UI 状态', () => {
|
||||
const store = useAppStore()
|
||||
|
||||
store.setSidebarCollapsed(true)
|
||||
store.setLoading(true)
|
||||
store.showSuccess('消息')
|
||||
|
||||
store.reset()
|
||||
|
||||
expect(store.sidebarCollapsed).toBe(false)
|
||||
expect(store.loading).toBe(false)
|
||||
expect(store.toasts).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
// --- 公开设置 ---
|
||||
|
||||
describe('公开设置加载', () => {
|
||||
it('从 window.__APP_CONFIG__ 初始化', () => {
|
||||
;(window as any).__APP_CONFIG__ = {
|
||||
site_name: 'TestSite',
|
||||
site_logo: '/logo.png',
|
||||
version: '1.0.0',
|
||||
contact_info: 'test@test.com',
|
||||
api_base_url: 'https://api.test.com',
|
||||
doc_url: 'https://docs.test.com',
|
||||
}
|
||||
|
||||
const store = useAppStore()
|
||||
const result = store.initFromInjectedConfig()
|
||||
|
||||
expect(result).toBe(true)
|
||||
expect(store.siteName).toBe('TestSite')
|
||||
expect(store.siteLogo).toBe('/logo.png')
|
||||
expect(store.siteVersion).toBe('1.0.0')
|
||||
expect(store.publicSettingsLoaded).toBe(true)
|
||||
})
|
||||
|
||||
it('无注入配置时返回 false', () => {
|
||||
const store = useAppStore()
|
||||
const result = store.initFromInjectedConfig()
|
||||
|
||||
expect(result).toBe(false)
|
||||
expect(store.publicSettingsLoaded).toBe(false)
|
||||
})
|
||||
|
||||
it('clearPublicSettingsCache 清除缓存', () => {
|
||||
;(window as any).__APP_CONFIG__ = { site_name: 'Test' }
|
||||
const store = useAppStore()
|
||||
store.initFromInjectedConfig()
|
||||
|
||||
expect(store.publicSettingsLoaded).toBe(true)
|
||||
|
||||
store.clearPublicSettingsCache()
|
||||
|
||||
expect(store.publicSettingsLoaded).toBe(false)
|
||||
expect(store.cachedPublicSettings).toBeNull()
|
||||
})
|
||||
})
|
||||
})
|
||||
289
frontend/src/stores/__tests__/auth.spec.ts
Normal file
289
frontend/src/stores/__tests__/auth.spec.ts
Normal file
@@ -0,0 +1,289 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { setActivePinia, createPinia } from 'pinia'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
|
||||
// Mock authAPI
|
||||
const mockLogin = vi.fn()
|
||||
const mockLogin2FA = vi.fn()
|
||||
const mockLogout = vi.fn()
|
||||
const mockGetCurrentUser = vi.fn()
|
||||
const mockRegister = vi.fn()
|
||||
const mockRefreshToken = vi.fn()
|
||||
|
||||
vi.mock('@/api', () => ({
|
||||
authAPI: {
|
||||
login: (...args: any[]) => mockLogin(...args),
|
||||
login2FA: (...args: any[]) => mockLogin2FA(...args),
|
||||
logout: (...args: any[]) => mockLogout(...args),
|
||||
getCurrentUser: (...args: any[]) => mockGetCurrentUser(...args),
|
||||
register: (...args: any[]) => mockRegister(...args),
|
||||
refreshToken: (...args: any[]) => mockRefreshToken(...args),
|
||||
},
|
||||
isTotp2FARequired: (response: any) => response?.requires_2fa === true,
|
||||
}))
|
||||
|
||||
const fakeUser = {
|
||||
id: 1,
|
||||
username: 'testuser',
|
||||
email: 'test@example.com',
|
||||
role: 'user' as const,
|
||||
balance: 100,
|
||||
concurrency: 5,
|
||||
status: 'active' as const,
|
||||
allowed_groups: null,
|
||||
created_at: '2024-01-01',
|
||||
updated_at: '2024-01-01',
|
||||
}
|
||||
|
||||
const fakeAdminUser = {
|
||||
...fakeUser,
|
||||
id: 2,
|
||||
username: 'admin',
|
||||
email: 'admin@example.com',
|
||||
role: 'admin' as const,
|
||||
}
|
||||
|
||||
const fakeAuthResponse = {
|
||||
access_token: 'test-token-123',
|
||||
refresh_token: 'refresh-token-456',
|
||||
expires_in: 3600,
|
||||
token_type: 'Bearer',
|
||||
user: { ...fakeUser },
|
||||
}
|
||||
|
||||
describe('useAuthStore', () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia())
|
||||
localStorage.clear()
|
||||
vi.useFakeTimers()
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
// --- login ---
|
||||
|
||||
describe('login', () => {
|
||||
it('成功登录后设置 token 和 user', async () => {
|
||||
mockLogin.mockResolvedValue(fakeAuthResponse)
|
||||
const store = useAuthStore()
|
||||
|
||||
await store.login({ email: 'test@example.com', password: '123456' })
|
||||
|
||||
expect(store.token).toBe('test-token-123')
|
||||
expect(store.user).toEqual(fakeUser)
|
||||
expect(store.isAuthenticated).toBe(true)
|
||||
expect(localStorage.getItem('auth_token')).toBe('test-token-123')
|
||||
expect(localStorage.getItem('auth_user')).toBe(JSON.stringify(fakeUser))
|
||||
})
|
||||
|
||||
it('登录失败时清除状态并抛出错误', async () => {
|
||||
mockLogin.mockRejectedValue(new Error('Invalid credentials'))
|
||||
const store = useAuthStore()
|
||||
|
||||
await expect(store.login({ email: 'test@example.com', password: 'wrong' })).rejects.toThrow(
|
||||
'Invalid credentials'
|
||||
)
|
||||
|
||||
expect(store.token).toBeNull()
|
||||
expect(store.user).toBeNull()
|
||||
expect(store.isAuthenticated).toBe(false)
|
||||
})
|
||||
|
||||
it('需要 2FA 时返回响应但不设置认证状态', async () => {
|
||||
const twoFAResponse = { requires_2fa: true, temp_token: 'temp-123' }
|
||||
mockLogin.mockResolvedValue(twoFAResponse)
|
||||
const store = useAuthStore()
|
||||
|
||||
const result = await store.login({ email: 'test@example.com', password: '123456' })
|
||||
|
||||
expect(result).toEqual(twoFAResponse)
|
||||
expect(store.token).toBeNull()
|
||||
expect(store.isAuthenticated).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
// --- login2FA ---
|
||||
|
||||
describe('login2FA', () => {
|
||||
it('2FA 验证成功后设置认证状态', async () => {
|
||||
mockLogin2FA.mockResolvedValue(fakeAuthResponse)
|
||||
const store = useAuthStore()
|
||||
|
||||
const user = await store.login2FA('temp-123', '654321')
|
||||
|
||||
expect(store.token).toBe('test-token-123')
|
||||
expect(store.user).toEqual(fakeUser)
|
||||
expect(user).toEqual(fakeUser)
|
||||
expect(mockLogin2FA).toHaveBeenCalledWith({
|
||||
temp_token: 'temp-123',
|
||||
totp_code: '654321',
|
||||
})
|
||||
})
|
||||
|
||||
it('2FA 验证失败时清除状态并抛出错误', async () => {
|
||||
mockLogin2FA.mockRejectedValue(new Error('Invalid TOTP'))
|
||||
const store = useAuthStore()
|
||||
|
||||
await expect(store.login2FA('temp-123', '000000')).rejects.toThrow('Invalid TOTP')
|
||||
expect(store.token).toBeNull()
|
||||
expect(store.isAuthenticated).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
// --- logout ---
|
||||
|
||||
describe('logout', () => {
|
||||
it('注销后清除所有状态和 localStorage', async () => {
|
||||
mockLogin.mockResolvedValue(fakeAuthResponse)
|
||||
mockLogout.mockResolvedValue(undefined)
|
||||
const store = useAuthStore()
|
||||
|
||||
// 先登录
|
||||
await store.login({ email: 'test@example.com', password: '123456' })
|
||||
expect(store.isAuthenticated).toBe(true)
|
||||
|
||||
// 注销
|
||||
await store.logout()
|
||||
|
||||
expect(store.token).toBeNull()
|
||||
expect(store.user).toBeNull()
|
||||
expect(store.isAuthenticated).toBe(false)
|
||||
expect(localStorage.getItem('auth_token')).toBeNull()
|
||||
expect(localStorage.getItem('auth_user')).toBeNull()
|
||||
expect(localStorage.getItem('refresh_token')).toBeNull()
|
||||
expect(localStorage.getItem('token_expires_at')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
// --- checkAuth ---
|
||||
|
||||
describe('checkAuth', () => {
|
||||
it('从 localStorage 恢复持久化状态', () => {
|
||||
localStorage.setItem('auth_token', 'saved-token')
|
||||
localStorage.setItem('auth_user', JSON.stringify(fakeUser))
|
||||
|
||||
// Mock refreshUser (getCurrentUser) 防止后台刷新报错
|
||||
mockGetCurrentUser.mockResolvedValue({ data: fakeUser })
|
||||
|
||||
const store = useAuthStore()
|
||||
store.checkAuth()
|
||||
|
||||
expect(store.token).toBe('saved-token')
|
||||
expect(store.user).toEqual(fakeUser)
|
||||
expect(store.isAuthenticated).toBe(true)
|
||||
})
|
||||
|
||||
it('localStorage 无数据时保持未认证状态', () => {
|
||||
const store = useAuthStore()
|
||||
store.checkAuth()
|
||||
|
||||
expect(store.token).toBeNull()
|
||||
expect(store.user).toBeNull()
|
||||
expect(store.isAuthenticated).toBe(false)
|
||||
})
|
||||
|
||||
it('localStorage 中用户数据损坏时清除状态', () => {
|
||||
localStorage.setItem('auth_token', 'saved-token')
|
||||
localStorage.setItem('auth_user', 'invalid-json{{{')
|
||||
|
||||
const store = useAuthStore()
|
||||
store.checkAuth()
|
||||
|
||||
expect(store.token).toBeNull()
|
||||
expect(store.user).toBeNull()
|
||||
expect(localStorage.getItem('auth_token')).toBeNull()
|
||||
})
|
||||
|
||||
it('恢复 refresh token 和过期时间', () => {
|
||||
const futureTs = String(Date.now() + 3600_000)
|
||||
localStorage.setItem('auth_token', 'saved-token')
|
||||
localStorage.setItem('auth_user', JSON.stringify(fakeUser))
|
||||
localStorage.setItem('refresh_token', 'saved-refresh')
|
||||
localStorage.setItem('token_expires_at', futureTs)
|
||||
|
||||
mockGetCurrentUser.mockResolvedValue({ data: fakeUser })
|
||||
|
||||
const store = useAuthStore()
|
||||
store.checkAuth()
|
||||
|
||||
expect(store.isAuthenticated).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
// --- isAdmin ---
|
||||
|
||||
describe('isAdmin', () => {
|
||||
it('管理员用户返回 true', async () => {
|
||||
const adminResponse = { ...fakeAuthResponse, user: { ...fakeAdminUser } }
|
||||
mockLogin.mockResolvedValue(adminResponse)
|
||||
const store = useAuthStore()
|
||||
|
||||
await store.login({ email: 'admin@example.com', password: '123456' })
|
||||
|
||||
expect(store.isAdmin).toBe(true)
|
||||
})
|
||||
|
||||
it('普通用户返回 false', async () => {
|
||||
mockLogin.mockResolvedValue(fakeAuthResponse)
|
||||
const store = useAuthStore()
|
||||
|
||||
await store.login({ email: 'test@example.com', password: '123456' })
|
||||
|
||||
expect(store.isAdmin).toBe(false)
|
||||
})
|
||||
|
||||
it('未登录时返回 false', () => {
|
||||
const store = useAuthStore()
|
||||
expect(store.isAdmin).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
// --- refreshUser ---
|
||||
|
||||
describe('refreshUser', () => {
|
||||
it('刷新用户数据并更新 localStorage', async () => {
|
||||
mockLogin.mockResolvedValue(fakeAuthResponse)
|
||||
const store = useAuthStore()
|
||||
await store.login({ email: 'test@example.com', password: '123456' })
|
||||
|
||||
const updatedUser = { ...fakeUser, username: 'updated-name' }
|
||||
mockGetCurrentUser.mockResolvedValue({ data: updatedUser })
|
||||
|
||||
const result = await store.refreshUser()
|
||||
|
||||
expect(result).toEqual(updatedUser)
|
||||
expect(store.user).toEqual(updatedUser)
|
||||
expect(JSON.parse(localStorage.getItem('auth_user')!)).toEqual(updatedUser)
|
||||
})
|
||||
|
||||
it('未认证时抛出错误', async () => {
|
||||
const store = useAuthStore()
|
||||
await expect(store.refreshUser()).rejects.toThrow('Not authenticated')
|
||||
})
|
||||
})
|
||||
|
||||
// --- isSimpleMode ---
|
||||
|
||||
describe('isSimpleMode', () => {
|
||||
it('run_mode 为 simple 时返回 true', async () => {
|
||||
const simpleResponse = {
|
||||
...fakeAuthResponse,
|
||||
user: { ...fakeUser, run_mode: 'simple' as const },
|
||||
}
|
||||
mockLogin.mockResolvedValue(simpleResponse)
|
||||
const store = useAuthStore()
|
||||
|
||||
await store.login({ email: 'test@example.com', password: '123456' })
|
||||
|
||||
expect(store.isSimpleMode).toBe(true)
|
||||
})
|
||||
|
||||
it('默认为 standard 模式', () => {
|
||||
const store = useAuthStore()
|
||||
expect(store.isSimpleMode).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
239
frontend/src/stores/__tests__/subscriptions.spec.ts
Normal file
239
frontend/src/stores/__tests__/subscriptions.spec.ts
Normal file
@@ -0,0 +1,239 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { setActivePinia, createPinia } from 'pinia'
|
||||
import { useSubscriptionStore } from '@/stores/subscriptions'
|
||||
|
||||
// Mock subscriptions API
|
||||
const mockGetActiveSubscriptions = vi.fn()
|
||||
|
||||
vi.mock('@/api/subscriptions', () => ({
|
||||
default: {
|
||||
getActiveSubscriptions: (...args: any[]) => mockGetActiveSubscriptions(...args),
|
||||
},
|
||||
}))
|
||||
|
||||
const fakeSubscriptions = [
|
||||
{
|
||||
id: 1,
|
||||
user_id: 1,
|
||||
group_id: 1,
|
||||
status: 'active' as const,
|
||||
daily_usage_usd: 5,
|
||||
weekly_usage_usd: 20,
|
||||
monthly_usage_usd: 50,
|
||||
daily_window_start: null,
|
||||
weekly_window_start: null,
|
||||
monthly_window_start: null,
|
||||
created_at: '2024-01-01',
|
||||
updated_at: '2024-01-01',
|
||||
expires_at: '2025-01-01',
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
user_id: 1,
|
||||
group_id: 2,
|
||||
status: 'active' as const,
|
||||
daily_usage_usd: 10,
|
||||
weekly_usage_usd: 40,
|
||||
monthly_usage_usd: 100,
|
||||
daily_window_start: null,
|
||||
weekly_window_start: null,
|
||||
monthly_window_start: null,
|
||||
created_at: '2024-02-01',
|
||||
updated_at: '2024-02-01',
|
||||
expires_at: '2025-02-01',
|
||||
},
|
||||
]
|
||||
|
||||
describe('useSubscriptionStore', () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia())
|
||||
vi.useFakeTimers()
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
// --- fetchActiveSubscriptions ---
|
||||
|
||||
describe('fetchActiveSubscriptions', () => {
|
||||
it('成功获取活跃订阅', async () => {
|
||||
mockGetActiveSubscriptions.mockResolvedValue(fakeSubscriptions)
|
||||
const store = useSubscriptionStore()
|
||||
|
||||
const result = await store.fetchActiveSubscriptions()
|
||||
|
||||
expect(result).toEqual(fakeSubscriptions)
|
||||
expect(store.activeSubscriptions).toEqual(fakeSubscriptions)
|
||||
expect(store.loading).toBe(false)
|
||||
})
|
||||
|
||||
it('缓存有效时返回缓存数据', async () => {
|
||||
mockGetActiveSubscriptions.mockResolvedValue(fakeSubscriptions)
|
||||
const store = useSubscriptionStore()
|
||||
|
||||
// 第一次请求
|
||||
await store.fetchActiveSubscriptions()
|
||||
expect(mockGetActiveSubscriptions).toHaveBeenCalledTimes(1)
|
||||
|
||||
// 第二次请求(60秒内)- 应返回缓存
|
||||
const result = await store.fetchActiveSubscriptions()
|
||||
expect(mockGetActiveSubscriptions).toHaveBeenCalledTimes(1) // 没有新请求
|
||||
expect(result).toEqual(fakeSubscriptions)
|
||||
})
|
||||
|
||||
it('缓存过期后重新请求', async () => {
|
||||
mockGetActiveSubscriptions.mockResolvedValue(fakeSubscriptions)
|
||||
const store = useSubscriptionStore()
|
||||
|
||||
await store.fetchActiveSubscriptions()
|
||||
expect(mockGetActiveSubscriptions).toHaveBeenCalledTimes(1)
|
||||
|
||||
// 推进 61 秒让缓存过期
|
||||
vi.advanceTimersByTime(61_000)
|
||||
|
||||
const updatedSubs = [fakeSubscriptions[0]]
|
||||
mockGetActiveSubscriptions.mockResolvedValue(updatedSubs)
|
||||
|
||||
const result = await store.fetchActiveSubscriptions()
|
||||
expect(mockGetActiveSubscriptions).toHaveBeenCalledTimes(2)
|
||||
expect(result).toEqual(updatedSubs)
|
||||
})
|
||||
|
||||
it('force=true 强制重新请求', async () => {
|
||||
mockGetActiveSubscriptions.mockResolvedValue(fakeSubscriptions)
|
||||
const store = useSubscriptionStore()
|
||||
|
||||
await store.fetchActiveSubscriptions()
|
||||
|
||||
const updatedSubs = [fakeSubscriptions[0]]
|
||||
mockGetActiveSubscriptions.mockResolvedValue(updatedSubs)
|
||||
|
||||
const result = await store.fetchActiveSubscriptions(true)
|
||||
expect(mockGetActiveSubscriptions).toHaveBeenCalledTimes(2)
|
||||
expect(result).toEqual(updatedSubs)
|
||||
})
|
||||
|
||||
it('并发请求共享同一个 Promise(去重)', async () => {
|
||||
let resolvePromise: (v: any) => void
|
||||
mockGetActiveSubscriptions.mockImplementation(
|
||||
() => new Promise((resolve) => { resolvePromise = resolve })
|
||||
)
|
||||
const store = useSubscriptionStore()
|
||||
|
||||
// 并发发起两个请求
|
||||
const p1 = store.fetchActiveSubscriptions()
|
||||
const p2 = store.fetchActiveSubscriptions()
|
||||
|
||||
// 只调用了一次 API
|
||||
expect(mockGetActiveSubscriptions).toHaveBeenCalledTimes(1)
|
||||
|
||||
// 解决 Promise
|
||||
resolvePromise!(fakeSubscriptions)
|
||||
|
||||
const [r1, r2] = await Promise.all([p1, p2])
|
||||
expect(r1).toEqual(fakeSubscriptions)
|
||||
expect(r2).toEqual(fakeSubscriptions)
|
||||
})
|
||||
|
||||
it('API 错误时抛出异常', async () => {
|
||||
mockGetActiveSubscriptions.mockRejectedValue(new Error('Network error'))
|
||||
const store = useSubscriptionStore()
|
||||
|
||||
await expect(store.fetchActiveSubscriptions()).rejects.toThrow('Network error')
|
||||
})
|
||||
})
|
||||
|
||||
// --- hasActiveSubscriptions ---
|
||||
|
||||
describe('hasActiveSubscriptions', () => {
|
||||
it('有订阅时返回 true', async () => {
|
||||
mockGetActiveSubscriptions.mockResolvedValue(fakeSubscriptions)
|
||||
const store = useSubscriptionStore()
|
||||
|
||||
await store.fetchActiveSubscriptions()
|
||||
|
||||
expect(store.hasActiveSubscriptions).toBe(true)
|
||||
})
|
||||
|
||||
it('无订阅时返回 false', () => {
|
||||
const store = useSubscriptionStore()
|
||||
expect(store.hasActiveSubscriptions).toBe(false)
|
||||
})
|
||||
|
||||
it('清除后返回 false', async () => {
|
||||
mockGetActiveSubscriptions.mockResolvedValue(fakeSubscriptions)
|
||||
const store = useSubscriptionStore()
|
||||
|
||||
await store.fetchActiveSubscriptions()
|
||||
expect(store.hasActiveSubscriptions).toBe(true)
|
||||
|
||||
store.clear()
|
||||
expect(store.hasActiveSubscriptions).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
// --- invalidateCache ---
|
||||
|
||||
describe('invalidateCache', () => {
|
||||
it('失效缓存后下次请求重新获取数据', async () => {
|
||||
mockGetActiveSubscriptions.mockResolvedValue(fakeSubscriptions)
|
||||
const store = useSubscriptionStore()
|
||||
|
||||
await store.fetchActiveSubscriptions()
|
||||
expect(mockGetActiveSubscriptions).toHaveBeenCalledTimes(1)
|
||||
|
||||
store.invalidateCache()
|
||||
|
||||
await store.fetchActiveSubscriptions()
|
||||
expect(mockGetActiveSubscriptions).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
})
|
||||
|
||||
// --- clear ---
|
||||
|
||||
describe('clear', () => {
|
||||
it('清除所有订阅数据', async () => {
|
||||
mockGetActiveSubscriptions.mockResolvedValue(fakeSubscriptions)
|
||||
const store = useSubscriptionStore()
|
||||
|
||||
await store.fetchActiveSubscriptions()
|
||||
expect(store.activeSubscriptions).toHaveLength(2)
|
||||
|
||||
store.clear()
|
||||
|
||||
expect(store.activeSubscriptions).toHaveLength(0)
|
||||
expect(store.hasActiveSubscriptions).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
// --- polling ---
|
||||
|
||||
describe('startPolling / stopPolling', () => {
|
||||
it('startPolling 不会创建重复 interval', () => {
|
||||
const store = useSubscriptionStore()
|
||||
mockGetActiveSubscriptions.mockResolvedValue([])
|
||||
|
||||
store.startPolling()
|
||||
store.startPolling() // 重复调用
|
||||
|
||||
// 推进5分钟只触发一次
|
||||
vi.advanceTimersByTime(5 * 60 * 1000)
|
||||
expect(mockGetActiveSubscriptions).toHaveBeenCalledTimes(1)
|
||||
|
||||
store.stopPolling()
|
||||
})
|
||||
|
||||
it('stopPolling 停止定期刷新', () => {
|
||||
const store = useSubscriptionStore()
|
||||
mockGetActiveSubscriptions.mockResolvedValue([])
|
||||
|
||||
store.startPolling()
|
||||
store.stopPolling()
|
||||
|
||||
vi.advanceTimersByTime(10 * 60 * 1000)
|
||||
expect(mockGetActiveSubscriptions).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user