58c40b6695
现场(用户 2026-10-22):点「立即更新」→ 闸门确认并等待 → 进程被 update.exe 替换、 WebView 整页重载。重载那一刻 pywebview 桥还没注入,getPywebviewApi() 返回的降级桥里 vc_ 方法数为 0,而变体采集页把「桥不在」画成「暂无变体采集任务」:loadTasks 四条出口 全是静默 return(无桥/并发中/无 uid/!success),空 catch 也一样;首拉只发一次, 60 秒轮询又被「列表里有活跃任务」反锁 ⇒ 撞空一次就永久空,除手点刷新没有出路。 另外两页是另一种"没掉":跟价 loadHistory 一次 catch 把列表清空,紧接的 syncPollingIdsWithHistory 把「history 没有 + 无快照」的 id 连 localStorage 快照一起删 (不可恢复);商品风险同型且更狠,连快照豁免都没有。更新的几十秒里进程已退、 新进程未起,任何一次 /history 失败或 401 都踩得到。 改法:新增 shared/bridges/bridge-list-state.ts(失败分类/可见文案/首拉重试驱动, 退避复用 getTaskPollBackoffMs,8 轮约 35 秒);变体采集页桥未就绪→重试到成功或次数 用尽,失败态写进空列表文案并给一次 ElMessage;跟价/商品风险加 historyLoadOk 守卫, 拉取失败不清列表、不结算、不删本地 id。静默 catch 棘轮 83→81(实测;本轮减 1, 另 1 处是此前台账未缩,已一并拉平并逐文件改小)。 门禁:npx vue-tsc --noEmit rc=0;npm test 905/905 全绿(含新增 7 例 tests/bridge-list-first-load-retry.test.ts,改前对旧代码真红)。
118 lines
5.5 KiB
TypeScript
118 lines
5.5 KiB
TypeScript
import { test } from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { readFileSync } from 'node:fs'
|
||
import { fileURLToPath } from 'node:url'
|
||
import { dirname, join } from 'node:path'
|
||
|
||
import {
|
||
BRIDGE_LIST_FIRST_LOAD_MAX_ATTEMPTS,
|
||
bridgeListHint,
|
||
bridgeListRetryDelayMs,
|
||
isRetryableBridgeListAttempt,
|
||
runBridgeListFirstLoad,
|
||
} from '../src/shared/bridges/bridge-list-state.ts'
|
||
|
||
/**
|
||
* 「更新客户端 → 在跑的任务列表没掉」回归钉。
|
||
*
|
||
* 现场:点「立即更新」→ 闸门放行 → 进程被 update.exe 替换 → WebView 整页重载。
|
||
* 重载那一下 pywebview 桥还没注入,`getPywebviewApi()` 返回的降级桥里 vc_ 方法数为 0,
|
||
* 而变体采集页把「桥不在」和「没有任务」画成同一句「暂无变体采集任务」,
|
||
* 首拉又只发一次、轮询还被「列表里有活跃任务」反锁 ⇒ 用户看到在跑的任务凭空没了。
|
||
* 这里钉三件事:①首拉必须重试到桥起来;②每种失败都要有可见文案;
|
||
* ③跟价页一次拉取失败不得反过来把本地轮询 id 连快照一起删掉。
|
||
*/
|
||
|
||
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..')
|
||
|
||
function sourceOf(rel: string) {
|
||
return readFileSync(join(ROOT, rel), 'utf8')
|
||
}
|
||
|
||
const VARIANT = 'src/pages/brand/components/BrandVariantTab.vue'
|
||
const PRICE_TRACK = 'src/pages/brand/components/BrandPriceTrackTab.vue'
|
||
const PRODUCT_RISK = 'src/pages/brand/components/BrandProductRiskTab.vue'
|
||
|
||
async function noWait(ms: number) {
|
||
void ms
|
||
}
|
||
|
||
test('桥未就绪的那几次必须重试回来,不能一次撞空就放弃', async () => {
|
||
const seen: string[] = []
|
||
const result = await runBridgeListFirstLoad(async () => {
|
||
// 前两轮正撞进程替换窗口(降级桥),第三轮桥就绪
|
||
seen.push(seen.length < 2 ? 'bridge' : 'ok')
|
||
return seen.length <= 2 ? 'bridge' : 'ok'
|
||
}, { sleep: noWait })
|
||
assert.equal(result.attempts, 3, `桥起来后应在第 3 轮恢复,实际跑了 ${result.attempts} 轮`)
|
||
assert.equal(result.attempt, 'ok')
|
||
assert.equal(result.cancelled, false)
|
||
})
|
||
|
||
test('一直失败就到上限为止,不许无限重试打爆桥', async () => {
|
||
let calls = 0
|
||
const result = await runBridgeListFirstLoad(async () => {
|
||
calls += 1
|
||
return 'failed'
|
||
}, { sleep: noWait })
|
||
assert.equal(calls, BRIDGE_LIST_FIRST_LOAD_MAX_ATTEMPTS)
|
||
assert.equal(result.attempts, BRIDGE_LIST_FIRST_LOAD_MAX_ATTEMPTS)
|
||
assert.equal(result.attempt, 'failed')
|
||
})
|
||
|
||
test('组件卸载即停链,且不把「已取消」说成「取不到」', async () => {
|
||
let calls = 0
|
||
const result = await runBridgeListFirstLoad(async () => {
|
||
calls += 1
|
||
return 'bridge'
|
||
}, { sleep: noWait, isCancelled: () => calls > 0 })
|
||
assert.equal(calls, 1, '卸载后不得再发起下一轮取回')
|
||
assert.equal(result.cancelled, true)
|
||
})
|
||
|
||
test('每一种失败都有用户可见文案,成功不许带文案', () => {
|
||
for (const attempt of ['bridge', 'uid', 'failed'] as const) {
|
||
assert.ok(bridgeListHint(attempt).trim(), `${attempt} 没有可见文案,会退化成「暂无任务」`)
|
||
assert.equal(isRetryableBridgeListAttempt(attempt), true, `${attempt} 必须可重试`)
|
||
}
|
||
assert.equal(bridgeListHint('ok'), '')
|
||
assert.equal(isRetryableBridgeListAttempt('ok'), false)
|
||
})
|
||
|
||
test('退避曲线复用任务轮询那一套,单调且封顶不失控', () => {
|
||
const delays = [1, 2, 3, 4, 5, 6, 7, 8].map(bridgeListRetryDelayMs)
|
||
for (let i = 1; i < delays.length; i += 1) {
|
||
assert.ok(delays[i] >= delays[i - 1], `第 ${i + 1} 轮退避比上一轮短:${delays}`)
|
||
assert.ok(delays[i] <= 10_000, `退避超过 10s:${delays}`)
|
||
}
|
||
})
|
||
|
||
test('变体采集页不再把「桥没起来」画成「暂无任务」', () => {
|
||
const source = sourceOf(VARIANT)
|
||
assert.ok(source.includes('runBridgeListFirstLoad'), `${VARIANT} 没接首拉重试驱动`)
|
||
assert.ok(source.includes('bridgeListHint'), `${VARIANT} 没把失败文案呈现给用户`)
|
||
// 三条无出口 return:撞上了既不重试也不告诉用户,就是这个 bug 的本体
|
||
assert.ok(!/if \(!bridge\?\.vc_list_tasks\) return/.test(source),
|
||
'桥未就绪仍被静默 return,重载后列表会永久空')
|
||
assert.ok(!/if \(!uid\(\)\) return/.test(source),
|
||
'登录态缺失仍被静默 return')
|
||
assert.match(source, /async function bootstrapListLoad\(\)[\s\S]*?runBridgeListFirstLoad\(\(\) => loadTasks\(1\)/,
|
||
'首拉必须走重试驱动,而不是裸发一次')
|
||
assert.match(source, /onMounted\(\(\) => \{[\s\S]*?void bootstrapListLoad\(\)/,
|
||
'onMounted 必须起这条重试链')
|
||
})
|
||
|
||
test('history 拉取失败不得清空列表、更不得据此删本地轮询 id', () => {
|
||
for (const [rel, name] of [[PRICE_TRACK, '跟价'], [PRODUCT_RISK, '商品风险']] as const) {
|
||
const source = sourceOf(rel)
|
||
const loadHistory = source.match(/async function loadHistory\(\)[\s\S]*?\n\}/)
|
||
assert.ok(loadHistory, `${name}:找不到 loadHistory,接线断言失去对象`)
|
||
assert.ok(!/historyItems\.value = \[\]/.test(loadHistory[0]),
|
||
`${name}:拉取失败把列表清空,随后的结算会把在跑的任务判成「已不存在」并删掉本地 id`)
|
||
const sync = source.match(/function syncPollingIdsWithHistory\(\)[\s\S]*?\n\}/)
|
||
assert.ok(sync, `${name}:找不到 syncPollingIdsWithHistory,接线断言失去对象`)
|
||
assert.match(sync[0], /historyLoad(Ok|Failed)/,
|
||
`${name}:结算必须先确认本轮 history 真拉成功,否则一次抖动就会删光本地轮询 id 与快照`)
|
||
}
|
||
})
|