chore(test): 清理测试用例与类型导入

This commit is contained in:
yangjianbo
2026-02-10 00:37:56 +08:00
parent 3fcb0cc37c
commit 29ca1290b3
8 changed files with 20 additions and 71 deletions

View File

@@ -28,9 +28,9 @@ func TestRedactText_QueryLike(t *testing.T) {
} }
func TestRedactText_GOCSPX(t *testing.T) { func TestRedactText_GOCSPX(t *testing.T) {
in := "client_secret=GOCSPX-abcdefghijklmnopqrstuvwxyz_0123456789" in := "client_secret=GOCSPX-your-client-secret"
out := RedactText(in) out := RedactText(in)
if strings.Contains(out, "abcdefghijklmnopqrstuvwxyz") { if strings.Contains(out, "your-client-secret") {
t.Fatalf("expected secret redacted, got %q", out) t.Fatalf("expected secret redacted, got %q", out)
} }
if !strings.Contains(out, "client_secret=***") { if !strings.Contains(out, "client_secret=***") {

View File

@@ -1,6 +1,6 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import axios from 'axios' import axios from 'axios'
import type { AxiosInstance, InternalAxiosRequestConfig, AxiosResponse, AxiosHeaders } from 'axios' import type { AxiosInstance } from 'axios'
// 需要在导入 client 之前设置 mock // 需要在导入 client 之前设置 mock
vi.mock('@/i18n', () => ({ vi.mock('@/i18n', () => ({

View File

@@ -9,7 +9,6 @@ import { defineComponent, ref, onMounted, nextTick } from 'vue'
// Mock API // Mock API
const mockGetDashboardStats = vi.fn() const mockGetDashboardStats = vi.fn()
const mockRefreshUser = vi.fn()
vi.mock('@/api', () => ({ vi.mock('@/api', () => ({
authAPI: { authAPI: {

View File

@@ -96,10 +96,12 @@ describe('useClipboard', () => {
}) })
it('Clipboard API 失败时降级到 fallback', async () => { it('Clipboard API 失败时降级到 fallback', async () => {
;(navigator.clipboard.writeText as any).mockRejectedValue(new Error('API failed')) const writeTextMock = navigator.clipboard.writeText as any
writeTextMock.mockRejectedValue(new Error('API failed'))
// jsdom 没有 execCommand手动定义 // jsdom 没有 execCommand手动定义
;(document as any).execCommand = vi.fn().mockReturnValue(true) const documentAny = document as any
documentAny.execCommand = vi.fn().mockReturnValue(true)
const { copyToClipboard, copied } = useClipboard() const { copyToClipboard, copied } = useClipboard()
const result = await copyToClipboard('fallback text') const result = await copyToClipboard('fallback text')
@@ -112,7 +114,8 @@ describe('useClipboard', () => {
it('非安全上下文使用 fallback', async () => { it('非安全上下文使用 fallback', async () => {
Object.defineProperty(window, 'isSecureContext', { value: false, writable: true }) Object.defineProperty(window, 'isSecureContext', { value: false, writable: true })
;(document as any).execCommand = vi.fn().mockReturnValue(true) const documentAny = document as any
documentAny.execCommand = vi.fn().mockReturnValue(true)
const { copyToClipboard, copied } = useClipboard() const { copyToClipboard, copied } = useClipboard()
const result = await copyToClipboard('insecure context text') const result = await copyToClipboard('insecure context text')
@@ -124,8 +127,11 @@ describe('useClipboard', () => {
}) })
it('所有复制方式均失败时调用 showError', async () => { it('所有复制方式均失败时调用 showError', async () => {
;(navigator.clipboard.writeText as any).mockRejectedValue(new Error('fail')) const writeTextMock = navigator.clipboard.writeText as any
;(document as any).execCommand = vi.fn().mockReturnValue(false) writeTextMock.mockRejectedValue(new Error('fail'))
const documentAny = document as any
documentAny.execCommand = vi.fn().mockReturnValue(false)
const { copyToClipboard, copied } = useClipboard() const { copyToClipboard, copied } = useClipboard()
const result = await copyToClipboard('text') const result = await copyToClipboard('text')

View File

@@ -1,6 +1,5 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { useTableLoader } from '@/composables/useTableLoader' import { useTableLoader } from '@/composables/useTableLoader'
import { nextTick } from 'vue'
// Mock @vueuse/core 的 useDebounceFn // Mock @vueuse/core 的 useDebounceFn
vi.mock('@vueuse/core', () => ({ vi.mock('@vueuse/core', () => ({
@@ -212,7 +211,7 @@ describe('useTableLoader', () => {
}) })
}) })
const { load, items } = useTableLoader({ fetchFn }) const { load } = useTableLoader({ fetchFn })
// 第一次加载 // 第一次加载
const p1 = load() const p1 = load()

View File

@@ -1,7 +1,5 @@
import { describe, it, expect, vi, beforeEach } from 'vitest' import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createRouter, createMemoryHistory } from 'vue-router'
import { setActivePinia, createPinia } from 'pinia' import { setActivePinia, createPinia } from 'pinia'
import { defineComponent, h } from 'vue'
// Mock 导航加载状态 // Mock 导航加载状态
vi.mock('@/composables/useNavigationLoading', () => { vi.mock('@/composables/useNavigationLoading', () => {
@@ -47,61 +45,6 @@ vi.mock('@/api/auth', () => ({
getPublicSettings: vi.fn(), getPublicSettings: vi.fn(),
})) }))
const DummyComponent = defineComponent({
render() {
return h('div', 'dummy')
},
})
/**
* 创建带守卫逻辑的测试路由
* 模拟 router/index.ts 中的 beforeEach 守卫逻辑
*/
function createTestRouter() {
const router = createRouter({
history: createMemoryHistory(),
routes: [
{ path: '/login', component: DummyComponent, meta: { requiresAuth: false, title: 'Login' } },
{
path: '/register',
component: DummyComponent,
meta: { requiresAuth: false, title: 'Register' },
},
{ path: '/home', component: DummyComponent, meta: { requiresAuth: false, title: 'Home' } },
{ path: '/dashboard', component: DummyComponent, meta: { title: 'Dashboard' } },
{ path: '/keys', component: DummyComponent, meta: { title: 'API Keys' } },
{ path: '/subscriptions', component: DummyComponent, meta: { title: 'Subscriptions' } },
{ path: '/redeem', component: DummyComponent, meta: { title: 'Redeem' } },
{
path: '/admin/dashboard',
component: DummyComponent,
meta: { requiresAdmin: true, title: 'Admin Dashboard' },
},
{
path: '/admin/users',
component: DummyComponent,
meta: { requiresAdmin: true, title: 'Admin Users' },
},
{
path: '/admin/groups',
component: DummyComponent,
meta: { requiresAdmin: true, title: 'Admin Groups' },
},
{
path: '/admin/subscriptions',
component: DummyComponent,
meta: { requiresAdmin: true, title: 'Admin Subscriptions' },
},
{
path: '/admin/redeem',
component: DummyComponent,
meta: { requiresAdmin: true, title: 'Admin Redeem' },
},
],
})
return router
}
// 用于测试的 auth 状态 // 用于测试的 auth 状态
interface MockAuthState { interface MockAuthState {

View File

@@ -250,7 +250,8 @@ describe('useAppStore', () => {
describe('公开设置加载', () => { describe('公开设置加载', () => {
it('从 window.__APP_CONFIG__ 初始化', () => { it('从 window.__APP_CONFIG__ 初始化', () => {
;(window as any).__APP_CONFIG__ = { const windowAny = window as any
windowAny.__APP_CONFIG__ = {
site_name: 'TestSite', site_name: 'TestSite',
site_logo: '/logo.png', site_logo: '/logo.png',
version: '1.0.0', version: '1.0.0',
@@ -278,7 +279,8 @@ describe('useAppStore', () => {
}) })
it('clearPublicSettingsCache 清除缓存', () => { it('clearPublicSettingsCache 清除缓存', () => {
;(window as any).__APP_CONFIG__ = { site_name: 'Test' } const windowAny = window as any
windowAny.__APP_CONFIG__ = { site_name: 'Test' }
const store = useAppStore() const store = useAppStore()
store.initFromInjectedConfig() store.initFromInjectedConfig()