Files
crawler-plugin/frontend-vue/tests/mount/jd-collect-progress.mount.test.ts
T
huangzd1997 c9ecae6e6d feat(jd): 京东数据库与建单页类目面板换京麦授权类目真树(2026-11-16 客户拍板「京麦类目是这些 我们后台的现在的不对」+「你直接爬」)
真机取证京麦首屏接口 getVenderLicenseCategory(62KB:一级就 3 个=该店铺授权经营类目 服饰内衣1315/母婴1319/运动户外1318,250 个末级,最深 4 级;落库末级名样本「艺考形体服/啦啦队服/拉丁舞服」都在树内 level 3=category_path 等值过滤契约成立;4 组末级同名跨支如实登记,取证件 D:/tmp/jd_cat_probe/)。两份 jd-category-tree.ts 正本同轮重写;JdCategoryCascader 重做京麦同款逐级下钻+末级选中(admin 单选过滤/建单页多选 categoryPaths),搜索搜树内末级(行 title 带路径)+树外兜底段(京麦树已无的历史名仍可选);建单页 hint 文案随真树口径更新。判据:vue-tsc×2 rc=0;node --test 树测试 6/6×2+jd-collect-tab 9/9;vitest admin mount 28/28、用户端 mount 78/78(「勾选类目」用例改真树下钻 运动户外>舞蹈体操鞋服/装备>艺考形体服);fast 闸门 overall=pass 四面绿
2026-11-16 12:16:18 +08:00

