126 lines
6.1 KiB
TypeScript
126 lines
6.1 KiB
TypeScript
/**
|
||
* 「凭据不可自愈 ⇒ 任务已中断」弹窗判据(2026-11-12 用户口径:
|
||
* 「没余额要退成失败,且直接中断任务,并弹窗提示」)。
|
||
*
|
||
* 靶子是**两头的错法**都安静:
|
||
* ① 判窄了 → 欠费时任务已判死但界面一句话不说,用户以为还在跑;
|
||
* ② 判宽了 → 青果「没有可用的空闲通道」这类约 3 分钟自愈的占用被弹成「去充值」,
|
||
* 把用户引去等一个不会来的释放,也把抖动当成故障报。
|
||
* 所以每个「该弹」的档位都配一条「不许弹」的反向判据。
|
||
*
|
||
* 跑法:`cd crawler-plugin/frontend-vue && npm test`
|
||
*/
|
||
import test from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { readFileSync } from 'node:fs'
|
||
import { fileURLToPath } from 'node:url'
|
||
|
||
import {
|
||
credentialFatalNotice,
|
||
type CredentialFatalKind,
|
||
} from '../src/shared/utils/credential-fatal-notice.ts'
|
||
|
||
function kindOf(message: string | null | undefined): CredentialFatalKind | null {
|
||
return credentialFatalNotice(message)?.kind ?? null
|
||
}
|
||
|
||
test('上游 LLM 欠费原文弹「额度不足」', () => {
|
||
const producer = '{"error":{"message":"预扣费额度失败, 用户[151246]剩余额度: '
|
||
+ '฿0.029168, 需要预扣费额度: ฿0.032402","type":"new_api_error","code":"insufficient_user_quota"}}'
|
||
assert.equal(kindOf(producer), 'llm_balance')
|
||
})
|
||
|
||
test('后端已判死并写下的指引句按「哪一句」分档', () => {
|
||
// 这两句是生产者写进 errorMessage 的(原文 + 指引),前端只认它,不重拼
|
||
const llmTerminal = 'LLM 网关返回 403;任务已终止:LLM 账户余额不足或密钥失效,不是表格问题;请充值或更换密钥后重新发起任务。'
|
||
const proxyTerminal = '青果提取失败 NO_AVAILABLE_CHANNEL;任务已终止:代理 IP 套餐已到期或余额不足、或提取链接不可用;请充值、续期或更换提取链接后重新发起任务。'
|
||
assert.equal(kindOf(llmTerminal), 'llm_balance')
|
||
assert.equal(kindOf(proxyTerminal), 'proxy')
|
||
})
|
||
|
||
test('代理侧三类不可自愈都弹「代理 IP 不可用」', () => {
|
||
assert.equal(kindOf('用户未配置代理'), 'proxy')
|
||
assert.equal(kindOf('代理套餐已过期'), 'proxy')
|
||
assert.equal(kindOf('代理余额不足'), 'proxy')
|
||
assert.equal(kindOf('青果提取失败 API_AUTH_DENY'), 'proxy')
|
||
// 光有「余额不足」分不清是哪一家的余额:兜底按 LLM 额度(后端同一档),弹窗仍会弹
|
||
assert.equal(kindOf('余额不足'), 'llm_balance')
|
||
})
|
||
|
||
test('LLM 密钥失效与额度不足分两档(要改的地方不同)', () => {
|
||
assert.equal(kindOf('LLM http 401: invalid api key'), 'key_invalid')
|
||
assert.equal(kindOf('密钥已失效,请更新'), 'key_invalid')
|
||
})
|
||
|
||
test('可自愈的供应商状态一律不弹', () => {
|
||
const selfHealing = [
|
||
// 这一条正是判宽了就会误伤的现网文案:它含「套餐到期」字样,但说的是通道被占
|
||
'青果回「没有可用的空闲通道」:可能是同账号的其他提取正占着(约 3 分钟释放),也可能是这把 key 的套餐到期/无余量',
|
||
'代理冷却中,请稍后重试',
|
||
'LLM http 502: Bad Gateway',
|
||
'LLM http 429: Too Many Requests',
|
||
'Connection refused',
|
||
'aiprice 请求超时',
|
||
'页面无效:风控墙',
|
||
]
|
||
for (const message of selfHealing) {
|
||
assert.equal(kindOf(message), null, `误弹: ${message}`)
|
||
}
|
||
})
|
||
|
||
test('京麦未登录和登录态失效提示去客户端重新登录', () => {
|
||
for (const message of [
|
||
'京麦未登录(登录态失效),请在客户端重新登录京麦后重试',
|
||
'京麦登录态失效,任务已终止,请在客户端重新登录京麦',
|
||
'未登录京麦',
|
||
'京麦登录态已失效(HTTP 401)',
|
||
]) {
|
||
const notice = credentialFatalNotice(message)
|
||
assert.equal(notice?.kind, 'jd_login')
|
||
assert.equal(notice?.title, '任务已中断:京麦未登录')
|
||
assert.match(notice?.body ?? '', /请在客户端重新登录京麦/)
|
||
}
|
||
})
|
||
|
||
test('京麦限流、页面结构变化和未注明京麦的登录错误不弹京麦提示', () => {
|
||
for (const message of ['京麦接口限流', '京麦登录态检查超时', '京麦发布页面结构变更', '登录态失效']) {
|
||
assert.equal(kindOf(message), null, `误弹: ${message}`)
|
||
}
|
||
})
|
||
|
||
test('没有凭据字样的失败不弹(抖动不是凭据问题)', () => {
|
||
assert.equal(kindOf('LLM 网关无应答'), null)
|
||
assert.equal(kindOf('扩展注入失败'), null)
|
||
assert.equal(kindOf(''), null)
|
||
assert.equal(kindOf(null), null)
|
||
assert.equal(kindOf(undefined), null)
|
||
})
|
||
|
||
test('文案本身不许把欠费说成抖动,也不许指错地方', () => {
|
||
const notices = ['用户未配置代理', 'insufficient_user_quota', 'invalid api key']
|
||
.map((message) => credentialFatalNotice(message))
|
||
.filter((notice) => notice !== null)
|
||
assert.equal(notices.length, 3)
|
||
for (const notice of notices) {
|
||
assert.equal(notice!.body.includes('稍后重试'), false)
|
||
assert.equal(notice!.body.includes('表格有问题'), true, '要明确告诉用户不是数据问题')
|
||
assert.equal(notice!.title.startsWith('任务已中断'), true)
|
||
}
|
||
})
|
||
|
||
// ---- 接线绊线:判据写好了但没人调 = 一条永远不会出现的弹窗 ----
|
||
|
||
test('五个特性的终态分支都接在同一处上报出口上', () => {
|
||
const sources: Array<[string, string, string]> = [
|
||
['外观专利+相似ASIN 共用面板', '../src/shared/composables/useLiveTaskBoard.ts', 'reportCredentialFatal('],
|
||
['跟价', '../src/pages/brand/components/BrandPriceTrackTab.vue', 'reportCredentialFatalAfterFetch('],
|
||
['采集数据', '../src/pages/brand/components/BrandCollectDataTab.vue', 'reportCredentialFatalAfterFetch('],
|
||
['店铺采集', '../src/pages/brand/components/BrandShopDataCrawlTab.vue', 'reportCredentialFatal('],
|
||
['根组件', '../src/App.vue', '<CredentialFatalAlert />'],
|
||
]
|
||
for (const [label, relative, needle] of sources) {
|
||
const text = readFileSync(fileURLToPath(new URL(relative, import.meta.url)), 'utf8')
|
||
assert.ok(text.includes(needle), `${label} 没有调用 ${needle}`)
|
||
}
|
||
})
|