From 29ca1290b3b7017736fa6783fce1e2d40968d4bf Mon Sep 17 00:00:00 2001 From: yangjianbo Date: Tue, 10 Feb 2026 00:37:56 +0800 Subject: [PATCH] =?UTF-8?q?chore(test):=20=E6=B8=85=E7=90=86=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B=E4=B8=8E=E7=B1=BB=E5=9E=8B=E5=AF=BC?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/internal/pkg/geminicli/oauth_test.go | 2 +- .../internal/util/logredact/redact_test.go | 4 +- frontend/src/api/__tests__/client.spec.ts | 2 +- .../components/__tests__/Dashboard.spec.ts | 1 - .../__tests__/useClipboard.spec.ts | 16 ++++-- .../__tests__/useTableLoader.spec.ts | 3 +- frontend/src/router/__tests__/guards.spec.ts | 57 ------------------- frontend/src/stores/__tests__/app.spec.ts | 6 +- 8 files changed, 20 insertions(+), 71 deletions(-) diff --git a/backend/internal/pkg/geminicli/oauth_test.go b/backend/internal/pkg/geminicli/oauth_test.go index 664e0344..14bc3c6b 100644 --- a/backend/internal/pkg/geminicli/oauth_test.go +++ b/backend/internal/pkg/geminicli/oauth_test.go @@ -377,7 +377,7 @@ func TestBuildAuthorizationURL_EmptyRedirectURI(t *testing.T) { OAuthConfig{}, "test-state", "test-challenge", - "", // 空 redirectURI + "", // 空 redirectURI "", "code_assist", ) diff --git a/backend/internal/util/logredact/redact_test.go b/backend/internal/util/logredact/redact_test.go index a9ec89c6..64a7b3cf 100644 --- a/backend/internal/util/logredact/redact_test.go +++ b/backend/internal/util/logredact/redact_test.go @@ -28,9 +28,9 @@ func TestRedactText_QueryLike(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) - if strings.Contains(out, "abcdefghijklmnopqrstuvwxyz") { + if strings.Contains(out, "your-client-secret") { t.Fatalf("expected secret redacted, got %q", out) } if !strings.Contains(out, "client_secret=***") { diff --git a/frontend/src/api/__tests__/client.spec.ts b/frontend/src/api/__tests__/client.spec.ts index 0e92c6d1..0f663e76 100644 --- a/frontend/src/api/__tests__/client.spec.ts +++ b/frontend/src/api/__tests__/client.spec.ts @@ -1,6 +1,6 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' import axios from 'axios' -import type { AxiosInstance, InternalAxiosRequestConfig, AxiosResponse, AxiosHeaders } from 'axios' +import type { AxiosInstance } from 'axios' // 需要在导入 client 之前设置 mock vi.mock('@/i18n', () => ({ diff --git a/frontend/src/components/__tests__/Dashboard.spec.ts b/frontend/src/components/__tests__/Dashboard.spec.ts index b83808cc..72bc4d28 100644 --- a/frontend/src/components/__tests__/Dashboard.spec.ts +++ b/frontend/src/components/__tests__/Dashboard.spec.ts @@ -9,7 +9,6 @@ import { defineComponent, ref, onMounted, nextTick } from 'vue' // Mock API const mockGetDashboardStats = vi.fn() -const mockRefreshUser = vi.fn() vi.mock('@/api', () => ({ authAPI: { diff --git a/frontend/src/composables/__tests__/useClipboard.spec.ts b/frontend/src/composables/__tests__/useClipboard.spec.ts index b2c4de41..3d1ffb05 100644 --- a/frontend/src/composables/__tests__/useClipboard.spec.ts +++ b/frontend/src/composables/__tests__/useClipboard.spec.ts @@ -96,10 +96,12 @@ describe('useClipboard', () => { }) 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,手动定义 - ;(document as any).execCommand = vi.fn().mockReturnValue(true) + const documentAny = document as any + documentAny.execCommand = vi.fn().mockReturnValue(true) const { copyToClipboard, copied } = useClipboard() const result = await copyToClipboard('fallback text') @@ -112,7 +114,8 @@ describe('useClipboard', () => { it('非安全上下文使用 fallback', async () => { 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 result = await copyToClipboard('insecure context text') @@ -124,8 +127,11 @@ describe('useClipboard', () => { }) it('所有复制方式均失败时调用 showError', async () => { - ;(navigator.clipboard.writeText as any).mockRejectedValue(new Error('fail')) - ;(document as any).execCommand = vi.fn().mockReturnValue(false) + const writeTextMock = navigator.clipboard.writeText as any + writeTextMock.mockRejectedValue(new Error('fail')) + + const documentAny = document as any + documentAny.execCommand = vi.fn().mockReturnValue(false) const { copyToClipboard, copied } = useClipboard() const result = await copyToClipboard('text') diff --git a/frontend/src/composables/__tests__/useTableLoader.spec.ts b/frontend/src/composables/__tests__/useTableLoader.spec.ts index 0eb6f42c..674ecf79 100644 --- a/frontend/src/composables/__tests__/useTableLoader.spec.ts +++ b/frontend/src/composables/__tests__/useTableLoader.spec.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' import { useTableLoader } from '@/composables/useTableLoader' -import { nextTick } from 'vue' // Mock @vueuse/core 的 useDebounceFn vi.mock('@vueuse/core', () => ({ @@ -212,7 +211,7 @@ describe('useTableLoader', () => { }) }) - const { load, items } = useTableLoader({ fetchFn }) + const { load } = useTableLoader({ fetchFn }) // 第一次加载 const p1 = load() diff --git a/frontend/src/router/__tests__/guards.spec.ts b/frontend/src/router/__tests__/guards.spec.ts index 931f4534..2f7cfad1 100644 --- a/frontend/src/router/__tests__/guards.spec.ts +++ b/frontend/src/router/__tests__/guards.spec.ts @@ -1,7 +1,5 @@ import { describe, it, expect, vi, beforeEach } from 'vitest' -import { createRouter, createMemoryHistory } from 'vue-router' import { setActivePinia, createPinia } from 'pinia' -import { defineComponent, h } from 'vue' // Mock 导航加载状态 vi.mock('@/composables/useNavigationLoading', () => { @@ -47,61 +45,6 @@ vi.mock('@/api/auth', () => ({ 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 状态 interface MockAuthState { diff --git a/frontend/src/stores/__tests__/app.spec.ts b/frontend/src/stores/__tests__/app.spec.ts index 432a7079..30ba5c8f 100644 --- a/frontend/src/stores/__tests__/app.spec.ts +++ b/frontend/src/stores/__tests__/app.spec.ts @@ -250,7 +250,8 @@ describe('useAppStore', () => { describe('公开设置加载', () => { it('从 window.__APP_CONFIG__ 初始化', () => { - ;(window as any).__APP_CONFIG__ = { + const windowAny = window as any + windowAny.__APP_CONFIG__ = { site_name: 'TestSite', site_logo: '/logo.png', version: '1.0.0', @@ -278,7 +279,8 @@ describe('useAppStore', () => { }) it('clearPublicSettingsCache 清除缓存', () => { - ;(window as any).__APP_CONFIG__ = { site_name: 'Test' } + const windowAny = window as any + windowAny.__APP_CONFIG__ = { site_name: 'Test' } const store = useAppStore() store.initFromInjectedConfig()