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:
yangjianbo
2026-02-08 12:05:39 +08:00
parent 53e1c8b268
commit bb5a5dd65e
41 changed files with 5101 additions and 182 deletions

View 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()
})
})
})