357 lines
15 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.
/**
* 「获取店铺数据」一键采集 + 任务面板挂载用例(v1.5 改版后)。
*
* 页面改版(2026-11-15):左侧只留「一键获取店铺数据」按钮,右侧换共享任务面板
* (TaskCenterPanel + useLiveTaskBoard,与外观专利同一骨架);单任务进度条移除,
* 进度在任务卡片上。四组靶子保留,缺一条就是假绿:
* ① 点一次按钮就走完整链路:建单 → 激活 → 把 `jd-shop-collect-run` 推进本机客户端队列 → 轮询批量进度;
* ② 有 total 时卡片进度条按已落库商品数算出真实百分比(分母只来自服务端游标,不编造);
* ③ total 缺失时只显示「已采集 N 个商品」,不编造百分比;
* ④ 终态即收手:SUCCESS/FAILED 从轮询移除,FAILED 显示服务端原因并出「重试」按钮
* (重试走通用原位复位 + 重新激活推队列——断点续传,不新建任务)。
*
* 跑法:`npx vitest run tests/mount/jd-collect-progress.mount.test.ts`
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
import JdCollectTab from '@/pages/jd/JdCollectTab.vue'
const api = vi.hoisted(() => ({
createJdCollectTask: vi.fn(),
// 8d8c88a5 起建单后先 PENDING→RUNNING 再推队列;mock 里缺这一个键会让组件拿到
// undefined 调用、enqueue 永远不被调用(本文件当时没跟上,红在这里)。
activateJdCollectTask: vi.fn(),
fetchJdCollectCategories: vi.fn(),
fetchJdCollectGroups: vi.fn(),
fetchJdCollectDashboard: vi.fn(),
fetchJdCollectHistory: vi.fn(),
fetchJdCollectTaskProgressBatch: vi.fn(),
deleteJdCollectTask: vi.fn(),
interruptJdCollectTask: vi.fn(),
retryClientTask: vi.fn(),
enqueue: vi.fn(),
}))
vi.mock('@/shared/api/types/modules/jd-collect', () => ({
createJdCollectTask: api.createJdCollectTask,
activateJdCollectTask: api.activateJdCollectTask,
fetchJdCollectCategories: api.fetchJdCollectCategories,
fetchJdCollectGroups: api.fetchJdCollectGroups,
fetchJdCollectProgress: vi.fn(),
fetchJdCollectDashboard: api.fetchJdCollectDashboard,
fetchJdCollectHistory: api.fetchJdCollectHistory,
fetchJdCollectTaskProgressBatch: api.fetchJdCollectTaskProgressBatch,
deleteJdCollectTask: api.deleteJdCollectTask,
interruptJdCollectTask: api.interruptJdCollectTask,
}))
vi.mock('@/shared/api/task-retry', () => ({
retryClientTask: api.retryClientTask,
}))
vi.mock('@/shared/bridges/pywebview', () => ({
getPywebviewApi: () => ({ enqueue_json: api.enqueue }),
}))
const EMPTY_DASHBOARD = {
pendingTaskCount: 0,
processedTaskCount: 0,
successTaskCount: 0,
failedTaskCount: 0,
}
function historyItem(over: Record<string, unknown> = {}) {
return {
taskId: 88,
taskNo: 'JD-1',
taskStatus: 'RUNNING',
success: false,
errorMessage: null,
shopName: '花屿巷旗舰店',
listTabs: ['ONSALE', 'OFFSALE'],
rowCount: 30,
itemsReceived: 30,
total: 100,
page: 2,
listTab: 'ONSALE',
batches: 1,
createdAt: '2026-11-15T10:00:00',
updatedAt: null,
finishedAt: null,
...over,
}
}
function batchOf(item: Record<string, unknown>) {
return {
items: [
{
task: {
id: item.taskId ?? 88,
taskNo: 'JD-1',
status: item.taskStatus ?? 'RUNNING',
errorMessage: item.errorMessage ?? null,
updatedAt: null,
},
items: [item],
},
],
missingTaskIds: [],
}
}
let wrapper: VueWrapper | null = null
async function mountPage() {
wrapper = mount(JdCollectTab, {
global: {
stubs: {
NotificationBell: true,
RouterLink: { template: '<a class="router-link-stub"><slot /></a>' },
'router-link': { template: '<a class="router-link-stub"><slot /></a>' },
},
},
})
await flushPromises()
return wrapper
}
async function startCollect(w: VueWrapper) {
await w.find('.collect-btn').trigger('click')
await flushPromises()
}
beforeEach(() => {
vi.useFakeTimers()
localStorage.setItem('username', 'admin')
for (const mock of Object.values(api)) mock.mockReset()
api.createJdCollectTask.mockResolvedValue({ taskId: 88, taskNo: 'JD-1', collectChannel: 'LOCAL' })
api.activateJdCollectTask.mockResolvedValue(null)
api.fetchJdCollectCategories.mockResolvedValue([])
api.fetchJdCollectGroups.mockResolvedValue([])
api.enqueue.mockResolvedValue({ success: true })
api.fetchJdCollectDashboard.mockResolvedValue(EMPTY_DASHBOARD)
api.fetchJdCollectHistory.mockResolvedValue({ items: [] })
api.fetchJdCollectTaskProgressBatch.mockResolvedValue(batchOf(historyItem()))
vi.spyOn(console, 'error').mockImplementation(() => {})
vi.spyOn(console, 'warn').mockImplementation(() => {})
})
afterEach(() => {
wrapper?.unmount()
wrapper = null
vi.useRealTimers()
vi.restoreAllMocks()
localStorage.clear()
})
describe('获取店铺数据:一键采集 + 任务面板', () => {
it('点一次按钮就走完整链路:建单 → 激活 → 推本机队列 → 轮询批量进度', async () => {
const w = await mountPage()
await startCollect(w)
expect(api.createJdCollectTask).toHaveBeenCalledTimes(1)
// 店铺名留空 → 按「京麦后台」占位(客户端从京麦后台现读真实店铺名落库);
// 不选类目=空数组(全店采集,后端归一化为 null 进 request_json)
expect(api.createJdCollectTask).toHaveBeenCalledWith('京麦后台', ['ONSALE', 'OFFSALE'], 0, [])
expect(api.activateJdCollectTask).toHaveBeenCalledWith(88)
expect(api.enqueue).toHaveBeenCalledTimes(1)
const payload = api.enqueue.mock.calls[0][0]
expect(payload.type).toBe('jd-shop-collect-run')
expect(payload.data).toEqual({
taskId: 88,
shopName: '京麦后台',
listTabs: ['ONSALE', 'OFFSALE'],
maxProducts: 0,
categoryPaths: [],
})
// 轮询已启动:批量进度端点拿到任务 88
expect(api.fetchJdCollectTaskProgressBatch).toHaveBeenCalled()
const calledIds = api.fetchJdCollectTaskProgressBatch.mock.calls[0][0]
expect(calledIds).toContain(88)
})
it('有 total 时任务卡片显示真实百分比(分母只来自服务端游标)', async () => {
const w = await mountPage()
await startCollect(w)
await flushPromises()
expect(w.find('.task-item').exists()).toBe(true)
expect(w.find('.status').text()).toBe('正在获取店铺数据…')
const bar = w.find('.file-progress-bar')
expect(bar.exists()).toBe(true)
expect(bar.attributes('style')).toContain('width: 30%')
// 百分比文字随 countLabel 一起出(TaskItemCard 有 countLabel 就不显示「总进度 X%」)
expect(w.find('.file-progress-meta').text()).toContain('已采集 30/100 个商品(30%)')
})
it('没有分母(total 缺失)时只显示已采集行数,不编造百分比', async () => {
api.fetchJdCollectTaskProgressBatch.mockResolvedValue(
batchOf(historyItem({ total: null, page: null, itemsReceived: 30, rowCount: 30 })),
)
const w = await mountPage()
await startCollect(w)
await flushPromises()
const meta = w.find('.file-progress-meta')
expect(meta.exists()).toBe(true)
expect(meta.text()).toContain('已采集 30 个商品')
expect(meta.text()).not.toContain('%')
expect(w.find('.file-progress-bar').attributes('style')).toContain('width: 0%')
})
it('SUCCESS 终态:状态徽章落「获取完成」、进度满格,且从轮询中收手', async () => {
const w = await mountPage()
await startCollect(w)
await flushPromises()
api.fetchJdCollectTaskProgressBatch.mockResolvedValue(
batchOf(historyItem({ taskStatus: 'SUCCESS', success: true, itemsReceived: 100, rowCount: 100 })),
)
await vi.advanceTimersByTimeAsync(60_000)
await flushPromises()
expect(w.find('.status').text()).toBe('获取完成')
// 终态任务没有进度条(isActiveTask 才有),行数行收口在卡片 extraLines
expect(w.find('.file-progress-bar').exists()).toBe(false)
expect(w.find('.task-item').text()).toContain('已采集 100 个商品')
// 终态即收手:再推进几轮轮询间隔,批量进度不再被调
const callsAfterTerminal = api.fetchJdCollectTaskProgressBatch.mock.calls.length
await vi.advanceTimersByTimeAsync(60_000)
await flushPromises()
expect(api.fetchJdCollectTaskProgressBatch.mock.calls.length).toBe(callsAfterTerminal)
})
it('FAILED 终态:显示服务端原因、出「重试」按钮,重试走原位复位并重新激活推队列', async () => {
const w = await mountPage()
await startCollect(w)
await flushPromises()
api.fetchJdCollectTaskProgressBatch.mockResolvedValue(
batchOf(historyItem({
taskStatus: 'FAILED',
success: false,
errorMessage: '京麦后台未登录,请先在客户端登录京麦',
itemsReceived: 30,
})),
)
await vi.advanceTimersByTimeAsync(60_000)
await flushPromises()
expect(w.find('.status').text()).toBe('获取失败')
expect(w.find('.task-item').text()).toContain('京麦后台未登录')
const retryBtn = w.find('.btn-retry')
expect(retryBtn.exists()).toBe(true)
expect(retryBtn.text()).toBe('重试')
await retryBtn.trigger('click')
await flushPromises()
// 铁律 26:原任务复位(不克隆新任务),复位后重新激活并推队列(断点续传)
expect(api.retryClientTask).toHaveBeenCalledWith(88)
expect(api.activateJdCollectTask).toHaveBeenCalledWith(88)
expect(api.enqueue).toHaveBeenCalled()
const pushPayload = api.enqueue.mock.calls.at(-1)?.[0]
expect(pushPayload?.type).toBe('jd-shop-collect-run')
expect(pushPayload?.data?.taskId).toBe(88)
})
it('建单失败:只报错不留假任务,一次批量进度请求也不发', async () => {
api.createJdCollectTask.mockRejectedValue(new Error('缺少登录凭据'))
const w = await mountPage()
await startCollect(w)
expect(w.find('.feedback.error').text()).toContain('缺少登录凭据')
expect(api.fetchJdCollectTaskProgressBatch).not.toHaveBeenCalled()
expect(api.enqueue).not.toHaveBeenCalled()
})
it('激活失败:不推队列(否则首个分片必被「任务不在运行中」拒收),并把原因说给用户', async () => {
api.activateJdCollectTask.mockRejectedValue(new Error('服务暂不可用'))
const w = await mountPage()
await startCollect(w)
expect(api.activateJdCollectTask).toHaveBeenCalledWith(88)
expect(api.enqueue).not.toHaveBeenCalled()
expect(w.find('.feedback.error').text()).toContain('任务激活失败')
})
it('激活时服务端回「已在执行中」:等同激活成功,照常推队列', async () => {
api.activateJdCollectTask.mockRejectedValue(new Error('任务已在执行中(客户端已认领)'))
const w = await mountPage()
await startCollect(w)
expect(api.enqueue).toHaveBeenCalledTimes(1)
expect(w.find('.feedback.error').exists()).toBe(false)
})
it('勾选类目后建单:categoryPaths 随建单请求体与推送载荷同链下发', async () => {
// 2026-11-16 用户口径「客户端前端也要同步加上京东类目」+「你直接爬」:面板换京麦
// 授权类目真树(逐级下钻、末级勾选)——下钻 运动户外 → 舞蹈体操鞋服/装备(客户
// 舞蹈服类目在京麦归运动户外支)→ 勾末级「艺考形体服」后建单请求体与推送载荷都要
// 带 ['艺考形体服'](客户端按行 categoryPath 等值过滤的源头;mock 类目用树内真实
// 末级名,贴合落库形状)。
api.fetchJdCollectCategories.mockResolvedValue([
{ name: '艺考形体服', productCount: 5 },
{ name: '啦啦队服', productCount: 3 },
])
const w = await mountPage()
await w.find('.cat-trigger').trigger('click')
await w.findAll('.cat-col')[0].findAll('.cat-row').find((row) => row.text().includes('运动户外'))!.trigger('click')
let cols = w.findAll('.cat-col')
expect(cols).toHaveLength(2, '点亮一级后第二栏出现(真树下钻)')
await cols[1].findAll('.cat-row').find((row) => row.text().includes('舞蹈体操鞋服/装备'))!.trigger('click')
cols = w.findAll('.cat-col')
expect(cols).toHaveLength(3, '二级非末级:第三栏出末级子级')
const leafRow = cols[2].findAll('.cat-row').find((row) => row.text().includes('艺考形体服'))
expect(leafRow, '第三栏必须渲染出该支的末级(艺考形体服)').toBeTruthy()
await leafRow!.trigger('click')
await flushPromises()
expect(w.find('.cat-trigger-text').text()).toBe('艺考形体服', '单选勾选后触发框显示类目名')
await w.find('.cat-mask').trigger('click')
await startCollect(w)
expect(api.createJdCollectTask).toHaveBeenCalledWith('京麦后台', ['ONSALE', 'OFFSALE'], 0, ['艺考形体服'])
const payload = api.enqueue.mock.calls[0][0]
expect(payload.data).toEqual({
taskId: 88,
shopName: '京麦后台',
listTabs: ['ONSALE', 'OFFSALE'],
maxProducts: 0,
categoryPaths: ['艺考形体服'],
})
})
it('店铺分类只读块:页面加载即拉取并把管理员建的分类与商品数展示出来', async () => {
// 2026-11-16 客户口径「管理员弄了多少店铺分类 普通账户都能看到」:普通账号在
// 「获取店铺数据」页就能看到后管京东数据库建的分类全集;本页只读,不提供增删改。
api.fetchJdCollectGroups.mockResolvedValue([
{ id: 1, name: 'ASSS', sortOrder: 0, productCount: 20 },
{ id: 2, name: '家居日用', sortOrder: 1, productCount: 7 },
])
const w = await mountPage()
expect(api.fetchJdCollectGroups).toHaveBeenCalledTimes(1)
const block = w.findAll('.category-block').find((el) => el.text().includes('店铺分类'))
expect(block, '必须有「店铺分类」展示块').toBeTruthy()
expect(block!.text()).toContain('店铺分类(管理员维护,只读)')
const chips = block!.findAll('.group-chip')
expect(chips.length).toBe(2)
expect(chips[0].text()).toContain('ASSS(20)')
expect(chips[1].text()).toContain('家居日用(7)')
expect(chips[0].attributes('title')).toContain('ASSS:20 个商品')
})
it('后台还没建分类:只读块落空态提示,采集主路照常', async () => {
const w = await mountPage()
const block = w.findAll('.category-block').find((el) => el.text().includes('店铺分类'))
expect(block, '空态也要有「店铺分类」块(不能整块消失让用户以为没这功能)').toBeTruthy()
expect(block!.text()).toContain('后台还没建店铺分类')
expect(block!.text()).toContain('管理员在「京东数据库 → 店铺分类」里建的会显示在这里')
expect(block!.find('.group-chip').exists()).toBe(false)
// 拉取失败只 warning、不影响建单主路(catch 口径见 catch-silence-ratchet 319)
expect(w.find('.collect-btn').exists()).toBe(true)
})
})