Files
crawler-plugin/frontend-vue/tests/mount/密钥面板青果检测.mount.test.ts
T
huangzd1997 7e64c12dd6 fix(proxy): 青果检测改真探并按供应商分码,三面收口凭据外漏与「无法判定」
用户报「青果也要加检测」,往下挖出四层假象,一次收口:

- 后端:probeProxy 的青果短路(过去无条件回「提取成功(青果隧道端点,连接鉴权在客户端)」,
  依据的注释已过期——存下来的链接里就带 key/pwd)删掉,改成真打一次提取,按供应商返回码
  分档出独立 check_code:channel_busy / invalid_key / forbidden / rate_limited /
  insufficient_balance / tunnel_unverified。文案出口一律抹 key/pwd。
- 后端:巡检遇 channel_busy 不落库也不推通知。通道被占不是「密钥坏了」而是「此刻问不出
  结论」,而巡检自己那一发真提取就要占掉唯一一条通道——落库等于把用户早上看到的
  「代理异常」写成我们自己造成的。
- 后端:余量口径按供应商分档。青果账号此前会落进「否则查平台套餐」那条,界面显示的是
  别人家的 jikip 余量(串档);青果不提供可鉴权的余量/余额查询(余额接口是控制台
  appid/appkey 签名档),这一档宁可返回「查不了」也不借极客的数。
- 后端:SecretMasking.maskProxy 的脏值回落不再套 mask()——它保后 4 字符,而青果的 pwd=
  恰好挂在末尾,等于把口令尾 4 位交给界面与日志;改成只交「像 host 的那一段」。
- 用户端:去掉「青果账密直连,无需检测」的 disabled 例外,未保存账密现拼链接送检;
  「无法判定」回落改成后端文案原样显示+可重试语义;代理卡不再套 LLM 渠道字典
  (「龙抬头」错标的真根因就在这行)。
- 后管:货源标签拆成两张互不相通的字典,代理列只读 masked 且不回落到原文,删掉
  user-select:all 的全选复制旁路,「修改代理」改成重填 Authkey/Authpwd 的弹窗。

判据:SecretMaskingTest(5)、UserProxyBalanceServiceTest(6)、UserApiSecretServiceTest
(+1 巡检不落库)、UserApiSecretCheckServiceTest(+138 行分码)、ProxyLinkKeyTest、
用户端 node 5 支+mount 10 支、后管 node 9 支+mount 8 支。后端整量 5187/0 失败,
full 闸门四面绿。
2026-11-02 15:18:41 +08:00

