/** * 「获取店铺数据」一键采集 + 进度条挂载用例(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 = {}) { 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: '' }, 'router-link': { template: '' }, }, }, }) 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() }) })