/** * 密钥设置面板「青果 Authpwd 眼睛」的挂载用例(2026-11-02 用户看图点名:Authkey 是明文可见的, * Authpwd 却是 •••••••• 且没有任何查看手段,填错了只能靠猜)。 * * 五组靶子: * ① 默认掩码,点眼睛变明文,再点回掩码(`useSecretVisibility` 的两态开关在界面上真的成立); * ② 切换眼睛**不动绑定值**、**不产生改动标记**、**不发保存/送检请求**(点一下眼睛就冒出 * 「检测输入账密(未保存)」=把可见性误判成用户改了代理); * ③ 眼睛只管 Authpwd 这一格:Authkey 照旧明文、各模块密钥照旧掩码,不做全站唯一的特例化扩散; * ④ 保存成功即回到掩码(沿用该面板「保存成功即清空输入」的口径),且输入值不丢; * ⑤ 卸载后重新挂载(重开弹窗/换号后重读配置)不得留一个还显示着明文的重载路径。 * * ⚠ 用例里的账密一律是假值(fakekey/fakepwd),真实凭据不得进任何跟踪件。 * * 跑法:`npx vitest run tests/mount/密钥面板authpwd显隐.mount.test.ts` */ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { flushPromises, mount, type VueWrapper } from '@vue/test-utils' import ApiSecretSettingsPanel from '@/shared/components/ApiSecretSettingsPanel.vue' const UID = '1301' const FAKE_KEY = 'fakekey' const FAKE_PWD = 'fakepwd' const message = vi.hoisted(() => ({ error: vi.fn(), warning: vi.fn(), success: vi.fn(), info: vi.fn(), })) const store = vi.hoisted(() => ({ checkApiSecret: vi.fn(), clearApiSecret: vi.fn(), getServerProxySnapshot: vi.fn(), getStoredApiSecretSnapshot: vi.fn(), listApiSecretModules: vi.fn(), loadApiSecrets: vi.fn(), saveApiSecret: vi.fn(), subscribeApiSecrets: vi.fn(), fetchProxyBalanceSafely: vi.fn(), })) const secretApi = vi.hoisted(() => ({ putMyApiSecret: vi.fn(), deleteMyApiSecret: vi.fn(), fetchProxyPlan: vi.fn(), saveProxyPlan: vi.fn(), clearProxyPlan: vi.fn(), })) const bridge = vi.hoisted(() => ({ readConfig: vi.fn(), saveConfig: vi.fn(), })) vi.mock('element-plus', () => ({ ElMessage: message })) vi.mock('@/shared/utils/api-secret-store', () => ({ checkApiSecret: store.checkApiSecret, clearApiSecret: store.clearApiSecret, getServerProxySnapshot: store.getServerProxySnapshot, getStoredApiSecretSnapshot: store.getStoredApiSecretSnapshot, listApiSecretModules: store.listApiSecretModules, loadApiSecrets: store.loadApiSecrets, saveApiSecret: store.saveApiSecret, subscribeApiSecrets: store.subscribeApiSecrets, fetchProxyBalanceSafely: store.fetchProxyBalanceSafely, })) vi.mock('@/shared/api/types/modules/user-secret', () => ({ putMyApiSecret: secretApi.putMyApiSecret, deleteMyApiSecret: secretApi.deleteMyApiSecret, fetchProxyPlan: secretApi.fetchProxyPlan, saveProxyPlan: secretApi.saveProxyPlan, clearProxyPlan: secretApi.clearProxyPlan, })) vi.mock('@/shared/bridges/pywebview', () => ({ getPywebviewApi: () => ({ read_config: bridge.readConfig, save_config: bridge.saveConfig }), })) function emptySnapshot() { return { value: '', masked: '', channel: '', exists: false, locked: false, checkStatus: 'unknown', checkCode: '', checkMessage: '', checkLatencyMs: null, checkedAt: null, updatedAt: null, } } /** 本机已存过青果账密(proxy_users[uid]):桌面桥读得到的就是这份。 */ function qingguoConfig() { return { proxy_users: { [UID]: { proxy_url: '', proxy_mode: 2, proxy_provider: 'qingguo', proxy_account: FAKE_KEY, proxy_password: FAKE_PWD, }, }, } } function checkResult(over: Record = {}) { return { moduleKey: 'proxy', checkStatus: 'passed', checkCode: 'ok', checkMessage: '连通正常', checkLatencyMs: 120, checkedAt: null, viaProxy: false, ...over, } } let wrapper: VueWrapper | null = null async function mountPanel() { wrapper = mount(ApiSecretSettingsPanel) await flushPromises() await flushPromises() return wrapper } /** 代理是最后一张卡。 */ function proxyCard(panel: VueWrapper) { const cards = panel.findAll('.secret-card') return cards[cards.length - 1] } /** 青果账密那一行:[0]=Authkey、[1]=Authpwd。 */ function accountInput(panel: VueWrapper) { return proxyCard(panel).findAll('input')[0] } function passwordInput(panel: VueWrapper) { return proxyCard(panel).findAll('input')[1] } function eyeButton(panel: VueWrapper) { return proxyCard(panel).find('.password-toggle') } /** 读输入框当前值:VTU 的 DOMWrapper 上没有取值的语法糖,别拿 text() 冒充。 */ function valueOf(node: { element: Element }) { return (node.element as HTMLInputElement).value } function proxyCheckButton(panel: VueWrapper) { return proxyCard(panel).findAll('.check-btn')[0] } async function saveAll(panel: VueWrapper, options: { requireAll?: boolean } = {}) { const exposed = panel.vm as unknown as { saveAll: (o?: { requireAll?: boolean }) => Promise } const saved = await exposed.saveAll(options) await flushPromises() await flushPromises() return saved } beforeEach(() => { localStorage.clear() localStorage.setItem('uid', UID) vi.clearAllMocks() store.listApiSecretModules.mockReturnValue([ { moduleKey: 'appearance-patent', moduleLabel: '外观专利密钥' }, { moduleKey: 'similar-asin', moduleLabel: '货源查询密钥' }, ]) store.getStoredApiSecretSnapshot.mockReturnValue(emptySnapshot()) store.getServerProxySnapshot.mockReturnValue(null) store.subscribeApiSecrets.mockReturnValue(() => undefined) store.loadApiSecrets.mockResolvedValue('complete') store.checkApiSecret.mockResolvedValue(checkResult()) store.fetchProxyBalanceSafely.mockResolvedValue({ available: true, surplus: '12', balance: '34', message: null, scope: 'user', low: false, lowReason: null, }) secretApi.putMyApiSecret.mockResolvedValue({}) secretApi.fetchProxyPlan.mockResolvedValue({ planId: null, jikipUserId: null, configured: false }) bridge.readConfig.mockResolvedValue(qingguoConfig()) bridge.saveConfig.mockResolvedValue(undefined) }) afterEach(() => { wrapper?.unmount() wrapper = null }) describe('Authpwd 眼睛:两态切换', () => { it('默认掩码,点一次变明文,再点一次回掩码', async () => { const panel = await mountPanel() const pwd = passwordInput(panel) expect(pwd.attributes('type')).toBe('password') expect(eyeButton(panel).attributes('aria-pressed')).toBe('false') await eyeButton(panel).trigger('click') expect(pwd.attributes('type'), '点眼睛就是把这一格的 type 换掉').toBe('text') expect(eyeButton(panel).attributes('aria-pressed')).toBe('true') expect(eyeButton(panel).attributes('aria-label')).toBe('隐藏密码') await eyeButton(panel).trigger('click') expect(pwd.attributes('type')).toBe('password') expect(eyeButton(panel).attributes('aria-pressed')).toBe('false') }) it('切换眼睛不动绑定值、不记成改动、也不发保存或送检请求', async () => { const panel = await mountPanel() const pwd = passwordInput(panel) const storageKeys = Object.keys(localStorage) await eyeButton(panel).trigger('click') await eyeButton(panel).trigger('click') await flushPromises() expect(valueOf(pwd), '可见性切换不该吃掉一个字符').toBe(FAKE_PWD) expect(valueOf(accountInput(panel))).toBe(FAKE_KEY) expect(proxyCheckButton(panel).text(), '点眼睛不等于「改了代理」:按钮不许开始自称检的是未保存输入值') .toContain('检测配置代理') expect(store.checkApiSecret).not.toHaveBeenCalled() expect(bridge.saveConfig).not.toHaveBeenCalled() expect(secretApi.putMyApiSecret).not.toHaveBeenCalled() expect(Object.keys(localStorage), '「用户点了显示密码」不配拥有一个持久化键').toEqual(storageKeys) }) it('只切 Authpwd 这一格:Authkey 照旧明文、模块密钥照旧掩码', async () => { const panel = await mountPanel() await eyeButton(panel).trigger('click') expect(passwordInput(panel).attributes('type')).toBe('text') expect(accountInput(panel).attributes('type')).toBe('text') for (const card of panel.findAll('.secret-card').slice(0, -1)) { expect(card.find('input').attributes('type'), '别的密钥不跟着这次改动放开').toBe('password') } }) }) describe('Authpwd 眼睛:复位口径', () => { it('保存成功后回到掩码,且账密输入值不丢', async () => { const panel = await mountPanel() const pwd = passwordInput(panel) await eyeButton(panel).trigger('click') expect(pwd.attributes('type')).toBe('text') await pwd.setValue(`${FAKE_PWD}Z`) expect(await saveAll(panel)).toBe(true) expect(bridge.saveConfig).toHaveBeenCalledTimes(1) expect(pwd.attributes('type'), '保存成功=配置已落地,界面没理由还亮着明文').toBe('password') expect(valueOf(pwd)).toBe(`${FAKE_PWD}Z`) }) it('卸载后重新挂载(重开弹窗/换号重读配置)不带出明文', async () => { const first = await mountPanel() await eyeButton(first).trigger('click') expect(passwordInput(first).attributes('type')).toBe('text') first.unmount() const again = await mountPanel() expect(passwordInput(again).attributes('type')).toBe('password') expect(eyeButton(again).attributes('aria-pressed')).toBe('false') expect(valueOf(passwordInput(again)), '重读到的还是那把假密码,只是重新盖上').toBe(FAKE_PWD) }) it('后台锁定代理时眼睛跟着输入框一起禁用', async () => { store.getServerProxySnapshot.mockReturnValue({ ...emptySnapshot(), exists: true, locked: true, channel: 'qingguo', full: '', }) const panel = await mountPanel() expect(passwordInput(panel).attributes('disabled')).toBeDefined() expect(eyeButton(panel).attributes('disabled'), '只读态不该还能把明文翻出来').toBeDefined() }) })