Files
crawler-plugin/frontend-vue/tests/jd-login-autoflow.test.ts
T
huangzd1997 a51f867d7c feat(jd): 「登录京麦」按钮退役改懒触发自动登录链+进度分母未知走不确定态+FAILED 归历史区(2026-11-23 客户三条口径)
1. **京麦登录自动化**(客户口径「登录能不能自动化的识别,登录按钮不要了」):
   新 shared/stores/jd-login-autoflow.ts——京麦登录态探测要起有头浏览器,常态化探测
   每次进页闪窗口 ⇒ 懒触发:会话内任一路(同步分类 jd_login/任务致命公告
   CredentialFatalAlert kind=jd_login)发现未登录即置旗;此后建单/重试前
   passJdLoginGate 预检,旗置位时自动开登录窗、登录成功才放行(重试闸排在复位**之前**
   ——复位成功即 PENDING,预检拦下却没人领=任务挂死);busy 档 8 秒自动重试一次;
   同步分类登录成功后自动重拉(fromAutoLogin 防递归)。open_jd_login 桥与客户端
   login_window.py 原样复用,app_client 零改动。
2. **进度条不确定态**:在跑但 totalCount 读不出(分类筛选未生效走行级兜底翻页)时
   TaskItemCard 进度条走流动条纹,计数文案点名「总量未知,按页累计」——0% 空轨道
   看着像坏了。
3. **FAILED 归历史任务区**(11-23 拍板,推翻 11-22「失败是当前区待办」):
   CURRENT_AREA_STATUSES 摘掉 FAILED——重试/删除按钮在历史行同形槽不丢,失败单堆当前区
   反而看不清在跑的。

判据:jd-login-autoflow.test.ts(store 分档+重试语义)+jd-collect-tab.test.ts 改反钉
(按钮/状态机复活即红、预检位置绊线)+mount 51 支(旗隔离 resetJdLoginAutoflowForTest
防串用例、jd_login 自动开窗、indeterminate 断言)。
2026-11-23 17:30:20 +08:00

