feat(phase-5): 后管京东数据库接口与页面+用户端采集入口——v1.4 Phase 5
This commit is contained in:
@@ -15,6 +15,8 @@ const BRAND_GATING_MIGRATION = join(process.cwd(), '..', 'backend-java', 'src',
|
||||
'V174__restore_admin_brand_gating_menu.sql')
|
||||
const KEYWORD_USAGE_MIGRATION = join(process.cwd(), '..', 'backend-java', 'src', 'main', 'resources', 'db',
|
||||
'V175__move_gating_and_add_keyword_usage_menu.sql')
|
||||
const JD_DATABASE_MIGRATION = join(process.cwd(), '..', 'backend-java', 'src', 'main', 'resources', 'db',
|
||||
'V196__jd_database_menu.sql')
|
||||
|
||||
interface MigrationMenu {
|
||||
name: string
|
||||
@@ -95,3 +97,24 @@ test('V175 新增的关键词使用记录菜单 key 必须在路由注册表登
|
||||
assert.ok(sql.includes('SET col.`parent_id` = grp.`id`'), 'V175 必须把品牌Gating库迁到店铺管理组')
|
||||
assert.ok(sql.includes('col.`sort_order` = 85'), '品牌Gating库排序应为 85')
|
||||
})
|
||||
|
||||
test('V196 新增的京东数据库菜单 key 必须在路由注册表登记', () => {
|
||||
const sql = readFileSync(JD_DATABASE_MIGRATION, 'utf8')
|
||||
const menus = menusInsertedBy(sql)
|
||||
const insertCount = (sql.match(/INSERT INTO `columns`/g) || []).length
|
||||
assert.ok(menus.length > 0, 'V196 应当插入后台菜单,一条都没解析出来说明正则失效')
|
||||
assert.equal(menus.length, insertCount, `解析到 ${menus.length} 条,但迁移有 ${insertCount} 条 INSERT INTO columns`)
|
||||
|
||||
const menu = menus.find((item) => item.columnKey === 'admin_jd_database')
|
||||
assert.ok(menu, 'V196 必须插入 admin_jd_database 菜单')
|
||||
const page = adminPages.find((item) => item.menuKey === 'admin_jd_database')
|
||||
assert.ok(page, 'routes.ts 必须有 admin_jd_database 页面,否则菜单点进去 404')
|
||||
assert.equal(page.path, menu.routePath, '库里的 route_path 应与注册表路径一致')
|
||||
assert.equal(page.title, menu.name, '页面标题应与菜单名一致')
|
||||
assert.equal(page.companyScoped, true, '京东数据库按公司 tab 取数')
|
||||
|
||||
// 每公司一套树(公司 1 模板行 + 其余公司复制),挂数据管理组、排序 68(V190 外观专利占 67)。
|
||||
assert.ok(sql.includes("grp.`column_key` = 'admin_group_data'"), 'V196 必须把京东数据库挂到数据管理组')
|
||||
assert.ok(/'京东数据库', 'admin_jd_database', 'admin', 'jd-center\/database', 68/.test(sql), '京东数据库排序应为 68')
|
||||
assert.ok(!sql.includes('user_column_permission'), 'V196 不得预插授权行(默认仅超管可见)')
|
||||
})
|
||||
|
||||
@@ -132,6 +132,11 @@ const SEND_POINTS: ReadonlyArray<SendPoint> = [
|
||||
// 500 够得着自己、够不到隔壁(对平用例逐处从真文件重算,注释过期即红)。
|
||||
{ menuKey: 'admin_appearance_patent_clean_items', file: 'src/pages/patent/appearance-patent-clean-api.ts', fn: 'fetchAppearancePatentCleanList', line: 'if (params.companyId != null) query.company_id = params.companyId', window: 500, title: 'GET /appearance-patent-clean-items 无侵权只读列表' },
|
||||
{ menuKey: 'admin_appearance_patent_clean_items', file: 'src/pages/patent/appearance-patent-clean-api.ts', fn: 'exportAppearancePatentCleanList', line: 'if (params.companyId != null) query.company_id = params.companyId', window: 500, title: 'GET /appearance-patent-clean-items/export 导出(2026-11-11 客户需求「数据得导出来」,与列表共用同一过滤面)' },
|
||||
// ↓ 批次 8(2026-11-14 v1.4 京东数据库):京麦店铺商品采集落库的只读镜像,归属物化在行
|
||||
// company_id 列(V193,同外观专利 V189 形状)⇒ 后端正门 readCompanyIdOrOwn。列表与导出
|
||||
// 共用同一过滤面,塞键行与外观专利逐字同行,但分属不同文件 ⇒ (文件, 整行) 各成一组。
|
||||
{ menuKey: 'admin_jd_database', file: 'src/pages/jd/jd-product-api.ts', fn: 'fetchJdProductList', line: 'if (params.companyId != null) query.company_id = params.companyId', window: 500, title: 'GET /jd-products 京东数据库只读列表' },
|
||||
{ menuKey: 'admin_jd_database', file: 'src/pages/jd/jd-product-api.ts', fn: 'exportJdProductList', line: 'if (params.companyId != null) query.company_id = params.companyId', window: 500, title: 'GET /jd-products/export 京东数据库导出(与列表共用同一过滤面)' },
|
||||
]
|
||||
|
||||
/** 任务看板那 5 处(1b ④ 那条用例按文件筛它们,沿用旧名字不漂;表只有一份,不会漂出两个口径)。 */
|
||||
@@ -255,6 +260,8 @@ const COMPANY_ID_SOURCE_PINS: Readonly<Record<string, readonly CompanyIdPin[]>>
|
||||
admin_columns: pinsFor('admin_columns'),
|
||||
// 批次 7(v1.3 无侵权只读页):同样由 SEND_POINTS 现算——只有一个发送口(列表)。
|
||||
admin_appearance_patent_clean_items: pinsFor('admin_appearance_patent_clean_items'),
|
||||
// 批次 8(v1.4 京东数据库):列表 + 导出两口,由 SEND_POINTS 现算。
|
||||
admin_jd_database: pinsFor('admin_jd_database'),
|
||||
}
|
||||
|
||||
/** 严格判据的分母由本轮实扫得出(修完不许恒绿也不许恒红):求值了几条 shape、其中几条通过。 */
|
||||
@@ -353,8 +360,8 @@ test('正向:声明 companyScoped 的页,其 api 文件必须真的带 company
|
||||
// ⚠ 分母是本轮实扫的表条数(批次 1 的 5 + 批次 2 的 7 + 批次 3 的 4 + 批次 4 的 1 + 批次 5 的 1 + 批次 6 的 3;
|
||||
// 批次 2 原 12 条里的品牌库 1 条 + 商品类目 4 条随 2026-10-06「这两页退回全平台共享」摘掉)。
|
||||
// 这条只防"整表被摘空/被改窄",逐处脱管由下面两条(对平闸门 + 窗口对平)负责。
|
||||
assert.ok(SEND_POINTS.length >= 21,
|
||||
`发送口表只剩 ${SEND_POINTS.length} 条(批次 1+2+3+4+5+6 实扫 21 条 + 批次 7 v1.3 无侵权列表/导出 2 条 + 主管聚合 1 条 + 按主管删除 1 条 = 26 条)⇒ 有人把钉从表里摘了,取值口看守成空`)
|
||||
assert.ok(SEND_POINTS.length >= 27,
|
||||
`发送口表只剩 ${SEND_POINTS.length} 条(批次 1+2+3+4+5+6 实扫 21 条 + 批次 7 v1.3 无侵权列表/导出 2 条 + 主管聚合 1 条 + 按主管删除 1 条 + 批次 8 v1.4 京东数据库列表/导出 2 条 = 28 条)⇒ 有人把钉从表里摘了,取值口看守成空`)
|
||||
for (const p of SEND_POINTS) {
|
||||
const shape = pointShape(p)
|
||||
const compliant = pointCompliant(p)
|
||||
@@ -386,8 +393,8 @@ test('正向:声明 companyScoped 的页,其 api 文件必须真的带 company
|
||||
if (hit) hit.pinned += 1
|
||||
else grouped.set(key, { file: p.file, line: p.line, pinned: 1 })
|
||||
}
|
||||
assert.equal(grouped.size, 19,
|
||||
`发送口表按 (文件, 整行) 应聚成 19 组(26 处发送口,批次 7 的列表/导出/主管聚合/按主管删除逐字同行走同一组),现在却有 ${grouped.size} 组 —— 表被改形状了,对平判据失去分母`)
|
||||
assert.equal(grouped.size, 20,
|
||||
`发送口表按 (文件, 整行) 应聚成 20 组(28 处发送口,批次 7 的列表/导出/主管聚合/按主管删除与批次 8 的京东列表/导出逐字同行各走同一组),现在却有 ${grouped.size} 组 —— 表被改形状了,对平判据失去分母`)
|
||||
for (const g of grouped.values()) {
|
||||
const actual = occurrences(stripComments(read(g.file)), g.line)
|
||||
assert.equal(g.pinned, actual,
|
||||
@@ -743,6 +750,19 @@ const WATCH_BODY_PINS: ReadonlyArray<{
|
||||
{ file: 'src/pages/patent/appearance-patent-clean-api.ts', shapes: [/company_id\s*=\s*params\.companyId\b/] },
|
||||
],
|
||||
},
|
||||
// ↓ 批次 8(2026-11-14 v1.4 京东数据库):店铺/商品编码/标题是按行集筛的自由文本,分组选项集是
|
||||
// 公司隔离的(/jd-product-groups 带 company_id)⇒ 上一家的筛选值与 groupId 在新一家的行集/选项集
|
||||
// 里不存在,切 tab 必清且选项重取;页码回 1 同外观专利页。
|
||||
{
|
||||
file: 'src/pages/jd/JdDatabasePage.vue',
|
||||
token: "filter.shopName = ''; filter.productCode = ''; filter.titleKeyword = ''; filter.saleStatus = ''; filter.groupId = ''; filter.syncedFrom = ''; filter.syncedTo = ''; page.value = 1; void loadGroupOptions(); void load()",
|
||||
why: '店铺/商品编码/标题是按行集筛的自由文本,上一家的值在新一家的行里不存在 ⇒ 切 tab 后首屏被筛成「暂无商品」,'
|
||||
+ '而输入框还显示着上一家填的值;分组选项集是公司隔离的,上一家选的 groupId 在新一家的选项里不存在,'
|
||||
+ '同罪必清且选项重取;页码回 1 是因为新一家的行数更少,末尾的 void load() 才让切 tab 真的换数据',
|
||||
premise: [
|
||||
{ file: 'src/pages/jd/jd-product-api.ts', shapes: [/company_id\s*=\s*params\.companyId\b/] },
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
/** 取「`watch(() => companyTab.activeId, () => { … })`」这一条语句的原文;匹配不出返回 null(由调用方判探针失效)。 */
|
||||
@@ -751,9 +771,9 @@ function watchStatement(source: string): string | null {
|
||||
return m ? m[0] : null
|
||||
}
|
||||
|
||||
test('切公司的 watch 体:每对 (file, token) 都要在该页 watch 体内出现(批次 1 两页 + 批次 2 三页 + 批次 3 两页 + 批次 4 一页 + 批次 5 一页 + 批次 6 一页 + 批次 7 一页)', () => {
|
||||
assert.ok(WATCH_BODY_PINS.length >= 10,
|
||||
`watch 体看守只剩 ${WATCH_BODY_PINS.length} 对(批次 1 的店铺管理/任务看板 + 批次 2 的去重库/查询ASIN/最低价ASIN + 批次 3 的撞款监控/店铺数据记录 + 批次 4 的用户管理 + 批次 5 的店铺密钥 + 批次 6 的菜单管理 + 批次 7 v1.3 无侵权只读页共 11 对)`
|
||||
test('切公司的 watch 体:每对 (file, token) 都要在该页 watch 体内出现(批次 1 两页 + 批次 2 三页 + 批次 3 两页 + 批次 4 一页 + 批次 5 一页 + 批次 6 一页 + 批次 7 一页 + 批次 8 一页)', () => {
|
||||
assert.ok(WATCH_BODY_PINS.length >= 11,
|
||||
`watch 体看守只剩 ${WATCH_BODY_PINS.length} 对(批次 1 的店铺管理/任务看板 + 批次 2 的去重库/查询ASIN/最低价ASIN + 批次 3 的撞款监控/店铺数据记录 + 批次 4 的用户管理 + 批次 5 的店铺密钥 + 批次 6 的菜单管理 + 批次 7 v1.3 无侵权只读页 + 批次 8 v1.4 京东数据库页共 12 对)`
|
||||
+ '⇒ 这一族又退回"只钉少数几页",被摘掉那一页的"切公司不清筛选项"再次没人看守')
|
||||
const pinnedFiles = new Set(WATCH_BODY_PINS.map((pin) => pin.file))
|
||||
for (const pin of WATCH_BODY_PINS) {
|
||||
|
||||
@@ -37,6 +37,9 @@ const EXEMPT = new Set([
|
||||
// 2026-11-10(v1.3):外观专利无侵权数据页与去重库同形状——出表成功自动同步的累计台账,
|
||||
// 默认当天会让首屏只剩今天同步的行,历史同步行整批"消失"。
|
||||
'src/pages/patent/AppearancePatentCleanItemsPage.vue',
|
||||
// 2026-11-14(v1.4 Phase 5):京东数据库页与外观专利页同形状——京东商品同步落库的累计台账,
|
||||
// 默认当天会让首屏只剩今天同步的行(页面注释与 filter 工厂同口径留空)。
|
||||
'src/pages/jd/JdDatabasePage.vue',
|
||||
// 2026-10-05(V146):两处是「到期时间」表单字段而非列表筛选——留空 = 永不过期,
|
||||
// 是有业务语义的默认值,绝不能默认当天(默认当天等于建号即过期)。
|
||||
'src/pages/account/CreateUserDialog.vue',
|
||||
|
||||
@@ -231,11 +231,11 @@ test('orderedPair 只在两端都有值且倒挂时交换', () => {
|
||||
})
|
||||
|
||||
test('分母硬钉:四面控件实例数与成对数(扫描失明即红)', () => {
|
||||
assert.equal(adminCtrls.length, 21, '后管实例数变了,先复算普查再改这里(v1.3 外观专利无侵权数据页再落一对起止=2 实例)')
|
||||
assert.equal(adminCtrls.length, 23, '后管实例数变了,先复算普查再改这里(v1.4 京东数据库页同步时间起止再落一对=2 实例)')
|
||||
assert.equal(userCtrls.length, 2, '用户端实例数变了')
|
||||
assert.equal(finCtrls.length, 30, '财务系统实例数变了')
|
||||
assert.equal(mxCtrls.length, 4, '麦象实例数变了')
|
||||
assert.equal(pairCount(adminCtrls), 9, '后管成对区间应为 9(v1.3 外观专利无侵权数据页再落一对)')
|
||||
assert.equal(pairCount(adminCtrls), 10, '后管成对区间应为 10(v1.4 京东数据库页同步时间起止再落一对)')
|
||||
assert.equal(pairCount(finCtrls), 11, '财务系统成对区间应为 11')
|
||||
assert.equal(pairCount(mxCtrls), 2, '麦象成对区间应为 2')
|
||||
assert.equal(pairCount(userCtrls), 0, '用户端出现成对区间说明新加了两个单只控件')
|
||||
|
||||
@@ -15,7 +15,7 @@ import { adminPages } from '../src/router/routes.ts'
|
||||
const MENU_KEY = 'admin_jd_database'
|
||||
const ROUTE = 'jd-center/database'
|
||||
|
||||
test('routes 注册表出现京东数据库页面且公司隔离', { skip: 'Pending Phase 5: 京东数据库页面落地后激活' }, () => {
|
||||
test('routes 注册表出现京东数据库页面且公司隔离', () => {
|
||||
const page = adminPages.find((item) => item.menuKey === MENU_KEY)
|
||||
assert.ok(page, `routes.ts 缺 menuKey=${MENU_KEY} 的注册行`)
|
||||
assert.equal(page.path, ROUTE, 'route_path 必须与 V196 菜单一侧一致')
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
/**
|
||||
* 京东数据库页纯逻辑契约(v1.4 Phase 5)—— node --test 直跑(纯函数零依赖)。
|
||||
*
|
||||
* 不用 readFileSync/readSource/globSync 读点:source-pin-ratchet 对台账外新文件带读点即红,
|
||||
* 本文件只 import 被测纯函数。
|
||||
*
|
||||
* 覆盖:
|
||||
* 1. **snake 参数形状**:toJdProductListParams 产出 `page`/`page_size`(默认 20、上限 100)、
|
||||
* `shop_name`/`product_code`/`title_keyword`/`sale_status`/`group_id`/`synced_from`/`synced_to`
|
||||
* (ISO yyyy-MM-dd;空白不下发;groupId 空串→不带、有值→snake group_id);分页钳制(铁律 15)。
|
||||
* 2. **详情解析**:parseJdProductDetail 解析 attrGroups/images(camel 为正 snake 容错、缺 url 弃行),
|
||||
* attrGroups 解析不出时 attrJsonRaw 原文兜底;列表行解析缺 id 弃行。
|
||||
*
|
||||
* 跑法:`node --test tests/jd-product-group.test.ts`
|
||||
*/
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import {
|
||||
JD_PRODUCT_MAX_PAGE,
|
||||
createJdProductFilterState,
|
||||
toJdProductExportParams,
|
||||
toJdProductListParams,
|
||||
} from '../src/pages/jd/jd-product-filter.ts'
|
||||
import {
|
||||
jdPriceRange,
|
||||
parseJdProductDetail,
|
||||
parseJdProductPage,
|
||||
toJdProductItem,
|
||||
} from '../src/pages/jd/jd-product-model.ts'
|
||||
|
||||
test('京东列表参数——snake 形状:groupId 空→不带、有值→group_id,日期空白/非法不下发', () => {
|
||||
const full = toJdProductListParams(
|
||||
{
|
||||
shopName: ' 旗舰自营店 ',
|
||||
productCode: ' JD-10086 ',
|
||||
titleKeyword: ' 保温杯 ',
|
||||
saleStatus: '上架中',
|
||||
groupId: ' 7 ',
|
||||
syncedFrom: '2026-11-01',
|
||||
syncedTo: '2026-11-09',
|
||||
},
|
||||
2,
|
||||
20,
|
||||
)
|
||||
assert.deepEqual(full, {
|
||||
page: 2,
|
||||
page_size: 20,
|
||||
shop_name: '旗舰自营店',
|
||||
product_code: 'JD-10086',
|
||||
title_keyword: '保温杯',
|
||||
sale_status: '上架中',
|
||||
group_id: 7,
|
||||
synced_from: '2026-11-01',
|
||||
synced_to: '2026-11-09',
|
||||
})
|
||||
|
||||
// 全空态只有分页两项;脏值(非数字分组、非法日期)同罪不下发。
|
||||
const blank = toJdProductListParams(createJdProductFilterState(), 1, 20)
|
||||
assert.deepEqual(blank, { page: 1, page_size: 20 })
|
||||
assert.equal('group_id' in blank, false, 'groupId 空串不带 group_id')
|
||||
const dirty = toJdProductListParams(
|
||||
{ shopName: ' ', productCode: '', titleKeyword: '', saleStatus: '', groupId: 'abc', syncedFrom: '2026/11/01', syncedTo: 'not-a-date' },
|
||||
1,
|
||||
20,
|
||||
)
|
||||
assert.deepEqual(dirty, { page: 1, page_size: 20 }, '脏值不下发')
|
||||
|
||||
// 导出与列表同一过滤面单出口(无分页)。
|
||||
const state = { shopName: '旗舰自营店', productCode: '', titleKeyword: '', saleStatus: '已下架', groupId: '7', syncedFrom: '2026-11-01', syncedTo: '' }
|
||||
const list = toJdProductListParams(state, 3, 20)
|
||||
const exp = toJdProductExportParams(state)
|
||||
for (const key of Object.keys(exp) as Array<keyof typeof exp>) {
|
||||
assert.equal((list as Record<string, unknown>)[key], exp[key], `导出与列表的 ${key} 必须同源`)
|
||||
}
|
||||
assert.equal('page' in exp, false, '导出参数无分页')
|
||||
assert.deepEqual(exp, { shop_name: '旗舰自营店', sale_status: '已下架', group_id: 7, synced_from: '2026-11-01' })
|
||||
})
|
||||
|
||||
test('京东列表参数——page 钳制防 (page-1)*page_size 溢出(铁律 15)', () => {
|
||||
const low = toJdProductListParams(createJdProductFilterState(), 0, 0)
|
||||
assert.equal(low.page, 1, 'page<1 归 1')
|
||||
assert.equal(low.page_size, 20, 'page_size 非法回默认 20')
|
||||
const big = toJdProductListParams(createJdProductFilterState(), 3, 500)
|
||||
assert.equal(big.page_size, 100, 'page_size 上限 100')
|
||||
|
||||
const extreme = toJdProductListParams(createJdProductFilterState(), Number.MAX_SAFE_INTEGER, 100)
|
||||
assert.equal(extreme.page, JD_PRODUCT_MAX_PAGE)
|
||||
const offset = (extreme.page - 1) * extreme.page_size
|
||||
assert.equal(Number.isSafeInteger(offset), true, `(page-1)*page_size=${offset} 必须留在安全整数域`)
|
||||
assert.equal(offset, 999_900)
|
||||
})
|
||||
|
||||
test('详情解析——attrGroups/images 结构化解析(snake 容错、缺 url 弃行)', () => {
|
||||
const detail = parseJdProductDetail({
|
||||
code: 0,
|
||||
message: 'ok',
|
||||
data: {
|
||||
id: 42,
|
||||
title: '304不锈钢保温杯',
|
||||
sale_status: '上架中',
|
||||
attrGroups: [
|
||||
{
|
||||
groupName: '规格',
|
||||
attrs: [
|
||||
{ key: 'capacity', label: '容量', value: '500ml' },
|
||||
{ key: 'material', label: '', value: '304不锈钢' },
|
||||
],
|
||||
},
|
||||
{ group_name: '其他', attrs: [{ key: 'color', label: '颜色', value: '白色' }] },
|
||||
],
|
||||
images: [
|
||||
{ imageType: 'MAIN', attrName: '', sortNo: 1, url: 'https://img.example/main.jpg', width: 800, height: 800 },
|
||||
{ image_type: 'ATTR', attrName: '白色', sortNo: 2, url: 'https://img.example/white.jpg' },
|
||||
{ imageType: 'ATTR', attrName: '', sortNo: 3, url: 'https://img.example/attr2.jpg' },
|
||||
{ imageType: 'DETAIL', sortNo: 4, url: '' }, // 缺 url 弃行
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
assert.equal(detail.item.id, 42)
|
||||
assert.equal(detail.item.title, '304不锈钢保温杯')
|
||||
assert.equal(detail.item.saleStatus, '上架中', 'snake 容错')
|
||||
assert.equal(detail.attrGroups.length, 2, '两个属性块')
|
||||
assert.equal(detail.attrGroups[0].groupName, '规格')
|
||||
assert.equal(detail.attrGroups[0].attrs[0].label, '容量')
|
||||
assert.equal(detail.attrGroups[1].groupName, '其他', 'group_name snake 容错')
|
||||
assert.equal(detail.images.length, 3, '缺 url 的 DETAIL 图弃行')
|
||||
assert.equal(detail.images[1].imageType, 'ATTR', 'image_type snake 容错')
|
||||
assert.equal(detail.images[1].attrName, '白色')
|
||||
assert.equal(detail.images[1].sortNo, 2)
|
||||
assert.equal(detail.images[2].attrName, '', 'attrName 缺省空串')
|
||||
assert.equal(detail.attrJsonRaw, '', '结构化解析成功时不需要原文兜底')
|
||||
})
|
||||
|
||||
test('详情解析——attrGroups 缺失时 attrJsonRaw 原文兜底;信封外裸负载同口径', () => {
|
||||
const raw = '{"规格":{"容量":"500ml"}}'
|
||||
const detail = parseJdProductDetail({ id: 7, attrJsonRaw: raw })
|
||||
assert.equal(detail.item.id, 7)
|
||||
assert.deepEqual(detail.attrGroups, [], 'attrGroups 解析不出为空')
|
||||
assert.equal(detail.attrJsonRaw, raw, '原文兜底字段原样透传(界面展示,不静默吞)')
|
||||
assert.deepEqual(detail.images, [])
|
||||
|
||||
// 非法负载不抛:整包垃圾回空详情(id 0),attrJsonRaw 留空。
|
||||
const junk = parseJdProductDetail('not-an-object')
|
||||
assert.equal(junk.item.id, 0)
|
||||
assert.equal(junk.attrJsonRaw, '')
|
||||
})
|
||||
|
||||
test('列表行解析——camel 为正 snake 容错、缺 id 弃行、价格区间拼装', () => {
|
||||
const page = parseJdProductPage({
|
||||
success: true,
|
||||
data: {
|
||||
items: [
|
||||
{
|
||||
id: 1,
|
||||
shopId: 3,
|
||||
shopName: '旗舰自营店',
|
||||
productCode: 'JD-10086',
|
||||
priceMin: '12.90',
|
||||
priceMax: '15.90',
|
||||
stockTotal: 500,
|
||||
saleStatus: '上架中',
|
||||
groupId: 7,
|
||||
groupName: '甲组',
|
||||
syncedAt: '2026-11-12 09:00:00',
|
||||
},
|
||||
{ id: 'not-a-number', title: '缺 id 弃行' },
|
||||
{ shop_name: 'snake 行也缺 id,弃' },
|
||||
],
|
||||
total: 1,
|
||||
page: 1,
|
||||
page_size: 20,
|
||||
},
|
||||
})
|
||||
assert.equal(page.items.length, 1, '三条原始行里两条缺有效 id,只留 1 行')
|
||||
const row = page.items[0]
|
||||
assert.equal(row.shopName, '旗舰自营店')
|
||||
assert.equal(row.groupId, 7)
|
||||
assert.equal(row.groupName, '甲组')
|
||||
assert.equal(jdPriceRange(row), '12.90 ~ 15.90', 'min/max 都有给区间')
|
||||
assert.equal(jdPriceRange({ priceMin: '12.90', priceMax: '' }), '12.90', '只空一侧给单值')
|
||||
assert.equal(jdPriceRange({ priceMin: '', priceMax: '' }), '', '全空为空串(界面展示 -)')
|
||||
|
||||
// snake 行同口径 + 缺省回默认。
|
||||
const snakePage = parseJdProductPage({ data: { items: [{ id: 9, shop_name: 'B店', main_image_url: 'https://img/x.jpg' }], total: 1, page: 2, page_size: 5 } })
|
||||
const snakeRow = toJdProductItem({ id: 9, shop_name: 'B店', main_image_url: 'https://img/x.jpg' })
|
||||
assert.equal(snakeRow?.mainImageUrl, 'https://img/x.jpg')
|
||||
assert.equal(snakePage.pageSize, 5)
|
||||
})
|
||||
@@ -0,0 +1,323 @@
|
||||
/**
|
||||
* 京东数据库页(v1.4 Phase 5)的挂载契约。
|
||||
*
|
||||
* 形态:panel-box + 筛选行(店铺/商品编码/标题关键字/商品状态/分组/同步起止 date 对)+
|
||||
* 查询/导出/重置/管理分组 + 自绘明细表(点行开详情抽屉)+ OldPagination;
|
||||
* 公司 tab 切换=清筛选重取(company-tab-wiring 同口径)。
|
||||
* 不用 readFileSync(source-pin-ratchet 对台账外新文件带读点即红),全部走真实挂载断言。
|
||||
*
|
||||
* 跑法:`npx vitest run tests/mount/jd-database-page.mount.test.ts`
|
||||
*/
|
||||
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 JdDatabasePage from '@/pages/jd/JdDatabasePage.vue'
|
||||
import JdProductGroupDialog from '@/pages/jd/JdProductGroupDialog.vue'
|
||||
import { useAdminSessionStore } from '@/stores/admin-session'
|
||||
import { useCompanyTabStore } from '@/stores/company-tab'
|
||||
|
||||
const jdApi = vi.hoisted(() => ({
|
||||
JD_PRODUCT_ENDPOINT: '/api/admin/jd-products',
|
||||
JD_PRODUCT_GROUP_ENDPOINT: '/api/admin/jd-product-groups',
|
||||
fetchJdProductList: vi.fn(),
|
||||
fetchJdProductDetail: vi.fn(),
|
||||
exportJdProductList: vi.fn(),
|
||||
fetchJdProductGroups: vi.fn(),
|
||||
createJdProductGroup: vi.fn(),
|
||||
updateJdProductGroup: vi.fn(),
|
||||
deleteJdProductGroup: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/pages/jd/jd-product-api.ts', () => jdApi)
|
||||
|
||||
function item(id: number, over: Record<string, unknown> = {}) {
|
||||
return {
|
||||
id,
|
||||
shopId: 3,
|
||||
shopName: '旗舰自营店',
|
||||
productCode: `JD-100${id}`,
|
||||
skuId: `SKU-${id}`,
|
||||
title: '304不锈钢保温杯大容量便携',
|
||||
brand: '锅宝',
|
||||
categoryPath: '家居 > 厨具 > 保温杯',
|
||||
priceMin: '12.90',
|
||||
priceMax: '15.90',
|
||||
stockTotal: 500,
|
||||
saleStatus: '上架中',
|
||||
listTab: 'search',
|
||||
jdUpdatedAt: '2026-11-10 08:30:00',
|
||||
mainImageUrl: `https://img.example/main-${id}.jpg`,
|
||||
attrCount: 6,
|
||||
imageCount: 9,
|
||||
groupId: 7,
|
||||
groupName: '甲组',
|
||||
syncedAt: '2026-11-12 09:00:00',
|
||||
...over,
|
||||
}
|
||||
}
|
||||
|
||||
let pinia: Pinia
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
pinia = createPinia()
|
||||
setActivePinia(pinia)
|
||||
useAdminSessionStore().user = { id: 1, username: 'root', role: 'super_admin' }
|
||||
useCompanyTabStore().setActiveId(1)
|
||||
jdApi.fetchJdProductGroups.mockResolvedValue([])
|
||||
})
|
||||
|
||||
async function mountWith(payload: unknown): Promise<VueWrapper<Element>> {
|
||||
jdApi.fetchJdProductList.mockImplementation(() => Promise.resolve(payload))
|
||||
const wrapper = mount(JdDatabasePage, { global: { plugins: [pinia] } })
|
||||
await flushPromises()
|
||||
await flushPromises()
|
||||
return wrapper
|
||||
}
|
||||
|
||||
describe('京东数据库页(v1.4 Phase 5)', () => {
|
||||
it('挂载后按首屏分页参数取列表(pageSize 默认 20、带公司 tab),行数=本页条数', async () => {
|
||||
const wrapper = await mountWith({ items: [item(1), item(2), item(3)], total: 42, page: 1, pageSize: 20 })
|
||||
expect(jdApi.fetchJdProductList).toHaveBeenCalledTimes(1)
|
||||
const firstCall = jdApi.fetchJdProductList.mock.calls[0][0]
|
||||
expect(firstCall.page).toBe(1)
|
||||
expect(firstCall.pageSize).toBe(20)
|
||||
expect(firstCall.companyId).toBe(1)
|
||||
expect(wrapper.findAll('tbody tr')).toHaveLength(3)
|
||||
expect(wrapper.find('.jd-summary').text()).toContain('共 42 条京东商品')
|
||||
})
|
||||
|
||||
it('行内容=主图/店铺/商品编码/标题/品牌/价格区间/库存/状态/分组/京东更新时间/同步时间(11 列)', async () => {
|
||||
const wrapper = await mountWith({ items: [item(1)], total: 1, page: 1, pageSize: 20 })
|
||||
const cells = wrapper.findAll('tbody tr')[0].findAll('td')
|
||||
expect(cells).toHaveLength(11)
|
||||
expect(cells[1].text()).toBe('旗舰自营店')
|
||||
expect(cells[2].text()).toBe('JD-1001')
|
||||
expect(cells[3].text()).toContain('304不锈钢保温杯')
|
||||
expect(cells[4].text()).toBe('锅宝')
|
||||
expect(cells[5].text()).toBe('12.90 ~ 15.90', '价格区间=min ~ max')
|
||||
expect(cells[6].text()).toBe('500')
|
||||
expect(cells[7].text()).toBe('上架中')
|
||||
expect(cells[8].text()).toBe('甲组')
|
||||
expect(cells[9].text()).not.toBe('-', '京东更新时间格式化展示')
|
||||
expect(cells[10].text()).not.toBe('-', '同步时间格式化展示')
|
||||
expect(wrapper.find('.jd-thumb').exists()).toBe(true, '主图列渲染缩略图')
|
||||
})
|
||||
|
||||
it('分组读不到的那行展示「未分组」(不猜名字)', async () => {
|
||||
const wrapper = await mountWith({ items: [item(1, { groupId: null, groupName: '' })], total: 1, page: 1, pageSize: 20 })
|
||||
expect(wrapper.findAll('tbody tr')[0].findAll('td')[8].text()).toBe('未分组')
|
||||
})
|
||||
|
||||
it('筛选生效:店铺/编码/关键字/状态/分组/起止日期,查询回第一页重发', async () => {
|
||||
jdApi.fetchJdProductGroups.mockResolvedValue([{ id: 7, name: '甲组', sortOrder: 0, productCount: 3 }])
|
||||
const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 20 })
|
||||
|
||||
const textInputs = wrapper.findAll('.form-group input[type="text"]')
|
||||
expect(textInputs).toHaveLength(4, '店铺+商品编码+标题关键字+商品状态四个文本筛选框')
|
||||
await textInputs[0].setValue('旗舰自营店')
|
||||
await textInputs[1].setValue('JD-1001')
|
||||
await textInputs[2].setValue('保温杯')
|
||||
await textInputs[3].setValue('上架中')
|
||||
await wrapper.find('select').setValue('7')
|
||||
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 = jdApi.fetchJdProductList.mock.calls.at(-1)![0]
|
||||
expect(last).toMatchObject({
|
||||
shopName: '旗舰自营店',
|
||||
productCode: 'JD-1001',
|
||||
titleKeyword: '保温杯',
|
||||
saleStatus: '上架中',
|
||||
groupId: '7',
|
||||
syncedFrom: '2026-11-01',
|
||||
syncedTo: '2026-11-09',
|
||||
page: 1,
|
||||
companyId: 1,
|
||||
})
|
||||
})
|
||||
|
||||
it('分组下拉选中即查', async () => {
|
||||
jdApi.fetchJdProductGroups.mockResolvedValue([
|
||||
{ id: 7, name: '甲组', sortOrder: 0, productCount: 3 },
|
||||
{ id: 8, name: '乙组', sortOrder: 1, productCount: 1 },
|
||||
])
|
||||
const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 20 })
|
||||
expect(jdApi.fetchJdProductGroups).toHaveBeenCalledWith(1)
|
||||
|
||||
const groupSelect = wrapper.find('select')
|
||||
const optionTexts = groupSelect.findAll('option').map((o) => o.text())
|
||||
expect(optionTexts).toContain('全部分组')
|
||||
expect(optionTexts).toContain('甲组')
|
||||
|
||||
const callsBeforeSelect = jdApi.fetchJdProductList.mock.calls.length
|
||||
await groupSelect.setValue('7')
|
||||
await flushPromises()
|
||||
expect(jdApi.fetchJdProductList.mock.calls.length).toBe(callsBeforeSelect + 1, '选中分组必须立刻重查')
|
||||
expect(jdApi.fetchJdProductList.mock.calls.at(-1)![0]).toMatchObject({ groupId: '7', page: 1 })
|
||||
})
|
||||
|
||||
it('重置把筛选面回空并回第一页重取', async () => {
|
||||
const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 20 })
|
||||
const textInputs = wrapper.findAll('.form-group input[type="text"]')
|
||||
await textInputs[0].setValue('旗舰自营店')
|
||||
await textInputs[2].setValue('保温杯')
|
||||
const callsBefore = jdApi.fetchJdProductList.mock.calls.length
|
||||
|
||||
await wrapper.findAll('button').find((b) => b.text() === '重置')!.trigger('click')
|
||||
await flushPromises()
|
||||
|
||||
expect(jdApi.fetchJdProductList.mock.calls.length).toBeGreaterThan(callsBefore)
|
||||
const last = jdApi.fetchJdProductList.mock.calls.at(-1)![0]
|
||||
expect(last).toMatchObject({ shopName: '', titleKeyword: '', page: 1 }, '重置后筛选全空、回第一页')
|
||||
})
|
||||
|
||||
it('翻页与改每页条数都重发请求,改条数回第一页', async () => {
|
||||
const wrapper = await mountWith({ items: [item(1)], total: 42, page: 2, pageSize: 20 })
|
||||
|
||||
await wrapper.findAll('button').find((b) => b.text() === '下一页')!.trigger('click')
|
||||
await flushPromises()
|
||||
expect(jdApi.fetchJdProductList).toHaveBeenCalledTimes(2)
|
||||
expect(jdApi.fetchJdProductList.mock.calls[1][0].page).toBe(3)
|
||||
|
||||
const sizeSelect = wrapper.find('.page-size-select')
|
||||
await sizeSelect.setValue('50')
|
||||
await flushPromises()
|
||||
const last = jdApi.fetchJdProductList.mock.calls.at(-1)![0]
|
||||
expect(last.pageSize).toBe(50)
|
||||
expect(last.page).toBe(1, '改每页条数后回第一页')
|
||||
})
|
||||
|
||||
it('导出=当前过滤面全量(含公司 tab 与分组),成功反馈点名落盘文件', async () => {
|
||||
const createObjectURL = vi.fn(() => 'blob:mock-url')
|
||||
const revokeObjectURL = vi.fn()
|
||||
const urlRecord = URL as unknown as Record<string, unknown>
|
||||
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(() => {})
|
||||
jdApi.exportJdProductList.mockImplementation(() =>
|
||||
Promise.resolve({ blob: new Blob(['xlsx']), filename: '京东数据库-20261114090000.xlsx' }),
|
||||
)
|
||||
|
||||
try {
|
||||
const wrapper = await mountWith({ items: [item(1)], total: 1, page: 1, pageSize: 20 })
|
||||
const btn = wrapper.findAll('button').find((b) => b.text() === '导出')
|
||||
expect(btn, '筛选行必须有「导出」按钮').toBeTruthy()
|
||||
await btn!.trigger('click')
|
||||
await flushPromises()
|
||||
await flushPromises()
|
||||
|
||||
expect(jdApi.exportJdProductList).toHaveBeenCalledTimes(1)
|
||||
const call = jdApi.exportJdProductList.mock.calls[0][0]
|
||||
expect(call).toMatchObject({ shopName: '', groupId: '', companyId: 1 })
|
||||
expect(createObjectURL).toHaveBeenCalledTimes(1)
|
||||
expect(clickSpy).toHaveBeenCalled()
|
||||
expect(document.body.textContent).toContain('已导出', '成功反馈点名落盘文件')
|
||||
} finally {
|
||||
Object.defineProperty(URL, 'createObjectURL', { value: undefined, configurable: true, writable: true })
|
||||
Object.defineProperty(URL, 'revokeObjectURL', { value: undefined, configurable: true, writable: true })
|
||||
clickSpy.mockRestore()
|
||||
}
|
||||
})
|
||||
|
||||
it('导出失败把服务端业务文案弹出来,不许静默;在途标记复位', async () => {
|
||||
jdApi.exportJdProductList.mockImplementation(() => Promise.reject(new Error('京东商品共 80001 行,超过导出上限 50000 行,请缩小筛选范围后重试')))
|
||||
const wrapper = await mountWith({ items: [item(1)], total: 1, page: 1, pageSize: 20 })
|
||||
await wrapper.findAll('button').find((b) => b.text() === '导出')!.trigger('click')
|
||||
await flushPromises()
|
||||
await flushPromises()
|
||||
expect(document.body.textContent).toContain('超过导出上限')
|
||||
const btn = wrapper.findAll('button').find((b) => b.text() === '导出')
|
||||
expect(btn!.text()).toBe('导出', '失败后在途标记必须复位')
|
||||
expect((btn!.element as HTMLButtonElement).disabled).toBe(false)
|
||||
})
|
||||
|
||||
it('点行打开详情抽屉:按 (id, companyId) 取详情,attrGroups 分块渲染', async () => {
|
||||
jdApi.fetchJdProductDetail.mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
item: item(1),
|
||||
attrGroups: [{ groupName: '规格', attrs: [{ key: 'capacity', label: '容量', value: '500ml' }] }],
|
||||
images: [{ imageType: 'MAIN', attrName: '', sortNo: 1, url: 'https://img.example/main-1.jpg', byteSize: null, width: 800, height: 800 }],
|
||||
attrJsonRaw: '',
|
||||
}),
|
||||
)
|
||||
const wrapper = await mountWith({ items: [item(1)], total: 1, page: 1, pageSize: 20 })
|
||||
expect(wrapper.find('.drawer-mask').exists()).toBe(false, '初始不显示抽屉')
|
||||
|
||||
await wrapper.findAll('tbody tr')[0].trigger('click')
|
||||
await flushPromises()
|
||||
await flushPromises()
|
||||
|
||||
expect(wrapper.find('.drawer-mask').exists()).toBe(true)
|
||||
expect(jdApi.fetchJdProductDetail).toHaveBeenCalledTimes(1)
|
||||
expect(jdApi.fetchJdProductDetail).toHaveBeenCalledWith(1, 1)
|
||||
expect(wrapper.find('.drawer-mask').text()).toContain('基本信息')
|
||||
expect(wrapper.find('.drawer-mask').text()).toContain('规格')
|
||||
expect(wrapper.find('.drawer-mask').text()).toContain('容量')
|
||||
expect(wrapper.find('.drawer-mask').text()).toContain('500ml')
|
||||
expect(wrapper.find('.drawer-mask').text()).toContain('主图')
|
||||
})
|
||||
|
||||
it('管理分组弹窗:打开即拉分组列表,变更后刷新分组下拉与列表', async () => {
|
||||
const wrapper = await mountWith({ items: [item(1)], total: 1, page: 1, pageSize: 20 })
|
||||
const callsBefore = jdApi.fetchJdProductList.mock.calls.length
|
||||
|
||||
await wrapper.findAll('button').find((b) => b.text() === '管理分组')!.trigger('click')
|
||||
await flushPromises()
|
||||
await flushPromises()
|
||||
|
||||
expect(wrapper.find('.group-modal').exists()).toBe(true)
|
||||
// 打开弹窗 = 拉一次分组列表(第 2 次:首屏已拉过一次)
|
||||
expect(jdApi.fetchJdProductGroups).toHaveBeenCalledTimes(2)
|
||||
expect(jdApi.fetchJdProductGroups).toHaveBeenLastCalledWith(1)
|
||||
|
||||
// 模拟分组管理里删了一个分组:emit changed → 页面刷新分组下拉与列表(回第一页)
|
||||
wrapper.findComponent(JdProductGroupDialog).vm.$emit('changed')
|
||||
await flushPromises()
|
||||
await flushPromises()
|
||||
expect(jdApi.fetchJdProductList.mock.calls.length).toBeGreaterThan(callsBefore)
|
||||
expect(jdApi.fetchJdProductList.mock.calls.at(-1)![0]).toMatchObject({ page: 1, companyId: 1 })
|
||||
})
|
||||
|
||||
it('切公司 Tab 重取并清掉上一家的筛选(company-tab-wiring 同口径)', async () => {
|
||||
jdApi.fetchJdProductGroups.mockResolvedValue([{ id: 7, name: '甲组', sortOrder: 0, productCount: 3 }])
|
||||
const wrapper = await mountWith({ items: [item(1)], total: 1, page: 1, pageSize: 20 })
|
||||
const textInputs = wrapper.findAll('.form-group input[type="text"]')
|
||||
await textInputs[0].setValue('旗舰自营店')
|
||||
await textInputs[2].setValue('保温杯')
|
||||
await wrapper.find('select').setValue('7')
|
||||
const callsBefore = jdApi.fetchJdProductList.mock.calls.length
|
||||
|
||||
useCompanyTabStore().setActiveId(2)
|
||||
await flushPromises()
|
||||
await flushPromises()
|
||||
|
||||
expect(jdApi.fetchJdProductList.mock.calls.length).toBeGreaterThan(callsBefore)
|
||||
const last = jdApi.fetchJdProductList.mock.calls.at(-1)![0]
|
||||
expect(last.companyId).toBe(2)
|
||||
expect(last.shopName).toBe('', '切公司必须清掉上一家的店铺名')
|
||||
expect(last.titleKeyword).toBe('', '切公司必须清掉上一家的标题关键字')
|
||||
expect(last.groupId).toBe('', '切公司必须清掉上一家选的分组')
|
||||
expect(last.page).toBe(1)
|
||||
expect(jdApi.fetchJdProductGroups).toHaveBeenLastCalledWith(2)
|
||||
})
|
||||
|
||||
it('空态与加载失败条(失败给重试,不把整页打死)', async () => {
|
||||
const empty = await mountWith({ items: [], total: 0, page: 1, pageSize: 20 })
|
||||
const tip = empty.find('.empty-tip')
|
||||
expect(tip.exists()).toBe(true)
|
||||
expect(tip.text()).toContain('暂无京东商品数据')
|
||||
empty.unmount()
|
||||
|
||||
jdApi.fetchJdProductList.mockRejectedValue(new Error('上游 502'))
|
||||
const failed = mount(JdDatabasePage, { global: { plugins: [pinia] } })
|
||||
await flushPromises()
|
||||
await flushPromises()
|
||||
expect(failed.find('.load-failure-bar').exists()).toBe(true)
|
||||
expect(failed.find('.load-failure-bar').text()).toContain('京东商品列表加载失败')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user