Files
crawler-plugin/admin-frontend-vue/tests/密钥货源标签与代理脱敏.test.ts
T

279 lines
17 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.
import test from 'node:test'
import assert from 'node:assert/strict'
import { readSource } from './helpers.ts'
import {
DEFAULT_PROXY_PROVIDER,
INDETERMINATE_BAND,
NOT_CHECKED_BAND,
PASSED_BAND,
UNKNOWN_SOURCE_LABEL,
buildProxyEntryPayload,
checkCodeBand,
emptyProxyEntryDraft,
isRetryBand,
llmSourceLabel,
moduleStatusBand,
proxyDisplayText,
proxySourceLabel,
rowDetailLines,
rowStatusBand,
validateProxyEntry,
} from '../src/pages/account/user-secret-model.ts'
import { QINGGUO_EXTRACT_URL, composeQingguoLink } from '../src/shared/utils/qingguo-link.ts'
// 密钥管理页(后管)三条收口的判据(2026-11-02):
// ① 货源标签按模块分字典——代理列只认供应商,LLM 渠道名不许出现在代理行上;
// ② 代理列只显示后端脱敏值(masked),界面既不回落原文、也没有可预填的整条链接;
// ③ 检测结果按 `check_code` 分档——后端今天把青果检测分成一族供应商码,界面只认
// `check_status` 三档就会把「通道被自己占着」和「密钥坏了」「欠费」挤成同一句「无法判定」。
// 夹具里的 key/pwd 全是假值(fakekey/fakepwd),本仓有尺子按长度+高熵认凭据。
test('代理列只认供应商:LLM 渠道值在代理列认不出', () => {
assert.equal(proxySourceLabel('qingguo'), '青果')
assert.equal(proxySourceLabel('JIKIP'), '极客', '后端值大小写不一时应归一')
assert.equal(proxySourceLabel(' qingguo '), '青果')
// 这一条就是截图上「代理行贴了龙抬头」的形状:合表按值匹配会认下来,分表必须认不出
assert.equal(proxySourceLabel('longtaitou'), UNKNOWN_SOURCE_LABEL)
assert.equal(proxySourceLabel('t8star'), UNKNOWN_SOURCE_LABEL)
assert.equal(proxySourceLabel(''), UNKNOWN_SOURCE_LABEL)
assert.equal(proxySourceLabel(null), UNKNOWN_SOURCE_LABEL)
assert.equal(proxySourceLabel(undefined), UNKNOWN_SOURCE_LABEL)
})
test('LLM 列只认渠道:代理供应商名不许贴到密钥列上', () => {
assert.equal(llmSourceLabel('longtaitou'), '龙抬头')
assert.equal(llmSourceLabel('t8star'), '贞贞')
assert.equal(llmSourceLabel('qingguo'), UNKNOWN_SOURCE_LABEL)
assert.equal(llmSourceLabel('jikip'), UNKNOWN_SOURCE_LABEL)
assert.equal(llmSourceLabel('nonsense'), UNKNOWN_SOURCE_LABEL)
// 两张表除占位符外不得有任何共同输出:合表就是从这个性质破掉的
const proxyLabels = ['qingguo', 'jikip'].map(proxySourceLabel)
assert.deepEqual(proxyLabels.filter((label) => label !== UNKNOWN_SOURCE_LABEL), ['青果', '极客'])
})
test('代理列文本只取 masked,绝不回落到原文字段', () => {
const link = composeQingguoLink('fakekey', 'fakepwd')
const masked = 'https://overseas.proxy.qg.net' // 后端 SecretMasking.maskProxy 的输出形状
assert.equal(proxyDisplayText({ masked, full: '' }), masked)
// 万一某个旧回包还带着 full:界面也不许把它渲染出来(回落原文=脱敏白做)
assert.equal(proxyDisplayText({ masked, full: link }), masked)
const shown = proxyDisplayText({ masked, full: link })
assert.ok(!shown.includes('fakekey'), `显示文本里漏出了 key:${shown}`)
assert.ok(!shown.includes('fakepwd'), `显示文本里漏出了 pwd:${shown}`)
assert.ok(!shown.includes('get?'), `显示文本里漏出了提取链接的查询段:${shown}`)
})
test('masked 为空时显示占位符,不猜原文', () => {
assert.equal(proxyDisplayText({ masked: '', full: composeQingguoLink('fakekey', 'fakepwd') }), UNKNOWN_SOURCE_LABEL)
assert.equal(proxyDisplayText({ masked: null }), UNKNOWN_SOURCE_LABEL)
assert.equal(proxyDisplayText(undefined), UNKNOWN_SOURCE_LABEL)
})
test('青果链接拼装口径:overseas 域+key+pwd,特殊字符按 URL 组件编码', () => {
assert.equal(composeQingguoLink('fakekey', 'fakepwd'), `${QINGGUO_EXTRACT_URL}?key=fakekey&pwd=fakepwd`)
assert.equal(composeQingguoLink(' fakekey ', ' fakepwd '), `${QINGGUO_EXTRACT_URL}?key=fakekey&pwd=fakepwd`)
// pwd 里的 @ 与 : 不编码会把 host 切歪(AGENTS 铁律 25 实录)
const encoded = composeQingguoLink('fakekey', 'p@ss:1')
assert.ok(encoded.includes('pwd=p%40ss%3A1'), encoded)
})
test('改代理每次从空表开始:没有原文可预填', () => {
const draft = emptyProxyEntryDraft('qingguo')
assert.equal(draft.provider, 'qingguo')
assert.deepEqual([draft.authkey, draft.authpwd, draft.link], ['', '', ''])
// 供应商值认不出时不猜成凭据,只回落到后端默认供应商(极客)
assert.equal(emptyProxyEntryDraft('longtaitou').provider, DEFAULT_PROXY_PROVIDER)
assert.equal(emptyProxyEntryDraft(null).provider, DEFAULT_PROXY_PROVIDER)
})
test('青果要两把都填:缺哪把就点名哪把', () => {
assert.equal(validateProxyEntry({ provider: 'qingguo', authkey: '', authpwd: 'fakepwd', link: '' }), '请填写青果 Authkey')
assert.match(validateProxyEntry({ provider: 'qingguo', authkey: 'fakekey', authpwd: ' ', link: '' }), /Authpwd/)
assert.equal(validateProxyEntry({ provider: 'qingguo', authkey: 'fakekey', authpwd: 'fakepwd', link: '' }), '')
assert.equal(buildProxyEntryPayload({ provider: 'qingguo', authkey: 'fakekey', authpwd: 'fakepwd', link: '' })?.value,
composeQingguoLink('fakekey', 'fakepwd'))
assert.equal(buildProxyEntryPayload({ provider: 'qingguo', authkey: 'fakekey', authpwd: '', link: '' }), null,
'不合法时不得产出载荷(拿空串顶上去等于把配置清空)')
})
test('极客填自家提取链接:形状与后端 validateProxyValue 同口径、不扩大判死', () => {
const draft = { provider: 'jikip' as const, authkey: '', authpwd: '', link: 'https://api.example.test/ip-get?appkey=fakekey' }
assert.equal(validateProxyEntry(draft), '')
assert.deepEqual(buildProxyEntryPayload(draft), { value: 'https://api.example.test/ip-get?appkey=fakekey', channel: 'jikip' })
assert.match(validateProxyEntry({ ...draft, link: '' }), /极客提取链接/)
assert.match(validateProxyEntry({ ...draft, link: 'ftp://api.example.test/ip-get' }), /格式不正确/)
assert.match(validateProxyEntry({ ...draft, link: 'not a url' }), /格式不正确/)
// 静态代理地址(http://host:port)同样是后端认的形态,前端不许判死
assert.equal(validateProxyEntry({ ...draft, link: 'http://1.2.3.4:8080' }), '')
})
test('载荷带上这一单的供应商:channel 缺失会被后端按域名识别,识别不出就静默换供应商', () => {
assert.equal(buildProxyEntryPayload({ provider: 'qingguo', authkey: 'fakekey', authpwd: 'fakepwd', link: '' })?.channel, 'qingguo')
assert.equal(buildProxyEntryPayload({ provider: 'jikip', authkey: '', authpwd: '', link: 'https://api.example.test/ip-get' })?.channel, 'jikip')
})
/* ------------------------------------------------------------------ *
* ③ 检测结果按 check_code 分档(2026-11-02)
*
* 码集与状态取自后端 `UserApiSecretCheckService` 的 `CODE_*`/`STATUS_*`(逐条对着源码抄):
* `channel_busy`、`tunnel_unverified`、`rate_limited`、`timeout`、`network_error`、`server_error`
* 落 `error`;`insufficient_balance`、`invalid_key`、`forbidden`、`bad_request`、`provider_error`、
* `network_error`(提取接口不可达那一支)落 `failed`;`ok` 落 `passed`。
* 界面只看 `check_status` 的话,前一半全被挤成「无法判定」——现网 uid=1 的 proxy 行就是这么花的。
* ------------------------------------------------------------------ */
/** 后端 2026-11-02 那一版实际会下发的码全集(新增码时这张清单要一起长)。 */
const BACKEND_CHECK_CODES = [
'ok',
'channel_busy',
'invalid_key',
'forbidden',
'bad_request',
'rate_limited',
'server_error',
'network_error',
'timeout',
'provider_error',
'insufficient_balance',
'tunnel_unverified',
]
/** 已配置的一格(默认 error + 空码:那是「问了问不出」的形状,不是任何一档码)。 */
function checkedModule(over: Record<string, unknown> = {}) {
return { exists: true, moduleLabel: '代理设置', checkStatus: 'error', checkCode: '', checkMessage: '', ...over }
}
test('后端分出的每一档码都不许掉进「无法判定」(逐码扫全集)', () => {
const offenders: string[] = []
for (const code of BACKEND_CHECK_CODES) {
// 同一个码在 error/failed 两个状态下都扫一遍:`network_error` 后端就两支都用
for (const status of ['error', 'failed']) {
const band = moduleStatusBand(checkedModule({ checkCode: code, checkStatus: status }))
if (band.label === INDETERMINATE_BAND.label) offenders.push(`${status}/${code}`)
}
}
assert.deepEqual(offenders, [], '这些码又被当成「无法判定」渲染:' + offenders.join(', '))
})
test('逐码档位表:档名与配色都登记在案(可重试那一族既不正红也不黄)', () => {
const table: Array<[string, string, string]> = [
['ok', '通过', 'is-allowed'],
['channel_busy', '无可用通道', 'is-retry'],
['rate_limited', '限流重试', 'is-retry'],
['timeout', '超时重试', 'is-retry'],
['network_error', '网络重试', 'is-retry'],
['server_error', '上游重试', 'is-retry'],
['insufficient_balance', '欠费', 'is-warn'],
['tunnel_unverified', '链接缺账密', 'is-warn'],
['invalid_key', '密钥失效', 'is-blocked'],
['forbidden', '鉴权被拒', 'is-blocked'],
['bad_request', '参数被拒', 'is-blocked'],
['provider_error', '供应商报错', 'is-blocked'],
]
assert.deepEqual(table.map((row) => row[0]).sort(), [...BACKEND_CHECK_CODES].sort(), '这张表与码集不同步:漏档就红')
for (const [code, label, tone] of table) {
const band = moduleStatusBand(checkedModule({ checkCode: code }))
assert.equal(band.label, label, `码 ${code} 的档名变了`)
assert.equal(band.tone, tone, `码 ${code} 的配色变了`)
assert.ok(band.label.length <= 5, `档名「${band.label}」超过 5 字:状态列 130px 会把药丸裁掉`)
}
})
test('变异钉子:把 channel_busy 改回「不认识」立刻红', () => {
const busy = moduleStatusBand(checkedModule({ checkCode: 'channel_busy' }))
const blank = moduleStatusBand(checkedModule({ checkCode: '' }))
// 这一条就是今天的线上形状:认不出码 ⇒ 与空码同档 ⇒ 显示「无法判定」
assert.notDeepEqual(busy, blank, 'channel_busy 与空码同档=又回到「error 一律无法判定」')
assert.ok(isRetryBand(busy), 'channel_busy 必须落在可重试那一族')
assert.equal(busy.tone, 'is-retry')
assert.notEqual(busy.tone, INDETERMINATE_BAND.tone, '可重试与查不出必须是两种配色')
})
test('欠费(现网 21 行)有自己的一档,且不再吊在 check_status=failed 上', () => {
const broke = checkedModule({ checkCode: 'insufficient_balance' })
assert.deepEqual(moduleStatusBand(broke), { label: '欠费', tone: 'is-warn' })
// 后端哪天把欠费落成 error,界面仍然说欠费(按码判档,不看状态脸色)
assert.deepEqual(moduleStatusBand({ ...broke, checkStatus: 'error' }), { label: '欠费', tone: 'is-warn' })
assert.notEqual(moduleStatusBand(broke).label, INDETERMINATE_BAND.label)
})
test('「无法判定」只留给真没结论:空码与未知码才落这一档', () => {
assert.deepEqual(moduleStatusBand(checkedModule({ checkCode: '' })), INDETERMINATE_BAND)
assert.deepEqual(moduleStatusBand(checkedModule({ checkCode: ' ' })), INDETERMINATE_BAND)
assert.deepEqual(moduleStatusBand(checkedModule({ checkCode: 'a_code_backend_has_not_invented_yet' })), INDETERMINATE_BAND)
// 没问过的格子不是「问不出」:后端 upsert 后 check_status=unknown,这里留「未检测」
assert.deepEqual(moduleStatusBand(checkedModule({ checkStatus: 'unknown' })), NOT_CHECKED_BAND)
assert.deepEqual(moduleStatusBand(checkedModule({ checkStatus: '' })), NOT_CHECKED_BAND)
assert.notDeepEqual(NOT_CHECKED_BAND, INDETERMINATE_BAND, '「未检测」与「无法判定」是两件事,不许并档')
// 未配置优先于一切状态
assert.deepEqual(moduleStatusBand(checkedModule({ exists: false, checkCode: 'channel_busy' })), { label: '未配置', tone: 'is-unknown' })
assert.deepEqual(moduleStatusBand(undefined), { label: '未配置', tone: 'is-unknown' })
// 后端新增一档状态而前端没登记:failed 兜底成「失败」,不把坏说成查不出
assert.equal(moduleStatusBand(checkedModule({ checkStatus: 'failed', checkCode: 'brand_new_code' })).tone, 'is-blocked')
})
test('passed 状态一律「通过」:码位上留着旧值也不许翻档', () => {
assert.deepEqual(moduleStatusBand(checkedModule({ checkStatus: 'passed', checkCode: 'ok' })), PASSED_BAND)
// 后端 applyCheckOutcome 状态与码是同一条 UPDATE,留旧码属异常;此时以状态为准,不给管理员一个假红灯
assert.deepEqual(moduleStatusBand(checkedModule({ checkStatus: 'passed', checkCode: 'timeout' })), PASSED_BAND)
})
test('码名归一:大小写与首尾空格不改变档位', () => {
assert.deepEqual(checkCodeBand(' CHANNEL_BUSY '), checkCodeBand('channel_busy'))
assert.deepEqual(checkCodeBand('Insufficient_Balance')?.label, '欠费')
assert.equal(checkCodeBand(null), null)
assert.equal(checkCodeBand(undefined), null)
assert.equal(checkCodeBand(''), null)
})
test('行级状态:整行只剩可重试那一族时补成「可重试」,混进别的就还「无法判定」', () => {
const busyRow = {
status: 'error',
similarAsin: checkedModule({ moduleLabel: '货源查询密钥', checkStatus: 'passed', checkCode: 'ok' }),
appearancePatent: checkedModule({ moduleLabel: '外观专利密钥', checkStatus: 'passed', checkCode: 'ok' }),
proxy: checkedModule({ checkStatus: 'error', checkCode: 'channel_busy' }),
}
assert.deepEqual(rowStatusBand(busyRow), { label: '可重试', tone: 'is-retry' })
// 另一格是「链接缺账密」:那一格不会自己好,把整行说成可重试等于让管理员一直等
assert.deepEqual(rowStatusBand({ ...busyRow, appearancePatent: checkedModule({ checkCode: 'tunnel_unverified' }) }), INDETERMINATE_BAND)
// 后端行级聚合的其余取值原样保留(优先级是后端的,界面不重排)
assert.deepEqual(rowStatusBand({ status: 'passed' }), { label: '检测通过', tone: 'is-allowed' })
assert.deepEqual(rowStatusBand({ status: 'failed' }), { label: '检测失败', tone: 'is-blocked' })
assert.deepEqual(rowStatusBand({ status: 'incomplete' }), { label: '未配齐', tone: 'is-warn' })
assert.deepEqual(rowStatusBand({ status: 'unknown' }), NOT_CHECKED_BAND)
assert.deepEqual(rowStatusBand({ status: 'error' }), INDETERMINATE_BAND, '行级 error 但一格码都认不出时仍是「无法判定」')
assert.deepEqual(rowStatusBand(null), NOT_CHECKED_BAND)
})
test('行级明细逐字搬后端 check_message:前端不改写、不另拼句子', () => {
const busyMessage = '后端原话:通道被占,等约 3 分钟再点'
const lines = rowDetailLines({
status: 'error',
similarAsin: checkedModule({ moduleLabel: '货源查询密钥', checkStatus: 'passed', checkCode: 'ok', checkMessage: '连通正常' }),
appearancePatent: checkedModule({ moduleLabel: '外观专利密钥', exists: false, checkMessage: '' }),
proxy: checkedModule({ checkStatus: 'error', checkCode: 'channel_busy', checkMessage: busyMessage }),
})
assert.deepEqual(lines, ['货源查询密钥:连通正常', `代理设置:${busyMessage}`])
assert.ok(lines[1].endsWith(busyMessage), `后端那句被改写了:${lines[1]}`)
// 没有 message 的格子不占位(否则 tooltip 里一串空段,运维读不到重点)
assert.deepEqual(rowDetailLines({ proxy: checkedModule({ checkMessage: ' ' }) }), [])
})
test('页面只有一张码表:不留旧 switch、tooltip 取后端 checkMessage、is-retry 真有条配色', () => {
const page = readSource('src/pages/account/UserSecretsPage.vue', true)
// ① 页面不得自带第二张表:两处各判各的,后端加分档时界面只会漏(今天漏的就是 channel_busy)
for (const code of BACKEND_CHECK_CODES) {
// 'ok' 这种两字母串当子串查必然误报(token/lock 里就有),它也不是会漏进旧 switch 的那族
if (code.length < 4) continue
assert.ok(!page.includes(code), `页面里又写了一遍码名 ${code}`)
}
assert.ok(!/case 'error':/.test(page), "页面里还留着按状态硬分档的旧 switch('error' 一律「无法判定」)")
assert.match(page, /moduleStatusBand\(/, '单格药丸必须走模型那一档')
assert.match(page, /rowStatusBand\(row\)/, '行级药丸必须吃整行:后端只剩一个 error,逐格码才说得清')
// ② 悬停那句必须是后端原文,且不许开回 `full` 那条旁路
assert.match(page, /module\.checkMessage/, 'tooltip 要的是后端那句人话(运维判欠费还是被自己占着就靠它)')
assert.ok(!/module\.full/.test(page), '不得读 full:后台侧恒空,读它等于把脱敏前的行为留在界面里')
// ③ 新档位得真有条样式,否则 is-retry 渲染出来是无框无底的裸文本
assert.match(page, /\.wh-pill\.is-retry\s*\{[^}]*background/, '页面没有 .is-retry 这一档配色')
})