331 lines
16 KiB
TypeScript
331 lines
16 KiB
TypeScript
/**
|
||
* 外观专利无侵权数据页(v1.3 AC-5)的挂载契约 —— Phase 3 激活。
|
||
*
|
||
* 展示形式(2026-11-11 客户改版,推翻 2026-11-10「参照去重库页自绘 table、不做卡片宫格」的
|
||
* 旧拍板):参照「店铺数据记录」页(ShopDataTasksPage)——form-box 筛选条件 + panel-box
|
||
* 工具条[导出] + 卡片宫格 + `OldPagination.vue` 服务端分页。同日三改(客户「要按主管分组
|
||
* 显示」「价格 asin 无侵权 这些都不需要展示的」「到时候能下载表格看就行」):卡片按所属主管
|
||
* 连续分组(组头=主管名+本页条数,保持行序、不跨页聚合、不按名重排),每卡一条明细:标题
|
||
* (卡头)/任务号/国家/品牌/SKU/同步时间;价格、ASIN、结论徽章(无侵权)、所属主管行不再
|
||
* 上卡——全量明细(含价格/ASIN/结论/主管列)仍走 /export xlsx。筛选面同日二次精简(客户「按账号对应的
|
||
* 主管吧 不需要店铺」):分组(=主管)/国家/起止时间三样,任务号/ASIN 输入框下架(后端
|
||
* §3.2 参数契约与过滤模块逐字对撞护栏保留,UI 不再暴露)。取数/导出/公司 tab 契约不变:
|
||
* `companyScoped: true` 公司 Tab;`page`/`page_size` snake 参数,响应
|
||
* `{items, total, page, pageSize}`。默认仅 super_admin 可见(菜单不预授权,
|
||
* 判据在后端 AdminAppearancePatentCleanItemsControllerTest 的菜单迁移支,这里只管前端接线)。
|
||
*
|
||
* 跑法:`npx vitest run tests/mount/appearance-patent-clean-page.test.ts`
|
||
*/
|
||
import { readFileSync } from 'node:fs'
|
||
import { resolve } from 'node:path'
|
||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
|
||
import { createPinia, setActivePinia, type Pinia } from 'pinia'
|
||
|
||
import AppearancePatentCleanItemsPage from '@/pages/patent/AppearancePatentCleanItemsPage.vue'
|
||
import { adminPages } from '@/router/routes'
|
||
import { COMPANY_SCOPED_PAGES } from '@/config/company-scope'
|
||
import { useAdminSessionStore } from '@/stores/admin-session'
|
||
import { useCompanyTabStore } from '@/stores/company-tab'
|
||
|
||
const PAGE_PATH = 'src/pages/patent/AppearancePatentCleanItemsPage.vue'
|
||
const MENU_KEY = 'admin_appearance_patent_clean_items'
|
||
|
||
const cleanApi = vi.hoisted(() => ({
|
||
fetchAppearancePatentCleanList: vi.fn(),
|
||
exportAppearancePatentCleanList: vi.fn(),
|
||
fetchDedupeTotalGroups: vi.fn(),
|
||
}))
|
||
|
||
vi.mock('@/pages/patent/appearance-patent-clean-api.ts', () => cleanApi)
|
||
|
||
function row(id: number, over: Record<string, unknown> = {}) {
|
||
return {
|
||
id,
|
||
resultId: 900 + id,
|
||
taskId: 33179,
|
||
userId: 7,
|
||
supervisorName: `主管${id}`,
|
||
companyId: 1,
|
||
rowNo: id,
|
||
asin: `B0AB8CDEF${id}`,
|
||
country: 'DE',
|
||
sku: `SKU-${id}`,
|
||
brand: 'Anker',
|
||
price: '29.99',
|
||
title: `无侵权商品 ${id}`,
|
||
imageUrl: '',
|
||
itemUrl: '',
|
||
conclusion: '无侵权',
|
||
titleReason: '',
|
||
appearanceReason: '',
|
||
patentReason: '',
|
||
sourceFilename: 'list.xlsx',
|
||
syncedAt: '2026-11-10T10:30:00',
|
||
...over,
|
||
}
|
||
}
|
||
|
||
let pinia: Pinia
|
||
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
pinia = createPinia()
|
||
setActivePinia(pinia)
|
||
useAdminSessionStore().user = { id: 1, username: 'root', role: 'super_admin' }
|
||
useCompanyTabStore().setActiveId(1)
|
||
// 分组(主管)选项默认空集:分组用例各自覆写
|
||
cleanApi.fetchDedupeTotalGroups.mockResolvedValue([])
|
||
})
|
||
|
||
async function mountWith(payload: unknown): Promise<VueWrapper<Element>> {
|
||
cleanApi.fetchAppearancePatentCleanList.mockImplementation(() => Promise.resolve(payload))
|
||
const wrapper = mount(AppearancePatentCleanItemsPage, { global: { plugins: [pinia] } })
|
||
await flushPromises()
|
||
await flushPromises()
|
||
return wrapper
|
||
}
|
||
|
||
describe('AC-5 外观专利无侵权数据页(卡片宫格,2026-11-11 改版参照店铺数据记录页)', () => {
|
||
it('页面组件落位且是店铺数据记录式卡片宫格 + OldPagination 的分页列表', () => {
|
||
// vitest 的 cwd = admin-frontend-vue 根(Phase 0 预置用 new URL 模板串,被 vite 静态分析成
|
||
// undefined——skip 期间从未执行所以没暴露,激活时改走 cwd 解析)。
|
||
const src = readFileSync(resolve(process.cwd(), PAGE_PATH), 'utf-8')
|
||
expect(src).toContain('OldPagination')
|
||
expect(src).toContain('clean-card-grid', '卡片宫格容器必须就位(2026-11-11 客户改版)')
|
||
expect(src).toContain('clean-card-head', '卡片本体结构必须就位')
|
||
expect(src).toContain('clean-group-head', '主管分组头必须就位(2026-11-11 三改「按主管分组显示」)')
|
||
expect(src).not.toContain('<table', '改版后不再有自绘行列表(推翻 2026-11-10 旧拍板)')
|
||
expect(src).not.toContain('el-card')
|
||
})
|
||
|
||
it('路由与公司 Tab 注册齐备(companyScoped: true)', () => {
|
||
const page = adminPages.find((p) => p.path === 'patent-center/clean-items')
|
||
expect(page).toBeTruthy()
|
||
expect(page?.menuKey).toBe(MENU_KEY)
|
||
expect(page?.title).toBe('外观专利数据库')
|
||
expect(page?.companyScoped).toBe(true)
|
||
const scoped = COMPANY_SCOPED_PAGES.find((p) => p.menuKey === MENU_KEY)
|
||
expect(scoped).toBeTruthy()
|
||
expect(scoped?.pageFiles).toContain(PAGE_PATH)
|
||
})
|
||
|
||
it('挂载后按首屏 snake 分页参数取数并解包 {items,total,page,pageSize}', async () => {
|
||
const wrapper = await mountWith({ items: [row(1), row(2)], total: 42, page: 1, pageSize: 10 })
|
||
|
||
expect(cleanApi.fetchAppearancePatentCleanList).toHaveBeenCalledTimes(1)
|
||
const firstCall = cleanApi.fetchAppearancePatentCleanList.mock.calls[0][0]
|
||
expect(firstCall.page).toBe(1)
|
||
expect(firstCall.pageSize).toBe(10)
|
||
expect(firstCall.companyId).toBe(1)
|
||
expect(wrapper.findAll('.clean-card')).toHaveLength(2, '一卡一条明细')
|
||
expect(wrapper.text()).toContain('共 42 条')
|
||
// 2026-11-11 三改:ASIN/价格/结论徽章不再上卡(全量明细走导出),卡头换成标题
|
||
expect(wrapper.text()).not.toContain('B0AB8CDEF1')
|
||
expect(wrapper.text()).not.toContain('29.99')
|
||
expect(wrapper.find('.st-pill').exists()).toBe(false, '无侵权结论徽章不再上卡')
|
||
const heads = wrapper.findAll('.clean-card-head')
|
||
expect(heads).toHaveLength(2)
|
||
expect(heads[0].text()).toContain('无侵权商品 1', '卡头=标题(替下原 ASIN 卡头)')
|
||
})
|
||
|
||
it('按所属主管分组显示(2026-11-11 三改):组头=主管名+本页条数,卡片不再重复主管行', async () => {
|
||
const wrapper = await mountWith({
|
||
items: [row(1, { supervisorName: '主管甲' }), row(2, { supervisorName: '主管甲' }), row(3, { supervisorName: '主管乙' })],
|
||
total: 3,
|
||
page: 1,
|
||
pageSize: 10,
|
||
})
|
||
const heads = wrapper.findAll('.clean-group-head')
|
||
expect(heads).toHaveLength(2, '连续同主管的行切段成组')
|
||
expect(heads[0].text()).toContain('主管甲')
|
||
expect(heads[0].text()).toContain('本页 2 条')
|
||
expect(heads[1].text()).toContain('主管乙')
|
||
expect(heads[1].text()).toContain('本页 1 条')
|
||
expect(wrapper.findAll('.clean-card')).toHaveLength(3, '组头不占卡片位,三张明细卡都在')
|
||
// 主管行已由组头表达,卡片不再重复;价格行同批下卡
|
||
expect(wrapper.text()).not.toContain('所属主管')
|
||
expect(wrapper.text()).not.toContain('价格')
|
||
})
|
||
|
||
it('分组只在当前页窗口内连续切段:保持服务端行序、不跨页聚合、不按名重排', async () => {
|
||
const wrapper = await mountWith({
|
||
items: [row(1, { supervisorName: '主管甲' }), row(2, { supervisorName: '主管乙' }), row(3, { supervisorName: '主管甲' })],
|
||
total: 3,
|
||
page: 1,
|
||
pageSize: 10,
|
||
})
|
||
const names = wrapper.findAll('.clean-group-name').map((n) => n.text())
|
||
expect(names).toEqual(['主管甲', '主管乙', '主管甲'], '同名主管被隔开时按行序出多个同名组头,忠实反映同步顺序')
|
||
expect(wrapper.findAll('.clean-card')).toHaveLength(3)
|
||
})
|
||
|
||
it('读不到主管名的行不猜:组头展示 -(与旧卡片行同口径)', async () => {
|
||
const wrapper = await mountWith({ items: [row(9, { supervisorName: '' })], total: 1, page: 1, pageSize: 10 })
|
||
const heads = wrapper.findAll('.clean-group-head')
|
||
expect(heads).toHaveLength(1)
|
||
expect(heads[0].text()).toContain('-', '读不到主管展示 -')
|
||
expect(heads[0].text()).toContain('本页 1 条')
|
||
})
|
||
|
||
it('导出按钮带当前过滤面与公司 tab 调导出接口,成功触发 blob 落盘', async () => {
|
||
const createObjectURL = vi.fn(() => 'blob:mock-url')
|
||
const revokeObjectURL = vi.fn()
|
||
const urlRecord = URL as unknown as Record<string, unknown>
|
||
const origCreate = urlRecord.createObjectURL
|
||
const origRevoke = urlRecord.revokeObjectURL
|
||
Object.defineProperty(URL, 'createObjectURL', { value: createObjectURL, configurable: true, writable: true })
|
||
Object.defineProperty(URL, 'revokeObjectURL', { value: revokeObjectURL, configurable: true, writable: true })
|
||
const clickSpy = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
|
||
cleanApi.exportAppearancePatentCleanList.mockImplementation(() =>
|
||
Promise.resolve({ blob: new Blob(['xlsx'], { type: 'application/vnd.ms-excel' }), filename: '外观专利数据库-20261111103000.xlsx' }),
|
||
)
|
||
|
||
try {
|
||
const wrapper = await mountWith({ items: [row(1)], total: 1, page: 1, pageSize: 10 })
|
||
const exportBtn = wrapper.findAll('button').find((b) => b.text() === '导出')
|
||
expect(exportBtn).toBeTruthy()
|
||
await exportBtn!.trigger('click')
|
||
await flushPromises()
|
||
await flushPromises()
|
||
|
||
expect(cleanApi.exportAppearancePatentCleanList).toHaveBeenCalledTimes(1)
|
||
const call = cleanApi.exportAppearancePatentCleanList.mock.calls[0][0]
|
||
expect(call).toMatchObject({
|
||
taskId: '',
|
||
asin: '',
|
||
country: '',
|
||
groupId: '',
|
||
startDate: '',
|
||
endDate: '',
|
||
companyId: 1,
|
||
})
|
||
expect(createObjectURL).toHaveBeenCalledTimes(1)
|
||
expect(revokeObjectURL).toHaveBeenCalledWith('blob:mock-url')
|
||
expect(clickSpy).toHaveBeenCalled()
|
||
expect(document.body.textContent).toContain('已导出', '成功反馈点名落盘文件')
|
||
} finally {
|
||
Object.defineProperty(URL, 'createObjectURL', { value: origCreate, configurable: true })
|
||
Object.defineProperty(URL, 'revokeObjectURL', { value: origRevoke, configurable: true })
|
||
clickSpy.mockRestore()
|
||
}
|
||
})
|
||
|
||
it('导出失败把服务端业务文案弹出来,不许静默', async () => {
|
||
cleanApi.exportAppearancePatentCleanList.mockImplementation(() =>
|
||
Promise.reject(new Error('无侵权数据共 90001 行,超过导出上限 50000 行,请缩小筛选范围后重试')))
|
||
const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 10 })
|
||
await wrapper.findAll('button').find((b) => b.text() === '导出')!.trigger('click')
|
||
await flushPromises()
|
||
await flushPromises()
|
||
expect(document.body.textContent).toContain('超过导出上限', '失败反馈必须是服务端那句可读文案')
|
||
// 失败后导出锁复位,按钮可再次点击
|
||
expect(wrapper.findAll('button').find((b) => b.text() === '导出')!.text()).toBe('导出')
|
||
})
|
||
|
||
it('翻页与改每页条数都重发请求,改条数回第一页', async () => {
|
||
const wrapper = await mountWith({ items: [row(11)], total: 42, page: 2, pageSize: 10 })
|
||
|
||
await wrapper.findAll('button').find((b) => b.text() === '下一页')!.trigger('click')
|
||
await flushPromises()
|
||
expect(cleanApi.fetchAppearancePatentCleanList).toHaveBeenCalledTimes(2)
|
||
expect(cleanApi.fetchAppearancePatentCleanList.mock.calls[1][0].page).toBe(3)
|
||
|
||
const sizeSelect = wrapper.find('.page-size-select')
|
||
await sizeSelect.setValue('50')
|
||
await flushPromises()
|
||
const last = cleanApi.fetchAppearancePatentCleanList.mock.calls.at(-1)![0]
|
||
expect(last.pageSize).toBe(50)
|
||
expect(last.page).toBe(1, '改每页条数后回第一页')
|
||
})
|
||
|
||
it('分组/国家/起止时间筛选生效(任务号/ASIN 输入框已下架,2026-11-11 客户拍板)', async () => {
|
||
cleanApi.fetchDedupeTotalGroups.mockResolvedValue([{ id: 7, name: '甲组', leaderUserId: 9 }])
|
||
const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 10 })
|
||
|
||
// 筛选面只剩分组/国家/时间,页面上不再有任务号/ASIN 文本框(OldPagination 跳页框除外)
|
||
const filterTextInputs = wrapper.findAll('.form-row input[type="text"]')
|
||
expect(filterTextInputs).toHaveLength(0)
|
||
|
||
const selects = wrapper.findAll('select')
|
||
await selects[0].setValue('7') // 分组(主管=组长)排在国家前,对齐获取店铺页字段顺序
|
||
await selects[1].setValue('DE')
|
||
const dateInputs = wrapper.findAll('input[type="date"]')
|
||
await dateInputs[0].setValue('2026-11-01')
|
||
await dateInputs[1].setValue('2026-11-09')
|
||
await wrapper.findAll('button').find((b) => b.text() === '查询')!.trigger('click')
|
||
await flushPromises()
|
||
|
||
const last = cleanApi.fetchAppearancePatentCleanList.mock.calls.at(-1)![0]
|
||
expect(last).toMatchObject({
|
||
taskId: '',
|
||
asin: '',
|
||
country: 'DE',
|
||
groupId: '7',
|
||
startDate: '2026-11-01',
|
||
endDate: '2026-11-09',
|
||
page: 1,
|
||
})
|
||
})
|
||
|
||
it('分组下拉(主管=组长):选项与去重页同源,选中后 groupId 进列表请求', async () => {
|
||
cleanApi.fetchDedupeTotalGroups.mockResolvedValue([
|
||
{ id: 7, name: '甲组', leaderUserId: 9 },
|
||
{ id: 8, name: '乙组', leaderUserId: 10 },
|
||
])
|
||
const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 10 })
|
||
|
||
// 选项取数走的是公司隔离的同一分组集(GET /shop-manages/groups 的适配层)
|
||
expect(cleanApi.fetchDedupeTotalGroups).toHaveBeenCalledWith(1)
|
||
|
||
const groupSelect = wrapper.findAll('select')[0]
|
||
const optionTexts = groupSelect.findAll('option').map((o) => o.text())
|
||
expect(optionTexts).toContain('全部分组')
|
||
expect(optionTexts).toContain('甲组')
|
||
expect(optionTexts).toContain('乙组')
|
||
|
||
await groupSelect.setValue('7')
|
||
await wrapper.findAll('button').find((b) => b.text() === '查询')!.trigger('click')
|
||
await flushPromises()
|
||
const last = cleanApi.fetchAppearancePatentCleanList.mock.calls.at(-1)![0]
|
||
expect(last.groupId).toBe('7', '选中的分组 id 以 groupId 进列表过滤面(服务端解析组长+名下整集)')
|
||
})
|
||
|
||
it('空态与加载失败条(失败给重试,不把整页打死)', async () => {
|
||
const empty = await mountWith({ items: [], total: 0, page: 1, pageSize: 10 })
|
||
const tip = empty.find('.empty-tip')
|
||
expect(tip.exists()).toBe(true)
|
||
expect(tip.text()).toContain('暂无无侵权数据')
|
||
empty.unmount()
|
||
|
||
cleanApi.fetchAppearancePatentCleanList.mockRejectedValue(new Error('上游 502'))
|
||
const failed = mount(AppearancePatentCleanItemsPage, { global: { plugins: [pinia] } })
|
||
await flushPromises()
|
||
await flushPromises()
|
||
expect(failed.find('.load-failure-bar').exists()).toBe(true)
|
||
expect(failed.find('.load-failure-bar').text()).toContain('无侵权数据加载失败')
|
||
})
|
||
|
||
it('切公司 Tab 重取并清掉上一家的筛选(company-tab-wiring 同口径)', async () => {
|
||
cleanApi.fetchDedupeTotalGroups.mockResolvedValue([{ id: 7, name: '甲组', leaderUserId: 9 }])
|
||
const wrapper = await mountWith({ items: [row(1)], total: 1, page: 1, pageSize: 10 })
|
||
// 选上上一家公司的分组:切走后必须连同 groupId 一起清(分组的成员集是公司隔离的)
|
||
const groupSelect = wrapper.findAll('select')[0]
|
||
await groupSelect.setValue('7')
|
||
const callsAfterMount = cleanApi.fetchAppearancePatentCleanList.mock.calls.length
|
||
|
||
useCompanyTabStore().setActiveId(2)
|
||
await flushPromises()
|
||
await flushPromises()
|
||
|
||
expect(cleanApi.fetchAppearancePatentCleanList.mock.calls.length).toBeGreaterThan(callsAfterMount)
|
||
const last = cleanApi.fetchAppearancePatentCleanList.mock.calls.at(-1)![0]
|
||
expect(last.companyId).toBe(2)
|
||
expect(last.groupId).toBe('', '切公司必须清掉上一家选的分组')
|
||
expect(last.taskId).toBe('', 'watch token 仍整把清栏(taskId/asin 字段留在契约里,UI 不再暴露)')
|
||
expect(last.page).toBe(1)
|
||
// 分组选项按新公司重取(选项集公司隔离)
|
||
expect(cleanApi.fetchDedupeTotalGroups).toHaveBeenLastCalledWith(2)
|
||
})
|
||
})
|