200 lines
8.7 KiB
TypeScript
200 lines
8.7 KiB
TypeScript
/**
|
||
* 京东数据库页纯逻辑契约(v1.4 Phase 5)—— node --test 直跑(纯函数零依赖)。
|
||
*
|
||
* 不用 readFileSync/readSource/globSync 读点:source-pin-ratchet 对台账外新文件带读点即红,
|
||
* 本文件只 import 被测纯函数。
|
||
*
|
||
* 覆盖:
|
||
* 1. **snake 参数形状**:toJdProductListParams 产出 `page`/`page_size`(默认 20、上限 100)、
|
||
* `product_code`/`title_keyword`/`group_id`/`synced_from`/`synced_to`
|
||
* (ISO yyyy-MM-dd;空白不下发;groupId 空串→不带、有值→snake group_id);分页钳制(铁律 15)。
|
||
* 商品状态筛选与导出参数口已随 2026-11-15 客户口径下线,不再出现在参数面;
|
||
* 店铺筛选同日客户口径「店铺不需要」下线(shop_name 也不在参数面)。
|
||
* 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,
|
||
toJdProductListParams,
|
||
} from '../src/pages/jd/jd-product-filter.ts'
|
||
import {
|
||
jdCategoryNavChildren,
|
||
jdCategoryNavRoots,
|
||
} from '../src/pages/jd/jd-category-tree.ts'
|
||
import {
|
||
jdPriceRange,
|
||
parseJdProductDetail,
|
||
parseJdProductPage,
|
||
toJdProductItem,
|
||
} from '../src/pages/jd/jd-product-model.ts'
|
||
|
||
test('类目级联静态导航树——服饰内衣二级与京麦面板样本逐字同源,一级集非空且无重名', () => {
|
||
// 京麦面板真机样本(2026-11-16 客户截图):服饰内衣下就是这五个二级,改名即红。
|
||
assert.deepEqual(jdCategoryNavChildren('服饰内衣'), ['服饰配件', '内衣', '女装', '男装', '定制服装/工装'])
|
||
assert.deepEqual(jdCategoryNavChildren('不存在的类目'), [], '未知一级回落空子级(面板不出第二栏)')
|
||
|
||
const roots = jdCategoryNavRoots()
|
||
assert.ok(roots.length >= 20, `一级导航 ${roots.length} 个,常用集不应被清空`)
|
||
assert.equal(new Set(roots).size, roots.length, '一级类目名不得重复')
|
||
assert.equal(roots[0], '服饰内衣', '服饰内衣置首(客户商品全在服饰类目)')
|
||
for (const root of roots) {
|
||
const children = jdCategoryNavChildren(root)
|
||
assert.equal(new Set(children).size, children.length, `${root} 的二级不得重复`)
|
||
}
|
||
})
|
||
|
||
test('京东列表参数——snake 形状:groupId 空→不带、有值→group_id,日期空白/非法不下发', () => {
|
||
const full = toJdProductListParams(
|
||
{
|
||
productCode: ' JD-10086 ',
|
||
titleKeyword: ' 保温杯 ',
|
||
groupId: ' 7 ',
|
||
syncedFrom: '2026-11-01',
|
||
syncedTo: '2026-11-09',
|
||
},
|
||
2,
|
||
20,
|
||
)
|
||
assert.deepEqual(full, {
|
||
page: 2,
|
||
page_size: 20,
|
||
product_code: 'JD-10086',
|
||
title_keyword: '保温杯',
|
||
group_id: 7,
|
||
synced_from: '2026-11-01',
|
||
synced_to: '2026-11-09',
|
||
})
|
||
assert.equal('shop_name' in full, false, '店铺筛选已下线,参数面不再出现 shop_name')
|
||
|
||
// 全空态只有分页两项;脏值(非数字分组、非法日期)同罪不下发。
|
||
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(
|
||
{ productCode: '', titleKeyword: '', groupId: 'abc', syncedFrom: '2026/11/01', syncedTo: 'not-a-date' },
|
||
1,
|
||
20,
|
||
)
|
||
assert.deepEqual(dirty, { page: 1, page_size: 20 }, '脏值不下发')
|
||
assert.equal('sale_status' in full, false, '商品状态筛选已下线,参数面不再出现 sale_status')
|
||
})
|
||
|
||
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, '甲组')
|
||
// 2026-11-16 价格改走 decimalText(数值/字符串都收)后同口径去尾零,与详情 SKU 价一致。
|
||
assert.equal(jdPriceRange(row), '12.9 ~ 15.9', 'min/max 都有给区间(去尾零)')
|
||
assert.equal(jdPriceRange({ priceMin: '12.90', priceMax: '' }), '12.9', '只空一侧给单值')
|
||
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)
|
||
})
|