170 lines
7.9 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-23「登录京麦」按钮退役)回归判据。
*
* 背景:按钮退役后登录态走懒触发自动链路——会话内任何一路发现「京麦未登录」(同步分类
* jd_login / 任务致命公告 kind=jd_login)就置会话旗;此后建单/重试前的 ensure 放行闸
* 自动开登录窗,登录成功清旗回到零探测常态。靶子是三种错法:
* ① 旗未置位也开窗(每次建单都闪浏览器窗口=常态化探测,探测必须有成本理由才做);
* ② 登录成功不清旗(旗永远置着 ⇒ 每次建单都多等一次登录窗确认,自动链路退化为手动);
* ③ busy 档不重试(任务判死瞬间 worker 还在收尾,第一发必撞 jd_busy,不重试=自动弹窗
* 十有八九哑火,用户又回到「找不到登录入口」的死局)。
*
* 跑法:`cd crawler-plugin/frontend-vue && node --test tests/jd-login-autoflow.test.ts`
*/
import test from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import {
clearJdLoginExpired,
ensureJdLogin,
isJdLoginExpired,
JD_BUSY_RETRY_DELAY_MS,
markJdLoginExpired,
openJdLoginViaBridge,
resetJdLoginAutoflowForTest,
} from '../src/shared/stores/jd-login-autoflow.ts'
const sleepCalls: number[] = []
const sleepSpy = async (ms: number) => { sleepCalls.push(ms) }
function bridgeOf(result: unknown, calls: unknown[] = []) {
return { open_jd_login: async () => { calls.push(result); return result } }
}
test('会话旗:默认未置、mark/clear/reset 各归其位', () => {
resetJdLoginAutoflowForTest()
assert.equal(isJdLoginExpired(), false)
markJdLoginExpired()
assert.equal(isJdLoginExpired(), true)
clearJdLoginExpired()
assert.equal(isJdLoginExpired(), false)
})
test('旗未置位时 ensure 直接放行,绝不碰桥(零探测常态,不闪浏览器窗口)', async () => {
resetJdLoginAutoflowForTest()
// 桥桩一被调就抛:旗未置位的路径任何桥调用都是判红点
const hostile = { open_jd_login: async () => { throw new Error('桥不该被调') } }
const gate = await ensureJdLogin(hostile, { sleep: sleepSpy })
assert.deepEqual(gate, { status: 'skipped', reason: 'flag_not_set' })
assert.equal(sleepCalls.length, 0)
})
test('旗置位+无桥(老客户端/纯浏览器)=升级指引的 error 档', async () => {
resetJdLoginAutoflowForTest()
markJdLoginExpired()
const gate = await ensureJdLogin(null, { sleep: sleepSpy })
assert.equal(gate.status, 'unresolved')
assert.equal(gate.reason, 'no_bridge')
assert.equal(gate.severity, 'error')
assert.match(gate.hint ?? '', /客户端版本较旧|本机客户端/)
})
test('already_logged_in = ok 且清旗:下次建单回到零探测常态', async () => {
resetJdLoginAutoflowForTest()
markJdLoginExpired()
const gate = await ensureJdLogin(bridgeOf({ success: true, code: 'already_logged_in',
message: '京麦已是登录状态' }), { sleep: sleepSpy })
assert.equal(gate.status, 'ok')
assert.equal(gate.hint, '京麦已是登录状态')
assert.equal(isJdLoginExpired(), false, '登录成功必须清会话旗,否则每次建单都卡一次登录窗')
})
test('jd_busy 档自动重试一次(覆盖任务判死后 worker 收尾窗口),第二次成功=ok', async () => {
resetJdLoginAutoflowForTest()
markJdLoginExpired()
let calls = 0
const api = { open_jd_login: async () => {
calls += 1
return calls === 1
? { success: false, code: 'jd_busy', error: '京东采集/发布任务正在运行' }
: { success: true, code: 'logged_in', message: '京麦登录成功' }
} }
const gate = await ensureJdLogin(api, { sleep: sleepSpy })
assert.equal(calls, 2, 'busy 必须自动重试一次:判死瞬间 worker 还在收尾,不重试=自动弹窗哑火')
assert.deepEqual(sleepCalls, [JD_BUSY_RETRY_DELAY_MS])
assert.equal(gate.status, 'ok')
})
test('两连 busy = busy 档(amber 动作句),busyRetry:false 关掉重试', async () => {
resetJdLoginAutoflowForTest()
markJdLoginExpired()
const busy = { success: false, code: 'jd_busy', error: '京东采集/发布任务正在运行' }
const gate = await ensureJdLogin(bridgeOf(busy), { sleep: sleepSpy })
assert.equal(gate.status, 'busy')
assert.equal(gate.severity, 'notice')
assert.match(gate.hint ?? '', /收尾|自动再弹出/)
markJdLoginExpired()
sleepCalls.length = 0
const noRetry = await ensureJdLogin(bridgeOf(busy), { sleep: sleepSpy, busyRetry: false })
assert.equal(noRetry.status, 'busy')
assert.deepEqual(sleepCalls, [], 'busyRetry:false 不得重试')
})
test('jd_login_running 与 jd_busy 同档(登录窗口已在等待=可恢复)', async () => {
resetJdLoginAutoflowForTest()
markJdLoginExpired()
const gate = await ensureJdLogin(bridgeOf({ success: false, code: 'jd_login_running',
error: '登录窗口已在等待登录' }), { sleep: sleepSpy })
assert.equal(gate.status, 'busy')
})
test('timeout/window_closed = unresolved notice(窗口还在或下次动作自动再弹);jd_page/未知 = error', async () => {
resetJdLoginAutoflowForTest()
markJdLoginExpired()
const timeout = await ensureJdLogin(bridgeOf({ success: false, code: 'timeout',
error: '等待登录超时' }), { sleep: sleepSpy })
assert.equal(timeout.status, 'unresolved')
assert.equal(timeout.severity, 'notice')
markJdLoginExpired()
const closed = await ensureJdLogin(bridgeOf({ success: false, code: 'window_closed',
error: '采集浏览器窗口已关闭' }), { sleep: sleepSpy })
assert.equal(closed.status, 'unresolved')
assert.equal(closed.severity, 'notice')
markJdLoginExpired()
const page = await ensureJdLogin(bridgeOf({ success: false, code: 'jd_page',
error: '打开登录窗口失败:boom' }), { sleep: sleepSpy })
assert.equal(page.status, 'unresolved')
assert.equal(page.severity, 'error')
assert.equal(page.hint, '打开登录窗口失败:boom')
markJdLoginExpired()
const unknown = await ensureJdLogin(bridgeOf({ success: false }), { sleep: sleepSpy })
assert.equal(unknown.status, 'unresolved')
assert.equal(unknown.severity, 'error')
})
test('桥抛异常兜底成 jd_page error 档(桥线程契约:不往调用方抛)', async () => {
resetJdLoginAutoflowForTest()
markJdLoginExpired()
const result = await openJdLoginViaBridge({ open_jd_login: async () => { throw new Error('boom') } })
assert.equal(result?.success, false)
assert.equal(result?.code, 'jd_page')
assert.match(result?.error ?? '', /boom/)
})
test('CredentialFatalAlert.vue 接线锚点:jd_login 公告必须置旗并 ensure 自动开窗', () => {
const here = fileURLToPath(new URL('.', import.meta.url))
const src = readFileSync(`${here}../src/shared/components/CredentialFatalAlert.vue`, 'utf8')
assert.ok(src.includes("queued.notice.kind === 'jd_login'"),
'致命公告组件必须按 kind 分档:只有京麦未登录才自动开登录窗(代理/LLM 欠费弹浏览器=灾难)')
assert.ok(src.includes('autoOpenJdLoginWindow()') && src.includes('markJdLoginExpired()')
&& src.includes('ensureJdLogin(getPywebviewApi())'),
'jd_login 公告的自动开窗链路必须接线(置旗 + ensure)')
assert.ok(src.includes('void ensureJdLogin'),
'自动开窗必须 fire-and-forget:登录窗要等人工登完(最长 10 分钟),不能阻塞公告循环')
})
test('JdCollectTab.vue 锚点:登录桥已不在页面直调(统一走 jd-login-autoflow)', () => {
const here = fileURLToPath(new URL('.', import.meta.url))
const src = readFileSync(`${here}../src/pages/jd/JdCollectTab.vue`, 'utf8')
assert.ok(!src.includes("api?.open_jd_login"),
'页面不得再直调 open_jd_login 桥:分档与重试统一收口在 jd-login-autoflow(重复实现=改一处漏一处)')
assert.ok(src.includes("from '@/shared/stores/jd-login-autoflow'"),
'页面必须经 jd-login-autoflow 的 ensure 闸触发登录')
})