289 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 密钥设置面板「代理」那一张卡的挂载用例(2026-11-02 三件事:放开青果检测、结果文案以接口为准、
* 卡片货源标签按供应商)。判的是运行时行为,不是读源码形状。
*
* 四组靶子:
* ① 青果不再被 `proxyProvider === 'qingguo'` 排除在「检测」之外:按钮可点、文案不许说「无需检测」;
* ② 未保存状态下点检测,送出去的是 composeQingguoLink(账密) 现拼的那条(与保存路径同一个出处),
* 没改动时送 undefined 让服务端检库里那条——后端列表读路已套 maskProxy,前端拿不到链接原文;
* ③ 结果文案一律用接口返回的 checkMessage 原文:channel_busy(后端落 error 档)必须是「可重试」语气
* (黄,不是红),也不许被吞成「无法判定」(用户报的就是这个现象);
* ④ 余量按供应商显示;查不到时说人话(不是「不支持」、也不是笼统「暂不可用」)。
*
* ⚠ 用例里的账密一律是假值(fakekey/fakepwd),真实凭据不得进任何跟踪件。
*
* 跑法:`npx vitest run tests/mount/密钥面板青果检测.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'
import { composeQingguoLink } from '@/shared/utils/qingguo-link'
const UID = '1301'
const FAKE_KEY = 'fakekey'
const FAKE_PWD = 'fakepwd'
/** 后端为「通道被占」发的独立码与文案(措辞权威在后端 UserApiSecretCheckService)。 */
const CHANNEL_BUSY_MESSAGE = '青果通道正被你自己的采集占着,等约 3 分钟释放后再点检测'
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<string, unknown> = {}) {
return {
moduleKey: 'proxy',
checkStatus: 'passed',
checkCode: 'ok',
checkMessage: '连通正常',
checkLatencyMs: 120,
checkedAt: null,
viaProxy: false,
...over,
}
}
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]
}
function proxyCheckButton(panel: VueWrapper) {
return proxyCard(panel).findAll('.check-btn')[0]
}
function proxyResultText(panel: VueWrapper) {
return proxyCard(panel).find('.check-result')
}
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.fetchProxyPlan.mockResolvedValue({ planId: null, jikipUserId: null, configured: false })
bridge.readConfig.mockResolvedValue(qingguoConfig())
bridge.saveConfig.mockResolvedValue(undefined)
})
let wrapper: VueWrapper | null = null
afterEach(() => {
wrapper?.unmount()
wrapper = null
})
describe('青果放开检测:按钮、送检链接与结果文案', () => {
it('青果选中时「检测」可点,且不再自称「无需检测」', async () => {
const panel = await mountPanel()
const button = proxyCheckButton(panel)
expect(button.attributes('disabled')).toBeUndefined()
expect(button.text()).not.toContain('无需检测')
expect(button.text()).toContain('检测配置代理')
expect(panel.html()).not.toContain('青果账密直连,无需检测')
})
it('没改动时送 undefined:服务端用它库里存的那条(链接原文已不下发前端)', async () => {
const panel = await mountPanel()
await proxyCheckButton(panel).trigger('click')
await flushPromises()
expect(store.checkApiSecret).toHaveBeenCalledWith('proxy', undefined, 'qingguo')
})
it('改了账密没保存就送现拼链接(与保存路径同一个 composeQingguoLink 出处)', async () => {
const panel = await mountPanel()
const accountInput = proxyCard(panel).findAll('input')[0]
await accountInput.setValue(`${FAKE_KEY}X`)
expect(proxyCheckButton(panel).text()).toContain('检测输入账密(未保存)')
await proxyCheckButton(panel).trigger('click')
await flushPromises()
const sent = store.checkApiSecret.mock.calls[0]?.[1] as string
expect(sent, '送检值必须逐字节等于拼装出处的输出').toBe(
composeQingguoLink(`${FAKE_KEY}X`, FAKE_PWD),
)
expect(sent).toContain('overseas.proxy.qg.net')
expect(message.success).toHaveBeenCalledWith('输入值连通正常(未保存)')
})
it('账密被清空时拦住这一次必然失败的检测(空串≠"用库里的值")', async () => {
const panel = await mountPanel()
const accountInput = proxyCard(panel).findAll('input')[0]
await accountInput.setValue('')
await proxyCheckButton(panel).trigger('click')
await flushPromises()
expect(store.checkApiSecret, '拼不出链接就不该发检测,否则服务端检的是库里那条旧链接').not.toHaveBeenCalled()
expect(message.warning).toHaveBeenCalled()
})
it('channel_busy:文案用接口原文,语气是可重试(黄),不是红色密钥错误也不是「无法判定」', async () => {
store.checkApiSecret.mockResolvedValue(checkResult({
checkStatus: 'error',
checkCode: 'channel_busy',
checkMessage: CHANNEL_BUSY_MESSAGE,
checkLatencyMs: null,
}))
const panel = await mountPanel()
await proxyCheckButton(panel).trigger('click')
await flushPromises()
const result = proxyResultText(panel)
expect(result.text()).toContain(CHANNEL_BUSY_MESSAGE)
expect(result.text()).not.toContain('无法判定')
expect(result.classes()).toContain('check-result--warn')
expect(result.classes()).not.toContain('check-result--fail')
})
it('failed 才标红,且仍原样显示接口给的文案', async () => {
store.checkApiSecret.mockResolvedValue(checkResult({
checkStatus: 'failed',
checkCode: 'invalid_key',
checkMessage: '青果密钥不可用或已失效(INVALID_KEY),请核对提取链接里的 key',
}))
const panel = await mountPanel()
await proxyCheckButton(panel).trigger('click')
await flushPromises()
const result = proxyResultText(panel)
expect(result.text()).toContain('青果密钥不可用或已失效(INVALID_KEY)')
expect(result.classes()).toContain('check-result--fail')
})
it('接口没给文案时才兜底:error 档兜「稍后再点一次」,不再回落到「无法判定」', async () => {
store.checkApiSecret.mockResolvedValue(checkResult({
checkStatus: 'error', checkCode: 'timeout', checkMessage: '',
}))
const panel = await mountPanel()
await proxyCheckButton(panel).trigger('click')
await flushPromises()
const text = proxyResultText(panel).text()
expect(text).toContain('稍后可以再点一次检测')
expect(text).not.toContain('无法判定')
})
})
describe('货源标签与代理余量:按供应商,不套 LLM 渠道字典', () => {
it('代理卡标题下的货源是供应商名(青果),不是 LLM 渠道名', async () => {
const panel = await mountPanel()
expect(proxyCard(panel).find('.source-tag').text()).toBe('青果')
expect(proxyCard(panel).text(), '代理卡里不许出现 LLM 渠道选项').not.toContain('贞贞')
})
it('余量行按当前供应商显示,并按 scope 说明是谁的池子', async () => {
const panel = await mountPanel()
expect(proxyCard(panel).find('.secret-meta').text()).toContain('青果余量(你的套餐)')
expect(proxyCard(panel).find('.secret-meta').text()).toContain('套餐IP 12')
})
it('available=false:把后端给的原因原样摆出来,不说成「该供应商不支持」', async () => {
store.fetchProxyBalanceSafely.mockResolvedValue({
available: false, surplus: null, balance: null, message: '尚未配置套餐,按平台套餐也读不到', scope: null,
})
const panel = await mountPanel()
const meta = proxyCard(panel).find('.secret-meta').text()
expect(meta).toContain('尚未配置套餐,按平台套餐也读不到')
expect(meta).not.toContain('暂不可用')
})
})