/** * 京麦登录自动流转(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 闸触发登录') })