feat(jd): 首页一级入口「京东」+工作台两菜单,获取店铺数据一键采集带进度条
首页在亚马逊/视频右侧新增「京东」一级入口(恒显示),落地页 /jd-console 按亚马逊控制台样式做两张工具卡:获取店铺数据(可用)、商品上架(占位,点开只提示「暂未开通」)。
获取店铺数据改成一键:建单 → 推 jd-shop-collect-run 到本机队列 → 轮询服务端进度。进度只读两处真源——任务行状态 + scope 行合并写游标里的 itemsReceived/cursor.total(客户端抓列表第一页时把 total 写进游标),没有分母就退回不定态动画,不编造百分比;终态即停轮询。后端新增 GET /api/jd-collect/tasks/{taskId}/progress(按 taskId+userId 校归属)。
判据:JdCollectProgressQueryTest(5)、jd-console-tool-catalog.test.ts(5)、jd-console-pending-menu.mount(3)、jd-collect-progress.mount(6);catch 棘轮 blocks 314→315 按名登记(轮询 catch 非静默)。
This commit is contained in:
@@ -70,7 +70,10 @@ const BASELINE = {
|
||||
// 页面推送包一支 try/catch(P0-1 派发链)——推送失败/桥不可用时 pushNote 附一句提示、
|
||||
// 建单照常保留(判据:推送是尽力而为的快路径,失败回滚建单反而丢单)。错误进了界面
|
||||
// 文案,非静默;silent 一格没涨。
|
||||
blocks: 314,
|
||||
// 315 = 314 + 1(2026-11-14 v1.5「获取店铺数据」进度条):JdCollectTab.vue 的
|
||||
// pollProgress 轮询 catch——单次进度查询失败不打断轮询(网络抖动/切页),
|
||||
// 连续 5 次才把一句中文提示写进 errorMessage,异常原文只进 console;非静默。
|
||||
blocks: 315,
|
||||
// 2026-10-30(ops Phase 6/可读棘轮):ApiSecretSettingsPanel.vue 1113→775 行,面板里那处
|
||||
// 静默 catch(余额查询失败只 console.warn、界面留空)随 loadBalance 整块搬进
|
||||
// useProxyPlan.ts。不是新增静默:面板 1→0、新文件记 1,silent 总数与 blocks 都一格没动,
|
||||
|
||||
@@ -314,8 +314,10 @@ test('test_endpoints_frozen_snapshot', () => {
|
||||
},
|
||||
// 2026-11-14 v1.4 京东数据库:三支端点(建单/心跳/分片回传)随 Phase 3 落码,
|
||||
// 冻结快照只许显式追加——端点漂移会让"新开一条匿名可达口"没人看见。
|
||||
// 2026-11-14 v1.5:progress(进度条数据源,鉴权与建单同口径:uid 会话身份 + owner 校验)。
|
||||
"jdCollect": {
|
||||
"create": "/api/jd-collect/tasks",
|
||||
"progress": "/api/jd-collect/tasks/{taskId}/progress",
|
||||
"heartbeat": "/api/jd-collect/tasks/{taskId}/heartbeat",
|
||||
"chunk": "/api/jd-collect/tasks/{taskId}/chunk"
|
||||
},
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import { existsSync, readFileSync } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
|
||||
import { JD_TOOLS, JD_TOOL_MAP } from '../src/pages/jd/tool-catalog.ts'
|
||||
|
||||
/** 京东一级入口(v1.5):首页「京东」→ 京东工作台两个菜单(获取店铺数据 / 商品上架占位)。 */
|
||||
|
||||
const ROOT = process.cwd()
|
||||
|
||||
function read(relativePath: string): string {
|
||||
const path = join(ROOT, relativePath)
|
||||
assert.ok(existsSync(path), `文件缺失:${path}`)
|
||||
return readFileSync(path, 'utf8')
|
||||
}
|
||||
|
||||
test('京东工作台只有两个菜单:获取店铺数据 + 商品上架', () => {
|
||||
assert.equal(JD_TOOLS.length, 2, '京东工作台菜单数应为 2')
|
||||
const names = JD_TOOLS.map((tool) => tool.name)
|
||||
assert.deepEqual(names, ['获取店铺数据', '商品上架'])
|
||||
assert.equal(new Set(JD_TOOLS.map((tool) => tool.id)).size, 2, '工具 id 不应重复')
|
||||
assert.ok(JD_TOOL_MAP.get('jd-collect'), 'JD_TOOL_MAP 应按 id 命中获取店铺数据')
|
||||
})
|
||||
|
||||
test('获取店铺数据指向采集页,商品上架先做占位菜单', () => {
|
||||
const collect = JD_TOOLS.find((tool) => tool.id === 'jd-collect')
|
||||
const publish = JD_TOOLS.find((tool) => tool.id === 'jd-publish')
|
||||
assert.equal(collect?.href, '/jd-collect', '获取店铺数据应指向 /jd-collect')
|
||||
assert.equal(publish?.href, undefined, '商品上架先做假菜单:不得挂路由')
|
||||
})
|
||||
|
||||
test('路由器注册京东工作台与采集页', () => {
|
||||
const router = read('src/router/index.ts')
|
||||
assert.ok(router.includes("path: '/jd-console'"), 'router 缺 /jd-console')
|
||||
assert.ok(router.includes('JdConsolePage.vue'), 'router 未指向 JdConsolePage')
|
||||
assert.ok(router.includes("path: '/jd-collect'"), 'router 缺 /jd-collect')
|
||||
})
|
||||
|
||||
test('首页京东入口与亚马逊/视频并列', () => {
|
||||
const home = read('src/pages/home/DesktopHomePage.vue')
|
||||
assert.ok(home.includes('jd-console.html'), '首页京东入口未指向京东工作台')
|
||||
assert.ok(/>京东</.test(home), '首页缺「京东」按钮文字')
|
||||
})
|
||||
|
||||
test('获取店铺数据页是一键触发 + 服务端进度条', () => {
|
||||
const page = read('src/pages/jd/JdCollectTab.vue')
|
||||
assert.ok(page.includes('fetchJdCollectProgress'), '采集页必须轮询服务端进度(不拿本地内存数充进度)')
|
||||
assert.ok(page.includes('progress-track'), '采集页缺进度条轨道')
|
||||
assert.ok(page.includes('jd-progress-indeterminate'),
|
||||
'拿不到 total 时必须走不定态进度条,不得编造百分比')
|
||||
assert.ok(page.includes('一键获取店铺数据'), '主按钮应是一键获取')
|
||||
assert.ok(page.includes('jd-shop-collect-run'), '建单后必须推送本机客户端队列')
|
||||
})
|
||||
@@ -0,0 +1,179 @@
|
||||
/**
|
||||
* 「获取店铺数据」一键采集 + 进度条挂载用例(v1.5)。
|
||||
*
|
||||
* 用户口径原话:「获取店铺就是点击获取后就自动抓已打开的京麦后台的店铺数据导入到后台 需要有进度条」。
|
||||
* 四组靶子,缺一条就是假绿:
|
||||
* ① 点一次按钮就走完整链路:建单 → 把 `jd-shop-collect-run` 推进本机客户端队列;
|
||||
* ② 有 total 时进度条按已落库商品数算出真实百分比(分母只来自服务端游标,不编造);
|
||||
* ③ total 缺失时走不定态动画(没有分母就不许编一个假百分比);
|
||||
* ④ 终态即收手:SUCCESS 落 100% + 导入完成文案,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(),
|
||||
fetchJdCollectProgress: vi.fn(),
|
||||
enqueue: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/shared/api/types/modules/jd-collect', () => ({
|
||||
createJdCollectTask: api.createJdCollectTask,
|
||||
fetchJdCollectProgress: api.fetchJdCollectProgress,
|
||||
}))
|
||||
vi.mock('@/shared/bridges/pywebview', () => ({
|
||||
getPywebviewApi: () => ({ enqueue_json: api.enqueue }),
|
||||
}))
|
||||
|
||||
const POLL_MS = 2000
|
||||
|
||||
let wrapper: VueWrapper | null = null
|
||||
|
||||
function baseProgress(over: Record<string, unknown> = {}) {
|
||||
return {
|
||||
taskId: 88,
|
||||
status: 'RUNNING',
|
||||
itemsReceived: 30,
|
||||
total: 100,
|
||||
page: 2,
|
||||
listTab: 'ONSALE',
|
||||
batches: 1,
|
||||
message: null,
|
||||
updatedAt: null,
|
||||
...over,
|
||||
}
|
||||
}
|
||||
|
||||
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')
|
||||
api.createJdCollectTask.mockReset()
|
||||
api.fetchJdCollectProgress.mockReset()
|
||||
api.enqueue.mockReset()
|
||||
api.createJdCollectTask.mockResolvedValue({ taskId: 88, taskNo: 'JD-1', collectChannel: 'LOCAL' })
|
||||
api.enqueue.mockResolvedValue({ success: true })
|
||||
vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
wrapper?.unmount()
|
||||
wrapper = null
|
||||
vi.useRealTimers()
|
||||
vi.restoreAllMocks()
|
||||
localStorage.clear()
|
||||
})
|
||||
|
||||
describe('获取店铺数据:一键采集 + 进度条', () => {
|
||||
it('点一次按钮就建单并把采集载荷推进本机客户端队列', async () => {
|
||||
api.fetchJdCollectProgress.mockResolvedValue(baseProgress())
|
||||
const w = await mountPage()
|
||||
await startCollect(w)
|
||||
|
||||
expect(api.createJdCollectTask).toHaveBeenCalledTimes(1)
|
||||
// 店铺名留空 → 按「京麦后台」记录(与后端 shop_name 语义一致)
|
||||
expect(api.createJdCollectTask).toHaveBeenCalledWith('京麦后台', ['ONSALE', 'OFFSALE'], 0)
|
||||
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,
|
||||
})
|
||||
})
|
||||
|
||||
it('有 total 时按已落库商品数显示真实百分比与进度元信息', async () => {
|
||||
api.fetchJdCollectProgress.mockResolvedValue(baseProgress())
|
||||
const w = await mountPage()
|
||||
await startCollect(w)
|
||||
|
||||
expect(w.find('.progress-track').exists()).toBe(true)
|
||||
expect(w.find('.progress-percent').text()).toBe('30%')
|
||||
const fill = w.find('.progress-fill')
|
||||
expect(fill.attributes('style')).toContain('width: 30%')
|
||||
expect(fill.classes()).not.toContain('progress-fill--indeterminate')
|
||||
expect(w.find('.progress-meta').text()).toBe('已采集 30 个商品 · 共 100 个 · 第 2 页 · 已回传 1 批')
|
||||
expect(w.find('.progress-status').text()).toBe('正在获取店铺数据…')
|
||||
})
|
||||
|
||||
it('没有分母(total 缺失)时走不定态动画,不编造百分比', async () => {
|
||||
api.fetchJdCollectProgress.mockResolvedValue(baseProgress({ total: null, page: null, batches: 0 }))
|
||||
const w = await mountPage()
|
||||
await startCollect(w)
|
||||
|
||||
expect(w.find('.progress-percent').exists()).toBe(false)
|
||||
expect(w.find('.progress-fill').classes()).toContain('progress-fill--indeterminate')
|
||||
expect(w.find('.progress-meta').text()).toBe('已采集 30 个商品')
|
||||
})
|
||||
|
||||
it('SUCCESS 终态:进度满格并提示已导入后台,且停止轮询', async () => {
|
||||
api.fetchJdCollectProgress.mockResolvedValue(baseProgress({ itemsReceived: 100 }))
|
||||
const w = await mountPage()
|
||||
await startCollect(w)
|
||||
|
||||
api.fetchJdCollectProgress.mockResolvedValue(
|
||||
baseProgress({ status: 'SUCCESS', itemsReceived: 100 }),
|
||||
)
|
||||
await vi.advanceTimersByTimeAsync(POLL_MS)
|
||||
await flushPromises()
|
||||
|
||||
expect(w.find('.progress-percent').text()).toBe('100%')
|
||||
expect(w.find('.progress-status').text()).toBe('获取完成')
|
||||
expect(w.find('.feedback.success').text()).toContain('已导入后台「京东数据库」')
|
||||
const callsAfterTerminal = api.fetchJdCollectProgress.mock.calls.length
|
||||
await vi.advanceTimersByTimeAsync(POLL_MS * 3)
|
||||
await flushPromises()
|
||||
expect(api.fetchJdCollectProgress.mock.calls.length).toBe(callsAfterTerminal)
|
||||
})
|
||||
|
||||
it('FAILED 终态:用服务端原因说明失败,按钮恢复可点', async () => {
|
||||
api.fetchJdCollectProgress.mockResolvedValue(baseProgress())
|
||||
const w = await mountPage()
|
||||
await startCollect(w)
|
||||
|
||||
api.fetchJdCollectProgress.mockResolvedValue(
|
||||
baseProgress({ status: 'FAILED', message: '京麦后台未登录,请先在客户端登录京麦' }),
|
||||
)
|
||||
await vi.advanceTimersByTimeAsync(POLL_MS)
|
||||
await flushPromises()
|
||||
|
||||
expect(w.find('.progress-status').text()).toBe('获取失败')
|
||||
expect(w.find('.feedback.error').text()).toContain('京麦后台未登录')
|
||||
expect(w.find('.collect-btn').attributes('disabled')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('建单失败:只报错不留假进度条,一次进度请求也不发', async () => {
|
||||
api.createJdCollectTask.mockRejectedValue(new Error('缺少登录凭据'))
|
||||
const w = await mountPage()
|
||||
await startCollect(w)
|
||||
|
||||
expect(w.find('.feedback.error').text()).toContain('缺少登录凭据')
|
||||
expect(w.find('.progress-track').exists()).toBe(false)
|
||||
expect(api.fetchJdCollectProgress).not.toHaveBeenCalled()
|
||||
expect(api.enqueue).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* 京东工作台挂载用例(v1.5):首页「京东」进来后是两个菜单。
|
||||
*
|
||||
* 三件事一起判,缺一条就是假绿:
|
||||
* ① 两个菜单名都在 DOM 里(获取店铺数据 / 商品上架);
|
||||
* ② 获取店铺数据是「可用」卡片(有 href → router-link 分支,文案是「打开工具」);
|
||||
* ③ 商品上架是「假菜单」:没有 href,点开只弹一句「暂未开通」,不发任何请求。
|
||||
*
|
||||
* 跑法:cd frontend-vue && npx vitest run(tests/mount/** 由 vitest.config.ts 收)。
|
||||
*/
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { mount, type VueWrapper } from '@vue/test-utils'
|
||||
|
||||
import JdConsolePage from '@/pages/jd/JdConsolePage.vue'
|
||||
|
||||
let wrapper: VueWrapper | null = null
|
||||
|
||||
function mountConsole() {
|
||||
wrapper = mount(JdConsolePage, {
|
||||
global: {
|
||||
stubs: {
|
||||
NotificationBell: true,
|
||||
RouterLink: { template: '<a class="router-link-stub"><slot /></a>' },
|
||||
'router-link': { template: '<a class="router-link-stub"><slot /></a>' },
|
||||
},
|
||||
},
|
||||
})
|
||||
return wrapper
|
||||
}
|
||||
|
||||
describe('京东工作台两个菜单', () => {
|
||||
beforeEach(() => {
|
||||
window.localStorage.setItem('username', 'admin')
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
wrapper?.unmount()
|
||||
wrapper = null
|
||||
window.localStorage.clear()
|
||||
})
|
||||
|
||||
it('两个菜单名都渲染出来', () => {
|
||||
const w = mountConsole()
|
||||
const text = w.text()
|
||||
expect(text).toContain('获取店铺数据')
|
||||
expect(text).toContain('商品上架')
|
||||
expect(w.findAll('.tool-card').length).toBe(2)
|
||||
})
|
||||
|
||||
it('获取店铺数据是可用卡片,商品上架是占位(暂未开通)', () => {
|
||||
const w = mountConsole()
|
||||
const cards = w.findAll('.tool-card')
|
||||
expect(cards[0].classes()).not.toContain('tool-card--pending')
|
||||
expect(cards[0].text()).toContain('打开工具')
|
||||
expect(cards[1].classes()).toContain('tool-card--pending')
|
||||
expect(cards[1].text()).toContain('暂未开通')
|
||||
})
|
||||
|
||||
it('点假菜单只弹提示,不跳转', async () => {
|
||||
const w = mountConsole()
|
||||
const pending = w.findAll('.tool-card')[1]
|
||||
await pending.trigger('click')
|
||||
const toast = w.find('.toast')
|
||||
expect(toast.text()).toContain('商品上架暂未开通')
|
||||
expect(toast.classes()).toContain('show')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user